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