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