s3/docs: Fix typos.
[Samba/gebeck_regimport.git] / lib / util / asn1.c
blobaadaf8643a04d31376ca5f7013fedca881fdb23e
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 bool ber_write_OID_String(DATA_BLOB *blob, const char *OID)
210 uint_t v, v2;
211 const char *p = (const char *)OID;
212 char *newp;
213 int i;
215 v = strtoul(p, &newp, 10);
216 if (newp[0] != '.') return false;
217 p = newp + 1;
219 v2 = strtoul(p, &newp, 10);
220 if (newp[0] != '.') return false;
221 p = newp + 1;
223 /*the ber representation can't use more space then the string one */
224 *blob = data_blob(NULL, strlen(OID));
225 if (!blob->data) return false;
227 blob->data[0] = 40*v + v2;
229 i = 1;
230 while (*p) {
231 v = strtoul(p, &newp, 10);
232 if (newp[0] == '.') {
233 p = newp + 1;
234 } else if (newp[0] == '\0') {
235 p = newp;
236 } else {
237 data_blob_free(blob);
238 return false;
240 if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
241 if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
242 if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
243 if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
244 blob->data[i++] = (v&0x7f);
247 blob->length = i;
249 return true;
252 /* write an object ID to a ASN1 buffer */
253 bool asn1_write_OID(struct asn1_data *data, const char *OID)
255 DATA_BLOB blob;
257 if (!asn1_push_tag(data, ASN1_OID)) return false;
259 if (!ber_write_OID_String(&blob, OID)) {
260 data->has_error = true;
261 return false;
264 if (!asn1_write(data, blob.data, blob.length)) {
265 data->has_error = true;
266 return false;
268 data_blob_free(&blob);
269 return asn1_pop_tag(data);
272 /* write an octet string */
273 bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
275 asn1_push_tag(data, ASN1_OCTET_STRING);
276 asn1_write(data, p, length);
277 asn1_pop_tag(data);
278 return !data->has_error;
281 /* write a LDAP string */
282 bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
284 asn1_write(data, s, strlen(s));
285 return !data->has_error;
288 /* write a LDAP string from a DATA_BLOB */
289 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
291 asn1_write(data, s->data, s->length);
292 return !data->has_error;
295 /* write a general string */
296 bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
298 asn1_push_tag(data, ASN1_GENERAL_STRING);
299 asn1_write_LDAPString(data, s);
300 asn1_pop_tag(data);
301 return !data->has_error;
304 bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
306 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
307 asn1_write(data, blob->data, blob->length);
308 asn1_pop_tag(data);
309 return !data->has_error;
312 /* write a BOOLEAN */
313 bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
315 asn1_push_tag(data, ASN1_BOOLEAN);
316 asn1_write_uint8(data, v ? 0xFF : 0);
317 asn1_pop_tag(data);
318 return !data->has_error;
321 bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
323 uint8_t tmp = 0;
324 asn1_start_tag(data, ASN1_BOOLEAN);
325 asn1_read_uint8(data, &tmp);
326 if (tmp == 0xFF) {
327 *v = true;
328 } else {
329 *v = false;
331 asn1_end_tag(data);
332 return !data->has_error;
335 /* write a BOOLEAN in a simple context */
336 bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
338 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context));
339 asn1_write_uint8(data, v ? 0xFF : 0);
340 asn1_pop_tag(data);
341 return !data->has_error;
344 bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
346 uint8_t tmp = 0;
347 asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context));
348 asn1_read_uint8(data, &tmp);
349 if (tmp == 0xFF) {
350 *v = true;
351 } else {
352 *v = false;
354 asn1_end_tag(data);
355 return !data->has_error;
358 /* check a BOOLEAN */
359 bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
361 uint8_t b = 0;
363 asn1_read_uint8(data, &b);
364 if (b != ASN1_BOOLEAN) {
365 data->has_error = true;
366 return false;
368 asn1_read_uint8(data, &b);
369 if (b != v) {
370 data->has_error = true;
371 return false;
373 return !data->has_error;
377 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
378 bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
380 ZERO_STRUCTP(data);
381 data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
382 if (!data->data) {
383 data->has_error = true;
384 return false;
386 data->length = blob.length;
387 return true;
390 /* Peek into an ASN1 buffer, not advancing the pointer */
391 bool asn1_peek(struct asn1_data *data, void *p, int len)
393 if (data->has_error)
394 return false;
396 if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
397 return false;
399 if (data->ofs + len > data->length) {
400 /* we need to mark the buffer as consumed, so the caller knows
401 this was an out of data error, and not a decode error */
402 data->ofs = data->length;
403 return false;
406 memcpy(p, data->data + data->ofs, len);
407 return true;
410 /* read from a ASN1 buffer, advancing the buffer pointer */
411 bool asn1_read(struct asn1_data *data, void *p, int len)
413 if (!asn1_peek(data, p, len)) {
414 data->has_error = true;
415 return false;
418 data->ofs += len;
419 return true;
422 /* read a uint8_t from a ASN1 buffer */
423 bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
425 return asn1_read(data, v, 1);
428 bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
430 return asn1_peek(data, v, 1);
433 bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
435 uint8_t b;
437 if (asn1_tag_remaining(data) <= 0) {
438 return false;
441 if (!asn1_peek_uint8(data, &b))
442 return false;
444 return (b == tag);
447 /* start reading a nested asn1 structure */
448 bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
450 uint8_t b;
451 struct nesting *nesting;
453 if (!asn1_read_uint8(data, &b))
454 return false;
456 if (b != tag) {
457 data->has_error = true;
458 return false;
460 nesting = talloc(data, struct nesting);
461 if (!nesting) {
462 data->has_error = true;
463 return false;
466 if (!asn1_read_uint8(data, &b)) {
467 return false;
470 if (b & 0x80) {
471 int n = b & 0x7f;
472 if (!asn1_read_uint8(data, &b))
473 return false;
474 nesting->taglen = b;
475 while (n > 1) {
476 if (!asn1_read_uint8(data, &b))
477 return false;
478 nesting->taglen = (nesting->taglen << 8) | b;
479 n--;
481 } else {
482 nesting->taglen = b;
484 nesting->start = data->ofs;
485 nesting->next = data->nesting;
486 data->nesting = nesting;
487 if (asn1_tag_remaining(data) == -1) {
488 return false;
490 return !data->has_error;
493 /* stop reading a tag */
494 bool asn1_end_tag(struct asn1_data *data)
496 struct nesting *nesting;
498 /* make sure we read it all */
499 if (asn1_tag_remaining(data) != 0) {
500 data->has_error = true;
501 return false;
504 nesting = data->nesting;
506 if (!nesting) {
507 data->has_error = true;
508 return false;
511 data->nesting = nesting->next;
512 talloc_free(nesting);
513 return true;
516 /* work out how many bytes are left in this nested tag */
517 int asn1_tag_remaining(struct asn1_data *data)
519 int remaining;
520 if (data->has_error) {
521 return -1;
524 if (!data->nesting) {
525 data->has_error = true;
526 return -1;
528 remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
529 if (remaining > (data->length - data->ofs)) {
530 data->has_error = true;
531 return -1;
533 return remaining;
536 /* read an object ID from a data blob */
537 bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
539 int i;
540 uint8_t *b;
541 uint_t v;
542 char *tmp_oid = NULL;
544 if (blob.length < 2) return false;
546 b = blob.data;
548 tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
549 if (!tmp_oid) goto nomem;
550 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
551 if (!tmp_oid) goto nomem;
553 for(i = 1, v = 0; i < blob.length; i++) {
554 v = (v<<7) | (b[i]&0x7f);
555 if ( ! (b[i] & 0x80)) {
556 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
557 v = 0;
559 if (!tmp_oid) goto nomem;
562 if (v != 0) {
563 talloc_free(tmp_oid);
564 return false;
567 *OID = tmp_oid;
568 return true;
570 nomem:
571 return false;
574 /* read an object ID from a ASN1 buffer */
575 bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID)
577 DATA_BLOB blob;
578 int len;
580 if (!asn1_start_tag(data, ASN1_OID)) return false;
582 len = asn1_tag_remaining(data);
583 if (len < 0) {
584 data->has_error = true;
585 return false;
588 blob = data_blob(NULL, len);
589 if (!blob.data) {
590 data->has_error = true;
591 return false;
594 asn1_read(data, blob.data, len);
595 asn1_end_tag(data);
596 if (data->has_error) {
597 data_blob_free(&blob);
598 return false;
601 if (!ber_read_OID_String(mem_ctx, blob, OID)) {
602 data->has_error = true;
603 data_blob_free(&blob);
604 return false;
607 data_blob_free(&blob);
608 return true;
611 /* check that the next object ID is correct */
612 bool asn1_check_OID(struct asn1_data *data, const char *OID)
614 const char *id;
616 if (!asn1_read_OID(data, data, &id)) return false;
618 if (strcmp(id, OID) != 0) {
619 talloc_free(discard_const(id));
620 data->has_error = true;
621 return false;
623 talloc_free(discard_const(id));
624 return true;
627 /* read a LDAPString from a ASN1 buffer */
628 bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
630 int len;
631 len = asn1_tag_remaining(data);
632 if (len < 0) {
633 data->has_error = true;
634 return false;
636 *s = talloc_array(mem_ctx, char, len+1);
637 if (! *s) {
638 data->has_error = true;
639 return false;
641 asn1_read(data, *s, len);
642 (*s)[len] = 0;
643 return !data->has_error;
647 /* read a GeneralString from a ASN1 buffer */
648 bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
650 if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
651 if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
652 return asn1_end_tag(data);
656 /* read a octet string blob */
657 bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
659 int len;
660 ZERO_STRUCTP(blob);
661 if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
662 len = asn1_tag_remaining(data);
663 if (len < 0) {
664 data->has_error = true;
665 return false;
667 *blob = data_blob_talloc(mem_ctx, NULL, len+1);
668 if (!blob->data) {
669 data->has_error = true;
670 return false;
672 asn1_read(data, blob->data, len);
673 asn1_end_tag(data);
674 blob->length--;
675 blob->data[len] = 0;
677 if (data->has_error) {
678 data_blob_free(blob);
679 *blob = data_blob_null;
680 return false;
682 return true;
685 bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
687 int len;
688 ZERO_STRUCTP(blob);
689 if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
690 len = asn1_tag_remaining(data);
691 if (len < 0) {
692 data->has_error = true;
693 return false;
695 *blob = data_blob(NULL, len);
696 if ((len != 0) && (!blob->data)) {
697 data->has_error = true;
698 return false;
700 asn1_read(data, blob->data, len);
701 asn1_end_tag(data);
702 return !data->has_error;
705 /* read an integer without tag*/
706 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
708 uint8_t b;
709 *i = 0;
711 while (!data->has_error && asn1_tag_remaining(data)>0) {
712 if (!asn1_read_uint8(data, &b)) return false;
713 *i = (*i << 8) + b;
715 return !data->has_error;
719 /* read an integer */
720 bool asn1_read_Integer(struct asn1_data *data, int *i)
722 *i = 0;
724 if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
725 if (!asn1_read_implicit_Integer(data, i)) return false;
726 return asn1_end_tag(data);
729 /* read an integer */
730 bool asn1_read_enumerated(struct asn1_data *data, int *v)
732 *v = 0;
734 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
735 while (!data->has_error && asn1_tag_remaining(data)>0) {
736 uint8_t b;
737 asn1_read_uint8(data, &b);
738 *v = (*v << 8) + b;
740 return asn1_end_tag(data);
743 /* check a enumerated value is correct */
744 bool asn1_check_enumerated(struct asn1_data *data, int v)
746 uint8_t b;
747 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
748 asn1_read_uint8(data, &b);
749 asn1_end_tag(data);
751 if (v != b)
752 data->has_error = false;
754 return !data->has_error;
757 /* write an enumerated value to the stream */
758 bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
760 if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
761 asn1_write_uint8(data, v);
762 asn1_pop_tag(data);
763 return !data->has_error;
767 check if a ASN.1 blob is a full tag
769 NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
771 struct asn1_data *asn1 = asn1_init(NULL);
772 int size;
774 NT_STATUS_HAVE_NO_MEMORY(asn1);
776 asn1->data = blob.data;
777 asn1->length = blob.length;
778 asn1_start_tag(asn1, tag);
779 if (asn1->has_error) {
780 talloc_free(asn1);
781 return STATUS_MORE_ENTRIES;
783 size = asn1_tag_remaining(asn1) + asn1->ofs;
785 talloc_free(asn1);
787 if (size > blob.length) {
788 return STATUS_MORE_ENTRIES;
791 *packet_size = size;
792 return NT_STATUS_OK;