doc/man3: reformat the function prototypes in the synopses
[openssl.git] / doc / man3 / PEM_read_bio_PrivateKey.pod
1 =pod
2
3 =head1 NAME
4
5 pem_password_cb,
6 PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
7 PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey,
8 PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
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,
27 PEM_write_bio_PKCS7, PEM_write_PKCS7 - PEM routines
28
29 =head1 SYNOPSIS
30
31  #include <openssl/pem.h>
32
33  typedef int (*pem_password_cb)(char *buf, int size, int rwflag, void *u);
34
35  EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
36                                    pem_password_cb *cb, void *u);
37  EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
38                                pem_password_cb *cb, void *u);
39  int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
40                               unsigned char *kstr, int klen,
41                               pem_password_cb *cb, void *u);
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);
46  int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
47                           unsigned char *kstr, int klen,
48                           pem_password_cb *cb, void *u);
49
50  int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
51                                    char *kstr, int klen,
52                                    pem_password_cb *cb, void *u);
53  int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
54                                char *kstr, int klen,
55                                pem_password_cb *cb, void *u);
56  int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
57                                        char *kstr, int klen,
58                                        pem_password_cb *cb, void *u);
59  int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
60                                    char *kstr, int klen,
61                                    pem_password_cb *cb, void *u);
62
63  EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
64                                pem_password_cb *cb, void *u);
65  EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
66                            pem_password_cb *cb, void *u);
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,
71                                  pem_password_cb *cb, void *u);
72  RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
73                              pem_password_cb *cb, void *u);
74  int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
75                                  unsigned char *kstr, int klen,
76                                  pem_password_cb *cb, void *u);
77  int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
78                              unsigned char *kstr, int klen,
79                              pem_password_cb *cb, void *u);
80
81  RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
82                                 pem_password_cb *cb, void *u);
83  RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
84                             pem_password_cb *cb, void *u);
85  int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
86  int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
87
88  RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
89                               pem_password_cb *cb, void *u);
90  RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
91                           pem_password_cb *cb, void *u);
92  int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
93  int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
94
95  DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
96                                  pem_password_cb *cb, void *u);
97  DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
98                              pem_password_cb *cb, void *u);
99  int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
100                                  unsigned char *kstr, int klen,
101                                  pem_password_cb *cb, void *u);
102  int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
103                              unsigned char *kstr, int klen,
104                              pem_password_cb *cb, void *u);
105
106  DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
107                               pem_password_cb *cb, void *u);
108  DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
109                           pem_password_cb *cb, void *u);
110  int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
111  int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
112
113  DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
114  DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
115  int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
116  int PEM_write_DSAparams(FILE *fp, DSA *x);
117
118  DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
119  DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
120  int PEM_write_bio_DHparams(BIO *bp, DH *x);
121  int PEM_write_DHparams(FILE *fp, DH *x);
122
123  X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
124  X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
125  int PEM_write_bio_X509(BIO *bp, X509 *x);
126  int PEM_write_X509(FILE *fp, X509 *x);
127
128  X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
129  X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
130  int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
131  int PEM_write_X509_AUX(FILE *fp, X509 *x);
132
133  X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
134                                  pem_password_cb *cb, void *u);
135  X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
136                              pem_password_cb *cb, void *u);
137  int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
138  int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
139  int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
140  int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
141
142  X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
143                                  pem_password_cb *cb, void *u);
144  X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
145                              pem_password_cb *cb, void *u);
146  int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
147  int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
148
149  PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
150  PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
151  int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
152  int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
153
154 =head1 DESCRIPTION
155
156 The PEM functions read or write structures in PEM format. In
157 this sense PEM format is simply base64 encoded data surrounded
158 by header lines.
159
160 For more details about the meaning of arguments see the
161 B<PEM FUNCTION ARGUMENTS> section.
162
163 Each operation has four functions associated with it. For
164 clarity the term "B<foobar> functions" will be used to collectively
165 refer to the PEM_read_bio_foobar(), PEM_read_foobar(),
166 PEM_write_bio_foobar() and PEM_write_foobar() functions.
167
168 The B<PrivateKey> functions read or write a private key in PEM format using an
169 EVP_PKEY structure. The write routines use PKCS#8 private key format and are
170 equivalent to PEM_write_bio_PKCS8PrivateKey().The read functions transparently
171 handle traditional and PKCS#8 format encrypted and unencrypted keys.
172
173 PEM_write_bio_PrivateKey_traditional() writes out a private key in legacy
174 "traditional" format.
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
178 PKCS#5 v2.0 password based encryption algorithms. The B<cipher> argument
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
181 B<cipher> is NULL then no encryption is used and a PKCS#8 PrivateKeyInfo
182 structure is used instead.
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
187 to use is specified in the B<nid> parameter and should be the NID of the
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
195 RSA structure. The write routines uses traditional format. The read
196 routines handles the same formats as the B<PrivateKey>
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
209 DSA structure. The write routines uses traditional format. The read
210 routines handles the same formats as the B<PrivateKey>
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
219 structure. The parameters are encoded using a Dss-Parms structure
220 as defined in RFC2459.
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
231 an X509 structure.
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
246 =head1 PEM FUNCTION ARGUMENTS
247
248 The PEM functions have many common arguments.
249
250 The B<bp> BIO parameter (if present) specifies the BIO to read from
251 or write to.
252
253 The B<fp> FILE parameter (if present) specifies the FILE pointer to
254 read from or write to.
255
256 The PEM read functions all take an argument B<TYPE **x> and return
257 a B<TYPE *> pointer. Where B<TYPE> is whatever structure the function
258 uses. If B<x> is NULL then the parameter is ignored. If B<x> is not
259 NULL but B<*x> is NULL then the structure returned will be written
260 to B<*x>. If neither B<x> nor B<*x> is NULL then an attempt is made
261 to reuse the structure at B<*x> (but see BUGS and EXAMPLES sections).
262 Irrespective of the value of B<x> a pointer to the structure is always
263 returned (or NULL if an error occurred).
264
265 The PEM functions which write private keys take an B<enc> parameter
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
270 The B<cb> argument is the callback to use when querying for the pass
271 phrase used for encrypted PEM structures (normally only private keys).
272
273 For the PEM write routines if the B<kstr> parameter is not NULL then
274 B<klen> bytes at B<kstr> are used as the passphrase and B<cb> is
275 ignored.
276
277 If the B<cb> parameters is set to NULL and the B<u> parameter is not
278 NULL then the B<u> parameter is interpreted as a null terminated string
279 to use as the passphrase. If both B<cb> and B<u> are NULL then the
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
289 B<buf> is the buffer to write the passphrase to. B<size> is the maximum
290 length of the passphrase (i.e. the size of buf). B<rwflag> is a flag
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
293 for it twice) if B<rwflag> is 1. The B<u> parameter has the same
294 value as the B<u> parameter passed to the PEM routine. It allows
295 arbitrary data to be passed to the callback by the application
296 (for example a window handle in a GUI application). The callback
297 B<must> return the number of characters in the passphrase or 0 if
298 an error occurred.
299
300 =head1 EXAMPLES
301
302 Although the PEM routines take several arguments in almost all applications
303 most of them are set to 0 or NULL.
304
305 Read a certificate in PEM format from a BIO:
306
307  X509 *x;
308
309  x = PEM_read_bio_X509(bp, NULL, 0, NULL);
310  if (x == NULL)
311      /* Error */
312
313 Alternative method:
314
315  X509 *x = NULL;
316
317  if (!PEM_read_bio_X509(bp, &x, 0, NULL))
318      /* Error */
319
320 Write a certificate to a BIO:
321
322  if (!PEM_write_bio_X509(bp, x))
323      /* Error */
324
325 Write a private key (using traditional format) to a BIO using
326 triple DES encryption, the pass phrase is prompted for:
327
328  if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
329      /* Error */
330
331 Write a private key (using PKCS#8 format) to a BIO using triple
332 DES encryption, using the pass phrase "hello":
333
334  if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
335                                     NULL, 0, 0, "hello"))
336      /* Error */
337
338 Read a private key from a BIO using a pass phrase callback:
339
340  key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
341  if (key == NULL)
342      /* Error */
343
344 Skeleton pass phrase callback:
345
346  int pass_cb(char *buf, int size, int rwflag, void *u)
347  {
348      int len;
349      char *tmp;
350
351      /* We'd probably do something else if 'rwflag' is 1 */
352      printf("Enter pass phrase for \"%s\"\n", (char *)u);
353
354      /* get pass phrase, length 'len' into 'tmp' */
355      tmp = "hello";
356      len = strlen(tmp);
357      if (len <= 0)
358          return 0;
359
360      if (len > size)
361          len = size;
362      memcpy(buf, tmp, len);
363      return len;
364  }
365
366 =head1 NOTES
367
368 The old B<PrivateKey> write routines are retained for compatibility.
369 New applications should write private keys using the
370 PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
371 because they are more secure (they use an iteration count of 2048 whereas
372 the traditional routines use a count of 1) unless compatibility with older
373 versions of OpenSSL is important.
374
375 The B<PrivateKey> read routines can be used in all applications because
376 they handle all formats transparently.
377
378 A frequent cause of problems is attempting to use the PEM routines like
379 this:
380
381  X509 *x;
382
383  PEM_read_bio_X509(bp, &x, 0, NULL);
384
385 this is a bug because an attempt will be made to reuse the data at B<x>
386 which is an uninitialised pointer.
387
388 =head1 PEM ENCRYPTION FORMAT
389
390 These old B<PrivateKey> routines use a non standard technique for encryption.
391
392 The private key (or other data) takes the following form:
393
394  -----BEGIN RSA PRIVATE KEY-----
395  Proc-Type: 4,ENCRYPTED
396  DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
397
398  ...base64 encoded data...
399  -----END RSA PRIVATE KEY-----
400
401 The line beginning with I<Proc-Type> contains the version and the
402 protection on the encapsulated data. The line beginning I<DEK-Info>
403 contains two comma separated values: the encryption algorithm name as
404 used by EVP_get_cipherbyname() and an initialization vector used by the
405 cipher encoded as a set of hexadecimal digits. After those two lines is
406 the base64-encoded encrypted data.
407
408 The encryption key is derived using EVP_BytesToKey(). The cipher's
409 initialization vector is passed to EVP_BytesToKey() as the B<salt>
410 parameter. Internally, B<PKCS5_SALT_LEN> bytes of the salt are used
411 (regardless of the size of the initialization vector). The user's
412 password is passed to EVP_BytesToKey() using the B<data> and B<datal>
413 parameters. Finally, the library uses an iteration count of 1 for
414 EVP_BytesToKey().
415
416 The B<key> derived by EVP_BytesToKey() along with the original initialization
417 vector is then used to decrypt the encrypted data. The B<iv> produced by
418 EVP_BytesToKey() is not utilized or needed, and NULL should be passed to
419 the function.
420
421 The pseudo code to derive the key would look similar to:
422
423  EVP_CIPHER* cipher = EVP_des_ede3_cbc();
424  EVP_MD* md = EVP_md5();
425
426  unsigned int nkey = EVP_CIPHER_key_length(cipher);
427  unsigned int niv = EVP_CIPHER_iv_length(cipher);
428  unsigned char key[nkey];
429  unsigned char iv[niv];
430
431  memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
432  rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
433  if (rc != nkey)
434      /* Error */
435
436  /* On success, use key and iv to initialize the cipher */
437
438 =head1 BUGS
439
440 The PEM read routines in some versions of OpenSSL will not correctly reuse
441 an existing structure. Therefore the following:
442
443  PEM_read_bio_X509(bp, &x, 0, NULL);
444
445 where B<x> already contains a valid certificate, may not work, whereas:
446
447  X509_free(x);
448  x = PEM_read_bio_X509(bp, NULL, 0, NULL);
449
450 is guaranteed to work.
451
452 =head1 RETURN CODES
453
454 The read routines return either a pointer to the structure read or NULL
455 if an error occurred.
456
457 The write routines return 1 for success or 0 for failure.
458
459 =head1 HISTORY
460
461 The old Netscape certificate sequences were no longer documented
462 in OpenSSL 1.1; applications should use the PKCS7 standard instead
463 as they will be formally deprecated in a future releases.
464
465 =head1 SEE ALSO
466
467 L<EVP_EncryptInit(3)>, L<EVP_BytesToKey(3)>
468
469 =head1 COPYRIGHT
470
471 Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
472
473 Licensed under the OpenSSL license (the "License").  You may not use
474 this file except in compliance with the License.  You can obtain a copy
475 in the file LICENSE in the source distribution or at
476 L<https://www.openssl.org/source/license.html>.
477
478 =cut