1PEM_READ_BIO_PRIVATEKEY(3)          OpenSSL         PEM_READ_BIO_PRIVATEKEY(3)
2
3
4

NAME

6       pem_password_cb, PEM_read_bio_PrivateKey, PEM_read_PrivateKey,
7       PEM_write_bio_PrivateKey, PEM_write_bio_PrivateKey_traditional,
8       PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey,
9       PEM_write_PKCS8PrivateKey, PEM_write_bio_PKCS8PrivateKey_nid,
10       PEM_write_PKCS8PrivateKey_nid, PEM_read_bio_PUBKEY, PEM_read_PUBKEY,
11       PEM_write_bio_PUBKEY, PEM_write_PUBKEY, PEM_read_bio_RSAPrivateKey,
12       PEM_read_RSAPrivateKey, PEM_write_bio_RSAPrivateKey,
13       PEM_write_RSAPrivateKey, PEM_read_bio_RSAPublicKey,
14       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,
17       PEM_read_bio_DSAPrivateKey, PEM_read_DSAPrivateKey,
18       PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
19       PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
20       PEM_write_DSA_PUBKEY, 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,
29       PEM_read_PKCS7, PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
30

SYNOPSIS

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        DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
115        DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
116        int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
117        int PEM_write_DSAparams(FILE *fp, DSA *x);
118
119        DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
120        DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
121        int PEM_write_bio_DHparams(BIO *bp, DH *x);
122        int PEM_write_DHparams(FILE *fp, DH *x);
123
124        X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
125        X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
126        int PEM_write_bio_X509(BIO *bp, X509 *x);
127        int PEM_write_X509(FILE *fp, X509 *x);
128
129        X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
130        X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
131        int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
132        int PEM_write_X509_AUX(FILE *fp, X509 *x);
133
134        X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
135                                        pem_password_cb *cb, void *u);
136        X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
137                                    pem_password_cb *cb, void *u);
138        int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
139        int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
140        int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
141        int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
142
143        X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
144                                        pem_password_cb *cb, void *u);
145        X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
146                                    pem_password_cb *cb, void *u);
147        int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
148        int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
149
150        PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
151        PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
152        int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
153        int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
154

DESCRIPTION

156       The PEM functions read or write structures in PEM format. In this sense
157       PEM format is simply base64 encoded data surrounded by header lines.
158
159       For more details about the meaning of arguments see the PEM FUNCTION
160       ARGUMENTS section.
161
162       Each operation has four functions associated with it. For brevity the
163       term "TYPE functions" will be used below to collectively refer to the
164       PEM_read_bio_TYPE(), PEM_read_TYPE(), PEM_write_bio_TYPE(), and
165       PEM_write_TYPE() functions.
166
167       The PrivateKey functions read or write a private key in PEM format
168       using an EVP_PKEY structure. The write routines use PKCS#8 private key
169       format and are equivalent to PEM_write_bio_PKCS8PrivateKey().The read
170       functions transparently handle traditional and PKCS#8 format encrypted
171       and unencrypted keys.
172
173       PEM_write_bio_PrivateKey_traditional() writes out a private key in the
174       "traditional" format with a simple private key marker and should only
175       be used for compatibility with legacy programs.
176
177       PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a
178       private key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo
179       format using PKCS#5 v2.0 password based encryption algorithms. The
180       cipher argument specifies the encryption algorithm to use: unlike some
181       other PEM routines the encryption is applied at the PKCS#8 level and
182       not in the PEM headers. If cipher is NULL then no encryption is used
183       and a PKCS#8 PrivateKeyInfo structure is used instead.
184
185       PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
186       also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo
187       however it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead.
188       The algorithm to use is specified in the nid parameter and should be
189       the NID of the corresponding OBJECT IDENTIFIER (see NOTES section).
190
191       The PUBKEY functions process a public key using an EVP_PKEY structure.
192       The public key is encoded as a SubjectPublicKeyInfo structure.
193
194       The RSAPrivateKey functions process an RSA private key using an RSA
195       structure. The write routines uses traditional format. The read
196       routines handles the same formats as the PrivateKey functions but an
197       error occurs if the private key is not RSA.
198
199       The RSAPublicKey functions process an RSA public key using an RSA
200       structure. The public key is encoded using a PKCS#1 RSAPublicKey
201       structure.
202
203       The RSA_PUBKEY functions also process an RSA public key using an RSA
204       structure. However the public key is encoded using a
205       SubjectPublicKeyInfo structure and an error occurs if the public key is
206       not RSA.
207
208       The DSAPrivateKey functions process a DSA private key using a DSA
209       structure. The write routines uses traditional format. The read
210       routines handles the same formats as the PrivateKey functions but an
211       error occurs if the private key is not DSA.
212
213       The DSA_PUBKEY functions process a DSA public key using a DSA
214       structure. The public key is encoded using a SubjectPublicKeyInfo
215       structure and an error occurs if the public key is not DSA.
216
217       The DSAparams functions process DSA parameters using a DSA structure.
218       The parameters are encoded using a Dss-Parms structure as defined in
219       RFC2459.
220
221       The DHparams functions process DH parameters using a DH structure. The
222       parameters are encoded using a PKCS#3 DHparameter structure.
223
224       The X509 functions process an X509 certificate using an X509 structure.
225       They will also process a trusted X509 certificate but any trust
226       settings are discarded.
227
228       The X509_AUX functions process a trusted X509 certificate using an X509
229       structure.
230
231       The X509_REQ and X509_REQ_NEW functions process a PKCS#10 certificate
232       request using an X509_REQ structure. The X509_REQ write functions use
233       CERTIFICATE REQUEST in the header whereas the X509_REQ_NEW functions
234       use NEW CERTIFICATE REQUEST (as required by some CAs). The X509_REQ
235       read functions will handle either form so there are no X509_REQ_NEW
236       read functions.
237
238       The X509_CRL functions process an X509 CRL using an X509_CRL structure.
239
240       The PKCS7 functions process a PKCS#7 ContentInfo using a PKCS7
241       structure.
242

