2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* free an asn1 structure */
23 void asn1_free(ASN1_DATA *data)
25 struct nesting *nesting = data->nesting;
28 struct nesting *nnext = nesting->next;
33 SAFE_FREE(data->data);
36 /* write to the ASN1 buffer, advancing the buffer pointer */
37 BOOL asn1_write(ASN1_DATA *data, const void *p, int len)
39 if (data->has_error) return False;
40 if (data->length < data->ofs+len) {
41 data->data = SMB_REALLOC_ARRAY(data->data, unsigned char,
44 data->has_error = True;
47 data->length = data->ofs+len;
49 memcpy(data->data + data->ofs, p, len);
54 /* useful fn for writing a uint8 */
55 BOOL asn1_write_uint8(ASN1_DATA *data, uint8 v)
57 return asn1_write(data, &v, 1);
60 /* push a tag onto the asn1 data buffer. Used for nested structures */
61 BOOL asn1_push_tag(ASN1_DATA *data, uint8 tag)
63 struct nesting *nesting;
65 asn1_write_uint8(data, tag);
66 nesting = SMB_MALLOC_P(struct nesting);
68 data->has_error = True;
72 nesting->start = data->ofs;
73 nesting->next = data->nesting;
74 data->nesting = nesting;
75 return asn1_write_uint8(data, 0xff);
79 BOOL asn1_pop_tag(ASN1_DATA *data)
81 struct nesting *nesting;
84 if (data->has_error) {
88 nesting = data->nesting;
91 data->has_error = True;
94 len = data->ofs - (nesting->start+1);
95 /* yes, this is ugly. We don't know in advance how many bytes the length
96 of a tag will take, so we assumed 1 byte. If we were wrong then we
97 need to correct our mistake */
99 data->data[nesting->start] = 0x83;
100 if (!asn1_write_uint8(data, 0)) return False;
101 if (!asn1_write_uint8(data, 0)) return False;
102 if (!asn1_write_uint8(data, 0)) return False;
103 memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
104 data->data[nesting->start+1] = (len>>16) & 0xFF;
105 data->data[nesting->start+2] = (len>>8) & 0xFF;
106 data->data[nesting->start+3] = len&0xff;
107 } else if (len > 255) {
108 data->data[nesting->start] = 0x82;
109 if (!asn1_write_uint8(data, 0)) return False;
110 if (!asn1_write_uint8(data, 0)) return False;
111 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
112 data->data[nesting->start+1] = len>>8;
113 data->data[nesting->start+2] = len&0xff;
114 } else if (len > 127) {
115 data->data[nesting->start] = 0x81;
116 if (!asn1_write_uint8(data, 0)) return False;
117 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
118 data->data[nesting->start+1] = len;
120 data->data[nesting->start] = len;
123 data->nesting = nesting->next;
129 /* write an integer */
130 BOOL asn1_write_Integer(ASN1_DATA *data, int i)
132 if (!asn1_push_tag(data, ASN1_INTEGER)) return False;
134 asn1_write_uint8(data, i);
137 return asn1_pop_tag(data);
140 /* write an object ID to a ASN1 buffer */
141 BOOL asn1_write_OID(ASN1_DATA *data, const char *OID)
144 const char *p = (const char *)OID;
147 if (!asn1_push_tag(data, ASN1_OID))
149 v = strtol(p, &newp, 10);
151 v2 = strtol(p, &newp, 10);
153 if (!asn1_write_uint8(data, 40*v + v2))
157 v = strtol(p, &newp, 10);
159 if (v >= (1<<28)) asn1_write_uint8(data, 0x80 | ((v>>28)&0xff));
160 if (v >= (1<<21)) asn1_write_uint8(data, 0x80 | ((v>>21)&0xff));
161 if (v >= (1<<14)) asn1_write_uint8(data, 0x80 | ((v>>14)&0xff));
162 if (v >= (1<<7)) asn1_write_uint8(data, 0x80 | ((v>>7)&0xff));
163 if (!asn1_write_uint8(data, v&0x7f))
166 return asn1_pop_tag(data);
169 /* write an octet string */
170 BOOL asn1_write_OctetString(ASN1_DATA *data, const void *p, size_t length)
172 asn1_push_tag(data, ASN1_OCTET_STRING);
173 asn1_write(data, p, length);
175 return !data->has_error;
178 /* write a general string */
179 BOOL asn1_write_GeneralString(ASN1_DATA *data, const char *s)
181 asn1_push_tag(data, ASN1_GENERAL_STRING);
182 asn1_write(data, s, strlen(s));
184 return !data->has_error;
187 /* write a BOOLEAN */
188 BOOL asn1_write_BOOLEAN(ASN1_DATA *data, BOOL v)
190 asn1_write_uint8(data, ASN1_BOOLEAN);
191 asn1_write_uint8(data, v);
192 return !data->has_error;
195 /* write a BOOLEAN - hmm, I suspect this one is the correct one, and the
196 above boolean is bogus. Need to check */
197 BOOL asn1_write_BOOLEAN2(ASN1_DATA *data, BOOL v)
199 asn1_push_tag(data, ASN1_BOOLEAN);
200 asn1_write_uint8(data, v);
202 return !data->has_error;
205 /* check a BOOLEAN */
206 BOOL asn1_check_BOOLEAN(ASN1_DATA *data, BOOL v)
210 asn1_read_uint8(data, &b);
211 if (b != ASN1_BOOLEAN) {
212 data->has_error = True;
215 asn1_read_uint8(data, &b);
217 data->has_error = True;
220 return !data->has_error;
224 /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
225 BOOL asn1_load(ASN1_DATA *data, DATA_BLOB blob)
228 data->data = (unsigned char *)memdup(blob.data, blob.length);
230 data->has_error = True;
233 data->length = blob.length;
237 /* read from a ASN1 buffer, advancing the buffer pointer */
238 BOOL asn1_read(ASN1_DATA *data, void *p, int len)
243 if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len) {
244 data->has_error = True;
248 if (data->ofs + len > data->length) {
249 data->has_error = True;
252 memcpy(p, data->data + data->ofs, len);
257 /* read a uint8 from a ASN1 buffer */
258 BOOL asn1_read_uint8(ASN1_DATA *data, uint8 *v)
260 return asn1_read(data, v, 1);
263 /* start reading a nested asn1 structure */
264 BOOL asn1_start_tag(ASN1_DATA *data, uint8 tag)
267 struct nesting *nesting;
269 if (!asn1_read_uint8(data, &b))
273 data->has_error = True;
276 nesting = SMB_MALLOC_P(struct nesting);
278 data->has_error = True;
282 if (!asn1_read_uint8(data, &b)) {
289 if (!asn1_read_uint8(data, &b)) {
295 if (!asn1_read_uint8(data, &b)) {
299 nesting->taglen = (nesting->taglen << 8) | b;
305 nesting->start = data->ofs;
306 nesting->next = data->nesting;
307 data->nesting = nesting;
308 return !data->has_error;
312 /* stop reading a tag */
313 BOOL asn1_end_tag(ASN1_DATA *data)
315 struct nesting *nesting;
317 /* make sure we read it all */
318 if (asn1_tag_remaining(data) != 0) {
319 data->has_error = True;
323 nesting = data->nesting;
326 data->has_error = True;
330 data->nesting = nesting->next;
335 /* work out how many bytes are left in this nested tag */
336 int asn1_tag_remaining(ASN1_DATA *data)
341 if (!data->nesting) {
342 data->has_error = True;
345 return data->nesting->taglen - (data->ofs - data->nesting->start);
348 /* read an object ID from a ASN1 buffer */
349 BOOL asn1_read_OID(ASN1_DATA *data, char **OID)
357 if (!asn1_start_tag(data, ASN1_OID)) {
360 asn1_read_uint8(data, &b);
363 fstr_sprintf(el, "%u", b/40);
364 pstrcat(oid_str, el);
365 fstr_sprintf(el, " %u", b%40);
366 pstrcat(oid_str, el);
368 while (asn1_tag_remaining(data) > 0) {
371 asn1_read_uint8(data, &b);
372 v = (v<<7) | (b&0x7f);
373 } while (!data->has_error && b & 0x80);
374 fstr_sprintf(el, " %u", v);
375 pstrcat(oid_str, el);
380 if (!data->has_error) {
381 *OID = SMB_STRDUP(oid_str);
384 return !data->has_error;
387 /* check that the next object ID is correct */
388 BOOL asn1_check_OID(ASN1_DATA *data, const char *OID)
392 if (!asn1_read_OID(data, &id)) {
396 if (strcmp(id, OID) != 0) {
397 data->has_error = True;
404 /* read a GeneralString from a ASN1 buffer */
405 BOOL asn1_read_GeneralString(ASN1_DATA *data, char **s)
412 if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) {
415 len = asn1_tag_remaining(data);
417 data->has_error = True;
420 str = SMB_MALLOC_ARRAY(char, len+1);
422 data->has_error = True;
425 asn1_read(data, str, len);
429 if (!data->has_error) {
432 return !data->has_error;
435 /* read a octet string blob */
436 BOOL asn1_read_OctetString(ASN1_DATA *data, DATA_BLOB *blob)
440 if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return False;
441 len = asn1_tag_remaining(data);
443 data->has_error = True;
446 *blob = data_blob(NULL, len);
447 asn1_read(data, blob->data, len);
449 return !data->has_error;
452 /* read an interger */
453 BOOL asn1_read_Integer(ASN1_DATA *data, int *i)
458 if (!asn1_start_tag(data, ASN1_INTEGER)) return False;
459 while (asn1_tag_remaining(data)>0) {
460 asn1_read_uint8(data, &b);
463 return asn1_end_tag(data);
467 /* check a enumarted value is correct */
468 BOOL asn1_check_enumerated(ASN1_DATA *data, int v)
471 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return False;
472 asn1_read_uint8(data, &b);
476 data->has_error = False;
478 return !data->has_error;
481 /* write an enumarted value to the stream */
482 BOOL asn1_write_enumerated(ASN1_DATA *data, uint8 v)
484 if (!asn1_push_tag(data, ASN1_ENUMERATED)) return False;
485 asn1_write_uint8(data, v);
487 return !data->has_error;