xref: /openbsd-src/lib/libcrypto/man/ASN1_put_object.3 (revision d89ec533011f513df1010f142a111086a0785f09)
1.\" $OpenBSD: ASN1_put_object.3,v 1.4 2021/12/09 19:00:00 schwarze Exp $
2.\"
3.\" Copyright (c) 2019, 2021 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 9 2021 $
18.Dt ASN1_PUT_OBJECT 3
19.Os
20.Sh NAME
21.Nm ASN1_put_object ,
22.Nm ASN1_put_eoc ,
23.Nm ASN1_object_size
24.Nd start and end the BER encoding of an arbitrary ASN.1 data element
25.Sh SYNOPSIS
26.In openssl/asn1.h
27.Ft void
28.Fo ASN1_put_object
29.Fa "unsigned char **ber_out"
30.Fa "int constructed"
31.Fa "int content_length"
32.Fa "int tag"
33.Fa "int class"
34.Fc
35.Ft int
36.Fo ASN1_put_eoc
37.Fa "unsigned char **ber_out"
38.Fc
39.Ft int
40.Fo ASN1_object_size
41.Fa "int constructed"
42.Fa "int content_length"
43.Fa "int tag"
44.Fc
45.Sh DESCRIPTION
46.Fn ASN1_put_object
47begins writing the BER encoding of an arbitrary ASN.1 data element
48to the buffer
49.Pf * ber_out
50by writing the identifier and the length bytes.
51Making sure that there is sufficient space in the buffer
52is the responsibility of the caller.
53This function does not write any content bytes
54nor any end-of-content bytes.
55.Pp
56The tag
57.Fa class
58can be
59.Dv V_ASN1_UNIVERSAL ,
60.Dv V_ASN1_APPLICATION ,
61.Dv V_ASN1_CONTEXT_SPECIFIC ,
62or
63.Dv V_ASN1_PRIVATE
64and is written to the two most significant bits of the first byte written.
65.Pp
66The
67.Fa constructed
68argument can have the following values:
69.Bl -tag -width 1n -offset 2n -compact
70.It 0
71Start a primitive value by setting the third most significant bit
72of the first byte written to 0.
73Always use the definite form.
74.It 1
75Start a constructed value by setting the third most significant bit
76of the first byte written to 1, and use the definite form.
77.It 2
78Start a constructed value and use the indefinite form,
79.El
80.Pp
81If the
82.Fa tag
83is less than
84.Dv V_ASN1_PRIMITIVE_TAG Pq = 0x1f ,
85it is written to the five least significant bits
86of the only identifier byte written.
87Otherwise, these five bits are all set to 1, and the
88.Fa tag
89is encoded in one or more following identifier bytes as needed.
90.Pp
91After completing the identifier byte(s),
92when using the definite form, the given
93.Fa content_length
94is encoded in one or more bytes as needed,
95using the long form if and only if the
96.Fa content_length
97is greater than 127.
98When using the indefinite form,
99the special byte 0x80 is written instead and the
100.Fa content_length
101argument is ignored.
102.Pp
103At the end,
104.Pf * Fa ber_out
105is set to the byte following the last byte written.
106The calling code can then start writing content bytes.
107.Pp
108If the indefinite form was selected,
109the calling code is also responsible for calling
110.Fn ASN1_put_eoc
111which writes an end-of-content marker to
112.Pf * Fa ber_out ,
113consisting of two NUL bytes, and advances
114.Pf * Fa ber_out
115by two bytes.
116.Pp
117.Fn ASN1_object_size
118calculates the total length in bytes of the BER encoding
119of an ASN.1 data element with the given
120.Fa tag
121and the number of content bytes given by
122.Fa content_length .
123The
124.Fa constructed
125argument has the same meaning as for
126.Fn ASN1_put_object .
127The return value includes the identifier, length, and content bytes.
128If
129.Fa constructed
130is 2, it also includes the end-of-content bytes.
131For the definite form, only the short form is supported if the
132.Fa content_length
133is less than 128.
134.Sh RETURN VALUES
135.Fn ASN1_put_eoc
136returns the number of bytes written, which is always 2.
137.Pp
138.Fn ASN1_object_size
139returns the total number of bytes in the encoding of the data element.
140.Sh SEE ALSO
141.Xr ASN1_item_i2d 3 ,
142.Xr ASN1_TYPE_get 3 ,
143.Xr i2d_ASN1_BOOLEAN 3 ,
144.Xr i2d_ASN1_NULL 3 ,
145.Xr i2d_ASN1_OBJECT 3 ,
146.Xr i2d_ASN1_OCTET_STRING 3 ,
147.Xr i2d_ASN1_SEQUENCE_ANY 3
148.Sh STANDARDS
149ITU-T Recommendation X.690, also known as ISO/IEC 8825-1:
150Information technology - ASN.1 encoding rules:
151Specification of Basic Encoding Rules (BER), Canonical Encoding
152Rules (CER) and Distinguished Encoding Rules (DER),
153section 8.1: General rules for encoding
154.Sh HISTORY
155.Fn ASN1_put_object
156and
157.Fn ASN1_object_size
158first appeared in SSLeay 0.5.1 and have been available since
159.Ox 2.4 .
160.Pp
161.Fn ASN1_put_eoc
162first appeared in OpenSSL 0.9.8 and has been available since
163.Ox 4.5 .
164.Sh CAVEATS
165None of these functions do any sanity checking.
166When called in inconsistent ways, invalid content may result in
167.Pf * Fa ber_out ,
168for example
169.Bl -dash -compact
170.It
171a
172.Fa tag
173number less than
174.Dv V_ASN1_PRIMITIVE_TAG
175with a
176.Fa class
177other than
178.Dv V_ASN1_UNIVERSAL
179.It
180a
181.Fa tag
182number equal to
183.Dv V_ASN1_EOC Pq 0x00
184or
185.Dv V_ASN1_PRIMITIVE_TAG Pq 0x1f
186.It
187a
188.Vt BOOLEAN ,
189.Vt INTEGER ,
190.Vt NULL
191etc. with the
192.Fa constructed
193bit set
194.It
195a
196.Vt SEQUENCE
197or
198.Vt SET
199etc. without the
200.Fa constructed
201bit set
202.It
203a
204.Fa content_length
205that makes no sense for the given
206.Fa tag
207.It
208a
209.Fa content_length
210that disagrees with the following data
211.It
212a
213.Vt BOOLEAN ,
214.Vt INTEGER ,
215.Vt NULL
216etc. in indefinite form
217.It
218an end-of-content marker even though no indefinite form was started
219.It
220\&...
221.El
222.Pp
223If the calling code wants to find out how many bytes were written,
224it needs to save a copy of the pointer
225.Pf * Fa ber_out
226before calling
227.Fn ASN1_put_object .
228