PEM FUNCTION ARGUMENTS

244       The PEM functions have many common arguments.
245
246       The bp BIO parameter (if present) specifies the BIO to read from or
247       write to.
248
249       The fp FILE parameter (if present) specifies the FILE pointer to read
250       from or write to.
251
252       The PEM read functions all take an argument TYPE **x and return a TYPE
253       * pointer. Where TYPE is whatever structure the function uses. If x is
254       NULL then the parameter is ignored. If x is not NULL but *x is NULL
255       then the structure returned will be written to *x. If neither x nor *x
256       is NULL then an attempt is made to reuse the structure at *x (but see
257       BUGS and EXAMPLES sections).  Irrespective of the value of x a pointer
258       to the structure is always returned (or NULL if an error occurred).
259
260       The PEM functions which write private keys take an enc parameter which
261       specifies the encryption algorithm to use, encryption is done at the
262       PEM level. If this parameter is set to NULL then the private key is
263       written in unencrypted form.
264
265       The cb argument is the callback to use when querying for the pass
266       phrase used for encrypted PEM structures (normally only private keys).
267
268       For the PEM write routines if the kstr parameter is not NULL then klen
269       bytes at kstr are used as the passphrase and cb is ignored.
270
271       If the cb parameters is set to NULL and the u parameter is not NULL
272       then the u parameter is interpreted as a null terminated string to use
273       as the passphrase. If both cb and u are NULL then the default callback
274       routine is used which will typically prompt for the passphrase on the
275       current terminal with echoing turned off.
276
277       The default passphrase callback is sometimes inappropriate (for example
278       in a GUI application) so an alternative can be supplied. The callback
279       routine has the following form:
280
281        int cb(char *buf, int size, int rwflag, void *u);
282
283       buf is the buffer to write the passphrase to. size is the maximum
284       length of the passphrase (i.e. the size of buf). rwflag is a flag which
285       is set to 0 when reading and 1 when writing. A typical routine will ask
286       the user to verify the passphrase (for example by prompting for it
287       twice) if rwflag is 1. The u parameter has the same value as the u
288       parameter passed to the PEM routine. It allows arbitrary data to be
289       passed to the callback by the application (for example a window handle
290       in a GUI application). The callback must return the number of
291       characters in the passphrase or -1 if an error occurred.
292

NOTES

294       The old PrivateKey write routines are retained for compatibility.  New
295       applications should write private keys using the
296       PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
297       because they are more secure (they use an iteration count of 2048
298       whereas the traditional routines use a count of 1) unless compatibility
299       with older versions of OpenSSL is important.
300
301       The PrivateKey read routines can be used in all applications because
302       they handle all formats transparently.
303
304       A frequent cause of problems is attempting to use the PEM routines like
305       this:
306
307        X509 *x;
308
309        PEM_read_bio_X509(bp, &x, 0, NULL);
310
311       this is a bug because an attempt will be made to reuse the data at x
312       which is an uninitialised pointer.
313
314       These functions make no assumption regarding the pass phrase received
315       from the password callback.  It will simply be treated as a byte
316       sequence.
317

PEM ENCRYPTION FORMAT

