Merge from vendor branch OPENSSL:
[dragonfly.git] / secure / lib / libcrypto / man / pem.3
1 .rn '' }`
2 ''' $RCSfile$$Revision$$Date$
3 '''
4 ''' $Log$
5 '''
6 .de Sh
7 .br
8 .if t .Sp
9 .ne 5
10 .PP
11 \fB\\$1\fR
12 .PP
13 ..
14 .de Sp
15 .if t .sp .5v
16 .if n .sp
17 ..
18 .de Ip
19 .br
20 .ie \\n(.$>=3 .ne \\$3
21 .el .ne 3
22 .IP "\\$1" \\$2
23 ..
24 .de Vb
25 .ft CW
26 .nf
27 .ne \\$1
28 ..
29 .de Ve
30 .ft R
31
32 .fi
33 ..
34 '''
35 '''
36 '''     Set up \*(-- to give an unbreakable dash;
37 '''     string Tr holds user defined translation string.
38 '''     Bell System Logo is used as a dummy character.
39 '''
40 .tr \(*W-|\(bv\*(Tr
41 .ie n \{\
42 .ds -- \(*W-
43 .ds PI pi
44 .if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
45 .if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
46 .ds L" ""
47 .ds R" ""
48 '''   \*(M", \*(S", \*(N" and \*(T" are the equivalent of
49 '''   \*(L" and \*(R", except that they are used on ".xx" lines,
50 '''   such as .IP and .SH, which do another additional levels of
51 '''   double-quote interpretation
52 .ds M" """
53 .ds S" """
54 .ds N" """""
55 .ds T" """""
56 .ds L' '
57 .ds R' '
58 .ds M' '
59 .ds S' '
60 .ds N' '
61 .ds T' '
62 'br\}
63 .el\{\
64 .ds -- \(em\|
65 .tr \*(Tr
66 .ds L" ``
67 .ds R" ''
68 .ds M" ``
69 .ds S" ''
70 .ds N" ``
71 .ds T" ''
72 .ds L' `
73 .ds R' '
74 .ds M' `
75 .ds S' '
76 .ds N' `
77 .ds T' '
78 .ds PI \(*p
79 'br\}
80 .\"     If the F register is turned on, we'll generate
81 .\"     index entries out stderr for the following things:
82 .\"             TH      Title 
83 .\"             SH      Header
84 .\"             Sh      Subsection 
85 .\"             Ip      Item
86 .\"             X<>     Xref  (embedded
87 .\"     Of course, you have to process the output yourself
88 .\"     in some meaninful fashion.
89 .if \nF \{
90 .de IX
91 .tm Index:\\$1\t\\n%\t"\\$2"
92 ..
93 .nr % 0
94 .rr F
95 .\}
96 .TH pem 3 "0.9.7d" "2/Sep/2004" "OpenSSL"
97 .UC
98 .if n .hy 0
99 .if n .na
100 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
101 .de CQ          \" put $1 in typewriter font
102 .ft CW
103 'if n "\c
104 'if t \\&\\$1\c
105 'if n \\&\\$1\c
106 'if n \&"
107 \\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7
108 '.ft R
109 ..
110 .\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2
111 .       \" AM - accent mark definitions
112 .bd B 3
113 .       \" fudge factors for nroff and troff
114 .if n \{\
115 .       ds #H 0
116 .       ds #V .8m
117 .       ds #F .3m
118 .       ds #[ \f1
119 .       ds #] \fP
120 .\}
121 .if t \{\
122 .       ds #H ((1u-(\\\\n(.fu%2u))*.13m)
123 .       ds #V .6m
124 .       ds #F 0
125 .       ds #[ \&
126 .       ds #] \&
127 .\}
128 .       \" simple accents for nroff and troff
129 .if n \{\
130 .       ds ' \&
131 .       ds ` \&
132 .       ds ^ \&
133 .       ds , \&
134 .       ds ~ ~
135 .       ds ? ?
136 .       ds ! !
137 .       ds /
138 .       ds q
139 .\}
140 .if t \{\
141 .       ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
142 .       ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
143 .       ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
144 .       ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
145 .       ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
146 .       ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
147 .       ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
148 .       ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
149 .       ds q o\h'-\w'o'u*8/10'\s-4\v'.4m'\z\(*i\v'-.4m'\s+4\h'\w'o'u*8/10'
150 .\}
151 .       \" troff and (daisy-wheel) nroff accents
152 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
153 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
154 .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
155 .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
156 .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
157 .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
158 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
159 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
160 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
161 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
162 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
163 .ds ae a\h'-(\w'a'u*4/10)'e
164 .ds Ae A\h'-(\w'A'u*4/10)'E
165 .ds oe o\h'-(\w'o'u*4/10)'e
166 .ds Oe O\h'-(\w'O'u*4/10)'E
167 .       \" corrections for vroff
168 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
169 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
170 .       \" for low resolution devices (crt and lpr)
171 .if \n(.H>23 .if \n(.V>19 \
172 \{\
173 .       ds : e
174 .       ds 8 ss
175 .       ds v \h'-1'\o'\(aa\(ga'
176 .       ds _ \h'-1'^
177 .       ds . \h'-1'.
178 .       ds 3 3
179 .       ds o a
180 .       ds d- d\h'-1'\(ga
181 .       ds D- D\h'-1'\(hy
182 .       ds th \o'bp'
183 .       ds Th \o'LP'
184 .       ds ae ae
185 .       ds Ae AE
186 .       ds oe oe
187 .       ds Oe OE
188 .\}
189 .rm #[ #] #H #V #F C
190 .SH "NAME"
191 PEM \- PEM routines
192 .SH "SYNOPSIS"
193 .PP
194 .Vb 1
195 \& #include <openssl/pem.h>
196 .Ve
197 .Vb 2
198 \& EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
199 \&                                        pem_password_cb *cb, void *u);
200 .Ve
201 .Vb 2
202 \& EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
203 \&                                        pem_password_cb *cb, void *u);
204 .Ve
205 .Vb 3
206 \& int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
207 \&                                        unsigned char *kstr, int klen,
208 \&                                        pem_password_cb *cb, void *u);
209 .Ve
210 .Vb 3
211 \& int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
212 \&                                        unsigned char *kstr, int klen,
213 \&                                        pem_password_cb *cb, void *u);
214 .Ve
215 .Vb 3
216 \& int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
217 \&                                        char *kstr, int klen,
218 \&                                        pem_password_cb *cb, void *u);
219 .Ve
220 .Vb 3
221 \& int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
222 \&                                        char *kstr, int klen,
223 \&                                        pem_password_cb *cb, void *u);
224 .Ve
225 .Vb 3
226 \& int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
227 \&                                        char *kstr, int klen,
228 \&                                        pem_password_cb *cb, void *u);
229 .Ve
230 .Vb 3
231 \& int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
232 \&                                        char *kstr, int klen,
233 \&                                        pem_password_cb *cb, void *u);
234 .Ve
235 .Vb 2
236 \& EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
237 \&                                        pem_password_cb *cb, void *u);
238 .Ve
239 .Vb 2
240 \& EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
241 \&                                        pem_password_cb *cb, void *u);
242 .Ve
243 .Vb 2
244 \& int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
245 \& int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
246 .Ve
247 .Vb 2
248 \& RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
249 \&                                        pem_password_cb *cb, void *u);
250 .Ve
251 .Vb 2
252 \& RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
253 \&                                        pem_password_cb *cb, void *u);
254 .Ve
255 .Vb 3
256 \& int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
257 \&                                        unsigned char *kstr, int klen,
258 \&                                        pem_password_cb *cb, void *u);
259 .Ve
260 .Vb 3
261 \& int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
262 \&                                        unsigned char *kstr, int klen,
263 \&                                        pem_password_cb *cb, void *u);
264 .Ve
265 .Vb 2
266 \& RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
267 \&                                        pem_password_cb *cb, void *u);
268 .Ve
269 .Vb 2
270 \& RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
271 \&                                        pem_password_cb *cb, void *u);
272 .Ve
273 .Vb 1
274 \& int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
275 .Ve
276 .Vb 1
277 \& int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
278 .Ve
279 .Vb 2
280 \& RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
281 \&                                        pem_password_cb *cb, void *u);
282 .Ve
283 .Vb 2
284 \& RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
285 \&                                        pem_password_cb *cb, void *u);
286 .Ve
287 .Vb 1
288 \& int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
289 .Ve
290 .Vb 1
291 \& int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
292 .Ve
293 .Vb 2
294 \& DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
295 \&                                        pem_password_cb *cb, void *u);
296 .Ve
297 .Vb 2
298 \& DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
299 \&                                        pem_password_cb *cb, void *u);
300 .Ve
301 .Vb 3
302 \& int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
303 \&                                        unsigned char *kstr, int klen,
304 \&                                        pem_password_cb *cb, void *u);
305 .Ve
306 .Vb 3
307 \& int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
308 \&                                        unsigned char *kstr, int klen,
309 \&                                        pem_password_cb *cb, void *u);
310 .Ve
311 .Vb 2
312 \& DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
313 \&                                        pem_password_cb *cb, void *u);
314 .Ve
315 .Vb 2
316 \& DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
317 \&                                        pem_password_cb *cb, void *u);
318 .Ve
319 .Vb 1
320 \& int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
321 .Ve
322 .Vb 1
323 \& int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
324 .Ve
325 .Vb 1
326 \& DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
327 .Ve
328 .Vb 1
329 \& DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
330 .Ve
331 .Vb 1
332 \& int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
333 .Ve
334 .Vb 1
335 \& int PEM_write_DSAparams(FILE *fp, DSA *x);
336 .Ve
337 .Vb 1
338 \& DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
339 .Ve
340 .Vb 1
341 \& DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
342 .Ve
343 .Vb 1
344 \& int PEM_write_bio_DHparams(BIO *bp, DH *x);
345 .Ve
346 .Vb 1
347 \& int PEM_write_DHparams(FILE *fp, DH *x);
348 .Ve
349 .Vb 1
350 \& X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
351 .Ve
352 .Vb 1
353 \& X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
354 .Ve
355 .Vb 1
356 \& int PEM_write_bio_X509(BIO *bp, X509 *x);
357 .Ve
358 .Vb 1
359 \& int PEM_write_X509(FILE *fp, X509 *x);
360 .Ve
361 .Vb 1
362 \& X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
363 .Ve
364 .Vb 1
365 \& X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
366 .Ve
367 .Vb 1
368 \& int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
369 .Ve
370 .Vb 1
371 \& int PEM_write_X509_AUX(FILE *fp, X509 *x);
372 .Ve
373 .Vb 2
374 \& X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
375 \&                                        pem_password_cb *cb, void *u);
376 .Ve
377 .Vb 2
378 \& X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
379 \&                                        pem_password_cb *cb, void *u);
380 .Ve
381 .Vb 1
382 \& int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
383 .Ve
384 .Vb 1
385 \& int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
386 .Ve
387 .Vb 1
388 \& int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
389 .Ve
390 .Vb 1
391 \& int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
392 .Ve
393 .Vb 6
394 \& X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
395 \&                                        pem_password_cb *cb, void *u);
396 \& X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
397 \&                                        pem_password_cb *cb, void *u);
398 \& int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
399 \& int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
400 .Ve
401 .Vb 1
402 \& PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
403 .Ve
404 .Vb 1
405 \& PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
406 .Ve
407 .Vb 1
408 \& int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
409 .Ve
410 .Vb 1
411 \& int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
412 .Ve
413 .Vb 3
414 \& NETSCAPE_CERT_SEQUENCE *PEM_read_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp,
415 \&                                                NETSCAPE_CERT_SEQUENCE **x,
416 \&                                                pem_password_cb *cb, void *u);
417 .Ve
418 .Vb 3
419 \& NETSCAPE_CERT_SEQUENCE *PEM_read_NETSCAPE_CERT_SEQUENCE(FILE *fp,
420 \&                                                NETSCAPE_CERT_SEQUENCE **x,
421 \&                                                pem_password_cb *cb, void *u);
422 .Ve
423 .Vb 1
424 \& int PEM_write_bio_NETSCAPE_CERT_SEQUENCE(BIO *bp, NETSCAPE_CERT_SEQUENCE *x);
425 .Ve
426 .Vb 1
427 \& int PEM_write_NETSCAPE_CERT_SEQUENCE(FILE *fp, NETSCAPE_CERT_SEQUENCE *x);
428 .Ve
429 .SH "DESCRIPTION"
430 The PEM functions read or write structures in PEM format. In
431 this sense PEM format is simply base64 encoded data surrounded
432 by header lines.
433 .PP
434 For more details about the meaning of arguments see the
435 \fBPEM FUNCTION ARGUMENTS\fR section.
436 .PP
437 Each operation has four functions associated with it. For
438 clarity the term \*(L"\fBfoobar\fR functions\*(R" will be used to collectively
439 refer to the \fIPEM_read_bio_foobar()\fR, \fIPEM_read_foobar()\fR,
440 \fIPEM_write_bio_foobar()\fR and \fIPEM_write_foobar()\fR functions.
441 .PP
442 The \fBPrivateKey\fR functions read or write a private key in
443 PEM format using an EVP_PKEY structure. The write routines use
444 \*(L"traditional\*(R" private key format and can handle both RSA and DSA
445 private keys. The read functions can additionally transparently
446 handle PKCS#8 format encrypted and unencrypted keys too.
447 .PP
448 \fIPEM_write_bio_PKCS8PrivateKey()\fR and \fIPEM_write_PKCS8PrivateKey()\fR
449 write a private key in an EVP_PKEY structure in PKCS#8
450 EncryptedPrivateKeyInfo format using PKCS#5 v2.0 password based encryption
451 algorithms. The \fBcipher\fR argument specifies the encryption algoritm to
452 use: unlike all other PEM routines the encryption is applied at the
453 PKCS#8 level and not in the PEM headers. If \fBcipher\fR is NULL then no
454 encryption is used and a PKCS#8 PrivateKeyInfo structure is used instead.
455 .PP
456 \fIPEM_write_bio_PKCS8PrivateKey_nid()\fR and \fIPEM_write_PKCS8PrivateKey_nid()\fR
457 also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
458 it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
459 to use is specified in the \fBnid\fR parameter and should be the NID of the
460 corresponding OBJECT IDENTIFIER (see NOTES section).
461 .PP
462 The \fBPUBKEY\fR functions process a public key using an EVP_PKEY
463 structure. The public key is encoded as a SubjectPublicKeyInfo
464 structure.
465 .PP
466 The \fBRSAPrivateKey\fR functions process an RSA private key using an
467 RSA structure. It handles the same formats as the \fBPrivateKey\fR
468 functions but an error occurs if the private key is not RSA.
469 .PP
470 The \fBRSAPublicKey\fR functions process an RSA public key using an
471 RSA structure. The public key is encoded using a PKCS#1 RSAPublicKey
472 structure.
473 .PP
474 The \fBRSA_PUBKEY\fR functions also process an RSA public key using
475 an RSA structure. However the public key is encoded using a
476 SubjectPublicKeyInfo structure and an error occurs if the public
477 key is not RSA.
478 .PP
479 The \fBDSAPrivateKey\fR functions process a DSA private key using a
480 DSA structure. It handles the same formats as the \fBPrivateKey\fR
481 functions but an error occurs if the private key is not DSA.
482 .PP
483 The \fBDSA_PUBKEY\fR functions process a DSA public key using
484 a DSA structure. The public key is encoded using a
485 SubjectPublicKeyInfo structure and an error occurs if the public
486 key is not DSA.
487 .PP
488 The \fBDSAparams\fR functions process DSA parameters using a DSA
489 structure. The parameters are encoded using a foobar structure.
490 .PP
491 The \fBDHparams\fR functions process DH parameters using a DH
492 structure. The parameters are encoded using a PKCS#3 DHparameter
493 structure.
494 .PP
495 The \fBX509\fR functions process an X509 certificate using an X509
496 structure. They will also process a trusted X509 certificate but
497 any trust settings are discarded.
498 .PP
499 The \fBX509_AUX\fR functions process a trusted X509 certificate using
500 an X509 structure. 
501 .PP
502 The \fBX509_REQ\fR and \fBX509_REQ_NEW\fR functions process a PKCS#10
503 certificate request using an X509_REQ structure. The \fBX509_REQ\fR
504 write functions use \fBCERTIFICATE REQUEST\fR in the header whereas
505 the \fBX509_REQ_NEW\fR functions use \fBNEW CERTIFICATE REQUEST\fR
506 (as required by some CAs). The \fBX509_REQ\fR read functions will
507 handle either form so there are no \fBX509_REQ_NEW\fR read functions.
508 .PP
509 The \fBX509_CRL\fR functions process an X509 CRL using an X509_CRL
510 structure.
511 .PP
512 The \fBPKCS7\fR functions process a PKCS#7 ContentInfo using a PKCS7
513 structure.
514 .PP
515 The \fBNETSCAPE_CERT_SEQUENCE\fR functions process a Netscape Certificate
516 Sequence using a NETSCAPE_CERT_SEQUENCE structure.
517 .SH "PEM FUNCTION ARGUMENTS"
518 The PEM functions have many common arguments.
519 .PP
520 The \fBbp\fR BIO parameter (if present) specifies the BIO to read from
521 or write to.
522 .PP
523 The \fBfp\fR FILE parameter (if present) specifies the FILE pointer to
524 read from or write to.
525 .PP
526 The PEM read functions all take an argument \fBTYPE **x\fR and return
527 a \fBTYPE *\fR pointer. Where \fBTYPE\fR is whatever structure the function
528 uses. If \fBx\fR is NULL then the parameter is ignored. If \fBx\fR is not
529 NULL but \fB*x\fR is NULL then the structure returned will be written
530 to \fB*x\fR. If neither \fBx\fR nor \fB*x\fR is NULL then an attempt is made
531 to reuse the structure at \fB*x\fR (but see BUGS and EXAMPLES sections).
532 Irrespective of the value of \fBx\fR a pointer to the structure is always
533 returned (or NULL if an error occurred).
534 .PP
535 The PEM functions which write private keys take an \fBenc\fR parameter
536 which specifies the encryption algorithm to use, encryption is done
537 at the PEM level. If this parameter is set to NULL then the private
538 key is written in unencrypted form.
539 .PP
540 The \fBcb\fR argument is the callback to use when querying for the pass
541 phrase used for encrypted PEM structures (normally only private keys).
542 .PP
543 For the PEM write routines if the \fBkstr\fR parameter is not NULL then
544 \fBklen\fR bytes at \fBkstr\fR are used as the passphrase and \fBcb\fR is
545 ignored.
546 .PP
547 If the \fBcb\fR parameters is set to NULL and the \fBu\fR parameter is not
548 NULL then the \fBu\fR parameter is interpreted as a null terminated string
549 to use as the passphrase. If both \fBcb\fR and \fBu\fR are NULL then the
550 default callback routine is used which will typically prompt for the
551 passphrase on the current terminal with echoing turned off.
552 .PP
553 The default passphrase callback is sometimes inappropriate (for example
554 in a GUI application) so an alternative can be supplied. The callback
555 routine has the following form:
556 .PP
557 .Vb 1
558 \& int cb(char *buf, int size, int rwflag, void *u);
559 .Ve
560 \fBbuf\fR is the buffer to write the passphrase to. \fBsize\fR is the maximum
561 length of the passphrase (i.e. the size of buf). \fBrwflag\fR is a flag
562 which is set to 0 when reading and 1 when writing. A typical routine
563 will ask the user to verify the passphrase (for example by prompting
564 for it twice) if \fBrwflag\fR is 1. The \fBu\fR parameter has the same
565 value as the \fBu\fR parameter passed to the PEM routine. It allows
566 arbitrary data to be passed to the callback by the application
567 (for example a window handle in a GUI application). The callback
568 \fBmust\fR return the number of characters in the passphrase or 0 if
569 an error occurred.
570 .SH "EXAMPLES"
571 Although the PEM routines take several arguments in almost all applications
572 most of them are set to 0 or NULL.
573 .PP
574 Read a certificate in PEM format from a BIO:
575 .PP
576 .Vb 6
577 \& X509 *x;
578 \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
579 \& if (x == NULL)
580 \&        {
581 \&        /* Error */
582 \&        }
583 .Ve
584 Alternative method:
585 .PP
586 .Vb 5
587 \& X509 *x = NULL;
588 \& if (!PEM_read_bio_X509(bp, &x, 0, NULL))
589 \&        {
590 \&        /* Error */
591 \&        }
592 .Ve
593 Write a certificate to a BIO:
594 .PP
595 .Vb 4
596 \& if (!PEM_write_bio_X509(bp, x))
597 \&        {
598 \&        /* Error */
599 \&        }
600 .Ve
601 Write an unencrypted private key to a FILE pointer:
602 .PP
603 .Vb 4
604 \& if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL))
605 \&        {
606 \&        /* Error */
607 \&        }
608 .Ve
609 Write a private key (using traditional format) to a BIO using
610 triple DES encryption, the pass phrase is prompted for:
611 .PP
612 .Vb 4
613 \& if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
614 \&        {
615 \&        /* Error */
616 \&        }
617 .Ve
618 Write a private key (using PKCS#8 format) to a BIO using triple
619 DES encryption, using the pass phrase \*(L"hello":
620 .PP
621 .Vb 4
622 \& if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, "hello"))
623 \&        {
624 \&        /* Error */
625 \&        }
626 .Ve
627 Read a private key from a BIO using the pass phrase \*(L"hello":
628 .PP
629 .Vb 5
630 \& key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
631 \& if (key == NULL)
632 \&        {
633 \&        /* Error */
634 \&        }
635 .Ve
636 Read a private key from a BIO using a pass phrase callback:
637 .PP
638 .Vb 5
639 \& key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
640 \& if (key == NULL)
641 \&        {
642 \&        /* Error */
643 \&        }
644 .Ve
645 Skeleton pass phrase callback:
646 .PP
647 .Vb 6
648 \& int pass_cb(char *buf, int size, int rwflag, void *u);
649 \&        {
650 \&        int len;
651 \&        char *tmp;
652 \&        /* We'd probably do something else if 'rwflag' is 1 */
653 \&        printf("Enter pass phrase for \e"%s\e"\en", u);
654 .Ve
655 .Vb 3
656 \&        /* get pass phrase, length 'len' into 'tmp' */
657 \&        tmp = "hello";
658 \&        len = strlen(tmp);
659 .Ve
660 .Vb 6
661 \&        if (len <= 0) return 0;
662 \&        /* if too long, truncate */
663 \&        if (len > size) len = size;
664 \&        memcpy(buf, tmp, len);
665 \&        return len;
666 \&        }
667 .Ve
668 .SH "NOTES"
669 The old \fBPrivateKey\fR write routines are retained for compatibility.
670 New applications should write private keys using the
671 \fIPEM_write_bio_PKCS8PrivateKey()\fR or \fIPEM_write_PKCS8PrivateKey()\fR routines
672 because they are more secure (they use an iteration count of 2048 whereas
673 the traditional routines use a count of 1) unless compatibility with older
674 versions of OpenSSL is important.
675 .PP
676 The \fBPrivateKey\fR read routines can be used in all applications because
677 they handle all formats transparently.
678 .PP
679 A frequent cause of problems is attempting to use the PEM routines like
680 this:
681 .PP
682 .Vb 2
683 \& X509 *x;
684 \& PEM_read_bio_X509(bp, &x, 0, NULL);
685 .Ve
686 this is a bug because an attempt will be made to reuse the data at \fBx\fR
687 which is an uninitialised pointer.
688 .SH "PEM ENCRYPTION FORMAT"
689 This old \fBPrivateKey\fR routines use a non standard technique for encryption.
690 .PP
691 The private key (or other data) takes the following form: 
692 .PP
693 .Vb 3
694 \& -----BEGIN RSA PRIVATE KEY-----
695 \& Proc-Type: 4,ENCRYPTED
696 \& DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89
697 .Ve
698 .Vb 2
699 \& ...base64 encoded data...
700 \& -----END RSA PRIVATE KEY-----
701 .Ve
702 The line beginning DEK\-Info contains two comma separated pieces of information:
703 the encryption algorithm name as used by \fIEVP_get_cipherbyname()\fR and an 8
704 byte \fBsalt\fR encoded as a set of hexadecimal digits.
705 .PP
706 After this is the base64 encoded encrypted data.
707 .PP
708 The encryption key is determined using \fIEVP_bytestokey()\fR, using \fBsalt\fR and an
709 iteration count of 1. The IV used is the value of \fBsalt\fR and *not* the IV
710 returned by \fIEVP_bytestokey()\fR.
711 .SH "BUGS"
712 The PEM read routines in some versions of OpenSSL will not correctly reuse
713 an existing structure. Therefore the following:
714 .PP
715 .Vb 1
716 \& PEM_read_bio_X509(bp, &x, 0, NULL);
717 .Ve
718 where \fBx\fR already contains a valid certificate, may not work, whereas: 
719 .PP
720 .Vb 2
721 \& X509_free(x);
722 \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
723 .Ve
724 is guaranteed to work.
725 .SH "RETURN CODES"
726 The read routines return either a pointer to the structure read or NULL
727 is an error occurred.
728 .PP
729 The write routines return 1 for success or 0 for failure.
730
731 .rn }` ''
732 .IX Title "pem 3"
733 .IX Name "PEM - PEM routines"
734
735 .IX Header "NAME"
736
737 .IX Header "SYNOPSIS"
738
739 .IX Header "DESCRIPTION"
740
741 .IX Header "PEM FUNCTION ARGUMENTS"
742
743 .IX Header "EXAMPLES"
744
745 .IX Header "NOTES"
746
747 .IX Header "PEM ENCRYPTION FORMAT"
748
749 .IX Header "BUGS"
750
751 .IX Header "RETURN CODES"
752