s4 dns: Import DNS win32 error codes from MS-ERREF
[Samba/gebeck_regimport.git] / lib / util / asn1.c
blob21d4bd43088d6a2dbb5f56d76cf9e6eb760c50c8
1 /*
2 Unix SMB/CIFS implementation.
3 simple ASN1 routines
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/>.
20 #include "includes.h"
21 #include "../lib/util/asn1.h"
23 /* allocate an asn1 structure */
24 struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx)
26 struct asn1_data *ret = talloc_zero(mem_ctx, struct asn1_data);
27 if (ret == NULL) {
28 DEBUG(0,("asn1_init failed! out of memory\n"));
30 return ret;
33 /* free an asn1 structure */
34 void asn1_free(struct asn1_data *data)
36 talloc_free(data);
39 /* write to the ASN1 buffer, advancing the buffer pointer */
40 bool asn1_write(struct asn1_data *data, const void *p, int len)
42 if (data->has_error) return false;
43 if (data->length < data->ofs+len) {
44 uint8_t *newp;
45 newp = talloc_realloc(data, data->data, uint8_t, data->ofs+len);
46 if (!newp) {
47 asn1_free(data);
48 data->has_error = true;
49 return false;
51 data->data = newp;
52 data->length = data->ofs+len;
54 memcpy(data->data + data->ofs, p, len);
55 data->ofs += len;
56 return true;
59 /* useful fn for writing a uint8_t */
60 bool asn1_write_uint8(struct asn1_data *data, uint8_t v)
62 return asn1_write(data, &v, 1);
65 /* push a tag onto the asn1 data buffer. Used for nested structures */
66 bool asn1_push_tag(struct asn1_data *data, uint8_t tag)
68 struct nesting *nesting;
70 asn1_write_uint8(data, tag);
71 nesting = talloc(data, struct nesting);
72 if (!nesting) {
73 data->has_error = true;
74 return false;
77 nesting->start = data->ofs;
78 nesting->next = data->nesting;
79 data->nesting = nesting;
80 return asn1_write_uint8(data, 0xff);
83 /* pop a tag */
84 bool asn1_pop_tag(struct asn1_data *data)
86 struct nesting *nesting;
87 size_t len;
89 nesting = data->nesting;
91 if (!nesting) {
92 data->has_error = true;
93 return false;
95 len = data->ofs - (nesting->start+1);
96 /* yes, this is ugly. We don't know in advance how many bytes the length
97 of a tag will take, so we assumed 1 byte. If we were wrong then we
98 need to correct our mistake */
99 if (len > 0xFFFFFF) {
100 data->data[nesting->start] = 0x84;
101 if (!asn1_write_uint8(data, 0)) return false;
102 if (!asn1_write_uint8(data, 0)) return false;
103 if (!asn1_write_uint8(data, 0)) return false;
104 if (!asn1_write_uint8(data, 0)) return false;
105 memmove(data->data+nesting->start+5, data->data+nesting->start+1, len);
106 data->data[nesting->start+1] = (len>>24) & 0xFF;
107 data->data[nesting->start+2] = (len>>16) & 0xFF;
108 data->data[nesting->start+3] = (len>>8) & 0xFF;
109 data->data[nesting->start+4] = len&0xff;
110 } else if (len > 0xFFFF) {
111 data->data[nesting->start] = 0x83;
112 if (!asn1_write_uint8(data, 0)) return false;
113 if (!asn1_write_uint8(data, 0)) return false;
114 if (!asn1_write_uint8(data, 0)) return false;
115 memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
116 data->data[nesting->start+1] = (len>>16) & 0xFF;
117 data->data[nesting->start+2] = (len>>8) & 0xFF;
118 data->data[nesting->start+3] = len&0xff;
119 } else if (len > 255) {
120 data->data[nesting->start] = 0x82;
121 if (!asn1_write_uint8(data, 0)) return false;
122 if (!asn1_write_uint8(data, 0)) return false;
123 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
124 data->data[nesting->start+1] = len>>8;
125 data->data[nesting->start+2] = len&0xff;
126 } else if (len > 127) {
127 data->data[nesting->start] = 0x81;
128 if (!asn1_write_uint8(data, 0)) return false;
129 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
130 data->data[nesting->start+1] = len;
131 } else {
132 data->data[nesting->start] = len;
135 data->nesting = nesting->next;
136 talloc_free(nesting);
137 return true;
140 /* "i" is the one's complement representation, as is the normal result of an
141 * implicit signed->unsigned conversion */
143 static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
145 uint8_t lowest = i & 0xFF;
147 i = i >> 8;
148 if (i != 0)
149 if (!push_int_bigendian(data, i, negative))
150 return false;
152 if (data->nesting->start+1 == data->ofs) {
154 /* We did not write anything yet, looking at the highest
155 * valued byte */
157 if (negative) {
158 /* Don't write leading 0xff's */
159 if (lowest == 0xFF)
160 return true;
162 if ((lowest & 0x80) == 0) {
163 /* The only exception for a leading 0xff is if
164 * the highest bit is 0, which would indicate
165 * a positive value */
166 if (!asn1_write_uint8(data, 0xff))
167 return false;
169 } else {
170 if (lowest & 0x80) {
171 /* The highest bit of a positive integer is 1,
172 * this would indicate a negative number. Push
173 * a 0 to indicate a positive one */
174 if (!asn1_write_uint8(data, 0))
175 return false;
180 return asn1_write_uint8(data, lowest);
183 /* write an Integer without the tag framing. Needed for example for the LDAP
184 * Abandon Operation */
186 bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
188 if (i == -1) {
189 /* -1 is special as it consists of all-0xff bytes. In
190 push_int_bigendian this is the only case that is not
191 properly handled, as all 0xff bytes would be handled as
192 leading ones to be ignored. */
193 return asn1_write_uint8(data, 0xff);
194 } else {
195 return push_int_bigendian(data, i, i<0);
200 /* write an integer */
201 bool asn1_write_Integer(struct asn1_data *data, int i)
203 if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
204 if (!asn1_write_implicit_Integer(data, i)) return false;
205 return asn1_pop_tag(data);
208 /* write a BIT STRING */
209 bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
211 if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
212 if (!asn1_write_uint8(data, padding)) return false;
213 if (!asn1_write(data, p, length)) return false;
214 return asn1_pop_tag(data);
217 bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
219 unsigned int v, v2;
220 const char *p = (const char *)OID;
221 char *newp;
222 int i;
224 if (!isdigit(*p)) return false;
225 v = strtoul(p, &newp, 10);
226 if (newp[0] != '.') return false;
227 p = newp + 1;
229 if (!isdigit(*p)) return false;
230 v2 = strtoul(p, &newp, 10);
231 if (newp[0] != '.') return false;
232 p = newp + 1;
234 /*the ber representation can't use more space then the string one */
235 *blob = data_blob_talloc(mem_ctx, NULL, strlen(OID));
236 if (!blob->data) return false;
238 blob->data[0] = 40*v + v2;
240 i = 1;
241 while (*p) {
242 if (!isdigit(*p)) return false;
243 v = strtoul(p, &newp, 10);
244 if (newp[0] == '.') {
245 p = newp + 1;
246 /* check for empty last component */
247 if (!*p) return false;
248 } else if (newp[0] == '\0') {
249 p = newp;
250 } else {
251 data_blob_free(blob);
252 return false;
254 if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
255 if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
256 if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
257 if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
258 blob->data[i++] = (v&0x7f);
261 blob->length = i;
263 return true;
267 * Serialize partial OID string.
268 * Partial OIDs are in the form:
269 * 1:2.5.6:0x81
270 * 1:2.5.6:0x8182
272 bool ber_write_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *partial_oid)
274 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
275 char *oid = talloc_strdup(tmp_ctx, partial_oid);
276 char *p;
278 /* truncate partial part so ber_write_OID_String() works */
279 p = strchr(oid, ':');
280 if (p) {
281 *p = '\0';
282 p++;
285 if (!ber_write_OID_String(mem_ctx, blob, oid)) {
286 talloc_free(tmp_ctx);
287 return false;
290 /* Add partially encoded sub-identifier */
291 if (p) {
292 DATA_BLOB tmp_blob = strhex_to_data_blob(tmp_ctx, p);
293 data_blob_append(mem_ctx, blob, tmp_blob.data, tmp_blob.length);
296 talloc_free(tmp_ctx);
298 return true;
301 /* write an object ID to a ASN1 buffer */
302 bool asn1_write_OID(struct asn1_data *data, const char *OID)
304 DATA_BLOB blob;
306 if (!asn1_push_tag(data, ASN1_OID)) return false;
308 if (!ber_write_OID_String(NULL, &blob, OID)) {
309 data->has_error = true;
310 return false;
313 if (!asn1_write(data, blob.data, blob.length)) {
314 data_blob_free(&blob);
315 data->has_error = true;
316 return false;
318 data_blob_free(&blob);
319 return asn1_pop_tag(data);
322 /* write an octet string */
323 bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
325 asn1_push_tag(data, ASN1_OCTET_STRING);
326 asn1_write(data, p, length);
327 asn1_pop_tag(data);
328 return !data->has_error;
331 /* write a LDAP string */
332 bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
334 asn1_write(data, s, strlen(s));
335 return !data->has_error;
338 /* write a LDAP string from a DATA_BLOB */
339 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
341 asn1_write(data, s->data, s->length);
342 return !data->has_error;
345 /* write a general string */
346 bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
348 asn1_push_tag(data, ASN1_GENERAL_STRING);
349 asn1_write_LDAPString(data, s);
350 asn1_pop_tag(data);
351 return !data->has_error;
354 bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
356 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
357 asn1_write(data, blob->data, blob->length);
358 asn1_pop_tag(data);
359 return !data->has_error;
362 /* write a BOOLEAN */
363 bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
365 asn1_push_tag(data, ASN1_BOOLEAN);
366 asn1_write_uint8(data, v ? 0xFF : 0);
367 asn1_pop_tag(data);
368 return !data->has_error;
371 bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
373 uint8_t tmp = 0;
374 asn1_start_tag(data, ASN1_BOOLEAN);
375 asn1_read_uint8(data, &tmp);
376 if (tmp == 0xFF) {
377 *v = true;
378 } else {
379 *v = false;
381 asn1_end_tag(data);
382 return !data->has_error;
385 /* write a BOOLEAN in a simple context */
386 bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
388 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context));
389 asn1_write_uint8(data, v ? 0xFF : 0);
390 asn1_pop_tag(data);
391 return !data->has_error;
394 bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
396 uint8_t tmp = 0;
397 asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context));
398 asn1_read_uint8(data, &tmp);
399 if (tmp == 0xFF) {
400 *v = true;
401 } else {
402 *v = false;
404 asn1_end_tag(data);
405 return !data->has_error;
408 /* check a BOOLEAN */
409 bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
411 uint8_t b = 0;
413 asn1_read_uint8(data, &b);
414 if (b != ASN1_BOOLEAN) {
415 data->has_error = true;
416 return false;
418 asn1_read_uint8(data, &b);
419 if (b != v) {
420 data->has_error = true;
421 return false;
423 return !data->has_error;
427 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
428 bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
430 ZERO_STRUCTP(data);
431 data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
432 if (!data->data) {
433 data->has_error = true;
434 return false;
436 data->length = blob.length;
437 return true;
440 /* Peek into an ASN1 buffer, not advancing the pointer */
441 bool asn1_peek(struct asn1_data *data, void *p, int len)
443 if (data->has_error)
444 return false;
446 if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
447 return false;
449 if (data->ofs + len > data->length) {
450 /* we need to mark the buffer as consumed, so the caller knows
451 this was an out of data error, and not a decode error */
452 data->ofs = data->length;
453 return false;
456 memcpy(p, data->data + data->ofs, len);
457 return true;
460 /* read from a ASN1 buffer, advancing the buffer pointer */
461 bool asn1_read(struct asn1_data *data, void *p, int len)
463 if (!asn1_peek(data, p, len)) {
464 data->has_error = true;
465 return false;
468 data->ofs += len;
469 return true;
472 /* read a uint8_t from a ASN1 buffer */
473 bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
475 return asn1_read(data, v, 1);
478 bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
480 return asn1_peek(data, v, 1);
483 bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
485 uint8_t b;
487 if (asn1_tag_remaining(data) <= 0) {
488 return false;
491 if (!asn1_peek_uint8(data, &b))
492 return false;
494 return (b == tag);
498 * just get the needed size the tag would consume
500 bool asn1_peek_tag_needed_size(struct asn1_data *data, uint8_t tag, size_t *size)
502 off_t start_ofs = data->ofs;
503 uint8_t b;
504 size_t taglen = 0;
506 if (data->has_error) {
507 return false;
510 if (!asn1_read_uint8(data, &b)) {
511 data->ofs = start_ofs;
512 data->has_error = false;
513 return false;
516 if (b != tag) {
517 data->ofs = start_ofs;
518 data->has_error = false;
519 return false;
522 if (!asn1_read_uint8(data, &b)) {
523 data->ofs = start_ofs;
524 data->has_error = false;
525 return false;
528 if (b & 0x80) {
529 int n = b & 0x7f;
530 if (!asn1_read_uint8(data, &b)) {
531 data->ofs = start_ofs;
532 data->has_error = false;
533 return false;
535 if (n > 4) {
537 * We should not allow more than 4 bytes
538 * for the encoding of the tag length.
540 * Otherwise we'd overflow the taglen
541 * variable on 32 bit systems.
543 data->ofs = start_ofs;
544 data->has_error = false;
545 return false;
547 taglen = b;
548 while (n > 1) {
549 if (!asn1_read_uint8(data, &b)) {
550 data->ofs = start_ofs;
551 data->has_error = false;
552 return false;
554 taglen = (taglen << 8) | b;
555 n--;
557 } else {
558 taglen = b;
561 *size = (data->ofs - start_ofs) + taglen;
563 data->ofs = start_ofs;
564 data->has_error = false;
565 return true;
568 /* start reading a nested asn1 structure */
569 bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
571 uint8_t b;
572 struct nesting *nesting;
574 if (!asn1_read_uint8(data, &b))
575 return false;
577 if (b != tag) {
578 data->has_error = true;
579 return false;
581 nesting = talloc(data, struct nesting);
582 if (!nesting) {
583 data->has_error = true;
584 return false;
587 if (!asn1_read_uint8(data, &b)) {
588 return false;
591 if (b & 0x80) {
592 int n = b & 0x7f;
593 if (!asn1_read_uint8(data, &b))
594 return false;
595 nesting->taglen = b;
596 while (n > 1) {
597 if (!asn1_read_uint8(data, &b))
598 return false;
599 nesting->taglen = (nesting->taglen << 8) | b;
600 n--;
602 } else {
603 nesting->taglen = b;
605 nesting->start = data->ofs;
606 nesting->next = data->nesting;
607 data->nesting = nesting;
608 if (asn1_tag_remaining(data) == -1) {
609 return false;
611 return !data->has_error;
614 /* stop reading a tag */
615 bool asn1_end_tag(struct asn1_data *data)
617 struct nesting *nesting;
619 /* make sure we read it all */
620 if (asn1_tag_remaining(data) != 0) {
621 data->has_error = true;
622 return false;
625 nesting = data->nesting;
627 if (!nesting) {
628 data->has_error = true;
629 return false;
632 data->nesting = nesting->next;
633 talloc_free(nesting);
634 return true;
637 /* work out how many bytes are left in this nested tag */
638 int asn1_tag_remaining(struct asn1_data *data)
640 int remaining;
641 if (data->has_error) {
642 return -1;
645 if (!data->nesting) {
646 data->has_error = true;
647 return -1;
649 remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
650 if (remaining > (data->length - data->ofs)) {
651 data->has_error = true;
652 return -1;
654 return remaining;
658 * Internal implementation for reading binary OIDs
659 * Reading is done as far in the buffer as valid OID
660 * till buffer ends or not valid sub-identifier is found.
662 static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
663 const char **OID, size_t *bytes_eaten)
665 int i;
666 uint8_t *b;
667 unsigned int v;
668 char *tmp_oid = NULL;
670 if (blob.length < 2) return false;
672 b = blob.data;
674 tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
675 if (!tmp_oid) goto nomem;
676 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
677 if (!tmp_oid) goto nomem;
679 if (bytes_eaten != NULL) {
680 *bytes_eaten = 0;
683 for(i = 1, v = 0; i < blob.length; i++) {
684 v = (v<<7) | (b[i]&0x7f);
685 if ( ! (b[i] & 0x80)) {
686 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
687 v = 0;
688 if (bytes_eaten)
689 *bytes_eaten = i+1;
691 if (!tmp_oid) goto nomem;
694 *OID = tmp_oid;
695 return true;
697 nomem:
698 return false;
701 /* read an object ID from a data blob */
702 bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
704 size_t bytes_eaten;
706 if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten))
707 return false;
709 return (bytes_eaten == blob.length);
713 * Deserialize partial OID string.
714 * Partial OIDs are in the form:
715 * 1:2.5.6:0x81
716 * 1:2.5.6:0x8182
718 bool ber_read_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **partial_oid)
720 size_t bytes_left;
721 size_t bytes_eaten;
722 char *identifier = NULL;
723 char *tmp_oid = NULL;
725 if (!_ber_read_OID_String_impl(mem_ctx, blob, (const char **)&tmp_oid, &bytes_eaten))
726 return false;
728 if (bytes_eaten < blob.length) {
729 bytes_left = blob.length - bytes_eaten;
730 identifier = hex_encode_talloc(mem_ctx, &blob.data[bytes_eaten], bytes_left);
731 if (!identifier) goto nomem;
733 *partial_oid = talloc_asprintf_append_buffer(tmp_oid, ":0x%s", identifier);
734 if (!*partial_oid) goto nomem;
735 TALLOC_FREE(identifier);
736 } else {
737 *partial_oid = tmp_oid;
740 return true;
742 nomem:
743 TALLOC_FREE(identifier);
744 TALLOC_FREE(tmp_oid);
745 return false;
748 /* read an object ID from a ASN1 buffer */
749 bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID)
751 DATA_BLOB blob;
752 int len;
754 if (!asn1_start_tag(data, ASN1_OID)) return false;
756 len = asn1_tag_remaining(data);
757 if (len < 0) {
758 data->has_error = true;
759 return false;
762 blob = data_blob(NULL, len);
763 if (!blob.data) {
764 data->has_error = true;
765 return false;
768 asn1_read(data, blob.data, len);
769 asn1_end_tag(data);
770 if (data->has_error) {
771 data_blob_free(&blob);
772 return false;
775 if (!ber_read_OID_String(mem_ctx, blob, OID)) {
776 data->has_error = true;
777 data_blob_free(&blob);
778 return false;
781 data_blob_free(&blob);
782 return true;
785 /* check that the next object ID is correct */
786 bool asn1_check_OID(struct asn1_data *data, const char *OID)
788 const char *id;
790 if (!asn1_read_OID(data, data, &id)) return false;
792 if (strcmp(id, OID) != 0) {
793 talloc_free(discard_const(id));
794 data->has_error = true;
795 return false;
797 talloc_free(discard_const(id));
798 return true;
801 /* read a LDAPString from a ASN1 buffer */
802 bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
804 int len;
805 len = asn1_tag_remaining(data);
806 if (len < 0) {
807 data->has_error = true;
808 return false;
810 *s = talloc_array(mem_ctx, char, len+1);
811 if (! *s) {
812 data->has_error = true;
813 return false;
815 asn1_read(data, *s, len);
816 (*s)[len] = 0;
817 return !data->has_error;
821 /* read a GeneralString from a ASN1 buffer */
822 bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
824 if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
825 if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
826 return asn1_end_tag(data);
830 /* read a octet string blob */
831 bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
833 int len;
834 ZERO_STRUCTP(blob);
835 if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
836 len = asn1_tag_remaining(data);
837 if (len < 0) {
838 data->has_error = true;
839 return false;
841 *blob = data_blob_talloc(mem_ctx, NULL, len+1);
842 if (!blob->data) {
843 data->has_error = true;
844 return false;
846 asn1_read(data, blob->data, len);
847 asn1_end_tag(data);
848 blob->length--;
849 blob->data[len] = 0;
851 if (data->has_error) {
852 data_blob_free(blob);
853 *blob = data_blob_null;
854 return false;
856 return true;
859 bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
861 int len;
862 ZERO_STRUCTP(blob);
863 if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
864 len = asn1_tag_remaining(data);
865 if (len < 0) {
866 data->has_error = true;
867 return false;
869 *blob = data_blob(NULL, len);
870 if ((len != 0) && (!blob->data)) {
871 data->has_error = true;
872 return false;
874 asn1_read(data, blob->data, len);
875 asn1_end_tag(data);
876 return !data->has_error;
879 /* read an integer without tag*/
880 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
882 uint8_t b;
883 *i = 0;
885 while (!data->has_error && asn1_tag_remaining(data)>0) {
886 if (!asn1_read_uint8(data, &b)) return false;
887 *i = (*i << 8) + b;
889 return !data->has_error;
893 /* read an integer */
894 bool asn1_read_Integer(struct asn1_data *data, int *i)
896 *i = 0;
898 if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
899 if (!asn1_read_implicit_Integer(data, i)) return false;
900 return asn1_end_tag(data);
903 /* read a BIT STRING */
904 bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
906 int len;
907 ZERO_STRUCTP(blob);
908 if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
909 len = asn1_tag_remaining(data);
910 if (len < 0) {
911 data->has_error = true;
912 return false;
914 if (!asn1_read_uint8(data, padding)) return false;
916 *blob = data_blob_talloc(mem_ctx, NULL, len);
917 if (!blob->data) {
918 data->has_error = true;
919 return false;
921 if (asn1_read(data, blob->data, len - 1)) {
922 blob->length--;
923 blob->data[len] = 0;
924 asn1_end_tag(data);
927 if (data->has_error) {
928 data_blob_free(blob);
929 *blob = data_blob_null;
930 *padding = 0;
931 return false;
933 return true;
936 /* read an integer */
937 bool asn1_read_enumerated(struct asn1_data *data, int *v)
939 *v = 0;
941 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
942 while (!data->has_error && asn1_tag_remaining(data)>0) {
943 uint8_t b;
944 asn1_read_uint8(data, &b);
945 *v = (*v << 8) + b;
947 return asn1_end_tag(data);
950 /* check a enumerated value is correct */
951 bool asn1_check_enumerated(struct asn1_data *data, int v)
953 uint8_t b;
954 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
955 asn1_read_uint8(data, &b);
956 asn1_end_tag(data);
958 if (v != b)
959 data->has_error = false;
961 return !data->has_error;
964 /* write an enumerated value to the stream */
965 bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
967 if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
968 asn1_write_uint8(data, v);
969 asn1_pop_tag(data);
970 return !data->has_error;
974 Get us the data just written without copying
976 bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
978 if (asn1->has_error) {
979 return false;
981 if (asn1->nesting != NULL) {
982 return false;
984 blob->data = asn1->data;
985 blob->length = asn1->length;
986 return true;
990 Fill in an asn1 struct without making a copy
992 void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
994 ZERO_STRUCTP(data);
995 data->data = buf;
996 data->length = len;
1000 check if a ASN.1 blob is a full tag
1002 NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
1004 struct asn1_data *asn1 = asn1_init(NULL);
1005 int size;
1007 NT_STATUS_HAVE_NO_MEMORY(asn1);
1009 asn1->data = blob.data;
1010 asn1->length = blob.length;
1011 asn1_start_tag(asn1, tag);
1012 if (asn1->has_error) {
1013 talloc_free(asn1);
1014 return STATUS_MORE_ENTRIES;
1016 size = asn1_tag_remaining(asn1) + asn1->ofs;
1018 talloc_free(asn1);
1020 if (size > blob.length) {
1021 return STATUS_MORE_ENTRIES;
1024 *packet_size = size;
1025 return NT_STATUS_OK;
1028 NTSTATUS asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
1030 struct asn1_data asn1;
1031 size_t size;
1032 bool ok;
1034 ZERO_STRUCT(asn1);
1035 asn1.data = blob.data;
1036 asn1.length = blob.length;
1038 ok = asn1_peek_tag_needed_size(&asn1, tag, &size);
1039 if (!ok) {
1040 return NT_STATUS_INVALID_BUFFER_SIZE;
1043 if (size > blob.length) {
1044 *packet_size = size;
1045 return STATUS_MORE_ENTRIES;
1048 *packet_size = size;
1049 return NT_STATUS_OK;