xref: /freebsd/secure/lib/libcrypto/man/man3/PEM_read_bio_PrivateKey.3 (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)

Standard preamble:
========================================================================
..
..
.. Set up some character translations and predefined strings. \*(-- will
give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
double quote, and \*(R" will give a right double quote. \*(C+ will
give a nicer C++. Capital omega is used to do unbreakable dashes and
therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
nothing in troff, for use with C<>.
.tr \(*W- . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\}
Escape single quotes in literal strings from groff's Unicode transform.

If the F register is >0, we'll generate index entries on stderr for
titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
entries marked with X<> in POD. Of course, you'll have to process the
output yourself in some meaningful fashion.

Avoid warning from groff about undefined register 'F'.
.. .nr rF 0 . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] .\} . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents . \" corrections for vroff . \" for low resolution devices (crt and lpr) \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} ========================================================================

Title "PEM_READ_BIO_PRIVATEKEY 3ossl"
PEM_READ_BIO_PRIVATEKEY 3ossl "2023-09-19" "3.0.11" "OpenSSL"
For nroff, turn off justification. Always turn off hyphenation; it makes
way too many mistakes in technical documents.
"NAME"
pem_password_cb, PEM_read_bio_PrivateKey_ex, PEM_read_bio_PrivateKey, PEM_read_PrivateKey_ex, PEM_read_PrivateKey, PEM_write_bio_PrivateKey_ex, PEM_write_bio_PrivateKey, PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey_ex, PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey, PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid, PEM_read_bio_PUBKEY_ex, PEM_read_bio_PUBKEY, PEM_read_PUBKEY_ex, PEM_read_PUBKEY, PEM_write_bio_PUBKEY_ex, PEM_write_bio_PUBKEY, PEM_write_PUBKEY_ex, PEM_write_PUBKEY, PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey, PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey, PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey, PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY, PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey, PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey, PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY, PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters_ex, PEM_read_bio_Parameters, PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams, PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams, PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams, PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509, PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX, PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ, PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW, PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL, PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7, PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
"SYNOPSIS"
Header "SYNOPSIS" .Vb 1 #include <openssl/pem.h> \& typedef int pem_password_cb(char *buf, int size, int rwflag, void *u); \& EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u); EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u); int PEM_write_bio_PrivateKey_ex(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_PrivateKey_ex(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid, char *kstr, int klen, pem_password_cb *cb, void *u); \& EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u); EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx, const char *propq); EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u); int PEM_write_bio_PUBKEY_ex(BIO *bp, EVP_PKEY *x, OSSL_LIB_CTX *libctx, const char *propq); int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x); int PEM_write_PUBKEY_ex(FILE *fp, EVP_PKEY *x, OSSL_LIB_CTX *libctx, const char *propq); int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x); \& EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x, OSSL_LIB_CTX *libctx, const char *propq); EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x); int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x); \& X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u); X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u); int PEM_write_bio_X509(BIO *bp, X509 *x); int PEM_write_X509(FILE *fp, X509 *x); \& X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u); X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u); int PEM_write_bio_X509_AUX(BIO *bp, X509 *x); int PEM_write_X509_AUX(FILE *fp, X509 *x); \& X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x, pem_password_cb *cb, void *u); X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x, pem_password_cb *cb, void *u); int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x); int PEM_write_X509_REQ(FILE *fp, X509_REQ *x); int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x); int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x); \& X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x, pem_password_cb *cb, void *u); X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x, pem_password_cb *cb, void *u); int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x); int PEM_write_X509_CRL(FILE *fp, X509_CRL *x); \& PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u); PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u); int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x); int PEM_write_PKCS7(FILE *fp, PKCS7 *x); .Ve

The following functions have been deprecated since OpenSSL 3.0, and can be hidden entirely by defining \s-1OPENSSL_API_COMPAT\s0 with a suitable version value, see openssl_user_macros\|(7):

.Vb 10 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x, pem_password_cb *cb, void *u); RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); \& RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, pem_password_cb *cb, void *u); RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x); int PEM_write_RSAPublicKey(FILE *fp, RSA *x); \& RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x, pem_password_cb *cb, void *u); RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x); int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x); \& DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x, pem_password_cb *cb, void *u); DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); \& DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x, pem_password_cb *cb, void *u); DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x); int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x); DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u); DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u); int PEM_write_bio_DSAparams(BIO *bp, DSA *x); int PEM_write_DSAparams(FILE *fp, DSA *x); \& DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u); DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u); int PEM_write_bio_DHparams(BIO *bp, DH *x); int PEM_write_DHparams(FILE *fp, DH *x); .Ve

