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,209 @@
1
+ require_relative "utils"
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ module OpenSSL
6
+ class TestPKCS12 < Test::Unit::TestCase
7
+ include OpenSSL::TestUtils
8
+
9
+ def setup
10
+ ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
11
+
12
+ now = Time.now
13
+ ca_exts = [
14
+ ["basicConstraints","CA:TRUE",true],
15
+ ["keyUsage","keyCertSign, cRLSign",true],
16
+ ["subjectKeyIdentifier","hash",false],
17
+ ["authorityKeyIdentifier","keyid:always",false],
18
+ ]
19
+
20
+ @cacert = issue_cert(ca, TEST_KEY_RSA2048, 1, now, now+3600, ca_exts,
21
+ nil, nil, OpenSSL::Digest::SHA1.new)
22
+
23
+ inter_ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=Intermediate CA")
24
+ inter_ca_key = OpenSSL::PKey.read <<-_EOS_
25
+ -----BEGIN RSA PRIVATE KEY-----
26
+ MIICXAIBAAKBgQDp7hIG0SFMG/VWv1dBUWziAPrNmkMXJgTCAoB7jffzRtyyN04K
27
+ oq/89HAszTMStZoMigQURfokzKsjpUp8OYCAEsBtt9d5zPndWMz/gHN73GrXk3LT
28
+ ZsxEn7Xv5Da+Y9F/Hx2QZUHarV5cdZixq2NbzWGwrToogOQMh2pxN3Z/0wIDAQAB
29
+ AoGBAJysUyx3olpsGzv3OMRJeahASbmsSKTXVLZvoIefxOINosBFpCIhZccAG6UV
30
+ 5c/xCvS89xBw8aD15uUfziw3AuT8QPEtHCgfSjeT7aWzBfYswEgOW4XPuWr7EeI9
31
+ iNHGD6z+hCN/IQr7FiEBgTp6A+i/hffcSdR83fHWKyb4M7TRAkEA+y4BNd668HmC
32
+ G5MPRx25n6LixuBxrNp1umfjEI6UZgEFVpYOg4agNuimN6NqM253kcTR94QNTUs5
33
+ Kj3EhG1YWwJBAO5rUjiOyCNVX2WUQrOMYK/c1lU7fvrkdygXkvIGkhsPoNRzLPeA
34
+ HGJszKtrKD8bNihWpWNIyqKRHfKVD7yXT+kCQGCAhVCIGTRoypcDghwljHqLnysf
35
+ ci0h5ZdPcIqc7ODfxYhFsJ/Rql5ONgYsT5Ig/+lOQAkjf+TRYM4c2xKx2/8CQBvG
36
+ jv6dy70qDgIUgqzONtlmHeYyFzn9cdBO5sShdVYHvRHjFSMEXsosqK9zvW2UqvuK
37
+ FJx7d3f29gkzynCLJDkCQGQZlEZJC4vWmWJGRKJ24P6MyQn3VsPfErSKOg4lvyM3
38
+ Li8JsX5yIiuVYaBg/6ha3tOg4TCa5K/3r3tVliRZ2Es=
39
+ -----END RSA PRIVATE KEY-----
40
+ _EOS_
41
+
42
+ @inter_cacert = issue_cert(inter_ca, inter_ca_key, 2, now, now+3600, ca_exts,
43
+ @ca_cert, TEST_KEY_RSA2048, OpenSSL::Digest::SHA1.new)
44
+
45
+ exts = [
46
+ ["keyUsage","digitalSignature",true],
47
+ ["subjectKeyIdentifier","hash",false],
48
+ ]
49
+ ee = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=Ruby PKCS12 Test Certificate")
50
+ @mycert = issue_cert(ee, TEST_KEY_RSA1024, 3, now, now+3600, exts,
51
+ @inter_cacert, inter_ca_key, OpenSSL::Digest::SHA1.new)
52
+ end
53
+
54
+ def test_create
55
+ pkcs12 = OpenSSL::PKCS12.create(
56
+ "omg",
57
+ "hello",
58
+ TEST_KEY_RSA1024,
59
+ @mycert
60
+ )
61
+ assert_equal @mycert, pkcs12.certificate
62
+ assert_equal TEST_KEY_RSA1024, pkcs12.key
63
+ assert_nil pkcs12.ca_certs
64
+ end
65
+
66
+ def test_create_no_pass
67
+ pkcs12 = OpenSSL::PKCS12.create(
68
+ nil,
69
+ "hello",
70
+ TEST_KEY_RSA1024,
71
+ @mycert
72
+ )
73
+ assert_equal @mycert, pkcs12.certificate
74
+ assert_equal TEST_KEY_RSA1024, pkcs12.key
75
+ assert_nil pkcs12.ca_certs
76
+
77
+ decoded = OpenSSL::PKCS12.new(pkcs12.to_der)
78
+ assert_cert @mycert, decoded.certificate
79
+ end
80
+
81
+ def test_create_with_chain
82
+ chain = [@inter_cacert, @cacert]
83
+
84
+ pkcs12 = OpenSSL::PKCS12.create(
85
+ "omg",
86
+ "hello",
87
+ TEST_KEY_RSA1024,
88
+ @mycert,
89
+ chain
90
+ )
91
+ assert_equal chain, pkcs12.ca_certs
92
+ end
93
+
94
+ def test_create_with_chain_decode
95
+ chain = [@cacert, @inter_cacert]
96
+
97
+ passwd = "omg"
98
+
99
+ pkcs12 = OpenSSL::PKCS12.create(
100
+ passwd,
101
+ "hello",
102
+ TEST_KEY_RSA1024,
103
+ @mycert,
104
+ chain
105
+ )
106
+
107
+ decoded = OpenSSL::PKCS12.new(pkcs12.to_der, passwd)
108
+ assert_equal chain.size, decoded.ca_certs.size
109
+ assert_include_cert @cacert, decoded.ca_certs
110
+ assert_include_cert @inter_cacert, decoded.ca_certs
111
+ assert_cert @mycert, decoded.certificate
112
+ assert_equal TEST_KEY_RSA1024.to_der, decoded.key.to_der
113
+ end
114
+
115
+ def test_create_with_bad_nid
116
+ assert_raises(ArgumentError) do
117
+ OpenSSL::PKCS12.create(
118
+ "omg",
119
+ "hello",
120
+ TEST_KEY_RSA1024,
121
+ @mycert,
122
+ [],
123
+ "foo"
124
+ )
125
+ end
126
+ end
127
+
128
+ def test_create_with_itr
129
+ OpenSSL::PKCS12.create(
130
+ "omg",
131
+ "hello",
132
+ TEST_KEY_RSA1024,
133
+ @mycert,
134
+ [],
135
+ nil,
136
+ nil,
137
+ 2048
138
+ )
139
+
140
+ assert_raises(TypeError) do
141
+ OpenSSL::PKCS12.create(
142
+ "omg",
143
+ "hello",
144
+ TEST_KEY_RSA1024,
145
+ @mycert,
146
+ [],
147
+ nil,
148
+ nil,
149
+ "omg"
150
+ )
151
+ end
152
+ end
153
+
154
+ def test_create_with_mac_itr
155
+ OpenSSL::PKCS12.create(
156
+ "omg",
157
+ "hello",
158
+ TEST_KEY_RSA1024,
159
+ @mycert,
160
+ [],
161
+ nil,
162
+ nil,
163
+ nil,
164
+ 2048
165
+ )
166
+
167
+ assert_raises(TypeError) do
168
+ OpenSSL::PKCS12.create(
169
+ "omg",
170
+ "hello",
171
+ TEST_KEY_RSA1024,
172
+ @mycert,
173
+ [],
174
+ nil,
175
+ nil,
176
+ nil,
177
+ "omg"
178
+ )
179
+ end
180
+ end
181
+
182
+ private
183
+ def assert_cert expected, actual
184
+ [
185
+ :subject,
186
+ :issuer,
187
+ :serial,
188
+ :not_before,
189
+ :not_after,
190
+ ].each do |attribute|
191
+ assert_equal expected.send(attribute), actual.send(attribute)
192
+ end
193
+ assert_equal expected.to_der, actual.to_der
194
+ end
195
+
196
+ def assert_include_cert cert, ary
197
+ der = cert.to_der
198
+ ary.each do |candidate|
199
+ if candidate.to_der == der
200
+ return true
201
+ end
202
+ end
203
+ false
204
+ end
205
+
206
+ end
207
+ end
208
+
209
+ end
@@ -0,0 +1,151 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestPKCS7 < Test::Unit::TestCase
6
+ def setup
7
+ @rsa1024 = OpenSSL::TestUtils::TEST_KEY_RSA1024
8
+ @rsa2048 = OpenSSL::TestUtils::TEST_KEY_RSA2048
9
+ ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
10
+ ee1 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE1")
11
+ ee2 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE2")
12
+
13
+ now = Time.now
14
+ ca_exts = [
15
+ ["basicConstraints","CA:TRUE",true],
16
+ ["keyUsage","keyCertSign, cRLSign",true],
17
+ ["subjectKeyIdentifier","hash",false],
18
+ ["authorityKeyIdentifier","keyid:always",false],
19
+ ]
20
+ @ca_cert = issue_cert(ca, @rsa2048, 1, now, now+3600, ca_exts,
21
+ nil, nil, OpenSSL::Digest::SHA1.new)
22
+ ee_exts = [
23
+ ["keyUsage","Non Repudiation, Digital Signature, Key Encipherment",true],
24
+ ["authorityKeyIdentifier","keyid:always",false],
25
+ ["extendedKeyUsage","clientAuth, emailProtection, codeSigning",false],
26
+ ]
27
+ @ee1_cert = issue_cert(ee1, @rsa1024, 2, now, now+1800, ee_exts,
28
+ @ca_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
29
+ @ee2_cert = issue_cert(ee2, @rsa1024, 3, now, now+1800, ee_exts,
30
+ @ca_cert, @rsa2048, OpenSSL::Digest::SHA1.new)
31
+ end
32
+
33
+ def issue_cert(*args)
34
+ OpenSSL::TestUtils.issue_cert(*args)
35
+ end
36
+
37
+ def test_signed
38
+ store = OpenSSL::X509::Store.new
39
+ store.add_cert(@ca_cert)
40
+ ca_certs = [@ca_cert]
41
+
42
+ data = "aaaaa\r\nbbbbb\r\nccccc\r\n"
43
+ tmp = OpenSSL::PKCS7.sign(@ee1_cert, @rsa1024, data, ca_certs)
44
+ p7 = OpenSSL::PKCS7.new(tmp.to_der)
45
+ certs = p7.certificates
46
+ signers = p7.signers
47
+ assert(p7.verify([], store))
48
+ assert_equal(data, p7.data)
49
+ assert_equal(2, certs.size)
50
+ assert_equal(@ee1_cert.subject.to_s, certs[0].subject.to_s)
51
+ assert_equal(@ca_cert.subject.to_s, certs[1].subject.to_s)
52
+ assert_equal(1, signers.size)
53
+ assert_equal(@ee1_cert.serial, signers[0].serial)
54
+ assert_equal(@ee1_cert.issuer.to_s, signers[0].issuer.to_s)
55
+
56
+ # Normaly OpenSSL tries to translate the supplied content into canonical
57
+ # MIME format (e.g. a newline character is converted into CR+LF).
58
+ # If the content is a binary, PKCS7::BINARY flag should be used.
59
+
60
+ data = "aaaaa\nbbbbb\nccccc\n"
61
+ flag = OpenSSL::PKCS7::BINARY
62
+ tmp = OpenSSL::PKCS7.sign(@ee1_cert, @rsa1024, data, ca_certs, flag)
63
+ p7 = OpenSSL::PKCS7.new(tmp.to_der)
64
+ certs = p7.certificates
65
+ signers = p7.signers
66
+ assert(p7.verify([], store))
67
+ assert_equal(data, p7.data)
68
+ assert_equal(2, certs.size)
69
+ assert_equal(@ee1_cert.subject.to_s, certs[0].subject.to_s)
70
+ assert_equal(@ca_cert.subject.to_s, certs[1].subject.to_s)
71
+ assert_equal(1, signers.size)
72
+ assert_equal(@ee1_cert.serial, signers[0].serial)
73
+ assert_equal(@ee1_cert.issuer.to_s, signers[0].issuer.to_s)
74
+
75
+ # A signed-data which have multiple signatures can be created
76
+ # through the following steps.
77
+ # 1. create two signed-data
78
+ # 2. copy signerInfo and certificate from one to another
79
+
80
+ tmp1 = OpenSSL::PKCS7.sign(@ee1_cert, @rsa1024, data, [], flag)
81
+ tmp2 = OpenSSL::PKCS7.sign(@ee2_cert, @rsa1024, data, [], flag)
82
+ tmp1.add_signer(tmp2.signers[0])
83
+ tmp1.add_certificate(@ee2_cert)
84
+
85
+ p7 = OpenSSL::PKCS7.new(tmp1.to_der)
86
+ certs = p7.certificates
87
+ signers = p7.signers
88
+ assert(p7.verify([], store))
89
+ assert_equal(data, p7.data)
90
+ assert_equal(2, certs.size)
91
+ assert_equal(2, signers.size)
92
+ assert_equal(@ee1_cert.serial, signers[0].serial)
93
+ assert_equal(@ee1_cert.issuer.to_s, signers[0].issuer.to_s)
94
+ assert_equal(@ee2_cert.serial, signers[1].serial)
95
+ assert_equal(@ee2_cert.issuer.to_s, signers[1].issuer.to_s)
96
+ end
97
+
98
+ def test_detached_sign
99
+ store = OpenSSL::X509::Store.new
100
+ store.add_cert(@ca_cert)
101
+ ca_certs = [@ca_cert]
102
+
103
+ data = "aaaaa\nbbbbb\nccccc\n"
104
+ flag = OpenSSL::PKCS7::BINARY|OpenSSL::PKCS7::DETACHED
105
+ tmp = OpenSSL::PKCS7.sign(@ee1_cert, @rsa1024, data, ca_certs, flag)
106
+ p7 = OpenSSL::PKCS7.new(tmp.to_der)
107
+ assert_nothing_raised do
108
+ OpenSSL::ASN1.decode(p7)
109
+ end
110
+
111
+ certs = p7.certificates
112
+ signers = p7.signers
113
+ assert(!p7.verify([], store))
114
+ assert(p7.verify([], store, data))
115
+ assert_equal(data, p7.data)
116
+ assert_equal(2, certs.size)
117
+ assert_equal(@ee1_cert.subject.to_s, certs[0].subject.to_s)
118
+ assert_equal(@ca_cert.subject.to_s, certs[1].subject.to_s)
119
+ assert_equal(1, signers.size)
120
+ assert_equal(@ee1_cert.serial, signers[0].serial)
121
+ assert_equal(@ee1_cert.issuer.to_s, signers[0].issuer.to_s)
122
+ end
123
+
124
+ def test_enveloped
125
+ if OpenSSL::OPENSSL_VERSION_NUMBER <= 0x0090704f
126
+ # PKCS7_encrypt() of OpenSSL-0.9.7d goes to SEGV.
127
+ # http://www.mail-archive.com/openssl-dev@openssl.org/msg17376.html
128
+ return
129
+ end
130
+
131
+ certs = [@ee1_cert, @ee2_cert]
132
+ cipher = OpenSSL::Cipher::AES.new("128-CBC")
133
+ data = "aaaaa\nbbbbb\nccccc\n"
134
+
135
+ tmp = OpenSSL::PKCS7.encrypt(certs, data, cipher, OpenSSL::PKCS7::BINARY)
136
+ p7 = OpenSSL::PKCS7.new(tmp.to_der)
137
+ recip = p7.recipients
138
+ assert_equal(:enveloped, p7.type)
139
+ assert_equal(2, recip.size)
140
+
141
+ assert_equal(@ca_cert.subject.to_s, recip[0].issuer.to_s)
142
+ assert_equal(2, recip[0].serial)
143
+ assert_equal(data, p7.decrypt(@rsa1024, @ee1_cert))
144
+
145
+ assert_equal(@ca_cert.subject.to_s, recip[1].issuer.to_s)
146
+ assert_equal(3, recip[1].serial)
147
+ assert_equal(data, p7.decrypt(@rsa1024, @ee2_cert))
148
+ end
149
+ end
150
+
151
+ end
@@ -0,0 +1,72 @@
1
+ require_relative 'utils'
2
+
3
+ if defined?(OpenSSL)
4
+
5
+ class OpenSSL::TestPKeyDH < Test::Unit::TestCase
6
+ def test_new
7
+ dh = OpenSSL::PKey::DH.new(256)
8
+ assert_key(dh)
9
+ end
10
+
11
+ def test_to_der
12
+ dh = OpenSSL::PKey::DH.new(256)
13
+ der = dh.to_der
14
+ dh2 = OpenSSL::PKey::DH.new(der)
15
+ assert_equal_params(dh, dh2)
16
+ assert_no_key(dh2)
17
+ end
18
+
19
+ def test_to_pem
20
+ dh = OpenSSL::PKey::DH.new(256)
21
+ pem = dh.to_pem
22
+ dh2 = OpenSSL::PKey::DH.new(pem)
23
+ assert_equal_params(dh, dh2)
24
+ assert_no_key(dh2)
25
+ end
26
+
27
+ def test_public_key
28
+ dh = OpenSSL::PKey::DH.new(256)
29
+ public_key = dh.public_key
30
+ assert_no_key(public_key) #implies public_key.public? is false!
31
+ assert_equal(dh.to_der, public_key.to_der)
32
+ assert_equal(dh.to_pem, public_key.to_pem)
33
+ end
34
+
35
+ def test_generate_key
36
+ dh = OpenSSL::TestUtils::TEST_KEY_DH512.public_key # creates a copy
37
+ assert_no_key(dh)
38
+ dh.generate_key!
39
+ assert_key(dh)
40
+ end
41
+
42
+ def test_key_exchange
43
+ dh = OpenSSL::TestUtils::TEST_KEY_DH512
44
+ dh2 = dh.public_key
45
+ dh.generate_key!
46
+ dh2.generate_key!
47
+ assert_equal(dh.compute_key(dh2.pub_key), dh2.compute_key(dh.pub_key))
48
+ end
49
+
50
+ private
51
+
52
+ def assert_equal_params(dh1, dh2)
53
+ assert_equal(dh1.g, dh2.g)
54
+ assert_equal(dh1.p, dh2.p)
55
+ end
56
+
57
+ def assert_no_key(dh)
58
+ assert_equal(false, dh.public?)
59
+ assert_equal(false, dh.private?)
60
+ assert_equal(nil, dh.pub_key)
61
+ assert_equal(nil, dh.priv_key)
62
+ end
63
+
64
+ def assert_key(dh)
65
+ assert(dh.public?)
66
+ assert(dh.private?)
67
+ assert(dh.pub_key)
68
+ assert(dh.priv_key)
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,224 @@
1
+ require_relative 'utils'
2
+ require 'base64'
3
+
4
+ if defined?(OpenSSL)
5
+
6
+ class OpenSSL::TestPKeyDSA < Test::Unit::TestCase
7
+ def test_private
8
+ key = OpenSSL::PKey::DSA.new(256)
9
+ assert(key.private?)
10
+ key2 = OpenSSL::PKey::DSA.new(key.to_der)
11
+ assert(key2.private?)
12
+ key3 = key.public_key
13
+ assert(!key3.private?)
14
+ key4 = OpenSSL::PKey::DSA.new(key3.to_der)
15
+ assert(!key4.private?)
16
+ end
17
+
18
+ def test_new
19
+ key = OpenSSL::PKey::DSA.new 256
20
+ pem = key.public_key.to_pem
21
+ OpenSSL::PKey::DSA.new pem
22
+ assert_equal([], OpenSSL.errors)
23
+ end
24
+
25
+ def test_sys_sign_verify
26
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
27
+ data = 'Sign me!'
28
+ digest = OpenSSL::Digest::SHA1.digest(data)
29
+ sig = key.syssign(digest)
30
+ assert(key.sysverify(digest, sig))
31
+ end
32
+
33
+ def test_sign_verify
34
+ check_sign_verify(OpenSSL::Digest::DSS1.new)
35
+ end
36
+
37
+ if (OpenSSL::OPENSSL_VERSION_NUMBER > 0x10000000)
38
+ def test_sign_verify_sha1
39
+ check_sign_verify(OpenSSL::Digest::SHA1.new)
40
+ end
41
+
42
+ def test_sign_verify_sha256
43
+ check_sign_verify(OpenSSL::Digest::SHA256.new)
44
+ end
45
+ end
46
+
47
+ def test_digest_state_irrelevant_verify
48
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
49
+ digest1 = OpenSSL::Digest::DSS1.new
50
+ digest2 = OpenSSL::Digest::DSS1.new
51
+ data = 'Sign me!'
52
+ sig = key.sign(digest1, data)
53
+ digest1.reset
54
+ digest1 << 'Change state of digest1'
55
+ assert(key.verify(digest1, sig, data))
56
+ assert(key.verify(digest2, sig, data))
57
+ end
58
+
59
+ def test_read_DSA_PUBKEY
60
+ p = 7188211954100152441468596248707152960171255279130004340103875772401008316444412091945435731597638374542374929457672178957081124632837356913990200866056699
61
+ q = 957032439192465935099784319494405376402293318491
62
+ g = 122928973717064636255205666162891733518376475981809749897454444301389338825906076467196186192907631719698166056821519884939865041993585844526937010746285
63
+ y = 1235756183583465414789073313502727057075641172514181938731172021825149551960029708596057102104063395063907739571546165975727369183495540798749742124846271
64
+ algo = OpenSSL::ASN1::ObjectId.new('DSA')
65
+ params = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(p),
66
+ OpenSSL::ASN1::Integer.new(q),
67
+ OpenSSL::ASN1::Integer.new(g)])
68
+ algo_id = OpenSSL::ASN1::Sequence.new ([algo, params])
69
+ pub_key = OpenSSL::ASN1::Integer.new(y)
70
+ seq = OpenSSL::ASN1::Sequence.new([algo_id, OpenSSL::ASN1::BitString.new(pub_key.to_der)])
71
+ key = OpenSSL::PKey::DSA.new(seq.to_der)
72
+ assert(key.public?)
73
+ assert(!key.private?)
74
+ assert_equal(p, key.p)
75
+ assert_equal(q, key.q)
76
+ assert_equal(g, key.g)
77
+ assert_equal(y, key.pub_key)
78
+ assert_equal(nil, key.priv_key)
79
+ assert_equal([], OpenSSL.errors)
80
+ end
81
+
82
+ def test_read_DSAPublicKey_pem
83
+ p = 12260055936871293565827712385212529106400444521449663325576634579961635627321079536132296996623400607469624537382977152381984332395192110731059176842635699
84
+ q = 979494906553787301107832405790107343409973851677
85
+ g = 3731695366899846297271147240305742456317979984190506040697507048095553842519347835107669437969086119948785140453492839427038591924536131566350847469993845
86
+ y = 10505239074982761504240823422422813362721498896040719759460296306305851824586095328615844661273887569281276387605297130014564808567159023649684010036304695
87
+ pem = <<-EOF
88
+ -----BEGIN DSA PUBLIC KEY-----
89
+ MIHfAkEAyJSJ+g+P/knVcgDwwTzC7Pwg/pWs2EMd/r+lYlXhNfzg0biuXRul8VR4
90
+ VUC/phySExY0PdcqItkR/xYAYNMbNwJBAOoV57X0FxKO/PrNa/MkoWzkCKV/hzhE
91
+ p0zbFdsicw+hIjJ7S6Sd/FlDlo89HQZ2FuvWJ6wGLM1j00r39+F2qbMCFQCrkhIX
92
+ SG+is37hz1IaBeEudjB2HQJAR0AloavBvtsng8obsjLb7EKnB+pSeHr/BdIQ3VH7
93
+ fWLOqqkzFeRrYMDzUpl36XktY6Yq8EJYlW9pCMmBVNy/dQ==
94
+ -----END DSA PUBLIC KEY-----
95
+ EOF
96
+ key = OpenSSL::PKey::DSA.new(pem)
97
+ assert(key.public?)
98
+ assert(!key.private?)
99
+ assert_equal(p, key.p)
100
+ assert_equal(q, key.q)
101
+ assert_equal(g, key.g)
102
+ assert_equal(y, key.pub_key)
103
+ assert_equal(nil, key.priv_key)
104
+ assert_equal([], OpenSSL.errors)
105
+ end
106
+
107
+ def test_read_DSA_PUBKEY_pem
108
+ p = 12260055936871293565827712385212529106400444521449663325576634579961635627321079536132296996623400607469624537382977152381984332395192110731059176842635699
109
+ q = 979494906553787301107832405790107343409973851677
110
+ g = 3731695366899846297271147240305742456317979984190506040697507048095553842519347835107669437969086119948785140453492839427038591924536131566350847469993845
111
+ y = 10505239074982761504240823422422813362721498896040719759460296306305851824586095328615844661273887569281276387605297130014564808567159023649684010036304695
112
+ pem = <<-EOF
113
+ -----BEGIN PUBLIC KEY-----
114
+ MIHxMIGoBgcqhkjOOAQBMIGcAkEA6hXntfQXEo78+s1r8yShbOQIpX+HOESnTNsV
115
+ 2yJzD6EiMntLpJ38WUOWjz0dBnYW69YnrAYszWPTSvf34XapswIVAKuSEhdIb6Kz
116
+ fuHPUhoF4S52MHYdAkBHQCWhq8G+2yeDyhuyMtvsQqcH6lJ4ev8F0hDdUft9Ys6q
117
+ qTMV5GtgwPNSmXfpeS1jpirwQliVb2kIyYFU3L91A0QAAkEAyJSJ+g+P/knVcgDw
118
+ wTzC7Pwg/pWs2EMd/r+lYlXhNfzg0biuXRul8VR4VUC/phySExY0PdcqItkR/xYA
119
+ YNMbNw==
120
+ -----END PUBLIC KEY-----
121
+ EOF
122
+ key = OpenSSL::PKey::DSA.new(pem)
123
+ assert(key.public?)
124
+ assert(!key.private?)
125
+ assert_equal(p, key.p)
126
+ assert_equal(q, key.q)
127
+ assert_equal(g, key.g)
128
+ assert_equal(y, key.pub_key)
129
+ assert_equal(nil, key.priv_key)
130
+ assert_equal([], OpenSSL.errors)
131
+ end
132
+
133
+ def test_export_format_is_DSA_PUBKEY_pem
134
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
135
+ pem = key.public_key.to_pem
136
+ pem.gsub!(/^-+(\w|\s)+-+$/, "") # eliminate --------BEGIN...-------
137
+ asn1 = OpenSSL::ASN1.decode(Base64.decode64(pem))
138
+ assert_equal(OpenSSL::ASN1::SEQUENCE, asn1.tag)
139
+ assert_equal(2, asn1.value.size)
140
+ seq = asn1.value
141
+ assert_equal(OpenSSL::ASN1::SEQUENCE, seq[0].tag)
142
+ assert_equal(2, seq[0].value.size)
143
+ algo_id = seq[0].value
144
+ assert_equal(OpenSSL::ASN1::OBJECT, algo_id[0].tag)
145
+ assert_equal('DSA', algo_id[0].value)
146
+ assert_equal(OpenSSL::ASN1::SEQUENCE, algo_id[1].tag)
147
+ assert_equal(3, algo_id[1].value.size)
148
+ params = algo_id[1].value
149
+ assert_equal(OpenSSL::ASN1::INTEGER, params[0].tag)
150
+ assert_equal(key.p, params[0].value)
151
+ assert_equal(OpenSSL::ASN1::INTEGER, params[1].tag)
152
+ assert_equal(key.q, params[1].value)
153
+ assert_equal(OpenSSL::ASN1::INTEGER, params[2].tag)
154
+ assert_equal(key.g, params[2].value)
155
+ assert_equal(OpenSSL::ASN1::BIT_STRING, seq[1].tag)
156
+ assert_equal(0, seq[1].unused_bits)
157
+ pub_key = OpenSSL::ASN1.decode(seq[1].value)
158
+ assert_equal(OpenSSL::ASN1::INTEGER, pub_key.tag)
159
+ assert_equal(key.pub_key, pub_key.value)
160
+ assert_equal([], OpenSSL.errors)
161
+ end
162
+
163
+ def test_read_private_key_der
164
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
165
+ der = key.to_der
166
+ key2 = OpenSSL::PKey.read(der)
167
+ assert(key2.private?)
168
+ assert_equal(der, key2.to_der)
169
+ assert_equal([], OpenSSL.errors)
170
+ end
171
+
172
+ def test_read_private_key_pem
173
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
174
+ pem = key.to_pem
175
+ key2 = OpenSSL::PKey.read(pem)
176
+ assert(key2.private?)
177
+ assert_equal(pem, key2.to_pem)
178
+ assert_equal([], OpenSSL.errors)
179
+ end
180
+
181
+ def test_read_public_key_der
182
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256.public_key
183
+ der = key.to_der
184
+ key2 = OpenSSL::PKey.read(der)
185
+ assert(!key2.private?)
186
+ assert_equal(der, key2.to_der)
187
+ assert_equal([], OpenSSL.errors)
188
+ end
189
+
190
+ def test_read_public_key_pem
191
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256.public_key
192
+ pem = key.to_pem
193
+ key2 = OpenSSL::PKey.read(pem)
194
+ assert(!key2.private?)
195
+ assert_equal(pem, key2.to_pem)
196
+ assert_equal([], OpenSSL.errors)
197
+ end
198
+
199
+ def test_read_private_key_pem_pw
200
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
201
+ pem = key.to_pem(OpenSSL::Cipher.new('AES-128-CBC'), 'secret')
202
+ #callback form for password
203
+ key2 = OpenSSL::PKey.read(pem) do
204
+ 'secret'
205
+ end
206
+ assert(key2.private?)
207
+ # pass password directly
208
+ key2 = OpenSSL::PKey.read(pem, 'secret')
209
+ assert(key2.private?)
210
+ #omit pem equality check, will be different due to cipher iv
211
+ assert_equal([], OpenSSL.errors)
212
+ end
213
+
214
+ private
215
+
216
+ def check_sign_verify(digest)
217
+ key = OpenSSL::TestUtils::TEST_KEY_DSA256
218
+ data = 'Sign me!'
219
+ sig = key.sign(digest, data)
220
+ assert(key.verify(digest, sig, data))
221
+ end
222
+ end
223
+
224
+ end