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