2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* free an asn1 structure */
25 void asn1_free(ASN1_DATA *data)
27 SAFE_FREE(data->data);
30 /* write to the ASN1 buffer, advancing the buffer pointer */
31 BOOL asn1_write(ASN1_DATA *data, const void *p, int len)
33 if (data->has_error) return False;
34 if (data->length < data->ofs+len) {
36 newp = Realloc(data->data, data->ofs+len);
38 SAFE_FREE(data->data);
39 data->has_error = True;
43 data->length = data->ofs+len;
45 memcpy(data->data + data->ofs, p, len);
50 /* useful fn for writing a uint8 */
51 BOOL asn1_write_uint8(ASN1_DATA *data, uint8 v)
53 return asn1_write(data, &v, 1);
56 /* push a tag onto the asn1 data buffer. Used for nested structures */
57 BOOL asn1_push_tag(ASN1_DATA *data, uint8 tag)
59 struct nesting *nesting;
61 asn1_write_uint8(data, tag);
62 nesting = (struct nesting *)malloc(sizeof(struct nesting));
64 data->has_error = True;
68 nesting->start = data->ofs;
69 nesting->next = data->nesting;
70 data->nesting = nesting;
71 return asn1_write_uint8(data, 0xff);
75 BOOL asn1_pop_tag(ASN1_DATA *data)
77 struct nesting *nesting;
80 nesting = data->nesting;
83 data->has_error = True;
86 len = data->ofs - (nesting->start+1);
87 /* yes, this is ugly. We don't know in advance how many bytes the length
88 of a tag will take, so we assumed 1 byte. If we were wrong then we
89 need to correct our mistake */
91 data->data[nesting->start] = 0x82;
92 if (!asn1_write_uint8(data, 0)) return False;
93 if (!asn1_write_uint8(data, 0)) return False;
94 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
95 data->data[nesting->start+1] = len>>8;
96 data->data[nesting->start+2] = len&0xff;
97 } else if (len > 127) {
98 data->data[nesting->start] = 0x81;
99 if (!asn1_write_uint8(data, 0)) return False;
100 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
101 data->data[nesting->start+1] = len;
103 data->data[nesting->start] = len;
106 data->nesting = nesting->next;
111 /* write an object ID to a ASN1 buffer */
112 BOOL asn1_write_OID(ASN1_DATA *data, const char *OID)
115 const char *p = (const char *)OID;
118 if (!asn1_push_tag(data, ASN1_OID))
120 v = strtol(p, &newp, 10);
122 v2 = strtol(p, &newp, 10);
124 if (!asn1_write_uint8(data, 40*v + v2))
128 v = strtol(p, &newp, 10);
130 if (v >= (1<<28)) asn1_write_uint8(data, 0x80 | ((v>>28)&0xff));
131 if (v >= (1<<21)) asn1_write_uint8(data, 0x80 | ((v>>21)&0xff));
132 if (v >= (1<<14)) asn1_write_uint8(data, 0x80 | ((v>>14)&0xff));
133 if (v >= (1<<7)) asn1_write_uint8(data, 0x80 | ((v>>7)&0xff));
134 if (!asn1_write_uint8(data, v&0x7f))
137 return asn1_pop_tag(data);
140 /* write an octet string */
141 BOOL asn1_write_OctetString(ASN1_DATA *data, const void *p, size_t length)
143 asn1_push_tag(data, ASN1_OCTET_STRING);
144 asn1_write(data, p, length);
146 return !data->has_error;
149 /* write a general string */
150 BOOL asn1_write_GeneralString(ASN1_DATA *data, const char *s)
152 asn1_push_tag(data, ASN1_GENERAL_STRING);
153 asn1_write(data, s, strlen(s));
155 return !data->has_error;
158 /* write a BOOLEAN */
159 BOOL asn1_write_BOOLEAN(ASN1_DATA *data, BOOL v)
161 asn1_write_uint8(data, ASN1_BOOLEAN);
162 asn1_write_uint8(data, v);
163 return !data->has_error;
166 /* check a BOOLEAN */
167 BOOL asn1_check_BOOLEAN(ASN1_DATA *data, BOOL v)
171 asn1_read_uint8(data, &b);
172 if (b != ASN1_BOOLEAN) {
173 data->has_error = True;
176 asn1_read_uint8(data, &b);
178 data->has_error = True;
181 return !data->has_error;
185 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
186 BOOL asn1_load(ASN1_DATA *data, DATA_BLOB blob)
189 data->data = memdup(blob.data, blob.length);
191 data->has_error = True;
194 data->length = blob.length;
198 /* read from a ASN1 buffer, advancing the buffer pointer */
199 BOOL asn1_read(ASN1_DATA *data, void *p, int len)
201 if (data->ofs + len > data->length) {
202 data->has_error = True;
205 memcpy(p, data->data + data->ofs, len);
210 /* read a uint8 from a ASN1 buffer */
211 BOOL asn1_read_uint8(ASN1_DATA *data, uint8 *v)
213 return asn1_read(data, v, 1);
216 /* start reading a nested asn1 structure */
217 BOOL asn1_start_tag(ASN1_DATA *data, uint8 tag)
220 struct nesting *nesting;
222 asn1_read_uint8(data, &b);
224 data->has_error = True;
227 nesting = (struct nesting *)malloc(sizeof(struct nesting));
229 data->has_error = True;
233 asn1_read_uint8(data, &b);
237 data->has_error = True;
240 asn1_read_uint8(data, &b);
243 asn1_read_uint8(data, &b);
244 nesting->taglen = (nesting->taglen << 8) | b;
249 nesting->start = data->ofs;
250 nesting->next = data->nesting;
251 data->nesting = nesting;
252 return !data->has_error;
256 /* stop reading a tag */
257 BOOL asn1_end_tag(ASN1_DATA *data)
259 struct nesting *nesting;
261 /* make sure we read it all */
262 if (asn1_tag_remaining(data) != 0) {
263 data->has_error = True;
267 nesting = data->nesting;
270 data->has_error = True;
274 data->nesting = nesting->next;
279 /* work out how many bytes are left in this nested tag */
280 int asn1_tag_remaining(ASN1_DATA *data)
282 if (!data->nesting) {
283 data->has_error = True;
286 return data->nesting->taglen - (data->ofs - data->nesting->start);
289 /* read an object ID from a ASN1 buffer */
290 BOOL asn1_read_OID(ASN1_DATA *data, char **OID)
296 if (!asn1_start_tag(data, ASN1_OID)) return False;
297 asn1_read_uint8(data, &b);
300 snprintf(el, sizeof(el), "%u", b/40);
302 snprintf(el, sizeof(el), " %u", b%40);
305 while (asn1_tag_remaining(data) > 0) {
308 asn1_read_uint8(data, &b);
309 v = (v<<7) | (b&0x7f);
310 } while (!data->has_error && b & 0x80);
311 snprintf(el, sizeof(el), " %u", v);
319 return !data->has_error;
322 /* check that the next object ID is correct */
323 BOOL asn1_check_OID(ASN1_DATA *data, char *OID)
327 if (!asn1_read_OID(data, &id)) return False;
329 if (strcmp(id, OID) != 0) {
330 data->has_error = True;
337 /* read a GeneralString from a ASN1 buffer */
338 BOOL asn1_read_GeneralString(ASN1_DATA *data, char **s)
341 if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return False;
342 len = asn1_tag_remaining(data);
345 data->has_error = True;
348 asn1_read(data, *s, len);
351 return !data->has_error;
354 /* read a octet string blob */
355 BOOL asn1_read_OctetString(ASN1_DATA *data, DATA_BLOB *blob)
358 if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return False;
359 len = asn1_tag_remaining(data);
360 blob->data = malloc(len);
362 data->has_error = True;
365 asn1_read(data, blob->data, len);
368 return !data->has_error;
371 /* check a enumarted value is correct */
372 BOOL asn1_check_enumerated(ASN1_DATA *data, int v)
375 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return False;
376 asn1_read_uint8(data, &b);
378 return !data->has_error && (v == b);
381 /* check a enumarted value is correct */
382 BOOL asn1_write_enumerated(ASN1_DATA *data, uint8 v)
384 if (!asn1_push_tag(data, ASN1_ENUMERATED)) return False;
385 asn1_write_uint8(data, v);
387 return !data->has_error;