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