xref: /onnv-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/asn.1/asn1buf.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*0Sstevel@tonic-gate 
3*0Sstevel@tonic-gate /* Coding Buffer Specifications */
4*0Sstevel@tonic-gate #ifndef __ASN1BUF_H__
5*0Sstevel@tonic-gate #define __ASN1BUF_H__
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate #include "k5-int.h"
8*0Sstevel@tonic-gate #include "krbasn1.h"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate typedef struct code_buffer_rep {
11*0Sstevel@tonic-gate   char *base, *bound, *next;
12*0Sstevel@tonic-gate } asn1buf;
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate /**************** Private Procedures ****************/
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate int asn1buf_size
18*0Sstevel@tonic-gate 	(const asn1buf *buf);
19*0Sstevel@tonic-gate /* requires  *buf has been created and not destroyed
20*0Sstevel@tonic-gate    effects   Returns the total size
21*0Sstevel@tonic-gate 	(in octets) of buf's octet buffer. */
22*0Sstevel@tonic-gate #define asn1buf_size(buf) \
23*0Sstevel@tonic-gate   (((buf) == NULL || (buf)->base == NULL) \
24*0Sstevel@tonic-gate    ? 0 \
25*0Sstevel@tonic-gate    : ((buf)->bound - (buf)->base + 1))
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate int asn1buf_free
28*0Sstevel@tonic-gate 	(const asn1buf *buf);
29*0Sstevel@tonic-gate /* requires  *buf is allocated
30*0Sstevel@tonic-gate    effects   Returns the number of unused, allocated octets in *buf. */
31*0Sstevel@tonic-gate #define asn1buf_free(buf) \
32*0Sstevel@tonic-gate   (((buf) == NULL || (buf)->base == NULL) \
33*0Sstevel@tonic-gate    ? 0 \
34*0Sstevel@tonic-gate    : ((buf)->bound - (buf)->next + 1))
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate asn1_error_code asn1buf_ensure_space
38*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int amount);
39*0Sstevel@tonic-gate /* requires  *buf is allocated
40*0Sstevel@tonic-gate    modifies  *buf
41*0Sstevel@tonic-gate    effects  If buf has less than amount octets of free space, then it is
42*0Sstevel@tonic-gate             expanded to have at least amount octets of free space.
43*0Sstevel@tonic-gate             Returns ENOMEM memory is exhausted. */
44*0Sstevel@tonic-gate #define asn1buf_ensure_space(buf,amount) \
45*0Sstevel@tonic-gate   ((asn1buf_free(buf) < (amount)) \
46*0Sstevel@tonic-gate    ? (asn1buf_expand((buf), (amount)-asn1buf_free(buf))) \
47*0Sstevel@tonic-gate    : 0)
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate asn1_error_code asn1buf_expand
51*0Sstevel@tonic-gate 	(asn1buf *buf, unsigned int inc);
52*0Sstevel@tonic-gate /* requires  *buf is allocated
53*0Sstevel@tonic-gate    modifies  *buf
54*0Sstevel@tonic-gate    effects   Expands *buf by allocating space for inc more octets.
55*0Sstevel@tonic-gate              Returns ENOMEM if memory is exhausted. */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate int asn1buf_len
58*0Sstevel@tonic-gate 	(const asn1buf *buf);
59*0Sstevel@tonic-gate /* requires  *buf is allocated
60*0Sstevel@tonic-gate    effects   Returns the length of the encoding in *buf. */
61*0Sstevel@tonic-gate #define asn1buf_len(buf)	((buf)->next - (buf)->base)
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /****** End of private procedures *****/
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate   Overview
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate     The coding buffer is an array of char (to match a krb5_data structure)
69*0Sstevel@tonic-gate      with 3 reference pointers:
70*0Sstevel@tonic-gate      1) base - The bottom of the octet array.  Used for memory management
71*0Sstevel@tonic-gate                operations on the array (e.g. alloc, realloc, free).
72*0Sstevel@tonic-gate      2) next - Points to the next available octet position in the array.
73*0Sstevel@tonic-gate                During encoding, this is the next free position, and it
74*0Sstevel@tonic-gate                  advances as octets are added to the array.
75*0Sstevel@tonic-gate 	       During decoding, this is the next unread position, and it
76*0Sstevel@tonic-gate                  advances as octets are read from the array.
77*0Sstevel@tonic-gate      3) bound - Points to the top of the array. Used for bounds-checking.
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate     All pointers to encoding buffers should be initalized to NULL.
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate   Operations
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate     asn1buf_create
84*0Sstevel@tonic-gate     asn1buf_wrap_data
85*0Sstevel@tonic-gate     asn1buf_destroy
86*0Sstevel@tonic-gate     asn1buf_insert_octet
87*0Sstevel@tonic-gate     asn1buf_insert_charstring
88*0Sstevel@tonic-gate     asn1buf_remove_octet
89*0Sstevel@tonic-gate     asn1buf_remove_charstring
90*0Sstevel@tonic-gate     asn1buf_unparse
91*0Sstevel@tonic-gate     asn1buf_hex_unparse
92*0Sstevel@tonic-gate     asn12krb5_buf
93*0Sstevel@tonic-gate     asn1buf_remains
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate     (asn1buf_size)
96*0Sstevel@tonic-gate     (asn1buf_free)
97*0Sstevel@tonic-gate     (asn1buf_ensure_space)
98*0Sstevel@tonic-gate     (asn1buf_expand)
99*0Sstevel@tonic-gate     (asn1buf_len)
100*0Sstevel@tonic-gate */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate asn1_error_code asn1buf_create
103*0Sstevel@tonic-gate 	(asn1buf **buf);
104*0Sstevel@tonic-gate /* effects   Creates a new encoding buffer pointed to by *buf.
105*0Sstevel@tonic-gate              Returns ENOMEM if the buffer can't be created. */
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate asn1_error_code asn1buf_wrap_data
108*0Sstevel@tonic-gate 	(asn1buf *buf, const krb5_data *code);
109*0Sstevel@tonic-gate /* requires  *buf has already been allocated
110*0Sstevel@tonic-gate    effects   Turns *buf into a "wrapper" for *code.  i.e. *buf is set up
111*0Sstevel@tonic-gate               such that its bottom is the beginning of *code, and its top
112*0Sstevel@tonic-gate 	      is the top of *code.
113*0Sstevel@tonic-gate 	     Returns ASN1_MISSING_FIELD if code is empty. */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate asn1_error_code asn1buf_imbed
116*0Sstevel@tonic-gate 	(asn1buf *subbuf, const asn1buf *buf,
117*0Sstevel@tonic-gate 		   const unsigned int length,
118*0Sstevel@tonic-gate 		   const int indef);
119*0Sstevel@tonic-gate /* requires  *subbuf and *buf are allocated
120*0Sstevel@tonic-gate    effects   *subbuf becomes a sub-buffer of *buf.  *subbuf begins
121*0Sstevel@tonic-gate               at *buf's current position and is length octets long.
122*0Sstevel@tonic-gate               (Unless this would exceed the bounds of *buf -- in
123*0Sstevel@tonic-gate 	      that case, ASN1_OVERRUN is returned)  *subbuf's current
124*0Sstevel@tonic-gate 	      position starts at the beginning of *subbuf. */
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate asn1_error_code asn1buf_sync
127*0Sstevel@tonic-gate 	(asn1buf *buf, asn1buf *subbuf, const asn1_class Class,
128*0Sstevel@tonic-gate 		   const asn1_tagnum lasttag,
129*0Sstevel@tonic-gate 		   const unsigned int length, const int indef,
130*0Sstevel@tonic-gate 		   const int seqindef);
131*0Sstevel@tonic-gate /* requires  *subbuf is a sub-buffer of *buf, as created by asn1buf_imbed.
132*0Sstevel@tonic-gate              lasttag is the last tagnumber read.
133*0Sstevel@tonic-gate    effects   Synchronizes *buf's current position to match that of *subbuf. */
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate asn1_error_code asn1buf_skiptail
136*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int length,
137*0Sstevel@tonic-gate 		   const int indef);
138*0Sstevel@tonic-gate /* requires  *buf is a subbuffer used in a decoding of a
139*0Sstevel@tonic-gate              constructed indefinite sequence.
140*0Sstevel@tonic-gate    effects   skips trailing fields. */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate asn1_error_code asn1buf_destroy
143*0Sstevel@tonic-gate 	(asn1buf **buf);
144*0Sstevel@tonic-gate /* effects   Deallocates **buf, sets *buf to NULL. */
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate asn1_error_code asn1buf_insert_octet
147*0Sstevel@tonic-gate 	(asn1buf *buf, const int o);
148*0Sstevel@tonic-gate /* requires  *buf is allocated
149*0Sstevel@tonic-gate    effects   Inserts o into the buffer *buf, expanding the buffer if
150*0Sstevel@tonic-gate              necessary.  Returns ENOMEM memory is exhausted. */
151*0Sstevel@tonic-gate #if ((__GNUC__ >= 2) && !defined(ASN1BUF_OMIT_INLINE_FUNCS))
asn1buf_insert_octet(asn1buf * buf,const int o)152*0Sstevel@tonic-gate extern __inline__ asn1_error_code asn1buf_insert_octet(asn1buf *buf, const int o)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate   asn1_error_code retval;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate   retval = asn1buf_ensure_space(buf,1U);
157*0Sstevel@tonic-gate   if(retval) return retval;
158*0Sstevel@tonic-gate   *(buf->next) = (char)o;
159*0Sstevel@tonic-gate   (buf->next)++;
160*0Sstevel@tonic-gate   return 0;
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate #endif
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate asn1_error_code asn1buf_insert_octetstring
165*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, const asn1_octet *s);
166*0Sstevel@tonic-gate /* requires  *buf is allocated
167*0Sstevel@tonic-gate    modifies  *buf
168*0Sstevel@tonic-gate    effects   Inserts the contents of s (an octet array of length len)
169*0Sstevel@tonic-gate               into the buffer *buf, expanding the buffer if necessary.
170*0Sstevel@tonic-gate 	     Returns ENOMEM if memory is exhausted. */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate asn1_error_code asn1buf_insert_charstring
173*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, const char *s);
174*0Sstevel@tonic-gate /* requires  *buf is allocated
175*0Sstevel@tonic-gate    modifies  *buf
176*0Sstevel@tonic-gate    effects   Inserts the contents of s (a character array of length len)
177*0Sstevel@tonic-gate               into the buffer *buf, expanding the buffer if necessary.
178*0Sstevel@tonic-gate 	     Returns ENOMEM if memory is exhausted. */
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate asn1_error_code asn1buf_remove_octet
181*0Sstevel@tonic-gate 	(asn1buf *buf, asn1_octet *o);
182*0Sstevel@tonic-gate /* requires  *buf is allocated
183*0Sstevel@tonic-gate    effects   Returns *buf's current octet in *o and advances to
184*0Sstevel@tonic-gate               the next octet.
185*0Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if *buf has already been exhausted. */
186*0Sstevel@tonic-gate #define asn1buf_remove_octet(buf,o) \
187*0Sstevel@tonic-gate   (((buf)->next > (buf)->bound) \
188*0Sstevel@tonic-gate    ? ASN1_OVERRUN \
189*0Sstevel@tonic-gate    : ((*(o) = (asn1_octet)(*(((buf)->next)++))),0))
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate asn1_error_code asn1buf_remove_octetstring
192*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, asn1_octet **s);
193*0Sstevel@tonic-gate /* requires  *buf is allocated
194*0Sstevel@tonic-gate    effects   Removes the next len octets of *buf and returns them in **s.
195*0Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if there are fewer than len unread octets
196*0Sstevel@tonic-gate 	      left in *buf.
197*0Sstevel@tonic-gate 	     Returns ENOMEM if *s could not be allocated. */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate asn1_error_code asn1buf_remove_charstring
200*0Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len,
201*0Sstevel@tonic-gate 					  char **s);
202*0Sstevel@tonic-gate /* requires  *buf is allocated
203*0Sstevel@tonic-gate    effects   Removes the next len octets of *buf and returns them in **s.
204*0Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if there are fewer than len unread octets
205*0Sstevel@tonic-gate 	      left in *buf.
206*0Sstevel@tonic-gate 	     Returns ENOMEM if *s could not be allocated. */
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate asn1_error_code asn1buf_unparse
209*0Sstevel@tonic-gate 	(const asn1buf *buf, char **s);
210*0Sstevel@tonic-gate /* modifies  *s
211*0Sstevel@tonic-gate    effects   Returns a human-readable representation of *buf in *s,
212*0Sstevel@tonic-gate              where each octet in *buf is represented by a character in *s. */
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate asn1_error_code asn1buf_hex_unparse
215*0Sstevel@tonic-gate 	(const asn1buf *buf, char **s);
216*0Sstevel@tonic-gate /* modifies  *s
217*0Sstevel@tonic-gate    effects   Returns a human-readable representation of *buf in *s,
218*0Sstevel@tonic-gate              where each octet in *buf is represented by a 2-digit
219*0Sstevel@tonic-gate 	     hexadecimal number in *s. */
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate asn1_error_code asn12krb5_buf
222*0Sstevel@tonic-gate 	(const asn1buf *buf, krb5_data **code);
223*0Sstevel@tonic-gate /* modifies  *code
224*0Sstevel@tonic-gate    effects   Instantiates **code with the krb5_data representation of **buf. */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate int asn1buf_remains
228*0Sstevel@tonic-gate 	(asn1buf *buf, int indef);
229*0Sstevel@tonic-gate /* requires  *buf is a buffer containing an asn.1 structure or array
230*0Sstevel@tonic-gate    modifies  *buf
231*0Sstevel@tonic-gate    effects   Returns the number of unprocessed octets remaining in *buf. */
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate #endif
234