319       These old PrivateKey routines use a non standard technique for
320       encryption.
321
322       The private key (or other data) takes the following form:
323
324        -----BEGIN RSA PRIVATE KEY-----
325        Proc-Type: 4,ENCRYPTED
326        DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
327
328        ...base64 encoded data...
329        -----END RSA PRIVATE KEY-----
330
331       The line beginning with Proc-Type contains the version and the
332       protection on the encapsulated data. The line beginning DEK-Info
333       contains two comma separated values: the encryption algorithm name as
334       used by EVP_get_cipherbyname() and an initialization vector used by the
335       cipher encoded as a set of hexadecimal digits. After those two lines is
336       the base64-encoded encrypted data.
337
338       The encryption key is derived using EVP_BytesToKey(). The cipher's
339       initialization vector is passed to EVP_BytesToKey() as the salt
340       parameter. Internally, PKCS5_SALT_LEN bytes of the salt are used
341       (regardless of the size of the initialization vector). The user's
342       password is passed to EVP_BytesToKey() using the data and datal
343       parameters. Finally, the library uses an iteration count of 1 for
344       EVP_BytesToKey().
345
346       The key derived by EVP_BytesToKey() along with the original
347       initialization vector is then used to decrypt the encrypted data. The
348       iv produced by EVP_BytesToKey() is not utilized or needed, and NULL
349       should be passed to the function.
350
351       The pseudo code to derive the key would look similar to:
352
353        EVP_CIPHER* cipher = EVP_des_ede3_cbc();
354        EVP_MD* md = EVP_md5();
355
356        unsigned int nkey = EVP_CIPHER_key_length(cipher);
357        unsigned int niv = EVP_CIPHER_iv_length(cipher);
358        unsigned char key[nkey];
359        unsigned char iv[niv];
360
361        memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
362        rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
363        if (rc != nkey)
364            /* Error */
365
366        /* On success, use key and iv to initialize the cipher */
367

BUGS

369       The PEM read routines in some versions of OpenSSL will not correctly
370       reuse an existing structure. Therefore the following:
371
372        PEM_read_bio_X509(bp, &x, 0, NULL);
373
374       where x already contains a valid certificate, may not work, whereas:
375
376        X509_free(x);
377        x = PEM_read_bio_X509(bp, NULL, 0, NULL);
378
379       is guaranteed to work.
380

RETURN VALUES

382       The read routines return either a pointer to the structure read or NULL
383       if an error occurred.
384
385       The write routines return 1 for success or 0 for failure.
386

EXAMPLES

388       Although the PEM routines take several arguments in almost all
389       applications most of them are set to 0 or NULL.
390
391       Read a certificate in PEM format from a BIO:
392
393        X509 *x;
394
395        x = PEM_read_bio_X509(bp, NULL, 0, NULL);
396        if (x == NULL)
397            /* Error */
398
399       Alternative method:
400
401        X509 *x = NULL;
402
403        if (!PEM_read_bio_X509(bp, &x, 0, NULL))
404            /* Error */
405
406       Write a certificate to a BIO:
407
408        if (!PEM_write_bio_X509(bp, x))
409            /* Error */
410
411       Write a private key (using traditional format) to a BIO using triple
412       DES encryption, the pass phrase is prompted for:
413
414        if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
415            /* Error */
416
417       Write a private key (using PKCS#8 format) to a BIO using triple DES
418       encryption, using the pass phrase "hello":
419
420        if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
421                                           NULL, 0, 0, "hello"))
422            /* Error */
423
424       Read a private key from a BIO using a pass phrase callback:
425
426        key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
427        if (key == NULL)
428            /* Error */
429
430       Skeleton pass phrase callback:
431
432        int pass_cb(char *buf, int size, int rwflag, void *u)
433        {
434
435            /* We'd probably do something else if 'rwflag' is 1 */
436            printf("Enter pass phrase for \"%s\"\n", (char *)u);
437
438            /* get pass phrase, length 'len' into 'tmp' */
439            char *tmp = "hello";
440            if (tmp == NULL) /* An error occurred */
441                return -1;
442
443            size_t len = strlen(tmp);
444
445            if (len > size)
446                len = size;
447            memcpy(buf, tmp, len);
448            return len;
449        }
450

SEE ALSO

452       EVP_EncryptInit(3), EVP_BytesToKey(3), passphrase-encoding(7)
453

HISTORY

455       The old Netscape certificate sequences were no longer documented in
456       OpenSSL 1.1.0; applications should use the PKCS7 standard instead as
457       they will be formally deprecated in a future releases.
458
460       Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
461
462       Licensed under the OpenSSL license (the "License").  You may not use
463       this file except in compliance with the License.  You can obtain a copy
464       in the file LICENSE in the source distribution or at
465       <https://www.openssl.org/source/license.html>.
466
467
468
4691.1.1c                            2019-05-28        PEM_READ_BIO_PRIVATEKEY(3)
Impressum