22require_relative "utils"
33
44class OpenSSL ::TestPKey < OpenSSL ::PKeyTestCase
5- def test_generic_oid_inspect
5+ def test_generic_oid_inspect_rsa
66 # RSA private key
77 rsa = Fixtures . pkey ( "rsa-1" )
88 assert_instance_of OpenSSL ::PKey ::RSA , rsa
99 assert_equal "rsaEncryption" , rsa . oid
1010 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
1116
1217 # 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" )
2419 assert_instance_of OpenSSL ::PKey ::PKey , x25519
2520 assert_equal "X25519" , x25519 . oid
2621 assert_match %r{oid=X25519} , x25519 . inspect
@@ -112,18 +107,14 @@ def test_ed25519
112107 assert_equal pub_pem , priv . public_to_pem
113108 assert_equal pub_pem , pub . public_to_pem
114109
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
127118
128119 sig = [ <<~EOF . gsub ( /[^0-9a-f]/ , "" ) ] . pack ( "H*" )
129120 92a009a9f0d4cab8720e820b5f642540
@@ -146,6 +137,9 @@ def test_ed25519
146137 end
147138
148139 def test_x25519
140+ omit "X25519 not supported" unless openssl? ( 1 , 1 , 0 ) || libressl? ( 3 , 7 , 0 )
141+ omit_on_fips
142+
149143 # Test vector from RFC 7748 Section 6.1
150144 alice_pem = <<~EOF
151145 -----BEGIN PRIVATE KEY-----
@@ -158,38 +152,31 @@ def test_x25519
158152 -----END PUBLIC KEY-----
159153 EOF
160154 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 )
168158 assert_instance_of OpenSSL ::PKey ::PKey , alice
169159 assert_equal alice_pem , alice . private_to_pem
170160 assert_equal bob_pem , bob . public_to_pem
171161 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"
180165 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 )
181168 assert_equal alice_private . private_to_pem ,
182169 alice . private_to_pem
183170 assert_equal bob_public . public_to_pem ,
184171 bob . public_to_pem
185172 assert_equal "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a" ,
186- alice_private_raw
173+ alice . raw_private_key . unpack1 ( "H*" )
187174 assert_equal "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f" ,
188- bob_public_raw
175+ bob . raw_public_key . unpack1 ( "H*" )
189176 end
190177
191- def raw_initialize
192- pend "Ed25519 is not implemented " unless openssl? ( 1 , 1 , 1 ) # >= v1.1.1
178+ def test_raw_initialize_errors
179+ omit "Ed25519 not supported " unless openssl? ( 1 , 1 , 1 ) || libressl? ( 3 , 7 , 0 )
193180
194181 assert_raise ( OpenSSL ::PKey ::PKeyError ) { OpenSSL ::PKey . new_raw_private_key ( "foo123" , "xxx" ) }
195182 assert_raise ( OpenSSL ::PKey ::PKeyError ) { OpenSSL ::PKey . new_raw_private_key ( "ED25519" , "xxx" ) }
0 commit comments