OpenSSL 1.0.2f
[tomato.git] / release / src / router / openssl / doc / crypto / pem.pod
blob763eb6f533925e71580ea2568b27cc9f7107aa55
1 =pod
3 =head1 NAME
5 PEM, PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey,
6 PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey,
7 PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid,
8 PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY,
9 PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey,
10 PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey,
11 PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey,
12 PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY,
13 PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey,
14 PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey,
15 PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY,
16 PEM_write_DSA_PUBKEY, PEM_read_bio_DSAparams, PEM_read_DSAparams,
17 PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams,
18 PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams,
19 PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509,
20 PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX,
21 PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ,
22 PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW,
23 PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL,
24 PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7,
25 PEM_write_bio_PKCS7, PEM_write_PKCS7, PEM_read_bio_NETSCAPE_CERT_SEQUENCE,
26 PEM_read_NETSCAPE_CERT_SEQUENCE, PEM_write_bio_NETSCAPE_CERT_SEQUENCE,
27 PEM_write_NETSCAPE_CERT_SEQUENCE - PEM routines
29 =head1 SYNOPSIS
31  #include <openssl/pem.h>
33  EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
34                                         pem_password_cb *cb, void *u);
36  EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
37                                         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);
43  int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
44                                         unsigned char *kstr, int klen,
45                                         pem_password_cb *cb, void *u);
47  int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
48                                         char *kstr, int klen,
49                                         pem_password_cb *cb, void *u);
51  int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
52                                         char *kstr, int klen,
53                                         pem_password_cb *cb, void *u);
55  int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
56                                         char *kstr, int klen,
57                                         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);
63  EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
64                                         pem_password_cb *cb, void *u);
66  EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
67                                         pem_password_cb *cb, void *u);
69  int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
70  int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
72  RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
73                                         pem_password_cb *cb, void *u);
75  RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
76                                         pem_password_cb *cb, void *u);
78  int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
79                                         unsigned char *kstr, int klen,
80                                         pem_password_cb *cb, void *u);
82  int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
83                                         unsigned char *kstr, int klen,
84                                         pem_password_cb *cb, void *u);
86  RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
87                                         pem_password_cb *cb, void *u);
89  RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
90                                         pem_password_cb *cb, void *u);
92  int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
94  int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
96  RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
97                                         pem_password_cb *cb, void *u);
99  RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
100                                         pem_password_cb *cb, void *u);
102  int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
104  int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
106  DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
107                                         pem_password_cb *cb, void *u);
109  DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
110                                         pem_password_cb *cb, void *u);
112  int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
113                                         unsigned char *kstr, int klen,
114                                         pem_password_cb *cb, void *u);
116  int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
117                                         unsigned char *kstr, int klen,
118                                         pem_password_cb *cb, void *u);
120  DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
121                                         pem_password_cb *cb, void *u);
123  DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
124                                         pem_password_cb *cb, void *u);
126  int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
128  int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
130  DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
132  DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
134  int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
136  int PEM_write_DSAparams(FILE *fp, DSA *x);
138  DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
140  DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
142  int PEM_write_bio_DHparams(BIO *bp, DH *x);
144  int PEM_write_DHparams(FILE *fp, DH *x);
146  X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
148  X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
150  int PEM_write_bio_X509(BIO *bp, X509 *x);
152  int PEM_write_X509(FILE *fp, X509 *x);
154  X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
156  X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
158  int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
160  int PEM_write_X509_AUX(FILE *fp, X509 *x);
162  X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
163                                         pem_password_cb *cb, void *u);
165  X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
166                                         pem_password_cb *cb, void *u);
168  int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
170  int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
172  int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
174  int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
176  X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
177                                         pem_password_cb *cb, void *u);
178  X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
179                                         pem_password_cb *cb, void *u);
180  int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
181  int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
183  PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
185  PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
187  int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
189  int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
191  NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
192                                                 NETSCAPE_CERT_SEQUENCE **x,
193                                                 pem_password_cb *cb, void *u);
195  NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
196                                                 NETSCAPE_CERT_SEQUENCE **x,
197                                                 pem_password_cb *cb, void *u);
199  int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x);
201  int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x);
203 =head1 DESCRIPTION
205 The PEM functions read or write structures in PEM format. In
206 this sense PEM format is simply base64 encoded data surrounded
207 by header lines.
209 For more details about the meaning of arguments see the
210 B<PEM FUNCTION ARGUMENTS> section.
212 Each operation has four functions associated with it. For
213 clarity the term "B<foobar> functions" will be used to collectively
214 refer to the PEM_read_bio_foobar(), PEM_read_foobar(),
215 PEM_write_bio_foobar() and PEM_write_foobar() functions.
217 The B<PrivateKey> functions read or write a private key in
218 PEM format using an EVP_PKEY structure. The write routines use
219 "traditional" private key format and can handle both RSA and DSA
220 private keys. The read functions can additionally transparently
221 handle PKCS#8 format encrypted and unencrypted keys too.
223 PEM_write_bio_PKCS8PrivateKey() and PEM_write_PKCS8PrivateKey()
224 write a private key in an EVP_PKEY structure in PKCS#8
225 EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption
226 algorithms. The B<cipher> argument specifies the encryption algorithm to
227 use: unlike all other PEM routines the encryption is applied at the
228 PKCS#8 level and not in the PEM headers. If B<cipher> is NULL then no
229 encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.
231 PEM_write_bio_PKCS8PrivateKey_nid() and PEM_write_PKCS8PrivateKey_nid()
232 also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
233 it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
234 to use is specified in the B<nid> parameter and should be the NID of the
235 corresponding OBJECT IDENTIFIER (see NOTES section).
237 The B<PUBKEY> functions process a public key using an EVP_PKEY
238 structure. The public key is encoded as a SubjectPublicKeyInfo
239 structure.
241 The B<RSAPrivateKey> functions process an RSA private key using an
242 RSA structure. It handles the same formats as the B<PrivateKey>
243 functions but an error occurs if the private key is not RSA.
245 The B<RSAPublicKey> functions process an RSA public key using an
246 RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
247 structure.
249 The B<RSA_PUBKEY> functions also process an RSA public key using
250 an RSA structure. However the public key is encoded using a
251 SubjectPublicKeyInfo structure and an error occurs if the public
252 key is not RSA.
254 The B<DSAPrivateKey> functions process a DSA private key using a
255 DSA structure. It handles the same formats as the B<PrivateKey>
256 functions but an error occurs if the private key is not DSA.
258 The B<DSA_PUBKEY> functions process a DSA public key using
259 a DSA structure. The public key is encoded using a
260 SubjectPublicKeyInfo structure and an error occurs if the public
261 key is not DSA.
263 The B<DSAparams> functions process DSA parameters using a DSA
264 structure. The parameters are encoded using a Dss-Parms structure
265 as defined in RFC2459.
267 The B<DHparams> functions process DH parameters using a DH
268 structure. The parameters are encoded using a PKCS#3 DHparameter
269 structure.
271 The B<X509> functions process an X509 certificate using an X509
272 structure. They will also process a trusted X509 certificate but
273 any trust settings are discarded.
275 The B<X509_AUX> functions process a trusted X509 certificate using
276 an X509 structure. 
278 The B<X509_REQ> and B<X509_REQ_NEW> functions process a PKCS#10
279 certificate request using an X509_REQ structure. The B<X509_REQ>
280 write functions use B<CERTIFICATE REQUEST> in the header whereas
281 the B<X509_REQ_NEW> functions use B<NEW CERTIFICATE REQUEST>
282 (as required by some CAs). The B<X509_REQ> read functions will
283 handle either form so there are no B<X509_REQ_NEW> read functions.
285 The B<X509_CRL> functions process an X509 CRL using an X509_CRL
286 structure.
288 The B<PKCS7> functions process a PKCS#7 ContentInfo using a PKCS7
289 structure.
291 The B<NETSCAPE_CERT_SEQUENCE> functions process a Netscape Certificate
292 Sequence using a NETSCAPE_CERT_SEQUENCE structure.
294 =head1 PEM FUNCTION ARGUMENTS
296 The PEM functions have many common arguments.
298 The B<bp> BIO parameter (if present) specifies the BIO to read from
299 or write to.
301 The B<fp> FILE parameter (if present) specifies the FILE pointer to
302 read from or write to.
304 The PEM read functions all take an argument B<TYPE **x> and return
305 a B<TYPE *> pointer. Where B<TYPE> is whatever structure the function
306 uses. If B<x> is NULL then the parameter is ignored. If B<x> is not
307 NULL but B<*x> is NULL then the structure returned will be written
308 to B<*x>. If neither B<x> nor B<*x> is NULL then an attempt is made
309 to reuse the structure at B<*x> (but see BUGS and EXAMPLES sections).
310 Irrespective of the value of B<x> a pointer to the structure is always
311 returned (or NULL if an error occurred).
313 The PEM functions which write private keys take an B<enc> parameter
314 which specifies the encryption algorithm to use, encryption is done
315 at the PEM level. If this parameter is set to NULL then the private
316 key is written in unencrypted form.
318 The B<cb> argument is the callback to use when querying for the pass
319 phrase used for encrypted PEM structures (normally only private keys).
321 For the PEM write routines if the B<kstr> parameter is not NULL then
322 B<klen> bytes at B<kstr> are used as the passphrase and B<cb> is
323 ignored.
325 If the B<cb> parameters is set to NULL and the B<u> parameter is not
326 NULL then the B<u> parameter is interpreted as a null terminated string
327 to use as the passphrase. If both B<cb> and B<u> are NULL then the
328 default callback routine is used which will typically prompt for the
329 passphrase on the current terminal with echoing turned off.
331 The default passphrase callback is sometimes inappropriate (for example
332 in a GUI application) so an alternative can be supplied. The callback
333 routine has the following form:
335  int cb(char *buf, int size, int rwflag, void *u);
337 B<buf> is the buffer to write the passphrase to. B<size> is the maximum
338 length of the passphrase (i.e. the size of buf). B<rwflag> is a flag
339 which is set to 0 when reading and 1 when writing. A typical routine
340 will ask the user to verify the passphrase (for example by prompting
341 for it twice) if B<rwflag> is 1. The B<u> parameter has the same
342 value as the B<u> parameter passed to the PEM routine. It allows
343 arbitrary data to be passed to the callback by the application
344 (for example a window handle in a GUI application). The callback
345 B<must> return the number of characters in the passphrase or 0 if
346 an error occurred.
348 =head1 EXAMPLES
350 Although the PEM routines take several arguments in almost all applications
351 most of them are set to 0 or NULL.
353 Read a certificate in PEM format from a BIO:
355  X509 *x;
356  x = PEM_read_bio_X509(bp, NULL, 0, NULL);
357  if (x == NULL)
358         {
359         /* Error */
360         }
362 Alternative method:
364  X509 *x = NULL;
365  if (!PEM_read_bio_X509(bp, &x, 0, NULL))
366         {
367         /* Error */
368         }
370 Write a certificate to a BIO:
372  if (!PEM_write_bio_X509(bp, x))
373         {
374         /* Error */
375         }
377 Write an unencrypted private key to a FILE pointer:
379  if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL))
380         {
381         /* Error */
382         }
384 Write a private key (using traditional format) to a BIO using
385 triple DES encryption, the pass phrase is prompted for:
387  if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
388         {
389         /* Error */
390         }
392 Write a private key (using PKCS#8 format) to a BIO using triple
393 DES encryption, using the pass phrase "hello":
395  if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello"))
396         {
397         /* Error */
398         }
400 Read a private key from a BIO using the pass phrase "hello":
402  key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
403  if (key == NULL)
404         {
405         /* Error */
406         }
408 Read a private key from a BIO using a pass phrase callback:
410  key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
411  if (key == NULL)
412         {
413         /* Error */
414         }
416 Skeleton pass phrase callback:
418  int pass_cb(char *buf, int size, int rwflag, void *u);
419         {
420         int len;
421         char *tmp;
422         /* We'd probably do something else if 'rwflag' is 1 */
423         printf("Enter pass phrase for \"%s\"\n", u);
425         /* get pass phrase, length 'len' into 'tmp' */
426         tmp = "hello";
427         len = strlen(tmp);
429         if (len <= 0) return 0;
430         /* if too long, truncate */
431         if (len > size) len = size;
432         memcpy(buf, tmp, len);
433         return len;
434         }
436 =head1 NOTES
438 The old B<PrivateKey> write routines are retained for compatibility.
439 New applications should write private keys using the
440 PEM_write_bio_PKCS8PrivateKey() or PEM_write_PKCS8PrivateKey() routines
441 because they are more secure (they use an iteration count of 2048 whereas
442 the traditional routines use a count of 1) unless compatibility with older
443 versions of OpenSSL is important.
445 The B<PrivateKey> read routines can be used in all applications because
446 they handle all formats transparently.
448 A frequent cause of problems is attempting to use the PEM routines like
449 this:
451  X509 *x;
452  PEM_read_bio_X509(bp, &x, 0, NULL);
454 this is a bug because an attempt will be made to reuse the data at B<x>
455 which is an uninitialised pointer.
457 =head1 PEM ENCRYPTION FORMAT
459 This old B<PrivateKey> routines use a non standard technique for encryption.
461 The private key (or other data) takes the following form: 
463  -----BEGIN RSA PRIVATE KEY-----
464  Proc-Type: 4,ENCRYPTED
465  DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
467  ...base64 encoded data...
468  -----END RSA PRIVATE KEY-----
470 The line beginning DEK-Info contains two comma separated pieces of information:
471 the encryption algorithm name as used by EVP_get_cipherbyname() and an 8
472 byte B<salt> encoded as a set of hexadecimal digits.
474 After this is the base64 encoded encrypted data.
476 The encryption key is determined using EVP_BytesToKey(), using B<salt> and an
477 iteration count of 1. The IV used is the value of B<salt> and *not* the IV
478 returned by EVP_BytesToKey().
480 =head1 BUGS
482 The PEM read routines in some versions of OpenSSL will not correctly reuse
483 an existing structure. Therefore the following:
485  PEM_read_bio_X509(bp, &x, 0, NULL);
487 where B<x> already contains a valid certificate, may not work, whereas: 
489  X509_free(x);
490  x = PEM_read_bio_X509(bp, NULL, 0, NULL);
492 is guaranteed to work.
494 =head1 RETURN CODES
496 The read routines return either a pointer to the structure read or NULL
497 if an error occurred.
499 The write routines return 1 for success or 0 for failure.
501 =head1 SEE ALSO
503 L<EVP_get_cipherbyname(3)|EVP_get_cipherbyname>, L<EVP_BytesToKey(3)|EVP_BytesToKey(3)>