xref: /openbsd-src/lib/libutil/ober_add_string.3 (revision 00183df2f16f5c0907c5527935031f5645be0973)
1*00183df2Sjmc.\" $OpenBSD: ober_add_string.3,v 1.2 2020/10/16 09:29:04 jmc Exp $
2696b5899Stb.\"
3696b5899Stb.\" Copyright (c) 2007, 2012 Reyk Floeter <reyk@openbsd.org>
4696b5899Stb.\"
5696b5899Stb.\" Permission to use, copy, modify, and distribute this software for any
6696b5899Stb.\" purpose with or without fee is hereby granted, provided that the above
7696b5899Stb.\" copyright notice and this permission notice appear in all copies.
8696b5899Stb.\"
9696b5899Stb.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10696b5899Stb.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11696b5899Stb.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12696b5899Stb.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13696b5899Stb.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14696b5899Stb.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15696b5899Stb.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16696b5899Stb.\"
17*00183df2Sjmc.Dd $Mdocdate: October 16 2020 $
18696b5899Stb.Dt OBER_ADD_STRING 3
19696b5899Stb.Os
20696b5899Stb.Sh NAME
21696b5899Stb.Nm ober_get_element ,
22696b5899Stb.Nm ober_add_sequence ,
23696b5899Stb.Nm ober_add_set ,
24696b5899Stb.Nm ober_add_null ,
25696b5899Stb.Nm ober_add_eoc ,
26696b5899Stb.Nm ober_add_integer ,
27696b5899Stb.Nm ober_add_enumerated ,
28696b5899Stb.Nm ober_add_boolean ,
29696b5899Stb.Nm ober_add_string ,
30696b5899Stb.Nm ober_add_nstring ,
31696b5899Stb.Nm ober_add_ostring ,
32696b5899Stb.Nm ober_add_bitstring ,
33696b5899Stb.Nm ober_add_oid ,
34696b5899Stb.Nm ober_add_noid ,
35696b5899Stb.Nm ober_add_oidstring ,
36696b5899Stb.Nm ober_printf_elements
37696b5899Stb.Nd create ASN.1 objects for BER encoding
38696b5899Stb.Sh SYNOPSIS
39696b5899Stb.In sys/types.h
40696b5899Stb.In ber.h
41696b5899Stb.Ft "struct ber_element *"
42696b5899Stb.Fn "ober_get_element" "unsigned int encoding"
43696b5899Stb.Ft "struct ber_element *"
44696b5899Stb.Fn "ober_add_sequence" "struct ber_element *prev"
45696b5899Stb.Ft "struct ber_element *"
46696b5899Stb.Fn "ober_add_set" "struct ber_element *prev"
47696b5899Stb.Ft "struct ber_element *"
48696b5899Stb.Fn "ober_add_null" "struct ber_element *prev"
49696b5899Stb.Ft "struct ber_element *"
50696b5899Stb.Fn "ober_add_eoc" "struct ber_element *prev"
51696b5899Stb.Ft "struct ber_element *"
52696b5899Stb.Fn "ober_add_integer" "struct ber_element *prev" "long long val"
53696b5899Stb.Ft "struct ber_element *"
54696b5899Stb.Fn "ober_add_enumerated" "struct ber_element *prev" "long long val"
55696b5899Stb.Ft "struct ber_element *"
56696b5899Stb.Fn "ober_add_boolean" "struct ber_element *prev" "int bool"
57696b5899Stb.Ft "struct ber_element *"
58696b5899Stb.Fn "ober_add_string" "struct ber_element *prev" "const char *string"
59696b5899Stb.Ft "struct ber_element *"
60696b5899Stb.Fn "ober_add_nstring" "struct ber_element *prev" "const char *string" "size_t size"
61696b5899Stb.Ft "struct ber_element *"
62696b5899Stb.Fo "ober_add_ostring"
63696b5899Stb.Fa "struct ber_element *prev"
64696b5899Stb.Fa "struct ber_octetstring *ostring"
65696b5899Stb.Fc
66696b5899Stb.Ft "struct ber_element *"
67696b5899Stb.Fo "ober_add_bitstring"
68696b5899Stb.Fa "struct ber_element *prev"
69696b5899Stb.Fa "const void *buf"
70696b5899Stb.Fa "size_t size"
71696b5899Stb.Fc
72696b5899Stb.Ft "struct ber_element *"
73696b5899Stb.Fn "ober_add_oid" "struct ber_element *prev" "struct ber_oid *oid"
74696b5899Stb.Ft "struct ber_element *"
75696b5899Stb.Fn "ober_add_noid" "struct ber_element *prev" "struct ber_oid *oid" "int n"
76696b5899Stb.Ft "struct ber_element *"
77696b5899Stb.Fn "ober_add_oidstring" "struct ber_element *prev" "const char *string"
78696b5899Stb.Ft "struct ber_element *"
79696b5899Stb.Fn "ober_printf_elements" "struct ber_element *prev" "char *format" "..."
80696b5899Stb.Sh DESCRIPTION
81696b5899StbIntermediary storage of BER elements during encoding and decoding uses the
82696b5899Stbfollowing structure:
83696b5899Stb.Bd -literal
84696b5899Stbstruct ber_element {
85696b5899Stb	struct ber_element	*be_next;
86696b5899Stb	unsigned int		 be_type;
87696b5899Stb	unsigned int		 be_encoding;
88696b5899Stb	size_t			 be_len;
89696b5899Stb	off_t			 be_offs;
90696b5899Stb	int			 be_free;
91696b5899Stb	u_int8_t		 be_class;
92696b5899Stb	void			(*be_cb)(void *, size_t);
93696b5899Stb	void			*be_cbarg;
94696b5899Stb	union {
95696b5899Stb		struct ber_element	*bv_sub;
96696b5899Stb		void			*bv_val;
97696b5899Stb		long long		 bv_numeric;
98696b5899Stb	} be_union;
99696b5899Stb#define be_sub		be_union.bv_sub
100696b5899Stb#define be_val		be_union.bv_val
101696b5899Stb#define be_numeric	be_union.bv_numeric
102696b5899Stb};
103696b5899Stb.Ed
104696b5899Stb.Pp
105696b5899Stb.Fn ober_get_element
106696b5899Stbcreates a new
107696b5899Stb.Vt ber_element
108696b5899Stbwith default values, dynamically allocates required storage, and sets
109696b5899Stb.Fa be_encoding
110696b5899Stbto
111696b5899Stb.Fa encoding .
112696b5899Stb.Pp
113696b5899StbThe
114696b5899Stb.Fn ober_add_*
115696b5899Stbfunctions allocate a new
116696b5899Stb.Vt ber_element
117696b5899Stbof the respective type.
118696b5899StbIf
119696b5899Stb.Fa prev
120696b5899Stbis an empty sequence or set, they put the new element into that
121696b5899Stbsequence or set.
122696b5899StbOtherwise, unless
123696b5899Stb.Fa prev
124696b5899Stbis
125696b5899Stb.Dv NULL ,
126696b5899Stbthey put it behind
127696b5899Stb.Fa prev .
128696b5899StbThose functions taking a second argument initialize the content
129696b5899Stbof the new element from the second argument.
130696b5899Stb.Pp
131696b5899Stb.Fn ober_printf_elements
132696b5899Stbcreates zero or more
133696b5899Stb.Vt ber_element
134696b5899Stbstructures.
135696b5899StbFor each byte in
136696b5899Stb.Fa fmt ,
137*00183df2Sjmcarguments of the types given in the following table are consumed
138696b5899Stband passed to the listed function, creating one
139696b5899Stb.Vt ber_element
140696b5899Stbper byte.
141696b5899StbThe following bytes are valid:
142696b5899Stb.Bl -column -offset indent byte ober_add_enumerated "struct ber_element *"
143696b5899Stb.It Sy byte Ta Sy function Ta Sy arguments
144696b5899Stb.It B   Ta Fn ober_add_bitstring  Ta 2: Vt void * , size_t
145696b5899Stb.It b   Ta Fn ober_add_boolean    Ta 1: Vt int
146696b5899Stb.It d   Ta Fn ober_add_integer    Ta 1: Vt int
147696b5899Stb.It E   Ta Fn ober_add_enumerated Ta 1: Vt long long
148696b5899Stb.It e   Ta see below              Ta 1: Vt struct ber_element *
149696b5899Stb.It i   Ta Fn ober_add_integer    Ta 1: Vt long long
150696b5899Stb.It O   Ta Fn ober_add_oid        Ta 1: Vt struct ber_oid *
151696b5899Stb.It o   Ta Fn ober_add_oidstring  Ta 1: Vt char *
152696b5899Stb.It s   Ta Fn ober_add_string     Ta 1: Vt char *
153696b5899Stb.It t   Ta Xr ober_set_header 3   Ta 2: Vt int , unsigned int
154696b5899Stb.It x   Ta Fn ober_add_nstring    Ta 2: Vt char * , size_t
155696b5899Stb.It \&( Ta Fn ober_add_set        Ta 0
156696b5899Stb.It \&) Ta see below              Ta 0
157696b5899Stb.It \&. Ta Fn ober_add_eoc        Ta 0
158696b5899Stb.It 0   Ta Fn ober_add_null       Ta 0
159696b5899Stb.It {   Ta Fn ober_add_sequence   Ta 0
160696b5899Stb.It }   Ta see below              Ta 0
161696b5899Stb.El
162696b5899Stb.Pp
163696b5899StbThe
164696b5899Stb.Sq e
165696b5899Stband
166696b5899Stb.Sq t
167696b5899Stbbytes are special in so far as they do not create new elements.
168696b5899StbThe
169696b5899Stb.Sq e
170696b5899Stbbyte adds an element that was already created earlier into or behind
171696b5899Stbthe previous element, or into and behind
172696b5899Stb.Fa ber
173696b5899Stbif the
174696b5899Stb.Sq e
175696b5899Stbis the first byte in
176696b5899Stb.Fa fmt ,
177696b5899Stbjust like the
178696b5899Stb.Fn ober_add_*
179696b5899Stbfunctions would add a new element.
180696b5899StbThe
181696b5899Stb.Sq t
182696b5899Stbbyte changes the class and type of the last element, or of
183696b5899Stb.Fa ber
184696b5899Stbif
185696b5899Stb.Sq t
186696b5899Stbis the first byte in
187696b5899Stb.Fa fmt ,
188696b5899Stbwithout changing its position relative to other elements.
189696b5899Stb.Pp
190696b5899StbA closing brace or parenthesis closes an open sequence or set,
191696b5899Stbif any, such that the next element will be added behind rather
192696b5899Stbthan into the sequence or set.
193696b5899StbOnly one sequence or set can be open at any time.
194696b5899StbNesting is not supported without multiple function calls.
195696b5899Stb.Sh RETURN VALUES
196696b5899StbUpon successful completion,
197696b5899Stbthese functions return a pointer to a populated
198696b5899Stb.Vt ber_element .
199696b5899StbOtherwise
200696b5899Stb.Dv NULL
201696b5899Stbis returned and the global variable
202696b5899Stb.Va errno
203696b5899Stbis set to indicate the error.
204696b5899Stb.Pp
205696b5899Stb.Fn ober_printf_elements
206696b5899Stbreturns
207696b5899Stb.Dv NULL
208696b5899Stbwithout setting
209696b5899Stb.Va errno
210696b5899Stbif
211696b5899Stb.Fa fmt
212696b5899Stbis an empty string and
213696b5899Stb.Fa ber
214696b5899Stbis
215696b5899Stb.Dv NULL .
216696b5899Stb.Sh SEE ALSO
217696b5899Stb.Xr ober_get_string 3 ,
218696b5899Stb.Xr ober_oid_cmp 3 ,
219696b5899Stb.Xr ober_read_elements 3 ,
220696b5899Stb.Xr ober_set_header 3
221696b5899Stb.Sh STANDARDS
222696b5899StbITU-T Recommendation X.690, also known as ISO/IEC 8825-1:
223696b5899StbInformation technology - ASN.1 encoding rules.
224696b5899Stb.Sh HISTORY
225696b5899StbThese functions first appeared as internal functions in
226696b5899Stb.Xr snmpd 8
227696b5899Stbin
228696b5899Stb.Ox 4.2
229696b5899Stband were moved to libutil in
230696b5899Stb.Ox 6.6 .
231696b5899Stb.Sh AUTHORS
232696b5899Stb.An -nosplit
233696b5899StbThe BER library was written by
234696b5899Stb.An Claudio Jeker Aq Mt claudio@openbsd.org ,
235696b5899Stb.An Marc Balmer Aq Mt marc@openbsd.org
236696b5899Stband
237696b5899Stb.An Reyk Floeter Aq Mt reyk@openbsd.org .
238