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