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
@@ -200,19 +200,6 @@ class OpenSSL::TestSSL < Test::Unit::TestCase
200
200
 
201
201
  assert_raise(ArgumentError) { ssl.sysread(-1) }
202
202
 
203
- # syswrite and sysread
204
- ITERATIONS.times{|i|
205
- str = "x" * 100 + "\n"
206
- ssl.syswrite(str)
207
- assert_equal(str, ssl.sysread(str.size))
208
-
209
- str = "x" * i * 100 + "\n"
210
- buf = ""
211
- ssl.syswrite(str)
212
- assert_equal(buf.object_id, ssl.sysread(str.size, buf).object_id)
213
- assert_equal(str, buf)
214
- }
215
-
216
203
  # puts and gets
217
204
  ITERATIONS.times{
218
205
  str = "x" * 100 + "\n"
@@ -237,6 +224,14 @@ class OpenSSL::TestSSL < Test::Unit::TestCase
237
224
  }
238
225
  end
239
226
 
227
+ def sysread_size(ssl, size)
228
+ buf = ''
229
+ while buf.bytesize < size
230
+ buf += ssl.sysread(size - buf.bytesize)
231
+ end
232
+ buf
233
+ end
234
+
240
235
  def test_sysread_chunks
241
236
  args = {}
242
237
  args[:server_proc] = proc { |ctx, ssl|
@@ -257,11 +252,11 @@ class OpenSSL::TestSSL < Test::Unit::TestCase
257
252
  ssl.sync_close = true
258
253
  ssl.connect
259
254
  ssl.syswrite("hello\n")
260
- assert_equal("0" * 200, ssl.sysread(200))
261
- assert_equal("0" * 200, ssl.sysread(200))
262
- assert_equal("0" * 200, ssl.sysread(200))
263
- assert_equal("0" * 200, ssl.sysread(200))
264
- assert_equal("1" * 200, ssl.sysread(200))
255
+ assert_equal("0" * 200, sysread_size(ssl, 200))
256
+ assert_equal("0" * 200, sysread_size(ssl, 200))
257
+ assert_equal("0" * 200, sysread_size(ssl, 200))
258
+ assert_equal("0" * 200, sysread_size(ssl, 200))
259
+ assert_equal("1" * 200, sysread_size(ssl, 200))
265
260
  ssl.close
266
261
  }
267
262
  end
@@ -285,12 +280,14 @@ class OpenSSL::TestSSL < Test::Unit::TestCase
285
280
  read = ssl.sysread(str.size, buf)
286
281
  assert(!read.empty?)
287
282
  assert_equal(buf.object_id, read.object_id)
288
- assert_equal(str, buf)
283
+ assert_equal(str[0, buf.bytesize], buf)
284
+ sysread_size(ssl, str.bytesize - buf.bytesize) # drop unread bytes
289
285
 
290
286
  ssl.syswrite(str)
291
287
  read = ssl.sysread(str.size, nil)
292
288
  assert(!read.empty?)
293
- assert_equal(str, read)
289
+ assert_equal(str[0, read.bytesize], read)
290
+ sysread_size(ssl, str.bytesize - read.bytesize) # drop unread bytes
294
291
  }
295
292
  ssl.close
