Merge from vendor branch BIND:
[dragonfly.git] / secure / lib / libcrypto / man / d2i_X509.3
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.14
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "d2i_X509 3"
132 .TH d2i_X509 3 "2007-03-28" "0.9.8e" "OpenSSL"
133 .SH "NAME"
134 d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio,
135 i2d_X509_fp \- X509 encode and decode functions
136 .SH "SYNOPSIS"
137 .IX Header "SYNOPSIS"
138 .Vb 1
139 \& #include <openssl/x509.h>
140 .Ve
141 .PP
142 .Vb 2
143 \& X509 *d2i_X509(X509 **px, const unsigned char **in, int len);
144 \& int i2d_X509(X509 *x, unsigned char **out);
145 .Ve
146 .PP
147 .Vb 2
148 \& X509 *d2i_X509_bio(BIO *bp, X509 **x);
149 \& X509 *d2i_X509_fp(FILE *fp, X509 **x);
150 .Ve
151 .PP
152 .Vb 2
153 \& int i2d_X509_bio(X509 *x, BIO *bp);
154 \& int i2d_X509_fp(X509 *x, FILE *fp);
155 .Ve
156 .SH "DESCRIPTION"
157 .IX Header "DESCRIPTION"
158 The X509 encode and decode routines encode and parse an
159 \&\fBX509\fR structure, which represents an X509 certificate.
160 .PP
161 \&\fId2i_X509()\fR attempts to decode \fBlen\fR bytes at \fB*in\fR. If 
162 successful a pointer to the \fBX509\fR structure is returned. If an error
163 occurred then \fB\s-1NULL\s0\fR is returned. If \fBpx\fR is not \fB\s-1NULL\s0\fR then the
164 returned structure is written to \fB*px\fR. If \fB*px\fR is not \fB\s-1NULL\s0\fR
165 then it is assumed that \fB*px\fR contains a valid \fBX509\fR
166 structure and an attempt is made to reuse it. If the call is
167 successful \fB*in\fR is incremented to the byte following the
168 parsed data.
169 .PP
170 \&\fIi2d_X509()\fR encodes the structure pointed to by \fBx\fR into \s-1DER\s0 format.
171 If \fBout\fR is not \fB\s-1NULL\s0\fR is writes the \s-1DER\s0 encoded data to the buffer
172 at \fB*out\fR, and increments it to point after the data just written.
173 If the return value is negative an error occurred, otherwise it
174 returns the length of the encoded data. 
175 .PP
176 For OpenSSL 0.9.7 and later if \fB*out\fR is \fB\s-1NULL\s0\fR memory will be
177 allocated for a buffer and the encoded data written to it. In this
178 case \fB*out\fR is not incremented and it points to the start of the
179 data just written.
180 .PP
181 \&\fId2i_X509_bio()\fR is similar to \fId2i_X509()\fR except it attempts
182 to parse data from \s-1BIO\s0 \fBbp\fR.
183 .PP
184 \&\fId2i_X509_fp()\fR is similar to \fId2i_X509()\fR except it attempts
185 to parse data from \s-1FILE\s0 pointer \fBfp\fR.
186 .PP
187 \&\fIi2d_X509_bio()\fR is similar to \fIi2d_X509()\fR except it writes
188 the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it
189 returns 1 for success and 0 for failure.
190 .PP
191 \&\fIi2d_X509_fp()\fR is similar to \fIi2d_X509()\fR except it writes
192 the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it
193 returns 1 for success and 0 for failure.
194 .SH "NOTES"
195 .IX Header "NOTES"
196 The letters \fBi\fR and \fBd\fR in for example \fBi2d_X509\fR stand for
197 \&\*(L"internal\*(R" (that is an internal C structure) and \*(L"\s-1DER\s0\*(R". So that
198 \&\fBi2d_X509\fR converts from internal to \s-1DER\s0.
199 .PP
200 The functions can also understand \fB\s-1BER\s0\fR forms.
201 .PP
202 The actual X509 structure passed to \fIi2d_X509()\fR must be a valid
203 populated \fBX509\fR structure it can \fBnot\fR simply be fed with an
204 empty structure such as that returned by \fIX509_new()\fR.
205 .PP
206 The encoded data is in binary form and may contain embedded zeroes.
207 Therefore any \s-1FILE\s0 pointers or BIOs should be opened in binary mode.
208 Functions such as \fB\f(BIstrlen()\fB\fR will \fBnot\fR return the correct length
209 of the encoded structure.
210 .PP
211 The ways that \fB*in\fR and \fB*out\fR are incremented after the operation
212 can trap the unwary. See the \fB\s-1WARNINGS\s0\fR section for some common
213 errors.
214 .PP
215 The reason for the auto increment behaviour is to reflect a typical
216 usage of \s-1ASN1\s0 functions: after one structure is encoded or decoded
217 another will processed after it.
218 .SH "EXAMPLES"
219 .IX Header "EXAMPLES"
220 Allocate and encode the \s-1DER\s0 encoding of an X509 structure:
221 .PP
222 .Vb 2
223 \& int len;
224 \& unsigned char *buf, *p;
225 .Ve
226 .PP
227 .Vb 1
228 \& len = i2d_X509(x, NULL);
229 .Ve
230 .PP
231 .Vb 1
232 \& buf = OPENSSL_malloc(len);
233 .Ve
234 .PP
235 .Vb 2
236 \& if (buf == NULL)
237 \&        /* error */
238 .Ve
239 .PP
240 .Vb 1
241 \& p = buf;
242 .Ve
243 .PP
244 .Vb 1
245 \& i2d_X509(x, &p);
246 .Ve
247 .PP
248 If you are using OpenSSL 0.9.7 or later then this can be
249 simplified to:
250 .PP
251 .Vb 2
252 \& int len;
253 \& unsigned char *buf;
254 .Ve
255 .PP
256 .Vb 1
257 \& buf = NULL;
258 .Ve
259 .PP
260 .Vb 1
261 \& len = i2d_X509(x, &buf);
262 .Ve
263 .PP
264 .Vb 2
265 \& if (len < 0)
266 \&        /* error */
267 .Ve
268 .PP
269 Attempt to decode a buffer:
270 .PP
271 .Vb 1
272 \& X509 *x;
273 .Ve
274 .PP
275 .Vb 1
276 \& unsigned char *buf, *p;
277 .Ve
278 .PP
279 .Vb 1
280 \& int len;
281 .Ve
282 .PP
283 .Vb 1
284 \& /* Something to setup buf and len */
285 .Ve
286 .PP
287 .Vb 1
288 \& p = buf;
289 .Ve
290 .PP
291 .Vb 1
292 \& x = d2i_X509(NULL, &p, len);
293 .Ve
294 .PP
295 .Vb 2
296 \& if (x == NULL)
297 \&    /* Some error */
298 .Ve
299 .PP
300 Alternative technique:
301 .PP
302 .Vb 1
303 \& X509 *x;
304 .Ve
305 .PP
306 .Vb 1
307 \& unsigned char *buf, *p;
308 .Ve
309 .PP
310 .Vb 1
311 \& int len;
312 .Ve
313 .PP
314 .Vb 1
315 \& /* Something to setup buf and len */
316 .Ve
317 .PP
318 .Vb 1
319 \& p = buf;
320 .Ve
321 .PP
322 .Vb 1
323 \& x = NULL;
324 .Ve
325 .PP
326 .Vb 2
327 \& if(!d2i_X509(&x, &p, len))
328 \&    /* Some error */
329 .Ve
330 .SH "WARNINGS"
331 .IX Header "WARNINGS"
332 The use of temporary variable is mandatory. A common
333 mistake is to attempt to use a buffer directly as follows:
334 .PP
335 .Vb 2
336 \& int len;
337 \& unsigned char *buf;
338 .Ve
339 .PP
340 .Vb 1
341 \& len = i2d_X509(x, NULL);
342 .Ve
343 .PP
344 .Vb 1
345 \& buf = OPENSSL_malloc(len);
346 .Ve
347 .PP
348 .Vb 2
349 \& if (buf == NULL)
350 \&        /* error */
351 .Ve
352 .PP
353 .Vb 1
354 \& i2d_X509(x, &buf);
355 .Ve
356 .PP
357 .Vb 1
358 \& /* Other stuff ... */
359 .Ve
360 .PP
361 .Vb 1
362 \& OPENSSL_free(buf);
363 .Ve
364 .PP
365 This code will result in \fBbuf\fR apparently containing garbage because
366 it was incremented after the call to point after the data just written.
367 Also \fBbuf\fR will no longer contain the pointer allocated by \fB\f(BIOPENSSL_malloc()\fB\fR
368 and the subsequent call to \fB\f(BIOPENSSL_free()\fB\fR may well crash.
369 .PP
370 The auto allocation feature (setting buf to \s-1NULL\s0) only works on OpenSSL
371 0.9.7 and later. Attempts to use it on earlier versions will typically
372 cause a segmentation violation.
373 .PP
374 Another trap to avoid is misuse of the \fBxp\fR argument to \fB\f(BId2i_X509()\fB\fR:
375 .PP
376 .Vb 1
377 \& X509 *x;
378 .Ve
379 .PP
380 .Vb 2
381 \& if (!d2i_X509(&x, &p, len))
382 \&        /* Some error */
383 .Ve
384 .PP
385 This will probably crash somewhere in \fB\f(BId2i_X509()\fB\fR. The reason for this
386 is that the variable \fBx\fR is uninitialized and an attempt will be made to
387 interpret its (invalid) value as an \fBX509\fR structure, typically causing
388 a segmentation violation. If \fBx\fR is set to \s-1NULL\s0 first then this will not
389 happen.
390 .SH "BUGS"
391 .IX Header "BUGS"
392 In some versions of OpenSSL the \*(L"reuse\*(R" behaviour of \fId2i_X509()\fR when 
393 \&\fB*px\fR is valid is broken and some parts of the reused structure may
394 persist if they are not present in the new one. As a result the use
395 of this \*(L"reuse\*(R" behaviour is strongly discouraged.
396 .PP
397 \&\fIi2d_X509()\fR will not return an error in many versions of OpenSSL,
398 if mandatory fields are not initialized due to a programming error
399 then the encoded structure may contain invalid data or omit the
400 fields entirely and will not be parsed by \fId2i_X509()\fR. This may be
401 fixed in future so code should not assume that \fIi2d_X509()\fR will
402 always succeed.
403 .SH "RETURN VALUES"
404 .IX Header "RETURN VALUES"
405 \&\fId2i_X509()\fR, \fId2i_X509_bio()\fR and \fId2i_X509_fp()\fR return a valid \fBX509\fR structure
406 or \fB\s-1NULL\s0\fR if an error occurs. The error code that can be obtained by
407 \&\fIERR_get_error\fR\|(3). 
408 .PP
409 \&\fIi2d_X509()\fR, \fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR return a the number of bytes
410 successfully encoded or a negative value if an error occurs. The error code
411 can be obtained by \fIERR_get_error\fR\|(3). 
412 .PP
413 \&\fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR returns 1 for success and 0 if an error 
414 occurs The error code can be obtained by \fIERR_get_error\fR\|(3). 
415 .SH "SEE ALSO"
416 .IX Header "SEE ALSO"
417 \&\fIERR_get_error\fR\|(3)
418 .SH "HISTORY"
419 .IX Header "HISTORY"
420 d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and i2d_X509_fp
421 are available in all versions of SSLeay and OpenSSL.