xref: /onnv-gate/usr/src/cmd/agents/snmp/snmplib/asn1.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2001,2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <string.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include "snmp_msg.h"
34*0Sstevel@tonic-gate #include "asn1.h"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * asn_parse_int - pulls a int32_t out of an ASN int type.
39*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
40*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
41*0Sstevel@tonic-gate  *   following the end of this object.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
44*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
45*0Sstevel@tonic-gate  *  Returns NULL on any error.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate u_char *
asn_parse_int(u_char * data,uint32_t * datalength,u_char * type,int32_t * intp,uint32_t intsize,char * error_label)48*0Sstevel@tonic-gate asn_parse_int(
49*0Sstevel@tonic-gate     u_char	    *data,	/* IN - pointer to start of object */
50*0Sstevel@tonic-gate     uint32_t	    *datalength,/* IN/OUT - number of valid bytes left in buffer */
51*0Sstevel@tonic-gate     u_char	    *type,	/* OUT - asn type of object */
52*0Sstevel@tonic-gate     int32_t	    *intp,	/* IN/OUT - pointer to start of output buffer */
53*0Sstevel@tonic-gate     uint32_t	    intsize,    /* IN - size of output buffer */
54*0Sstevel@tonic-gate     char 	    *error_label)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * ASN.1 integer ::= 0x02 asnlength byte {byte}*
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate     u_char *bufp = data;
60*0Sstevel@tonic-gate     uint32_t  asn_length = 0;
61*0Sstevel@tonic-gate     int32_t   value = 0;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	error_label[0] = '\0';
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate     if (intsize != sizeof (int32_t)){
67*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_NOT_LONG);
68*0Sstevel@tonic-gate 	return NULL;
69*0Sstevel@tonic-gate     }
70*0Sstevel@tonic-gate     *type = *bufp++;
71*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp, &asn_length, error_label);
72*0Sstevel@tonic-gate     if (bufp == NULL){
73*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_BAD_LENGTH);
74*0Sstevel@tonic-gate 	return NULL;
75*0Sstevel@tonic-gate     }
76*0Sstevel@tonic-gate     /* LINTED */
77*0Sstevel@tonic-gate     if (asn_length + (uint32_t)(bufp - data) > *datalength){
78*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_OVERFLOW);
79*0Sstevel@tonic-gate 	return NULL;
80*0Sstevel@tonic-gate     }
81*0Sstevel@tonic-gate     if (asn_length > intsize){
82*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_DONT_SUPPORT_LARGE_INT);
83*0Sstevel@tonic-gate 	return NULL;
84*0Sstevel@tonic-gate     }
85*0Sstevel@tonic-gate     /* LINTED */
86*0Sstevel@tonic-gate     *datalength -= asn_length + (uint32_t)(bufp - data);
87*0Sstevel@tonic-gate     if (*bufp & 0x80)
88*0Sstevel@tonic-gate 	value = -1; /* integer is negative */
89*0Sstevel@tonic-gate     while(asn_length--)
90*0Sstevel@tonic-gate 	value = (value << 8) | *bufp++;
91*0Sstevel@tonic-gate     *intp = value;
92*0Sstevel@tonic-gate     return bufp;
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * asn_parse_unsigned_int - pulls an unsigned int32_t out of an ASN int type.
97*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
98*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
99*0Sstevel@tonic-gate  *   following the end of this object.
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
102*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
103*0Sstevel@tonic-gate  *  Returns NULL on any error.
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate u_char *
asn_parse_unsigned_int(u_char * data,uint32_t * datalength,u_char * type,int32_t * intp,uint32_t intsize,char * error_label)106*0Sstevel@tonic-gate asn_parse_unsigned_int(
107*0Sstevel@tonic-gate     u_char      *data,      /* IN - pointer to start of object */
108*0Sstevel@tonic-gate     uint32_t *	datalength,/* IN/OUT - number of valid bytes left in buffer */
109*0Sstevel@tonic-gate     u_char      *type,      /* OUT - asn type of object */
110*0Sstevel@tonic-gate     int32_t    *intp,      /* IN/OUT - pointer to start of output buffer */
111*0Sstevel@tonic-gate     uint32_t    intsize,    /* IN - size of output buffer */
112*0Sstevel@tonic-gate     char        *error_label)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * ASN.1 integer ::= 0x02 asnlength byte {byte}*
116*0Sstevel@tonic-gate  */
117*0Sstevel@tonic-gate     u_char *bufp = data;
118*0Sstevel@tonic-gate     uint32_t asn_length;
119*0Sstevel@tonic-gate     uint32_t value = 0;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate     error_label[0] = '\0';
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate     if (intsize != sizeof (int32_t)){
124*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_NOT_LONG);
125*0Sstevel@tonic-gate 	return NULL;
126*0Sstevel@tonic-gate     }
127*0Sstevel@tonic-gate     *type = *bufp++;
128*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp, &asn_length, error_label);
129*0Sstevel@tonic-gate     if (bufp == NULL){
130*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_BAD_LENGTH);
131*0Sstevel@tonic-gate         return NULL;
132*0Sstevel@tonic-gate     }
133*0Sstevel@tonic-gate     /* LINTED */
134*0Sstevel@tonic-gate     if (asn_length + (uint32_t)(bufp - data) > *datalength){
135*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_OVERFLOW);
136*0Sstevel@tonic-gate         return NULL;
137*0Sstevel@tonic-gate     }
138*0Sstevel@tonic-gate     if ((asn_length > (intsize + 1)) ||
139*0Sstevel@tonic-gate         ((asn_length == intsize + 1) && *bufp != 0x00)){
140*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_DONT_SUPPORT_LARGE_INT);
141*0Sstevel@tonic-gate         return NULL;
142*0Sstevel@tonic-gate     }
143*0Sstevel@tonic-gate     /* LINTED */
144*0Sstevel@tonic-gate     *datalength -= asn_length + (uint32_t)(bufp - data);
145*0Sstevel@tonic-gate     if (*bufp & 0x80)
146*0Sstevel@tonic-gate         value = -1U; /* integer is negative */
147*0Sstevel@tonic-gate     while(asn_length--)
148*0Sstevel@tonic-gate         value = (value << 8) | *bufp++;
149*0Sstevel@tonic-gate     *intp = value;
150*0Sstevel@tonic-gate     return bufp;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * asn_build_int - builds an ASN object containing an integer.
156*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
157*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
158*0Sstevel@tonic-gate  *   following the end of this object.
159*0Sstevel@tonic-gate  *
160*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
161*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
162*0Sstevel@tonic-gate  *  Returns NULL on any error.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate u_char *
asn_build_int(u_char * data,uint32_t * datalength,u_char type,int32_t * intp,uint32_t intsize,char * error_label)165*0Sstevel@tonic-gate asn_build_int(
166*0Sstevel@tonic-gate     u_char *data,	/* IN - pointer to start of output buffer */
167*0Sstevel@tonic-gate     uint32_t * datalength,/* IN/OUT - number of valid bytes left in buffer */
168*0Sstevel@tonic-gate     u_char    type,	/* IN - asn type of object */
169*0Sstevel@tonic-gate     int32_t   *intp,	/* IN - pointer to start of integer */
170*0Sstevel@tonic-gate     uint32_t    intsize,    /* IN - size of *intp */
171*0Sstevel@tonic-gate     char *error_label)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * ASN.1 integer ::= 0x02 asnlength byte {byte}*
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate     int32_t integer;
178*0Sstevel@tonic-gate     uint32_t mask;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	error_label[0] = '\0';
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate     if (intsize != sizeof (int32_t))
183*0Sstevel@tonic-gate 	return NULL;
184*0Sstevel@tonic-gate     integer = *intp;
185*0Sstevel@tonic-gate     /*
186*0Sstevel@tonic-gate      * Truncate "unnecessary" bytes off of the most significant end of this 2's
187*0Sstevel@tonic-gate      * complement integer. There should be no sequence of 9 consecutive 1's or
188*0Sstevel@tonic-gate      *  0's at the most significant end of the integer.
189*0Sstevel@tonic-gate      */
190*0Sstevel@tonic-gate 	mask = ((uint32_t) 0x1FF) << ((8 * (sizeof(int32_t) - 1)) - 1);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate     /* mask is 0xFF800000 on a big-endian machine */
193*0Sstevel@tonic-gate     while((((integer & mask) == 0) || ((integer & mask) == mask)) && intsize > 1){
194*0Sstevel@tonic-gate 	intsize--;
195*0Sstevel@tonic-gate 	integer <<= 8;
196*0Sstevel@tonic-gate     }
197*0Sstevel@tonic-gate     data = asn_build_header(data, datalength, type, intsize, error_label);
198*0Sstevel@tonic-gate     if (data == NULL)
199*0Sstevel@tonic-gate 	return NULL;
200*0Sstevel@tonic-gate     if (*datalength < intsize)
201*0Sstevel@tonic-gate 	return NULL;
202*0Sstevel@tonic-gate     *datalength -= intsize;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	mask = ((uint32_t) 0xFF) << (8 * (sizeof(int32_t) - 1));
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate     /* mask is 0xFF000000 on a big-endian machine */
207*0Sstevel@tonic-gate     while(intsize--){
208*0Sstevel@tonic-gate 	/* LINTED */
209*0Sstevel@tonic-gate 	*data++ = (u_char)((integer & mask) >> (8 * (sizeof(int32_t) - 1)));
210*0Sstevel@tonic-gate 	integer <<= 8;
211*0Sstevel@tonic-gate     }
212*0Sstevel@tonic-gate     return data;
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*
216*0Sstevel@tonic-gate  * asn_build_unsigned_int - builds an ASN object containing an integer.
217*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
218*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
219*0Sstevel@tonic-gate  *   following the end of this object.
220*0Sstevel@tonic-gate  *
221*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
222*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
223*0Sstevel@tonic-gate  *  Returns NULL on any error.
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate u_char *
asn_build_unsigned_int(u_char * data,uint32_t * datalength,u_char type,int32_t * intp,uint32_t intsize,char * error_label)226*0Sstevel@tonic-gate asn_build_unsigned_int(
227*0Sstevel@tonic-gate     u_char *data,      /* IN - pointer to start of output buffer */
228*0Sstevel@tonic-gate     uint32_t    *datalength,/* IN/OUT - number of valid bytes left in buffer */
229*0Sstevel@tonic-gate     u_char          type,       /* IN - asn type of object */
230*0Sstevel@tonic-gate     int32_t *intp,      /* IN - pointer to start of int32_t integer */
231*0Sstevel@tonic-gate     uint32_t    intsize,    /* IN - size of *intp */
232*0Sstevel@tonic-gate     char            *error_label)
233*0Sstevel@tonic-gate {
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate  * ASN.1 integer ::= 0x02 asnlength byte {byte}*
236*0Sstevel@tonic-gate  */
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate     uint32_t integer;
239*0Sstevel@tonic-gate     uint32_t mask;
240*0Sstevel@tonic-gate     int add_null_byte = 0;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate     error_label[0] = '\0';
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate     if (intsize != sizeof (int32_t))
245*0Sstevel@tonic-gate         return NULL;
246*0Sstevel@tonic-gate     integer = *intp;
247*0Sstevel@tonic-gate     mask = ((uint32_t) 0xFF) << (8 * (sizeof(int32_t) - 1));
248*0Sstevel@tonic-gate     /* mask is 0xFF000000 on a big-endian machine */
249*0Sstevel@tonic-gate 	/* LINTED */
250*0Sstevel@tonic-gate     if ((u_char)((integer & mask) >> (8 * (sizeof(int32_t) - 1))) & 0x80){
251*0Sstevel@tonic-gate         /* if MSB is set */
252*0Sstevel@tonic-gate         add_null_byte = 1;
253*0Sstevel@tonic-gate         intsize++;
254*0Sstevel@tonic-gate     } else {
255*0Sstevel@tonic-gate         /*
256*0Sstevel@tonic-gate          * Truncate "unnecessary" bytes off of the most significant end of this 2's complement integer.
257*0Sstevel@tonic-gate          * There should be no sequence of 9 consecutive 1's or 0's at the most significant end of the
258*0Sstevel@tonic-gate          * integer.
259*0Sstevel@tonic-gate          */
260*0Sstevel@tonic-gate         mask = ((uint32_t) 0x1FF) << ((8 * (sizeof(int32_t) - 1)) - 1);
261*0Sstevel@tonic-gate         /* mask is 0xFF800000 on a big-endian machine */
262*0Sstevel@tonic-gate         while(((integer & mask) == 0) && intsize > 1){
263*0Sstevel@tonic-gate             intsize--;
264*0Sstevel@tonic-gate             integer <<= 8;
265*0Sstevel@tonic-gate         }
266*0Sstevel@tonic-gate     }
267*0Sstevel@tonic-gate     data = asn_build_header(data, datalength, type, intsize, error_label);
268*0Sstevel@tonic-gate     if (data == NULL)
269*0Sstevel@tonic-gate         return NULL;
270*0Sstevel@tonic-gate     if (*datalength < intsize)
271*0Sstevel@tonic-gate         return NULL;
272*0Sstevel@tonic-gate     *datalength -= intsize;
273*0Sstevel@tonic-gate     if (add_null_byte == 1){
274*0Sstevel@tonic-gate         *data++ = '\0';
275*0Sstevel@tonic-gate         intsize--;
276*0Sstevel@tonic-gate     }
277*0Sstevel@tonic-gate     mask = ((uint32_t) 0xFF) << (8 * (sizeof(int32_t) - 1));
278*0Sstevel@tonic-gate     /* mask is 0xFF000000 on a big-endian machine */
279*0Sstevel@tonic-gate     while(intsize--){
280*0Sstevel@tonic-gate 	/* LINTED */
281*0Sstevel@tonic-gate         *data++ = (u_char)((integer & mask) >> (8 * (sizeof(int32_t) - 1)));
282*0Sstevel@tonic-gate         integer <<= 8;
283*0Sstevel@tonic-gate     }
284*0Sstevel@tonic-gate     return data;
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /*
289*0Sstevel@tonic-gate  * asn_parse_string - pulls an octet string out of an ASN octet string type.
290*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
291*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
292*0Sstevel@tonic-gate  *   following the beginning of the next object.
293*0Sstevel@tonic-gate  *
294*0Sstevel@tonic-gate  *  "string" is filled with the octet string.
295*0Sstevel@tonic-gate  *
296*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
297*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
298*0Sstevel@tonic-gate  *  Returns NULL on any error.
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate u_char *
asn_parse_string(u_char * data,uint32_t * datalength,u_char * type,u_char * string,uint32_t * strlength,char * error_label)301*0Sstevel@tonic-gate asn_parse_string(
302*0Sstevel@tonic-gate     u_char	*data,	    /* IN - pointer to start of object */
303*0Sstevel@tonic-gate     uint32_t    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
304*0Sstevel@tonic-gate     u_char	*type,	    /* OUT - asn type of object */
305*0Sstevel@tonic-gate     u_char	*string,	    /* IN/OUT - pointer to start of output buffer */
306*0Sstevel@tonic-gate     uint32_t    *strlength,     /* IN/OUT - size of output buffer */
307*0Sstevel@tonic-gate     char *error_label)
308*0Sstevel@tonic-gate {
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate  * ASN.1 octet string ::= primstring | cmpdstring
311*0Sstevel@tonic-gate  * primstring ::= 0x04 asnlength byte {byte}*
312*0Sstevel@tonic-gate  * cmpdstring ::= 0x24 asnlength string {string}*
313*0Sstevel@tonic-gate  * This doesn't yet support the compound string.
314*0Sstevel@tonic-gate  */
315*0Sstevel@tonic-gate     u_char *bufp = data;
316*0Sstevel@tonic-gate     uint32_t	    asn_length = 0;
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	error_label[0] = '\0';
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate     *type = *bufp++;
322*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp, &asn_length, error_label);
323*0Sstevel@tonic-gate     if (bufp == NULL)
324*0Sstevel@tonic-gate 	return NULL;
325*0Sstevel@tonic-gate     /* LINTED */
326*0Sstevel@tonic-gate     if (asn_length + (uint32_t)(bufp - data) > *datalength){
327*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_OVERFLOW);
328*0Sstevel@tonic-gate 	return NULL;
329*0Sstevel@tonic-gate     }
330*0Sstevel@tonic-gate     if (asn_length > *strlength){
331*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_DONT_SUPPORT_LARGE_STR);
332*0Sstevel@tonic-gate 	return NULL;
333*0Sstevel@tonic-gate     }
334*0Sstevel@tonic-gate     memcpy(string, bufp, asn_length);
335*0Sstevel@tonic-gate     *strlength = asn_length;
336*0Sstevel@tonic-gate     /* LINTED */
337*0Sstevel@tonic-gate     *datalength -= asn_length + (uint32_t)(bufp - data);
338*0Sstevel@tonic-gate     return bufp + asn_length;
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate /*
343*0Sstevel@tonic-gate  * asn_build_string - Builds an ASN octet string object containing the input string.
344*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
345*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
346*0Sstevel@tonic-gate  *   following the beginning of the next object.
347*0Sstevel@tonic-gate  *
348*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
349*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
350*0Sstevel@tonic-gate  *  Returns NULL on any error.
351*0Sstevel@tonic-gate  */
352*0Sstevel@tonic-gate u_char *
asn_build_string(u_char * data,uint32_t * datalength,u_char type,u_char * string,uint32_t strlength,char * error_label)353*0Sstevel@tonic-gate asn_build_string(
354*0Sstevel@tonic-gate     u_char	    *data,	    /* IN - pointer to start of object */
355*0Sstevel@tonic-gate     uint32_t    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
356*0Sstevel@tonic-gate     u_char	    type,	    /* IN - ASN type of string */
357*0Sstevel@tonic-gate     u_char	    *string,	    /* IN - pointer to start of input buffer */
358*0Sstevel@tonic-gate     uint32_t    strlength,	    /* IN - size of input buffer */
359*0Sstevel@tonic-gate     char *error_label)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate /*
362*0Sstevel@tonic-gate  * ASN.1 octet string ::= primstring | cmpdstring
363*0Sstevel@tonic-gate  * primstring ::= 0x04 asnlength byte {byte}*
364*0Sstevel@tonic-gate  * cmpdstring ::= 0x24 asnlength string {string}*
365*0Sstevel@tonic-gate  * This code will never send a compound string.
366*0Sstevel@tonic-gate  */
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	error_label[0] = '\0';
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate     data = asn_build_header(data, datalength, type, strlength, error_label);
371*0Sstevel@tonic-gate     if (data == NULL)
372*0Sstevel@tonic-gate 	return NULL;
373*0Sstevel@tonic-gate     if (*datalength < strlength)
374*0Sstevel@tonic-gate 	return NULL;
375*0Sstevel@tonic-gate     memcpy(data, string, strlength);
376*0Sstevel@tonic-gate     *datalength -= strlength;
377*0Sstevel@tonic-gate     return data + (intptr_t)strlength;
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate /*
382*0Sstevel@tonic-gate  * asn_parse_header - interprets the ID and length of the current object.
383*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
384*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
385*0Sstevel@tonic-gate  *   in this object following the id and length.
386*0Sstevel@tonic-gate  *
387*0Sstevel@tonic-gate  *  Returns a pointer to the first byte of the contents of this object.
388*0Sstevel@tonic-gate  *  Returns NULL on any error.
389*0Sstevel@tonic-gate  */
390*0Sstevel@tonic-gate u_char *
asn_parse_header(u_char * data,uint32_t * datalength,u_char * type,char * error_label)391*0Sstevel@tonic-gate asn_parse_header(
392*0Sstevel@tonic-gate     u_char	    *data,	/* IN - pointer to start of object */
393*0Sstevel@tonic-gate     uint32_t *	    datalength,/* IN/OUT - number of valid bytes left in buffer */
394*0Sstevel@tonic-gate     u_char	    *type,	/* OUT - ASN type of object */
395*0Sstevel@tonic-gate     char *error_label)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate     u_char *bufp = data;
398*0Sstevel@tonic-gate     uint32_t	    header_len;
399*0Sstevel@tonic-gate     uint32_t	    asn_length = 0;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate     error_label[0] = '\0';
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate     /* this only works on data types < 30, i.e. no extension octets */
404*0Sstevel@tonic-gate     if (IS_EXTENSION_ID(*bufp)){
405*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_CANT_PROCESS_LONG_ID);
406*0Sstevel@tonic-gate 	return NULL;
407*0Sstevel@tonic-gate     }
408*0Sstevel@tonic-gate     *type = *bufp;
409*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp + 1, &asn_length, error_label);
410*0Sstevel@tonic-gate     if (bufp == NULL)
411*0Sstevel@tonic-gate 	return NULL;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate     /* LINTED */
414*0Sstevel@tonic-gate     header_len = (uint32_t)(bufp - data);
415*0Sstevel@tonic-gate     if (header_len + asn_length > *datalength){
416*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_ASN_LEN_TOO_LONG);
417*0Sstevel@tonic-gate 	return NULL;
418*0Sstevel@tonic-gate     }
419*0Sstevel@tonic-gate     *datalength = asn_length;
420*0Sstevel@tonic-gate     return bufp;
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate /*
424*0Sstevel@tonic-gate  * asn_build_header - builds an ASN header for an object with the ID and
425*0Sstevel@tonic-gate  * length specified.
426*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
427*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
428*0Sstevel@tonic-gate  *   in this object following the id and length.
429*0Sstevel@tonic-gate  *
430*0Sstevel@tonic-gate  *  This only works on data types < 30, i.e. no extension octets.
431*0Sstevel@tonic-gate  *  The maximum length is 0xFFFF;
432*0Sstevel@tonic-gate  *
433*0Sstevel@tonic-gate  *  Returns a pointer to the first byte of the contents of this object.
434*0Sstevel@tonic-gate  *  Returns NULL on any error.
435*0Sstevel@tonic-gate  */
436*0Sstevel@tonic-gate u_char *
asn_build_header(u_char * data,uint32_t * datalength,u_char type,uint32_t length,char * error_label)437*0Sstevel@tonic-gate asn_build_header(
438*0Sstevel@tonic-gate     u_char *data,	/* IN - pointer to start of object */
439*0Sstevel@tonic-gate     uint32_t   *datalength,/* IN/OUT - number of valid bytes left in buffer */
440*0Sstevel@tonic-gate     u_char	    type,	/* IN - ASN type of object */
441*0Sstevel@tonic-gate     uint32_t	    length,	/* IN - length of object */
442*0Sstevel@tonic-gate     char *error_label)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	error_label[0] = '\0';
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate     if (*datalength == 0)
447*0Sstevel@tonic-gate 	return NULL;
448*0Sstevel@tonic-gate     *data++ = type;
449*0Sstevel@tonic-gate     (*datalength)--;
450*0Sstevel@tonic-gate     return asn_build_length(data, datalength, length, error_label);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate /*
455*0Sstevel@tonic-gate  * asn_parse_length - interprets the length of the current object.
456*0Sstevel@tonic-gate  *  On exit, length contains the value of this length field.
457*0Sstevel@tonic-gate  *
458*0Sstevel@tonic-gate  *  Returns a pointer to the first byte after this length
459*0Sstevel@tonic-gate  *  field (aka: the start of the data field).
460*0Sstevel@tonic-gate  *  Returns NULL on any error.
461*0Sstevel@tonic-gate  */
462*0Sstevel@tonic-gate u_char *
asn_parse_length(u_char * data,uint32_t * length,char * error_label)463*0Sstevel@tonic-gate asn_parse_length(
464*0Sstevel@tonic-gate     u_char  *data,	/* IN - pointer to start of length field */
465*0Sstevel@tonic-gate     uint32_t  *length,	/* OUT - value of length field */
466*0Sstevel@tonic-gate     char *error_label)
467*0Sstevel@tonic-gate {
468*0Sstevel@tonic-gate     u_char lengthbyte = *data;
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	error_label[0] = '\0';
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate     if (lengthbyte & ASN_LONG_LEN){
473*0Sstevel@tonic-gate 	lengthbyte &= ~ASN_LONG_LEN;	/* turn MSb off */
474*0Sstevel@tonic-gate 	if (lengthbyte == 0){
475*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_DONT_SUPPORT_INDEF_LEN);
476*0Sstevel@tonic-gate 	    return NULL;
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 	if (lengthbyte > sizeof(int32_t)){
479*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_DONT_SUPPORT_SUCH_LEN);
480*0Sstevel@tonic-gate 	    return NULL;
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 	memcpy(length, data + 1, (int)lengthbyte);
483*0Sstevel@tonic-gate 	*length = ntohl(*length);
484*0Sstevel@tonic-gate 	*length >>= (8 * ((sizeof *length) - lengthbyte));
485*0Sstevel@tonic-gate 	return data + lengthbyte + 1;
486*0Sstevel@tonic-gate     } else { /* short asnlength */
487*0Sstevel@tonic-gate 	*length = (int32_t)lengthbyte;
488*0Sstevel@tonic-gate 	return data + 1;
489*0Sstevel@tonic-gate     }
490*0Sstevel@tonic-gate }
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate u_char *
asn_build_length(u_char * data,uint32_t * datalength,uint32_t length,char * error_label)493*0Sstevel@tonic-gate asn_build_length(
494*0Sstevel@tonic-gate     u_char *data,	/* IN - pointer to start of object */
495*0Sstevel@tonic-gate     uint32_t   *datalength, /* IN/OUT - number of valid bytes left in buffer */
496*0Sstevel@tonic-gate     uint32_t    length,	/* IN - length of object */
497*0Sstevel@tonic-gate     char *error_label)
498*0Sstevel@tonic-gate {
499*0Sstevel@tonic-gate     u_char    *start_data = data;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	error_label[0] = '\0';
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate     /* no indefinite lengths sent */
504*0Sstevel@tonic-gate     if (length < 0x80){
505*0Sstevel@tonic-gate 	if (*datalength < 1)
506*0Sstevel@tonic-gate 		goto errout;
507*0Sstevel@tonic-gate 	/* LINTED */
508*0Sstevel@tonic-gate 	*data++ = (u_char)length;
509*0Sstevel@tonic-gate     } else if (length <= 0xFF){
510*0Sstevel@tonic-gate 	if (*datalength < 2)
511*0Sstevel@tonic-gate 		goto errout;
512*0Sstevel@tonic-gate 	/* LINTED */
513*0Sstevel@tonic-gate 	*data++ = (u_char)(0x01 | ASN_LONG_LEN);
514*0Sstevel@tonic-gate 	/* LINTED */
515*0Sstevel@tonic-gate 	*data++ = (u_char)length;
516*0Sstevel@tonic-gate     } else { /* 0xFF < length <= 0xFFFF */
517*0Sstevel@tonic-gate 	if (*datalength < 3)
518*0Sstevel@tonic-gate 		goto errout;
519*0Sstevel@tonic-gate 	/* LINTED */
520*0Sstevel@tonic-gate 	*data++ = (u_char)(0x02 | ASN_LONG_LEN);
521*0Sstevel@tonic-gate 	/* LINTED */
522*0Sstevel@tonic-gate 	*data++ = (u_char)((length >> 8) & 0xFF);
523*0Sstevel@tonic-gate 	/* LINTED */
524*0Sstevel@tonic-gate 	*data++ = (u_char)(length & 0xFF);
525*0Sstevel@tonic-gate     }
526*0Sstevel@tonic-gate     /* LINTED */
527*0Sstevel@tonic-gate     *datalength -= (uint32_t)(data - start_data);
528*0Sstevel@tonic-gate     return data;
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate errout:
531*0Sstevel@tonic-gate     (void)sprintf(error_label, ERR_MSG_BUILD_LENGTH);
532*0Sstevel@tonic-gate     return NULL;
533*0Sstevel@tonic-gate }
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate /*
536*0Sstevel@tonic-gate  * asn_parse_objid - pulls an object indentifier out of an ASN object identifier type.
537*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
538*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
539*0Sstevel@tonic-gate  *   following the beginning of the next object.
540*0Sstevel@tonic-gate  *
541*0Sstevel@tonic-gate  *  "objid" is filled with the object identifier.
542*0Sstevel@tonic-gate  *
543*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
544*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
545*0Sstevel@tonic-gate  *  Returns NULL on any error.
546*0Sstevel@tonic-gate  */
547*0Sstevel@tonic-gate u_char *
asn_parse_objid(u_char * data,uint32_t * datalength,u_char * type,Subid * objid,int32_t * objidlength,char * error_label)548*0Sstevel@tonic-gate asn_parse_objid(
549*0Sstevel@tonic-gate     u_char	    *data,	    /* IN - pointer to start of object */
550*0Sstevel@tonic-gate     uint32_t 	    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
551*0Sstevel@tonic-gate     u_char	    *type,	    /* OUT - ASN type of object */
552*0Sstevel@tonic-gate     Subid	    *objid,	    /* IN/OUT - pointer to start of output buffer */
553*0Sstevel@tonic-gate     int32_t	    *objidlength,   /* IN/OUT - number of sub-id's in objid */
554*0Sstevel@tonic-gate     char *error_label)
555*0Sstevel@tonic-gate {
556*0Sstevel@tonic-gate /*
557*0Sstevel@tonic-gate  * ASN.1 objid ::= 0x06 asnlength subidentifier {subidentifier}*
558*0Sstevel@tonic-gate  * subidentifier ::= {leadingbyte}* lastbyte
559*0Sstevel@tonic-gate  * leadingbyte ::= 1 7bitvalue
560*0Sstevel@tonic-gate  * lastbyte ::= 0 7bitvalue
561*0Sstevel@tonic-gate  */
562*0Sstevel@tonic-gate     u_char *bufp = data;
563*0Sstevel@tonic-gate     Subid *oidp = objid + 1;
564*0Sstevel@tonic-gate     uint32_t subidentifier;
565*0Sstevel@tonic-gate     int32_t   length;
566*0Sstevel@tonic-gate     uint32_t	    asn_length = 0;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	error_label[0] = '\0';
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate     *type = *bufp++;
572*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp, &asn_length, error_label);
573*0Sstevel@tonic-gate     if (bufp == NULL)
574*0Sstevel@tonic-gate 	return NULL;
575*0Sstevel@tonic-gate     /* LINTED */
576*0Sstevel@tonic-gate     if (asn_length + (uint32_t)(bufp - data) > *datalength){
577*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_OVERFLOW);
578*0Sstevel@tonic-gate 	return NULL;
579*0Sstevel@tonic-gate     }
580*0Sstevel@tonic-gate     /* LINTED */
581*0Sstevel@tonic-gate     *datalength -= asn_length + (uint32_t)(bufp - data);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate     length = asn_length;
584*0Sstevel@tonic-gate     (*objidlength)--;	/* account for expansion of first byte */
585*0Sstevel@tonic-gate     while (length > 0 && (*objidlength)-- > 0){
586*0Sstevel@tonic-gate 	subidentifier = 0;
587*0Sstevel@tonic-gate 	do {	/* shift and add in low order 7 bits */
588*0Sstevel@tonic-gate 	    subidentifier = (subidentifier << 7) + (*(u_char *)bufp & ~ASN_BIT8);
589*0Sstevel@tonic-gate 	    length--;
590*0Sstevel@tonic-gate 	} while (*(u_char *)bufp++ & ASN_BIT8);	/* last byte has high bit clear */
591*0Sstevel@tonic-gate 	if (subidentifier > (uint32_t)MAX_SUBID){
592*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_SUBIDENTIFIER_TOO_LONG);
593*0Sstevel@tonic-gate 	    return NULL;
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 	*oidp++ = (Subid)subidentifier;
596*0Sstevel@tonic-gate     }
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate     /*
599*0Sstevel@tonic-gate      * The first two subidentifiers are encoded into the first component
600*0Sstevel@tonic-gate      * with the value (X * 40) + Y, where:
601*0Sstevel@tonic-gate      *	X is the value of the first subidentifier.
602*0Sstevel@tonic-gate      *  Y is the value of the second subidentifier.
603*0Sstevel@tonic-gate      */
604*0Sstevel@tonic-gate     subidentifier = (uint32_t)objid[1];
605*0Sstevel@tonic-gate     /* LINTED */
606*0Sstevel@tonic-gate     objid[1] = (u_char)(subidentifier % 0x28);
607*0Sstevel@tonic-gate     /* LINTED */
608*0Sstevel@tonic-gate     objid[0] = (u_char)((subidentifier - objid[1]) / 0x28);
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate     /* LINTED */
611*0Sstevel@tonic-gate     *objidlength = (int32_t)(oidp - objid);
612*0Sstevel@tonic-gate     return bufp;
613*0Sstevel@tonic-gate }
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate /*
616*0Sstevel@tonic-gate  * asn_build_objid - Builds an ASN object identifier object containing the input string.
617*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
618*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
619*0Sstevel@tonic-gate  *   following the beginning of the next object.
620*0Sstevel@tonic-gate  *
621*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
622*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
623*0Sstevel@tonic-gate  *  Returns NULL on any error.
624*0Sstevel@tonic-gate  */
625*0Sstevel@tonic-gate u_char *
asn_build_objid(u_char * data,uint32_t * datalength,u_char type,Subid * objid,int32_t objidlength,char * error_label)626*0Sstevel@tonic-gate asn_build_objid(
627*0Sstevel@tonic-gate     u_char *data,	    /* IN - pointer to start of object */
628*0Sstevel@tonic-gate     uint32_t    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
629*0Sstevel@tonic-gate     u_char	    type,	    /* IN - ASN type of object */
630*0Sstevel@tonic-gate     Subid	    *objid,	    /* IN - pointer to start of input buffer */
631*0Sstevel@tonic-gate     int32_t	    objidlength,    /* IN - number of sub-id's in objid */
632*0Sstevel@tonic-gate     char *error_label)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate /*
635*0Sstevel@tonic-gate  * ASN.1 objid ::= 0x06 asnlength subidentifier {subidentifier}*
636*0Sstevel@tonic-gate  * subidentifier ::= {leadingbyte}* lastbyte
637*0Sstevel@tonic-gate  * leadingbyte ::= 1 7bitvalue
638*0Sstevel@tonic-gate  * lastbyte ::= 0 7bitvalue
639*0Sstevel@tonic-gate  */
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	uchar_t buf[MAX_OID_LEN * 5];
642*0Sstevel@tonic-gate 	uchar_t *bp = buf;
643*0Sstevel@tonic-gate 	Subid objbuf[MAX_OID_LEN];
644*0Sstevel@tonic-gate 	Subid *op = objbuf;
645*0Sstevel@tonic-gate 	uint32_t    asnlength;
646*0Sstevel@tonic-gate 	uint32_t subid, mask, testmask;
647*0Sstevel@tonic-gate 	int bits, testbits;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	error_label[0] = '\0';
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	if (objidlength > MAX_OID_LEN)
652*0Sstevel@tonic-gate 		return (NULL);
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	memcpy(objbuf, objid, objidlength * (int32_t)sizeof (Subid));
655*0Sstevel@tonic-gate 	/* transform size in bytes to size in subid's */
656*0Sstevel@tonic-gate 	/* encode the first two components into the first subidentifier */
657*0Sstevel@tonic-gate 	op[1] = op[1] + (op[0] * 40);
658*0Sstevel@tonic-gate 	op++;
659*0Sstevel@tonic-gate 	objidlength--;
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	while (objidlength-- > 0){
662*0Sstevel@tonic-gate 	subid = *op++;
663*0Sstevel@tonic-gate 	mask = 0x7F; /* handle subid == 0 case */
664*0Sstevel@tonic-gate 	bits = 0;
665*0Sstevel@tonic-gate 	/* testmask *MUST* !!!! be of an unsigned type */
666*0Sstevel@tonic-gate 	for (testmask = 0x7F, testbits = 0; testmask != 0;
667*0Sstevel@tonic-gate 			testmask <<= 7, testbits += 7) {
668*0Sstevel@tonic-gate 		if (subid & testmask) {	/* if any bits set */
669*0Sstevel@tonic-gate 			mask = testmask;
670*0Sstevel@tonic-gate 			bits = testbits;
671*0Sstevel@tonic-gate 		}
672*0Sstevel@tonic-gate 	}
673*0Sstevel@tonic-gate 	/* mask can't be zero here */
674*0Sstevel@tonic-gate 	for (; mask != 0x7F; mask >>= 7, bits -= 7){
675*0Sstevel@tonic-gate 		if (mask == 0x1E00000)
676*0Sstevel@tonic-gate 			/* fix a mask that got truncated above */
677*0Sstevel@tonic-gate 		mask = 0xFE00000;
678*0Sstevel@tonic-gate 	/* LINTED */
679*0Sstevel@tonic-gate 	*bp++ = (uchar_t)(((subid & mask) >> bits) | ASN_BIT8);
680*0Sstevel@tonic-gate 	}
681*0Sstevel@tonic-gate 	/* LINTED */
682*0Sstevel@tonic-gate 	*bp++ = (uchar_t)(subid & mask);
683*0Sstevel@tonic-gate 	}
684*0Sstevel@tonic-gate 	/* LINTED */
685*0Sstevel@tonic-gate 	asnlength = (uint32_t)(bp - buf);
686*0Sstevel@tonic-gate 	data = asn_build_header(data, datalength, type, asnlength, error_label);
687*0Sstevel@tonic-gate 	if (data == NULL)
688*0Sstevel@tonic-gate 		return (NULL);
689*0Sstevel@tonic-gate 	if (*datalength < asnlength)
690*0Sstevel@tonic-gate 		return (NULL);
691*0Sstevel@tonic-gate 	memcpy(data, buf, asnlength);
692*0Sstevel@tonic-gate 	*datalength -= asnlength;
693*0Sstevel@tonic-gate 	return (data + (uintptr_t)asnlength);
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate /*
697*0Sstevel@tonic-gate  * asn_parse_null - Interprets an ASN null type.
698*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
699*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
700*0Sstevel@tonic-gate  *   following the beginning of the next object.
701*0Sstevel@tonic-gate  *
702*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
703*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
704*0Sstevel@tonic-gate  *  Returns NULL on any error.
705*0Sstevel@tonic-gate  */
706*0Sstevel@tonic-gate u_char *
asn_parse_null(u_char * data,uint32_t * datalength,u_char * type,char * error_label)707*0Sstevel@tonic-gate asn_parse_null(
708*0Sstevel@tonic-gate     u_char	    *data,	    /* IN - pointer to start of object */
709*0Sstevel@tonic-gate     uint32_t	    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
710*0Sstevel@tonic-gate     u_char	    *type,	    /* OUT - ASN type of object */
711*0Sstevel@tonic-gate     char *error_label)
712*0Sstevel@tonic-gate {
713*0Sstevel@tonic-gate /*
714*0Sstevel@tonic-gate  * ASN.1 null ::= 0x05 0x00
715*0Sstevel@tonic-gate  */
716*0Sstevel@tonic-gate     u_char	*bufp = data;
717*0Sstevel@tonic-gate     uint32_t	asn_length = 0;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	error_label[0] = '\0';
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate     *type = *bufp++;
723*0Sstevel@tonic-gate     bufp = asn_parse_length(bufp, &asn_length, error_label);
724*0Sstevel@tonic-gate     if (bufp == NULL)
725*0Sstevel@tonic-gate 	return NULL;
726*0Sstevel@tonic-gate     if (asn_length != 0){
727*0Sstevel@tonic-gate 	(void)sprintf(error_label, ERR_MSG_MALFORMED_NULL);
728*0Sstevel@tonic-gate 	return NULL;
729*0Sstevel@tonic-gate     }
730*0Sstevel@tonic-gate     /* LINTED */
731*0Sstevel@tonic-gate     *datalength -= (uint32_t)(bufp - data);
732*0Sstevel@tonic-gate     return bufp + (uintptr_t)asn_length;
733*0Sstevel@tonic-gate }
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate /*
736*0Sstevel@tonic-gate  * asn_build_null - Builds an ASN null object.
737*0Sstevel@tonic-gate  *  On entry, datalength is input as the number of valid bytes following
738*0Sstevel@tonic-gate  *   "data".  On exit, it is returned as the number of valid bytes
739*0Sstevel@tonic-gate  *   following the beginning of the next object.
740*0Sstevel@tonic-gate  *
741*0Sstevel@tonic-gate  *  Returns a pointer to the first byte past the end
742*0Sstevel@tonic-gate  *   of this object (i.e. the start of the next object).
743*0Sstevel@tonic-gate  *  Returns NULL on any error.
744*0Sstevel@tonic-gate  */
745*0Sstevel@tonic-gate u_char *
asn_build_null(u_char * data,uint32_t * datalength,u_char type,char * error_label)746*0Sstevel@tonic-gate asn_build_null(
747*0Sstevel@tonic-gate     u_char	    *data,	    /* IN - pointer to start of object */
748*0Sstevel@tonic-gate     uint32_t	    *datalength,    /* IN/OUT - number of valid bytes left in buffer */
749*0Sstevel@tonic-gate     u_char	    type,	    /* IN - ASN type of object */
750*0Sstevel@tonic-gate     char *error_label)
751*0Sstevel@tonic-gate {
752*0Sstevel@tonic-gate /*
753*0Sstevel@tonic-gate  * ASN.1 null ::= 0x05 0x00
754*0Sstevel@tonic-gate  */
755*0Sstevel@tonic-gate 	error_label[0] = '\0';
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	return asn_build_header(data, datalength, type, 0, error_label);
758*0Sstevel@tonic-gate }
759