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