]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/PEM_read_bio_PrivateKey.pod
Document the new libctx aware private key functions
[thirdparty/openssl.git] / doc / man3 / PEM_read_bio_PrivateKey.pod
CommitLineData
a29d78e9
DSH
1=pod
2
3=head1 NAME
4
91da5e77 5pem_password_cb,
137b274a
MC
6PEM_read_bio_PrivateKey_ex, PEM_read_bio_PrivateKey, PEM_read_PrivateKey_ex,
7PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
05dba815
DSH
8PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey,
9PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
f097f81c
DSH
10PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
11PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY,
12PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
13PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
14PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
15PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
16PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
17PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
18PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
9a6abb95
RL
19PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters, PEM_write_bio_Parameters,
20PEM_read_bio_DSAparams, PEM_read_DSAparams,
f097f81c
DSH
21PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
22PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
23PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
24PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
25PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
26PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
27PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
28PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
a0474357 29PEM_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
166The PEM functions read or write structures in PEM format. In
167this sense PEM format is simply base64 encoded data surrounded
168by header lines.
169
170For more details about the meaning of arguments see the
171B<PEM FUNCTION ARGUMENTS> section.
172
173Each operation has four functions associated with it. For
bbecf04e
RL
174brevity the term "B<I<TYPE>> functions" will be used below to collectively
175refer to the B<PEM_read_bio_I<TYPE>>(), B<PEM_read_I<TYPE>>(),
176B<PEM_write_bio_I<TYPE>>(), and B<PEM_write_I<TYPE>>() functions.
a29d78e9 177
137b274a
MC
178Some operations have additional variants that take a library context I<libctx>
179and a property query string I<propq>.
180
05dba815
DSH
181The B<PrivateKey> functions read or write a private key in PEM format using an
182EVP_PKEY structure. The write routines use PKCS#8 private key format and are
183equivalent to PEM_write_bio_PKCS8PrivateKey().The read functions transparently
184handle traditional and PKCS#8 format encrypted and unencrypted keys.
a29d78e9 185
a95d7574
RS
186PEM_write_bio_PrivateKey_traditional() writes out a private key in the
187"traditional" format with a simple private key marker and should only
188be used for compatibility with legacy programs.
05dba815
DSH
189
190PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey() write a private
191key in an EVP_PKEY structure in PKCS#8 EncryptedPrivateKeyInfo format using
bbecf04e 192PKCS#5 v2.0 password based encryption algorithms. The I<cipher> argument
05dba815
DSH
193specifies the encryption algorithm to use: unlike some other PEM routines the
194encryption is applied at the PKCS#8 level and not in the PEM headers. If
bbecf04e 195I<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
05dba815 196structure is used instead.
a29d78e9
DSH
197
198PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
199also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
200it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
bbecf04e 201to use is specified in the I<nid> parameter and should be the NID of the
a29d78e9
DSH
202corresponding OBJECT IDENTIFIER (see NOTES section).
203
204The B<PUBKEY> functions process a public key using an EVP_PKEY
205structure. The public key is encoded as a SubjectPublicKeyInfo
206structure.
207
208The B<RSAPrivateKey> functions process an RSA private key using an
05dba815
DSH
209RSA structure. The write routines uses traditional format. The read
210routines handles the same formats as the B<PrivateKey>
a29d78e9
DSH
211functions but an error occurs if the private key is not RSA.
212
213The B<RSAPublicKey> functions process an RSA public key using an
214RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
215structure.
216
217The B<RSA_PUBKEY> functions also process an RSA public key using
218an RSA structure. However the public key is encoded using a
219SubjectPublicKeyInfo structure and an error occurs if the public
220key is not RSA.
221
222The B<DSAPrivateKey> functions process a DSA private key using a
05dba815
DSH
223DSA structure. The write routines uses traditional format. The read
224routines handles the same formats as the B<PrivateKey>
a29d78e9
DSH
225functions but an error occurs if the private key is not DSA.
226
227The B<DSA_PUBKEY> functions process a DSA public key using
228a DSA structure. The public key is encoded using a
229SubjectPublicKeyInfo structure and an error occurs if the public
230key is not DSA.
231
9a6abb95
RL
232The B<Parameters> functions read or write key parameters in PEM format using
233an EVP_PKEY structure. The encoding depends on the type of key; for DSA key
234parameters, it will be a Dss-Parms structure as defined in RFC2459, and for DH
235key parameters, it will be a PKCS#3 DHparameter structure. I<These functions
236only exist for the B<BIO> type>.
237
a29d78e9 238The B<DSAparams> functions process DSA parameters using a DSA
f097f81c
DSH
239structure. The parameters are encoded using a Dss-Parms structure
240as defined in RFC2459.
a29d78e9
DSH
241
242The B<DHparams> functions process DH parameters using a DH
243structure. The parameters are encoded using a PKCS#3 DHparameter
244structure.
245
246The B<X509> functions process an X509 certificate using an X509
247structure. They will also process a trusted X509 certificate but
248any trust settings are discarded.
249
250The B<X509_AUX> functions process a trusted X509 certificate using
1bc74519 251an X509 structure.
a29d78e9
DSH
252
253The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
254certificate request using an X509_REQ structure. The B<X509_REQ>
255write functions use B<CERTIFICATE REQUEST> in the header whereas
256the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
257(as required by some CAs). The B<X509_REQ> read functions will
258handle either form so there are no B<X509_REQ_NEW> read functions.
259
260The B<X509_CRL> functions process an X509 CRL using an X509_CRL
261structure.
262
263The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
264structure.
265
a29d78e9
DSH
266=head1 PEM FUNCTION ARGUMENTS
267
268The PEM functions have many common arguments.
269
bbecf04e 270The I<bp> BIO parameter (if present) specifies the BIO to read from
a29d78e9
DSH
271or write to.
272
bbecf04e 273The I<fp> FILE parameter (if present) specifies the FILE pointer to
a29d78e9
DSH
274read from or write to.
275
bbecf04e
RL
276The PEM read functions all take an argument I<B<TYPE> **x> and return
277a I<B<TYPE> *> pointer. Where I<B<TYPE>> is whatever structure the function
278uses. If I<x> is NULL then the parameter is ignored. If I<x> is not
279NULL but I<*x> is NULL then the structure returned will be written
280to I<*x>. If neither I<x> nor I<*x> is NULL then an attempt is made
281to reuse the structure at I<*x> (but see BUGS and EXAMPLES sections).
282Irrespective of the value of I<x> a pointer to the structure is always
a29d78e9
DSH
283returned (or NULL if an error occurred).
284
bbecf04e 285The PEM functions which write private keys take an I<enc> parameter
a29d78e9
DSH
286which specifies the encryption algorithm to use, encryption is done
287at the PEM level. If this parameter is set to NULL then the private
288key is written in unencrypted form.
289
bbecf04e 290The I<cb> argument is the callback to use when querying for the pass
a29d78e9
DSH
291phrase used for encrypted PEM structures (normally only private keys).
292
bbecf04e
RL
293For the PEM write routines if the I<kstr> parameter is not NULL then
294I<klen> bytes at I<kstr> are used as the passphrase and I<cb> is
a29d78e9
DSH
295ignored.
296
bbecf04e
RL
297If the I<cb> parameters is set to NULL and the I<u> parameter is not
298NULL then the I<u> parameter is interpreted as a null terminated string
299to use as the passphrase. If both I<cb> and I<u> are NULL then the
a29d78e9
DSH
300default callback routine is used which will typically prompt for the
301passphrase on the current terminal with echoing turned off.
302
303The default passphrase callback is sometimes inappropriate (for example
304in a GUI application) so an alternative can be supplied. The callback
305routine has the following form:
306
307 int cb(char *buf, int size, int rwflag, void *u);
308
bbecf04e
RL
309I<buf> is the buffer to write the passphrase to. I<size> is the maximum
310length of the passphrase (i.e. the size of buf). I<rwflag> is a flag
a29d78e9
DSH
311which is set to 0 when reading and 1 when writing. A typical routine
312will ask the user to verify the passphrase (for example by prompting
bbecf04e
RL
313for it twice) if I<rwflag> is 1. The I<u> parameter has the same
314value as the I<u> parameter passed to the PEM routine. It allows
a29d78e9
DSH
315arbitrary data to be passed to the callback by the application
316(for example a window handle in a GUI application). The callback
bbecf04e 317I<must> return the number of characters in the passphrase or -1 if
a29d78e9
DSH
318an error occurred.
319
137b274a
MC
320Some implementations may need to use cryptographic algorithms during their
321operation. If this is the case and I<libctx> and I<propq> parameters have been
322passed then any algorithm fetches will use that library context and property
323query string. Otherwise the default library context and property query string
324will be used.
325
a29d78e9
DSH
326=head1 NOTES
327
328The old B<PrivateKey> write routines are retained for compatibility.
329New applications should write private keys using the
330PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
331because they are more secure (they use an iteration count of 2048 whereas
332the traditional routines use a count of 1) unless compatibility with older
333versions of OpenSSL is important.
334
335The B<PrivateKey> read routines can be used in all applications because
336they handle all formats transparently.
337
338A frequent cause of problems is attempting to use the PEM routines like
339this:
340
341 X509 *x;
e9b77246 342
a29d78e9
DSH
343 PEM_read_bio_X509(bp, &x, 0, NULL);
344
bbecf04e 345this is a bug because an attempt will be made to reuse the data at I<x>
a29d78e9
DSH
346which is an uninitialised pointer.
347
84814344
RL
348These functions make no assumption regarding the pass phrase received from the
349password callback.
350It will simply be treated as a byte sequence.
351
06623ff0
DSH
352=head1 PEM ENCRYPTION FORMAT
353
a0474357 354These old B<PrivateKey> routines use a non standard technique for encryption.
06623ff0 355
1bc74519 356The 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
365The line beginning with I<Proc-Type> contains the version and the
366protection on the encapsulated data. The line beginning I<DEK-Info>
367contains two comma separated values: the encryption algorithm name as
368used by EVP_get_cipherbyname() and an initialization vector used by the
369cipher encoded as a set of hexadecimal digits. After those two lines is
370the base64-encoded encrypted data.
06623ff0 371
a0474357 372The encryption key is derived using EVP_BytesToKey(). The cipher's
bbecf04e 373initialization vector is passed to EVP_BytesToKey() as the I<salt>
a0474357
RS
374parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
375(regardless of the size of the initialization vector). The user's
bbecf04e 376password is passed to EVP_BytesToKey() using the I<data> and I<datal>
a0474357
RS
377parameters. Finally, the library uses an iteration count of 1 for
378EVP_BytesToKey().
06623ff0 379
bbecf04e
RL
380The I<key> derived by EVP_BytesToKey() along with the original initialization
381vector is then used to decrypt the encrypted data. The I<iv> produced by
a0474357
RS
382EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
383the function.
384
385The 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
404The PEM read routines in some versions of OpenSSL will not correctly reuse
405an existing structure. Therefore the following:
406
e88c5777 407 PEM_read_bio_X509(bp, &x, 0, NULL);
a29d78e9 408
bbecf04e 409where 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
414is guaranteed to work.
415
1f13ad31 416=head1 RETURN VALUES
a29d78e9
DSH
417
418The read routines return either a pointer to the structure read or NULL
9449e385 419if an error occurred.
a29d78e9
DSH
420
421The write routines return 1 for success or 0 for failure.
d48e78f0 422
4564e77a
PY
423=head1 EXAMPLES
424
425Although the PEM routines take several arguments in almost all applications
426most of them are set to 0 or NULL.
427
428Read 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
436Alternative method:
437
438 X509 *x = NULL;
439
440 if (!PEM_read_bio_X509(bp, &x, 0, NULL))
441 /* Error */
442
443Write a certificate to a BIO:
444
445 if (!PEM_write_bio_X509(bp, x))
446 /* Error */
447
448Write a private key (using traditional format) to a BIO using
449triple 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
454Write a private key (using PKCS#8 format) to a BIO using triple
455DES 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
461Read 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
467Skeleton 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
490L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>,
491L<passphrase-encoding(7)>
492
a0474357
RS
493=head1 HISTORY
494
495The old Netscape certificate sequences were no longer documented
a95d7574 496in OpenSSL 1.1.0; applications should use the PKCS7 standard instead
a0474357
RS
497as they will be formally deprecated in a future releases.
498
137b274a
MC
499PEM_read_bio_PrivateKey_ex() and PEM_read_PrivateKey_ex() were introduced in
500OpenSSL 3.0.
501
e2f92610
RS
502=head1 COPYRIGHT
503
b5c4bbbe 504Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 505
4746f25a 506Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
507this file except in compliance with the License. You can obtain a copy
508in the file LICENSE in the source distribution or at
509L<https://www.openssl.org/source/license.html>.
510
511=cut