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