1=pod
2
3=head1 NAME
4
5pem_password_cb,
6PEM_read_bio_PrivateKey_ex, PEM_read_bio_PrivateKey,
7PEM_read_PrivateKey_ex, PEM_read_PrivateKey,
8PEM_write_bio_PrivateKey_ex, PEM_write_bio_PrivateKey,
9PEM_write_bio_PrivateKey_traditional,
10PEM_write_PrivateKey_ex, PEM_write_PrivateKey,
11PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
12PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
13PEM_read_bio_PUBKEY_ex, PEM_read_bio_PUBKEY,
14PEM_read_PUBKEY_ex, PEM_read_PUBKEY,
15PEM_write_bio_PUBKEY_ex, PEM_write_bio_PUBKEY,
16PEM_write_PUBKEY_ex, PEM_write_PUBKEY,
17PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
18PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
19PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
20PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
21PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
22PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
23PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
24PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters_ex, PEM_read_bio_Parameters,
25PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams,
26PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
27PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
28PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
29PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
30PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
31PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
32PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
33PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
34PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
35
36=head1 SYNOPSIS
37
38 #include <openssl/pem.h>
39
40 typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
41
42 EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
43                                      pem_password_cb *cb, void *u,
44                                      OSSL_LIB_CTX *libctx, const char *propq);
45 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
46                                   pem_password_cb *cb, void *u);
47 EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
48                                  void *u, OSSL_LIB_CTX *libctx,
49                                  const char *propq);
50 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
51                               pem_password_cb *cb, void *u);
52 int PEM_write_bio_PrivateKey_ex(BIO *bp, const EVP_PKEY *x,
53                                 const EVP_CIPHER *enc,
54                                 unsigned char *kstr, int klen,
55                                 pem_password_cb *cb, void *u,
56                                 OSSL_LIB_CTX *libctx, const char *propq);
57 int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
58                              unsigned char *kstr, int klen,
59                              pem_password_cb *cb, void *u);
60 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
61                                          const EVP_CIPHER *enc,
62                                          unsigned char *kstr, int klen,
63                                          pem_password_cb *cb, void *u);
64 int PEM_write_PrivateKey_ex(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
65                             unsigned char *kstr, int klen,
66                             pem_password_cb *cb, void *u,
67                             OSSL_LIB_CTX *libctx, const char *propq);
68 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
69                          unsigned char *kstr, int klen,
70                          pem_password_cb *cb, void *u);
71 int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
72                                   char *kstr, int klen,
73                                   pem_password_cb *cb, void *u);
74 int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
75                               char *kstr, int klen,
76                               pem_password_cb *cb, void *u);
77 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
78                                       char *kstr, int klen,
79                                       pem_password_cb *cb, void *u);
80 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
81                                   char *kstr, int klen,
82                                   pem_password_cb *cb, void *u);
83
84 EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
85                                  pem_password_cb *cb, void *u,
86                                  OSSL_LIB_CTX *libctx, const char *propq);
87 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
88                               pem_password_cb *cb, void *u);
89 EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
90                              pem_password_cb *cb, void *u,
91                              OSSL_LIB_CTX *libctx, const char *propq);
92 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
93                           pem_password_cb *cb, void *u);
94 int PEM_write_bio_PUBKEY_ex(BIO *bp, EVP_PKEY *x,
95                             OSSL_LIB_CTX *libctx, const char *propq);
96 int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
97 int PEM_write_PUBKEY_ex(FILE *fp, EVP_PKEY *x,
98                         OSSL_LIB_CTX *libctx, const char *propq);
99 int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
100
101 EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
102                                      OSSL_LIB_CTX *libctx, const char *propq);
103 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
104 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
105
106 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
107 X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
108 int PEM_write_bio_X509(BIO *bp, X509 *x);
109 int PEM_write_X509(FILE *fp, X509 *x);
110
111 X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
112 X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
113 int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
114 int PEM_write_X509_AUX(FILE *fp, X509 *x);
115
116 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
117                                 pem_password_cb *cb, void *u);
118 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
119                             pem_password_cb *cb, void *u);
120 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
121 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
122 int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
123 int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
124
125 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
126                                 pem_password_cb *cb, void *u);
127 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
128                             pem_password_cb *cb, void *u);
129 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
130 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
131
132 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
133 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
134 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
135 int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
136
137Deprecated since OpenSSL 3.0, can be hidden entirely by defining
138B<OPENSSL_API_COMPAT> with a suitable version value, see
139L<openssl_user_macros(7)>:
140
141 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
142                                 pem_password_cb *cb, void *u);
143 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
144                             pem_password_cb *cb, void *u);
145 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
146                                 unsigned char *kstr, int klen,
147                                 pem_password_cb *cb, void *u);
148 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
149                             unsigned char *kstr, int klen,
150                             pem_password_cb *cb, void *u);
151
152 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
153                                pem_password_cb *cb, void *u);
154 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
155                            pem_password_cb *cb, void *u);
156 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
157 int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
158
159 RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
160                              pem_password_cb *cb, void *u);
161 RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
162                          pem_password_cb *cb, void *u);
163 int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
164 int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
165
166 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
167                                 pem_password_cb *cb, void *u);
168 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
169                             pem_password_cb *cb, void *u);
170 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
171                                 unsigned char *kstr, int klen,
172                                 pem_password_cb *cb, void *u);
173 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
174                             unsigned char *kstr, int klen,
175                             pem_password_cb *cb, void *u);
176
177 DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
178                              pem_password_cb *cb, void *u);
179 DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
180                          pem_password_cb *cb, void *u);
181 int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
182 int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
183 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
184 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
185 int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
186 int PEM_write_DSAparams(FILE *fp, DSA *x);
187
188 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
189 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
190 int PEM_write_bio_DHparams(BIO *bp, DH *x);
191 int PEM_write_DHparams(FILE *fp, DH *x);
192
193=head1 DESCRIPTION
194
195All of the functions described on this page that have a I<TYPE> of B<DH>, B<DSA>
196and B<RSA> are deprecated. Applications should use OSSL_ENCODER_to_bio() and
197OSSL_DECODER_from_bio() instead.
198
199The PEM functions read or write structures in PEM format. In
200this sense PEM format is simply base64 encoded data surrounded
201by header lines.
202
203For more details about the meaning of arguments see the
204B<PEM FUNCTION ARGUMENTS> section.
205
206Each operation has four functions associated with it. For
207brevity the term "B<I<TYPE>> functions" will be used below to collectively
208refer to the B<PEM_read_bio_I<TYPE>>(), B<PEM_read_I<TYPE>>(),
209B<PEM_write_bio_I<TYPE>>(), and B<PEM_write_I<TYPE>>() functions.
210
211Some operations have additional variants that take a library context I<libctx>
212and a property query string I<propq>.
213
214The B<PrivateKey> functions read or write a private key in PEM format using
215an EVP_PKEY structure. The write routines use PKCS#8 private key format and are
216equivalent to PEM_write_bio_PKCS8PrivateKey(). The read functions transparently
217handle traditional and PKCS#8 format encrypted and unencrypted keys.
218
219PEM_write_bio_PrivateKey_traditional() writes out a private key in the
220"traditional" format with a simple private key marker and should only
221be used for compatibility with legacy programs.
222
223PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private
224key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using
225PKCS#5 v2.0 password based encryption algorithms. The I<cipher> argument
226specifies the encryption algorithm to use: unlike some other PEM routines the
227encryption is applied at the PKCS#8 level and not in the PEM headers. If
228I<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
229structure is used instead.
230
231PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
232also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
233it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
234to use is specified in the I<nid> parameter and should be the NID of the
235corresponding OBJECT IDENTIFIER (see NOTES section).
236
237The B<PUBKEY> functions process a public key using an EVP_PKEY
238structure. The public key is encoded as a SubjectPublicKeyInfo
239structure.
240
241The B<RSAPrivateKey> functions process an RSA private key using an
242RSA structure. The write routines uses traditional format. The read
243routines handles the same formats as the B<PrivateKey>
244functions but an error occurs if the private key is not RSA.
245
246The B<RSAPublicKey> functions process an RSA public key using an
247RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
248structure.
249
250The B<RSA_PUBKEY> functions also process an RSA public key using
251an RSA structure. However, the public key is encoded using a
252SubjectPublicKeyInfo structure and an error occurs if the public
253key is not RSA.
254
255The B<DSAPrivateKey> functions process a DSA private key using a
256DSA structure. The write routines uses traditional format. The read
257routines handles the same formats as the B<PrivateKey>
258functions but an error occurs if the private key is not DSA.
259
260The B<DSA_PUBKEY> functions process a DSA public key using
261a DSA structure. The public key is encoded using a
262SubjectPublicKeyInfo structure and an error occurs if the public
263key is not DSA.
264
265The B<Parameters> functions read or write key parameters in PEM format using
266an EVP_PKEY structure.  The encoding depends on the type of key; for DSA key
267parameters, it will be a Dss-Parms structure as defined in RFC2459, and for DH
268key parameters, it will be a PKCS#3 DHparameter structure.  I<These functions
269only exist for the B<BIO> type>.
270
271The B<DSAparams> functions process DSA parameters using a DSA
272structure. The parameters are encoded using a Dss-Parms structure
273as defined in RFC2459.
274
275The B<DHparams> functions process DH parameters using a DH
276structure. The parameters are encoded using a PKCS#3 DHparameter
277structure.
278
279The B<X509> functions process an X509 certificate using an X509
280structure. They will also process a trusted X509 certificate but
281any trust settings are discarded.
282
283The B<X509_AUX> functions process a trusted X509 certificate using
284an X509 structure.
285
286The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
287certificate request using an X509_REQ structure. The B<X509_REQ>
288write functions use B<CERTIFICATE REQUEST> in the header whereas
289the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
290(as required by some CAs). The B<X509_REQ> read functions will
291handle either form so there are no B<X509_REQ_NEW> read functions.
292
293The B<X509_CRL> functions process an X509 CRL using an X509_CRL
294structure.
295
296The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
297structure.
298
299=head1 PEM FUNCTION ARGUMENTS
300
301The PEM functions have many common arguments.
302
303The I<bp> BIO parameter (if present) specifies the BIO to read from
304or write to.
305
306The I<fp> FILE parameter (if present) specifies the FILE pointer to
307read from or write to.
308
309The PEM read functions all take an argument I<B<TYPE> **x> and return
310a I<B<TYPE> *> pointer. Where I<B<TYPE>> is whatever structure the function
311uses. If I<x> is NULL then the parameter is ignored. If I<x> is not
312NULL but I<*x> is NULL then the structure returned will be written
313to I<*x>. If neither I<x> nor I<*x> is NULL then an attempt is made
314to reuse the structure at I<*x> (but see BUGS and EXAMPLES sections).
315Irrespective of the value of I<x> a pointer to the structure is always
316returned (or NULL if an error occurred).
317
318The PEM functions which write private keys take an I<enc> parameter
319which specifies the encryption algorithm to use, encryption is done
320at the PEM level. If this parameter is set to NULL then the private
321key is written in unencrypted form.
322
323The I<cb> argument is the callback to use when querying for the pass
324phrase used for encrypted PEM structures (normally only private keys).
325
326For the PEM write routines if the I<kstr> parameter is not NULL then
327I<klen> bytes at I<kstr> are used as the passphrase and I<cb> is
328ignored.
329
330If the I<cb> parameters is set to NULL and the I<u> parameter is not
331NULL then the I<u> parameter is interpreted as a null terminated string
332to use as the passphrase. If both I<cb> and I<u> are NULL then the
333default callback routine is used which will typically prompt for the
334passphrase on the current terminal with echoing turned off.
335
336The default passphrase callback is sometimes inappropriate (for example
337in a GUI application) so an alternative can be supplied. The callback
338routine has the following form:
339
340 int cb(char *buf, int size, int rwflag, void *u);
341
342I<buf> is the buffer to write the passphrase to. I<size> is the maximum
343length of the passphrase (i.e. the size of buf). I<rwflag> is a flag
344which is set to 0 when reading and 1 when writing. A typical routine
345will ask the user to verify the passphrase (for example by prompting
346for it twice) if I<rwflag> is 1. The I<u> parameter has the same
347value as the I<u> parameter passed to the PEM routine. It allows
348arbitrary data to be passed to the callback by the application
349(for example a window handle in a GUI application). The callback
350I<must> return the number of characters in the passphrase or -1 if
351an error occurred.
352
353Some implementations may need to use cryptographic algorithms during their
354operation. If this is the case and I<libctx> and I<propq> parameters have been
355passed then any algorithm fetches will use that library context and property
356query string. Otherwise the default library context and property query string
357will be used.
358
359=head1 NOTES
360
361The PEM reading functions will skip any extraneous content or PEM data of
362a different type than they expect. This allows for example having a certificate
363(or multiple certificates) and a key in the PEM format in a single file.
364
365The old B<PrivateKey> write routines are retained for compatibility.
366New applications should write private keys using the
367PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
368because they are more secure (they use an iteration count of 2048 whereas
369the traditional routines use a count of 1) unless compatibility with older
370versions of OpenSSL is important.
371
372The B<PrivateKey> read routines can be used in all applications because
373they handle all formats transparently.
374
375A frequent cause of problems is attempting to use the PEM routines like
376this:
377
378 X509 *x;
379
380 PEM_read_bio_X509(bp, &x, 0, NULL);
381
382this is a bug because an attempt will be made to reuse the data at I<x>
383which is an uninitialised pointer.
384
385These functions make no assumption regarding the pass phrase received from the
386password callback.
387It will simply be treated as a byte sequence.
388
389=head1 PEM ENCRYPTION FORMAT
390
391These old B<PrivateKey> routines use a non standard technique for encryption.
392
393The private key (or other data) takes the following form:
394
395 -----BEGIN RSA PRIVATE KEY-----
396 Proc-Type: 4,ENCRYPTED
397 DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
398
399 ...base64 encoded data...
400 -----END RSA PRIVATE KEY-----
401
402The line beginning with I<Proc-Type> contains the version and the
403protection on the encapsulated data. The line beginning I<DEK-Info>
404contains two comma separated values: the encryption algorithm name as
405used by EVP_get_cipherbyname() and an initialization vector used by the
406cipher encoded as a set of hexadecimal digits. After those two lines is
407the base64-encoded encrypted data.
408
409The encryption key is derived using EVP_BytesToKey(). The cipher's
410initialization vector is passed to EVP_BytesToKey() as the I<salt>
411parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
412(regardless of the size of the initialization vector). The user's
413password is passed to EVP_BytesToKey() using the I<data> and I<datal>
414parameters. Finally, the library uses an iteration count of 1 for
415EVP_BytesToKey().
416
417The I<key> derived by EVP_BytesToKey() along with the original initialization
418vector is then used to decrypt the encrypted data. The I<iv> produced by
419EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
420the function.
421
422The pseudo code to derive the key would look similar to:
423
424 EVP_CIPHER* cipher = EVP_des_ede3_cbc();
425 EVP_MD* md = EVP_md5();
426
427 unsigned int nkey = EVP_CIPHER_get_key_length(cipher);
428 unsigned int niv = EVP_CIPHER_get_iv_length(cipher);
429 unsigned char key[nkey];
430 unsigned char iv[niv];
431
432 memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
433 rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
434 if (rc != nkey)
435     /* Error */
436
437 /* On success, use key and iv to initialize the cipher */
438
439=head1 BUGS
440
441The PEM read routines in some versions of OpenSSL will not correctly reuse
442an existing structure. Therefore, the following:
443
444 PEM_read_bio_X509(bp, &x, 0, NULL);
445
446where I<x> already contains a valid certificate, may not work, whereas:
447
448 X509_free(x);
449 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
450
451is guaranteed to work.
452
453=head1 RETURN VALUES
454
455The read routines return either a pointer to the structure read or NULL
456if an error occurred.
457
458The write routines return 1 for success or 0 for failure.
459
460=head1 EXAMPLES
461
462Although the PEM routines take several arguments in almost all applications
463most of them are set to 0 or NULL.
464
465To read a certificate with a library context in PEM format from a BIO:
466
467 X509 *x = X509_new_ex(libctx, NULL);
468
469 if (x == NULL)
470     /* Error */
471
472 if (PEM_read_bio_X509(bp, &x, 0, NULL) == NULL)
473     /* Error */
474
475Read a certificate in PEM format from a BIO:
476
477 X509 *x;
478
479 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
480 if (x == NULL)
481     /* Error */
482
483Alternative method:
484
485 X509 *x = NULL;
486
487 if (!PEM_read_bio_X509(bp, &x, 0, NULL))
488     /* Error */
489
490Write a certificate to a BIO:
491
492 if (!PEM_write_bio_X509(bp, x))
493     /* Error */
494
495Write a private key (using traditional format) to a BIO using
496triple DES encryption, the pass phrase is prompted for:
497
498 if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
499     /* Error */
500
501Write a private key (using PKCS#8 format) to a BIO using triple
502DES encryption, using the pass phrase "hello":
503
504 if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
505                                    NULL, 0, 0, "hello"))
506     /* Error */
507
508Read a private key from a BIO using a pass phrase callback:
509
510 key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
511 if (key == NULL)
512     /* Error */
513
514Skeleton pass phrase callback:
515
516 int pass_cb(char *buf, int size, int rwflag, void *u)
517 {
518
519     /* We'd probably do something else if 'rwflag' is 1 */
520     printf("Enter pass phrase for \"%s\"\n", (char *)u);
521
522     /* get pass phrase, length 'len' into 'tmp' */
523     char *tmp = "hello";
524     if (tmp == NULL) /* An error occurred */
525         return -1;
526
527     size_t len = strlen(tmp);
528
529     if (len > size)
530         len = size;
531     memcpy(buf, tmp, len);
532     return len;
533 }
534
535=head1 SEE ALSO
536
537L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>,
538L<passphrase-encoding(7)>
539
540=head1 HISTORY
541
542The old Netscape certificate sequences were no longer documented
543in OpenSSL 1.1.0; applications should use the PKCS7 standard instead
544as they will be formally deprecated in a future releases.
545
546PEM_read_bio_PrivateKey_ex(), PEM_read_PrivateKey_ex(),
547PEM_read_bio_PUBKEY_ex(), PEM_read_PUBKEY_ex() and
548PEM_read_bio_Parameters_ex() were introduced in OpenSSL 3.0.
549
550The functions PEM_read_bio_RSAPrivateKey(), PEM_read_RSAPrivateKey(),
551PEM_write_bio_RSAPrivateKey(), PEM_write_RSAPrivateKey(),
552PEM_read_bio_RSAPublicKey(), PEM_read_RSAPublicKey(),
553PEM_write_bio_RSAPublicKey(), PEM_write_RSAPublicKey(),
554PEM_read_bio_RSA_PUBKEY(), PEM_read_RSA_PUBKEY(),
555PEM_write_bio_RSA_PUBKEY(), PEM_write_RSA_PUBKEY(),
556PEM_read_bio_DSAPrivateKey(), PEM_read_DSAPrivateKey(),
557PEM_write_bio_DSAPrivateKey(), PEM_write_DSAPrivateKey(),
558PEM_read_bio_DSA_PUBKEY(), PEM_read_DSA_PUBKEY(),
559PEM_write_bio_DSA_PUBKEY(), PEM_write_DSA_PUBKEY();
560PEM_read_bio_DSAparams(), PEM_read_DSAparams(),
561PEM_write_bio_DSAparams(), PEM_write_DSAparams(),
562PEM_read_bio_DHparams(), PEM_read_DHparams(),
563PEM_write_bio_DHparams() and PEM_write_DHparams() were deprecated in 3.0.
564
565
566=head1 COPYRIGHT
567
568Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
569
570Licensed under the Apache License 2.0 (the "License").  You may not use
571this file except in compliance with the License.  You can obtain a copy
572in the file LICENSE in the source distribution or at
573L<https://www.openssl.org/source/license.html>.
574
575=cut
576