jruby-openssl 0.7.5.dev → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/History.txt +44 -14
  2. data/Manifest.txt +135 -80
  3. data/Rakefile +14 -6
  4. data/lib/{openssl.rb → 1.8/openssl.rb} +2 -11
  5. data/lib/{openssl → 1.8/openssl}/bn.rb +2 -2
  6. data/lib/{openssl → 1.8/openssl}/buffering.rb +3 -1
  7. data/lib/{openssl → 1.8/openssl}/cipher.rb +0 -0
  8. data/lib/{openssl → 1.8/openssl}/config.rb +1 -1
  9. data/lib/{openssl → 1.8/openssl}/digest.rb +2 -2
  10. data/lib/{openssl → 1.8/openssl}/pkcs7.rb +0 -0
  11. data/lib/{openssl/ssl.rb → 1.8/openssl/ssl-internal.rb} +2 -2
  12. data/lib/1.8/openssl/ssl.rb +1 -0
  13. data/lib/{openssl/x509.rb → 1.8/openssl/x509-internal.rb} +8 -9
  14. data/lib/1.8/openssl/x509.rb +1 -0
  15. data/lib/1.9/openssl.rb +22 -0
  16. data/lib/1.9/openssl/bn.rb +35 -0
  17. data/lib/1.9/openssl/buffering.rb +448 -0
  18. data/lib/1.9/openssl/cipher.rb +65 -0
  19. data/lib/1.9/openssl/config.rb +313 -0
  20. data/lib/1.9/openssl/digest.rb +72 -0
  21. data/lib/1.9/openssl/ssl-internal.rb +177 -0
  22. data/lib/1.9/openssl/ssl.rb +2 -0
  23. data/lib/1.9/openssl/x509-internal.rb +158 -0
  24. data/lib/1.9/openssl/x509.rb +2 -0
  25. data/lib/{jopenssl.jar → shared/jopenssl.jar} +0 -0
  26. data/lib/{jopenssl → shared/jopenssl}/version.rb +1 -1
  27. data/lib/shared/openssl.rb +18 -0
  28. data/lib/{openssl → shared/openssl}/dummy.rb +0 -0
  29. data/lib/{openssl → shared/openssl}/dummyssl.rb +0 -0
  30. data/lib/shared/openssl/ssl.rb +1 -0
  31. data/lib/shared/openssl/x509.rb +1 -0
  32. data/test/{openssl → 1.8}/ssl_server.rb +0 -0
  33. data/test/{openssl → 1.8}/test_asn1.rb +15 -0
  34. data/test/{openssl → 1.8}/test_cipher.rb +0 -0
  35. data/test/{openssl → 1.8}/test_config.rb +0 -0
  36. data/test/{openssl → 1.8}/test_digest.rb +0 -0
  37. data/test/{openssl → 1.8}/test_ec.rb +0 -0
  38. data/test/{openssl → 1.8}/test_hmac.rb +0 -0
  39. data/test/{openssl → 1.8}/test_ns_spki.rb +0 -0
  40. data/test/{openssl → 1.8}/test_pair.rb +10 -2
  41. data/test/{openssl → 1.8}/test_pkcs7.rb +0 -0
  42. data/test/{openssl → 1.8}/test_pkey_rsa.rb +0 -0
  43. data/test/{openssl → 1.8}/test_ssl.rb +17 -20
  44. data/test/{openssl → 1.8}/test_x509cert.rb +0 -0
  45. data/test/{openssl → 1.8}/test_x509crl.rb +0 -0
  46. data/test/{openssl → 1.8}/test_x509ext.rb +0 -0
  47. data/test/{openssl → 1.8}/test_x509name.rb +0 -0
  48. data/test/{openssl → 1.8}/test_x509req.rb +0 -0
  49. data/test/{openssl → 1.8}/test_x509store.rb +0 -0
  50. data/test/{openssl → 1.8}/utils.rb +0 -0
  51. data/test/1.9/ssl_server.rb +81 -0
  52. data/test/1.9/test_asn1.rb +589 -0
  53. data/test/1.9/test_bn.rb +23 -0
  54. data/test/1.9/test_buffering.rb +88 -0
  55. data/test/1.9/test_cipher.rb +107 -0
  56. data/test/1.9/test_config.rb +288 -0
  57. data/test/1.9/test_digest.rb +118 -0
  58. data/test/1.9/test_engine.rb +15 -0
  59. data/test/1.9/test_hmac.rb +32 -0
  60. data/test/1.9/test_ns_spki.rb +50 -0
  61. data/test/1.9/test_ocsp.rb +47 -0
  62. data/test/1.9/test_pair.rb +257 -0
  63. data/test/1.9/test_pkcs12.rb +209 -0
  64. data/test/1.9/test_pkcs7.rb +151 -0
  65. data/test/1.9/test_pkey_dh.rb +72 -0
  66. data/test/1.9/test_pkey_dsa.rb +224 -0
  67. data/test/1.9/test_pkey_ec.rb +182 -0
  68. data/test/1.9/test_pkey_rsa.rb +244 -0
  69. data/test/1.9/test_ssl.rb +455 -0
  70. data/test/1.9/test_ssl_session.rb +327 -0
  71. data/test/1.9/test_x509cert.rb +217 -0
  72. data/test/1.9/test_x509crl.rb +221 -0
  73. data/test/1.9/test_x509ext.rb +69 -0
  74. data/test/1.9/test_x509name.rb +296 -0
  75. data/test/1.9/test_x509req.rb +150 -0
  76. data/test/1.9/test_x509store.rb +229 -0
  77. data/test/1.9/utils.rb +304 -0
  78. data/test/fixture/purpose/ca/ca_config.rb +1 -1
  79. data/test/fixture/purpose/ca/gen_cert.rb +128 -0
  80. data/test/fixture/purpose/ca/newcerts/4_cert.pem +19 -0
  81. data/test/fixture/purpose/ca/serial +1 -1
  82. data/test/fixture/purpose/sslserver_no_dsig_in_keyUsage.pem +19 -0
  83. data/test/ruby/envutil.rb +208 -0
  84. data/test/ruby/ut_eof.rb +128 -0
  85. data/test/test_java.rb +1 -1
  86. data/test/test_openssl.rb +1 -1
  87. data/test/test_pkcs7.rb +16 -0
  88. data/test/test_pkey_dsa.rb +180 -0
  89. data/test/test_pkey_rsa.rb +298 -0
  90. data/test/test_ssl.rb +1 -1
  91. data/test/test_x509store.rb +8 -0
  92. metadata +121 -75
  93. data/test/test_pkey.rb +0 -204
