@@ -136,6 +136,35 @@ ossl_pkey_new(EVP_PKEY *pkey)
136136 return obj ;
137137}
138138
139+ EVP_PKEY *
140+ ossl_pkey_read_generic (BIO * bio , VALUE pass )
141+ {
142+ void * ppass = (void * )pass ;
143+ EVP_PKEY * pkey ;
144+
145+ if ((pkey = d2i_PrivateKey_bio (bio , NULL )))
146+ goto out ;
147+ OSSL_BIO_reset (bio );
148+ if ((pkey = d2i_PKCS8PrivateKey_bio (bio , NULL , ossl_pem_passwd_cb , ppass )))
149+ goto out ;
150+ OSSL_BIO_reset (bio );
151+ if ((pkey = d2i_PUBKEY_bio (bio , NULL )))
152+ goto out ;
153+ OSSL_BIO_reset (bio );
154+ /* PEM_read_bio_PrivateKey() also parses PKCS #8 formats */
155+ if ((pkey = PEM_read_bio_PrivateKey (bio , NULL , ossl_pem_passwd_cb , ppass )))
156+ goto out ;
157+ OSSL_BIO_reset (bio );
158+ if ((pkey = PEM_read_bio_PUBKEY (bio , NULL , NULL , NULL )))
159+ goto out ;
160+ OSSL_BIO_reset (bio );
161+ if ((pkey = PEM_read_bio_Parameters (bio , NULL )))
162+ goto out ;
163+
164+ out :
165+ return pkey ;
166+ }
167+
139168/*
140169 * call-seq:
141170 * OpenSSL::PKey.read(string [, pwd ]) -> PKey
@@ -160,33 +189,11 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
160189 VALUE data , pass ;
161190
162191 rb_scan_args (argc , argv , "11" , & data , & pass );
163- pass = ossl_pem_passwd_value (pass );
164-
165192 bio = ossl_obj2bio (& data );
166- if ((pkey = d2i_PrivateKey_bio (bio , NULL )))
167- goto ok ;
168- OSSL_BIO_reset (bio );
169- if ((pkey = d2i_PKCS8PrivateKey_bio (bio , NULL , ossl_pem_passwd_cb , (void * )pass )))
170- goto ok ;
171- OSSL_BIO_reset (bio );
172- if ((pkey = d2i_PUBKEY_bio (bio , NULL )))
173- goto ok ;
174- OSSL_BIO_reset (bio );
175- /* PEM_read_bio_PrivateKey() also parses PKCS #8 formats */
176- if ((pkey = PEM_read_bio_PrivateKey (bio , NULL , ossl_pem_passwd_cb , (void * )pass )))
177- goto ok ;
178- OSSL_BIO_reset (bio );
179- if ((pkey = PEM_read_bio_PUBKEY (bio , NULL , NULL , NULL )))
180- goto ok ;
181- OSSL_BIO_reset (bio );
182- if ((pkey = PEM_read_bio_Parameters (bio , NULL )))
183- goto ok ;
184-
185- BIO_free (bio );
186- ossl_raise (ePKeyError , "Could not parse PKey" );
187-
188- ok :
193+ pkey = ossl_pkey_read_generic (bio , ossl_pem_passwd_value (pass ));
189194 BIO_free (bio );
195+ if (!pkey )
196+ ossl_raise (ePKeyError , "Could not parse PKey" );
190197 return ossl_pkey_new (pkey );
191198}
192199
0 commit comments