Switch from OpenSSL 0.9.7a to OpenSSL 0.9.7d.
[dragonfly.git] / secure / lib / libcrypto / man / d2i_X509.3
1 .\" Automatically generated by Pod::Man version 1.15
2 .\" Wed Feb 19 16:43:03 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 "d2i_X509 3"
141 .TH d2i_X509 3 "0.9.7a" "2003-02-19" "OpenSSL"
142 .UC
143 .SH "NAME"
144 d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio,
145 i2d_X509_fp \- X509 encode and decode functions
146 .SH "SYNOPSIS"
147 .IX Header "SYNOPSIS"
148 .Vb 1
149 \& #include <openssl/x509.h>
150 .Ve
151 .Vb 2
152 \& X509 *d2i_X509(X509 **px, unsigned char **in, int len);
153 \& int i2d_X509(X509 *x, unsigned char **out);
154 .Ve
155 .Vb 2
156 \& X509 *d2i_X509_bio(BIO *bp, X509 **x);
157 \& X509 *d2i_X509_fp(FILE *fp, X509 **x);
158 .Ve
159 .Vb 2
160 \& int i2d_X509_bio(X509 *x, BIO *bp);
161 \& int i2d_X509_fp(X509 *x, FILE *fp);
162 .Ve
163 .SH "DESCRIPTION"
164 .IX Header "DESCRIPTION"
165 The X509 encode and decode routines encode and parse an
166 \&\fBX509\fR structure, which represents an X509 certificate.
167 .PP
168 \&\fId2i_X509()\fR attempts to decode \fBlen\fR bytes at \fB*out\fR. If 
169 successful a pointer to the \fBX509\fR structure is returned. If an error
170 occurred then \fB\s-1NULL\s0\fR is returned. If \fBpx\fR is not \fB\s-1NULL\s0\fR then the
171 returned structure is written to \fB*px\fR. If \fB*px\fR is not \fB\s-1NULL\s0\fR
172 then it is assumed that \fB*px\fR contains a valid \fBX509\fR
173 structure and an attempt is made to reuse it. If the call is
174 successful \fB*out\fR is incremented to the byte following the
175 parsed data.
176 .PP
177 \&\fIi2d_X509()\fR encodes the structure pointed to by \fBx\fR into \s-1DER\s0 format.
178 If \fBout\fR is not \fB\s-1NULL\s0\fR is writes the \s-1DER\s0 encoded data to the buffer
179 at \fB*out\fR, and increments it to point after the data just written.
180 If the return value is negative an error occurred, otherwise it
181 returns the length of the encoded data. 
182 .PP
183 For OpenSSL 0.9.7 and later if \fB*out\fR is \fB\s-1NULL\s0\fR memory will be
184 allocated for a buffer and the encoded data written to it. In this
185 case \fB*out\fR is not incremented and it points to the start of the
186 data just written.
187 .PP
188 \&\fId2i_X509_bio()\fR is similar to \fId2i_X509()\fR except it attempts
189 to parse data from \s-1BIO\s0 \fBbp\fR.
190 .PP
191 \&\fId2i_X509_fp()\fR is similar to \fId2i_X509()\fR except it attempts
192 to parse data from \s-1FILE\s0 pointer \fBfp\fR.
193 .PP
194 \&\fIi2d_X509_bio()\fR is similar to \fIi2d_X509()\fR except it writes
195 the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it
196 returns 1 for success and 0 for failure.
197 .PP
198 \&\fIi2d_X509_fp()\fR is similar to \fIi2d_X509()\fR except it writes
199 the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it
200 returns 1 for success and 0 for failure.
201 .SH "NOTES"
202 .IX Header "NOTES"
203 The letters \fBi\fR and \fBd\fR in for example \fBi2d_X509\fR stand for
204 \&\*(L"internal\*(R" (that is an internal C structure) and \*(L"\s-1DER\s0\*(R". So that
205 \&\fBi2d_X509\fR converts from internal to \s-1DER\s0.
206 .PP
207 The functions can also understand \fB\s-1BER\s0\fR forms.
208 .PP
209 The actual X509 structure passed to \fIi2d_X509()\fR must be a valid
210 populated \fBX509\fR structure it can \fBnot\fR simply be fed with an
211 empty structure such as that returned by \fIX509_new()\fR.
212 .PP
213 The encoded data is in binary form and may contain embedded zeroes.
214 Therefore any \s-1FILE\s0 pointers or BIOs should be opened in binary mode.
215 Functions such as \fB\f(BIstrlen()\fB\fR will \fBnot\fR return the correct length
216 of the encoded structure.
217 .PP
218 The ways that \fB*in\fR and \fB*out\fR are incremented after the operation
219 can trap the unwary. See the \fB\s-1WARNINGS\s0\fR section for some common
220 errors.
221 .PP
222 The reason for the auto increment behaviour is to reflect a typical
223 usage of \s-1ASN1\s0 functions: after one structure is encoded or decoded
224 another will processed after it.
225 .SH "EXAMPLES"
226 .IX Header "EXAMPLES"
227 Allocate and encode the \s-1DER\s0 encoding of an X509 structure:
228 .PP
229 .Vb 2
230 \& int len;
231 \& unsigned char *buf, *p;
232 .Ve
233 .Vb 1
234 \& len = i2d_X509(x, NULL);
235 .Ve
236 .Vb 1
237 \& buf = OPENSSL_malloc(len);
238 .Ve
239 .Vb 2
240 \& if (buf == NULL)
241 \&        /* error */
242 .Ve
243 .Vb 1
244 \& p = buf;
245 .Ve
246 .Vb 1
247 \& i2d_X509(x, &p);
248 .Ve
249 If you are using OpenSSL 0.9.7 or later then this can be
250 simplified to:
251 .PP
252 .Vb 2
253 \& int len;
254 \& unsigned char *buf;
255 .Ve
256 .Vb 1
257 \& buf = NULL;
258 .Ve
259 .Vb 1
260 \& len = i2d_X509(x, &buf);
261 .Ve
262 .Vb 2
263 \& if (len < 0)
264 \&        /* error */
265 .Ve
266 Attempt to decode a buffer:
267 .PP
268 .Vb 1
269 \& X509 *x;
270 .Ve
271 .Vb 1
272 \& unsigned char *buf, *p;
273 .Ve
274 .Vb 1
275 \& int len;
276 .Ve
277 .Vb 1
278 \& /* Something to setup buf and len */
279 .Ve
280 .Vb 1
281 \& p = buf;
282 .Ve
283 .Vb 1
284 \& x = d2i_X509(NULL, &p, len);
285 .Ve
286 .Vb 2
287 \& if (x == NULL)
288 \&    /* Some error */
289 .Ve
290 Alternative technique:
291 .PP
292 .Vb 1
293 \& X509 *x;
294 .Ve
295 .Vb 1
296 \& unsigned char *buf, *p;
297 .Ve
298 .Vb 1
299 \& int len;
300 .Ve
301 .Vb 1
302 \& /* Something to setup buf and len */
303 .Ve
304 .Vb 1
305 \& p = buf;
306 .Ve
307 .Vb 1
308 \& x = NULL;
309 .Ve
310 .Vb 2
311 \& if(!d2i_X509(&x, &p, len))
312 \&    /* Some error */
313 .Ve
314 .SH "WARNINGS"
315 .IX Header "WARNINGS"
316 The use of temporary variable is mandatory. A common
317 mistake is to attempt to use a buffer directly as follows:
318 .PP
319 .Vb 2
320 \& int len;
321 \& unsigned char *buf;
322 .Ve
323 .Vb 1
324 \& len = i2d_X509(x, NULL);
325 .Ve
326 .Vb 1
327 \& buf = OPENSSL_malloc(len);
328 .Ve
329 .Vb 2
330 \& if (buf == NULL)
331 \&        /* error */
332 .Ve
333 .Vb 1
334 \& i2d_X509(x, &buf);
335 .Ve
336 .Vb 1
337 \& /* Other stuff ... */
338 .Ve
339 .Vb 1
340 \& OPENSSL_free(buf);
341 .Ve
342 This code will result in \fBbuf\fR apparently containing garbage because
343 it was incremented after the call to point after the data just written.
344 Also \fBbuf\fR will no longer contain the pointer allocated by \fB\f(BIOPENSSL_malloc()\fB\fR
345 and the subsequent call to \fB\f(BIOPENSSL_free()\fB\fR may well crash.
346 .PP
347 The auto allocation feature (setting buf to \s-1NULL\s0) only works on OpenSSL
348 0.9.7 and later. Attempts to use it on earlier versions will typically
349 cause a segmentation violation.
350 .PP
351 Another trap to avoid is misuse of the \fBxp\fR argument to \fB\f(BId2i_X509()\fB\fR:
352 .PP
353 .Vb 1
354 \& X509 *x;
355 .Ve
356 .Vb 2
357 \& if (!d2i_X509(&x, &p, len))
358 \&        /* Some error */
359 .Ve
360 This will probably crash somewhere in \fB\f(BId2i_X509()\fB\fR. The reason for this
361 is that the variable \fBx\fR is uninitialized and an attempt will be made to
362 interpret its (invalid) value as an \fBX509\fR structure, typically causing
363 a segmentation violation. If \fBx\fR is set to \s-1NULL\s0 first then this will not
364 happen.
365 .SH "BUGS"
366 .IX Header "BUGS"
367 In some versions of OpenSSL the \*(L"reuse\*(R" behaviour of \fId2i_X509()\fR when 
368 \&\fB*px\fR is valid is broken and some parts of the reused structure may
369 persist if they are not present in the new one. As a result the use
370 of this \*(L"reuse\*(R" behaviour is strongly discouraged.
371 .PP
372 \&\fIi2d_X509()\fR will not return an error in many versions of OpenSSL,
373 if mandatory fields are not initialized due to a programming error
374 then the encoded structure may contain invalid data or omit the
375 fields entirely and will not be parsed by \fId2i_X509()\fR. This may be
376 fixed in future so code should not assume that \fIi2d_X509()\fR will
377 always succeed.
378 .SH "RETURN VALUES"
379 .IX Header "RETURN VALUES"
380 \&\fId2i_X509()\fR, \fId2i_X509_bio()\fR and \fId2i_X509_fp()\fR return a valid \fBX509\fR structure
381 or \fB\s-1NULL\s0\fR if an error occurs. The error code that can be obtained by
382 ERR_get_error(3). 
383 .PP
384 \&\fIi2d_X509()\fR, \fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR return a the number of bytes
385 successfully encoded or a negative value if an error occurs. The error code
386 can be obtained by ERR_get_error(3). 
387 .PP
388 \&\fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR returns 1 for success and 0 if an error 
389 occurs The error code can be obtained by ERR_get_error(3). 
390 .SH "SEE ALSO"
391 .IX Header "SEE ALSO"
392 ERR_get_error(3)
393 .SH "HISTORY"
394 .IX Header "HISTORY"
395 d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and i2d_X509_fp
396 are available in all versions of SSLeay and OpenSSL.