"DESCRIPTION"
Header "DESCRIPTION" All of the functions described on this page that have a \s-1TYPE\s0 of \s-1DH\s0, \s-1DSA\s0 and \s-1RSA\s0 are deprecated. Applications should use OSSL_ENCODER_to_bio\|(3) and \fBOSSL_DECODER_from_bio\|(3) instead.

The \s-1PEM\s0 functions read or write structures in \s-1PEM\s0 format. In this sense \s-1PEM\s0 format is simply base64 encoded data surrounded by header lines.

For more details about the meaning of arguments see the \fB\s-1PEM FUNCTION ARGUMENTS\s0 section.

Each operation has four functions associated with it. For brevity the term "\f(BI\s-1TYPE\s0 functions" will be used below to collectively refer to the PEM_read_bio_\f(BI\s-1TYPE\s0(), PEM_read_\f(BI\s-1TYPE\s0(), \fBPEM_write_bio_\f(BI\s-1TYPE\s0(), and PEM_write_\f(BI\s-1TYPE\s0() functions.

Some operations have additional variants that take a library context libctx and a property query string propq. The X509, X509_REQ and X509_CRL objects may have an associated library context or property query string but there are no variants of these functions that take a library context or property query string parameter. In this case it is possible to set the appropriate library context or property query string by creating an empty X509, \fBX509_REQ or X509_CRL object using X509_new_ex\|(3), X509_REQ_new_ex\|(3) or X509_CRL_new_ex\|(3) respectively. Then pass the empty object as a parameter to the relevant \s-1PEM\s0 function. See the \*(L"\s-1EXAMPLES\*(R"\s0 section below.

The PrivateKey functions read or write a private key in \s-1PEM\s0 format using an \s-1EVP_PKEY\s0 structure. The write routines use PKCS#8 private key format and are equivalent to PEM_write_bio_PKCS8PrivateKey(). The read functions transparently handle traditional and PKCS#8 format encrypted and unencrypted keys.

\fBPEM_write_bio_PrivateKey_traditional() writes out a private key in the \*(L"traditional\*(R" format with a simple private key marker and should only be used for compatibility with legacy programs.

\fBPEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private key in an \s-1EVP_PKEY\s0 structure in PKCS#8 EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption algorithms. The cipher argument specifies the encryption algorithm to use: unlike some other \s-1PEM\s0 routines the encryption is applied at the PKCS#8 level and not in the \s-1PEM\s0 headers. If \fIcipher is \s-1NULL\s0 then no encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.

\fBPEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid() also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm to use is specified in the nid parameter and should be the \s-1NID\s0 of the corresponding \s-1OBJECT IDENTIFIER\s0 (see \s-1NOTES\s0 section).

The \s-1PUBKEY\s0 functions process a public key using an \s-1EVP_PKEY\s0 structure. The public key is encoded as a SubjectPublicKeyInfo structure.

The RSAPrivateKey functions process an \s-1RSA\s0 private key using an \s-1RSA\s0 structure. The write routines uses traditional format. The read routines handles the same formats as the PrivateKey functions but an error occurs if the private key is not \s-1RSA.\s0

The RSAPublicKey functions process an \s-1RSA\s0 public key using an \s-1RSA\s0 structure. The public key is encoded using a PKCS#1 RSAPublicKey structure.

The \s-1RSA_PUBKEY\s0 functions also process an \s-1RSA\s0 public key using an \s-1RSA\s0 structure. However, the public key is encoded using a SubjectPublicKeyInfo structure and an error occurs if the public key is not \s-1RSA.\s0

The DSAPrivateKey functions process a \s-1DSA\s0 private key using a \s-1DSA\s0 structure. The write routines uses traditional format. The read routines handles the same formats as the PrivateKey functions but an error occurs if the private key is not \s-1DSA.\s0

The \s-1DSA_PUBKEY\s0 functions process a \s-1DSA\s0 public key using a \s-1DSA\s0 structure. The public key is encoded using a SubjectPublicKeyInfo structure and an error occurs if the public key is not \s-1DSA.\s0

The Parameters functions read or write key parameters in \s-1PEM\s0 format using an \s-1EVP_PKEY\s0 structure. The encoding depends on the type of key; for \s-1DSA\s0 key parameters, it will be a Dss-Parms structure as defined in \s-1RFC2459,\s0 and for \s-1DH\s0 key parameters, it will be a PKCS#3 DHparameter structure. These functions only exist for the \f(BI\s-1BIO\s0 type.

The DSAparams functions process \s-1DSA\s0 parameters using a \s-1DSA\s0 structure. The parameters are encoded using a Dss-Parms structure as defined in \s-1RFC2459.\s0

The DHparams functions process \s-1DH\s0 parameters using a \s-1DH\s0 structure. The parameters are encoded using a PKCS#3 DHparameter structure.

The X509 functions process an X509 certificate using an X509 structure. They will also process a trusted X509 certificate but any trust settings are discarded.

The X509_AUX functions process a trusted X509 certificate using an X509 structure.

The X509_REQ and X509_REQ_NEW functions process a PKCS#10 certificate request using an X509_REQ structure. The X509_REQ write functions use \s-1CERTIFICATE REQUEST\s0 in the header whereas the X509_REQ_NEW functions use \s-1NEW CERTIFICATE REQUEST\s0 (as required by some CAs). The X509_REQ read functions will handle either form so there are no X509_REQ_NEW read functions.

The X509_CRL functions process an X509 \s-1CRL\s0 using an X509_CRL structure.

The \s-1PKCS7\s0 functions process a PKCS#7 ContentInfo using a \s-1PKCS7\s0 structure.

"PEM FUNCTION ARGUMENTS"
Header "PEM FUNCTION ARGUMENTS" The \s-1PEM\s0 functions have many common arguments.

The bp \s-1BIO\s0 parameter (if present) specifies the \s-1BIO\s0 to read from or write to.

The fp \s-1FILE\s0 parameter (if present) specifies the \s-1FILE\s0 pointer to read from or write to.

The \s-1PEM\s0 read functions all take an argument \f(BI\s-1TYPE\s0 **x and return a \f(BI\s-1TYPE\s0 * pointer. Where \f(BI\s-1TYPE\s0 is whatever structure the function uses. If x is \s-1NULL\s0 then the parameter is ignored. If x is not \s-1NULL\s0 but *x is \s-1NULL\s0 then the structure returned will be written to *x. If neither x nor *x is \s-1NULL\s0 then an attempt is made to reuse the structure at *x (but see \s-1BUGS\s0 and \s-1EXAMPLES\s0 sections). Irrespective of the value of x a pointer to the structure is always returned (or \s-1NULL\s0 if an error occurred).

The \s-1PEM\s0 functions which write private keys take an enc parameter which specifies the encryption algorithm to use, encryption is done at the \s-1PEM\s0 level. If this parameter is set to \s-1NULL\s0 then the private key is written in unencrypted form.

The cb argument is the callback to use when querying for the pass phrase used for encrypted \s-1PEM\s0 structures (normally only private keys).

For the \s-1PEM\s0 write routines if the kstr parameter is not \s-1NULL\s0 then \fIklen bytes at kstr are used as the passphrase and cb is ignored.

If the cb parameters is set to \s-1NULL\s0 and the u parameter is not \s-1NULL\s0 then the u parameter is interpreted as a \s-1NUL\s0 terminated string to use as the passphrase. If both cb and u are \s-1NULL\s0 then the default callback routine is used which will typically prompt for the passphrase on the current terminal with echoing turned off.

The default passphrase callback is sometimes inappropriate (for example in a \s-1GUI\s0 application) so an alternative can be supplied. The callback routine has the following form:

.Vb 1 int cb(char *buf, int size, int rwflag, void *u); .Ve

\fIbuf is the buffer to write the passphrase to. size is the maximum length of the passphrase (i.e. the size of buf). rwflag is a flag which is set to 0 when reading and 1 when writing. A typical routine will ask the user to verify the passphrase (for example by prompting for it twice) if rwflag is 1. The u parameter has the same value as the u parameter passed to the \s-1PEM\s0 routine. It allows arbitrary data to be passed to the callback by the application (for example a window handle in a \s-1GUI\s0 application). The callback \fImust return the number of characters in the passphrase or -1 if an error occurred. The passphrase can be arbitrary data; in the case where it is a string, it is not \s-1NUL\s0 terminated. See the \*(L"\s-1EXAMPLES\*(R"\s0 section below.

Some implementations may need to use cryptographic algorithms during their operation. If this is the case and libctx and propq parameters have been passed then any algorithm fetches will use that library context and property query string. Otherwise the default library context and property query string will be used.

"NOTES"
Header "NOTES" The \s-1PEM\s0 reading functions will skip any extraneous content or \s-1PEM\s0 data of a different type than they expect. This allows for example having a certificate (or multiple certificates) and a key in the \s-1PEM\s0 format in a single file.

The old PrivateKey write routines are retained for compatibility. New applications should write private keys using the \fBPEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines because they are more secure (they use an iteration count of 2048 whereas the traditional routines use a count of 1) unless compatibility with older versions of OpenSSL is important.

The PrivateKey read routines can be used in all applications because they handle all formats transparently.

A frequent cause of problems is attempting to use the \s-1PEM\s0 routines like this:

.Vb 1 X509 *x; \& PEM_read_bio_X509(bp, &x, 0, NULL); .Ve

this is a bug because an attempt will be made to reuse the data at x which is an uninitialised pointer.

These functions make no assumption regarding the pass phrase received from the password callback. It will simply be treated as a byte sequence.

"PEM ENCRYPTION FORMAT"
Header "PEM ENCRYPTION FORMAT" These old PrivateKey routines use a non standard technique for encryption.

The private key (or other data) takes the following form:

.Vb 3 -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89 \& ...base64 encoded data... -----END RSA PRIVATE KEY----- .Ve

The line beginning with Proc-Type contains the version and the protection on the encapsulated data. The line beginning DEK-Info contains two comma separated values: the encryption algorithm name as used by EVP_get_cipherbyname() and an initialization vector used by the cipher encoded as a set of hexadecimal digits. After those two lines is the base64-encoded encrypted data.

The encryption key is derived using EVP_BytesToKey(). The cipher's initialization vector is passed to EVP_BytesToKey() as the salt parameter. Internally, \s-1PKCS5_SALT_LEN\s0 bytes of the salt are used (regardless of the size of the initialization vector). The user's password is passed to EVP_BytesToKey() using the data and datal parameters. Finally, the library uses an iteration count of 1 for \fBEVP_BytesToKey().

The key derived by EVP_BytesToKey() along with the original initialization vector is then used to decrypt the encrypted data. The iv produced by \fBEVP_BytesToKey() is not utilized or needed, and \s-1NULL\s0 should be passed to the function.

The pseudo code to derive the key would look similar to:

.Vb 2 EVP_CIPHER* cipher = EVP_des_ede3_cbc(); EVP_MD* md = EVP_md5(); \& unsigned int nkey = EVP_CIPHER_get_key_length(cipher); unsigned int niv = EVP_CIPHER_get_iv_length(cipher); unsigned char key[nkey]; unsigned char iv[niv]; \& memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv); rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/); if (rc != nkey) /* Error */ \& /* On success, use key and iv to initialize the cipher */ .Ve

"BUGS"
Header "BUGS" The \s-1PEM\s0 read routines in some versions of OpenSSL will not correctly reuse an existing structure. Therefore, the following:

.Vb 1 PEM_read_bio_X509(bp, &x, 0, NULL); .Ve

where x already contains a valid certificate, may not work, whereas:

.Vb 2 X509_free(x); x = PEM_read_bio_X509(bp, NULL, 0, NULL); .Ve

is guaranteed to work. It is always acceptable for x to contain a newly allocated, empty X509 object (for example allocated via X509_new_ex\|(3)).

"RETURN VALUES"
Header "RETURN VALUES" The read routines return either a pointer to the structure read or \s-1NULL\s0 if an error occurred.

The write routines return 1 for success or 0 for failure.

"EXAMPLES"
Header "EXAMPLES" Although the \s-1PEM\s0 routines take several arguments in almost all applications most of them are set to 0 or \s-1NULL.\s0

To read a certificate with a library context in \s-1PEM\s0 format from a \s-1BIO:\s0

.Vb 1 X509 *x = X509_new_ex(libctx, NULL); \& if (x == NULL) /* Error */ \& if (PEM_read_bio_X509(bp, &x, 0, NULL) == NULL) /* Error */ .Ve

Read a certificate in \s-1PEM\s0 format from a \s-1BIO:\s0

.Vb 1 X509 *x; \& x = PEM_read_bio_X509(bp, NULL, 0, NULL); if (x == NULL) /* Error */ .Ve

Alternative method:

.Vb 1 X509 *x = NULL; \& if (!PEM_read_bio_X509(bp, &x, 0, NULL)) /* Error */ .Ve

Write a certificate to a \s-1BIO:\s0

.Vb 2 if (!PEM_write_bio_X509(bp, x)) /* Error */ .Ve

Write a private key (using traditional format) to a \s-1BIO\s0 using triple \s-1DES\s0 encryption, the pass phrase is prompted for:

.Vb 2 if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL)) /* Error */ .Ve

Write a private key (using PKCS#8 format) to a \s-1BIO\s0 using triple \s-1DES\s0 encryption, using the pass phrase \*(L"hello\*(R":

.Vb 3 if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello")) /* Error */ .Ve

Read a private key from a \s-1BIO\s0 using a pass phrase callback:

.Vb 3 key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key"); if (key == NULL) /* Error */ .Ve

Skeleton pass phrase callback:

.Vb 2 int pass_cb(char *buf, int size, int rwflag, void *u) { \& /* We\*(Aqd probably do something else if \*(Aqrwflag\*(Aq is 1 */ printf("Enter pass phrase for \e"%s\e"\en", (char *)u); \& /* get pass phrase, length \*(Aqlen\*(Aq into \*(Aqtmp\*(Aq */ char *tmp = "hello"; if (tmp == NULL) /* An error occurred */ return -1; \& size_t len = strlen(tmp); \& if (len > size) len = size; memcpy(buf, tmp, len); return len; } .Ve

"SEE ALSO"
Header "SEE ALSO" \fBEVP_EncryptInit\|(3), EVP_BytesToKey\|(3), \fBpassphrase-encoding\|(7)
"HISTORY"
Header "HISTORY" The old Netscape certificate sequences were no longer documented in OpenSSL 1.1.0; applications should use the \s-1PKCS7\s0 standard instead as they will be formally deprecated in a future releases.

\fBPEM_read_bio_PrivateKey_ex(), PEM_read_PrivateKey_ex(), \fBPEM_read_bio_PUBKEY_ex(), PEM_read_PUBKEY_ex() and \fBPEM_read_bio_Parameters_ex() were introduced in OpenSSL 3.0.

The functions PEM_read_bio_RSAPrivateKey(), PEM_read_RSAPrivateKey(), \fBPEM_write_bio_RSAPrivateKey(), PEM_write_RSAPrivateKey(), \fBPEM_read_bio_RSAPublicKey(), PEM_read_RSAPublicKey(), \fBPEM_write_bio_RSAPublicKey(), PEM_write_RSAPublicKey(), \fBPEM_read_bio_RSA_PUBKEY(), PEM_read_RSA_PUBKEY(), \fBPEM_write_bio_RSA_PUBKEY(), PEM_write_RSA_PUBKEY(), \fBPEM_read_bio_DSAPrivateKey(), PEM_read_DSAPrivateKey(), \fBPEM_write_bio_DSAPrivateKey(), PEM_write_DSAPrivateKey(), \fBPEM_read_bio_DSA_PUBKEY(), PEM_read_DSA_PUBKEY(), \fBPEM_write_bio_DSA_PUBKEY(), PEM_write_DSA_PUBKEY(); \fBPEM_read_bio_DSAparams(), PEM_read_DSAparams(), \fBPEM_write_bio_DSAparams(), PEM_write_DSAparams(), \fBPEM_read_bio_DHparams(), PEM_read_DHparams(), \fBPEM_write_bio_DHparams() and PEM_write_DHparams() were deprecated in 3.0.

"COPYRIGHT"
Header "COPYRIGHT" Copyright 2001-2022 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use this file except in compliance with the License. You can obtain a copy in the file \s-1LICENSE\s0 in the source distribution or at <https://www.openssl.org/source/license.html>.