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
4 .\" This file is a derived work.
5 .\" The changes are covered by the following Copyright and license:
7 .\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
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.
24 .\" Redistribution and use in source and binary forms, with or without
25 .\" modification, are permitted provided that the following conditions
28 .\" 1. Redistributions of source code must retain the above copyright
29 .\" notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
50 .\" 6. Redistributions of any form whatsoever must retain the following
52 .\" "This product includes software developed by the OpenSSL Project
53 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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.
68 .Dd $Mdocdate: January 3 2017 $
73 .Nm ASN1_item_d2i_bio ,
74 .Nm ASN1_item_d2i_fp ,
77 .Nm ASN1_item_i2d_bio ,
78 .Nm ASN1_item_i2d_fp ,
82 .Nd decode and encode ASN.1 objects
87 .Fa "ASN1_VALUE **val_out"
88 .Fa "const unsigned char **der_in"
90 .Fa "const ASN1_ITEM *it"
94 .Fa "const ASN1_ITEM *it"
100 .Fa "const ASN1_ITEM *it"
106 .Fa "ASN1_TYPE **val_out"
107 .Fa "const unsigned char **der_in"
112 .Fa "ASN1_VALUE *val_in"
113 .Fa "unsigned char **der_out"
114 .Fa "const ASN1_ITEM *it"
117 .Fo ASN1_item_i2d_bio
118 .Fa "const ASN1_ITEM *it"
124 .Fa "const ASN1_ITEM *it"
130 .Fa "ASN1_TYPE *val_in"
131 .Fa "unsigned char **der_out"
135 .Fa "const ASN1_ITEM *it"
141 .Fa "ASN1_VALUE *val_in"
143 .Fa "const ASN1_ITEM *it"
144 .Fa "const ASN1_PCTX *pctx"
147 These functions convert ASN.1 values from their BER encoding to
148 internal C structures
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.
159 as a DER- or BER-encoded byte array and decodes one value of type
166 is advanced to the byte following the parsed data.
168 If decoding succeeds and
174 a new object is allocated.
176 If decoding succeeds and
180 it is assumed to point to a valid populated object and an attempt
182 It must not be an empty structure such as one returned by
184 or by one of the various type-specific
189 capability is present for backward compatibility, but its use is
190 strongly discouraged; see the
194 .Fn ASN1_item_d2i_bio
199 except that they read from a
208 except that it does not require a desired type to be specified by
209 the user, but instead returns an
211 wrapper object containing both the type and the value found in the input.
214 encodes the object pointed to by
222 it writes the DER-encoded data to the buffer at
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
228 is long enough, such that no buffer owerflow can occur.
234 memory is allocated for a buffer, and
236 is not incremented, but points to the start of the data just written.
242 the encoded bytes are not written anywhere but discarded.
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
250 This explicit double-call technique is often not needed because the
251 auto-allocation technique described in the previous paragraph can
254 .Fn ASN1_item_i2d_bio
259 except that they write to a
268 except that the type and the value are not provided separately,
269 but in the form of a single
274 creates a deep copy of
283 .Fn ASN1_item_d2i_bio ,
284 .Fn ASN1_item_d2i_fp ,
287 return a pointer to the decoded ASN.1 value.
292 the pointer is also written to
301 return the number of bytes written
302 or a negative value if an error occurs.
304 .Fn ASN1_item_i2d_bio
307 return 1 for success or 0 for failure.
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
323 For example, to allocate a buffer and write the DER encoding of an
326 .Bd -literal -offset indent
332 len = i2d_X509(x, &buf);
337 Attempt to decode a buffer:
338 .Bd -literal -offset indent
340 unsigned char *buf, *p;
343 /* Set up buf and len to point to the input buffer. */
345 x = d2i_X509(NULL, &p, len);
350 Equivalent technique:
351 .Bd -literal -offset indent
353 unsigned char *buf, *p;
356 /* Set up buf and len to point to the input buffer. */
360 if (d2i_X509(&x, &p, len) == NULL)
364 .Xr ASN1_item_new 3 ,
367 If the type described by
369 fails to match the true type of
373 buffer overflows and segmentation faults are likely to occur.
374 For more details about why the type
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.
382 will not return the correct length of the encoded data.
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
399 len = i2d_X509(x, NULL);
402 /* do something with buf[] */
406 This code will result in
408 apparently containing garbage because it was incremented during
410 to point after the data just written.
413 will no longer contain the pointer allocated by
415 and the subsequent call to
419 Another trap to avoid is misuse of the
422 .Bd -literal -offset indent
425 if (d2i_X509(&x, &p, len) == NULL)
429 This will probably crash somewhere in
433 is uninitialized and an attempt will be made to interpret its invalid
436 object, typically causing a segmentation violation.
441 first, then this will not happen.
445 capability is used, a valid object is passed in via
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
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,
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
465 Any function which encodes an object may return a stale encoding
466 if the object has been modified after deserialization or previous
468 This is because some objects cache the encoding for efficiency reasons.