jruby-jruby-openssl 0.5.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. data/History.txt +34 -0
  2. data/License.txt +30 -0
  3. data/README.txt +24 -0
  4. data/lib/bcmail-jdk14-139.jar +0 -0
  5. data/lib/bcprov-jdk14-139.jar +0 -0
  6. data/lib/jopenssl.jar +0 -0
  7. data/lib/jopenssl/version.rb +5 -0
  8. data/lib/openssl.rb +24 -0
  9. data/lib/openssl/bn.rb +35 -0
  10. data/lib/openssl/buffering.rb +239 -0
  11. data/lib/openssl/cipher.rb +58 -0
  12. data/lib/openssl/digest.rb +48 -0
  13. data/lib/openssl/dummy.rb +34 -0
  14. data/lib/openssl/dummyssl.rb +13 -0
  15. data/lib/openssl/ssl.rb +135 -0
  16. data/lib/openssl/x509.rb +154 -0
  17. data/test/fixture/cacert.pem +23 -0
  18. data/test/fixture/cert_localhost.pem +19 -0
  19. data/test/fixture/localhost_keypair.pem +18 -0
  20. data/test/openssl/ssl_server.rb +99 -0
  21. data/test/openssl/test_asn1.rb +199 -0
  22. data/test/openssl/test_cipher.rb +174 -0
  23. data/test/openssl/test_digest.rb +88 -0
  24. data/test/openssl/test_hmac.rb +44 -0
  25. data/test/openssl/test_ns_spki.rb +69 -0
  26. data/test/openssl/test_pair.rb +149 -0
  27. data/test/openssl/test_pkcs7.rb +159 -0
  28. data/test/openssl/test_pkey_rsa.rb +49 -0
  29. data/test/openssl/test_ssl.rb +307 -0
  30. data/test/openssl/test_x509cert.rb +236 -0
  31. data/test/openssl/test_x509crl.rb +234 -0
  32. data/test/openssl/test_x509ext.rb +74 -0
  33. data/test/openssl/test_x509name.rb +265 -0
  34. data/test/openssl/test_x509req.rb +178 -0
  35. data/test/openssl/test_x509store.rb +245 -0
  36. data/test/openssl/utils.rb +135 -0
  37. data/test/pkcs7_mime_enveloped.message +19 -0
  38. data/test/pkcs7_mime_signed.message +30 -0
  39. data/test/pkcs7_multipart_signed.message +45 -0
  40. data/test/ref/a.out +0 -0
  41. data/test/ref/compile.rb +8 -0
  42. data/test/ref/pkcs1 +0 -0
  43. data/test/ref/pkcs1.c +21 -0
  44. data/test/test_cipher.rb +81 -0
  45. data/test/test_integration.rb +100 -0
  46. data/test/test_java.rb +98 -0
  47. data/test/test_java_attribute.rb +25 -0
  48. data/test/test_java_bio.rb +42 -0
  49. data/test/test_java_mime.rb +173 -0
  50. data/test/test_java_pkcs7.rb +769 -0
  51. data/test/test_java_smime.rb +177 -0
  52. data/test/test_openssl.rb +34 -0
  53. data/test/test_openssl_x509.rb +34 -0
  54. data/test/test_pkey.rb +46 -0
  55. data/test/ut_eof.rb +128 -0
  56. metadata +120 -0
