]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/man3/PEM_read_bio_PrivateKey.3
OpenSSL: Merge OpenSSL 1.1.1o
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / man3 / PEM_read_bio_PrivateKey.3
1 .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43)
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 >0, 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_READ_BIO_PRIVATEKEY 3"
136 .TH PEM_READ_BIO_PRIVATEKEY 3 "2022-05-03" "1.1.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_password_cb, PEM_read_bio_PrivateKey, PEM_read_PrivateKey, PEM_write_bio_PrivateKey, PEM_write_bio_PrivateKey_traditional, PEM_write_PrivateKey, PEM_write_bio_PKCS8PrivateKey, PEM_write_PKCS8PrivateKey, PEM_write_bio_PKCS8PrivateKey_nid, PEM_write_PKCS8PrivateKey_nid, PEM_read_bio_PUBKEY, PEM_read_PUBKEY, PEM_write_bio_PUBKEY, PEM_write_PUBKEY, PEM_read_bio_RSAPrivateKey, PEM_read_RSAPrivateKey, PEM_write_bio_RSAPrivateKey, PEM_write_RSAPrivateKey, PEM_read_bio_RSAPublicKey, PEM_read_RSAPublicKey, PEM_write_bio_RSAPublicKey, PEM_write_RSAPublicKey, PEM_read_bio_RSA_PUBKEY, PEM_read_RSA_PUBKEY, PEM_write_bio_RSA_PUBKEY, PEM_write_RSA_PUBKEY, PEM_read_bio_DSAPrivateKey, PEM_read_DSAPrivateKey, PEM_write_bio_DSAPrivateKey, PEM_write_DSAPrivateKey, PEM_read_bio_DSA_PUBKEY, PEM_read_DSA_PUBKEY, PEM_write_bio_DSA_PUBKEY, PEM_write_DSA_PUBKEY, PEM_read_bio_Parameters, PEM_write_bio_Parameters, PEM_read_bio_DSAparams, PEM_read_DSAparams, PEM_write_bio_DSAparams, PEM_write_DSAparams, PEM_read_bio_DHparams, PEM_read_DHparams, PEM_write_bio_DHparams, PEM_write_DHparams, PEM_read_bio_X509, PEM_read_X509, PEM_write_bio_X509, PEM_write_X509, PEM_read_bio_X509_AUX, PEM_read_X509_AUX, PEM_write_bio_X509_AUX, PEM_write_X509_AUX, PEM_read_bio_X509_REQ, PEM_read_X509_REQ, PEM_write_bio_X509_REQ, PEM_write_X509_REQ, PEM_write_bio_X509_REQ_NEW, PEM_write_X509_REQ_NEW, PEM_read_bio_X509_CRL, PEM_read_X509_CRL, PEM_write_bio_X509_CRL, PEM_write_X509_CRL, PEM_read_bio_PKCS7, PEM_read_PKCS7, PEM_write_bio_PKCS7, PEM_write_PKCS7 \- PEM routines
143 .SH "SYNOPSIS"
144 .IX Header "SYNOPSIS"
145 .Vb 1
146 \& #include <openssl/pem.h>
147 \&
148 \& typedef int pem_password_cb(char *buf, int size, int rwflag, void *u);
149 \&
150 \& EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x,
151 \&                                   pem_password_cb *cb, void *u);
152 \& EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x,
153 \&                               pem_password_cb *cb, void *u);
154 \& int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
155 \&                              unsigned char *kstr, int klen,
156 \&                              pem_password_cb *cb, void *u);
157 \& int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
158 \&                                          const EVP_CIPHER *enc,
159 \&                                          unsigned char *kstr, int klen,
160 \&                                          pem_password_cb *cb, void *u);
161 \& int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
162 \&                          unsigned char *kstr, int klen,
163 \&                          pem_password_cb *cb, void *u);
164 \&
165 \& int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
166 \&                                   char *kstr, int klen,
167 \&                                   pem_password_cb *cb, void *u);
168 \& int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
169 \&                               char *kstr, int klen,
170 \&                               pem_password_cb *cb, void *u);
171 \& int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
172 \&                                       char *kstr, int klen,
173 \&                                       pem_password_cb *cb, void *u);
174 \& int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
175 \&                                   char *kstr, int klen,
176 \&                                   pem_password_cb *cb, void *u);
177 \&
178 \& EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
179 \&                               pem_password_cb *cb, void *u);
180 \& EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
181 \&                           pem_password_cb *cb, void *u);
182 \& int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
183 \& int PEM_write_PUBKEY(FILE *fp, EVP_PKEY *x);
184 \&
185 \& RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **x,
186 \&                                 pem_password_cb *cb, void *u);
187 \& RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **x,
188 \&                             pem_password_cb *cb, void *u);
189 \& int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
190 \&                                 unsigned char *kstr, int klen,
191 \&                                 pem_password_cb *cb, void *u);
192 \& int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
193 \&                             unsigned char *kstr, int klen,
194 \&                             pem_password_cb *cb, void *u);
195 \&
196 \& RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x,
197 \&                                pem_password_cb *cb, void *u);
198 \& RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x,
199 \&                            pem_password_cb *cb, void *u);
200 \& int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);
201 \& int PEM_write_RSAPublicKey(FILE *fp, RSA *x);
202 \&
203 \& RSA *PEM_read_bio_RSA_PUBKEY(BIO *bp, RSA **x,
204 \&                              pem_password_cb *cb, void *u);
205 \& RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
206 \&                          pem_password_cb *cb, void *u);
207 \& int PEM_write_bio_RSA_PUBKEY(BIO *bp, RSA *x);
208 \& int PEM_write_RSA_PUBKEY(FILE *fp, RSA *x);
209 \&
210 \& DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **x,
211 \&                                 pem_password_cb *cb, void *u);
212 \& DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **x,
213 \&                             pem_password_cb *cb, void *u);
214 \& int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
215 \&                                 unsigned char *kstr, int klen,
216 \&                                 pem_password_cb *cb, void *u);
217 \& int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
218 \&                             unsigned char *kstr, int klen,
219 \&                             pem_password_cb *cb, void *u);
220 \&
221 \& DSA *PEM_read_bio_DSA_PUBKEY(BIO *bp, DSA **x,
222 \&                              pem_password_cb *cb, void *u);
223 \& DSA *PEM_read_DSA_PUBKEY(FILE *fp, DSA **x,
224 \&                          pem_password_cb *cb, void *u);
225 \& int PEM_write_bio_DSA_PUBKEY(BIO *bp, DSA *x);
226 \& int PEM_write_DSA_PUBKEY(FILE *fp, DSA *x);
227 \&
228 \& EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
229 \& int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
230 \&
231 \& DSA *PEM_read_bio_DSAparams(BIO *bp, DSA **x, pem_password_cb *cb, void *u);
232 \& DSA *PEM_read_DSAparams(FILE *fp, DSA **x, pem_password_cb *cb, void *u);
233 \& int PEM_write_bio_DSAparams(BIO *bp, DSA *x);
234 \& int PEM_write_DSAparams(FILE *fp, DSA *x);
235 \&
236 \& DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u);
237 \& DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u);
238 \& int PEM_write_bio_DHparams(BIO *bp, DH *x);
239 \& int PEM_write_DHparams(FILE *fp, DH *x);
240 \&
241 \& X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
242 \& X509 *PEM_read_X509(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
243 \& int PEM_write_bio_X509(BIO *bp, X509 *x);
244 \& int PEM_write_X509(FILE *fp, X509 *x);
245 \&
246 \& X509 *PEM_read_bio_X509_AUX(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
247 \& X509 *PEM_read_X509_AUX(FILE *fp, X509 **x, pem_password_cb *cb, void *u);
248 \& int PEM_write_bio_X509_AUX(BIO *bp, X509 *x);
249 \& int PEM_write_X509_AUX(FILE *fp, X509 *x);
250 \&
251 \& X509_REQ *PEM_read_bio_X509_REQ(BIO *bp, X509_REQ **x,
252 \&                                 pem_password_cb *cb, void *u);
253 \& X509_REQ *PEM_read_X509_REQ(FILE *fp, X509_REQ **x,
254 \&                             pem_password_cb *cb, void *u);
255 \& int PEM_write_bio_X509_REQ(BIO *bp, X509_REQ *x);
256 \& int PEM_write_X509_REQ(FILE *fp, X509_REQ *x);
257 \& int PEM_write_bio_X509_REQ_NEW(BIO *bp, X509_REQ *x);
258 \& int PEM_write_X509_REQ_NEW(FILE *fp, X509_REQ *x);
259 \&
260 \& X509_CRL *PEM_read_bio_X509_CRL(BIO *bp, X509_CRL **x,
261 \&                                 pem_password_cb *cb, void *u);
262 \& X509_CRL *PEM_read_X509_CRL(FILE *fp, X509_CRL **x,
263 \&                             pem_password_cb *cb, void *u);
264 \& int PEM_write_bio_X509_CRL(BIO *bp, X509_CRL *x);
265 \& int PEM_write_X509_CRL(FILE *fp, X509_CRL *x);
266 \&
267 \& PKCS7 *PEM_read_bio_PKCS7(BIO *bp, PKCS7 **x, pem_password_cb *cb, void *u);
268 \& PKCS7 *PEM_read_PKCS7(FILE *fp, PKCS7 **x, pem_password_cb *cb, void *u);
269 \& int PEM_write_bio_PKCS7(BIO *bp, PKCS7 *x);
270 \& int PEM_write_PKCS7(FILE *fp, PKCS7 *x);
271 .Ve
272 .SH "DESCRIPTION"
273 .IX Header "DESCRIPTION"
274 The \s-1PEM\s0 functions read or write structures in \s-1PEM\s0 format. In
275 this sense \s-1PEM\s0 format is simply base64 encoded data surrounded
276 by header lines.
277 .PP
278 For more details about the meaning of arguments see the
279 \&\fB\s-1PEM FUNCTION ARGUMENTS\s0\fR section.
280 .PP
281 Each operation has four functions associated with it. For
282 brevity the term "\fB\s-1TYPE\s0\fR functions" will be used below to collectively
283 refer to the \fBPEM_read_bio_TYPE()\fR, \fBPEM_read_TYPE()\fR,
284 \&\fBPEM_write_bio_TYPE()\fR, and \fBPEM_write_TYPE()\fR functions.
285 .PP
286 The \fBPrivateKey\fR functions read or write a private key in \s-1PEM\s0 format using an
287 \&\s-1EVP_PKEY\s0 structure. The write routines use PKCS#8 private key format and are
288 equivalent to \fBPEM_write_bio_PKCS8PrivateKey()\fR.The read functions transparently
289 handle traditional and PKCS#8 format encrypted and unencrypted keys.
290 .PP
291 \&\fBPEM_write_bio_PrivateKey_traditional()\fR writes out a private key in the
292 \&\*(L"traditional\*(R" format with a simple private key marker and should only
293 be used for compatibility with legacy programs.
294 .PP
295 \&\fBPEM_write_bio_PKCS8PrivateKey()\fR and \fBPEM_write_PKCS8PrivateKey()\fR write a private
296 key in an \s-1EVP_PKEY\s0 structure in PKCS#8 EncryptedPrivateKeyInfo format using
297 PKCS#5 v2.0 password based encryption algorithms. The \fBcipher\fR argument
298 specifies the encryption algorithm to use: unlike some other \s-1PEM\s0 routines the
299 encryption is applied at the PKCS#8 level and not in the \s-1PEM\s0 headers. If
300 \&\fBcipher\fR is \s-1NULL\s0 then no encryption is used and a PKCS#8 PrivateKeyInfo
301 structure is used instead.
302 .PP
303 \&\fBPEM_write_bio_PKCS8PrivateKey_nid()\fR and \fBPEM_write_PKCS8PrivateKey_nid()\fR
304 also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo however
305 it uses PKCS#5 v1.5 or PKCS#12 encryption algorithms instead. The algorithm
306 to use is specified in the \fBnid\fR parameter and should be the \s-1NID\s0 of the
307 corresponding \s-1OBJECT IDENTIFIER\s0 (see \s-1NOTES\s0 section).
308 .PP
309 The \fB\s-1PUBKEY\s0\fR functions process a public key using an \s-1EVP_PKEY\s0
310 structure. The public key is encoded as a SubjectPublicKeyInfo
311 structure.
312 .PP
313 The \fBRSAPrivateKey\fR functions process an \s-1RSA\s0 private key using an
314 \&\s-1RSA\s0 structure. The write routines uses traditional format. The read
315 routines handles the same formats as the \fBPrivateKey\fR
316 functions but an error occurs if the private key is not \s-1RSA.\s0
317 .PP
318 The \fBRSAPublicKey\fR functions process an \s-1RSA\s0 public key using an
319 \&\s-1RSA\s0 structure. The public key is encoded using a PKCS#1 RSAPublicKey
320 structure.
321 .PP
322 The \fB\s-1RSA_PUBKEY\s0\fR functions also process an \s-1RSA\s0 public key using
323 an \s-1RSA\s0 structure. However, the public key is encoded using a
324 SubjectPublicKeyInfo structure and an error occurs if the public
325 key is not \s-1RSA.\s0
326 .PP
327 The \fBDSAPrivateKey\fR functions process a \s-1DSA\s0 private key using a
328 \&\s-1DSA\s0 structure. The write routines uses traditional format. The read
329 routines handles the same formats as the \fBPrivateKey\fR
330 functions but an error occurs if the private key is not \s-1DSA.\s0
331 .PP
332 The \fB\s-1DSA_PUBKEY\s0\fR functions process a \s-1DSA\s0 public key using
333 a \s-1DSA\s0 structure. The public key is encoded using a
334 SubjectPublicKeyInfo structure and an error occurs if the public
335 key is not \s-1DSA.\s0
336 .PP
337 The \fBParameters\fR functions read or write key parameters in \s-1PEM\s0 format using
338 an \s-1EVP_PKEY\s0 structure.  The encoding depends on the type of key; for \s-1DSA\s0 key
339 parameters, it will be a Dss-Parms structure as defined in \s-1RFC2459,\s0 and for \s-1DH\s0
340 key parameters, it will be a PKCS#3 DHparameter structure.  \fIThese functions
341 only exist for the \f(BI\s-1BIO\s0\fI type\fR.
342 .PP
343 The \fBDSAparams\fR functions process \s-1DSA\s0 parameters using a \s-1DSA\s0
344 structure. The parameters are encoded using a Dss-Parms structure
345 as defined in \s-1RFC2459.\s0
346 .PP
347 The \fBDHparams\fR functions process \s-1DH\s0 parameters using a \s-1DH\s0
348 structure. The parameters are encoded using a PKCS#3 DHparameter
349 structure.
350 .PP
351 The \fBX509\fR functions process an X509 certificate using an X509
352 structure. They will also process a trusted X509 certificate but
353 any trust settings are discarded.
354 .PP
355 The \fBX509_AUX\fR functions process a trusted X509 certificate using
356 an X509 structure.
357 .PP
358 The \fBX509_REQ\fR and \fBX509_REQ_NEW\fR functions process a PKCS#10
359 certificate request using an X509_REQ structure. The \fBX509_REQ\fR
360 write functions use \fB\s-1CERTIFICATE REQUEST\s0\fR in the header whereas
361 the \fBX509_REQ_NEW\fR functions use \fB\s-1NEW CERTIFICATE REQUEST\s0\fR
362 (as required by some CAs). The \fBX509_REQ\fR read functions will
363 handle either form so there are no \fBX509_REQ_NEW\fR read functions.
364 .PP
365 The \fBX509_CRL\fR functions process an X509 \s-1CRL\s0 using an X509_CRL
366 structure.
367 .PP
368 The \fB\s-1PKCS7\s0\fR functions process a PKCS#7 ContentInfo using a \s-1PKCS7\s0
369 structure.
370 .SH "PEM FUNCTION ARGUMENTS"
371 .IX Header "PEM FUNCTION ARGUMENTS"
372 The \s-1PEM\s0 functions have many common arguments.
373 .PP
374 The \fBbp\fR \s-1BIO\s0 parameter (if present) specifies the \s-1BIO\s0 to read from
375 or write to.
376 .PP
377 The \fBfp\fR \s-1FILE\s0 parameter (if present) specifies the \s-1FILE\s0 pointer to
378 read from or write to.
379 .PP
380 The \s-1PEM\s0 read functions all take an argument \fB\s-1TYPE\s0 **x\fR and return
381 a \fB\s-1TYPE\s0 *\fR pointer. Where \fB\s-1TYPE\s0\fR is whatever structure the function
382 uses. If \fBx\fR is \s-1NULL\s0 then the parameter is ignored. If \fBx\fR is not
383 \&\s-1NULL\s0 but \fB*x\fR is \s-1NULL\s0 then the structure returned will be written
384 to \fB*x\fR. If neither \fBx\fR nor \fB*x\fR is \s-1NULL\s0 then an attempt is made
385 to reuse the structure at \fB*x\fR (but see \s-1BUGS\s0 and \s-1EXAMPLES\s0 sections).
386 Irrespective of the value of \fBx\fR a pointer to the structure is always
387 returned (or \s-1NULL\s0 if an error occurred).
388 .PP
389 The \s-1PEM\s0 functions which write private keys take an \fBenc\fR parameter
390 which specifies the encryption algorithm to use, encryption is done
391 at the \s-1PEM\s0 level. If this parameter is set to \s-1NULL\s0 then the private
392 key is written in unencrypted form.
393 .PP
394 The \fBcb\fR argument is the callback to use when querying for the pass
395 phrase used for encrypted \s-1PEM\s0 structures (normally only private keys).
396 .PP
397 For the \s-1PEM\s0 write routines if the \fBkstr\fR parameter is not \s-1NULL\s0 then
398 \&\fBklen\fR bytes at \fBkstr\fR are used as the passphrase and \fBcb\fR is
399 ignored.
400 .PP
401 If the \fBcb\fR parameters is set to \s-1NULL\s0 and the \fBu\fR parameter is not
402 \&\s-1NULL\s0 then the \fBu\fR parameter is interpreted as a null terminated string
403 to use as the passphrase. If both \fBcb\fR and \fBu\fR are \s-1NULL\s0 then the
404 default callback routine is used which will typically prompt for the
405 passphrase on the current terminal with echoing turned off.
406 .PP
407 The default passphrase callback is sometimes inappropriate (for example
408 in a \s-1GUI\s0 application) so an alternative can be supplied. The callback
409 routine has the following form:
410 .PP
411 .Vb 1
412 \& int cb(char *buf, int size, int rwflag, void *u);
413 .Ve
414 .PP
415 \&\fBbuf\fR is the buffer to write the passphrase to. \fBsize\fR is the maximum
416 length of the passphrase (i.e. the size of buf). \fBrwflag\fR is a flag
417 which is set to 0 when reading and 1 when writing. A typical routine
418 will ask the user to verify the passphrase (for example by prompting
419 for it twice) if \fBrwflag\fR is 1. The \fBu\fR parameter has the same
420 value as the \fBu\fR parameter passed to the \s-1PEM\s0 routine. It allows
421 arbitrary data to be passed to the callback by the application
422 (for example a window handle in a \s-1GUI\s0 application). The callback
423 \&\fBmust\fR return the number of characters in the passphrase or \-1 if
424 an error occurred.
425 .SH "NOTES"
426 .IX Header "NOTES"
427 The old \fBPrivateKey\fR write routines are retained for compatibility.
428 New applications should write private keys using the
429 \&\fBPEM_write_bio_PKCS8PrivateKey()\fR or \fBPEM_write_PKCS8PrivateKey()\fR routines
430 because they are more secure (they use an iteration count of 2048 whereas
431 the traditional routines use a count of 1) unless compatibility with older
432 versions of OpenSSL is important.
433 .PP
434 The \fBPrivateKey\fR read routines can be used in all applications because
435 they handle all formats transparently.
436 .PP
437 A frequent cause of problems is attempting to use the \s-1PEM\s0 routines like
438 this:
439 .PP
440 .Vb 1
441 \& X509 *x;
442 \&
443 \& PEM_read_bio_X509(bp, &x, 0, NULL);
444 .Ve
445 .PP
446 this is a bug because an attempt will be made to reuse the data at \fBx\fR
447 which is an uninitialised pointer.
448 .PP
449 These functions make no assumption regarding the pass phrase received from the
450 password callback.
451 It will simply be treated as a byte sequence.
452 .SH "PEM ENCRYPTION FORMAT"
453 .IX Header "PEM ENCRYPTION FORMAT"
454 These old \fBPrivateKey\fR routines use a non standard technique for encryption.
455 .PP
456 The private key (or other data) takes the following form:
457 .PP
458 .Vb 3
459 \& \-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-
460 \& Proc\-Type: 4,ENCRYPTED
461 \& DEK\-Info: DES\-EDE3\-CBC,3F17F5316E2BAC89
462 \&
463 \& ...base64 encoded data...
464 \& \-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-
465 .Ve
466 .PP
467 The line beginning with \fIProc-Type\fR contains the version and the
468 protection on the encapsulated data. The line beginning \fIDEK-Info\fR
469 contains two comma separated values: the encryption algorithm name as
470 used by \fBEVP_get_cipherbyname()\fR and an initialization vector used by the
471 cipher encoded as a set of hexadecimal digits. After those two lines is
472 the base64\-encoded encrypted data.
473 .PP
474 The encryption key is derived using \fBEVP_BytesToKey()\fR. The cipher's
475 initialization vector is passed to \fBEVP_BytesToKey()\fR as the \fBsalt\fR
476 parameter. Internally, \fB\s-1PKCS5_SALT_LEN\s0\fR bytes of the salt are used
477 (regardless of the size of the initialization vector). The user's
478 password is passed to \fBEVP_BytesToKey()\fR using the \fBdata\fR and \fBdatal\fR
479 parameters. Finally, the library uses an iteration count of 1 for
480 \&\fBEVP_BytesToKey()\fR.
481 .PP
482 The \fBkey\fR derived by \fBEVP_BytesToKey()\fR along with the original initialization
483 vector is then used to decrypt the encrypted data. The \fBiv\fR produced by
484 \&\fBEVP_BytesToKey()\fR is not utilized or needed, and \s-1NULL\s0 should be passed to
485 the function.
486 .PP
487 The pseudo code to derive the key would look similar to:
488 .PP
489 .Vb 2
490 \& EVP_CIPHER* cipher = EVP_des_ede3_cbc();
491 \& EVP_MD* md = EVP_md5();
492 \&
493 \& unsigned int nkey = EVP_CIPHER_key_length(cipher);
494 \& unsigned int niv = EVP_CIPHER_iv_length(cipher);
495 \& unsigned char key[nkey];
496 \& unsigned char iv[niv];
497 \&
498 \& memcpy(iv, HexToBin("3F17F5316E2BAC89"), niv);
499 \& rc = EVP_BytesToKey(cipher, md, iv /*salt*/, pword, plen, 1, key, NULL /*iv*/);
500 \& if (rc != nkey)
501 \&     /* Error */
502 \&
503 \& /* On success, use key and iv to initialize the cipher */
504 .Ve
505 .SH "BUGS"
506 .IX Header "BUGS"
507 The \s-1PEM\s0 read routines in some versions of OpenSSL will not correctly reuse
508 an existing structure. Therefore, the following:
509 .PP
510 .Vb 1
511 \& PEM_read_bio_X509(bp, &x, 0, NULL);
512 .Ve
513 .PP
514 where \fBx\fR already contains a valid certificate, may not work, whereas:
515 .PP
516 .Vb 2
517 \& X509_free(x);
518 \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
519 .Ve
520 .PP
521 is guaranteed to work.
522 .SH "RETURN VALUES"
523 .IX Header "RETURN VALUES"
524 The read routines return either a pointer to the structure read or \s-1NULL\s0
525 if an error occurred.
526 .PP
527 The write routines return 1 for success or 0 for failure.
528 .SH "EXAMPLES"
529 .IX Header "EXAMPLES"
530 Although the \s-1PEM\s0 routines take several arguments in almost all applications
531 most of them are set to 0 or \s-1NULL.\s0
532 .PP
533 Read a certificate in \s-1PEM\s0 format from a \s-1BIO:\s0
534 .PP
535 .Vb 1
536 \& X509 *x;
537 \&
538 \& x = PEM_read_bio_X509(bp, NULL, 0, NULL);
539 \& if (x == NULL)
540 \&     /* Error */
541 .Ve
542 .PP
543 Alternative method:
544 .PP
545 .Vb 1
546 \& X509 *x = NULL;
547 \&
548 \& if (!PEM_read_bio_X509(bp, &x, 0, NULL))
549 \&     /* Error */
550 .Ve
551 .PP
552 Write a certificate to a \s-1BIO:\s0
553 .PP
554 .Vb 2
555 \& if (!PEM_write_bio_X509(bp, x))
556 \&     /* Error */
557 .Ve
558 .PP
559 Write a private key (using traditional format) to a \s-1BIO\s0 using
560 triple \s-1DES\s0 encryption, the pass phrase is prompted for:
561 .PP
562 .Vb 2
563 \& if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(), NULL, 0, 0, NULL))
564 \&     /* Error */
565 .Ve
566 .PP
567 Write a private key (using PKCS#8 format) to a \s-1BIO\s0 using triple
568 \&\s-1DES\s0 encryption, using the pass phrase \*(L"hello\*(R":
569 .PP
570 .Vb 3
571 \& if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
572 \&                                    NULL, 0, 0, "hello"))
573 \&     /* Error */
574 .Ve
575 .PP
576 Read a private key from a \s-1BIO\s0 using a pass phrase callback:
577 .PP
578 .Vb 3
579 \& key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
580 \& if (key == NULL)
581 \&     /* Error */
582 .Ve
583 .PP
584 Skeleton pass phrase callback:
585 .PP
586 .Vb 2
587 \& int pass_cb(char *buf, int size, int rwflag, void *u)
588 \& {
589 \&
590 \&     /* We\*(Aqd probably do something else if \*(Aqrwflag\*(Aq is 1 */
591 \&     printf("Enter pass phrase for \e"%s\e"\en", (char *)u);
592 \&
593 \&     /* get pass phrase, length \*(Aqlen\*(Aq into \*(Aqtmp\*(Aq */
594 \&     char *tmp = "hello";
595 \&     if (tmp == NULL) /* An error occurred */
596 \&         return \-1;
597 \&
598 \&     size_t len = strlen(tmp);
599 \&
600 \&     if (len > size)
601 \&         len = size;
602 \&     memcpy(buf, tmp, len);
603 \&     return len;
604 \& }
605 .Ve
606 .SH "SEE ALSO"
607 .IX Header "SEE ALSO"
608 \&\fBEVP_EncryptInit\fR\|(3), \fBEVP_BytesToKey\fR\|(3),
609 \&\fBpassphrase\-encoding\fR\|(7)
610 .SH "HISTORY"
611 .IX Header "HISTORY"
612 The old Netscape certificate sequences were no longer documented
613 in OpenSSL 1.1.0; applications should use the \s-1PKCS7\s0 standard instead
614 as they will be formally deprecated in a future releases.
615 .SH "COPYRIGHT"
616 .IX Header "COPYRIGHT"
617 Copyright 2001\-2020 The OpenSSL Project Authors. All Rights Reserved.
618 .PP
619 Licensed under the OpenSSL license (the \*(L"License\*(R").  You may not use
620 this file except in compliance with the License.  You can obtain a copy
621 in the file \s-1LICENSE\s0 in the source distribution or at
622 <https://www.openssl.org/source/license.html>.