import libcrypto (LibreSSL 2.5.2)
[unleashed.git] / lib / libcrypto / man / ASN1_item_d2i.3
blob4bdaf4bad405be96e4b459bf4d117f9548a090a7
1 .\"     $OpenBSD: ASN1_item_d2i.3,v 1.4 2017/01/03 23:56:50 schwarze Exp $
2 .\"     OpenSSL doc/man3/d2i_X509.pod b97fdb57 Nov 11 09:33:09 2016 +0100
3 .\"
4 .\" This file is a derived work.
5 .\" The changes are covered by the following Copyright and license:
6 .\"
7 .\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
8 .\"
9 .\" Permission to use, copy, modify, and distribute this software for any
10 .\" purpose with or without fee is hereby granted, provided that the above
11 .\" copyright notice and this permission notice appear in all copies.
12 .\"
13 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 .\"
21 .\" The original file was written by Dr. Stephen Henson <steve@openssl.org>.
22 .\" Copyright (c) 2002, 2003, 2015 The OpenSSL Project.  All rights reserved.
23 .\"
24 .\" Redistribution and use in source and binary forms, with or without
25 .\" modification, are permitted provided that the following conditions
26 .\" are met:
27 .\"
28 .\" 1. Redistributions of source code must retain the above copyright
29 .\"    notice, this list of conditions and the following disclaimer.
30 .\"
31 .\" 2. Redistributions in binary form must reproduce the above copyright
32 .\"    notice, this list of conditions and the following disclaimer in
33 .\"    the documentation and/or other materials provided with the
34 .\"    distribution.
35 .\"
36 .\" 3. All advertising materials mentioning features or use of this
37 .\"    software must display the following acknowledgment:
38 .\"    "This product includes software developed by the OpenSSL Project
39 .\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
40 .\"
41 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
42 .\"    endorse or promote products derived from this software without
43 .\"    prior written permission. For written permission, please contact
44 .\"    openssl-core@openssl.org.
45 .\"
46 .\" 5. Products derived from this software may not be called "OpenSSL"
47 .\"    nor may "OpenSSL" appear in their names without prior written
48 .\"    permission of the OpenSSL Project.
49 .\"
50 .\" 6. Redistributions of any form whatsoever must retain the following
51 .\"    acknowledgment:
52 .\"    "This product includes software developed by the OpenSSL Project
53 .\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
54 .\"
55 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
56 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
59 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
62 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
66 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
67 .\"
68 .Dd $Mdocdate: January 3 2017 $
69 .Dt ASN1_ITEM_D2I 3
70 .Os
71 .Sh NAME
72 .Nm ASN1_item_d2i ,
73 .Nm ASN1_item_d2i_bio ,
74 .Nm ASN1_item_d2i_fp ,
75 .Nm d2i_ASN1_TYPE ,
76 .Nm ASN1_item_i2d ,
77 .Nm ASN1_item_i2d_bio ,
78 .Nm ASN1_item_i2d_fp ,
79 .Nm i2d_ASN1_TYPE ,
80 .Nm ASN1_item_dup ,
81 .Nm ASN1_item_print
82 .Nd decode and encode ASN.1 objects
83 .Sh SYNOPSIS
84 .In openssl/asn1.h
85 .Ft ASN1_VALUE *
86 .Fo ASN1_item_d2i
87 .Fa "ASN1_VALUE **val_out"
88 .Fa "const unsigned char **der_in"
89 .Fa "long length"
90 .Fa "const ASN1_ITEM *it"
91 .Fc
92 .Ft void *
93 .Fo ASN1_item_d2i_bio
94 .Fa "const ASN1_ITEM *it"
95 .Fa "BIO *in_bio"
96 .Fa "void *val_out"
97 .Fc
98 .Ft void *
99 .Fo ASN1_item_d2i_fp
100 .Fa "const ASN1_ITEM *it"
101 .Fa "FILE *in_fp"
102 .Fa "void *val_out"
104 .Ft ASN1_TYPE *
105 .Fo d2i_ASN1_TYPE
106 .Fa "ASN1_TYPE **val_out"
107 .Fa "const unsigned char **der_in"
108 .Fa "long length"
110 .Ft int
111 .Fo ASN1_item_i2d
112 .Fa "ASN1_VALUE *val_in"
113 .Fa "unsigned char **der_out"
114 .Fa "const ASN1_ITEM *it"
116 .Ft int
117 .Fo ASN1_item_i2d_bio
118 .Fa "const ASN1_ITEM *it"
119 .Fa "BIO *out_bio"
120 .Fa "void *val_in"
122 .Ft int
123 .Fo ASN1_item_i2d_fp
124 .Fa "const ASN1_ITEM *it"
125 .Fa "FILE *out_fp"
126 .Fa "void *val_in"
128 .Ft int
129 .Fo i2d_ASN1_TYPE
130 .Fa "ASN1_TYPE *val_in"
131 .Fa "unsigned char **der_out"
133 .Ft void *
134 .Fo ASN1_item_dup
135 .Fa "const ASN1_ITEM *it"
136 .Fa "void *val_in"
138 .Ft int
139 .Fo ASN1_item_print
140 .Fa "BIO *out_bio"
141 .Fa "ASN1_VALUE *val_in"
142 .Fa "int indent"
143 .Fa "const ASN1_ITEM *it"
144 .Fa "const ASN1_PCTX *pctx"
146 .Sh DESCRIPTION
147 These functions convert ASN.1 values from their BER encoding to
148 internal C structures
149 .Pq Dq d2i
150 and vice versa
151 .Pq Dq i2d .
152 Unlike the C structures which contain pointers to sub-objects, BER
153 is a serialized encoding, suitable for transfer over the network
154 and for storage in a file.
156 .Fn ASN1_item_d2i
157 interpretes
158 .Pf * Fa der_in
159 as a DER- or BER-encoded byte array and decodes one value of type
160 .Fa it
161 represented by up to
162 .Fa length
163 bytes.
164 If successful,
165 .Pf * Fa der_in
166 is advanced to the byte following the parsed data.
168 If decoding succeeds and
169 .Fa val_out
171 .Pf * Fa val_out
173 .Dv NULL ,
174 a new object is allocated.
176 If decoding succeeds and
177 .Pf * Fa val_out
178 is not
179 .Dv NULL ,
180 it is assumed to point to a valid populated object and an attempt
181 is made to reuse it.
182 It must not be an empty structure such as one returned by
183 .Xr ASN1_item_new 3
184 or by one of the various type-specific
185 .Fn *_new
186 functions.
187 This
188 .Dq reuse
189 capability is present for backward compatibility, but its use is
190 strongly discouraged; see the
191 .Sx BUGS
192 section below.
194 .Fn ASN1_item_d2i_bio
196 .Fn ASN1_item_d2i_fp
197 are similar to
198 .Fn ASN1_item_d2i
199 except that they read from a
200 .Vt BIO
202 .Vt FILE ,
203 respectively.
205 .Fn d2i_ASN1_TYPE
206 is similar to
207 .Fn ASN1_item_d2i
208 except that it does not require a desired type to be specified by
209 the user, but instead returns an
210 .Vt ASN1_TYPE
211 wrapper object containing both the type and the value found in the input.
213 .Fn ASN1_item_i2d
214 encodes the object pointed to by
215 .Fa val_in
216 into DER format.
219 .Pf * Fa der_out
220 is not
221 .Dv NULL ,
222 it writes the DER-encoded data to the buffer at
223 .Pf * Fa der_out
224 and increments it to point after the data just written.
225 In this case, it is the responsibility of the user to make sure
226 that the buffer pointed to by
227 .Pf * Fa der_out
228 is long enough, such that no buffer owerflow can occur.
231 .Pf * Fa der_out
233 .Dv NULL ,
234 memory is allocated for a buffer, and
235 .Pf * Fa der_out
236 is not incremented, but points to the start of the data just written.
239 .Fa der_out
241 .Dv NULL ,
242 the encoded bytes are not written anywhere but discarded.
244 .Fa val_in
245 objects of variable encoding size, this is sometimes used to first
246 find the number of bytes that will be written.
247 Then, a sufficient amount of memory is allocated before calling
248 .Fn ASN1_item_i2d
249 again.
250 This explicit double-call technique is often not needed because the
251 auto-allocation technique described in the previous paragraph can
252 be used.
254 .Fn ASN1_item_i2d_bio
256 .Fn ASN1_item_i2d_fp
257 are similar to
258 .Fn ASN1_item_i2d
259 except that they write to a
260 .Vt BIO
262 .Vt FILE ,
263 respectively.
265 .Fn i2d_ASN1_TYPE
266 is similar to
267 .Fn ASN1_item_i2d
268 except that the type and the value are not provided separately,
269 but in the form of a single
270 .Vt ASN1_TYPE
271 object.
273 .Fn ASN1_item_dup
274 creates a deep copy of
275 .Fa val_in
276 by calling
277 .Fn ASN1_item_i2d
279 .Fn ASN1_item_d2i .
280 .Sh RETURN VALUES
281 If successful,
282 .Fn ASN1_item_d2i ,
283 .Fn ASN1_item_d2i_bio ,
284 .Fn ASN1_item_d2i_fp ,
286 .Fn d2i_ASN1_TYPE
287 return a pointer to the decoded ASN.1 value.
288 In addition, if
289 .Fa val_out
290 is not
291 .Dv NULL ,
292 the pointer is also written to
293 .Pf * Fa val_out .
294 If an error occurs,
295 .Dv NULL
296 is returned.
298 .Fn ASN1_item_i2d
300 .Fn i2d_ASN1_TYPE
301 return the number of bytes written
302 or a negative value if an error occurs.
304 .Fn ASN1_item_i2d_bio
306 .Fn ASN1_item_i2d_fp
307 return 1 for success or 0 for failure.
309 .Fn ASN1_item_dup
310 returns the new
311 .Vt ASN1_VALUE
312 object or
313 .Dv NULL
314 if an error occurs.
315 .Sh EXAMPLES
316 Many type-specific wrapper functions exist.
317 Using those wrappers is recommended in application code
318 because it restores part of the type safety that the low-level
319 interfaces using
320 .Vt ASN1_VALUE
321 lack.
323 For example, to allocate a buffer and write the DER encoding of an
324 .Vt X509
325 object into it:
326 .Bd -literal -offset indent
327 X509            *x;
328 unsigned char   *buf;
329 int              len;
331 buf = NULL;
332 len = i2d_X509(x, &buf);
333 if (len < 0)
334         /* error */
337 Attempt to decode a buffer:
338 .Bd -literal -offset indent
339 X509            *x;
340 unsigned char   *buf, *p;
341 int              len;
343 /* Set up buf and len to point to the input buffer. */
344 p = buf;
345 x = d2i_X509(NULL, &p, len);
346 if (x == NULL)
347         /* error */
350 Equivalent technique:
351 .Bd -literal -offset indent
352 X509            *x;
353 unsigned char   *buf, *p;
354 int              len;
356 /* Set up buf and len to point to the input buffer. */
357 p = buf;
358 x = NULL;
360 if (d2i_X509(&x, &p, len) == NULL)
361         /* error */
363 .Sh SEE ALSO
364 .Xr ASN1_item_new 3 ,
365 .Xr ASN1_TYPE_new 3
366 .Sh CAVEATS
367 If the type described by
368 .Fa it
369 fails to match the true type of
370 .Fa val_in
372 .Pf * Fa val_out ,
373 buffer overflows and segmentation faults are likely to occur.
374 For more details about why the type
375 .Vt ASN1_VALUE
376 constitutes dangerous user interface design, see
377 .Xr ASN1_item_new 3 .
379 The encoded data is in binary form and may contain embedded NUL bytes.
380 Functions such as
381 .Xr strlen 3
382 will not return the correct length of the encoded data.
384 While the way that
385 .Pf * Fa der_in
387 .Pf * Fa der_out
388 are incremented after the operation supports the typical usage
389 patterns of reading or writing one object after another, this
390 behaviour can trap the unwary.
392 Using a temporary pointer into the buffer is mandatory.
393 A common mistake is to attempt to use a buffer directly as follows:
394 .Bd -literal -offset indent
395 X509            *x;
396 unsigned char   *buf;
397 int              len;
399 len = i2d_X509(x, NULL);
400 buf = malloc(len);
401 i2d_X509(x, &buf);
402 /* do something with buf[] */
403 free(buf);
406 This code will result in
407 .Va buf
408 apparently containing garbage because it was incremented during
409 .Fn i2d_X509
410 to point after the data just written.
411 Also
412 .Va buf
413 will no longer contain the pointer allocated by
414 .Xr malloc 3
415 and the subsequent call to
416 .Xr free 3
417 is likely to crash.
419 Another trap to avoid is misuse of the
420 .Fa val_out
421 argument:
422 .Bd -literal -offset indent
423 X509            *x;
425 if (d2i_X509(&x, &p, len) == NULL)
426         /* error */
429 This will probably crash somewhere in
430 .Fn d2i_X509
431 because
432 .Va x
433 is uninitialized and an attempt will be made to interpret its invalid
434 content as an
435 .Vt X509
436 object, typically causing a segmentation violation.
438 .Va x
439 is set to
440 .Dv NULL
441 first, then this will not happen.
442 .Sh BUGS
443 If the
444 .Dq reuse
445 capability is used, a valid object is passed in via
446 .Pf * Fa val_out ,
447 and an error occurs, then the object is not freed and may be left
448 in an invalid or inconsistent state.
450 In some versions of OpenSSL, the
451 .Dq reuse
452 behaviour is broken such that some parts of the reused object may
453 persist if they are not present in the new one.
455 In many versions of OpenSSL,
456 .Fn ASN1_item_i2d
457 will not return an error if mandatory fields are not initialized
458 due to a programming error.
459 In that case, the encoded structure may contain invalid data and
460 some fields may be missing entirely, such that trying to parse it
461 with
462 .Fn ASN1_item_d2i
463 may fail.
465 Any function which encodes an object may return a stale encoding
466 if the object has been modified after deserialization or previous
467 serialization.
468 This is because some objects cache the encoding for efficiency reasons.