summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorschwarze <>2016-12-24 21:42:30 +0000
committerschwarze <>2016-12-24 21:42:30 +0000
commit823ebb4bac20fa9711aa3eeabca11eed198723da (patch)
treeea4c6283036d6c98a0a9f626a810b24f1beff039 /src/lib
parentd38aea3af37495c4f8f32f6d68e1f458590f658a (diff)
downloadopenbsd-823ebb4bac20fa9711aa3eeabca11eed198723da.tar.gz
openbsd-823ebb4bac20fa9711aa3eeabca11eed198723da.tar.bz2
openbsd-823ebb4bac20fa9711aa3eeabca11eed198723da.zip
Very carefully tweak OpenSSL doc/man3/d2i_X509.pod and create a new
ASN1_item_d2i(3) manual page from it. Enough text remains to keep Stephen Henson's Copyright. The eight functions documented in this new page are listed in <openssl/asn1.h> and in Symbols.list, so they are public even though OpenSSL does not document them. They are very important because hundreds of documented, much-used public interface functions are trivial wrappers around them, sharing their complicated semantics and their copious CAVEATS and BUGS. The plan is for the many pages documenting the wrappers to become very concise, to focus on the few type-dependent specifics, and to point to this new page for the details of the semantics, for the CAVEATS, and for the BUGS. While here, write a companion page ASN1_item_new(3) from scratch. The user interface described in that page scares the hell out of me, and i think people writing code to handle ASN.1 ought to be aware of that dangerous user interface design, or they will sooner or later get trapped.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/man/ASN1_item_d2i.3408
-rw-r--r--src/lib/libcrypto/man/ASN1_item_new.3103
-rw-r--r--src/lib/libcrypto/man/Makefile4
3 files changed, 514 insertions, 1 deletions
diff --git a/src/lib/libcrypto/man/ASN1_item_d2i.3 b/src/lib/libcrypto/man/ASN1_item_d2i.3
new file mode 100644
index 0000000000..52707f017a
--- /dev/null
+++ b/src/lib/libcrypto/man/ASN1_item_d2i.3
@@ -0,0 +1,408 @@
1.\" $OpenBSD: ASN1_item_d2i.3,v 1.1 2016/12/24 21:42:29 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: December 24 2016 $
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 ASN1_item_i2d ,
76.Nm ASN1_item_i2d_bio ,
77.Nm ASN1_item_i2d_fp ,
78.Nm ASN1_item_dup ,
79.Nm ASN1_item_print
80.Nd decode and encode ASN.1 objects
81.Sh SYNOPSIS
82.In openssl/asn1.h
83.Ft ASN1_VALUE *
84.Fo ASN1_item_d2i
85.Fa "ASN1_VALUE **val_out"
86.Fa "const unsigned char **der_in"
87.Fa "long length"
88.Fa "const ASN1_ITEM *it"
89.Fc
90.Ft void *
91.Fo ASN1_item_d2i_bio
92.Fa "const ASN1_ITEM *it"
93.Fa "BIO *in_bio"
94.Fa "void *val_out"
95.Fc
96.Ft void *
97.Fo ASN1_item_d2i_fp
98.Fa "const ASN1_ITEM *it"
99.Fa "FILE *in_fp"
100.Fa "void *val_out"
101.Fc
102.Ft int
103.Fo ASN1_item_i2d
104.Fa "ASN1_VALUE *val_in"
105.Fa "unsigned char **der_out"
106.Fa "const ASN1_ITEM *it"
107.Fc
108.Ft int
109.Fo ASN1_item_i2d_bio
110.Fa "const ASN1_ITEM *it"
111.Fa "BIO *out_bio"
112.Fa "void *val_in"
113.Fc
114.Ft int
115.Fo ASN1_item_i2d_fp
116.Fa "const ASN1_ITEM *it"
117.Fa "FILE *out_fp"
118.Fa "void *val_in"
119.Fc
120.Ft void *
121.Fo ASN1_item_dup
122.Fa "const ASN1_ITEM *it"
123.Fa "void *val_in"
124.Fc
125.Ft int
126.Fo ASN1_item_print
127.Fa "BIO *out_bio"
128.Fa "ASN1_VALUE *val_in"
129.Fa "int indent"
130.Fa "const ASN1_ITEM *it"
131.Fa "const ASN1_PCTX *pctx"
132.Fc
133.Sh DESCRIPTION
134These functions convert ASN.1 values from their DER encoding to
135internal C structures
136.Pq Dq d2i
137and vice versa
138.Pq Dq i2d .
139Unlike the C structures which contain pointers to sub-objects, DER
140is a serialized encoding, suitable for transfer over the network
141and for storage in a file.
142.Pp
143.Fn ASN1_item_d2i
144interpretes
145.Pf * Fa der_in
146as a DER- or BER-encoded byte array and decodes
147.Fa length
148bytes.
149If successful,
150.Pf * Fa der_in
151is advanced to the byte following the parsed data.
152.Pp
153If decoding succeeds and
154.Pf * Fa val_out
155is
156.Dv NULL ,
157a new object is allocated.
158.Pp
159If decoding succeeds and
160.Pf * Fa val_out
161is not
162.Dv NULL ,
163it is assumed to point to a valid populated object and an attempt
164is made to reuse it.
165It must not be an empty structure such as one returned by
166.Xr ASN1_item_new 3
167or by one of the various type-specific
168.Fn *_new
169functions.
170This
171.Dq reuse
172capability is present for backward compatibility, but its use is
173strongly discouraged; see the
174.Sx BUGS
175section below.
176.Pp
177.Fn ASN1_item_d2i_bio
178and
179.Fn ASN1_item_d2i_fp
180are similar to
181.Fn ASN1_item_d2i
182except that they read from a
183.Vt BIO
184or
185.Vt FILE ,
186respectively.
187.Pp
188.Fn ASN1_item_i2d
189encodes the object pointed to by
190.Fa val_in
191into DER format.
192If
193.Fa der_out
194is not
195.Dv NULL ,
196it writes the DER-encoded data to the buffer at
197.Pf * Fa der_out
198and increments it to point after the data just written.
199.Pp
200If
201.Pf * Fa der_out
202is
203.Dv NULL ,
204memory is allocated for a buffer, and
205.Pf * Fa der_out
206is not incremented, but points to the start of the data just written.
207.Pp
208.Fn ASN1_item_i2d_bio
209and
210.Fn ASN1_item_i2d_fp
211are similar to
212.Fn ASN1_item_i2d
213except that they write to a
214.Vt BIO
215or
216.Vt FILE ,
217respectively.
218.Pp
219.Fn ASN1_item_dup
220creates a deep copy of
221.Fa val_in
222by calling
223.Fn ASN1_item_i2d
224and
225.Fn ASN1_item_d2i .
226.Sh RETURN VALUES
227If succesful,
228.Fn ASN1_item_d2i ,
229.Fn ASN1_item_d2i_bio ,
230and
231.Fn ASN1_item_d2i_fp
232return a pointer to the decoded ASN.1 value.
233In addition, if
234.Fa val_out
235is not
236.Dv NULL ,
237the pointer is also written to
238.Pf * Fa val_out.
239If an error occurs,
240.Dv NULL
241is returned.
242.Pp
243.Fn ASN1_item_i2d
244returns the number of bytes written
245or a negative value if an error occurs.
246.Pp
247.Fn ASN1_item_i2d_bio
248and
249.Fn ASN1_item_i2d_fp
250return 1 for success or 0 for failure.
251.Pp
252.Fn ASN1_item_dup
253returns the new
254.Vt ASN1_VALUE
255object or
256.Dv NULL
257if an error occurs.
258.Sh EXAMPLES
259Many type-specific wrapper functions exist.
260Using those wrappers is recommended in application code
261because it restores part of the type safety that the low-level
262interfaces described in the present manual page lack.
263.Pp
264For example, to allocate a buffer and write the DER encoding of an
265.Vt X509
266object into it:
267.Bd -literal -offset indent
268X509 *x;
269unsigned char *buf;
270int len;
271
272buf = NULL;
273len = i2d_X509(x, &buf);
274if (len < 0)
275 /* error */
276.Ed
277.Pp
278Attempt to decode a buffer:
279.Bd -literal -offset indent
280X509 *x;
281unsigned char *buf, *p;
282int len;
283
284/* Set up buf and len to point to the input buffer. */
285p = buf;
286x = d2i_X509(NULL, &p, len);
287if (x == NULL)
288 /* error */
289.Ed
290.Pp
291Equivalent technique:
292.Bd -literal -offset indent
293X509 *x;
294unsigned char *buf, *p;
295int len;
296
297/* Set up buf and len to point to the input buffer. */
298p = buf;
299x = NULL;
300
301if (d2i_X509(&x, &p, len) == NULL)
302 /* error */
303.Ed
304.Sh SEE ALSO
305.Xr ASN1_item_new 3
306.Sh CAVEATS
307If the type described by
308.Fa it
309fails to match the true type of
310.Fa val_in
311or
312.Pf * Fa val_out ,
313buffer overflows and segmentation faults are likely to occur.
314For more details about why the type
315.Vt ASN1_VALUE
316constitutes dangerous user interface design, see
317.Xr ASN1_item_new 3 .
318.Pp
319The encoded data is in binary form and may contain embedded NUL bytes.
320Functions such as
321.Xr strlen 3
322will not return the correct length of the encoded data.
323.Pp
324While the way that
325.Pf * Fa der_in
326and
327.Pf * Fa der_out
328are incremented after the operation supports the typical usage
329patterns of reading or writing one object after another, this
330behaviour can trap the unwary.
331.Pp
332Using a temporary pointer into the buffer is mandatory.
333A common mistake is to attempt to use a buffer directly as follows:
334.Bd -literal -offset indent
335X509 *x;
336unsigned char *buf;
337int len;
338
339len = i2d_X509(x, NULL);
340buf = malloc(len);
341i2d_X509(x, &buf);
342/* do something with buf[] */
343free(buf);
344.Ed
345.Pp
346This code will result in
347.Va buf
348apparently containing garbage because it was incremented during
349.Fn i2d_X509
350to point after the data just written.
351Also
352.Va buf
353will no longer contain the pointer allocated by
354.Xr malloc 3
355and the subsequent call to
356.Xr free 3
357is likely to crash.
358.Pp
359Another trap to avoid is misuse of the
360.Fa val_out
361argument:
362.Bd -literal -offset indent
363X509 *x;
364
365if (d2i_X509(&x, &p, len) == NULL)
366 /* error */
367.Ed
368.Pp
369This will probably crash somewhere in
370.Fn d2i_X509
371because
372.Va x
373is uninitialized and an attempt will be made to interpret its invalid
374content as an
375.Vt X509
376object, typically causing a segmentation violation.
377If
378.Va x
379is set to
380.Dv NULL
381first, then this will not happen.
382.Sh BUGS
383If the
384.Dq reuse
385capability is used, a valid object is passed in via
386.Pf * Fa val_out ,
387and an error occurs, then the object is not freed and may be left
388in an invalid or inconsistent state.
389.Pp
390In some versions of OpenSSL, the
391.Dq reuse
392behaviour is broken such that some parts of the reused object may
393persist if they are not present in the new one.
394.Pp
395In many versions of OpenSSL,
396.Fn ASN1_item_i2d
397will not return an error if mandatory fields are not initialized
398due to a programming error.
399In that case, the encoded structure may contain invalid data and
400some fields may be missing entirely, such that trying to parse it
401with
402.Fn ASN1_item_d2i
403may fail.
404.Pp
405Any function which encodes an object may return a stale encoding
406if the object has been modified after deserialization or previous
407serialization.
408This is because some objects cache the encoding for efficiency reasons.
diff --git a/src/lib/libcrypto/man/ASN1_item_new.3 b/src/lib/libcrypto/man/ASN1_item_new.3
new file mode 100644
index 0000000000..0444bbe15a
--- /dev/null
+++ b/src/lib/libcrypto/man/ASN1_item_new.3
@@ -0,0 +1,103 @@
1.\" $OpenBSD: ASN1_item_new.3,v 1.1 2016/12/24 21:42:29 schwarze Exp $
2.\"
3.\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: December 24 2016 $
18.Dt ASN1_ITEM_NEW 3
19.Os
20.Sh NAME
21.Nm ASN1_item_new ,
22.Nm ASN1_item_free
23.Nd generic ASN.1 value constructor and destructor
24.Sh SYNOPSIS
25.In openssl/asn1.h
26.Ft ASN1_VALUE *
27.Fo ASN1_item_new
28.Fa "const ASN1_ITEM *it"
29.Fc
30.Ft void
31.Fo ASN1_item_free
32.Fa "ASN1_VALUE *val_in"
33.Fa "const ASN1_ITEM *it"
34.Fc
35.Sh DESCRIPTION
36.Fn ASN1_item_new
37allocates and initializes an empty ASN.1 value
38of the type described by the global static object
39.Fa it .
40.Pp
41.Fn ASN1_item_free
42frees an ASN.1 value of the type described by
43.Fa it .
44If the true type of
45.Fa val_in
46fails to match the specified
47.Fa it ,
48buffer overflows and segmentation faults are likely to occur.
49It is not possible to recover the type of an
50.Vt ASN1_VALUE
51object by inspecting it; the type always needs to be remembered
52separately.
53.Pp
54.Vt ASN1_VALUE
55is an incomplete type, and pointers to it always require casting
56to the correct complete type before they can be dereferenced.
57For all practical purposes, a pointer to
58.Vt ASN1_VALUE
59is equivalent to a
60.Vt void
61pointer.
62.Pp
63Depending on
64.Fa it ,
65there are more than 150 different types that
66.Fn ASN1_item_new
67may return.
68Most of them are pointers to structures or pointers to arrays of
69structures, but there are a few exceptions, for example:
70If
71.Fa it
72is
73.Dv ASN1_NULL_it ,
74.Fn ASN1_item_new
75returns a specific invalid pointer representing the unique
76.Vt ASN1_NULL
77object.
78If
79.Fa it
80is
81.Dv ASN1_BOOLEAN_it
82or
83.Dv LONG_it ,
84.Fn ASN1_item_new
85does not return a pointer at all, but a
86.Vt long
87value cast to
88.Vt ASN1_VALUE * .
89.Sh RETURN VALUES
90.Fn ASN1_item_new
91returns the new
92.Vt ASN1_VALUE
93object or
94.Dv NULL
95if an error occurs.
96.Sh SEE ALSO
97.Xr ASN1_item_d2i 3 ,
98.Xr OBJ_nid2obj 3
99.Sh BUGS
100The
101.Vt ASN1_VALUE
102type compromises type safety and invites programming mistakes that
103will typically have severe consequences.
diff --git a/src/lib/libcrypto/man/Makefile b/src/lib/libcrypto/man/Makefile
index 7d013ab44b..a7d1d9a7e6 100644
--- a/src/lib/libcrypto/man/Makefile
+++ b/src/lib/libcrypto/man/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.100 2016/12/24 01:00:48 schwarze Exp $ 1# $OpenBSD: Makefile,v 1.101 2016/12/24 21:42:30 schwarze Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4 4
@@ -11,6 +11,8 @@ MAN= \
11 ASN1_TIME_set.3 \ 11 ASN1_TIME_set.3 \
12 ASN1_TYPE_get.3 \ 12 ASN1_TYPE_get.3 \
13 ASN1_generate_nconf.3 \ 13 ASN1_generate_nconf.3 \
14 ASN1_item_d2i.3 \
15 ASN1_item_new.3 \
14 ASN1_time_parse.3 \ 16 ASN1_time_parse.3 \
15 AUTHORITY_KEYID_new.3 \ 17 AUTHORITY_KEYID_new.3 \
16 BASIC_CONSTRAINTS_new.3 \ 18 BASIC_CONSTRAINTS_new.3 \