jruby-openssl 0.7.4 → 0.7.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. data/.gemtest +0 -0
  2. data/History.txt +51 -14
  3. data/Manifest.txt +136 -79
  4. data/README.txt +1 -1
  5. data/Rakefile +17 -8
  6. data/lib/{openssl → 1.8/openssl}/bn.rb +2 -2
  7. data/lib/{openssl → 1.8/openssl}/buffering.rb +3 -1
  8. data/lib/{openssl → 1.8/openssl}/cipher.rb +0 -0
  9. data/lib/{openssl → 1.8/openssl}/config.rb +1 -1
  10. data/lib/{openssl → 1.8/openssl}/digest.rb +2 -2
  11. data/lib/{openssl → 1.8/openssl}/pkcs7.rb +0 -0
  12. data/lib/{openssl/ssl.rb → 1.8/openssl/ssl-internal.rb} +2 -2
  13. data/lib/1.8/openssl/ssl.rb +1 -0
  14. data/lib/{openssl/x509.rb → 1.8/openssl/x509-internal.rb} +8 -9
  15. data/lib/1.8/openssl/x509.rb +1 -0
  16. data/lib/{openssl.rb → 1.8/openssl.rb} +2 -11
  17. data/lib/1.9/openssl/bn.rb +35 -0
  18. data/lib/1.9/openssl/buffering.rb +448 -0
  19. data/lib/1.9/openssl/cipher.rb +65 -0
  20. data/lib/1.9/openssl/config.rb +313 -0
  21. data/lib/1.9/openssl/digest.rb +72 -0
  22. data/lib/1.9/openssl/ssl-internal.rb +177 -0
  23. data/lib/1.9/openssl/ssl.rb +2 -0
  24. data/lib/1.9/openssl/x509-internal.rb +158 -0
  25. data/lib/1.9/openssl/x509.rb +2 -0
  26. data/lib/1.9/openssl.rb +22 -0
  27. data/lib/{jopenssl → shared/jopenssl}/version.rb +1 -1
  28. data/lib/shared/jopenssl.jar +0 -0
  29. data/lib/{openssl → shared/openssl}/dummy.rb +0 -0
  30. data/lib/{openssl → shared/openssl}/dummyssl.rb +0 -0
  31. data/lib/shared/openssl/pkcs12.rb +50 -0
  32. data/lib/shared/openssl/ssl.rb +1 -0
  33. data/lib/shared/openssl/x509.rb +1 -0
  34. data/lib/shared/openssl.rb +20 -0
  35. data/test/{openssl → 1.8}/ssl_server.rb +0 -0
  36. data/test/{openssl → 1.8}/test_asn1.rb +15 -0
  37. data/test/{openssl → 1.8}/test_cipher.rb +0 -0
  38. data/test/{openssl → 1.8}/test_config.rb +0 -0
  39. data/test/{openssl → 1.8}/test_digest.rb +0 -0
  40. data/test/{openssl → 1.8}/test_ec.rb +0 -0
  41. data/test/{openssl → 1.8}/test_hmac.rb +0 -0
  42. data/test/{openssl → 1.8}/test_ns_spki.rb +0 -0
  43. data/test/{openssl → 1.8}/test_pair.rb +10 -2
  44. data/test/{openssl → 1.8}/test_pkcs7.rb +0 -0
  45. data/test/{openssl → 1.8}/test_pkey_rsa.rb +0 -0
  46. data/test/{openssl → 1.8}/test_ssl.rb +17 -20
  47. data/test/{openssl → 1.8}/test_x509cert.rb +0 -0
  48. data/test/{openssl → 1.8}/test_x509crl.rb +0 -0
  49. data/test/{openssl → 1.8}/test_x509ext.rb +0 -0
  50. data/test/{openssl → 1.8}/test_x509name.rb +0 -0
  51. data/test/{openssl → 1.8}/test_x509req.rb +0 -0
  52. data/test/{openssl → 1.8}/test_x509store.rb +0 -0
  53. data/test/{openssl → 1.8}/utils.rb +0 -0
  54. data/test/1.9/ssl_server.rb +81 -0
  55. data/test/1.9/test_asn1.rb +589 -0
  56. data/test/1.9/test_bn.rb +23 -0
  57. data/test/1.9/test_buffering.rb +88 -0
  58. data/test/1.9/test_cipher.rb +107 -0
  59. data/test/1.9/test_config.rb +288 -0
  60. data/test/1.9/test_digest.rb +118 -0
  61. data/test/1.9/test_engine.rb +15 -0
  62. data/test/1.9/test_hmac.rb +32 -0
  63. data/test/1.9/test_ns_spki.rb +50 -0
  64. data/test/1.9/test_ocsp.rb +47 -0
  65. data/test/1.9/test_pair.rb +257 -0
  66. data/test/1.9/test_pkcs12.rb +209 -0
  67. data/test/1.9/test_pkcs7.rb +151 -0
  68. data/test/1.9/test_pkey_dh.rb +72 -0
  69. data/test/1.9/test_pkey_dsa.rb +224 -0
  70. data/test/1.9/test_pkey_ec.rb +182 -0
  71. data/test/1.9/test_pkey_rsa.rb +244 -0
  72. data/test/1.9/test_ssl.rb +455 -0
  73. data/test/1.9/test_ssl_session.rb +327 -0
  74. data/test/1.9/test_x509cert.rb +217 -0
  75. data/test/1.9/test_x509crl.rb +221 -0
  76. data/test/1.9/test_x509ext.rb +69 -0
  77. data/test/1.9/test_x509name.rb +296 -0
  78. data/test/1.9/test_x509req.rb +150 -0
  79. data/test/1.9/test_x509store.rb +229 -0
  80. data/test/1.9/utils.rb +304 -0
  81. data/test/fixture/ids_in_subject_rdn_set.pem +31 -0
  82. data/test/fixture/purpose/ca/ca_config.rb +1 -1
  83. data/test/fixture/purpose/ca/gen_cert.rb +128 -0
  84. data/test/fixture/purpose/ca/newcerts/4_cert.pem +19 -0
  85. data/test/fixture/purpose/ca/serial +1 -1
  86. data/test/fixture/purpose/sslserver_no_dsig_in_keyUsage.pem +19 -0
  87. data/test/ruby/envutil.rb +208 -0
  88. data/test/ruby/ut_eof.rb +128 -0
  89. data/test/test_certificate.rb +9 -0
  90. data/test/test_java.rb +1 -1
  91. data/test/test_openssl.rb +1 -1
  92. data/test/test_pkcs7.rb +16 -0
  93. data/test/test_pkey_dsa.rb +180 -0
  94. data/test/test_pkey_rsa.rb +298 -0
  95. data/test/test_ssl.rb +1 -1
  96. data/test/test_x509store.rb +8 -0
  97. metadata +133 -73
  98. data/lib/jopenssl.jar +0 -0
  99. data/test/test_pkey.rb +0 -204
