1 # frozen_string_literal: true
2 require_relative "utils"
6 class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
7 def test_no_private_exp
8 key = OpenSSL::PKey::RSA.new
9 rsa = Fixtures.pkey("rsa2048")
10 key.set_key(rsa.n, rsa.e, nil)
11 key.set_factors(rsa.p, rsa.q)
12 assert_raise(OpenSSL::PKey::RSAError){ key.private_encrypt("foo") }
13 assert_raise(OpenSSL::PKey::RSAError){ key.private_decrypt("foo") }
14 end if !openssl?(3, 0, 0) # Impossible state in OpenSSL 3.0
17 # Generated by key size and public exponent
18 key = OpenSSL::PKey::RSA.new(512, 3)
22 key2 = OpenSSL::PKey::RSA.new(key.to_der)
27 assert(!key3.private?)
29 # Generated by public key DER
30 key4 = OpenSSL::PKey::RSA.new(key3.to_der)
31 assert(!key4.private?)
32 rsa1024 = Fixtures.pkey("rsa1024")
35 key = OpenSSL::PKey::RSA.new
36 # Generated by RSA#set_key
37 key5 = OpenSSL::PKey::RSA.new
38 key5.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
41 # Generated by RSA#set_key, without d
42 key6 = OpenSSL::PKey::RSA.new
43 key6.set_key(rsa1024.n, rsa1024.e, nil)
44 assert(!key6.private?)
49 key = OpenSSL::PKey::RSA.new(512)
50 assert_equal 512, key.n.num_bits
51 assert_equal 65537, key.e
54 # Specify public exponent
55 key2 = OpenSSL::PKey::RSA.new(512, 3)
56 assert_equal 512, key2.n.num_bits
57 assert_equal 3, key2.e
62 key1 = OpenSSL::PKey::RSA.generate(512)
63 assert_equal 512, key1.n.num_bits
64 assert_equal 65537, key1.e
66 # Specify public exponent
67 key2 = OpenSSL::PKey::RSA.generate(512, 3)
68 assert_equal 512, key2.n.num_bits
69 assert_equal 3, key2.e
74 assert_nil(OpenSSL::PKey::RSA.new(1024) { break })
75 assert_raise(RuntimeError) do
76 OpenSSL::PKey::RSA.new(1024) { raise }
81 rsa1024 = Fixtures.pkey("rsa1024")
83 signature = rsa1024.sign("SHA1", data)
84 assert_equal true, rsa1024.verify("SHA1", signature, data)
86 signature0 = (<<~'end;').unpack("m")[0]
87 oLCgbprPvfhM4pjFQiDTFeWI9Sk+Og7Nh9TmIZ/xSxf2CGXQrptlwo7NQ28+
88 WA6YQo8jPH4hSuyWIM4Gz4qRYiYRkl5TDMUYob94zm8Si1HxEiS9354tzvqS
89 zS8MLW2BtNPuTubMxTItHGTnOzo9sUg0LAHVFt8kHG2NfKAw/gQ=
91 assert_equal true, rsa1024.verify("SHA256", signature0, data)
92 signature1 = signature0.succ
93 assert_equal false, rsa1024.verify("SHA256", signature1, data)
96 def test_sign_verify_options
97 key = Fixtures.pkey("rsa1024")
100 "rsa_padding_mode" => "pss",
101 "rsa_pss_saltlen" => 20,
102 "rsa_mgf1_md" => "SHA1"
104 sig_pss = key.sign("SHA256", data, pssopts)
105 assert_equal 128, sig_pss.bytesize
106 assert_equal true, key.verify("SHA256", sig_pss, data, pssopts)
107 assert_equal true, key.verify_pss("SHA256", sig_pss, data,
108 salt_length: 20, mgf1_hash: "SHA1")
109 # Defaults to PKCS #1 v1.5 padding => verification failure
110 assert_equal false, key.verify("SHA256", sig_pss, data)
113 def test_sign_verify_raw
114 key = Fixtures.pkey("rsa-1")
116 hash = OpenSSL::Digest.digest("SHA1", data)
117 signature = key.sign_raw("SHA1", hash)
118 assert_equal true, key.verify_raw("SHA1", signature, hash)
119 assert_equal true, key.verify("SHA1", signature, data)
122 assert_raise(OpenSSL::PKey::PKeyError) {
123 key.sign_raw("SHA1", "x" * (key.n.num_bytes + 1))
128 "rsa_padding_mode" => "pss",
129 "rsa_pss_saltlen" => 20,
130 "rsa_mgf1_md" => "SHA256"
132 sig_pss = key.sign_raw("SHA1", hash, pssopts)
133 assert_equal true, key.verify("SHA1", sig_pss, data, pssopts)
134 assert_equal true, key.verify_raw("SHA1", sig_pss, hash, pssopts)
137 def test_sign_verify_raw_legacy
138 key = Fixtures.pkey("rsa-1")
139 bits = key.n.num_bits
141 # Need right size for raw mode
142 plain0 = "x" * (bits/8)
143 cipher = key.private_encrypt(plain0, OpenSSL::PKey::RSA::NO_PADDING)
144 plain1 = key.public_decrypt(cipher, OpenSSL::PKey::RSA::NO_PADDING)
145 assert_equal(plain0, plain1)
147 # Need smaller size for pkcs1 mode
148 plain0 = "x" * (bits/8 - 11)
149 cipher1 = key.private_encrypt(plain0, OpenSSL::PKey::RSA::PKCS1_PADDING)
150 plain1 = key.public_decrypt(cipher1, OpenSSL::PKey::RSA::PKCS1_PADDING)
151 assert_equal(plain0, plain1)
153 cipherdef = key.private_encrypt(plain0) # PKCS1_PADDING is default
154 plain1 = key.public_decrypt(cipherdef)
155 assert_equal(plain0, plain1)
156 assert_equal(cipher1, cipherdef)
159 assert_raise(ArgumentError){ key.private_encrypt() }
160 assert_raise(ArgumentError){ key.private_encrypt("hi", 1, nil) }
161 assert_raise(OpenSSL::PKey::RSAError){ key.private_encrypt(plain0, 666) }
165 def test_verify_empty_rsa
166 rsa = OpenSSL::PKey::RSA.new
167 assert_raise(OpenSSL::PKey::PKeyError, "[Bug #12783]") {
168 rsa.verify("SHA1", "a", "b")
172 def test_sign_verify_pss
173 key = Fixtures.pkey("rsa1024")
175 invalid_data = "Sign me?"
177 signature = key.sign_pss("SHA256", data, salt_length: 20, mgf1_hash: "SHA1")
178 assert_equal 128, signature.bytesize
180 key.verify_pss("SHA256", signature, data, salt_length: 20, mgf1_hash: "SHA1")
182 key.verify_pss("SHA256", signature, data, salt_length: :auto, mgf1_hash: "SHA1")
184 key.verify_pss("SHA256", signature, invalid_data, salt_length: 20, mgf1_hash: "SHA1")
186 signature = key.sign_pss("SHA256", data, salt_length: :digest, mgf1_hash: "SHA1")
188 key.verify_pss("SHA256", signature, data, salt_length: 32, mgf1_hash: "SHA1")
190 key.verify_pss("SHA256", signature, data, salt_length: :auto, mgf1_hash: "SHA1")
192 key.verify_pss("SHA256", signature, data, salt_length: 20, mgf1_hash: "SHA1")
194 signature = key.sign_pss("SHA256", data, salt_length: :max, mgf1_hash: "SHA1")
196 key.verify_pss("SHA256", signature, data, salt_length: 94, mgf1_hash: "SHA1")
198 key.verify_pss("SHA256", signature, data, salt_length: :auto, mgf1_hash: "SHA1")
200 assert_raise(OpenSSL::PKey::RSAError) {
201 key.sign_pss("SHA256", data, salt_length: 95, mgf1_hash: "SHA1")
205 def test_encrypt_decrypt
206 rsapriv = Fixtures.pkey("rsa-1")
207 rsapub = OpenSSL::PKey.read(rsapriv.public_to_der)
209 # Defaults to PKCS #1 v1.5
211 enc = rsapub.encrypt(raw)
212 assert_equal raw, rsapriv.decrypt(enc)
215 assert_raise(OpenSSL::PKey::PKeyError) {
216 rsapub.encrypt(raw, { "nonexistent" => "option" })
220 def test_encrypt_decrypt_legacy
221 rsapriv = Fixtures.pkey("rsa-1")
222 rsapub = OpenSSL::PKey.read(rsapriv.public_to_der)
224 # Defaults to PKCS #1 v1.5
226 enc_legacy = rsapub.public_encrypt(raw)
227 assert_equal raw, rsapriv.decrypt(enc_legacy)
228 enc_new = rsapub.encrypt(raw)
229 assert_equal raw, rsapriv.private_decrypt(enc_new)
231 # OAEP with default parameters
233 enc_legacy = rsapub.public_encrypt(raw, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
234 assert_equal raw, rsapriv.decrypt(enc_legacy, { "rsa_padding_mode" => "oaep" })
235 enc_new = rsapub.encrypt(raw, { "rsa_padding_mode" => "oaep" })
236 assert_equal raw, rsapriv.private_decrypt(enc_legacy, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
240 rsa1024 = Fixtures.pkey("rsa1024")
242 pub = OpenSSL::PKey.read(rsa1024.public_to_der)
243 assert_not_equal rsa1024.export, pub.export
244 assert_equal rsa1024.public_to_pem, pub.export
246 # PKey is immutable in OpenSSL >= 3.0
247 if !openssl?(3, 0, 0)
248 key = OpenSSL::PKey::RSA.new
250 # key has only n, e and d
251 key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
252 assert_equal rsa1024.public_key.export, key.export
254 # key has only n, e, d, p and q
255 key.set_factors(rsa1024.p, rsa1024.q)
256 assert_equal rsa1024.public_key.export, key.export
258 # key has n, e, d, p, q, dmp1, dmq1 and iqmp
259 key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
260 assert_equal rsa1024.export, key.export
265 rsa1024 = Fixtures.pkey("rsa1024")
267 pub = OpenSSL::PKey.read(rsa1024.public_to_der)
268 assert_not_equal rsa1024.to_der, pub.to_der
269 assert_equal rsa1024.public_to_der, pub.to_der
271 # PKey is immutable in OpenSSL >= 3.0
272 if !openssl?(3, 0, 0)
273 key = OpenSSL::PKey::RSA.new
275 # key has only n, e and d
276 key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
277 assert_equal rsa1024.public_key.to_der, key.to_der
279 # key has only n, e, d, p and q
280 key.set_factors(rsa1024.p, rsa1024.q)
281 assert_equal rsa1024.public_key.to_der, key.to_der
283 # key has n, e, d, p, q, dmp1, dmq1 and iqmp
284 key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
285 assert_equal rsa1024.to_der, key.to_der
289 def test_RSAPrivateKey
290 rsa1024 = Fixtures.pkey("rsa1024")
291 asn1 = OpenSSL::ASN1::Sequence([
292 OpenSSL::ASN1::Integer(0),
293 OpenSSL::ASN1::Integer(rsa1024.n),
294 OpenSSL::ASN1::Integer(rsa1024.e),
295 OpenSSL::ASN1::Integer(rsa1024.d),
296 OpenSSL::ASN1::Integer(rsa1024.p),
297 OpenSSL::ASN1::Integer(rsa1024.q),
298 OpenSSL::ASN1::Integer(rsa1024.dmp1),
299 OpenSSL::ASN1::Integer(rsa1024.dmq1),
300 OpenSSL::ASN1::Integer(rsa1024.iqmp)
302 key = OpenSSL::PKey::RSA.new(asn1.to_der)
303 assert_predicate key, :private?
304 assert_same_rsa rsa1024, key
307 -----BEGIN RSA PRIVATE KEY-----
308 MIICXgIBAAKBgQDLwsSw1ECnPtT+PkOgHhcGA71nwC2/nL85VBGnRqDxOqjVh7Cx
309 aKPERYHsk4BPCkE3brtThPWc9kjHEQQ7uf9Y1rbCz0layNqHyywQEVLFmp1cpIt/
310 Q3geLv8ZD9pihowKJDyMDiN6ArYUmZczvW4976MU3+l54E6lF/JfFEU5hwIDAQAB
311 AoGBAKSl/MQarye1yOysqX6P8fDFQt68VvtXkNmlSiKOGuzyho0M+UVSFcs6k1L0
312 maDE25AMZUiGzuWHyaU55d7RXDgeskDMakD1v6ZejYtxJkSXbETOTLDwUWTn618T
313 gnb17tU1jktUtU67xK/08i/XodlgnQhs6VoHTuCh3Hu77O6RAkEA7+gxqBuZR572
314 74/akiW/SuXm0SXPEviyO1MuSRwtI87B02D0qgV8D1UHRm4AhMnJ8MCs1809kMQE
315 JiQUCrp9mQJBANlt2ngBO14us6NnhuAseFDTBzCHXwUUu1YKHpMMmxpnGqaldGgX
316 sOZB3lgJsT9VlGf3YGYdkLTNVbogQKlKpB8CQQDiSwkb4vyQfDe8/NpU5Not0fII
317 8jsDUCb+opWUTMmfbxWRR3FBNu8wnym/m19N4fFj8LqYzHX4KY0oVPu6qvJxAkEA
318 wa5snNekFcqONLIE4G5cosrIrb74sqL8GbGb+KuTAprzj5z1K8Bm0UW9lTjVDjDi
319 qRYgZfZSL+x1P/54+xTFSwJAY1FxA/N3QPCXCjPh5YqFxAMQs2VVYTfg+t0MEcJD
320 dPMQD5JX6g5HKnHFg2mZtoXQrWmJSn7p8GJK8yNTopEErA==
321 -----END RSA PRIVATE KEY-----
323 key = OpenSSL::PKey::RSA.new(pem)
324 assert_same_rsa rsa1024, key
326 assert_equal asn1.to_der, rsa1024.to_der
327 assert_equal pem, rsa1024.export
329 # Unknown PEM prepended
330 cert = issue_cert(OpenSSL::X509::Name.new([["CN", "nobody"]]), rsa1024, 1, [], nil, nil)
331 str = cert.to_text + cert.to_pem + rsa1024.to_pem
332 key = OpenSSL::PKey::RSA.new(str)
333 assert_same_rsa rsa1024, key
336 def test_RSAPrivateKey_encrypted
337 rsa1024 = Fixtures.pkey("rsa1024")
340 -----BEGIN RSA PRIVATE KEY-----
341 Proc-Type: 4,ENCRYPTED
342 DEK-Info: AES-128-CBC,733F5302505B34701FC41F5C0746E4C0
344 zgJniZZQfvv8TFx3LzV6zhAQVayvQVZlAYqFq2yWbbxzF7C+IBhKQle9IhUQ9j/y
345 /jkvol550LS8vZ7TX5WxyDLe12cdqzEvpR6jf3NbxiNysOCxwG4ErhaZGP+krcoB
346 ObuL0nvls/+3myy5reKEyy22+0GvTDjaChfr+FwJjXMG+IBCLscYdgZC1LQL6oAn
347 9xY5DH3W7BW4wR5ttxvtN32TkfVQh8xi3jrLrduUh+hV8DTiAiLIhv0Vykwhep2p
348 WZA+7qbrYaYM8GLLgLrb6LfBoxeNxAEKiTpl1quFkm+Hk1dKq0EhVnxHf92x0zVF
349 jRGZxAMNcrlCoE4f5XK45epVZSZvihdo1k73GPbp84aZ5P/xlO4OwZ3i4uCQXynl
350 jE9c+I+4rRWKyPz9gkkqo0+teJL8ifeKt/3ab6FcdA0aArynqmsKJMktxmNu83We
351 YVGEHZPeOlyOQqPvZqWsLnXQUfg54OkbuV4/4mWSIzxFXdFy/AekSeJugpswMXqn
352 oNck4qySNyfnlyelppXyWWwDfVus9CVAGZmJQaJExHMT/rQFRVchlmY0Ddr5O264
353 gcjv90o1NBOc2fNcqjivuoX7ROqys4K/YdNQ1HhQ7usJghADNOtuLI8ZqMh9akXD
354 Eqp6Ne97wq1NiJj0nt3SJlzTnOyTjzrTe0Y+atPkVKp7SsjkATMI9JdhXwGhWd7a
355 qFVl0owZiDasgEhyG2K5L6r+yaJLYkPVXZYC/wtWC3NEchnDWZGQcXzB4xROCQkD
356 OlWNYDkPiZioeFkA3/fTMvG4moB2Pp9Q4GU5fJ6k43Ccu1up8dX/LumZb4ecg5/x
357 -----END RSA PRIVATE KEY-----
359 key = OpenSSL::PKey::RSA.new(pem, "abcdef")
360 assert_same_rsa rsa1024, key
361 key = OpenSSL::PKey::RSA.new(pem) { "abcdef" }
362 assert_same_rsa rsa1024, key
364 cipher = OpenSSL::Cipher.new("aes-128-cbc")
365 exported = rsa1024.to_pem(cipher, "abcdef\0\1")
366 assert_same_rsa rsa1024, OpenSSL::PKey::RSA.new(exported, "abcdef\0\1")
367 assert_raise(OpenSSL::PKey::RSAError) {
368 OpenSSL::PKey::RSA.new(exported, "abcdef")
372 def test_RSAPublicKey
373 rsa1024 = Fixtures.pkey("rsa1024")
374 rsa1024pub = OpenSSL::PKey::RSA.new(rsa1024.public_to_der)
376 asn1 = OpenSSL::ASN1::Sequence([
377 OpenSSL::ASN1::Integer(rsa1024.n),
378 OpenSSL::ASN1::Integer(rsa1024.e)
380 key = OpenSSL::PKey::RSA.new(asn1.to_der)
381 assert_not_predicate key, :private?
382 assert_same_rsa rsa1024pub, key
385 -----BEGIN RSA PUBLIC KEY-----
386 MIGJAoGBAMvCxLDUQKc+1P4+Q6AeFwYDvWfALb+cvzlUEadGoPE6qNWHsLFoo8RF
387 geyTgE8KQTduu1OE9Zz2SMcRBDu5/1jWtsLPSVrI2ofLLBARUsWanVyki39DeB4u
388 /xkP2mKGjAokPIwOI3oCthSZlzO9bj3voxTf6XngTqUX8l8URTmHAgMBAAE=
389 -----END RSA PUBLIC KEY-----
391 key = OpenSSL::PKey::RSA.new(pem)
392 assert_same_rsa rsa1024pub, key
396 rsa1024 = Fixtures.pkey("rsa1024")
397 rsa1024pub = OpenSSL::PKey::RSA.new(rsa1024.public_to_der)
399 asn1 = OpenSSL::ASN1::Sequence([
400 OpenSSL::ASN1::Sequence([
401 OpenSSL::ASN1::ObjectId("rsaEncryption"),
402 OpenSSL::ASN1::Null(nil)
404 OpenSSL::ASN1::BitString(
405 OpenSSL::ASN1::Sequence([
406 OpenSSL::ASN1::Integer(rsa1024.n),
407 OpenSSL::ASN1::Integer(rsa1024.e)
411 key = OpenSSL::PKey::RSA.new(asn1.to_der)
412 assert_not_predicate key, :private?
413 assert_same_rsa rsa1024pub, key
416 -----BEGIN PUBLIC KEY-----
417 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLwsSw1ECnPtT+PkOgHhcGA71n
418 wC2/nL85VBGnRqDxOqjVh7CxaKPERYHsk4BPCkE3brtThPWc9kjHEQQ7uf9Y1rbC
419 z0layNqHyywQEVLFmp1cpIt/Q3geLv8ZD9pihowKJDyMDiN6ArYUmZczvW4976MU
420 3+l54E6lF/JfFEU5hwIDAQAB
421 -----END PUBLIC KEY-----
423 key = OpenSSL::PKey::RSA.new(pem)
424 assert_same_rsa rsa1024pub, key
426 assert_equal asn1.to_der, key.to_der
427 assert_equal pem, key.export
429 assert_equal asn1.to_der, rsa1024.public_to_der
430 assert_equal asn1.to_der, key.public_to_der
431 assert_equal pem, rsa1024.public_to_pem
432 assert_equal pem, key.public_to_pem
436 key = Fixtures.pkey("rsa1024")
437 pem3c = key.to_pem("aes-128-cbc", "key")
438 assert_match (/ENCRYPTED/), pem3c
439 assert_equal key.to_der, OpenSSL::PKey.read(pem3c, "key").to_der
440 assert_equal key.to_der, OpenSSL::PKey.read(pem3c) { "key" }.to_der
441 assert_raise(OpenSSL::PKey::PKeyError) {
442 OpenSSL::PKey.read(pem3c) { nil }
446 def test_private_encoding
447 rsa1024 = Fixtures.pkey("rsa1024")
448 asn1 = OpenSSL::ASN1::Sequence([
449 OpenSSL::ASN1::Integer(0),
450 OpenSSL::ASN1::Sequence([
451 OpenSSL::ASN1::ObjectId("rsaEncryption"),
452 OpenSSL::ASN1::Null(nil)
454 OpenSSL::ASN1::OctetString(rsa1024.to_der)
456 assert_equal asn1.to_der, rsa1024.private_to_der
457 assert_same_rsa rsa1024, OpenSSL::PKey.read(asn1.to_der)
460 -----BEGIN PRIVATE KEY-----
461 MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMvCxLDUQKc+1P4+
462 Q6AeFwYDvWfALb+cvzlUEadGoPE6qNWHsLFoo8RFgeyTgE8KQTduu1OE9Zz2SMcR
463 BDu5/1jWtsLPSVrI2ofLLBARUsWanVyki39DeB4u/xkP2mKGjAokPIwOI3oCthSZ
464 lzO9bj3voxTf6XngTqUX8l8URTmHAgMBAAECgYEApKX8xBqvJ7XI7Kypfo/x8MVC
465 3rxW+1eQ2aVKIo4a7PKGjQz5RVIVyzqTUvSZoMTbkAxlSIbO5YfJpTnl3tFcOB6y
466 QMxqQPW/pl6Ni3EmRJdsRM5MsPBRZOfrXxOCdvXu1TWOS1S1TrvEr/TyL9eh2WCd
467 CGzpWgdO4KHce7vs7pECQQDv6DGoG5lHnvbvj9qSJb9K5ebRJc8S+LI7Uy5JHC0j
468 zsHTYPSqBXwPVQdGbgCEycnwwKzXzT2QxAQmJBQKun2ZAkEA2W3aeAE7Xi6zo2eG
469 4Cx4UNMHMIdfBRS7VgoekwybGmcapqV0aBew5kHeWAmxP1WUZ/dgZh2QtM1VuiBA
470 qUqkHwJBAOJLCRvi/JB8N7z82lTk2i3R8gjyOwNQJv6ilZRMyZ9vFZFHcUE27zCf
471 Kb+bX03h8WPwupjMdfgpjShU+7qq8nECQQDBrmyc16QVyo40sgTgblyiysitvviy
472 ovwZsZv4q5MCmvOPnPUrwGbRRb2VONUOMOKpFiBl9lIv7HU//nj7FMVLAkBjUXED
473 83dA8JcKM+HlioXEAxCzZVVhN+D63QwRwkN08xAPklfqDkcqccWDaZm2hdCtaYlK
475 -----END PRIVATE KEY-----
477 assert_equal pem, rsa1024.private_to_pem
478 assert_same_rsa rsa1024, OpenSSL::PKey.read(pem)
481 def test_private_encoding_encrypted
482 rsa1024 = Fixtures.pkey("rsa1024")
483 encoded = rsa1024.private_to_der("aes-128-cbc", "abcdef")
484 asn1 = OpenSSL::ASN1.decode(encoded) # PKCS #8 EncryptedPrivateKeyInfo
485 assert_kind_of OpenSSL::ASN1::Sequence, asn1
486 assert_equal 2, asn1.value.size
487 assert_not_equal rsa1024.private_to_der, encoded
488 assert_same_rsa rsa1024, OpenSSL::PKey.read(encoded, "abcdef")
489 assert_same_rsa rsa1024, OpenSSL::PKey.read(encoded) { "abcdef" }
490 assert_raise(OpenSSL::PKey::PKeyError) { OpenSSL::PKey.read(encoded, "abcxyz") }
492 encoded = rsa1024.private_to_pem("aes-128-cbc", "abcdef")
493 assert_match (/BEGIN ENCRYPTED PRIVATE KEY/), encoded.lines[0]
494 assert_same_rsa rsa1024, OpenSSL::PKey.read(encoded, "abcdef")
496 # certtool --load-privkey=test/fixtures/pkey/rsa1024.pem --to-p8 --password=abcdef
498 -----BEGIN ENCRYPTED PRIVATE KEY-----
499 MIICojAcBgoqhkiG9w0BDAEDMA4ECLqajUdSNfzwAgIEkQSCAoCDWhxr1HUrKLXA
500 FsFGGQfPT0aKH4gZipaSXXQRl0KwifHwHoDtfo/mAkJVZMnUVOm1AQ4LTFS3EdTy
501 JUwICGEQHb7QAiokIRoi0K2yHhOxVO8qgbnWuisWpiT6Ru1jCqTs/wcqlqF7z2jM
502 oXDk/vuekKst1DDXDcHrzhDkwhCQWj6jt1r2Vwaryy0FyeqsWAgBDiK2LsnCgkGD
503 21uhNZ/iWMG6tvY9hB8MDdiBJ41YdSG/AKLulAxQ1ibJz0Tasu66TmwFvWhBlME+
504 QbqfgmkgWg5buu53SvDfCA47zXihclbtdfW+U3CJ9OJkx0535TVdZbuC1QgKXvG7
505 4iKGFRMWYJqZvZM3GL4xbC75AxjXZsdCfV81VjZxjeU6ung/NRzCuCUcmBOQzo1D
506 Vv6COwAa6ttQWM0Ti8oIQHdu5Qi+nuOEHDLxCxD962M37H99sEO5cESjmrGVxhEo
507 373L4+11geGSCajdp0yiAGnXQfwaKta8cL693bRObN+b1Y+vqtDKH26N9a4R3qgg
508 2XwgQ5GH5CODoXZpi0wxncXO+3YuuhGeArtzKSXLNxHzIMlY7wZX+0e9UU03zfV/
509 aOe4/q5DpkNxgHePt0oEpamSKY5W3jzVi1dlFWsRjud1p/Grt2zjSWTYClBlJqG1
510 A/3IeDZCu+acaePJjFyv5dFffIj2l4bAYB+LFrZlSu3F/EimO/dCDWJ9JGlMK0aF
511 l9brh7786Mo+YfyklaqMMEHBbbR2Es7PR6Gt7lrcIXmzy9XSsxT6IiD1rG9KKR3i
512 CQxTup6JAx9w1q+adL+Ypikoy3gGD/ccUY6TtPoCmkQwSCS+JqQnFlCiThDJbu+V
514 -----END ENCRYPTED PRIVATE KEY-----
516 assert_same_rsa rsa1024, OpenSSL::PKey.read(pem, "abcdef")
520 key = Fixtures.pkey("rsa1024")
522 assert_equal key.params, key2.params
524 # PKey is immutable in OpenSSL >= 3.0
525 if !openssl?(3, 0, 0)
526 key2.set_key(key2.n, 3, key2.d)
527 assert_not_equal key.params, key2.params
532 key = Fixtures.pkey("rsa2048")
533 deserialized = Marshal.load(Marshal.dump(key))
535 assert_equal key.to_der, deserialized.to_der
539 def assert_same_rsa(expected, key)
540 check_component(expected, key, [:n, :e, :d, :p, :q, :dmp1, :dmq1, :iqmp])