296
293
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,81 @@
1
+ require "socket"
2
+ require "thread"
3
+ require "openssl"
4
+ require File.join(File.dirname(__FILE__), "utils.rb")
5
+
6
+ def get_pem(io=$stdin)
7
+ buf = ""
8
+ while line = io.gets
9
+ if /^-----BEGIN / =~ line
10
+ buf << line
11
+ break
12
+ end
13
+ end
14
+ while line = io.gets
15
+ buf << line
16
+ if /^-----END / =~ line
17
+ break
18
+ end
19
+ end
20
+ return buf
21
+ end
22
+
23
+ def make_key(pem)
24
+ begin
25
+ return OpenSSL::PKey::RSA.new(pem)
26
+ rescue
27
+ return OpenSSL::PKey::DSA.new(pem)
28
+ end
29
+ end
30
+
31
+ ca_cert = OpenSSL::X509::Certificate.new(get_pem)
32
+ ssl_cert = OpenSSL::X509::Certificate.new(get_pem)
33
+ ssl_key = make_key(get_pem)
34
+ port = Integer(ARGV.shift)
35
+ verify_mode = Integer(ARGV.shift)
36
+ start_immediately = (/yes/ =~ ARGV.shift)
37
+
38
+ store = OpenSSL::X509::Store.new
39
+ store.add_cert(ca_cert)
40
+ store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT
41
+ ctx = OpenSSL::SSL::SSLContext.new
42
+ ctx.cert_store = store
43
+ #ctx.extra_chain_cert = [ ca_cert ]
44
+ ctx.cert = ssl_cert
45
+ ctx.key = ssl_key
46
+ ctx.verify_mode = verify_mode
47
+
48
+ Socket.do_not_reverse_lookup = true
49
+ tcps = nil
50
+ 100.times{|i|
51
+ begin
52
+ tcps = TCPServer.new("0.0.0.0", port+i)
53
+ port = port + i
54
+ break
55
+ rescue Errno::EADDRINUSE
56
+ next
57
+ end
58
+ }
59
+ ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
60
+ ssls.start_immediately = start_immediately
61
+
62
+ $stdout.sync = true
63
+ $stdout.puts Process.pid
64
+ $stdout.puts port
65
+
66
+ loop do
67
+ ssl = ssls.accept rescue next
68
+ Thread.start{
69
+ q = Queue.new
70
+ th = Thread.start{ ssl.write(q.shift) while true }
71
+ while line = ssl.gets
72
+ if line =~ /^STARTTLS$/
73
+ ssl.accept
74
+ next
75
+ end
76
+ q.push(line)
77
+ end
78
+ th.kill if q.empty?
79
+ ssl.close
80
+ }
81
+ end
@@ -0,0 +1,589 @@
1
+ require_relative 'utils'
2
+
3
+ class OpenSSL::TestASN1 < Test::Unit::TestCase
4
+ def test_decode
5
+ subj = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=TestCA")
6
+ key = OpenSSL::TestUtils::TEST_KEY_RSA1024
7
+ now = Time.at(Time.now.to_i) # suppress usec
8
+ s = 0xdeadbeafdeadbeafdeadbeafdeadbeaf
9
+ exts = [
10
+ ["basicConstraints","CA:TRUE,pathlen:1",true],
11
+ ["keyUsage","keyCertSign, cRLSign",true],
12
+ ["subjectKeyIdentifier","hash",false],
13
+ ]
14
+ dgst = OpenSSL::Digest::SHA1.new
15
+ cert = OpenSSL::TestUtils.issue_cert(
16
+ subj, key, s, now, now+3600, exts, nil, nil, dgst)
17
+
18
+
19
+ asn1 = OpenSSL::ASN1.decode(cert)
20
+ assert_equal(OpenSSL::ASN1::Sequence, asn1.class)
21
+ assert_equal(3, asn1.value.size)
22
+ tbs_cert, sig_alg, sig_val = *asn1.value
23
+
24
+ assert_equal(OpenSSL::ASN1::Sequence, tbs_cert.class)
25
+ assert_equal(8, tbs_cert.value.size)
26
+
27
+ version = tbs_cert.value[0]
28
+ assert_equal(:CONTEXT_SPECIFIC, version.tag_class)
29
+ assert_equal(0, version.tag)
30
+ assert_equal(1, version.value.size)
31
+ assert_equal(OpenSSL::ASN1::Integer, version.value[0].class)
32
+ assert_equal(2, version.value[0].value)
33
+
34
+ serial = tbs_cert.value[1]
35
+ assert_equal(OpenSSL::ASN1::Integer, serial.class)
36
+ assert_equal(0xdeadbeafdeadbeafdeadbeafdeadbeaf, serial.value)
37
+
38
+ sig = tbs_cert.value[2]
39
+ assert_equal(OpenSSL::ASN1::Sequence, sig.class)
40
+ assert_equal(2, sig.value.size)
41
+ assert_equal(OpenSSL::ASN1::ObjectId, sig.value[0].class)
42
+ assert_equal("1.2.840.113549.1.1.5", sig.value[0].oid)
43
+ assert_equal(OpenSSL::ASN1::Null, sig.value[1].class)
44
+
45
+ dn = tbs_cert.value[3] # issuer
46
+ assert_equal(subj.hash, OpenSSL::X509::Name.new(dn).hash)
47
+ assert_equal(OpenSSL::ASN1::Sequence, dn.class)
48
+ assert_equal(3, dn.value.size)
49
+ assert_equal(OpenSSL::ASN1::Set, dn.value[0].class)
50
+ assert_equal(OpenSSL::ASN1::Set, dn.value[1].class)
51
+ assert_equal(OpenSSL::ASN1::Set, dn.value[2].class)
52
+ assert_equal(1, dn.value[0].value.size)
53
+ assert_equal(1, dn.value[1].value.size)
54
+ assert_equal(1, dn.value[2].value.size)
55
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[0].value[0].class)
56
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[1].value[0].class)
57
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[2].value[0].class)
58
+ assert_equal(2, dn.value[0].value[0].value.size)
59
+ assert_equal(2, dn.value[1].value[0].value.size)
60
+ assert_equal(2, dn.value[2].value[0].value.size)
61
+ oid, value = *dn.value[0].value[0].value
62
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
63
+ assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
64
+ assert_equal(OpenSSL::ASN1::IA5String, value.class)
65
+ assert_equal("org", value.value)
66
+ oid, value = *dn.value[1].value[0].value
67
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
68
+ assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
69
+ assert_equal(OpenSSL::ASN1::IA5String, value.class)
70
+ assert_equal("ruby-lang", value.value)
71
+ oid, value = *dn.value[2].value[0].value
72
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
73
+ assert_equal("2.5.4.3", oid.oid)
74
+ assert_equal(OpenSSL::ASN1::UTF8String, value.class)
75
+ assert_equal("TestCA", value.value)
76
+
77
+ validity = tbs_cert.value[4]
78
+ assert_equal(OpenSSL::ASN1::Sequence, validity.class)
79
+ assert_equal(2, validity.value.size)
80
+ assert_equal(OpenSSL::ASN1::UTCTime, validity.value[0].class)
81
+ assert_equal(now, validity.value[0].value)
82
+ assert_equal(OpenSSL::ASN1::UTCTime, validity.value[1].class)
83
+ assert_equal(now+3600, validity.value[1].value)
84
+
85
+ dn = tbs_cert.value[5] # subject
86
+ assert_equal(subj.hash, OpenSSL::X509::Name.new(dn).hash)
87
+ assert_equal(OpenSSL::ASN1::Sequence, dn.class)
88
+ assert_equal(3, dn.value.size)
89
+ assert_equal(OpenSSL::ASN1::Set, dn.value[0].class)
90
+ assert_equal(OpenSSL::ASN1::Set, dn.value[1].class)
91
+ assert_equal(OpenSSL::ASN1::Set, dn.value[2].class)
92
+ assert_equal(1, dn.value[0].value.size)
93
+ assert_equal(1, dn.value[1].value.size)
94
+ assert_equal(1, dn.value[2].value.size)
95
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[0].value[0].class)
96
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[1].value[0].class)
97
+ assert_equal(OpenSSL::ASN1::Sequence, dn.value[2].value[0].class)
98
+ assert_equal(2, dn.value[0].value[0].value.size)
99
+ assert_equal(2, dn.value[1].value[0].value.size)
100
+ assert_equal(2, dn.value[2].value[0].value.size)
101
+ oid, value = *dn.value[0].value[0].value
102
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
103
+ assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
104
+ assert_equal(OpenSSL::ASN1::IA5String, value.class)
105
+ assert_equal("org", value.value)
106
+ oid, value = *dn.value[1].value[0].value
107
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
108
+ assert_equal("0.9.2342.19200300.100.1.25", oid.oid)
109
+ assert_equal(OpenSSL::ASN1::IA5String, value.class)
110
+ assert_equal("ruby-lang", value.value)
111
+ oid, value = *dn.value[2].value[0].value
112
+ assert_equal(OpenSSL::ASN1::ObjectId, oid.class)
113
+ assert_equal("2.5.4.3", oid.oid)
114
+ assert_equal(OpenSSL::ASN1::UTF8String, value.class)
115
+ assert_equal("TestCA", value.value)
116
+
117
+ pkey = tbs_cert.value[6]
118
+ assert_equal(OpenSSL::ASN1::Sequence, pkey.class)
119
+ assert_equal(2, pkey.value.size)
120
+ assert_equal(OpenSSL::ASN1::Sequence, pkey.value[0].class)
121
+ assert_equal(2, pkey.value[0].value.size)
122
+ assert_equal(OpenSSL::ASN1::ObjectId, pkey.value[0].value[0].class)
123
+ assert_equal("1.2.840.113549.1.1.1", pkey.value[0].value[0].oid)
124
+ assert_equal(OpenSSL::ASN1::BitString, pkey.value[1].class)
125
+ assert_equal(0, pkey.value[1].unused_bits)
126
+ spkey = OpenSSL::ASN1.decode(pkey.value[1].value)
127
+ assert_equal(OpenSSL::ASN1::Sequence, spkey.class)
128
+ assert_equal(2, spkey.value.size)
129
+ assert_equal(OpenSSL::ASN1::Integer, spkey.value[0].class)
130
+ assert_equal(143085709396403084580358323862163416700436550432664688288860593156058579474547937626086626045206357324274536445865308750491138538454154232826011964045825759324933943290377903384882276841880081931690695505836279972214003660451338124170055999155993192881685495391496854691199517389593073052473319331505702779271, spkey.value[0].value)
131
+ assert_equal(OpenSSL::ASN1::Integer, spkey.value[1].class)
132
+ assert_equal(65537, spkey.value[1].value)
133
+
134
+ extensions = tbs_cert.value[7]
135
+ assert_equal(:CONTEXT_SPECIFIC, extensions.tag_class)
136
+ assert_equal(3, extensions.tag)
137
+ assert_equal(1, extensions.value.size)
138
+ assert_equal(OpenSSL::ASN1::Sequence, extensions.value[0].class)
139
+ assert_equal(3, extensions.value[0].value.size)
140
+
141
+ ext = extensions.value[0].value[0] # basicConstraints
142
+ assert_equal(OpenSSL::ASN1::Sequence, ext.class)
143
+ assert_equal(3, ext.value.size)
144
+ assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
145
+ assert_equal("2.5.29.19", ext.value[0].oid)
146
+ assert_equal(OpenSSL::ASN1::Boolean, ext.value[1].class)
147
+ assert_equal(true, ext.value[1].value)
148
+ assert_equal(OpenSSL::ASN1::OctetString, ext.value[2].class)
149
+ extv = OpenSSL::ASN1.decode(ext.value[2].value)
150
+ assert_equal(OpenSSL::ASN1::Sequence, extv.class)
151
+ assert_equal(2, extv.value.size)
152
+ assert_equal(OpenSSL::ASN1::Boolean, extv.value[0].class)
153
+ assert_equal(true, extv.value[0].value)
154
+ assert_equal(OpenSSL::ASN1::Integer, extv.value[1].class)
155
+ assert_equal(1, extv.value[1].value)
156
+
157
+ ext = extensions.value[0].value[1] # keyUsage
158
+ assert_equal(OpenSSL::ASN1::Sequence, ext.class)
159
+ assert_equal(3, ext.value.size)
160
+ assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
161
+ assert_equal("2.5.29.15", ext.value[0].oid)
162
+ assert_equal(OpenSSL::ASN1::Boolean, ext.value[1].class)
163
+ assert_equal(true, ext.value[1].value)
164
+ assert_equal(OpenSSL::ASN1::OctetString, ext.value[2].class)
165
+ extv = OpenSSL::ASN1.decode(ext.value[2].value)
166
+ assert_equal(OpenSSL::ASN1::BitString, extv.class)
167
+ str = "\000"; str[0] = 0b00000110.chr
168
+ assert_equal(str, extv.value)
169
+
170
+ ext = extensions.value[0].value[2] # subjetKeyIdentifier
171
+ assert_equal(OpenSSL::ASN1::Sequence, ext.class)
172
+ assert_equal(2, ext.value.size)
173
+ assert_equal(OpenSSL::ASN1::ObjectId, ext.value[0].class)
174
+ assert_equal("2.5.29.14", ext.value[0].oid)
175
+ assert_equal(OpenSSL::ASN1::OctetString, ext.value[1].class)
176
+ extv = OpenSSL::ASN1.decode(ext.value[1].value)
177
+ assert_equal(OpenSSL::ASN1::OctetString, extv.class)
178
+ sha1 = OpenSSL::Digest::SHA1.new
179
+ sha1.update(pkey.value[1].value)
180
+ assert_equal(sha1.digest, extv.value)
181
+
182
+ assert_equal(OpenSSL::ASN1::Sequence, sig_alg.class)
183
+ assert_equal(2, sig_alg.value.size)
184
+ assert_equal(OpenSSL::ASN1::ObjectId, pkey.value[0].value[0].class)
185
+ assert_equal("1.2.840.113549.1.1.1", pkey.value[0].value[0].oid)
186
+ assert_equal(OpenSSL::ASN1::Null, pkey.value[0].value[1].class)
187
+
188
+ assert_equal(OpenSSL::ASN1::BitString, sig_val.class)
189
+ cululated_sig = key.sign(OpenSSL::Digest::SHA1.new, tbs_cert.to_der)
190
+ assert_equal(cululated_sig, sig_val.value)
191
+ end
192
+
193
+ def test_encode_boolean
194
+ encode_decode_test(OpenSSL::ASN1::Boolean, [true, false])
195
+ end
196
+
197
+ def test_encode_integer
198
+ encode_decode_test(OpenSSL::ASN1::Integer, [72, -127, -128, 128, -1, 0, 1, -(2**12345), 2**12345])
199
+ end
200
+
201
+ def encode_decode_test(type, values)
202
+ values.each do |v|
203
+ assert_equal(v, OpenSSL::ASN1.decode(type.new(v).to_der).value)
204
+ end
205
+ end
206
+
207
+ def test_decode_pem #should fail gracefully (cf. [ruby-dev:44542])
208
+ pem = <<-_EOS_
209
+ -----BEGIN CERTIFICATE-----
210
+ MIIC8zCCAdugAwIBAgIBATANBgkqhkiG9w0BAQUFADA9MRMwEQYKCZImiZPyLGQB
211
+ GRYDb3JnMRkwFwYKCZImiZPyLGQBGRYJcnVieS1sYW5nMQswCQYDVQQDDAJDQTAe
212
+ Fw0xMTA5MjUxMzQ4MjZaFw0xMTA5MjUxNDQ4MjZaMD0xEzARBgoJkiaJk/IsZAEZ
213
+ FgNvcmcxGTAXBgoJkiaJk/IsZAEZFglydWJ5LWxhbmcxCzAJBgNVBAMMAkNBMIIB
214
+ IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuV9ht9J7k4NBs38jOXvvTKY9
215
+ gW8nLICSno5EETR1cuF7i4pNs9I1QJGAFAX0BEO4KbzXmuOvfCpD3CU+Slp1enen
216
+ fzq/t/e/1IRW0wkJUJUFQign4CtrkJL+P07yx18UjyPlBXb81ApEmAB5mrJVSrWm
217
+ qbjs07JbuS4QQGGXLc+Su96DkYKmSNVjBiLxVVSpyZfAY3hD37d60uG+X8xdW5v6
218
+ 8JkRFIhdGlb6JL8fllf/A/blNwdJOhVr9mESHhwGjwfSeTDPfd8ZLE027E5lyAVX
219
+ 9KZYcU00mOX+fdxOSnGqS/8JDRh0EPHDL15RcJjV2J6vZjPb0rOYGDoMcH+94wID
220
+ AQABMA0GCSqGSIb3DQEBBQUAA4IBAQAiAtrIr1pLX4GYN5klviWKb8HC9ICYuAFI
221
+ NfE3FwqzErEVXotuMe3yPVyB3Bv6rjYY/x5EtS5+WPTbHlvHZTkfcsnTpizcn4mW
222
+ dJ6dDRaFCHt1YKKjUxqBt9lvvrc3nReYZN/P+s1mrDhWzGf8iPZgf8sFUHgnaK7W
223
+ CXRVXmPFgCDRNpDDVQ0MQkr509yYfTH+dujNzqTCwSvkyZFyQ7Oe8Yj0VR6kquG3
224
+ rEzBQ0F9dUyqQ9gyRg8KHhDfv9HzT1d/rnUZMkoombwYBRIUChGCYV0GnJcan2Zm
225
+ /93PnPG1IvPjYNd5VlV+sXSnaxQn974HRCsMv7jA8BD6IgSaX6WK
226
+ -----END CERTIFICATE-----
227
+ _EOS_
228
+ assert_raise(OpenSSL::ASN1::ASN1Error) { OpenSSL::ASN1.decode(pem) }
229
+ assert_raise(OpenSSL::ASN1::ASN1Error) { OpenSSL::ASN1.decode_all(pem) }
230
+ end
231
+
232
+ def test_primitive_cannot_set_infinite_length
233
+ begin
234
+ prim = OpenSSL::ASN1::Integer.new(50)
235
+ assert_equal(false, prim.infinite_length)
236
+ prim.infinite_length = true
237
+ flunk('Could set infinite length on primitive value')
238
+ rescue NoMethodError => e
239
+ #ok
240
+ end
241
+ end
242
+
243
+ def test_decode_all
244
+ expected = %w{ 02 01 01 02 01 02 02 01 03 }
245
+ raw = [expected.join('')].pack('H*')
246
+ ary = OpenSSL::ASN1.decode_all(raw)
247
+ assert_equal(3, ary.size)
248
+ ary.each_with_index do |asn1, i|
249
+ assert_universal(OpenSSL::ASN1::INTEGER, asn1)
250
+ assert_equal(i + 1, asn1.value)
251
+ end
252
+ end
253
+
254
+ def test_create_inf_length_primitive
255
+ expected = %w{ 24 80 04 01 61 00 00 }
256
+ raw = [expected.join('')].pack('H*')
257
+ val = OpenSSL::ASN1::OctetString.new('a')
258
+ cons = OpenSSL::ASN1::Constructive.new([val,
259
+ OpenSSL::ASN1::EndOfContent.new],
260
+ OpenSSL::ASN1::OCTET_STRING,
261
+ nil,
262
+ :UNIVERSAL)
263
+ cons.infinite_length = true
264
+ assert_equal(nil, cons.tagging)
265
+ assert_equal(raw, cons.to_der)
266
+ asn1 = OpenSSL::ASN1.decode(raw)
267
+ assert(asn1.infinite_length)
268
+ assert_equal(raw, asn1.to_der)
269
+ end
270
+
271
+ def test_cons_without_inf_length_forbidden
272
+ assert_raise(OpenSSL::ASN1::ASN1Error) do
273
+ val = OpenSSL::ASN1::OctetString.new('a')
274
+ cons = OpenSSL::ASN1::Constructive.new([val],
275
+ OpenSSL::ASN1::OCTET_STRING,
276
+ nil,
277
+ :UNIVERSAL)
278
+ cons.to_der
279
+ end
280
+ end
281
+
282
+ def test_cons_without_array_forbidden
283
+ assert_raise(OpenSSL::ASN1::ASN1Error) do
284
+ val = OpenSSL::ASN1::OctetString.new('a')
285
+ cons = OpenSSL::ASN1::Constructive.new(val,
286
+ OpenSSL::ASN1::OCTET_STRING,
287
+ nil,
288
+ :UNIVERSAL)
289
+ cons.infinite_length = true
290
+ cons.to_der
291
+ end
292
+ end
293
+
294
+ def test_parse_empty_sequence
295
+ expected = %w{ A0 07 30 02 30 00 02 01 00 }
296
+ raw = [expected.join('')].pack('H*')
297
+ asn1 = OpenSSL::ASN1.decode(raw)
298
+ assert_equal(raw, asn1.to_der)
299
+ assert_equal(2, asn1.value.size)
300
+ seq = asn1.value[0]
301
+ assert_equal(1, seq.value.size)
302
+ inner_seq = seq.value[0]
303
+ assert_equal(0, inner_seq.value.size)
304
+ end
305
+
306
+ def test_parse_tagged_0_infinite
307
+ expected = %w{ 30 80 02 01 01 80 01 02 00 00 }
308
+ raw = [expected.join('')].pack('H*')
309
+ asn1 = OpenSSL::ASN1.decode(raw)
310
+ assert_equal(3, asn1.value.size)
311
+ int = asn1.value[0]
312
+ assert_universal(OpenSSL::ASN1::INTEGER, int)
313
+ tagged = asn1.value[1]
314
+ assert_equal(0, tagged.tag)
315
+ assert_universal(OpenSSL::ASN1::EOC, asn1.value[2])
316
+ assert_equal(raw, asn1.to_der)
317
+ end
318
+
319
+ def test_seq_infinite_length
320
+ begin
321
+ content = [ OpenSSL::ASN1::Null.new(nil),
322
+ OpenSSL::ASN1::EndOfContent.new ]
323
+ cons = OpenSSL::ASN1::Sequence.new(content)
324
+ cons.infinite_length = true
325
+ expected = %w{ 30 80 05 00 00 00 }
326
+ raw = [expected.join('')].pack('H*')
327
+ assert_equal(raw, cons.to_der)
328
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
329
+ end
330
+ end
331
+
332
+ def test_set_infinite_length
333
+ begin
334
+ content = [ OpenSSL::ASN1::Null.new(nil),
335
+ OpenSSL::ASN1::EndOfContent.new() ]
336
+ cons = OpenSSL::ASN1::Set.new(content)
337
+ cons.infinite_length = true
338
+ expected = %w{ 31 80 05 00 00 00 }
339
+ raw = [expected.join('')].pack('H*')
340
+ assert_equal(raw, cons.to_der)
341
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
342
+ end
343
+ end
344
+
345
+ def test_octet_string_infinite_length
346
+ begin
347
+ octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
348
+ OpenSSL::ASN1::EndOfContent.new() ]
349
+ cons = OpenSSL::ASN1::Constructive.new(
350
+ octets,
351
+ OpenSSL::ASN1::OCTET_STRING,
352
+ nil,
353
+ :UNIVERSAL)
354
+ cons.infinite_length = true
355
+ expected = %w{ 24 80 04 03 61 61 61 00 00 }
356
+ raw = [expected.join('')].pack('H*')
357
+ assert_equal(raw, cons.to_der)
358
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
359
+ end
360
+ end
361
+
362
+ def test_prim_explicit_tagging
363
+ begin
364
+ oct_str = OpenSSL::ASN1::OctetString.new("a", 0, :EXPLICIT)
365
+ expected = %w{ A0 03 04 01 61 }
366
+ raw = [expected.join('')].pack('H*')
367
+ assert_equal(raw, oct_str.to_der)
368
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
369
+ end
370
+ end
371
+
372
+ def test_prim_explicit_tagging_tag_class
373
+ begin
374
+ oct_str = OpenSSL::ASN1::OctetString.new("a", 0, :EXPLICIT)
375
+ oct_str2 = OpenSSL::ASN1::OctetString.new(
376
+ "a",
377
+ 0,
378
+ :EXPLICIT,
379
+ :CONTEXT_SPECIFIC)
380
+ assert_equal(oct_str.to_der, oct_str2.to_der)
381
+ end
382
+ end
383
+
384
+ def test_prim_implicit_tagging
385
+ begin
386
+ int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT)
387
+ expected = %w{ 80 01 01 }
388
+ raw = [expected.join('')].pack('H*')
389
+ assert_equal(raw, int.to_der)
390
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
391
+ end
392
+ end
393
+
394
+ def test_prim_implicit_tagging_tag_class
395
+ begin
396
+ int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT)
397
+ int2 = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT, :CONTEXT_SPECIFIC);
398
+ assert_equal(int.to_der, int2.to_der)
399
+ end
400
+ end
401
+
402
+ def test_cons_explicit_tagging
403
+ begin
404
+ content = [ OpenSSL::ASN1::PrintableString.new('abc') ]
405
+ seq = OpenSSL::ASN1::Sequence.new(content, 2, :EXPLICIT)
406
+ expected = %w{ A2 07 30 05 13 03 61 62 63 }
407
+ raw = [expected.join('')].pack('H*')
408
+ assert_equal(raw, seq.to_der)
409
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
410
+ end
411
+ end
412
+
413
+ def test_cons_explicit_tagging_inf_length
414
+ begin
415
+ content = [ OpenSSL::ASN1::PrintableString.new('abc') ,
416
+ OpenSSL::ASN1::EndOfContent.new() ]
417
+ seq = OpenSSL::ASN1::Sequence.new(content, 2, :EXPLICIT)
418
+ seq.infinite_length = true
419
+ expected = %w{ A2 80 30 80 13 03 61 62 63 00 00 00 00 }
420
+ raw = [expected.join('')].pack('H*')
421
+ assert_equal(raw, seq.to_der)
422
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
423
+ end
424
+ end
425
+
426
+ def test_cons_implicit_tagging
427
+ begin
428
+ content = [ OpenSSL::ASN1::Null.new(nil) ]
429
+ seq = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT)
430
+ expected = %w{ A1 02 05 00 }
431
+ raw = [expected.join('')].pack('H*')
432
+ assert_equal(raw, seq.to_der)
433
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
434
+ end
435
+ end
436
+
437
+ def test_cons_implicit_tagging_inf_length
438
+ begin
439
+ content = [ OpenSSL::ASN1::Null.new(nil),
440
+ OpenSSL::ASN1::EndOfContent.new() ]
441
+ seq = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT)
442
+ seq.infinite_length = true
443
+ expected = %w{ A1 80 05 00 00 00 }
444
+ raw = [expected.join('')].pack('H*')
445
+ assert_equal(raw, seq.to_der)
446
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
447
+ end
448
+ end
449
+
450
+ def test_octet_string_infinite_length_explicit_tagging
451
+ begin
452
+ octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
453
+ OpenSSL::ASN1::EndOfContent.new() ]
454
+ cons = OpenSSL::ASN1::Constructive.new(
455
+ octets,
456
+ 1,
457
+ :EXPLICIT)
458
+ cons.infinite_length = true
459
+ expected = %w{ A1 80 24 80 04 03 61 61 61 00 00 00 00 }
460
+ raw = [expected.join('')].pack('H*')
461
+ assert_equal(raw, cons.to_der)
462
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
463
+ end
464
+ end
465
+
466
+ def test_octet_string_infinite_length_implicit_tagging
467
+ begin
468
+ octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
469
+ OpenSSL::ASN1::EndOfContent.new() ]
470
+ cons = OpenSSL::ASN1::Constructive.new(
471
+ octets,
472
+ 0,
473
+ :IMPLICIT)
474
+ cons.infinite_length = true
475
+ expected = %w{ A0 80 04 03 61 61 61 00 00 }
476
+ raw = [expected.join('')].pack('H*')
477
+ assert_equal(raw, cons.to_der)
478
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
479
+ end
480
+ end
481
+
482
+ def test_recursive_octet_string_infinite_length
483
+ begin
484
+ octets_sub1 = [ OpenSSL::ASN1::OctetString.new("\x01"),
485
+ OpenSSL::ASN1::EndOfContent.new() ]
486
+ octets_sub2 = [ OpenSSL::ASN1::OctetString.new("\x02"),
487
+ OpenSSL::ASN1::EndOfContent.new() ]
488
+ container1 = OpenSSL::ASN1::Constructive.new(
489
+ octets_sub1,
490
+ OpenSSL::ASN1::OCTET_STRING,
491
+ nil,
492
+ :UNIVERSAL)
493
+ container1.infinite_length = true
494
+ container2 = OpenSSL::ASN1::Constructive.new(
495
+ octets_sub2,
496
+ OpenSSL::ASN1::OCTET_STRING,
497
+ nil,
498
+ :UNIVERSAL)
499
+ container2.infinite_length = true
500
+ octets3 = OpenSSL::ASN1::OctetString.new("\x03")
501
+
502
+ octets = [ container1, container2, octets3,
503
+ OpenSSL::ASN1::EndOfContent.new() ]
504
+ cons = OpenSSL::ASN1::Constructive.new(
505
+ octets,
506
+ OpenSSL::ASN1::OCTET_STRING,
507
+ nil,
508
+ :UNIVERSAL)
509
+ cons.infinite_length = true
510
+ expected = %w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 }
511
+ raw = [expected.join('')].pack('H*')
512
+ assert_equal(raw, cons.to_der)
513
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
514
+ end
515
+ end
516
+
517
+ def test_bit_string_infinite_length
518
+ begin
519
+ content = [ OpenSSL::ASN1::BitString.new("\x01"),
520
+ OpenSSL::ASN1::EndOfContent.new() ]
521
+ cons = OpenSSL::ASN1::Constructive.new(
522
+ content,
523
+ OpenSSL::ASN1::BIT_STRING,
524
+ nil,
525
+ :UNIVERSAL)
526
+ cons.infinite_length = true
527
+ expected = %w{ 23 80 03 02 00 01 00 00 }
528
+ raw = [expected.join('')].pack('H*')
529
+ assert_equal(raw, cons.to_der)
530
+ assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
531
+ end
532
+ end
533
+
534
+ def test_primitive_inf_length
535
+ assert_raises(OpenSSL::ASN1::ASN1Error) do
536
+ spec = %w{ 02 80 02 01 01 00 00 }
537
+ raw = [spec.join('')].pack('H*')
538
+ OpenSSL::ASN1.decode(raw)
539
+ OpenSSL::ASN1.decode_all(raw)
540
+ end
541
+ end
542
+
543
+ def test_recursive_octet_string_parse
544
+ test = %w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 }
545
+ raw = [test.join('')].pack('H*')
546
+ asn1 = OpenSSL::ASN1.decode(raw)
547
+ assert_equal(OpenSSL::ASN1::Constructive, asn1.class)
548
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, asn1)
549
+ assert_equal(true, asn1.infinite_length)
550
+ assert_equal(4, asn1.value.size)
551
+ nested1 = asn1.value[0]
552
+ assert_equal(OpenSSL::ASN1::Constructive, nested1.class)
553
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, nested1)
554
+ assert_equal(true, nested1.infinite_length)
555
+ assert_equal(2, nested1.value.size)
556
+ oct1 = nested1.value[0]
557
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, oct1)
558
+ assert_equal(false, oct1.infinite_length)
559
+ assert_universal(OpenSSL::ASN1::EOC, nested1.value[1])
560
+ assert_equal(false, nested1.value[1].infinite_length)
561
+ nested2 = asn1.value[1]
562
+ assert_equal(OpenSSL::ASN1::Constructive, nested2.class)
563
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, nested2)
564
+ assert_equal(true, nested2.infinite_length)
565
+ assert_equal(2, nested2.value.size)
566
+ oct2 = nested2.value[0]
567
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, oct2)
568
+ assert_equal(false, oct2.infinite_length)
569
+ assert_universal(OpenSSL::ASN1::EOC, nested2.value[1])
570
+ assert_equal(false, nested2.value[1].infinite_length)
571
+ oct3 = asn1.value[2]
572
+ assert_universal(OpenSSL::ASN1::OCTET_STRING, oct3)
573
+ assert_equal(false, oct3.infinite_length)
574
+ assert_universal(OpenSSL::ASN1::EOC, asn1.value[3])
575
+ assert_equal(false, asn1.value[3].infinite_length)
576
+ end
577
+
578
+ private
579
+
580
+ def assert_universal(tag, asn1)
581
+ assert_equal(tag, asn1.tag)
582
+ if asn1.respond_to?(:tagging)
583
+ assert_nil(asn1.tagging)
584
+ end
585
+ assert_equal(:UNIVERSAL, asn1.tag_class)
586
+ end
587
+
588
+ end if defined?(OpenSSL)
589
+