jruby-openssl 0.2.3 → 0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of jruby-openssl might be problematic. Click here for more details.

@@ -0,0 +1,100 @@
1
+ if defined?(JRUBY_VERSION)
2
+ require "java"
3
+ base = File.join(File.dirname(__FILE__), '..')
4
+ $CLASSPATH << File.join(base, 'pkg', 'classes')
5
+ $CLASSPATH << File.join(base, 'lib', 'bcprov-jdk14-139.jar')
6
+ end
7
+
8
+ begin
9
+ require "openssl"
10
+ rescue LoadError
11
+ end
12
+ require "test/unit"
13
+ require 'net/https'
14
+
15
+ class TestIntegration < Test::Unit::TestCase
16
+ # JRUBY-2471
17
+ def _test_drb
18
+ config = {
19
+ :SSLVerifyMode => OpenSSL::SSL::VERIFY_PEER,
20
+ :SSLCACertificateFile => File.join(File.dirname(__FILE__), "fixture", "cacert.pem"),
21
+ :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(File.join(File.dirname(__FILE__), "fixture", "localhost_keypair.pem"))),
22
+ :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(File.join(File.dirname(__FILE__), "fixture", "cert_localhost.pem"))),
23
+ }
24
+ p config
25
+ DRb.start_service(nil, nil, config)
26
+ end
27
+
28
+ # JRUBY-2913
29
+ # Warning - this test actually uses the internet connection.
30
+ # If there is no connection, it will fail.
31
+ def test_ca_path_name
32
+ uri = URI.parse('https://www.paypal.com')
33
+
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
36
+ http.ca_path = "./"
37
+ http.use_ssl = true
38
+
39
+ response = http.start do |s|
40
+ assert s.get(uri.request_uri).length > 0
41
+ end
42
+ end
43
+
44
+ # JRUBY-2178 and JRUBY-1307
45
+ # Warning - this test actually uses the internet connection.
46
+ # If there is no connection, it will fail.
47
+ # This test generally throws an exception
48
+ # about illegal_parameter when
49
+ # it can't use the cipher string correctly
50
+ def test_cipher_strings
51
+ socket = TCPSocket.new('rubyforge.org', 443)
52
+ ctx = OpenSSL::SSL::SSLContext.new
53
+ ctx.cert_store = OpenSSL::X509::Store.new
54
+ ctx.verify_mode = 0
55
+ ctx.cert = nil
56
+ ctx.key = nil
57
+ ctx.client_ca = nil
58
+ ctx.ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"
59
+
60
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ctx)
61
+ ssl_socket.connect
62
+ ssl_socket.close
63
+ end
64
+
65
+ # JRUBY-1194
66
+ def test_des_encryption
67
+ iv = "IVIVIVIV"
68
+ key = "KEYKEYKE"
69
+ alg = "des"
70
+ str = "string abc foo bar baxz"
71
+
72
+ cipher = OpenSSL::Cipher::Cipher.new(alg)
73
+ cipher.encrypt(key, iv)
74
+ cipher.padding = 32
75
+ cipher.key = key
76
+ cipher.iv = iv
77
+
78
+ encrypted = cipher.update(str)
79
+ encrypted << cipher.final
80
+
81
+ assert_equal "\253\305\306\372;\374\235\302\357/\006\360\355XO\232\312S\356* #\227\217", encrypted
82
+ end
83
+
84
+ def _test_perf_of_nil
85
+ # require 'net/https'
86
+ # require 'benchmark'
87
+
88
+ # def request(data)
89
+ # connection = Net::HTTP.new("www.google.com", 443)
90
+ # connection.use_ssl = true
91
+ # connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
92
+ # connection.start do |connection|
93
+ # connection.request_post("/tbproxy/spell?lang=en", data, { 'User-Agent' => "Test", 'Accept' => 'text/xml' })
94
+ # end
95
+ # end
96
+
97
+ # puts "is not: #{Benchmark.measure { request("") }.to_s.chomp}"
98
+ # puts "is nil: #{Benchmark.measure { request(nil) }.to_s.chomp}"
99
+ end
100
+ end
@@ -0,0 +1,98 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'mocha', 'lib')
2
+
3
+ require "test/unit"
4
+ require 'mocha'
5
+
6
+ if defined?(JRUBY_VERSION)
7
+ require "java"
8
+ $CLASSPATH << 'pkg/classes'
9
+ $CLASSPATH << 'lib/bcprov-jdk14-139.jar'
10
+
11
+ module PKCS7Test
12
+ module ASN1
13
+ OctetString = org.bouncycastle.asn1.DEROctetString
14
+ end
15
+
16
+ PKCS7 = org.jruby.ext.openssl.impl.PKCS7 unless defined?(PKCS7)
17
+ Attribute = org.jruby.ext.openssl.impl.Attribute unless defined?(Attribute)
18
+ Digest = org.jruby.ext.openssl.impl.Digest unless defined?(Digest)
19
+ EncContent = org.jruby.ext.openssl.impl.EncContent unless defined?(EncContent)
20
+ Encrypt = org.jruby.ext.openssl.impl.Encrypt unless defined?(Encrypt)
21
+ Envelope = org.jruby.ext.openssl.impl.Envelope unless defined?(Envelope)
22
+ IssuerAndSerial = org.jruby.ext.openssl.impl.IssuerAndSerial unless defined?(IssuerAndSerial)
23
+ RecipInfo = org.jruby.ext.openssl.impl.RecipInfo unless defined?(RecipInfo)
24
+ SignEnvelope = org.jruby.ext.openssl.impl.SignEnvelope unless defined?(SignEnvelope)
25
+ Signed = org.jruby.ext.openssl.impl.Signed unless defined?(Signed)
26
+ SMIME = org.jruby.ext.openssl.impl.SMIME unless defined?(SMIME)
27
+ Mime = org.jruby.ext.openssl.impl.Mime unless defined?(Mime)
28
+ MimeHeader = org.jruby.ext.openssl.impl.MimeHeader unless defined?(MimeHeader)
29
+ MimeParam = org.jruby.ext.openssl.impl.MimeParam unless defined?(MimeParam)
30
+ BIO = org.jruby.ext.openssl.impl.BIO unless defined?(BIO)
31
+ PKCS7Exception = org.jruby.ext.openssl.impl.PKCS7Exception unless defined?(PKCS7Exception)
32
+ ASN1Registry = org.jruby.ext.openssl.impl.ASN1Registry unless defined?(ASN1Registry)
33
+ AlgorithmIdentifier = org.bouncycastle.asn1.x509.AlgorithmIdentifier unless defined?(AlgorithmIdentifier)
34
+ SignerInfoWithPkey = org.jruby.ext.openssl.impl.SignerInfoWithPkey unless defined?(SignerInfoWithPkey)
35
+ IssuerAndSerialNumber = org.bouncycastle.asn1.pkcs.IssuerAndSerialNumber unless defined?(IssuerAndSerialNumber)
36
+ ASN1InputStream = org.bouncycastle.asn1.ASN1InputStream unless defined?(ASN1InputStream)
37
+ X509AuxCertificate = org.jruby.ext.openssl.x509store.X509AuxCertificate unless defined?(X509AuxCertificate)
38
+
39
+ ArrayList = java.util.ArrayList unless defined?(ArrayList)
40
+ CertificateFactory = java.security.cert.CertificateFactory unless defined?(CertificateFactory)
41
+ BCP = org.bouncycastle.jce.provider.BouncyCastleProvider unless defined?(BCP)
42
+ ByteArrayInputStream = java.io.ByteArrayInputStream unless defined?(ByteArrayInputStream)
43
+ BigInteger = java.math.BigInteger unless defined?(BigInteger)
44
+ Cipher = javax.crypto.Cipher unless defined?(Cipher)
45
+
46
+ DERInteger = org.bouncycastle.asn1.DERInteger
47
+ DERSet = org.bouncycastle.asn1.DERSet
48
+ DEROctetString = org.bouncycastle.asn1.DEROctetString
49
+ X509Name = org.bouncycastle.asn1.x509.X509Name
50
+
51
+
52
+ MimeEnvelopedString = File::read(File.join(File.dirname(__FILE__), 'pkcs7_mime_enveloped.message'))
53
+ MimeSignedString = File::read(File.join(File.dirname(__FILE__), 'pkcs7_mime_signed.message'))
54
+ MultipartSignedString = File::read(File.join(File.dirname(__FILE__), 'pkcs7_multipart_signed.message'))
55
+
56
+ X509CertString = <<CERT
57
+ -----BEGIN CERTIFICATE-----
58
+ MIICijCCAXKgAwIBAgIBAjANBgkqhkiG9w0BAQUFADA9MRMwEQYKCZImiZPyLGQB
59
+ GRYDb3JnMRkwFwYKCZImiZPyLGQBGRYJcnVieS1sYW5nMQswCQYDVQQDDAJDQTAe
60
+ Fw0wODA3MDgxOTE1NDZaFw0wODA3MDgxOTQ1NDZaMEQxEzARBgoJkiaJk/IsZAEZ
61
+ FgNvcmcxGTAXBgoJkiaJk/IsZAEZFglydWJ5LWxhbmcxEjAQBgNVBAMMCWxvY2Fs
62
+ aG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAy8LEsNRApz7U/j5DoB4X
63
+ BgO9Z8Atv5y/OVQRp0ag8Tqo1YewsWijxEWB7JOATwpBN267U4T1nPZIxxEEO7n/
64
+ WNa2ws9JWsjah8ssEBFSxZqdXKSLf0N4Hi7/GQ/aYoaMCiQ8jA4jegK2FJmXM71u
65
+ Pe+jFN/peeBOpRfyXxRFOYcCAwEAAaMSMBAwDgYDVR0PAQH/BAQDAgWgMA0GCSqG
66
+ SIb3DQEBBQUAA4IBAQCU879BALJIM9avHiuZ3WTjDy0UYP3ZG5wtuSqBSnD1k8pr
67
+ hXfRaga7mDj6EQaGUovImb+KrRi6mZc+zsx4rTxwBNJT9U8yiW2eYxmgcT9/qKrD
68
+ /1nz+e8NeUCCDY5UTUHGszZw5zLEDgDX2n3E/CDIZsoRSyq5vXq1jpfih/tSWanj
69
+ Y9uP/o8Dc7ZcRJOAX7NPu1bbZcbxEbZ8sMe5wZ5HNiAR6gnOrjz2Yyazb//PSskE
70
+ 4flt/2h4pzGA0/ZHcnDjcoLdiLtInsqPOlVDLgqd/XqRYWtj84N4gw1iS9cHyrIZ
71
+ dqbS54IKvzElD+R0QVS2z6TIGJSpuSBnZ4yfuNuq
72
+ -----END CERTIFICATE-----
73
+ CERT
74
+
75
+ X509CRLString = <<CRL
76
+ ----BEGIN X509 CRL-----
77
+ MIIBlTB/AgEBMA0GCSqGSIb3DQEBBQUAMD0xEzARBgoJkiaJk/IsZAEZFgNvcmcx
78
+ GTAXBgoJkiaJk/IsZAEZFglydWJ5LWxhbmcxCzAJBgNVBAMMAkNBFw0wODA3MTgx
79
+ NzQxMjhaFw0wODA3MTgxODA4MDhaoA4wDDAKBgNVHRQEAwIBATANBgkqhkiG9w0B
80
+ AQUFAAOCAQEASJaj1keN+tMmsF3QmjH2RhbW/9rZAl4gjv+uQQqrcS2ByfkXLU1d
81
+ l/8rCHeT/XMoeU6xhQNHPP3uZBwfuuETcp65BMBcZFOUhUR0U5AaGhvSDS/+6EsP
82
+ zFdQgAagmThFdN5ei9guTLqWwN0ZyqiaHyevFJuk+L9qbKavaSeKqfJbU7Sj/Z3J
83
+ WLKoixvyj3N6W7evygH80lTvjZugmxJ1/AjICVSYr1hpHHd6EWq0b0YFrGFmg27R
84
+ WmsAXd0QV5UChfAJ2+Cz5U1bPszvIJGrzfAIoLxHv5rI5rseQzqZdPaFSe4Oehln
85
+ 9qEYmsK3PS6bYoQol0cgj97Ep4olS8CulA==
86
+ -----END X509 CRL-----
87
+ CRL
88
+
89
+ X509Cert = X509AuxCertificate.new(CertificateFactory.getInstance("X.509",BCP.new).generateCertificate(ByteArrayInputStream.new(X509CertString.to_java_bytes)))
90
+ X509CRL = CertificateFactory.getInstance("X.509",BCP.new).generateCRL(ByteArrayInputStream.new(X509CRLString.to_java_bytes))
91
+ end
92
+
93
+ require File.join(File.dirname(__FILE__), 'test_java_attribute')
94
+ require File.join(File.dirname(__FILE__), 'test_java_bio')
95
+ require File.join(File.dirname(__FILE__), 'test_java_mime')
96
+ require File.join(File.dirname(__FILE__), 'test_java_pkcs7')
97
+ require File.join(File.dirname(__FILE__), 'test_java_smime')
98
+ end
@@ -0,0 +1,25 @@
1
+ module PKCS7Test
2
+ class TestJavaAttribute < Test::Unit::TestCase
3
+ def test_attributes
4
+ val = ASN1::OctetString.new("foo".to_java_bytes)
5
+ val2 = ASN1::OctetString.new("bar".to_java_bytes)
6
+ attr = Attribute.create(123, 444, val)
7
+ assert_raises NoMethodError do
8
+ attr.type = 12
9
+ end
10
+ assert_raises NoMethodError do
11
+ attr.value = val2
12
+ end
13
+
14
+ assert_equal 123, attr.type
15
+ assert_equal val, attr.set.get(0)
16
+
17
+ attr2 = Attribute.create(123, 444, val)
18
+
19
+ assert_equal attr, attr2
20
+
21
+ assert_not_equal Attribute.create(124, 444, val), attr
22
+ assert_not_equal Attribute.create(123, 444, val2), attr
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ module PKCS7Test
2
+ class TestJavaBIO < Test::Unit::TestCase
3
+ def test_string_bio_simple
4
+ bio = BIO::from_string("abc")
5
+ arr = Java::byte[20].new
6
+ read = bio.gets(arr, 10)
7
+ assert_equal 3, read
8
+ assert_equal "abc".to_java_bytes.to_a, arr.to_a[0...read]
9
+ end
10
+
11
+ def test_string_bio_simple_with_newline
12
+ bio = BIO::from_string("abc\n")
13
+ arr = Java::byte[20].new
14
+ read = bio.gets(arr, 10)
15
+ assert_equal 4, read
16
+ assert_equal "abc\n".to_java_bytes.to_a, arr.to_a[0...read]
17
+ end
18
+
19
+ def test_string_bio_simple_with_newline_and_more_data
20
+ bio = BIO::from_string("abc\nfoo\n\nbar")
21
+ arr = Java::byte[20].new
22
+ read = bio.gets(arr, 10)
23
+ assert_equal 4, read
24
+ assert_equal "abc\n".to_java_bytes.to_a, arr.to_a[0...read]
25
+
26
+ read = bio.gets(arr, 10)
27
+ assert_equal 4, read
28
+ assert_equal "foo\n".to_java_bytes.to_a, arr.to_a[0...read]
29
+
30
+ read = bio.gets(arr, 10)
31
+ assert_equal 1, read
32
+ assert_equal "\n".to_java_bytes.to_a, arr.to_a[0...read]
33
+
34
+ read = bio.gets(arr, 10)
35
+ assert_equal 3, read
36
+ assert_equal "bar".to_java_bytes.to_a, arr.to_a[0...read]
37
+
38
+ read = bio.gets(arr, 10)
39
+ assert_equal 0, read
40
+ end
41
+ end
42
+ end
@@ -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.first
32
+ assert_equal "foo", result.first.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.first
40
+ assert_equal "foo", result.first.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.first
48
+ assert_equal "foo", result.first.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.first
56
+ assert_equal "foo", result.first.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.first
64
+ assert_equal "foo", result.first.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.first
73
+ assert_equal "foo", result.first.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.first.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.first.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.first
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.first
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.first
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
+