]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/PEM_read_bio_PrivateKey.pod
Add RFC 5755 attribute certificate support
[thirdparty/openssl.git] / doc / man3 / PEM_read_bio_PrivateKey.pod
CommitLineData
a29d78e9
DSH
1=pod
2
3=head1 NAME
4
91da5e77 5pem_password_cb,
9256e8a2
RL
6PEM_read_bio_PrivateKey_ex, PEM_read_bio_PrivateKey,
7PEM_read_PrivateKey_ex, PEM_read_PrivateKey,
8PEM_write_bio_PrivateKey_ex, PEM_write_bio_PrivateKey,
9PEM_write_bio_PrivateKey_traditional,
10PEM_write_PrivateKey_ex, PEM_write_PrivateKey,
05dba815 11PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
f097f81c 12PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
9256e8a2
RL
13PEM_read_bio_PUBKEY_ex, PEM_read_bio_PUBKEY,
14PEM_read_PUBKEY_ex, PEM_read_PUBKEY,
15PEM_write_bio_PUBKEY_ex, PEM_write_bio_PUBKEY,
16PEM_write_PUBKEY_ex, PEM_write_PUBKEY,
f097f81c
DSH
17PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
18PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
19PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
20PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
21PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
22PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
23PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
6e5ccd58
RL
24PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters_ex, PEM_read_bio_Parameters,
25PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams,
f097f81c
DSH
26PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
27PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
28PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
7dcee34c
DHG
29PEM_read_bio_X509_ACERT, PEM_read_X509_ACERT,
30PEM_write_bio_X509_ACERT, PEM_write_X509_ACERT,
f097f81c
DSH
31PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
32PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
33PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
34PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
35PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
a0474357 36PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
a29d78e9
DSH
37
38=head1 SYNOPSIS
39
40 #include <openssl/pem.h>
41
5bf6d418 42 typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
91da5e77 43
9256e8a2
RL
44 EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
45 pem_password_cb *cb, void *u,
46 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9 47 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
a0474357 48 pem_password_cb *cb, void *u);
137b274a 49 EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
b4250010 50 void *u, OSSL_LIB_CTX *libctx,
137b274a 51 const char *propq);
a29d78e9 52 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
a0474357 53 pem_password_cb *cb, void *u);
9256e8a2
RL
54 int PEM_write_bio_PrivateKey_ex(BIO *bp, const EVP_PKEY *x,
55 const EVP_CIPHER *enc,
56 unsigned char *kstr, int klen,
57 pem_password_cb *cb, void *u,
58 OSSL_LIB_CTX *libctx, const char *propq);
9fdcc21f 59 int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
a0474357
RS
60 unsigned char *kstr, int klen,
61 pem_password_cb *cb, void *u);
05dba815
DSH
62 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
63 const EVP_CIPHER *enc,
64 unsigned char *kstr, int klen,
65 pem_password_cb *cb, void *u);
9256e8a2
RL
66 int PEM_write_PrivateKey_ex(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
67 unsigned char *kstr, int klen,
68 pem_password_cb *cb, void *u,
69 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9 70 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
a0474357
RS
71 unsigned char *kstr, int klen,
72 pem_password_cb *cb, void *u);
a29d78e9 73 int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
a0474357
RS
74 char *kstr, int klen,
75 pem_password_cb *cb, void *u);
a29d78e9 76 int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
a0474357
RS
77 char *kstr, int klen,
78 pem_password_cb *cb, void *u);
9fdcc21f 79 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
a0474357
RS
80 char *kstr, int klen,
81 pem_password_cb *cb, void *u);
9fdcc21f 82 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
a0474357
RS
83 char *kstr, int klen,
84 pem_password_cb *cb, void *u);
a29d78e9 85
6e5ccd58
RL
86 EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
87 pem_password_cb *cb, void *u,
b4250010 88 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9 89 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
a0474357 90 pem_password_cb *cb, void *u);
6e5ccd58
RL
91 EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
92 pem_password_cb *cb, void *u,
b4250010 93 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9 94 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
a0474357 95 pem_password_cb *cb, void *u);
9256e8a2
RL
96 int PEM_write_bio_PUBKEY_ex(BIO *bp, EVP_PKEY *x,
97 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9 98 int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
9256e8a2
RL
99 int PEM_write_PUBKEY_ex(FILE *fp, EVP_PKEY *x,
100 OSSL_LIB_CTX *libctx, const char *propq);
a29d78e9
DSH
101 int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
102
e52b4215
SL
103 EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
104 OSSL_LIB_CTX *libctx, const char *propq);
105 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
106 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
107
108 X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
109 X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
110 int PEM_write_bio_X509(BIO *bp, X509 *x);
111 int PEM_write_X509(FILE *fp, X509 *x);
112
7dcee34c
DHG
113 X509_ACERT *PEM_read_bio_X509_ACERT(BIO *bp, X509_ACERT **x,
114 pem_password_cb *cb, void *u);
115 X509_ACERT *PEM_read_X509_ACERT(FILE *fp, X509_ACERT **x,
116 pem_password_cb *cb, void *u);
117 int PEM_write_bio_X509_ACERT(BIO *bp, X509_ACERT *x);
118 int PEM_write_X509_ACERT(FILE *fp, X509_ACERT *x);
119
e52b4215
SL
120 X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
121 X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
122 int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
123 int PEM_write_X509_AUX(FILE *fp, X509 *x);
124
125 X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
126 pem_password_cb *cb, void *u);
127 X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
128 pem_password_cb *cb, void *u);
129 int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
130 int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
131 int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
132 int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
133
134 X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
135 pem_password_cb *cb, void *u);
136 X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
137 pem_password_cb *cb, void *u);
138 int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
139 int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
140
141 PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
142 PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
143 int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
144 int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
145
3dbf8243
MC
146The following functions have been deprecated since OpenSSL 3.0, and can be
147hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
148see L<openssl_user_macros(7)>:
e52b4215 149
a29d78e9 150 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
a0474357 151 pem_password_cb *cb, void *u);
a29d78e9 152 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
a0474357 153 pem_password_cb *cb, void *u);
a29d78e9 154 int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
a0474357
RS
155 unsigned char *kstr, int klen,
156 pem_password_cb *cb, void *u);
a29d78e9 157 int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
a0474357
RS
158 unsigned char *kstr, int klen,
159 pem_password_cb *cb, void *u);
a29d78e9
DSH
160
161 RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
a0474357 162 pem_password_cb *cb, void *u);
a29d78e9 163 RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
a0474357 164 pem_password_cb *cb, void *u);
a29d78e9 165 int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
a29d78e9
DSH
166 int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
167
168 RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
a0474357 169 pem_password_cb *cb, void *u);
a29d78e9 170 RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
a0474357 171 pem_password_cb *cb, void *u);
a29d78e9 172 int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
a29d78e9
DSH
173 int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
174
175 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
a0474357 176 pem_password_cb *cb, void *u);
a29d78e9 177 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
a0474357 178 pem_password_cb *cb, void *u);
a29d78e9 179 int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
a0474357
RS
180 unsigned char *kstr, int klen,
181 pem_password_cb *cb, void *u);
a29d78e9 182 int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
a0474357
RS
183 unsigned char *kstr, int klen,
184 pem_password_cb *cb, void *u);
a29d78e9
DSH
185
186 DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
a0474357 187 pem_password_cb *cb, void *u);
a29d78e9 188 DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
a0474357 189 pem_password_cb *cb, void *u);
a29d78e9 190 int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
a29d78e9 191 int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
a29d78e9 192 DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
a29d78e9 193 DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
a29d78e9 194 int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
a29d78e9
DSH
195 int PEM_write_DSAparams(FILE *fp, DSA *x);
196
197 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
a29d78e9 198 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
a29d78e9 199 int PEM_write_bio_DHparams(BIO *bp, DH *x);
a29d78e9
DSH
200 int PEM_write_DHparams(FILE *fp, DH *x);
201
a29d78e9
DSH
202=head1 DESCRIPTION
203
e52b4215 204All of the functions described on this page that have a I<TYPE> of B<DH>, B<DSA>
7510aee2
E
205and B<RSA> are deprecated. Applications should use L<OSSL_ENCODER_to_bio(3)> and
206L<OSSL_DECODER_from_bio(3)> instead.
e52b4215 207
a29d78e9
DSH
208The PEM functions read or write structures in PEM format. In
209this sense PEM format is simply base64 encoded data surrounded
210by header lines.
211
212For more details about the meaning of arguments see the
213B<PEM FUNCTION ARGUMENTS> section.
214
215Each operation has four functions associated with it. For
bbecf04e
RL
216brevity the term "B<I<TYPE>> functions" will be used below to collectively
217refer to the B<PEM_read_bio_I<TYPE>>(), B<PEM_read_I<TYPE>>(),
218B<PEM_write_bio_I<TYPE>>(), and B<PEM_write_I<TYPE>>() functions.
a29d78e9 219
137b274a 220Some operations have additional variants that take a library context I<libctx>
e48fe798
MC
221and a property query string I<propq>. The B<X509>, B<X509_REQ> and B<X509_CRL>
222objects may have an associated library context or property query string but
223there are no variants of these functions that take a library context or property
224query string parameter. In this case it is possible to set the appropriate
225library context or property query string by creating an empty B<X509>,
226B<X509_REQ> or B<X509_CRL> object using L<X509_new_ex(3)>, L<X509_REQ_new_ex(3)>
227or L<X509_CRL_new_ex(3)> respectively. Then pass the empty object as a parameter
228to the relevant PEM function. See the L</EXAMPLES> section below.
137b274a 229
9256e8a2
RL
230The B<PrivateKey> functions read or write a private key in PEM format using
231an EVP_PKEY structure. The write routines use PKCS#8 private key format and are
232equivalent to PEM_write_bio_PKCS8PrivateKey(). The read functions transparently
05dba815 233handle traditional and PKCS#8 format encrypted and unencrypted keys.
a29d78e9 234
a95d7574
RS
235PEM_write_bio_PrivateKey_traditional() writes out a private key in the
236"traditional" format with a simple private key marker and should only
237be used for compatibility with legacy programs.
05dba815
DSH
238
239PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private
240key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using
bbecf04e 241PKCS#5 v2.0 password based encryption algorithms. The I<cipher> argument
05dba815
DSH
242specifies the encryption algorithm to use: unlike some other PEM routines the
243encryption is applied at the PKCS#8 level and not in the PEM headers. If
bbecf04e 244I<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
05dba815 245structure is used instead.
a29d78e9
DSH
246
247PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
248also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
249it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
bbecf04e 250to use is specified in the I<nid> parameter and should be the NID of the
a29d78e9
DSH
251corresponding OBJECT IDENTIFIER (see NOTES section).
252
253The B<PUBKEY> functions process a public key using an EVP_PKEY
254structure. The public key is encoded as a SubjectPublicKeyInfo
255structure.
256
257The B<RSAPrivateKey> functions process an RSA private key using an
05dba815
DSH
258RSA structure. The write routines uses traditional format. The read
259routines handles the same formats as the B<PrivateKey>
a29d78e9
DSH
260functions but an error occurs if the private key is not RSA.
261
262The B<RSAPublicKey> functions process an RSA public key using an
263RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
264structure.
265
266The B<RSA_PUBKEY> functions also process an RSA public key using
8c1cbc72 267an RSA structure. However, the public key is encoded using a
a29d78e9
DSH
268SubjectPublicKeyInfo structure and an error occurs if the public
269key is not RSA.
270
271The B<DSAPrivateKey> functions process a DSA private key using a
05dba815
DSH
272DSA structure. The write routines uses traditional format. The read
273routines handles the same formats as the B<PrivateKey>
a29d78e9
DSH
274functions but an error occurs if the private key is not DSA.
275
276The B<DSA_PUBKEY> functions process a DSA public key using
277a DSA structure. The public key is encoded using a
278SubjectPublicKeyInfo structure and an error occurs if the public
279key is not DSA.
280
9a6abb95
RL
281The B<Parameters> functions read or write key parameters in PEM format using
282an EVP_PKEY structure. The encoding depends on the type of key; for DSA key
283parameters, it will be a Dss-Parms structure as defined in RFC2459, and for DH
284key parameters, it will be a PKCS#3 DHparameter structure. I<These functions
285only exist for the B<BIO> type>.
286
a29d78e9 287The B<DSAparams> functions process DSA parameters using a DSA
f097f81c
DSH
288structure. The parameters are encoded using a Dss-Parms structure
289as defined in RFC2459.
a29d78e9
DSH
290
291The B<DHparams> functions process DH parameters using a DH
292structure. The parameters are encoded using a PKCS#3 DHparameter
293structure.
294
295The B<X509> functions process an X509 certificate using an X509
296structure. They will also process a trusted X509 certificate but
297any trust settings are discarded.
298
7dcee34c
DHG
299The B<X509_ACERT> functions process an X509 attribute certificate using
300an X509_ACERT structure.
301
a29d78e9 302The B<X509_AUX> functions process a trusted X509 certificate using
1bc74519 303an X509 structure.
a29d78e9
DSH
304
305The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
306certificate request using an X509_REQ structure. The B<X509_REQ>
307write functions use B<CERTIFICATE REQUEST> in the header whereas
308the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
309(as required by some CAs). The B<X509_REQ> read functions will
310handle either form so there are no B<X509_REQ_NEW> read functions.
311
312The B<X509_CRL> functions process an X509 CRL using an X509_CRL
313structure.
314
315The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
316structure.
317
a29d78e9
DSH
318=head1 PEM FUNCTION ARGUMENTS
319
320The PEM functions have many common arguments.
321
bbecf04e 322The I<bp> BIO parameter (if present) specifies the BIO to read from
a29d78e9
DSH
323or write to.
324
bbecf04e 325The I<fp> FILE parameter (if present) specifies the FILE pointer to
a29d78e9
DSH
326read from or write to.
327
bbecf04e
RL
328The PEM read functions all take an argument I<B<TYPE> **x> and return
329a I<B<TYPE> *> pointer. Where I<B<TYPE>> is whatever structure the function
330uses. If I<x> is NULL then the parameter is ignored. If I<x> is not
331NULL but I<*x> is NULL then the structure returned will be written
332to I<*x>. If neither I<x> nor I<*x> is NULL then an attempt is made
333to reuse the structure at I<*x> (but see BUGS and EXAMPLES sections).
334Irrespective of the value of I<x> a pointer to the structure is always
a29d78e9
DSH
335returned (or NULL if an error occurred).
336
bbecf04e 337The PEM functions which write private keys take an I<enc> parameter
a29d78e9
DSH
338which specifies the encryption algorithm to use, encryption is done
339at the PEM level. If this parameter is set to NULL then the private
340key is written in unencrypted form.
341
bbecf04e 342The I<cb> argument is the callback to use when querying for the pass
a29d78e9
DSH
343phrase used for encrypted PEM structures (normally only private keys).
344
bbecf04e
RL
345For the PEM write routines if the I<kstr> parameter is not NULL then
346I<klen> bytes at I<kstr> are used as the passphrase and I<cb> is
a29d78e9
DSH
347ignored.
348
bbecf04e 349If the I<cb> parameters is set to NULL and the I<u> parameter is not
5b5342e0 350NULL then the I<u> parameter is interpreted as a NUL terminated string
bbecf04e 351to use as the passphrase. If both I<cb> and I<u> are NULL then the
a29d78e9
DSH
352default callback routine is used which will typically prompt for the
353passphrase on the current terminal with echoing turned off.
354
355The default passphrase callback is sometimes inappropriate (for example
356in a GUI application) so an alternative can be supplied. The callback
357routine has the following form:
358
359 int cb(char *buf, int size, int rwflag, void *u);
360
bbecf04e
RL
361I<buf> is the buffer to write the passphrase to. I<size> is the maximum
362length of the passphrase (i.e. the size of buf). I<rwflag> is a flag
a29d78e9
DSH
363which is set to 0 when reading and 1 when writing. A typical routine
364will ask the user to verify the passphrase (for example by prompting
bbecf04e
RL
365for it twice) if I<rwflag> is 1. The I<u> parameter has the same
366value as the I<u> parameter passed to the PEM routine. It allows
a29d78e9
DSH
367arbitrary data to be passed to the callback by the application
368(for example a window handle in a GUI application). The callback
bbecf04e 369I<must> return the number of characters in the passphrase or -1 if
5b5342e0
TM
370an error occurred. The passphrase can be arbitrary data; in the case where it
371is a string, it is not NUL terminated. See the L</EXAMPLES> section below.
a29d78e9 372
137b274a
MC
373Some implementations may need to use cryptographic algorithms during their
374operation. If this is the case and I<libctx> and I<propq> parameters have been
375passed then any algorithm fetches will use that library context and property
376query string. Otherwise the default library context and property query string
377will be used.
378
a29d78e9
DSH
379=head1 NOTES
380
5cffc49f
TM
381The PEM reading functions will skip any extraneous content or PEM data of
382a different type than they expect. This allows for example having a certificate
383(or multiple certificates) and a key in the PEM format in a single file.
384
a29d78e9
DSH
385The old B<PrivateKey> write routines are retained for compatibility.
386New applications should write private keys using the
387PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
388because they are more secure (they use an iteration count of 2048 whereas
389the traditional routines use a count of 1) unless compatibility with older
390versions of OpenSSL is important.
391
392The B<PrivateKey> read routines can be used in all applications because
393they handle all formats transparently.
394
395A frequent cause of problems is attempting to use the PEM routines like
396this:
397
398 X509 *x;
e9b77246 399
a29d78e9
DSH
400 PEM_read_bio_X509(bp, &x, 0, NULL);
401
bbecf04e 402this is a bug because an attempt will be made to reuse the data at I<x>
a29d78e9
DSH
403which is an uninitialised pointer.
404
84814344
RL
405These functions make no assumption regarding the pass phrase received from the
406password callback.
407It will simply be treated as a byte sequence.
408
06623ff0
DSH
409=head1 PEM ENCRYPTION FORMAT
410
a0474357 411These old B<PrivateKey> routines use a non standard technique for encryption.
06623ff0 412
1bc74519 413The private key (or other data) takes the following form:
06623ff0
DSH
414
415 -----BEGIN RSA PRIVATE KEY-----
416 Proc-Type: 4,ENCRYPTED
417 DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
418
419 ...base64 encoded data...
420 -----END RSA PRIVATE KEY-----
421
a0474357
RS
422The line beginning with I<Proc-Type> contains the version and the
423protection on the encapsulated data. The line beginning I<DEK-Info>
424contains two comma separated values: the encryption algorithm name as
425used by EVP_get_cipherbyname() and an initialization vector used by the
426cipher encoded as a set of hexadecimal digits. After those two lines is
427the base64-encoded encrypted data.
06623ff0 428
a0474357 429The encryption key is derived using EVP_BytesToKey(). The cipher's
bbecf04e 430initialization vector is passed to EVP_BytesToKey() as the I<salt>
a0474357
RS
431parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
432(regardless of the size of the initialization vector). The user's
bbecf04e 433password is passed to EVP_BytesToKey() using the I<data> and I<datal>
a0474357
RS
434parameters. Finally, the library uses an iteration count of 1 for
435EVP_BytesToKey().
06623ff0 436
bbecf04e
RL
437The I<key> derived by EVP_BytesToKey() along with the original initialization
438vector is then used to decrypt the encrypted data. The I<iv> produced by
a0474357
RS
439EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
440the function.
441
442The pseudo code to derive the key would look similar to:
443
444 EVP_CIPHER* cipher = EVP_des_ede3_cbc();
445 EVP_MD* md = EVP_md5();
446
ed576acd
TM
447 unsigned int nkey = EVP_CIPHER_get_key_length(cipher);
448 unsigned int niv = EVP_CIPHER_get_iv_length(cipher);
a0474357
RS
449 unsigned char key[nkey];
450 unsigned char iv[niv];
451
452 memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
453 rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
2947af32 454 if (rc != nkey)
a0474357 455 /* Error */
a0474357
RS
456
457 /* On success, use key and iv to initialize the cipher */
06623ff0 458
a29d78e9
DSH
459=head1 BUGS
460
461The PEM read routines in some versions of OpenSSL will not correctly reuse
8c1cbc72 462an existing structure. Therefore, the following:
a29d78e9 463
e88c5777 464 PEM_read_bio_X509(bp, &x, 0, NULL);
a29d78e9 465
bbecf04e 466where I<x> already contains a valid certificate, may not work, whereas:
a29d78e9
DSH
467
468 X509_free(x);
e88c5777 469 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
a29d78e9 470
e48fe798
MC
471is guaranteed to work. It is always acceptable for I<x> to contain a newly
472allocated, empty B<X509> object (for example allocated via L<X509_new_ex(3)>).
a29d78e9 473
1f13ad31 474=head1 RETURN VALUES
a29d78e9
DSH
475
476The read routines return either a pointer to the structure read or NULL
9449e385 477if an error occurred.
a29d78e9
DSH
478
479The write routines return 1 for success or 0 for failure.
d48e78f0 480
4564e77a
PY
481=head1 EXAMPLES
482
483Although the PEM routines take several arguments in almost all applications
484most of them are set to 0 or NULL.
485
6725682d
SL
486To read a certificate with a library context in PEM format from a BIO:
487
d8652be0 488 X509 *x = X509_new_ex(libctx, NULL);
6725682d
SL
489
490 if (x == NULL)
491 /* Error */
492
493 if (PEM_read_bio_X509(bp, &x, 0, NULL) == NULL)
494 /* Error */
495
4564e77a
PY
496Read a certificate in PEM format from a BIO:
497
498 X509 *x;
499
500 x = PEM_read_bio_X509(bp, NULL, 0, NULL);
501 if (x == NULL)
502 /* Error */
503
504Alternative method:
505
506 X509 *x = NULL;
507
508 if (!PEM_read_bio_X509(bp, &x, 0, NULL))
509 /* Error */
510
511Write a certificate to a BIO:
512
513 if (!PEM_write_bio_X509(bp, x))
514 /* Error */
515
516Write a private key (using traditional format) to a BIO using
517triple DES encryption, the pass phrase is prompted for:
518
519 if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
520 /* Error */
521
522Write a private key (using PKCS#8 format) to a BIO using triple
523DES encryption, using the pass phrase "hello":
524
525 if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
526 NULL, 0, 0, "hello"))
527 /* Error */
528
529Read a private key from a BIO using a pass phrase callback:
530
531 key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
532 if (key == NULL)
533 /* Error */
534
535Skeleton pass phrase callback:
536
537 int pass_cb(char *buf, int size, int rwflag, void *u)
538 {
539
540 /* We'd probably do something else if 'rwflag' is 1 */
541 printf("Enter pass phrase for \"%s\"\n", (char *)u);
542
543 /* get pass phrase, length 'len' into 'tmp' */
544 char *tmp = "hello";
545 if (tmp == NULL) /* An error occurred */
546 return -1;
547
548 size_t len = strlen(tmp);
549
550 if (len > size)
551 len = size;
552 memcpy(buf, tmp, len);
553 return len;
554 }
555
b5c4bbbe
JL
556=head1 SEE ALSO
557
558L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>,
559L<passphrase-encoding(7)>
560
a0474357
RS
561=head1 HISTORY
562
563The old Netscape certificate sequences were no longer documented
a95d7574 564in OpenSSL 1.1.0; applications should use the PKCS7 standard instead
a0474357
RS
565as they will be formally deprecated in a future releases.
566
6e5ccd58
RL
567PEM_read_bio_PrivateKey_ex(), PEM_read_PrivateKey_ex(),
568PEM_read_bio_PUBKEY_ex(), PEM_read_PUBKEY_ex() and
569PEM_read_bio_Parameters_ex() were introduced in OpenSSL 3.0.
137b274a 570
e52b4215
SL
571The functions PEM_read_bio_RSAPrivateKey(), PEM_read_RSAPrivateKey(),
572PEM_write_bio_RSAPrivateKey(), PEM_write_RSAPrivateKey(),
573PEM_read_bio_RSAPublicKey(), PEM_read_RSAPublicKey(),
574PEM_write_bio_RSAPublicKey(), PEM_write_RSAPublicKey(),
575PEM_read_bio_RSA_PUBKEY(), PEM_read_RSA_PUBKEY(),
576PEM_write_bio_RSA_PUBKEY(), PEM_write_RSA_PUBKEY(),
577PEM_read_bio_DSAPrivateKey(), PEM_read_DSAPrivateKey(),
578PEM_write_bio_DSAPrivateKey(), PEM_write_DSAPrivateKey(),
579PEM_read_bio_DSA_PUBKEY(), PEM_read_DSA_PUBKEY(),
580PEM_write_bio_DSA_PUBKEY(), PEM_write_DSA_PUBKEY();
581PEM_read_bio_DSAparams(), PEM_read_DSAparams(),
582PEM_write_bio_DSAparams(), PEM_write_DSAparams(),
583PEM_read_bio_DHparams(), PEM_read_DHparams(),
584PEM_write_bio_DHparams() and PEM_write_DHparams() were deprecated in 3.0.
585
586
e2f92610
RS
587=head1 COPYRIGHT
588
fecb3aae 589Copyright 2001-2022 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 590
4746f25a 591Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
592this file except in compliance with the License. You can obtain a copy
593in the file LICENSE in the source distribution or at
594L<https://www.openssl.org/source/license.html>.
595
596=cut