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