@@ -0,0 +1,107 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestCipher < Test::Unit::TestCase
6
+ def setup
7
+ @c1 = OpenSSL::Cipher::Cipher.new("DES-EDE3-CBC")
8
+ @c2 = OpenSSL::Cipher::DES.new(:EDE3, "CBC")
9
+ @key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
10
+ @iv = "\0\0\0\0\0\0\0\0"
11
+ @hexkey = "0000000000000000000000000000000000000000000000"
12
+ @hexiv = "0000000000000000"
13
+ @data = "DATA"
14
+ end
15
+
16
+ def teardown
17
+ @c1 = @c2 = nil
18
+ end
19
+
20
+ def test_crypt
21
+ @c1.encrypt.pkcs5_keyivgen(@key, @iv)
22
+ @c2.encrypt.pkcs5_keyivgen(@key, @iv)
23
+ s1 = @c1.update(@data) + @c1.final
24
+ s2 = @c2.update(@data) + @c2.final
25
+ assert_equal(s1, s2, "encrypt")
26
+
27
+ @c1.decrypt.pkcs5_keyivgen(@key, @iv)
28
+ @c2.decrypt.pkcs5_keyivgen(@key, @iv)
29
+ assert_equal(@data, @c1.update(s1)+@c1.final, "decrypt")
30
+ assert_equal(@data, @c2.update(s2)+@c2.final, "decrypt")
31
+ end
32
+
33
+ def test_info
34
+ assert_equal("DES-EDE3-CBC", @c1.name, "name")
35
+ assert_equal("DES-EDE3-CBC", @c2.name, "name")
36
+ assert_kind_of(Fixnum, @c1.key_len, "key_len")
37
+ assert_kind_of(Fixnum, @c1.iv_len, "iv_len")
38
+ end
39
+
40
+ def test_dup
41
+ assert_equal(@c1.name, @c1.dup.name, "dup")
42
+ assert_equal(@c1.name, @c1.clone.name, "clone")
43
+ @c1.encrypt
44
+ @c1.key = @key
45
+ @c1.iv = @iv
46
+ tmpc = @c1.dup
47
+ s1 = @c1.update(@data) + @c1.final
48
+ s2 = tmpc.update(@data) + tmpc.final
49
+ assert_equal(s1, s2, "encrypt dup")
50
+ end
51
+
52
+ def test_reset
53
+ @c1.encrypt
54
+ @c1.key = @key
55
+ @c1.iv = @iv
56
+ s1 = @c1.update(@data) + @c1.final
57
+ @c1.reset
58
+ s2 = @c1.update(@data) + @c1.final
59
+ assert_equal(s1, s2, "encrypt reset")
60
+ end
61
+
62
+ def test_empty_data
63
+ @c1.encrypt
64
+ assert_raise(ArgumentError){ @c1.update("") }
65
+ end
66
+
67
+ def test_initialize
68
+ assert_raise(RuntimeError) {@c1.__send__(:initialize, "DES-EDE3-CBC")}
69
+ assert_raise(RuntimeError) {OpenSSL::Cipher.allocate.final}
70
+ end
71
+
72
+ if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00907000
73
+ def test_ciphers
74
+ OpenSSL::Cipher.ciphers.each{|name|
75
+ assert(OpenSSL::Cipher::Cipher.new(name).is_a?(OpenSSL::Cipher::Cipher))
76
+ }
77
+ end
78
+
79
+ # This test fails without unlimited US_export.policy
80
+ def test_AES
81
+ pt = File.read(__FILE__)
82
+ %w(ECB CBC CFB OFB).each{|mode|
83
+ c1 = OpenSSL::Cipher::AES256.new(mode)
84
+ c1.encrypt
85
+ c1.pkcs5_keyivgen("passwd")
86
+ ct = c1.update(pt) + c1.final
87
+
88
+ c2 = OpenSSL::Cipher::AES256.new(mode)
89
+ c2.decrypt
90
+ c2.pkcs5_keyivgen("passwd")
91
+ assert_equal(pt, c2.update(ct) + c2.final)
92
+ }
93
+ end
94
+
95
+ # In JRuby key must be provided first. (CipherError)
96
+ def NOT_test_AES_crush
97
+ 500.times do
98
+ assert_nothing_raised("[Bug #2768]") do
99
+ # it caused OpenSSL SEGV by uninitialized key
100
+ OpenSSL::Cipher::AES128.new("ECB").update "." * 17
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,288 @@
1
+ require_relative 'utils'
2
+
3
+ class OpenSSL::TestConfig < Test::Unit::TestCase
4
+ def setup
5
+ file = Tempfile.open("openssl.cnf")
6
+ file << <<__EOD__
7
+ HOME = .
8
+ [ ca ]
9
+ default_ca = CA_default
10
+ [ CA_default ]
11
+ dir = ./demoCA
12
+ certs = ./certs
13
+ __EOD__
14
+ file.close
15
+ @it = OpenSSL::Config.new(file.path)
16
+ end
17
+
18
+ def test_constants
19
+ assert(defined?(OpenSSL::Config::DEFAULT_CONFIG_FILE))
20
+ assert_nothing_raised do
21
+ OpenSSL::Config.load(OpenSSL::Config::DEFAULT_CONFIG_FILE)
22
+ end
23
+ end
24
+
25
+ def test_s_parse
26
+ c = OpenSSL::Config.parse('')
27
+ assert_equal("[ default ]\n\n", c.to_s)
28
+ c = OpenSSL::Config.parse(@it.to_s)
29
+ assert_equal(['CA_default', 'ca', 'default'], c.sections.sort)
30
+ end
31
+
32
+ def test_s_parse_format
33
+ c = OpenSSL::Config.parse(<<__EOC__)
34
+ baz =qx\t # "baz = qx"
35
+
36
+ foo::bar = baz # shortcut section::key format
37
+ default::bar = baz # ditto
38
+ a=\t \t # "a = ": trailing spaces are ignored
39
+ =b # " = b": empty key
40
+ =c # " = c": empty key (override the above line)
41
+ d= # "c = ": trailing comment is ignored
42
+
43
+ sq = 'foo''b\\'ar'
44
+ dq ="foo""''\\""
45
+ dq2 = foo""bar
46
+ esc=a\\r\\n\\b\\tb
47
+ foo\\bar = foo\\b\\\\ar
48
+ foo\\bar::foo\\bar = baz
49
+ [default1 default2]\t\t # space is allowed in section name
50
+ fo =b ar # space allowed in value
51
+ [emptysection]
52
+ [doller ]
53
+ foo=bar
54
+ bar = $(foo)
55
+ baz = 123$(default::bar)456${foo}798
56
+ qux = ${baz}
57
+ quxx = $qux.$qux
58
+ __EOC__
59
+ assert_equal(['default', 'default1 default2', 'doller', 'emptysection', 'foo', 'foo\\bar'], c.sections.sort)
60
+ assert_equal(['', 'a', 'bar', 'baz', 'd', 'dq', 'dq2', 'esc', 'foo\\bar', 'sq'], c['default'].keys.sort)
61
+ assert_equal('c', c['default'][''])
62
+ assert_equal('', c['default']['a'])
63
+ assert_equal('qx', c['default']['baz'])
64
+ assert_equal('', c['default']['d'])
65
+ assert_equal('baz', c['default']['bar'])
66
+ assert_equal("foob'ar", c['default']['sq'])
67
+ assert_equal("foo''\"", c['default']['dq'])
68
+ assert_equal("foobar", c['default']['dq2'])
69
+ assert_equal("a\r\n\b\tb", c['default']['esc'])
70
+ assert_equal("foo\b\\ar", c['default']['foo\\bar'])
71
+ assert_equal('baz', c['foo']['bar'])
72
+ assert_equal('baz', c['foo\\bar']['foo\\bar'])
73
+ assert_equal('b ar', c['default1 default2']['fo'])
74
+
75
+ # dolloer
76
+ assert_equal('bar', c['doller']['foo'])
77
+ assert_equal('bar', c['doller']['bar'])
78
+ assert_equal('123baz456bar798', c['doller']['baz'])
79
+ assert_equal('123baz456bar798', c['doller']['qux'])
80
+ assert_equal('123baz456bar798.123baz456bar798', c['doller']['quxx'])
81
+
82
+ excn = assert_raise(OpenSSL::ConfigError) do
83
+ OpenSSL::Config.parse("foo = $bar")
84
+ end
85
+ assert_equal("error in line 1: variable has no value", excn.message)
86
+
87
+ excn = assert_raise(OpenSSL::ConfigError) do
88
+ OpenSSL::Config.parse("foo = $(bar")
89
+ end
90
+ assert_equal("error in line 1: no close brace", excn.message)
91
+
92
+ excn = assert_raise(OpenSSL::ConfigError) do
93
+ OpenSSL::Config.parse("f o =b ar # no space in key")
94
+ end
95
+ assert_equal("error in line 1: missing equal sign", excn.message)
96
+
97
+ excn = assert_raise(OpenSSL::ConfigError) do
98
+ OpenSSL::Config.parse(<<__EOC__)
99
+ # comment 1 # comments
100
+
101
+ #
102
+ # comment 2
103
+ \t#comment 3
104
+ [second ]\t
105
+ [third # section not terminated
106
+ __EOC__
107
+ end
108
+ assert_equal("error in line 7: missing close square bracket", excn.message)
109
+ end
110
+
111
+ def test_s_load
112
+ # alias of new
113
+ c = OpenSSL::Config.load
114
+ assert_equal("", c.to_s)
115
+ assert_equal([], c.sections)
116
+ #
117
+ file = Tempfile.open("openssl.cnf")
118
+ file.close
119
+ c = OpenSSL::Config.load(file.path)
120
+ assert_equal("[ default ]\n\n", c.to_s)
121
+ assert_equal(['default'], c.sections)
122
+ end
123
+
124
+ def test_initialize
125
+ c = OpenSSL::Config.new
126
+ assert_equal("", c.to_s)
127
+ assert_equal([], c.sections)
128
+ end
129
+
130
+ def test_initialize_with_empty_file
131
+ file = Tempfile.open("openssl.cnf")
132
+ file.close
133
+ c = OpenSSL::Config.new(file.path)
134
+ assert_equal("[ default ]\n\n", c.to_s)
135
+ assert_equal(['default'], c.sections)
136
+ end
137
+
138
+ def test_initialize_with_example_file
139
+ assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
140
+ end
141
+
142
+ def test_get_value
143
+ assert_equal('CA_default', @it.get_value('ca', 'default_ca'))
144
+ assert_equal(nil, @it.get_value('ca', 'no such key'))
145
+ assert_equal(nil, @it.get_value('no such section', 'no such key'))
146
+ assert_equal('.', @it.get_value('', 'HOME'))
147
+ assert_raise(TypeError) do
148
+ @it.get_value(nil, 'HOME') # not allowed unlike Config#value
149
+ end
150
+ # fallback to 'default' ugly...
151
+ assert_equal('.', @it.get_value('unknown', 'HOME'))
152
+ end
153
+
154
+ def test_get_value_ENV
155
+ key = ENV.keys.first
156
+ assert_not_nil(key) # make sure we have at least one ENV var.
157
+ assert_equal(ENV[key], @it.get_value('ENV', key))
158
+ end
159
+
160
+ def test_value
161
+ # supress deprecation warnings
162
+ OpenSSL::TestUtils.silent do
163
+ assert_equal('CA_default', @it.value('ca', 'default_ca'))
164
+ assert_equal(nil, @it.value('ca', 'no such key'))
165
+ assert_equal(nil, @it.value('no such section', 'no such key'))
166
+ assert_equal('.', @it.value('', 'HOME'))
167
+ assert_equal('.', @it.value(nil, 'HOME'))
168
+ assert_equal('.', @it.value('HOME'))
169
+ # fallback to 'default' ugly...
170
+ assert_equal('.', @it.value('unknown', 'HOME'))
171
+ end
172
+ end
173
+
174
+ def test_value_ENV
175
+ OpenSSL::TestUtils.silent do
176
+ key = ENV.keys.first
177
+ assert_not_nil(key) # make sure we have at least one ENV var.
178
+ assert_equal(ENV[key], @it.value('ENV', key))
179
+ end
180
+ end
181
+
182
+ def test_aref
183
+ assert_equal({'HOME' => '.'}, @it['default'])
184
+ assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it['CA_default'])
185
+ assert_equal({}, @it['no_such_section'])
186
+ assert_equal({}, @it[''])
187
+ end
188
+
189
+ def test_section
190
+ OpenSSL::TestUtils.silent do
191
+ assert_equal({'HOME' => '.'}, @it.section('default'))
192
+ assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it.section('CA_default'))
193
+ assert_equal({}, @it.section('no_such_section'))
194
+ assert_equal({}, @it.section(''))
195
+ end
196
+ end
197
+
198
+ def test_sections
199
+ assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
200
+ @it['new_section'] = {'foo' => 'bar'}
201
+ assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
202
+ @it['new_section'] = {}
203
+ assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
204
+ end
205
+
206
+ def test_add_value
207
+ c = OpenSSL::Config.new
208
+ assert_equal("", c.to_s)
209
+ # add key
210
+ c.add_value('default', 'foo', 'bar')
211
+ assert_equal("[ default ]\nfoo=bar\n\n", c.to_s)
212
+ # add another key
213
+ c.add_value('default', 'baz', 'qux')
214
+ assert_equal('bar', c['default']['foo'])
215
+ assert_equal('qux', c['default']['baz'])
216
+ # update the value
217
+ c.add_value('default', 'baz', 'quxxx')
218
+ assert_equal('bar', c['default']['foo'])
219
+ assert_equal('quxxx', c['default']['baz'])
220
+ # add section and key
221
+ c.add_value('section', 'foo', 'bar')
222
+ assert_equal('bar', c['default']['foo'])
223
+ assert_equal('quxxx', c['default']['baz'])
224
+ assert_equal('bar', c['section']['foo'])
225
+ end
226
+
227
+ def test_aset
228
+ @it['foo'] = {'bar' => 'baz'}
229
+ assert_equal({'bar' => 'baz'}, @it['foo'])
230
+ @it['foo'] = {'bar' => 'qux', 'baz' => 'quxx'}
231
+ assert_equal({'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
232
+
233
+ # OpenSSL::Config is add only for now.
234
+ @it['foo'] = {'foo' => 'foo'}
235
+ assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
236
+ # you cannot override or remove any section and key.
237
+ @it['foo'] = {}
238
+ assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
239
+ end
240
+
241
+ def test_each
242
+ # each returns [section, key, value] array.
243
+ ary = @it.map { |e| e }.sort { |a, b| a[0] <=> b[0] }
244
+ assert_equal(4, ary.size)
245
+ assert_equal('CA_default', ary[0][0])
246
+ assert_equal('CA_default', ary[1][0])
247
+ assert_equal(["ca", "default_ca", "CA_default"], ary[2])
248
+ assert_equal(["default", "HOME", "."], ary[3])
249
+ end
250
+
251
+ def test_to_s
252
+ c = OpenSSL::Config.parse("[empty]\n")
253
+ assert_equal("[ default ]\n\n[ empty ]\n\n", c.to_s)
254
+ end
255
+
256
+ def test_inspect
257
+ assert_match(/#<OpenSSL::Config sections=\[.*\]>/, @it.inspect)
258
+ end
259
+
260
+ def test_freeze
261
+ c = OpenSSL::Config.new
262
+ c['foo'] = [['key', 'value']]
263
+ c.freeze
264
+
265
+ bug = '[ruby-core:18377]'
266
+ # RuntimeError for 1.9, TypeError for 1.8
267
+ e = assert_raise(TypeError, bug) do
268
+ c['foo'] = [['key', 'wrong']]
269
+ end
270
+ assert_match(/can't modify/, e.message, bug)
271
+ end
272
+
273
+ def test_dup
274
+ assert(!@it.sections.empty?)
275
+ c = @it.dup
276
+ assert_equal(@it.sections.sort, c.sections.sort)
277
+ @it['newsection'] = {'a' => 'b'}
278
+ assert_not_equal(@it.sections.sort, c.sections.sort)
279
+ end
280
+
281
+ def test_clone
282
+ assert(!@it.sections.empty?)
283
+ c = @it.clone
284
+ assert_equal(@it.sections.sort, c.sections.sort)
285
+ @it['newsection'] = {'a' => 'b'}
286
+ assert_not_equal(@it.sections.sort, c.sections.sort)
287
+ end
288
+ end if defined?(OpenSSL)
@@ -0,0 +1,118 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestDigest < Test::Unit::TestCase
6
+ def setup
7
+ @d1 = OpenSSL::Digest::Digest::new("MD5")
8
+ @d2 = OpenSSL::Digest::MD5.new
9
+ @md = Digest::MD5.new
10
+ @data = "DATA"
11
+ end
12
+
13
+ def teardown
14
+ @d1 = @d2 = @md = nil
15
+ end
16
+
17
+ def test_digest
18
+ assert_equal(@md.digest, @d1.digest)
19
+ assert_equal(@md.hexdigest, @d1.hexdigest)
20
+ @d1 << @data
21
+ @d2 << @data
22
+ @md << @data
23
+ assert_equal(@md.digest, @d1.digest)
24
+ assert_equal(@md.hexdigest, @d1.hexdigest)
25
+ assert_equal(@d1.digest, @d2.digest)
26
+ assert_equal(@d1.hexdigest, @d2.hexdigest)
27
+ assert_equal(@md.digest, OpenSSL::Digest::MD5.digest(@data))
28
+ assert_equal(@md.hexdigest, OpenSSL::Digest::MD5.hexdigest(@data))
29
+ end
30
+
31
+ def test_eql
32
+ assert(@d1 == @d2, "==")
33
+ d = @d1.clone
34
+ assert(d == @d1, "clone")
35
+ end
36
+
37
+ def test_info
38
+ assert_equal("MD5", @d1.name, "name")
39
+ assert_equal("MD5", @d2.name, "name")
40
+ assert_equal(16, @d1.size, "size")
41
+ end
42
+
43
+ def test_dup
44
+ @d1.update(@data)
45
+ assert_equal(@d1.name, @d1.dup.name, "dup")
46
+ assert_equal(@d1.name, @d1.clone.name, "clone")
47
+ assert_equal(@d1.digest, @d1.clone.digest, "clone .digest")
48
+ end
49
+
50
+ def test_reset
51
+ @d1.update(@data)
52
+ dig1 = @d1.digest
53
+ @d1.reset
54
+ @d1.update(@data)
55
+ dig2 = @d1.digest
56
+ assert_equal(dig1, dig2, "reset")
57
+ end
58
+
59
+ def test_digest_constants
60
+ algs = %w(DSS1 MD4 MD5 RIPEMD160 SHA SHA1)
61
+ if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
62
+ algs += %w(SHA224 SHA256 SHA384 SHA512)
63
+ end
64
+ algs.each do |alg|
65
+ assert_not_nil(OpenSSL::Digest.new(alg))
66
+ klass = OpenSSL::Digest.const_get(alg)
67
+ assert_not_nil(klass.new)
68
+ end
69
+ end
70
+
71
+ def test_digest_by_oid_and_name
72
+ check_digest(OpenSSL::ASN1::ObjectId.new("MD5"))
73
+ check_digest(OpenSSL::ASN1::ObjectId.new("SHA1"))
74
+ end
75
+
76
+ if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
77
+ def encode16(str)
78
+ str.unpack("H*").first
79
+ end
80
+
81
+ def test_098_features
82
+ sha224_a = "abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5"
83
+ sha256_a = "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
84
+ sha384_a = "54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31"
85
+ sha512_a = "1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75"
86
+
87
+ assert_equal(sha224_a, OpenSSL::Digest::SHA224.hexdigest("a"))
88
+ assert_equal(sha256_a, OpenSSL::Digest::SHA256.hexdigest("a"))
89
+ assert_equal(sha384_a, OpenSSL::Digest::SHA384.hexdigest("a"))
90
+ assert_equal(sha512_a, OpenSSL::Digest::SHA512.hexdigest("a"))
91
+
92
+ assert_equal(sha224_a, encode16(OpenSSL::Digest::SHA224.digest("a")))
93
+ assert_equal(sha256_a, encode16(OpenSSL::Digest::SHA256.digest("a")))
94
+ assert_equal(sha384_a, encode16(OpenSSL::Digest::SHA384.digest("a")))
95
+ assert_equal(sha512_a, encode16(OpenSSL::Digest::SHA512.digest("a")))
96
+ end
97
+
98
+ def test_digest_by_oid_and_name_sha2
99
+ check_digest(OpenSSL::ASN1::ObjectId.new("SHA224"))
100
+ check_digest(OpenSSL::ASN1::ObjectId.new("SHA256"))
101
+ check_digest(OpenSSL::ASN1::ObjectId.new("SHA384"))
102
+ check_digest(OpenSSL::ASN1::ObjectId.new("SHA512"))
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def check_digest(oid)
109
+ d = OpenSSL::Digest.new(oid.sn)
110
+ assert_not_nil(d)
111
+ d = OpenSSL::Digest.new(oid.ln)
112
+ assert_not_nil(d)
113
+ d = OpenSSL::Digest.new(oid.oid)
114
+ assert_not_nil(d)
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL) && defined?(OpenSSL::Engine)
4
+
5
+ class OpenSSL::TestEngine < Test::Unit::TestCase
6
+
7
+ def test_engines_free # [ruby-dev:44173]
8
+ OpenSSL::Engine.load
9
+ OpenSSL::Engine.engines
10
+ OpenSSL::Engine.engines
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'utils'
2
+
3
+ class OpenSSL::TestHMAC < Test::Unit::TestCase
4
+ def setup
5
+ @digest = OpenSSL::Digest::MD5
6
+ @key = "KEY"
7
+ @data = "DATA"
8
+ @h1 = OpenSSL::HMAC.new(@key, @digest.new)
9
+ @h2 = OpenSSL::HMAC.new(@key, "MD5")
10
+ end
11
+
12
+ def teardown
13
+ end
14
+
15
+ def test_hmac
16
+ @h1.update(@data)
17
+ @h2.update(@data)
18
+ assert_equal(@h1.digest, @h2.digest)
19
+
20
+ assert_equal(OpenSSL::HMAC.digest(@digest.new, @key, @data), @h1.digest, "digest")
21
+ assert_equal(OpenSSL::HMAC.hexdigest(@digest.new, @key, @data), @h1.hexdigest, "hexdigest")
22
+
23
+ assert_equal(OpenSSL::HMAC.digest("MD5", @key, @data), @h2.digest, "digest")
24
+ assert_equal(OpenSSL::HMAC.hexdigest("MD5", @key, @data), @h2.hexdigest, "hexdigest")
25
+ end
26
+
27
+ def test_dup
28
+ @h1.update(@data)
29
+ h = @h1.dup
30
+ assert_equal(@h1.digest, h.digest, "dup digest")
31
+ end
32
+ end if defined?(OpenSSL)
@@ -0,0 +1,50 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestNSSPI < Test::Unit::TestCase
6
+ def setup
7
+ # This request data is adopt from the specification of
8
+ # "Netscape Extensions for User Key Generation".
9
+ # -- http://wp.netscape.com/eng/security/comm4-keygen.html
10
+ @b64 = "MIHFMHEwXDANBgkqhkiG9w0BAQEFAANLADBIAkEAnX0TILJrOMUue+PtwBRE6XfV"
11
+ @b64 << "WtKQbsshxk5ZhcUwcwyvcnIq9b82QhJdoACdD34rqfCAIND46fXKQUnb0mvKzQID"
12
+ @b64 << "AQABFhFNb3ppbGxhSXNNeUZyaWVuZDANBgkqhkiG9w0BAQQFAANBAAKv2Eex2n/S"
13
+ @b64 << "r/7iJNroWlSzSMtTiQTEB+ADWHGj9u1xrUrOilq/o2cuQxIfZcNZkYAkWP4DubqW"
14
+ @b64 << "i0//rgBvmco="
15
+ end
16
+
17
+ def test_build_data
18
+ key1 = OpenSSL::TestUtils::TEST_KEY_RSA1024
19
+ key2 = OpenSSL::TestUtils::TEST_KEY_RSA2048
20
+ spki = OpenSSL::Netscape::SPKI.new
21
+ spki.challenge = "RandomString"
22
+ spki.public_key = key1.public_key
23
+ spki.sign(key1, OpenSSL::Digest::SHA1.new)
24
+ assert(spki.verify(spki.public_key))
25
+ assert(spki.verify(key1.public_key))
26
+ assert(!spki.verify(key2.public_key))
27
+
28
+ der = spki.to_der
29
+ spki = OpenSSL::Netscape::SPKI.new(der)
30
+ assert_equal("RandomString", spki.challenge)
31
+ assert_equal(key1.public_key.to_der, spki.public_key.to_der)
32
+ assert(spki.verify(spki.public_key))
33
+ end
34
+
35
+ def test_decode_data
36
+ spki = OpenSSL::Netscape::SPKI.new(@b64)
37
+ assert_equal(@b64, spki.to_pem)
38
+ assert_equal(@b64.unpack("m").first, spki.to_der)
39
+ assert_equal("MozillaIsMyFriend", spki.challenge)
40
+ assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
41
+
42
+ spki = OpenSSL::Netscape::SPKI.new(@b64.unpack("m").first)
43
+ assert_equal(@b64, spki.to_pem)
44
+ assert_equal(@b64.unpack("m").first, spki.to_der)
45
+ assert_equal("MozillaIsMyFriend", spki.challenge)
46
+ assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,47 @@
1
+ require_relative "utils"
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestOCSP < Test::Unit::TestCase
6
+ def setup
7
+ ca_subj = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=TestCA")
8
+ ca_key = OpenSSL::TestUtils::TEST_KEY_RSA1024
9
+ ca_serial = 0xabcabcabcabc
10
+
11
+ subj = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=TestCert")
12
+ @key = OpenSSL::TestUtils::TEST_KEY_RSA1024
13
+ serial = 0xabcabcabcabd
14
+
15
+ now = Time.at(Time.now.to_i) # suppress usec
16
+ dgst = OpenSSL::Digest::SHA1.new
17
+
18
+ @ca_cert = OpenSSL::TestUtils.issue_cert(
19
+ ca_subj, ca_key, ca_serial, now, now+3600, [], nil, nil, dgst)
20
+ @cert = OpenSSL::TestUtils.issue_cert(
21
+ subj, @key, serial, now, now+3600, [], @ca_cert, nil, dgst)
22
+ end
23
+
24
+ def test_new_certificate_id
25
+ cid = OpenSSL::OCSP::CertificateId.new(@cert, @ca_cert)
26
+ assert_kind_of OpenSSL::OCSP::CertificateId, cid
27
+ assert_equal @cert.serial, cid.serial
28
+ end
29
+
30
+ def test_new_certificate_id_with_digest
31
+ cid = OpenSSL::OCSP::CertificateId.new(@cert, @ca_cert, OpenSSL::Digest::SHA256.new)
32
+ assert_kind_of OpenSSL::OCSP::CertificateId, cid
33
+ assert_equal @cert.serial, cid.serial
34
+ end if defined?(OpenSSL::Digest::SHA256)
35
+
36
+ def test_new_ocsp_request
37
+ request = OpenSSL::OCSP::Request.new
38
+ cid = OpenSSL::OCSP::CertificateId.new(@cert, @ca_cert, OpenSSL::Digest::SHA1.new)
39
+ request.add_certid(cid)
40
+ request.sign(@cert, @key, [@cert])
41
+ assert_kind_of OpenSSL::OCSP::Request, request
42
+ # in current implementation not same instance of certificate id, but should contain same data
43
+ assert_equal cid.serial, request.certid.first.serial
44
+ end
45
+ end
46
+
47
+ end