summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/man/ASN1_item_d2i.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/man/ASN1_item_d2i.3')
-rw-r--r--src/lib/libcrypto/man/ASN1_item_d2i.3490
1 files changed, 0 insertions, 490 deletions
diff --git a/src/lib/libcrypto/man/ASN1_item_d2i.3 b/src/lib/libcrypto/man/ASN1_item_d2i.3
deleted file mode 100644
index 140ea6f1ba..0000000000
--- a/src/lib/libcrypto/man/ASN1_item_d2i.3
+++ /dev/null
@@ -1,490 +0,0 @@
1.\" $OpenBSD: ASN1_item_d2i.3,v 1.10 2021/07/11 15:30:21 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: July 11 2021 $
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"
103.Fc
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"
109.Fc
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"
115.Fc
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"
121.Fc
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"
127.Fc
128.Ft int
129.Fo i2d_ASN1_TYPE
130.Fa "ASN1_TYPE *val_in"
131.Fa "unsigned char **der_out"
132.Fc
133.Ft void *
134.Fo ASN1_item_dup
135.Fa "const ASN1_ITEM *it"
136.Fa "void *val_in"
137.Fc
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"
145.Fc
146.Sh DESCRIPTION
147These functions convert ASN.1 values from their BER encoding to
148internal C structures
149.Pq Dq d2i
150and vice versa
151.Pq Dq i2d .
152Unlike the C structures which contain pointers to sub-objects, BER
153is a serialized encoding, suitable for transfer over the network
154and for storage in a file.
155.Pp
156.Fn ASN1_item_d2i
157interprets
158.Pf * Fa der_in
159as a DER- or BER-encoded byte array and decodes one value of type
160.Fa it
161represented by up to
162.Fa length
163bytes.
164If successful,
165.Pf * Fa der_in
166is advanced to the byte following the parsed data.
167.Pp
168If decoding succeeds and
169.Fa val_out
170or
171.Pf * Fa val_out
172is
173.Dv NULL ,
174a new object is allocated.
175.Pp
176If decoding succeeds and
177.Pf * Fa val_out
178is not
179.Dv NULL ,
180it is assumed to point to a valid populated object and an attempt
181is made to reuse it.
182It must not be an empty structure such as one returned by
183.Xr ASN1_item_new 3
184or by one of the various type-specific
185.Fn *_new
186functions.
187This
188.Dq reuse
189capability is present for backward compatibility, but its use is
190strongly discouraged; see the
191.Sx BUGS
192section below.
193.Pp
194.Fn ASN1_item_d2i_bio
195and
196.Fn ASN1_item_d2i_fp
197are similar to
198.Fn ASN1_item_d2i
199except that they read from a
200.Vt BIO
201or
202.Vt FILE ,
203respectively.
204.Pp
205.Fn d2i_ASN1_TYPE
206is similar to
207.Fn ASN1_item_d2i
208except that it does not require a desired type to be specified by
209the user, but instead returns an
210.Vt ASN1_TYPE
211wrapper object containing both the type and the value found in the input.
212.Pp
213.Fn ASN1_item_i2d
214encodes the object pointed to by
215.Fa val_in
216into DER format.
217.Pp
218If
219.Pf * Fa der_out
220is not
221.Dv NULL ,
222it writes the DER-encoded data to the buffer at
223.Pf * Fa der_out
224and increments it to point after the data just written.
225In this case, it is the responsibility of the user to make sure
226that the buffer pointed to by
227.Pf * Fa der_out
228is long enough, such that no buffer overflow can occur.
229.Pp
230If
231.Pf * Fa der_out
232is
233.Dv NULL ,
234memory is allocated for a buffer, and
235.Pf * Fa der_out
236is not incremented, but points to the start of the data just written.
237.Pp
238If
239.Fa der_out
240is
241.Dv NULL ,
242the encoded bytes are not written anywhere but discarded.
243For
244.Fa val_in
245objects of variable encoding size, this is sometimes used to first
246find the number of bytes that will be written.
247Then, a sufficient amount of memory is allocated before calling
248.Fn ASN1_item_i2d
249again.
250This explicit double-call technique is often not needed because the
251auto-allocation technique described in the previous paragraph can
252be used.
253.Pp
254.Fn ASN1_item_i2d_bio
255and
256.Fn ASN1_item_i2d_fp
257are similar to
258.Fn ASN1_item_i2d
259except that they write to a
260.Vt BIO
261or
262.Vt FILE ,
263respectively.
264.Pp
265.Fn i2d_ASN1_TYPE
266is similar to
267.Fn ASN1_item_i2d
268except that the type and the value are not provided separately,
269but in the form of a single
270.Vt ASN1_TYPE
271object.
272.Pp
273.Fn ASN1_item_dup
274creates a deep copy of
275.Fa val_in
276by calling
277.Fn ASN1_item_i2d
278and
279.Fn ASN1_item_d2i .
280.Sh RETURN VALUES
281If successful,
282.Fn ASN1_item_d2i ,
283.Fn ASN1_item_d2i_bio ,
284.Fn ASN1_item_d2i_fp ,
285and
286.Fn d2i_ASN1_TYPE
287return a pointer to the decoded ASN.1 value.
288In addition, if
289.Fa val_out
290is not
291.Dv NULL ,
292the pointer is also written to
293.Pf * Fa val_out .
294If an error occurs,
295.Dv NULL
296is returned.
297.Pp
298.Fn ASN1_item_i2d
299and
300.Fn i2d_ASN1_TYPE
301return the number of bytes written
302or a negative value if an error occurs.
303.Pp
304.Fn ASN1_item_i2d_bio
305and
306.Fn ASN1_item_i2d_fp
307return 1 for success or 0 for failure.
308.Pp
309.Fn ASN1_item_dup
310returns the new
311.Vt ASN1_VALUE
312object or
313.Dv NULL
314if an error occurs.
315.Sh EXAMPLES
316Many type-specific wrapper functions exist.
317Using those wrappers is recommended in application code
318because it restores part of the type safety that the low-level
319interfaces using
320.Vt ASN1_VALUE
321lack.
322.Pp
323For example, to allocate a buffer and write the DER encoding of an
324.Vt X509
325object into it:
326.Bd -literal -offset indent
327X509 *x;
328unsigned char *buf;
329int len;
330
331buf = NULL;
332len = i2d_X509(x, &buf);
333if (len < 0)
334 /* error */
335.Ed
336.Pp
337Attempt to decode a buffer:
338.Bd -literal -offset indent
339X509 *x;
340unsigned char *buf, *p;
341int len;
342
343/* Set up buf and len to point to the input buffer. */
344p = buf;
345x = d2i_X509(NULL, &p, len);
346if (x == NULL)
347 /* error */
348.Ed
349.Pp
350Equivalent technique:
351.Bd -literal -offset indent
352X509 *x;
353unsigned char *buf, *p;
354int len;
355
356/* Set up buf and len to point to the input buffer. */
357p = buf;
358x = NULL;
359
360if (d2i_X509(&x, &p, len) == NULL)
361 /* error */
362.Ed
363.Sh SEE ALSO
364.Xr ASN1_get_object 3 ,
365.Xr ASN1_item_new 3 ,
366.Xr ASN1_TYPE_new 3
367.Sh HISTORY
368.Fn d2i_ASN1_TYPE
369and
370.Fn i2d_ASN1_TYPE
371first appeared in SSLeay 0.5.1 and have been available since
372.Ox 2.4 .
373.Pp
374.Fn ASN1_item_d2i ,
375.Fn ASN1_item_d2i_bio ,
376.Fn ASN1_item_d2i_fp ,
377.Fn ASN1_item_i2d ,
378.Fn ASN1_item_i2d_bio ,
379.Fn ASN1_item_i2d_fp ,
380and
381.Fn ASN1_item_dup
382first appeared in OpenSSL 0.9.7 and have been available since
383.Ox 3.2 .
384.Pp
385.Fn ASN1_item_print
386first appeared in OpenSSL 1.0.0 and has been available since
387.Ox 4.9 .
388.Sh CAVEATS
389If the type described by
390.Fa it
391fails to match the true type of
392.Fa val_in
393or
394.Pf * Fa val_out ,
395buffer overflows and segmentation faults are likely to occur.
396For more details about why the type
397.Vt ASN1_VALUE
398constitutes dangerous user interface design, see
399.Xr ASN1_item_new 3 .
400.Pp
401The encoded data is in binary form and may contain embedded NUL bytes.
402Functions such as
403.Xr strlen 3
404will not return the correct length of the encoded data.
405.Pp
406While the way that
407.Pf * Fa der_in
408and
409.Pf * Fa der_out
410are incremented after the operation supports the typical usage
411patterns of reading or writing one object after another, this
412behaviour can trap the unwary.
413.Pp
414Using a temporary pointer into the buffer is mandatory.
415A common mistake is to attempt to use a buffer directly as follows:
416.Bd -literal -offset indent
417X509 *x;
418unsigned char *buf;
419int len;
420
421len = i2d_X509(x, NULL);
422buf = malloc(len);
423i2d_X509(x, &buf);
424/* do something with buf[] */
425free(buf);
426.Ed
427.Pp
428This code will result in
429.Va buf
430apparently containing garbage because it was incremented during
431.Fn i2d_X509
432to point after the data just written.
433Also
434.Va buf
435will no longer contain the pointer allocated by
436.Xr malloc 3
437and the subsequent call to
438.Xr free 3
439is likely to crash.
440.Pp
441Another trap to avoid is misuse of the
442.Fa val_out
443argument:
444.Bd -literal -offset indent
445X509 *x;
446
447if (d2i_X509(&x, &p, len) == NULL)
448 /* error */
449.Ed
450.Pp
451This will probably crash somewhere in
452.Fn d2i_X509
453because
454.Va x
455is uninitialized and an attempt will be made to interpret its invalid
456content as an
457.Vt X509
458object, typically causing a segmentation violation.
459If
460.Va x
461is set to
462.Dv NULL
463first, then this will not happen.
464.Sh BUGS
465If the
466.Dq reuse
467capability is used, a valid object is passed in via
468.Pf * Fa val_out ,
469and an error occurs, then the object is not freed and may be left
470in an invalid or inconsistent state.
471.Pp
472In some versions of OpenSSL, the
473.Dq reuse
474behaviour is broken such that some parts of the reused object may
475persist if they are not present in the new one.
476.Pp
477In many versions of OpenSSL,
478.Fn ASN1_item_i2d
479will not return an error if mandatory fields are not initialized
480due to a programming error.
481In that case, the encoded structure may contain invalid data and
482some fields may be missing entirely, such that trying to parse it
483with
484.Fn ASN1_item_d2i
485may fail.
486.Pp
487Any function which encodes an object may return a stale encoding
488if the object has been modified after deserialization or previous
489serialization.
490This is because some objects cache the encoding for efficiency reasons.