End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
OpenSSL::PKey.read
OpenSSL::PKey.read(string [, pwd ]) → PKey
OpenSSL::PKey.read(io [, pwd ]) → PKey
ext/openssl/ossl_pkey.c
View on GitHub ↗
static VALUE
ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
{
EVP_PKEY *pkey;
BIO *bio;
VALUE data, pass;
rb_scan_args(argc, argv, "11", &data, &pass);
pass = ossl_pem_passwd_value(pass);
bio = ossl_obj2bio(&data);
if ((pkey = d2i_PrivateKey_bio(bio, NULL)))
goto ok;
OSSL_BIO_reset(bio);
if ((pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, ossl_pem_passwd_cb, (void *)pass)))
goto ok;
OSSL_BIO_reset(bio);
if ((pkey = d2i_PUBKEY_bio(bio, NULL)))
goto ok;
OSSL_BIO_reset(bio);
/* PEM_read_bio_PrivateKey() also parses PKCS #8 formats */
if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, ossl_pem_passwd_cb, (void *)pass)))
goto ok;
OSSL_BIO_reset(bio);
if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)))
goto ok;
BIO_free(bio);
ossl_raise(ePKeyError, "Could not parse PKey");
ok:
BIO_free(bio);
return ossl_pkey_new(pkey);
}
Reads a DER or PEM encoded string from string or io and returns an instance of the appropriate PKey class.
Parameters
-
_string+ is a DER- or PEM-encoded string containing an arbitrary private or public key.
-
io is an instance of
IOcontaining a DER- or PEM-encoded arbitrary private or public key. -
pwd is an optional password in case string or io is an encrypted PEM resource.