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_Parameters,
21       PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams,
22       PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
23       PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
24       PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
25       PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
26       PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
27       PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
28       PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
29       PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7,
30       PEM_read_PKCS7, PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
31

SYNOPSIS

33        #include <openssl/pem.h>
34
35        typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
36
37        EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
38                                          pem_password_cb *cb, void *u);
39        EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
40                                      pem_password_cb *cb, void *u);
41        int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
42                                     unsigned char *kstr, int klen,
43                                     pem_password_cb *cb, void *u);
44        int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
45                                                 const EVP_CIPHER *enc,
46                                                 unsigned char *kstr, int klen,
47                                                 pem_password_cb *cb, void *u);
48        int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
49                                 unsigned char *kstr, int klen,
50                                 pem_password_cb *cb, void *u);
51
52        int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
53                                          char *kstr, int klen,
54                                          pem_password_cb *cb, void *u);
55        int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
56                                      char *kstr, int klen,
57                                      pem_password_cb *cb, void *u);
58        int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
59                                              char *kstr, int klen,
60                                              pem_password_cb *cb, void *u);
61        int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
62                                          char *kstr, int klen,
63                                          pem_password_cb *cb, void *u);
64
65        EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
66                                      pem_password_cb *cb, void *u);
67        EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
68                                  pem_password_cb *cb, void *u);
69        int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
70        int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
71
72        RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
73                                        pem_password_cb *cb, void *u);
74        RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
75                                    pem_password_cb *cb, void *u);
76        int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
77                                        unsigned char *kstr, int klen,
78                                        pem_password_cb *cb, void *u);
79        int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
80                                    unsigned char *kstr, int klen,
81                                    pem_password_cb *cb, void *u);
82
83        RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
84                                       pem_password_cb *cb, void *u);
85        RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
86                                   pem_password_cb *cb, void *u);
87        int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
88        int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
89
90        RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
91                                     pem_password_cb *cb, void *u);
92        RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
93                                 pem_password_cb *cb, void *u);
94        int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
95        int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
96
97        DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
98                                        pem_password_cb *cb, void *u);
99        DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
100                                    pem_password_cb *cb, void *u);
101        int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
102                                        unsigned char *kstr, int klen,
103                                        pem_password_cb *cb, void *u);
104        int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
105                                    unsigned char *kstr, int klen,
106                                    pem_password_cb *cb, void *u);
107
108        DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
109                                     pem_password_cb *cb, void *u);
110        DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
111                                 pem_password_cb *cb, void *u);
112        int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
113        int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
114
115        EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
116        int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
117
118        DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
119        DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
120        int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
121        int PEM_write_DSAparams(FILE *fp, DSA *x);
122
123        DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
124        DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
125        int PEM_write_bio_DHparams(BIO *bp, DH *x);
126        int PEM_write_DHparams(FILE *fp, DH *x);
127
128        X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
129        X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
130        int PEM_write_bio_X509(BIO *bp, X509 *x);
131        int PEM_write_X509(FILE *fp, X509 *x);
132
133        X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
134        X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
135        int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
136        int PEM_write_X509_AUX(FILE *fp, X509 *x);
137
138        X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
139                                        pem_password_cb *cb, void *u);
140        X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
141                                    pem_password_cb *cb, void *u);
142        int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
143        int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
144        int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
145        int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
146
147        X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
148                                        pem_password_cb *cb, void *u);
149        X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
150                                    pem_password_cb *cb, void *u);
151        int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
152        int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
153
154        PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
155        PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
156        int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
157        int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
158

DESCRIPTION

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

PEM FUNCTION ARGUMENTS

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

NOTES

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

PEM ENCRYPTION FORMAT

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

BUGS

379       The PEM read routines in some versions of OpenSSL will not correctly
380       reuse an existing structure. Therefore, the following:
381
382        PEM_read_bio_X509(bp, &x, 0, NULL);
383
384       where x already contains a valid certificate, may not work, whereas:
385
386        X509_free(x);
387        x = PEM_read_bio_X509(bp, NULL, 0, NULL);
388
389       is guaranteed to work.
390

RETURN VALUES

392       The read routines return either a pointer to the structure read or NULL
393       if an error occurred.
394
395       The write routines return 1 for success or 0 for failure.
396

EXAMPLES

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

SEE ALSO

462       EVP_EncryptInit(3), EVP_BytesToKey(3), passphrase-encoding(7)
463

HISTORY

465       The old Netscape certificate sequences were no longer documented in
466       OpenSSL 1.1.0; applications should use the PKCS7 standard instead as
467       they will be formally deprecated in a future releases.
468
470       Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
471
472       Licensed under the OpenSSL license (the "License").  You may not use
473       this file except in compliance with the License.  You can obtain a copy
474       in the file LICENSE in the source distribution or at
475       <https://www.openssl.org/source/license.html>.
476
477
478
4791.1.1q                            2022-07-21        PEM_READ_BIO_PRIVATEKEY(3)
Impressum