6 PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
7 PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey,
8 PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
9 PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
10 PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY,
11 PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
12 PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
13 PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
14 PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
15 PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
16 PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
17 PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
18 PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters, PEM_write_bio_Parameters,
19 PEM_read_bio_DSAparams, PEM_read_DSAparams,
20 PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
21 PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
22 PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
23 PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
24 PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
25 PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
26 PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
27 PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
28 PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
32 #include <openssl/pem.h>
34 typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
36 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
37 pem_password_cb *cb, void *u);
38 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
39 pem_password_cb *cb, void *u);
40 int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
41 unsigned char *kstr, int klen,
42 pem_password_cb *cb, void *u);
43 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
44 const EVP_CIPHER *enc,
45 unsigned char *kstr, int klen,
46 pem_password_cb *cb, void *u);
47 int PEM_write_PrivateKey(FILE *fp, 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_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
52 pem_password_cb *cb, void *u);
53 int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
55 pem_password_cb *cb, void *u);
56 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
58 pem_password_cb *cb, void *u);
59 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
61 pem_password_cb *cb, void *u);
63 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
64 pem_password_cb *cb, void *u);
65 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
66 pem_password_cb *cb, void *u);
67 int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
68 int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
70 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
71 pem_password_cb *cb, void *u);
72 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
73 pem_password_cb *cb, void *u);
74 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
75 unsigned char *kstr, int klen,
76 pem_password_cb *cb, void *u);
77 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
78 unsigned char *kstr, int klen,
79 pem_password_cb *cb, void *u);
81 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
82 pem_password_cb *cb, void *u);
83 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
84 pem_password_cb *cb, void *u);
85 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
86 int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
88 RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
89 pem_password_cb *cb, void *u);
90 RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
91 pem_password_cb *cb, void *u);
92 int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
93 int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
95 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
96 pem_password_cb *cb, void *u);
97 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
98 pem_password_cb *cb, void *u);
99 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
100 unsigned char *kstr, int klen,
101 pem_password_cb *cb, void *u);
102 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
103 unsigned char *kstr, int klen,
104 pem_password_cb *cb, void *u);
106 DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
107 pem_password_cb *cb, void *u);
108 DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
109 pem_password_cb *cb, void *u);
110 int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
111 int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
113 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
114 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
116 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
117 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
118 int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
119 int PEM_write_DSAparams(FILE *fp, DSA *x);
121 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
122 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
123 int PEM_write_bio_DHparams(BIO *bp, DH *x);
124 int PEM_write_DHparams(FILE *fp, DH *x);
126 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
127 X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
128 int PEM_write_bio_X509(BIO *bp, X509 *x);
129 int PEM_write_X509(FILE *fp, X509 *x);
131 X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
132 X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
133 int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
134 int PEM_write_X509_AUX(FILE *fp, X509 *x);
136 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
137 pem_password_cb *cb, void *u);
138 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
139 pem_password_cb *cb, void *u);
140 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
141 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
142 int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
143 int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
145 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
146 pem_password_cb *cb, void *u);
147 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
148 pem_password_cb *cb, void *u);
149 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
150 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
152 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
153 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
154 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
155 int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
159 The PEM functions read or write structures in PEM format. In
160 this sense PEM format is simply base64 encoded data surrounded
163 For more details about the meaning of arguments see the
164 B<PEM FUNCTION ARGUMENTS> section.
166 Each operation has four functions associated with it. For
167 brevity the term "B<I<TYPE>> functions" will be used below to collectively
168 refer to the B<PEM_read_bio_I<TYPE>>(), B<PEM_read_I<TYPE>>(),
169 B<PEM_write_bio_I<TYPE>>(), and B<PEM_write_I<TYPE>>() functions.
171 The B<PrivateKey> functions read or write a private key in PEM format using an
172 EVP_PKEY structure. The write routines use PKCS#8 private key format and are
173 equivalent to PEM_write_bio_PKCS8PrivateKey().The read functions transparently
174 handle traditional and PKCS#8 format encrypted and unencrypted keys.
176 PEM_write_bio_PrivateKey_traditional() writes out a private key in the
177 "traditional" format with a simple private key marker and should only
178 be used for compatibility with legacy programs.
180 PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private
181 key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using
182 PKCS#5 v2.0 password based encryption algorithms. The I<cipher> argument
183 specifies the encryption algorithm to use: unlike some other PEM routines the
184 encryption is applied at the PKCS#8 level and not in the PEM headers. If
185 I<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
186 structure is used instead.
188 PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
189 also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
190 it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
191 to use is specified in the I<nid> parameter and should be the NID of the
192 corresponding OBJECT IDENTIFIER (see NOTES section).
194 The B<PUBKEY> functions process a public key using an EVP_PKEY
195 structure. The public key is encoded as a SubjectPublicKeyInfo
198 The B<RSAPrivateKey> functions process an RSA private key using an
199 RSA structure. The write routines uses traditional format. The read
200 routines handles the same formats as the B<PrivateKey>
201 functions but an error occurs if the private key is not RSA.
203 The B<RSAPublicKey> functions process an RSA public key using an
204 RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
207 The B<RSA_PUBKEY> functions also process an RSA public key using
208 an RSA structure. However the public key is encoded using a
209 SubjectPublicKeyInfo structure and an error occurs if the public
212 The B<DSAPrivateKey> functions process a DSA private key using a
213 DSA structure. The write routines uses traditional format. The read
214 routines handles the same formats as the B<PrivateKey>
215 functions but an error occurs if the private key is not DSA.
217 The B<DSA_PUBKEY> functions process a DSA public key using
218 a DSA structure. The public key is encoded using a
219 SubjectPublicKeyInfo structure and an error occurs if the public
222 The B<Parameters> functions read or write key parameters in PEM format using
223 an EVP_PKEY structure. The encoding depends on the type of key; for DSA key
224 parameters, it will be a Dss-Parms structure as defined in RFC2459, and for DH
225 key parameters, it will be a PKCS#3 DHparameter structure. I<These functions
226 only exist for the B<BIO> type>.
228 The B<DSAparams> functions process DSA parameters using a DSA
229 structure. The parameters are encoded using a Dss-Parms structure
230 as defined in RFC2459.
232 The B<DHparams> functions process DH parameters using a DH
233 structure. The parameters are encoded using a PKCS#3 DHparameter
236 The B<X509> functions process an X509 certificate using an X509
237 structure. They will also process a trusted X509 certificate but
238 any trust settings are discarded.
240 The B<X509_AUX> functions process a trusted X509 certificate using
243 The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
244 certificate request using an X509_REQ structure. The B<X509_REQ>
245 write functions use B<CERTIFICATE REQUEST> in the header whereas
246 the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
247 (as required by some CAs). The B<X509_REQ> read functions will
248 handle either form so there are no B<X509_REQ_NEW> read functions.
250 The B<X509_CRL> functions process an X509 CRL using an X509_CRL
253 The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
256 =head1 PEM FUNCTION ARGUMENTS
258 The PEM functions have many common arguments.
260 The I<bp> BIO parameter (if present) specifies the BIO to read from
263 The I<fp> FILE parameter (if present) specifies the FILE pointer to
264 read from or write to.
266 The PEM read functions all take an argument I<B<TYPE> **x> and return
267 a I<B<TYPE> *> pointer. Where I<B<TYPE>> is whatever structure the function
268 uses. If I<x> is NULL then the parameter is ignored. If I<x> is not
269 NULL but I<*x> is NULL then the structure returned will be written
270 to I<*x>. If neither I<x> nor I<*x> is NULL then an attempt is made
271 to reuse the structure at I<*x> (but see BUGS and EXAMPLES sections).
272 Irrespective of the value of I<x> a pointer to the structure is always
273 returned (or NULL if an error occurred).
275 The PEM functions which write private keys take an I<enc> parameter
276 which specifies the encryption algorithm to use, encryption is done
277 at the PEM level. If this parameter is set to NULL then the private
278 key is written in unencrypted form.
280 The I<cb> argument is the callback to use when querying for the pass
281 phrase used for encrypted PEM structures (normally only private keys).
283 For the PEM write routines if the I<kstr> parameter is not NULL then
284 I<klen> bytes at I<kstr> are used as the passphrase and I<cb> is
287 If the I<cb> parameters is set to NULL and the I<u> parameter is not
288 NULL then the I<u> parameter is interpreted as a null terminated string
289 to use as the passphrase. If both I<cb> and I<u> are NULL then the
290 default callback routine is used which will typically prompt for the
291 passphrase on the current terminal with echoing turned off.
293 The default passphrase callback is sometimes inappropriate (for example
294 in a GUI application) so an alternative can be supplied. The callback
295 routine has the following form:
297 int cb(char *buf, int size, int rwflag, void *u);
299 I<buf> is the buffer to write the passphrase to. I<size> is the maximum
300 length of the passphrase (i.e. the size of buf). I<rwflag> is a flag
301 which is set to 0 when reading and 1 when writing. A typical routine
302 will ask the user to verify the passphrase (for example by prompting
303 for it twice) if I<rwflag> is 1. The I<u> parameter has the same
304 value as the I<u> parameter passed to the PEM routine. It allows
305 arbitrary data to be passed to the callback by the application
306 (for example a window handle in a GUI application). The callback
307 I<must> return the number of characters in the passphrase or -1 if
312 The old B<PrivateKey> write routines are retained for compatibility.
313 New applications should write private keys using the
314 PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
315 because they are more secure (they use an iteration count of 2048 whereas
316 the traditional routines use a count of 1) unless compatibility with older
317 versions of OpenSSL is important.
319 The B<PrivateKey> read routines can be used in all applications because
320 they handle all formats transparently.
322 A frequent cause of problems is attempting to use the PEM routines like
327 PEM_read_bio_X509(bp, &x, 0, NULL);
329 this is a bug because an attempt will be made to reuse the data at I<x>
330 which is an uninitialised pointer.
332 These functions make no assumption regarding the pass phrase received from the
334 It will simply be treated as a byte sequence.
336 =head1 PEM ENCRYPTION FORMAT
338 These old B<PrivateKey> routines use a non standard technique for encryption.
340 The private key (or other data) takes the following form:
342 -----BEGIN RSA PRIVATE KEY-----
343 Proc-Type: 4,ENCRYPTED
344 DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
346 ...base64 encoded data...
347 -----END RSA PRIVATE KEY-----
349 The line beginning with I<Proc-Type> contains the version and the
350 protection on the encapsulated data. The line beginning I<DEK-Info>
351 contains two comma separated values: the encryption algorithm name as
352 used by EVP_get_cipherbyname() and an initialization vector used by the
353 cipher encoded as a set of hexadecimal digits. After those two lines is
354 the base64-encoded encrypted data.
356 The encryption key is derived using EVP_BytesToKey(). The cipher's
357 initialization vector is passed to EVP_BytesToKey() as the I<salt>
358 parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
359 (regardless of the size of the initialization vector). The user's
360 password is passed to EVP_BytesToKey() using the I<data> and I<datal>
361 parameters. Finally, the library uses an iteration count of 1 for
364 The I<key> derived by EVP_BytesToKey() along with the original initialization
365 vector is then used to decrypt the encrypted data. The I<iv> produced by
366 EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
369 The pseudo code to derive the key would look similar to:
371 EVP_CIPHER* cipher = EVP_des_ede3_cbc();
372 EVP_MD* md = EVP_md5();
374 unsigned int nkey = EVP_CIPHER_key_length(cipher);
375 unsigned int niv = EVP_CIPHER_iv_length(cipher);
376 unsigned char key[nkey];
377 unsigned char iv[niv];
379 memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
380 rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
384 /* On success, use key and iv to initialize the cipher */
388 The PEM read routines in some versions of OpenSSL will not correctly reuse
389 an existing structure. Therefore the following:
391 PEM_read_bio_X509(bp, &x, 0, NULL);
393 where I<x> already contains a valid certificate, may not work, whereas:
396 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
398 is guaranteed to work.
402 The read routines return either a pointer to the structure read or NULL
403 if an error occurred.
405 The write routines return 1 for success or 0 for failure.
409 Although the PEM routines take several arguments in almost all applications
410 most of them are set to 0 or NULL.
412 Read a certificate in PEM format from a BIO:
416 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
424 if (!PEM_read_bio_X509(bp, &x, 0, NULL))
427 Write a certificate to a BIO:
429 if (!PEM_write_bio_X509(bp, x))
432 Write a private key (using traditional format) to a BIO using
433 triple DES encryption, the pass phrase is prompted for:
435 if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
438 Write a private key (using PKCS#8 format) to a BIO using triple
439 DES encryption, using the pass phrase "hello":
441 if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
442 NULL, 0, 0, "hello"))
445 Read a private key from a BIO using a pass phrase callback:
447 key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
451 Skeleton pass phrase callback:
453 int pass_cb(char *buf, int size, int rwflag, void *u)
456 /* We'd probably do something else if 'rwflag' is 1 */
457 printf("Enter pass phrase for \"%s\"\n", (char *)u);
459 /* get pass phrase, length 'len' into 'tmp' */
461 if (tmp == NULL) /* An error occurred */
464 size_t len = strlen(tmp);
468 memcpy(buf, tmp, len);
474 L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>,
475 L<passphrase-encoding(7)>
479 The old Netscape certificate sequences were no longer documented
480 in OpenSSL 1.1.0; applications should use the PKCS7 standard instead
481 as they will be formally deprecated in a future releases.
485 Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
487 Licensed under the Apache License 2.0 (the "License"). You may not use
488 this file except in compliance with the License. You can obtain a copy
489 in the file LICENSE in the source distribution or at
490 L<https://www.openssl.org/source/license.html>.