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