@@ -231,7 +231,7 @@ def test_RSAPrivateKey_encrypted
231231 }
232232 end
233233
234- def test_RSAPublicKey
234+ def test_RSAPublicKey_custom
235235 rsa1024 = Fixtures . pkey ( "rsa1024" )
236236
237237 asn1 = OpenSSL ::ASN1 ::Sequence ( [ OpenSSL ::ASN1 ::Integer ( rsa1024 . n ) , OpenSSL ::ASN1 ::Integer ( rsa1024 . e ) ] )
@@ -265,6 +265,80 @@ def test_RSAPublicKey
265265 assert_equal expected , OpenSSL ::Digest ::SHA1 . hexdigest ( key . to_der )
266266 end if !defined? ( JRUBY_VERSION ) || JRUBY_VERSION > '9.1' # set_key only since Ruby 2.3
267267
268+ def test_RSAPublicKey
269+ rsa1024 = Fixtures . pkey ( "rsa1024" )
270+ rsa1024pub = OpenSSL ::PKey ::RSA . new ( rsa1024 . public_to_der )
271+
272+ asn1 = OpenSSL ::ASN1 ::Sequence ( [
273+ OpenSSL ::ASN1 ::Integer ( rsa1024 . n ) ,
274+ OpenSSL ::ASN1 ::Integer ( rsa1024 . e )
275+ ] )
276+ key = OpenSSL ::PKey ::RSA . new ( asn1 . to_der )
277+ assert_not_predicate key , :private?
278+ assert_same_rsa rsa1024pub , key
279+
280+ pem = <<~EOF
281+ -----BEGIN RSA PUBLIC KEY-----
282+ MIGJAoGBAMvCxLDUQKc+1P4+Q6AeFwYDvWfALb+cvzlUEadGoPE6qNWHsLFoo8RF
283+ geyTgE8KQTduu1OE9Zz2SMcRBDu5/1jWtsLPSVrI2ofLLBARUsWanVyki39DeB4u
284+ /xkP2mKGjAokPIwOI3oCthSZlzO9bj3voxTf6XngTqUX8l8URTmHAgMBAAE=
285+ -----END RSA PUBLIC KEY-----
286+ EOF
287+ key = OpenSSL ::PKey ::RSA . new ( pem )
288+ assert_same_rsa rsa1024pub , key
289+ end
290+
291+ def test_export
292+ rsa1024 = Fixtures . pkey ( "rsa1024" )
293+
294+ #pub = OpenSSL::PKey.read(rsa1024.public_to_der) # TODO not supported
295+ pub = OpenSSL ::PKey ::RSA . new ( rsa1024 . public_to_der )
296+ assert_not_equal rsa1024 . export , pub . export
297+ assert_equal rsa1024 . public_to_pem , pub . export
298+
299+ # PKey is immutable in OpenSSL >= 3.0
300+ #if !openssl?(3, 0, 0)
301+ key = OpenSSL ::PKey ::RSA . new
302+
303+ # key has only n, e and d
304+ key . set_key ( rsa1024 . n , rsa1024 . e , rsa1024 . d )
305+ assert_equal rsa1024 . public_key . export , key . export
306+
307+ # key has only n, e, d, p and q
308+ key . set_factors ( rsa1024 . p , rsa1024 . q )
309+ assert_equal rsa1024 . public_key . export , key . export
310+
311+ # key has n, e, d, p, q, dmp1, dmq1 and iqmp
312+ key . set_crt_params ( rsa1024 . dmp1 , rsa1024 . dmq1 , rsa1024 . iqmp )
313+ #assert_equal rsa1024.export, key.export # TODO does not pass
314+ #end
315+ end
316+
317+ def test_to_der
318+ rsa1024 = Fixtures . pkey ( "rsa1024" )
319+
320+ pub = OpenSSL ::PKey ::RSA . new ( rsa1024 . public_to_der )
321+ assert_not_equal rsa1024 . to_der , pub . to_der
322+ assert_equal rsa1024 . public_to_der , pub . to_der
323+
324+ # PKey is immutable in OpenSSL >= 3.0
325+ #if !openssl?(3, 0, 0)
326+ key = OpenSSL ::PKey ::RSA . new
327+
328+ # key has only n, e and d
329+ key . set_key ( rsa1024 . n , rsa1024 . e , rsa1024 . d )
330+ assert_equal rsa1024 . public_key . to_der , key . to_der
331+
332+ # key has only n, e, d, p and q
333+ key . set_factors ( rsa1024 . p , rsa1024 . q )
334+ assert_equal rsa1024 . public_key . to_der , key . to_der
335+
336+ # key has n, e, d, p, q, dmp1, dmq1 and iqmp
337+ key . set_crt_params ( rsa1024 . dmp1 , rsa1024 . dmq1 , rsa1024 . iqmp )
338+ #assert_equal rsa1024.to_der, key.to_der # TODO does not pass
339+ #end
340+ end
341+
268342 def test_to_java
269343 key_file = File . join ( File . dirname ( __FILE__ ) , 'private_key.pem' )
270344 pkey = OpenSSL ::PKey ::RSA . new ( File . read ( key_file ) )
0 commit comments