@@ -0,0 +1,221 @@
1
+ require_relative "utils"
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestX509CRL < Test::Unit::TestCase
6
+ def setup
7
+ @rsa1024 = OpenSSL::TestUtils::TEST_KEY_RSA1024
8
+ @rsa2048 = OpenSSL::TestUtils::TEST_KEY_RSA2048
9
+ @dsa256 = OpenSSL::TestUtils::TEST_KEY_DSA256
10
+ @dsa512 = OpenSSL::TestUtils::TEST_KEY_DSA512
11
+ @ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
12
+ @ee1 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE1")
13
+ @ee2 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE2")
14
+ end
15
+
16
+ def teardown
17
+ end
18
+
19
+ def issue_crl(*args)
20
+ OpenSSL::TestUtils.issue_crl(*args)
21
+ end
22
+
23
+ def issue_cert(*args)
24
+ OpenSSL::TestUtils.issue_cert(*args)
25
+ end
26
+
27
+ def test_basic
28
+ now = Time.at(Time.now.to_i)
29
+
30
+ cert = issue_cert(@ca, @rsa2048, 1, now, now+3600, [],
31
+ nil, nil, OpenSSL::Digest::SHA1.new)
32
+ crl = issue_crl([], 1, now, now+1600, [],
33
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
34
+ assert_equal(1, crl.version)
35
+ assert_equal(cert.issuer.to_der, crl.issuer.to_der)
36
+ assert_equal(now, crl.last_update)
37
+ assert_equal(now+1600, crl.next_update)
38
+
39
+ crl = OpenSSL::X509::CRL.new(crl.to_der)
40
+ assert_equal(1, crl.version)
41
+ assert_equal(cert.issuer.to_der, crl.issuer.to_der)
42
+ assert_equal(now, crl.last_update)
43
+ assert_equal(now+1600, crl.next_update)
44
+ end
45
+
46
+ def test_revoked
47
+
48
+ # CRLReason ::= ENUMERATED {
49
+ # unspecified (0),
50
+ # keyCompromise (1),
51
+ # cACompromise (2),
52
+ # affiliationChanged (3),
53
+ # superseded (4),
54
+ # cessationOfOperation (5),
55
+ # certificateHold (6),
56
+ # removeFromCRL (8),
57
+ # privilegeWithdrawn (9),
58
+ # aACompromise (10) }
59
+
60
+ now = Time.at(Time.now.to_i)
61
+ revoke_info = [
62
+ [1, Time.at(0), 1],
63
+ [2, Time.at(0x7fffffff), 2],
64
+ [3, now, 3],
65
+ [4, now, 4],
66
+ [5, now, 5],
67
+ ]
68
+ cert = issue_cert(@ca, @rsa2048, 1, Time.now, Time.now+3600, [],
69
+ nil, nil, OpenSSL::Digest::SHA1.new)
70
+ crl = issue_crl(revoke_info, 1, Time.now, Time.now+1600, [],
71
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
72
+ revoked = crl.revoked
73
+ assert_equal(5, revoked.size)
74
+ assert_equal(1, revoked[0].serial)
75
+ assert_equal(2, revoked[1].serial)
76
+ assert_equal(3, revoked[2].serial)
77
+ assert_equal(4, revoked[3].serial)
78
+ assert_equal(5, revoked[4].serial)
79
+
80
+ assert_equal(Time.at(0), revoked[0].time)
81
+ assert_equal(Time.at(0x7fffffff), revoked[1].time)
82
+ assert_equal(now, revoked[2].time)
83
+ assert_equal(now, revoked[3].time)
84
+ assert_equal(now, revoked[4].time)
85
+
86
+ assert_equal("CRLReason", revoked[0].extensions[0].oid)
87
+ assert_equal("CRLReason", revoked[1].extensions[0].oid)
88
+ assert_equal("CRLReason", revoked[2].extensions[0].oid)
89
+ assert_equal("CRLReason", revoked[3].extensions[0].oid)
90
+ assert_equal("CRLReason", revoked[4].extensions[0].oid)
91
+
92
+ assert_equal("Key Compromise", revoked[0].extensions[0].value)
93
+ assert_equal("CA Compromise", revoked[1].extensions[0].value)
94
+ assert_equal("Affiliation Changed", revoked[2].extensions[0].value)
95
+ assert_equal("Superseded", revoked[3].extensions[0].value)
96
+ assert_equal("Cessation Of Operation", revoked[4].extensions[0].value)
97
+
98
+ assert_equal(false, revoked[0].extensions[0].critical?)
99
+ assert_equal(false, revoked[1].extensions[0].critical?)
100
+ assert_equal(false, revoked[2].extensions[0].critical?)
101
+ assert_equal(false, revoked[3].extensions[0].critical?)
102
+ assert_equal(false, revoked[4].extensions[0].critical?)
103
+
104
+ crl = OpenSSL::X509::CRL.new(crl.to_der)
105
+ assert_equal("Key Compromise", revoked[0].extensions[0].value)
106
+ assert_equal("CA Compromise", revoked[1].extensions[0].value)
107
+ assert_equal("Affiliation Changed", revoked[2].extensions[0].value)
108
+ assert_equal("Superseded", revoked[3].extensions[0].value)
109
+ assert_equal("Cessation Of Operation", revoked[4].extensions[0].value)
110
+
111
+ revoke_info = (1..1000).collect{|i| [i, now, 0] }
112
+ crl = issue_crl(revoke_info, 1, Time.now, Time.now+1600, [],
113
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
114
+ revoked = crl.revoked
115
+ assert_equal(1000, revoked.size)
116
+ assert_equal(1, revoked[0].serial)
117
+ assert_equal(1000, revoked[999].serial)
118
+ end
119
+
120
+ def test_extension
121
+ cert_exts = [
122
+ ["basicConstraints", "CA:TRUE", true],
123
+ ["subjectKeyIdentifier", "hash", false],
124
+ ["authorityKeyIdentifier", "keyid:always", false],
125
+ ["subjectAltName", "email:xyzzy@ruby-lang.org", false],
126
+ ["keyUsage", "cRLSign, keyCertSign", true],
127
+ ]
128
+ crl_exts = [
129
+ ["authorityKeyIdentifier", "keyid:always", false],
130
+ ["issuerAltName", "issuer:copy", false],
131
+ ]
132
+
133
+ cert = issue_cert(@ca, @rsa2048, 1, Time.now, Time.now+3600, cert_exts,
134
+ nil, nil, OpenSSL::Digest::SHA1.new)
135
+ crl = issue_crl([], 1, Time.now, Time.now+1600, crl_exts,
136
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
137
+ exts = crl.extensions
138
+ assert_equal(3, exts.size)
139
+ assert_equal("1", exts[0].value)
140
+ assert_equal("crlNumber", exts[0].oid)
141
+ assert_equal(false, exts[0].critical?)
142
+
143
+ assert_equal("authorityKeyIdentifier", exts[1].oid)
144
+ keyid = OpenSSL::TestUtils.get_subject_key_id(cert)
145
+ assert_match(/^keyid:#{keyid}/, exts[1].value)
146
+ assert_equal(false, exts[1].critical?)
147
+
148
+ assert_equal("issuerAltName", exts[2].oid)
149
+ assert_equal("email:xyzzy@ruby-lang.org", exts[2].value)
150
+ assert_equal(false, exts[2].critical?)
151
+
152
+ crl = OpenSSL::X509::CRL.new(crl.to_der)
153
+ exts = crl.extensions
154
+ assert_equal(3, exts.size)
155
+ assert_equal("1", exts[0].value)
156
+ assert_equal("crlNumber", exts[0].oid)
157
+ assert_equal(false, exts[0].critical?)
158
+
159
+ assert_equal("authorityKeyIdentifier", exts[1].oid)
160
+ keyid = OpenSSL::TestUtils.get_subject_key_id(cert)
161
+ assert_match(/^keyid:#{keyid}/, exts[1].value)
162
+ assert_equal(false, exts[1].critical?)
163
+
164
+ assert_equal("issuerAltName", exts[2].oid)
165
+ assert_equal("email:xyzzy@ruby-lang.org", exts[2].value)
166
+ assert_equal(false, exts[2].critical?)
167
+ end
168
+
169
+ def test_crlnumber
170
+ cert = issue_cert(@ca, @rsa2048, 1, Time.now, Time.now+3600, [],
171
+ nil, nil, OpenSSL::Digest::SHA1.new)
172
+ crl = issue_crl([], 1, Time.now, Time.now+1600, [],
173
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
174
+ assert_match(1.to_s, crl.extensions[0].value)
175
+ assert_match(/X509v3 CRL Number:\s+#{1}/m, crl.to_text)
176
+
177
+ crl = issue_crl([], 2**32, Time.now, Time.now+1600, [],
178
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
179
+ assert_match((2**32).to_s, crl.extensions[0].value)
180
+ assert_match(/X509v3 CRL Number:\s+#{2**32}/m, crl.to_text)
181
+
182
+ crl = issue_crl([], 2**100, Time.now, Time.now+1600, [],
183
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
184
+ assert_match(/X509v3 CRL Number:\s+#{2**100}/m, crl.to_text)
185
+ assert_match((2**100).to_s, crl.extensions[0].value)
186
+ end
187
+
188
+ def test_sign_and_verify
189
+ cert = issue_cert(@ca, @rsa2048, 1, Time.now, Time.now+3600, [],
190
+ nil, nil, OpenSSL::Digest::SHA1.new)
191
+ crl = issue_crl([], 1, Time.now, Time.now+1600, [],
192
+ cert, @rsa2048, OpenSSL::Digest::SHA1.new)
193
+ assert_equal(false, crl.verify(@rsa1024))
194
+ assert_equal(true, crl.verify(@rsa2048))
195
+ assert_equal(false, crl_error_returns_false { crl.verify(@dsa256) })
196
+ assert_equal(false, crl_error_returns_false { crl.verify(@dsa512) })
197
+ crl.version = 0
198
+ assert_equal(false, crl.verify(@rsa2048))
199
+
200
+ cert = issue_cert(@ca, @dsa512, 1, Time.now, Time.now+3600, [],
201
+ nil, nil, OpenSSL::Digest::DSS1.new)
202
+ crl = issue_crl([], 1, Time.now, Time.now+1600, [],
203
+ cert, @dsa512, OpenSSL::Digest::DSS1.new)
204
+ assert_equal(false, crl_error_returns_false { crl.verify(@rsa1024) })
205
+ assert_equal(false, crl_error_returns_false { crl.verify(@rsa2048) })
206
+ assert_equal(false, crl.verify(@dsa256))
207
+ assert_equal(true, crl.verify(@dsa512))
208
+ crl.version = 0
209
+ assert_equal(false, crl.verify(@dsa512))
210
+ end
211
+
212
+ private
213
+
214
+ def crl_error_returns_false
215
+ yield
216
+ rescue OpenSSL::X509::CRLError
217
+ false
218
+ end
219
+ end
220
+
221
+ end
@@ -0,0 +1,69 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestX509Extension < Test::Unit::TestCase
6
+ def setup
7
+ @basic_constraints_value = OpenSSL::ASN1::Sequence([
8
+ OpenSSL::ASN1::Boolean(true), # CA
9
+ OpenSSL::ASN1::Integer(2) # pathlen
10
+ ])
11
+ @basic_constraints = OpenSSL::ASN1::Sequence([
12
+ OpenSSL::ASN1::ObjectId("basicConstraints"),
13
+ OpenSSL::ASN1::Boolean(true),
14
+ OpenSSL::ASN1::OctetString(@basic_constraints_value.to_der),
15
+ ])
16
+ end
17
+
18
+ def teardown
19
+ end
20
+
21
+ def test_new
22
+ ext = OpenSSL::X509::Extension.new(@basic_constraints.to_der)
23
+ assert_equal("basicConstraints", ext.oid)
24
+ assert_equal(true, ext.critical?)
25
+ assert_equal("CA:TRUE, pathlen:2", ext.value)
26
+
27
+ ext = OpenSSL::X509::Extension.new("2.5.29.19",
28
+ @basic_constraints_value.to_der, true)
29
+ assert_equal(@basic_constraints.to_der, ext.to_der)
30
+ end
31
+
32
+ def test_create_by_factory
33
+ ef = OpenSSL::X509::ExtensionFactory.new
34
+
35
+ bc = ef.create_extension("basicConstraints", "critical, CA:TRUE, pathlen:2")
36
+ assert_equal(@basic_constraints.to_der, bc.to_der)
37
+
38
+ bc = ef.create_extension("basicConstraints", "CA:TRUE, pathlen:2", true)
39
+ assert_equal(@basic_constraints.to_der, bc.to_der)
40
+
41
+ begin
42
+ ef.config = OpenSSL::Config.parse(<<-_end_of_cnf_)
43
+ [crlDistPts]
44
+ URI.1 = http://www.example.com/crl
45
+ URI.2 = ldap://ldap.example.com/cn=ca?certificateRevocationList;binary
46
+ _end_of_cnf_
47
+ rescue NotImplementedError
48
+ return
49
+ end
50
+
51
+ cdp = ef.create_extension("crlDistributionPoints", "@crlDistPts")
52
+ assert_equal(false, cdp.critical?)
53
+ assert_equal("crlDistributionPoints", cdp.oid)
54
+ assert_match(%{URI:http://www\.example\.com/crl}, cdp.value)
55
+ assert_match(
56
+ %r{URI:ldap://ldap\.example\.com/cn=ca\?certificateRevocationList;binary},
57
+ cdp.value)
58
+
59
+ cdp = ef.create_extension("crlDistributionPoints", "critical, @crlDistPts")
60
+ assert_equal(true, cdp.critical?)
61
+ assert_equal("crlDistributionPoints", cdp.oid)
62
+ assert_match(%{URI:http://www.example.com/crl}, cdp.value)
63
+ assert_match(
64
+ %r{URI:ldap://ldap.example.com/cn=ca\?certificateRevocationList;binary},
65
+ cdp.value)
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,296 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestX509Name < Test::Unit::TestCase
6
+ OpenSSL::ASN1::ObjectId.register(
7
+ "1.2.840.113549.1.9.1", "emailAddress", "emailAddress")
8
+ OpenSSL::ASN1::ObjectId.register(
9
+ "2.5.4.5", "serialNumber", "serialNumber")
10
+
11
+ def setup
12
+ @obj_type_tmpl = Hash.new(OpenSSL::ASN1::PRINTABLESTRING)
13
+ @obj_type_tmpl.update(OpenSSL::X509::Name::OBJECT_TYPE_TEMPLATE)
14
+ end
15
+
16
+ def teardown
17
+ end
18
+
19
+ def test_s_new
20
+ dn = [ ["C", "JP"], ["O", "example"], ["CN", "www.example.jp"] ]
21
+ name = OpenSSL::X509::Name.new(dn)
22
+ ary = name.to_a
23
+ assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
24
+ assert_equal("C", ary[0][0])
25
+ assert_equal("O", ary[1][0])
26
+ assert_equal("CN", ary[2][0])
27
+ assert_equal("JP", ary[0][1])
28
+ assert_equal("example", ary[1][1])
29
+ assert_equal("www.example.jp", ary[2][1])
30
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
31
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
32
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
33
+
34
+ dn = [
35
+ ["countryName", "JP"],
36
+ ["organizationName", "example"],
37
+ ["commonName", "www.example.jp"]
38
+ ]
39
+ name = OpenSSL::X509::Name.new(dn)
40
+ ary = name.to_a
41
+ assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
42
+ assert_equal("C", ary[0][0])
43
+ assert_equal("O", ary[1][0])
44
+ assert_equal("CN", ary[2][0])
45
+ assert_equal("JP", ary[0][1])
46
+ assert_equal("example", ary[1][1])
47
+ assert_equal("www.example.jp", ary[2][1])
48
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
49
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
50
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
51
+
52
+ name = OpenSSL::X509::Name.new(dn, @obj_type_tmpl)
53
+ ary = name.to_a
54
+ assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
55
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
56
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
57
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
58
+
59
+ dn = [
60
+ ["countryName", "JP", OpenSSL::ASN1::PRINTABLESTRING],
61
+ ["organizationName", "example", OpenSSL::ASN1::PRINTABLESTRING],
62
+ ["commonName", "www.example.jp", OpenSSL::ASN1::PRINTABLESTRING]
63
+ ]
64
+ name = OpenSSL::X509::Name.new(dn)
65
+ ary = name.to_a
66
+ assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
67
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
68
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
69
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
70
+
71
+ dn = [
72
+ ["DC", "org"],
73
+ ["DC", "ruby-lang"],
74
+ ["CN", "GOTOU Yuuzou"],
75
+ ["emailAddress", "gotoyuzo@ruby-lang.org"],
76
+ ["serialNumber", "123"],
77
+ ]
78
+ name = OpenSSL::X509::Name.new(dn)
79
+ ary = name.to_a
80
+ assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
81
+ assert_equal("DC", ary[0][0])
82
+ assert_equal("DC", ary[1][0])
83
+ assert_equal("CN", ary[2][0])
84
+ assert_equal("emailAddress", ary[3][0])
85
+ assert_equal("serialNumber", ary[4][0])
86
+ assert_equal("org", ary[0][1])
87
+ assert_equal("ruby-lang", ary[1][1])
88
+ assert_equal("GOTOU Yuuzou", ary[2][1])
89
+ assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
90
+ assert_equal("123", ary[4][1])
91
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
92
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
93
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
94
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
95
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
96
+
97
+ name_from_der = OpenSSL::X509::Name.new(name.to_der)
98
+ assert_equal(name_from_der.to_s, name.to_s)
99
+ assert_equal(name_from_der.to_a, name.to_a)
100
+ assert_equal(name_from_der.to_der, name.to_der)
101
+ end
102
+
103
+ def test_s_parse
104
+ dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org"
105
+ name = OpenSSL::X509::Name.parse(dn)
106
+ assert_equal(dn, name.to_s)
107
+ ary = name.to_a
108
+ assert_equal("DC", ary[0][0])
109
+ assert_equal("DC", ary[1][0])
110
+ assert_equal("CN", ary[2][0])
111
+ assert_equal("org", ary[0][1])
112
+ assert_equal("ruby-lang", ary[1][1])
113
+ assert_equal("www.ruby-lang.org", ary[2][1])
114
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
115
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
116
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
117
+
118
+ dn2 = "DC=org, DC=ruby-lang, CN=www.ruby-lang.org"
119
+ name = OpenSSL::X509::Name.parse(dn2)
120
+ ary = name.to_a
121
+ assert_equal(dn, name.to_s)
122
+ assert_equal("org", ary[0][1])
123
+ assert_equal("ruby-lang", ary[1][1])
124
+ assert_equal("www.ruby-lang.org", ary[2][1])
125
+
126
+ name = OpenSSL::X509::Name.parse(dn2, @obj_type_tmpl)
127
+ ary = name.to_a
128
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
129
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
130
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
131
+ end
132
+
133
+ def test_s_parse_rfc2253
134
+ scanner = OpenSSL::X509::Name::RFC2253DN.method(:scan)
135
+
136
+ assert_equal([["C", "JP"]], scanner.call("C=JP"))
137
+ assert_equal([
138
+ ["DC", "org"],
139
+ ["DC", "ruby-lang"],
140
+ ["CN", "GOTOU Yuuzou"],
141
+ ["emailAddress", "gotoyuzo@ruby-lang.org"],
142
+ ],
143
+ scanner.call(
144
+ "emailAddress=gotoyuzo@ruby-lang.org,CN=GOTOU Yuuzou,"+
145
+ "DC=ruby-lang,DC=org")
146
+ )
147
+
148
+ u8 = OpenSSL::ASN1::UTF8STRING
149
+ assert_equal([
150
+ ["DC", "org"],
151
+ ["DC", "ruby-lang"],
152
+ ["O", ",=+<>#;"],
153
+ ["O", ",=+<>#;"],
154
+ ["OU", ""],
155
+ ["OU", ""],
156
+ ["L", "aaa=\"bbb, ccc\""],
157
+ ["L", "aaa=\"bbb, ccc\""],
158
+ ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
159
+ ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
160
+ ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
161
+ ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265", u8],
162
+ ["2.5.4.3", "GOTOU, Yuuzou"],
163
+ ["2.5.4.3", "GOTOU, Yuuzou"],
164
+ ["2.5.4.3", "GOTOU, Yuuzou"],
165
+ ["2.5.4.3", "GOTOU, Yuuzou"],
166
+ ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
167
+ ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
168
+ ["1.2.840.113549.1.9.1", "gotoyuzo@ruby-lang.org"],
169
+ ["emailAddress", "gotoyuzo@ruby-lang.org"],
170
+ ],
171
+ scanner.call(
172
+ "emailAddress=gotoyuzo@ruby-lang.org," +
173
+ "1.2.840.113549.1.9.1=gotoyuzo@ruby-lang.org," +
174
+ 'CN=GOTOU \"gotoyuzo\" Yuuzou,' +
175
+ 'CN="GOTOU \"gotoyuzo\" Yuuzou",' +
176
+ '2.5.4.3=GOTOU\,\20Yuuzou,' +
177
+ '2.5.4.3=GOTOU\, Yuuzou,' +
178
+ '2.5.4.3="GOTOU, Yuuzou",' +
179
+ '2.5.4.3="GOTOU\, Yuuzou",' +
180
+ "CN=#0C0CE5BE8CE897A4E8A395E894B5," +
181
+ 'CN=\E5\BE\8C\E8\97\A4\E8\A3\95\E8\94\B5,' +
182
+ "CN=\"\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5\"," +
183
+ "CN=\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5," +
184
+ 'L=aaa\=\"bbb\, ccc\",' +
185
+ 'L="aaa=\"bbb, ccc\"",' +
186
+ 'OU=,' +
187
+ 'OU="",' +
188
+ 'O=\,\=\+\<\>\#\;,' +
189
+ 'O=",=+<>#;",' +
190
+ "DC=ruby-lang," +
191
+ "DC=org")
192
+ )
193
+
194
+ [
195
+ "DC=org+DC=jp",
196
+ "DC=org,DC=ruby-lang+DC=rubyist,DC=www"
197
+ ].each{|dn|
198
+ ex = scanner.call(dn) rescue $!
199
+ dn_r = Regexp.escape(dn)
200
+ assert_match(/^multi-valued RDN is not supported: #{dn_r}/, ex.message)
201
+ }
202
+
203
+ [
204
+ ["DC=org,DC=exapmle,CN", "CN"],
205
+ ["DC=org,DC=example,", ""],
206
+ ["DC=org,DC=exapmle,CN=www.example.org;", "CN=www.example.org;"],
207
+ ["DC=org,DC=exapmle,CN=#www.example.org", "CN=#www.example.org"],
208
+ ["DC=org,DC=exapmle,CN=#777777.example.org", "CN=#777777.example.org"],
209
+ ["DC=org,DC=exapmle,CN=\"www.example\".org", "CN=\"www.example\".org"],
210
+ ["DC=org,DC=exapmle,CN=www.\"example.org\"", "CN=www.\"example.org\""],
211
+ ["DC=org,DC=exapmle,CN=www.\"example\".org", "CN=www.\"example\".org"],
212
+ ].each{|dn, msg|
213
+ ex = scanner.call(dn) rescue $!
214
+ assert_match(/^malformed RDN: .*=>#{Regexp.escape(msg)}/, ex.message)
215
+ }
216
+
217
+ dn = "CN=www.ruby-lang.org,DC=ruby-lang,DC=org"
218
+ name = OpenSSL::X509::Name.parse_rfc2253(dn)
219
+ assert_equal(dn, name.to_s(OpenSSL::X509::Name::RFC2253))
220
+ ary = name.to_a
221
+ assert_equal("DC", ary[0][0])
222
+ assert_equal("DC", ary[1][0])
223
+ assert_equal("CN", ary[2][0])
224
+ assert_equal("org", ary[0][1])
225
+ assert_equal("ruby-lang", ary[1][1])
226
+ assert_equal("www.ruby-lang.org", ary[2][1])
227
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
228
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
229
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
230
+ end
231
+
232
+ def test_add_entry
233
+ dn = [
234
+ ["DC", "org"],
235
+ ["DC", "ruby-lang"],
236
+ ["CN", "GOTOU Yuuzou"],
237
+ ["emailAddress", "gotoyuzo@ruby-lang.org"],
238
+ ["serialNumber", "123"],
239
+ ]
240
+ name = OpenSSL::X509::Name.new
241
+ dn.each{|attr| name.add_entry(*attr) }
242
+ ary = name.to_a
243
+ assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
244
+ assert_equal("DC", ary[0][0])
245
+ assert_equal("DC", ary[1][0])
246
+ assert_equal("CN", ary[2][0])
247
+ assert_equal("emailAddress", ary[3][0])
248
+ assert_equal("serialNumber", ary[4][0])
249
+ assert_equal("org", ary[0][1])
250
+ assert_equal("ruby-lang", ary[1][1])
251
+ assert_equal("GOTOU Yuuzou", ary[2][1])
252
+ assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
253
+ assert_equal("123", ary[4][1])
254
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
255
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
256
+ assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
257
+ assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
258
+ assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
259
+ end
260
+
261
+ def test_equals2
262
+ n1 = OpenSSL::X509::Name.parse 'CN=a'
263
+ n2 = OpenSSL::X509::Name.parse 'CN=a'
264
+
265
+ assert_equal n1, n2
266
+ end
267
+
268
+ def test_spaceship
269
+ n1 = OpenSSL::X509::Name.parse 'CN=a'
270
+ n2 = OpenSSL::X509::Name.parse 'CN=b'
271
+
272
+ assert_equal -1, n1 <=> n2
273
+ end
274
+
275
+ def name_hash(name)
276
+ # OpenSSL 1.0.0 uses SHA1 for canonical encoding (not just a der) of
277
+ # X509Name for X509_NAME_hash.
278
+ name.respond_to?(:hash_old) ? name.hash_old : name.hash
279
+ end
280
+
281
+ def test_hash
282
+ dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org"
283
+ name = OpenSSL::X509::Name.parse(dn)
284
+ d = Digest::MD5.digest(name.to_der)
285
+ expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24
286
+ assert_equal(expected, name_hash(name))
287
+ #
288
+ dn = "/DC=org/DC=ruby-lang/CN=baz.ruby-lang.org"
289
+ name = OpenSSL::X509::Name.parse(dn)
290
+ d = Digest::MD5.digest(name.to_der)
291
+ expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24
292
+ assert_equal(expected, name_hash(name))
293
+ end
294
+ end
295
+
296
+ end