2
2
require_relative "utils"
3
3
4
4
class OpenSSL ::TestPKey < OpenSSL ::PKeyTestCase
5
- def test_generic_oid_inspect
5
+ def test_generic_oid_inspect_rsa
6
6
# RSA private key
7
7
rsa = Fixtures . pkey ( "rsa-1" )
8
8
assert_instance_of OpenSSL ::PKey ::RSA , rsa
9
9
assert_equal "rsaEncryption" , rsa . oid
10
10
assert_match %r{oid=rsaEncryption} , rsa . inspect
11
+ end
12
+
13
+ def test_generic_oid_inspect_x25519
14
+ omit "X25519 not supported" unless openssl? ( 1 , 1 , 0 ) || libressl? ( 3 , 7 , 0 )
15
+ omit_on_fips
11
16
12
17
# X25519 private key
13
- x25519_pem = <<~EOF
14
- -----BEGIN PRIVATE KEY-----
15
- MC4CAQAwBQYDK2VuBCIEIHcHbQpzGKV9PBbBclGyZkXfTC+H68CZKrF3+6UduSwq
16
- -----END PRIVATE KEY-----
17
- EOF
18
- begin
19
- x25519 = OpenSSL ::PKey . read ( x25519_pem )
20
- rescue OpenSSL ::PKey ::PKeyError
21
- # OpenSSL < 1.1.0
22
- pend "X25519 is not implemented"
23
- end
18
+ x25519 = OpenSSL ::PKey . generate_key ( "X25519" )
24
19
assert_instance_of OpenSSL ::PKey ::PKey , x25519
25
20
assert_equal "X25519" , x25519 . oid
26
21
assert_match %r{oid=X25519} , x25519 . inspect
@@ -112,18 +107,14 @@ def test_ed25519
112
107
assert_equal pub_pem , priv . public_to_pem
113
108
assert_equal pub_pem , pub . public_to_pem
114
109
115
- begin
116
- assert_equal "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb" ,
117
- priv . raw_private_key . unpack1 ( "H*" )
118
- assert_equal OpenSSL ::PKey . new_raw_private_key ( "ED25519" , priv . raw_private_key ) . private_to_pem ,
119
- priv . private_to_pem
120
- assert_equal "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" ,
121
- priv . raw_public_key . unpack1 ( "H*" )
122
- assert_equal OpenSSL ::PKey . new_raw_public_key ( "ED25519" , priv . raw_public_key ) . public_to_pem ,
123
- pub . public_to_pem
124
- rescue NoMethodError
125
- pend "running OpenSSL version does not have raw public key support"
126
- end
110
+ assert_equal "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb" ,
111
+ priv . raw_private_key . unpack1 ( "H*" )
112
+ assert_equal OpenSSL ::PKey . new_raw_private_key ( "ED25519" , priv . raw_private_key ) . private_to_pem ,
113
+ priv . private_to_pem
114
+ assert_equal "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" ,
115
+ priv . raw_public_key . unpack1 ( "H*" )
116
+ assert_equal OpenSSL ::PKey . new_raw_public_key ( "ED25519" , priv . raw_public_key ) . public_to_pem ,
117
+ pub . public_to_pem
127
118
128
119
sig = [ <<~EOF . gsub ( /[^0-9a-f]/ , "" ) ] . pack ( "H*" )
129
120
92a009a9f0d4cab8720e820b5f642540
@@ -146,6 +137,9 @@ def test_ed25519
146
137
end
147
138
148
139
def test_x25519
140
+ omit "X25519 not supported" unless openssl? ( 1 , 1 , 0 ) || libressl? ( 3 , 7 , 0 )
141
+ omit_on_fips
142
+
149
143
# Test vector from RFC 7748 Section 6.1
150
144
alice_pem = <<~EOF
151
145
-----BEGIN PRIVATE KEY-----
@@ -158,38 +152,31 @@ def test_x25519
158
152
-----END PUBLIC KEY-----
159
153
EOF
160
154
shared_secret = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
161
- begin
162
- alice = OpenSSL ::PKey . read ( alice_pem )
163
- bob = OpenSSL ::PKey . read ( bob_pem )
164
- rescue OpenSSL ::PKey ::PKeyError
165
- # OpenSSL < 1.1.0
166
- pend "X25519 is not implemented"
167
- end
155
+
156
+ alice = OpenSSL ::PKey . read ( alice_pem )
157
+ bob = OpenSSL ::PKey . read ( bob_pem )
168
158
assert_instance_of OpenSSL ::PKey ::PKey , alice
169
159
assert_equal alice_pem , alice . private_to_pem
170
160
assert_equal bob_pem , bob . public_to_pem
171
161
assert_equal [ shared_secret ] . pack ( "H*" ) , alice . derive ( bob )
172
- begin
173
- alice_private = OpenSSL ::PKey . new_raw_private_key ( "X25519" , alice . raw_private_key )
174
- bob_public = OpenSSL ::PKey . new_raw_public_key ( "X25519" , bob . raw_public_key )
175
- alice_private_raw = alice . raw_private_key . unpack1 ( "H*" )
176
- bob_public_raw = bob . raw_public_key . unpack1 ( "H*" )
177
- rescue NoMethodError
178
- # OpenSSL < 1.1.1
179
- pend "running OpenSSL version does not have raw public key support"
162
+
163
+ unless openssl? ( 1 , 1 , 1 ) || libressl? ( 3 , 7 , 0 )
164
+ omit "running OpenSSL version does not have raw public key support"
180
165
end
166
+ alice_private = OpenSSL ::PKey . new_raw_private_key ( "X25519" , alice . raw_private_key )
167
+ bob_public = OpenSSL ::PKey . new_raw_public_key ( "X25519" , bob . raw_public_key )
181
168
assert_equal alice_private . private_to_pem ,
182
169
alice . private_to_pem
183
170
assert_equal bob_public . public_to_pem ,
184
171
bob . public_to_pem
185
172
assert_equal "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a" ,
186
- alice_private_raw
173
+ alice . raw_private_key . unpack1 ( "H*" )
187
174
assert_equal "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f" ,
188
- bob_public_raw
175
+ bob . raw_public_key . unpack1 ( "H*" )
189
176
end
190
177
191
178
def test_raw_initialize_errors
192
- pend "Ed25519 is not implemented " unless openssl? ( 1 , 1 , 1 ) # >= v1.1.1
179
+ omit "Ed25519 not supported " unless openssl? ( 1 , 1 , 1 ) || libressl? ( 3 , 7 , 0 )
193
180
194
181
assert_raise ( OpenSSL ::PKey ::PKeyError ) { OpenSSL ::PKey . new_raw_private_key ( "foo123" , "xxx" ) }
195
182
assert_raise ( OpenSSL ::PKey ::PKeyError ) { OpenSSL ::PKey . new_raw_private_key ( "ED25519" , "xxx" ) }
0 commit comments