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