@@ -0,0 +1,173 @@
1
+ module PKCS7Test
2
+ class TestJavaMime < Test::Unit::TestCase
3
+ def test_find_header_returns_null_on_nonexisting_header
4
+ headers = []
5
+ assert_nil Mime::DEFAULT.find_header(headers, "foo")
6
+
7
+ headers = [MimeHeader.new("blarg", "bluff")]
8
+ assert_nil Mime::DEFAULT.find_header(headers, "foo")
9
+ end
10
+
11
+ def test_find_header_returns_the_header_with_the_same_name
12
+ hdr = MimeHeader.new("one", "two")
13
+ assert_equal hdr, Mime::DEFAULT.find_header([hdr], "one")
14
+ end
15
+
16
+ def test_find_param_returns_null_on_nonexisting_param
17
+ assert_nil Mime::DEFAULT.find_param(MimeHeader.new("one", "two", []), "foo")
18
+ assert_nil Mime::DEFAULT.find_param(MimeHeader.new("one", "two", [MimeParam.new("hi", "ho")]), "foo")
19
+ end
20
+
21
+ def test_find_param_returns_the_param_with_the_same_name
22
+ par = MimeParam.new("hox", "box")
23
+ hdr = MimeHeader.new("one", "two", [par])
24
+ assert_equal par, Mime::DEFAULT.find_param(hdr, "hox")
25
+ end
26
+
27
+ def test_simple_parse_headers
28
+ bio = BIO::from_string("Foo: bar")
29
+ result = Mime::DEFAULT.parse_headers(bio)
30
+ assert_equal 1, result.size
31
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
32
+ assert_equal "foo", result[0].name
33
+ end
34
+
35
+ def test_simple_parse_headers2
36
+ bio = BIO::from_string("Foo:bar")
37
+ result = Mime::DEFAULT.parse_headers(bio)
38
+ assert_equal 1, result.size
39
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
40
+ assert_equal "foo", result[0].name
41
+ end
42
+
43
+ def test_simple_parse_headers3
44
+ bio = BIO::from_string("Foo: bar")
45
+ result = Mime::DEFAULT.parse_headers(bio)
46
+ assert_equal 1, result.size
47
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
48
+ assert_equal "foo", result[0].name
49
+ end
50
+
51
+ def test_simple_parse_headers4
52
+ bio = BIO::from_string("Foo: bar\n")
53
+ result = Mime::DEFAULT.parse_headers(bio)
54
+ assert_equal 1, result.size
55
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
56
+ assert_equal "foo", result[0].name
57
+ end
58
+
59
+ def test_simple_parse_headers5
60
+ bio = BIO::from_string(" Foo : bar \n")
61
+ result = Mime::DEFAULT.parse_headers(bio)
62
+ assert_equal 1, result.size
63
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
64
+ assert_equal "foo", result[0].name
65
+ end
66
+
67
+
68
+ def test_simple_parse_headers6
69
+ bio = BIO::from_string("Foo: bar;\n")
70
+ result = Mime::DEFAULT.parse_headers(bio)
71
+ assert_equal 1, result.size
72
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
73
+ assert_equal "foo", result[0].name
74
+ end
75
+
76
+ def test_simple_parse_headers7
77
+ bio = BIO::from_string("Foo: bar;\nFlurg: blarg")
78
+ result = Mime::DEFAULT.parse_headers(bio)
79
+ assert_equal 2, result.size
80
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
81
+ assert_equal MimeHeader.new("Flurg", "blarg"), result[1]
82
+ assert_equal "foo", result[0].name
83
+ assert_equal "flurg", result[1].name
84
+ end
85
+
86
+ def test_simple_parse_headers_quotes
87
+ bio = BIO::from_string("Foo: \"bar\"")
88
+ result = Mime::DEFAULT.parse_headers(bio)
89
+ assert_equal 1, result.size
90
+ assert_equal MimeHeader.new("Foo", "bar"), result[0]
91
+ assert_equal "foo", result[0].name
92
+ end
93
+
94
+ def test_simple_parse_headers_comment
95
+ bio = BIO::from_string("Foo: (this is the right thing)ba(and this is the wrong one)r")
96
+ result = Mime::DEFAULT.parse_headers(bio)
97
+ assert_equal 1, result.size
98
+ assert_equal MimeHeader.new("Foo", "(this is the right thing)ba(and this is the wrong one)r"), result[0]
99
+ assert_equal "foo", result[0].name
100
+ end
101
+
102
+ def test_parse_headers_with_param
103
+ bio = BIO::from_string("Content-Type: Multipart/Related; boundary=MIME_boundary; type=text/xml")
104
+ result = Mime::DEFAULT.parse_headers(bio)
105
+ assert_equal 1, result.size
106
+ header = result[0]
107
+ assert_equal "content-type", header.name
108
+ assert_equal "multipart/related", header.value
109
+ assert_equal [MimeParam.new("boundary","MIME_boundary"),
110
+ MimeParam.new("type","text/xml")], header.params.to_a
111
+ end
112
+
113
+ def test_parse_headers_with_param_newline
114
+ bio = BIO::from_string("Content-Type: Multipart/Related\n boundary=MIME_boundary; type=text/xml")
115
+ result = Mime::DEFAULT.parse_headers(bio)
116
+ assert_equal 1, result.size
117
+ header = result[0]
118
+ assert_equal "content-type", header.name
119
+ assert_equal "multipart/related", header.value
120
+ assert_equal [MimeParam.new("boundary","MIME_boundary"),
121
+ MimeParam.new("type","text/xml")], header.params.to_a
122
+ end
123
+
124
+ def test_parse_headers_with_param_newline_and_semicolon
125
+ bio = BIO::from_string("Content-Type: Multipart/Related;\n boundary=MIME_boundary;\n Type=text/xml")
126
+ result = Mime::DEFAULT.parse_headers(bio)
127
+ assert_equal 1, result.size
128
+ header = result[0]
129
+ assert_equal "content-type", header.name
130
+ assert_equal "multipart/related", header.value
131
+ assert_equal [MimeParam.new("boundary","MIME_boundary"),
132
+ MimeParam.new("type","text/xml")], header.params.to_a
133
+ end
134
+
135
+ def test_advanced_mime_message
136
+ bio = BIO::from_string(MultipartSignedString)
137
+ result = Mime::DEFAULT.parse_headers(bio)
138
+
139
+ assert_equal "mime-version", result[0].name
140
+ assert_equal "1.0", result[0].value
141
+
142
+ assert_equal "to", result[1].name
143
+ assert_equal "user2@examples.com", result[1].value
144
+
145
+ assert_equal "from", result[2].name
146
+ assert_equal "alicedss@examples.com", result[2].value
147
+
148
+ assert_equal "subject", result[3].name
149
+ assert_equal "example 4.8", result[3].value
150
+
151
+ assert_equal "message-id", result[4].name
152
+ assert_equal "<020906002550300.249@examples.com>", result[4].value
153
+
154
+ assert_equal "date", result[5].name
155
+ assert_equal "fri, 06 sep 2002 00:25:21 -0300", result[5].value
156
+
157
+ assert_equal "content-type", result[6].name
158
+ assert_equal "multipart/signed", result[6].value
159
+
160
+ assert_equal "micalg", result[6].params[0].param_name
161
+ assert_equal "SHA1", result[6].params[0].param_value
162
+
163
+ assert_equal "boundary", result[6].params[1].param_name
164
+ assert_equal "----=_NextBoundry____Fri,_06_Sep_2002_00:25:21", result[6].params[1].param_value
165
+
166
+ assert_equal "protocol", result[6].params[2].param_name
167
+ assert_equal "application/pkcs7-signature", result[6].params[2].param_value
168
+
169
+ assert_equal 3, result[6].params.length
170
+ assert_equal 7, result.length
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,769 @@
1
+ module PKCS7Test
2
+ class TestJavaPKCS7 < Test::Unit::TestCase
3
+ def test_is_signed
4
+ p7 = PKCS7.new
5
+ p7.type = ASN1Registry::NID_pkcs7_signed
6
+ assert p7.signed?
7
+ assert !p7.encrypted?
8
+ assert !p7.enveloped?
9
+ assert !p7.signed_and_enveloped?
10
+ assert !p7.data?
11
+ assert !p7.digest?
12
+ end
13
+
14
+ def test_is_encrypted
15
+ p7 = PKCS7.new
16
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
17
+ assert !p7.signed?
18
+ assert p7.encrypted?
19
+ assert !p7.enveloped?
20
+ assert !p7.signed_and_enveloped?
21
+ assert !p7.data?
22
+ assert !p7.digest?
23
+ end
24
+
25
+ def test_is_enveloped
26
+ p7 = PKCS7.new
27
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
28
+ assert !p7.signed?
29
+ assert !p7.encrypted?
30
+ assert p7.enveloped?
31
+ assert !p7.signed_and_enveloped?
32
+ assert !p7.data?
33
+ assert !p7.digest?
34
+ end
35
+
36
+ def test_is_signed_and_enveloped
37
+ p7 = PKCS7.new
38
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
39
+ assert !p7.signed?
40
+ assert !p7.encrypted?
41
+ assert !p7.enveloped?
42
+ assert p7.signed_and_enveloped?
43
+ assert !p7.data?
44
+ assert !p7.digest?
45
+ end
46
+
47
+ def test_is_data
48
+ p7 = PKCS7.new
49
+ p7.type = ASN1Registry::NID_pkcs7_data
50
+ assert !p7.signed?
51
+ assert !p7.encrypted?
52
+ assert !p7.enveloped?
53
+ assert !p7.signed_and_enveloped?
54
+ assert p7.data?
55
+ assert !p7.digest?
56
+ end
57
+
58
+ def test_is_digest
59
+ p7 = PKCS7.new
60
+ p7.type = ASN1Registry::NID_pkcs7_digest
61
+ assert !p7.signed?
62
+ assert !p7.encrypted?
63
+ assert !p7.enveloped?
64
+ assert !p7.signed_and_enveloped?
65
+ assert !p7.data?
66
+ assert p7.digest?
67
+ end
68
+
69
+ def test_set_detached
70
+ p7 = PKCS7.new
71
+ p7.type = ASN1Registry::NID_pkcs7_signed
72
+
73
+ sign = Signed.new
74
+ p7.sign = sign
75
+
76
+ test_p7 = PKCS7.new
77
+ test_p7.type = ASN1Registry::NID_pkcs7_data
78
+ test_p7.data = ASN1::OctetString.new("foo".to_java_bytes)
79
+ sign.contents = test_p7
80
+
81
+ p7.detached = 2
82
+ assert_equal 1, p7.get_detached
83
+ assert_equal nil, test_p7.get_data
84
+ end
85
+
86
+ def test_set_not_detached
87
+ p7 = PKCS7.new
88
+ p7.type = ASN1Registry::NID_pkcs7_signed
89
+
90
+ sign = Signed.new
91
+ p7.sign = sign
92
+
93
+ test_p7 = PKCS7.new
94
+ test_p7.type = ASN1Registry::NID_pkcs7_data
95
+ data = ASN1::OctetString.new("foo".to_java_bytes)
96
+ test_p7.data = data
97
+ sign.contents = test_p7
98
+
99
+ p7.detached = 0
100
+ assert_equal 0, p7.get_detached
101
+ assert_equal data, test_p7.get_data
102
+ end
103
+
104
+ def test_is_detached
105
+ p7 = PKCS7.new
106
+ p7.type = ASN1Registry::NID_pkcs7_signed
107
+
108
+ sign = Signed.new
109
+ p7.sign = sign
110
+
111
+ test_p7 = PKCS7.new
112
+ test_p7.type = ASN1Registry::NID_pkcs7_data
113
+ data = ASN1::OctetString.new("foo".to_java_bytes)
114
+ test_p7.data = data
115
+ sign.contents = test_p7
116
+
117
+ p7.detached = 1
118
+ assert p7.detached?
119
+ end
120
+
121
+ def test_is_detached_with_wrong_type
122
+ p7 = PKCS7.new
123
+ p7.type = ASN1Registry::NID_pkcs7_data
124
+
125
+ assert !p7.detached?
126
+ end
127
+
128
+ def _test_encrypt_generates_enveloped_PKCS7_object
129
+ p7 = PKCS7.encrypt([], "".to_java_bytes, nil, 0)
130
+ assert !p7.signed?
131
+ assert !p7.encrypted?
132
+ assert p7.enveloped?
133
+ assert !p7.signed_and_enveloped?
134
+ assert !p7.data?
135
+ assert !p7.digest?
136
+ end
137
+
138
+ def test_set_type_throws_exception_on_wrong_argument
139
+ assert_raises NativeException do
140
+ # 42 is a value that is not one of the valid NID's for type
141
+ PKCS7.new.type = 42
142
+ end
143
+ end
144
+
145
+ def test_set_type_signed
146
+ p7 = PKCS7.new
147
+ p7.type = ASN1Registry::NID_pkcs7_signed
148
+
149
+ assert p7.signed?
150
+ assert_equal 1, p7.get_sign.version
151
+
152
+ assert_nil p7.get_data
153
+ assert_nil p7.get_enveloped
154
+ assert_nil p7.get_signed_and_enveloped
155
+ assert_nil p7.get_digest
156
+ assert_nil p7.get_encrypted
157
+ assert_nil p7.get_other
158
+ end
159
+
160
+ def test_set_type_data
161
+ p7 = PKCS7.new
162
+ p7.type = ASN1Registry::NID_pkcs7_data
163
+
164
+ assert p7.data?
165
+ assert_equal ASN1::OctetString.new("".to_java_bytes), p7.get_data
166
+
167
+ assert_nil p7.get_sign
168
+ assert_nil p7.get_enveloped
169
+ assert_nil p7.get_signed_and_enveloped
170
+ assert_nil p7.get_digest
171
+ assert_nil p7.get_encrypted
172
+ assert_nil p7.get_other
173
+ end
174
+
175
+ def test_set_type_signed_and_enveloped
176
+ p7 = PKCS7.new
177
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
178
+
179
+ assert p7.signed_and_enveloped?
180
+ assert_equal 1, p7.get_signed_and_enveloped.version
181
+ assert_equal ASN1Registry::NID_pkcs7_data, p7.get_signed_and_enveloped.enc_data.content_type
182
+
183
+ assert_nil p7.get_sign
184
+ assert_nil p7.get_enveloped
185
+ assert_nil p7.get_data
186
+ assert_nil p7.get_digest
187
+ assert_nil p7.get_encrypted
188
+ assert_nil p7.get_other
189
+ end
190
+
191
+ def test_set_type_enveloped
192
+ p7 = PKCS7.new
193
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
194
+
195
+ assert p7.enveloped?
196
+ assert_equal 0, p7.get_enveloped.version
197
+ assert_equal ASN1Registry::NID_pkcs7_data, p7.get_enveloped.enc_data.content_type
198
+
199
+ assert_nil p7.get_sign
200
+ assert_nil p7.get_signed_and_enveloped
201
+ assert_nil p7.get_data
202
+ assert_nil p7.get_digest
203
+ assert_nil p7.get_encrypted
204
+ assert_nil p7.get_other
205
+ end
206
+
207
+ def test_set_type_encrypted
208
+ p7 = PKCS7.new
209
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
210
+
211
+ assert p7.encrypted?
212
+ assert_equal 0, p7.get_encrypted.version
213
+ assert_equal ASN1Registry::NID_pkcs7_data, p7.get_encrypted.enc_data.content_type
214
+
215
+ assert_nil p7.get_sign
216
+ assert_nil p7.get_signed_and_enveloped
217
+ assert_nil p7.get_data
218
+ assert_nil p7.get_digest
219
+ assert_nil p7.get_enveloped
220
+ assert_nil p7.get_other
221
+ end
222
+
223
+ def test_set_type_digest
224
+ p7 = PKCS7.new
225
+ p7.type = ASN1Registry::NID_pkcs7_digest
226
+
227
+ assert p7.digest?
228
+ assert_equal 0, p7.get_digest.version
229
+
230
+ assert_nil p7.get_sign
231
+ assert_nil p7.get_signed_and_enveloped
232
+ assert_nil p7.get_data
233
+ assert_nil p7.get_encrypted
234
+ assert_nil p7.get_enveloped
235
+ assert_nil p7.get_other
236
+ end
237
+
238
+ def test_set_cipher_on_non_enveloped_object
239
+ p7 = PKCS7.new
240
+ p7.type = ASN1Registry::NID_pkcs7_digest
241
+
242
+ assert_raises NativeException do
243
+ p7.cipher = nil
244
+ end
245
+
246
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
247
+
248
+ assert_raises NativeException do
249
+ p7.cipher = nil
250
+ end
251
+
252
+ p7.type = ASN1Registry::NID_pkcs7_data
253
+
254
+ assert_raises NativeException do
255
+ p7.cipher = nil
256
+ end
257
+
258
+ p7.type = ASN1Registry::NID_pkcs7_signed
259
+
260
+ assert_raises NativeException do
261
+ p7.cipher = nil
262
+ end
263
+ end
264
+
265
+ def test_set_cipher_on_enveloped_object
266
+ p7 = PKCS7.new
267
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
268
+
269
+ cipher = javax.crypto.Cipher.getInstance("RSA")
270
+
271
+ p7.cipher = cipher
272
+
273
+ assert_equal cipher, p7.get_enveloped.enc_data.cipher
274
+ end
275
+
276
+
277
+ def test_set_cipher_on_signedAndEnveloped_object
278
+ p7 = PKCS7.new
279
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
280
+
281
+ cipher = javax.crypto.Cipher.getInstance("RSA")
282
+
283
+ p7.cipher = cipher
284
+
285
+ assert_equal cipher, p7.get_signed_and_enveloped.enc_data.cipher
286
+ end
287
+
288
+ def test_add_recipient_info_to_something_that_cant_have_recipients
289
+ p7 = PKCS7.new
290
+ p7.type = ASN1Registry::NID_pkcs7_signed
291
+ assert_raises NativeException do
292
+ p7.add_recipient(X509Cert)
293
+ end
294
+
295
+ p7 = PKCS7.new
296
+ p7.type = ASN1Registry::NID_pkcs7_data
297
+ assert_raises NativeException do
298
+ p7.add_recipient(X509Cert)
299
+ end
300
+
301
+ p7 = PKCS7.new
302
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
303
+ assert_raises NativeException do
304
+ p7.add_recipient(X509Cert)
305
+ end
306
+
307
+ p7 = PKCS7.new
308
+ p7.type = ASN1Registry::NID_pkcs7_digest
309
+ assert_raises NativeException do
310
+ p7.add_recipient(X509Cert)
311
+ end
312
+ end
313
+
314
+ def test_add_recipient_info_to_enveloped_should_add_that_to_stack
315
+ p7 = PKCS7.new
316
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
317
+
318
+ ri = p7.add_recipient(X509Cert)
319
+
320
+ assert_equal 1, p7.get_enveloped.recipient_info.size
321
+ assert_equal ri, p7.get_enveloped.recipient_info.iterator.next
322
+ end
323
+
324
+
325
+ def test_add_recipient_info_to_signedAndEnveloped_should_add_that_to_stack
326
+ p7 = PKCS7.new
327
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
328
+
329
+ ri = p7.add_recipient(X509Cert)
330
+
331
+ assert_equal 1, p7.get_signed_and_enveloped.recipient_info.size
332
+ assert_equal ri, p7.get_signed_and_enveloped.recipient_info.iterator.next
333
+ end
334
+
335
+ def test_add_signer_to_something_that_cant_have_signers
336
+ p7 = PKCS7.new
337
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
338
+ assert_raises NativeException do
339
+ p7.add_signer(SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil))
340
+ end
341
+
342
+ p7 = PKCS7.new
343
+ p7.type = ASN1Registry::NID_pkcs7_data
344
+ assert_raises NativeException do
345
+ p7.add_signer(SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil))
346
+ end
347
+
348
+ p7 = PKCS7.new
349
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
350
+ assert_raises NativeException do
351
+ p7.add_signer(SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil))
352
+ end
353
+
354
+ p7 = PKCS7.new
355
+ p7.type = ASN1Registry::NID_pkcs7_digest
356
+ assert_raises NativeException do
357
+ p7.add_signer(SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil))
358
+ end
359
+ end
360
+
361
+ def test_add_signer_to_signed_should_add_that_to_stack
362
+ p7 = PKCS7.new
363
+ p7.type = ASN1Registry::NID_pkcs7_signed
364
+
365
+ si = SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil)
366
+ p7.add_signer(si)
367
+
368
+ assert_equal 1, p7.get_sign.signer_info.size
369
+ assert_equal si, p7.get_sign.signer_info.iterator.next
370
+ end
371
+
372
+
373
+ def test_add_signer_to_signedAndEnveloped_should_add_that_to_stack
374
+ p7 = PKCS7.new
375
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
376
+
377
+ si = SignerInfoWithPkey.new(nil, nil, nil, nil, nil, nil, nil)
378
+ p7.add_signer(si)
379
+
380
+ assert_equal 1, p7.get_signed_and_enveloped.signer_info.size
381
+ assert_equal si, p7.get_signed_and_enveloped.signer_info.iterator.next
382
+ end
383
+
384
+ def create_signer_info_with_algo(algo)
385
+ md5 = AlgorithmIdentifier.new(ASN1Registry.nid2obj(4))
386
+ SignerInfoWithPkey.new(DERInteger.new(BigInteger::ONE),
387
+ IssuerAndSerialNumber.new(X509Name.new("C=SE"), DERInteger.new(BigInteger::ONE)),
388
+ algo,
389
+ DERSet.new,
390
+ md5,
391
+ DEROctetString.new([].to_java(:byte)),
392
+ DERSet.new)
393
+ end
394
+
395
+ def test_add_signer_to_signed_with_new_algo_should_add_that_algo_to_the_algo_list
396
+ p7 = PKCS7.new
397
+ p7.type = ASN1Registry::NID_pkcs7_signed
398
+
399
+ # YES, these numbers are correct. Don't change them. They are OpenSSL internal NIDs
400
+ md5 = AlgorithmIdentifier.new(ASN1Registry.nid2obj(4))
401
+ md4 = AlgorithmIdentifier.new(ASN1Registry.nid2obj(5))
402
+
403
+ si = create_signer_info_with_algo(md5)
404
+ p7.add_signer(si)
405
+
406
+ assert_equal md5, p7.get_sign.md_algs.iterator.next
407
+ assert_equal 1, p7.get_sign.md_algs.size
408
+
409
+ si = create_signer_info_with_algo(md5)
410
+ p7.add_signer(si)
411
+
412
+ assert_equal md5, p7.get_sign.md_algs.iterator.next
413
+ assert_equal 1, p7.get_sign.md_algs.size
414
+
415
+ si = create_signer_info_with_algo(md4)
416
+ p7.add_signer(si)
417
+
418
+ assert_equal 2, p7.get_sign.md_algs.size
419
+ assert p7.get_sign.md_algs.contains(md4)
420
+ assert p7.get_sign.md_algs.contains(md5)
421
+ end
422
+
423
+
424
+ def test_add_signer_to_signedAndEnveloped_with_new_algo_should_add_that_algo_to_the_algo_list
425
+ p7 = PKCS7.new
426
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
427
+
428
+ # YES, these numbers are correct. Don't change them. They are OpenSSL internal NIDs
429
+ md5 = AlgorithmIdentifier.new(ASN1Registry.nid2obj(4))
430
+ md4 = AlgorithmIdentifier.new(ASN1Registry.nid2obj(5))
431
+
432
+ si = create_signer_info_with_algo(md5)
433
+ p7.add_signer(si)
434
+
435
+ assert_equal md5, p7.get_signed_and_enveloped.md_algs.iterator.next
436
+ assert_equal 1, p7.get_signed_and_enveloped.md_algs.size
437
+
438
+ si = create_signer_info_with_algo(md5)
439
+ p7.add_signer(si)
440
+
441
+ assert_equal md5, p7.get_signed_and_enveloped.md_algs.iterator.next
442
+ assert_equal 1, p7.get_signed_and_enveloped.md_algs.size
443
+
444
+ si = create_signer_info_with_algo(md4)
445
+ p7.add_signer(si)
446
+
447
+ assert_equal 2, p7.get_signed_and_enveloped.md_algs.size
448
+ assert p7.get_signed_and_enveloped.md_algs.contains(md4)
449
+ assert p7.get_signed_and_enveloped.md_algs.contains(md5)
450
+ end
451
+
452
+ def test_set_content_on_data_throws_exception
453
+ p7 = PKCS7.new
454
+ p7.type = ASN1Registry::NID_pkcs7_data
455
+ assert_raises NativeException do
456
+ p7.setContent(PKCS7.new)
457
+ end
458
+ end
459
+
460
+ def test_set_content_on_enveloped_throws_exception
461
+ p7 = PKCS7.new
462
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
463
+ assert_raises NativeException do
464
+ p7.setContent(PKCS7.new)
465
+ end
466
+ end
467
+
468
+ def test_set_content_on_signedAndEnveloped_throws_exception
469
+ p7 = PKCS7.new
470
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
471
+ assert_raises NativeException do
472
+ p7.setContent(PKCS7.new)
473
+ end
474
+ end
475
+
476
+ def test_set_content_on_encrypted_throws_exception
477
+ p7 = PKCS7.new
478
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
479
+ assert_raises NativeException do
480
+ p7.setContent(PKCS7.new)
481
+ end
482
+ end
483
+
484
+ def test_set_content_on_signed_sets_the_content
485
+ p7 = PKCS7.new
486
+ p7.type = ASN1Registry::NID_pkcs7_signed
487
+ p7new = PKCS7.new
488
+ p7.setContent(p7new)
489
+
490
+ assert_equal p7new, p7.get_sign.contents
491
+ end
492
+
493
+ def test_set_content_on_digest_sets_the_content
494
+ p7 = PKCS7.new
495
+ p7.type = ASN1Registry::NID_pkcs7_digest
496
+ p7new = PKCS7.new
497
+ p7.setContent(p7new)
498
+
499
+ assert_equal p7new, p7.get_digest.contents
500
+ end
501
+
502
+ def test_get_signer_info_on_digest_returns_null
503
+ p7 = PKCS7.new
504
+ p7.type = ASN1Registry::NID_pkcs7_digest
505
+ assert_nil p7.signer_info
506
+ end
507
+
508
+ def test_get_signer_info_on_data_returns_null
509
+ p7 = PKCS7.new
510
+ p7.type = ASN1Registry::NID_pkcs7_data
511
+ assert_nil p7.signer_info
512
+ end
513
+
514
+ def test_get_signer_info_on_encrypted_returns_null
515
+ p7 = PKCS7.new
516
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
517
+ assert_nil p7.signer_info
518
+ end
519
+
520
+ def test_get_signer_info_on_enveloped_returns_null
521
+ p7 = PKCS7.new
522
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
523
+ assert_nil p7.signer_info
524
+ end
525
+
526
+ def test_get_signer_info_on_signed_returns_signer_info
527
+ p7 = PKCS7.new
528
+ p7.type = ASN1Registry::NID_pkcs7_signed
529
+ assert_equal p7.get_sign.signer_info.object_id, p7.signer_info.object_id
530
+ end
531
+
532
+ def test_get_signer_info_on_signedAndEnveloped_returns_signer_info
533
+ p7 = PKCS7.new
534
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
535
+ assert_equal p7.get_signed_and_enveloped.signer_info.object_id, p7.signer_info.object_id
536
+ end
537
+
538
+ def test_content_new_on_data_raises_exception
539
+ p7 = PKCS7.new
540
+ p7.type = ASN1Registry::NID_pkcs7_data
541
+ assert_raises NativeException do
542
+ p7.content_new(ASN1Registry::NID_pkcs7_data)
543
+ end
544
+ end
545
+
546
+ def test_content_new_on_encrypted_raises_exception
547
+ p7 = PKCS7.new
548
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
549
+ assert_raises NativeException do
550
+ p7.content_new(ASN1Registry::NID_pkcs7_data)
551
+ end
552
+ end
553
+
554
+ def test_content_new_on_enveloped_raises_exception
555
+ p7 = PKCS7.new
556
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
557
+ assert_raises NativeException do
558
+ p7.content_new(ASN1Registry::NID_pkcs7_data)
559
+ end
560
+ end
561
+
562
+ def test_content_new_on_signedAndEnveloped_raises_exception
563
+ p7 = PKCS7.new
564
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
565
+ assert_raises NativeException do
566
+ p7.content_new(ASN1Registry::NID_pkcs7_data)
567
+ end
568
+ end
569
+
570
+ def test_content_new_on_digest_creates_new_content
571
+ p7 = PKCS7.new
572
+ p7.type = ASN1Registry::NID_pkcs7_digest
573
+ p7.content_new(ASN1Registry::NID_pkcs7_signedAndEnveloped)
574
+ assert p7.get_digest.contents.signed_and_enveloped?
575
+
576
+ p7.content_new(ASN1Registry::NID_pkcs7_encrypted)
577
+ assert p7.get_digest.contents.encrypted?
578
+ end
579
+
580
+ def test_content_new_on_signed_creates_new_content
581
+ p7 = PKCS7.new
582
+ p7.type = ASN1Registry::NID_pkcs7_signed
583
+ p7.content_new(ASN1Registry::NID_pkcs7_signedAndEnveloped)
584
+ assert p7.get_sign.contents.signed_and_enveloped?
585
+
586
+ p7.content_new(ASN1Registry::NID_pkcs7_encrypted)
587
+ assert p7.get_sign.contents.encrypted?
588
+ end
589
+
590
+
591
+ def test_add_certificate_on_data_throws_exception
592
+ p7 = PKCS7.new
593
+ p7.type = ASN1Registry::NID_pkcs7_data
594
+ assert_raises NativeException do
595
+ p7.add_certificate(X509Cert)
596
+ end
597
+ end
598
+
599
+ def test_add_certificate_on_enveloped_throws_exception
600
+ p7 = PKCS7.new
601
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
602
+ assert_raises NativeException do
603
+ p7.add_certificate(X509Cert)
604
+ end
605
+ end
606
+
607
+ def test_add_certificate_on_encrypted_throws_exception
608
+ p7 = PKCS7.new
609
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
610
+ assert_raises NativeException do
611
+ p7.add_certificate(X509Cert)
612
+ end
613
+ end
614
+
615
+ def test_add_certificate_on_digest_throws_exception
616
+ p7 = PKCS7.new
617
+ p7.type = ASN1Registry::NID_pkcs7_digest
618
+ assert_raises NativeException do
619
+ p7.add_certificate(X509Cert)
620
+ end
621
+ end
622
+
623
+ def test_add_certificate_on_signed_adds_the_certificate
624
+ p7 = PKCS7.new
625
+ p7.type = ASN1Registry::NID_pkcs7_signed
626
+ p7.add_certificate(X509Cert)
627
+ assert_equal 1, p7.get_sign.cert.size
628
+ assert_equal X509Cert, p7.get_sign.cert.iterator.next
629
+ end
630
+
631
+ def test_add_certificate_on_signedAndEnveloped_adds_the_certificate
632
+ p7 = PKCS7.new
633
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
634
+ p7.add_certificate(X509Cert)
635
+ assert_equal 1, p7.get_signed_and_enveloped.cert.size
636
+ assert_equal X509Cert, p7.get_signed_and_enveloped.cert.get(0)
637
+ end
638
+
639
+ def test_add_crl_on_data_throws_exception
640
+ p7 = PKCS7.new
641
+ p7.type = ASN1Registry::NID_pkcs7_data
642
+ assert_raises NativeException do
643
+ p7.add_crl(X509CRL)
644
+ end
645
+ end
646
+
647
+ def test_add_crl_on_enveloped_throws_exception
648
+ p7 = PKCS7.new
649
+ p7.type = ASN1Registry::NID_pkcs7_enveloped
650
+ assert_raises NativeException do
651
+ p7.add_crl(X509CRL)
652
+ end
653
+ end
654
+
655
+ def test_add_crl_on_encrypted_throws_exception
656
+ p7 = PKCS7.new
657
+ p7.type = ASN1Registry::NID_pkcs7_encrypted
658
+ assert_raises NativeException do
659
+ p7.add_crl(X509CRL)
660
+ end
661
+ end
662
+
663
+ def test_add_crl_on_digest_throws_exception
664
+ p7 = PKCS7.new
665
+ p7.type = ASN1Registry::NID_pkcs7_digest
666
+ assert_raises NativeException do
667
+ p7.add_crl(X509CRL)
668
+ end
669
+ end
670
+
671
+ def test_add_crl_on_signed_adds_the_crl
672
+ p7 = PKCS7.new
673
+ p7.type = ASN1Registry::NID_pkcs7_signed
674
+ p7.add_crl(X509CRL)
675
+ assert_equal 1, p7.get_sign.crl.size
676
+ assert_equal X509CRL, p7.get_sign.crl.iterator.next
677
+ end
678
+
679
+ def test_add_crl_on_signedAndEnveloped_adds_the_crl
680
+ p7 = PKCS7.new
681
+ p7.type = ASN1Registry::NID_pkcs7_signedAndEnveloped
682
+ p7.add_crl(X509CRL)
683
+ assert_equal 1, p7.get_signed_and_enveloped.crl.size
684
+ assert_equal X509CRL, p7.get_signed_and_enveloped.crl.get(0)
685
+ end
686
+
687
+ EXISTING_PKCS7_DEF = "0\202\002 \006\t*\206H\206\367\r\001\a\003\240\202\002\0210\202\002\r\002\001\0001\202\001\2700\201\331\002\001\0000B0=1\0230\021\006\n\t\222&\211\223\362,d\001\031\026\003org1\0310\027\006\n\t\222&\211\223\362,d\001\031\026\truby-lang1\v0\t\006\003U\004\003\f\002CA\002\001\0020\r\006\t*\206H\206\367\r\001\001\001\005\000\004\201\200\213kF\330\030\362\237\363$\311\351\207\271+_\310sr\344\233N\200\233)\272\226\343\003\224OOf\372 \r\301{\206\367\241\270\006\240\254\3179F\232\231Q\232\225\347\373\233\032\375\360\035o\371\275p\306\v5Z)\263\037\302|\307\300\327\a\375\023G'Ax\313\346\261\254\227K\026\364\242\337\367\362rk\276\023\217m\326\343F\366I1\263\nLuNf\234\203\261\300\030\232Q\277\231\f0\030\001\332\021\0030\201\331\002\001\0000B0=1\0230\021\006\n\t\222&\211\223\362,d\001\031\026\003org1\0310\027\006\n\t\222&\211\223\362,d\001\031\026\truby-lang1\v0\t\006\003U\004\003\f\002CA\002\001\0030\r\006\t*\206H\206\367\r\001\001\001\005\000\004\201\200\215\223\3428\2440]\0278\016\230,\315\023Tg\325`\376~\353\304\020\243N{\326H\003\005\361q\224OI\310\2324-\341?\355&r\215\233\361\245jF\255R\271\203D\304v\325\265\243\321$\bSh\031i\eS\240\227\362\221\364\232\035\202\f?x\031\223D\004ZHD\355'g\243\037\236mJ\323\210\347\274m\324-\351\332\353#A\273\002\"h\aM\202\347\236\265\aI$@\240bt=<\212\2370L\006\t*\206H\206\367\r\001\a\0010\035\006\t`\206H\001e\003\004\001\002\004\020L?\325\372\\\360\366\372\237|W\333nnI\255\200 \253\234\252\263\006\335\037\320\350{s\352r\337\304\305\216\223k\003\376f\027_\201\035#*\002yM\334"
688
+
689
+ EXISTING_PKCS7_1 = PKCS7::from_asn1(ASN1InputStream.new(EXISTING_PKCS7_DEF.to_java_bytes).read_object)
690
+
691
+ def test_encrypt_integration_test
692
+ certs = [X509Cert]
693
+ cipher = Cipher.get_instance("AES", BCP.new)
694
+ data = "aaaaa\nbbbbb\nccccc\n".to_java_bytes
695
+ PKCS7::encrypt(certs, data, cipher, PKCS7::BINARY)
696
+ # puts
697
+ # puts PKCS7::encrypt(certs, data, cipher, PKCS7::BINARY)
698
+ # puts
699
+ # puts EXISTING_PKCS7_1
700
+ end
701
+
702
+ EXISTING_PKCS7_PEM = <<PKCS7STR
703
+ -----BEGIN PKCS7-----
704
+ MIICIAYJKoZIhvcNAQcDoIICETCCAg0CAQAxggG4MIHZAgEAMEIwPTETMBEGCgmS
705
+ JomT8ixkARkWA29yZzEZMBcGCgmSJomT8ixkARkWCXJ1YnktbGFuZzELMAkGA1UE
706
+ AwwCQ0ECAQIwDQYJKoZIhvcNAQEBBQAEgYCPGMV4KS/8amYA2xeIjj9qLseJf7dl
707
+ BtSDp+YAU3y1JnW7XufBCKxYw7eCuhWWA/mrxijr+wdsFDvSalM6nPX2P2NiVMWP
708
+ a7mzErZ4WrzkKIuGczYPYPJetwBYuhik3ya4ygYygoYssVRAITOSsEKpfqHAPmI+
709
+ AUJkqmCdGpQu9TCB2QIBADBCMD0xEzARBgoJkiaJk/IsZAEZFgNvcmcxGTAXBgoJ
710
+ kiaJk/IsZAEZFglydWJ5LWxhbmcxCzAJBgNVBAMMAkNBAgEDMA0GCSqGSIb3DQEB
711
+ AQUABIGAPaBX0KM3S+2jcrQrncu1jrvm1PUXlUvMfFIG2oBfPkMhiqCBvkOct1Ve
712
+ ws1hxvGtsqyjAUn02Yx1+gQJhTN4JZZHNqkfi0TwN32nlwLxclKcrbF9bvtMiVHx
713
+ V3LrSygblxxJsBf8reoV4yTJRa3w98bEoDhjUwjfy5xTml2cAn4wTAYJKoZIhvcN
714
+ AQcBMB0GCWCGSAFlAwQBAgQQath+2gUo4ntkKl8FO1LLhoAg58j0Jn/OfWG3rNRH
715
+ kTtUQfnBFk/UGbTZgExHILaGz8Y=
716
+ -----END PKCS7-----
717
+ PKCS7STR
718
+
719
+ PKCS7_PEM_CONTENTS = "\347\310\364&\177\316}a\267\254\324G\221;TA\371\301\026O\324\031\264\331\200LG \266\206\317\306"
720
+
721
+ PKCS7_PEM_FIRST_KEY = "\217\030\305x)/\374jf\000\333\027\210\216?j.\307\211\177\267e\006\324\203\247\346\000S|\265&u\273^\347\301\b\254X\303\267\202\272\025\226\003\371\253\306(\353\373\al\024;\322jS:\234\365\366?cbT\305\217k\271\263\022\266xZ\274\344(\213\206s6\017`\362^\267\000X\272\030\244\337&\270\312\0062\202\206,\261T@!3\222\260B\251~\241\300>b>\001Bd\252`\235\032\224.\365"
722
+
723
+ PKCS7_PEM_SECOND_KEY = "=\240W\320\2437K\355\243r\264+\235\313\265\216\273\346\324\365\027\225K\314|R\006\332\200_>C!\212\240\201\276C\234\267U^\302\315a\306\361\255\262\254\243\001I\364\331\214u\372\004\t\2053x%\226G6\251\037\213D\3607}\247\227\002\361rR\234\255\261}n\373L\211Q\361Wr\353K(\e\227\034I\260\027\374\255\352\025\343$\311E\255\360\367\306\304\2408cS\b\337\313\234S\232]\234\002~"
724
+
725
+ def test_PEM_read_pkcs7_bio
726
+ bio = BIO::mem_buf(EXISTING_PKCS7_PEM.to_java_bytes)
727
+ p7 = PKCS7.read_pem(bio)
728
+
729
+ assert_equal ASN1Registry::NID_pkcs7_enveloped, p7.type
730
+ env = p7.get_enveloped
731
+ assert_equal 0, env.version
732
+ enc_data = env.enc_data
733
+ assert_equal ASN1Registry::NID_pkcs7_data, enc_data.content_type
734
+ assert_equal ASN1Registry::NID_aes_128_cbc, ASN1Registry::obj2nid(enc_data.algorithm.get_object_id)
735
+ assert_equal PKCS7_PEM_CONTENTS, String.from_java_bytes(enc_data.enc_data.octets)
736
+
737
+ ris = env.recipient_info
738
+ assert_equal 2, ris.size
739
+
740
+ first = second = nil
741
+ tmp = ris.iterator.next
742
+
743
+ if tmp.issuer_and_serial.certificate_serial_number.value == 2
744
+ first = tmp
745
+ iter = ris.iterator
746
+ iter.next
747
+ second = iter.next
748
+ else
749
+ second = tmp
750
+ iter = ris.iterator
751
+ iter.next
752
+ first = iter.next
753
+ end
754
+
755
+ assert_equal 0, first.version
756
+ assert_equal 0, second.version
757
+
758
+ assert_equal "DC=org,DC=ruby-lang,CN=CA", first.issuer_and_serial.name.to_s
759
+ assert_equal "DC=org,DC=ruby-lang,CN=CA", second.issuer_and_serial.name.to_s
760
+
761
+ assert_equal ASN1Registry::NID_rsaEncryption, ASN1Registry::obj2nid(first.key_enc_algor.get_object_id)
762
+ assert_equal ASN1Registry::NID_rsaEncryption, ASN1Registry::obj2nid(second.key_enc_algor.get_object_id)
763
+
764
+ assert_equal PKCS7_PEM_FIRST_KEY, String.from_java_bytes(first.enc_key.octets)
765
+ assert_equal PKCS7_PEM_SECOND_KEY, String.from_java_bytes(second.enc_key.octets)
766
+ end
767
+ end
768
+ end
769
+