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