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/>.
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);
28 DEBUG(0,("asn1_init failed! out of memory\n"));
33 /* free an asn1 structure */
34 void asn1_free(struct asn1_data *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;
44 if ((len < 0) || (data->ofs + (size_t)len < data->ofs)) {
45 data->has_error = true;
49 if (data->length < data->ofs+len) {
51 newp = talloc_realloc(data, data->data, uint8_t, data->ofs+len);
53 data->has_error = true;
57 data->length = data->ofs+len;
59 memcpy(data->data + data->ofs, p, len);
64 /* useful fn for writing a uint8_t */
65 bool asn1_write_uint8(struct asn1_data *data, uint8_t v)
67 return asn1_write(data, &v, 1);
70 /* push a tag onto the asn1 data buffer. Used for nested structures */
71 bool asn1_push_tag(struct asn1_data *data, uint8_t tag)
73 struct nesting *nesting;
75 asn1_write_uint8(data, tag);
76 nesting = talloc(data, struct nesting);
78 data->has_error = true;
82 nesting->start = data->ofs;
83 nesting->next = data->nesting;
84 data->nesting = nesting;
85 return asn1_write_uint8(data, 0xff);
89 bool asn1_pop_tag(struct asn1_data *data)
91 struct nesting *nesting;
94 nesting = data->nesting;
97 data->has_error = true;
100 len = data->ofs - (nesting->start+1);
101 /* yes, this is ugly. We don't know in advance how many bytes the length
102 of a tag will take, so we assumed 1 byte. If we were wrong then we
103 need to correct our mistake */
104 if (len > 0xFFFFFF) {
105 data->data[nesting->start] = 0x84;
106 if (!asn1_write_uint8(data, 0)) return false;
107 if (!asn1_write_uint8(data, 0)) return false;
108 if (!asn1_write_uint8(data, 0)) return false;
109 if (!asn1_write_uint8(data, 0)) return false;
110 memmove(data->data+nesting->start+5, data->data+nesting->start+1, len);
111 data->data[nesting->start+1] = (len>>24) & 0xFF;
112 data->data[nesting->start+2] = (len>>16) & 0xFF;
113 data->data[nesting->start+3] = (len>>8) & 0xFF;
114 data->data[nesting->start+4] = len&0xff;
115 } else if (len > 0xFFFF) {
116 data->data[nesting->start] = 0x83;
117 if (!asn1_write_uint8(data, 0)) return false;
118 if (!asn1_write_uint8(data, 0)) return false;
119 if (!asn1_write_uint8(data, 0)) return false;
120 memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
121 data->data[nesting->start+1] = (len>>16) & 0xFF;
122 data->data[nesting->start+2] = (len>>8) & 0xFF;
123 data->data[nesting->start+3] = len&0xff;
124 } else if (len > 255) {
125 data->data[nesting->start] = 0x82;
126 if (!asn1_write_uint8(data, 0)) return false;
127 if (!asn1_write_uint8(data, 0)) return false;
128 memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
129 data->data[nesting->start+1] = len>>8;
130 data->data[nesting->start+2] = len&0xff;
131 } else if (len > 127) {
132 data->data[nesting->start] = 0x81;
133 if (!asn1_write_uint8(data, 0)) return false;
134 memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
135 data->data[nesting->start+1] = len;
137 data->data[nesting->start] = len;
140 data->nesting = nesting->next;
141 talloc_free(nesting);
145 /* "i" is the one's complement representation, as is the normal result of an
146 * implicit signed->unsigned conversion */
148 static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
150 uint8_t lowest = i & 0xFF;
154 if (!push_int_bigendian(data, i, negative))
157 if (data->nesting->start+1 == data->ofs) {
159 /* We did not write anything yet, looking at the highest
163 /* Don't write leading 0xff's */
167 if ((lowest & 0x80) == 0) {
168 /* The only exception for a leading 0xff is if
169 * the highest bit is 0, which would indicate
170 * a positive value */
171 if (!asn1_write_uint8(data, 0xff))
176 /* The highest bit of a positive integer is 1,
177 * this would indicate a negative number. Push
178 * a 0 to indicate a positive one */
179 if (!asn1_write_uint8(data, 0))
185 return asn1_write_uint8(data, lowest);
188 /* write an Integer without the tag framing. Needed for example for the LDAP
189 * Abandon Operation */
191 bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
194 /* -1 is special as it consists of all-0xff bytes. In
195 push_int_bigendian this is the only case that is not
196 properly handled, as all 0xff bytes would be handled as
197 leading ones to be ignored. */
198 return asn1_write_uint8(data, 0xff);
200 return push_int_bigendian(data, i, i<0);
205 /* write an integer */
206 bool asn1_write_Integer(struct asn1_data *data, int i)
208 if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
209 if (!asn1_write_implicit_Integer(data, i)) return false;
210 return asn1_pop_tag(data);
213 /* write a BIT STRING */
214 bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
216 if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
217 if (!asn1_write_uint8(data, padding)) return false;
218 if (!asn1_write(data, p, length)) return false;
219 return asn1_pop_tag(data);
222 bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
225 const char *p = (const char *)OID;
229 if (!isdigit(*p)) return false;
230 v = strtoul(p, &newp, 10);
231 if (newp[0] != '.') return false;
234 if (!isdigit(*p)) return false;
235 v2 = strtoul(p, &newp, 10);
236 if (newp[0] != '.') return false;
239 /*the ber representation can't use more space then the string one */
240 *blob = data_blob_talloc(mem_ctx, NULL, strlen(OID));
241 if (!blob->data) return false;
243 blob->data[0] = 40*v + v2;
247 if (!isdigit(*p)) return false;
248 v = strtoul(p, &newp, 10);
249 if (newp[0] == '.') {
251 /* check for empty last component */
252 if (!*p) return false;
253 } else if (newp[0] == '\0') {
256 data_blob_free(blob);
259 if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
260 if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
261 if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
262 if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
263 blob->data[i++] = (v&0x7f);
272 * Serialize partial OID string.
273 * Partial OIDs are in the form:
277 bool ber_write_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *partial_oid)
279 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
280 char *oid = talloc_strdup(tmp_ctx, partial_oid);
283 /* truncate partial part so ber_write_OID_String() works */
284 p = strchr(oid, ':');
290 if (!ber_write_OID_String(mem_ctx, blob, oid)) {
291 talloc_free(tmp_ctx);
295 /* Add partially encoded sub-identifier */
297 DATA_BLOB tmp_blob = strhex_to_data_blob(tmp_ctx, p);
298 if (!data_blob_append(mem_ctx, blob, tmp_blob.data,
300 talloc_free(tmp_ctx);
305 talloc_free(tmp_ctx);
310 /* write an object ID to a ASN1 buffer */
311 bool asn1_write_OID(struct asn1_data *data, const char *OID)
315 if (!asn1_push_tag(data, ASN1_OID)) return false;
317 if (!ber_write_OID_String(NULL, &blob, OID)) {
318 data->has_error = true;
322 if (!asn1_write(data, blob.data, blob.length)) {
323 data_blob_free(&blob);
324 data->has_error = true;
327 data_blob_free(&blob);
328 return asn1_pop_tag(data);
331 /* write an octet string */
332 bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
334 if (!asn1_push_tag(data, ASN1_OCTET_STRING)) return false;
335 if (!asn1_write(data, p, length)) return false;
336 return asn1_pop_tag(data);
339 /* write a LDAP string */
340 bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
342 return asn1_write(data, s, strlen(s));
345 /* write a LDAP string from a DATA_BLOB */
346 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
348 return asn1_write(data, s->data, s->length);
351 /* write a general string */
352 bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
354 if (!asn1_push_tag(data, ASN1_GENERAL_STRING)) return false;
355 if (!asn1_write_LDAPString(data, s)) return false;
356 return asn1_pop_tag(data);
359 bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
361 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
362 if (!asn1_write(data, blob->data, blob->length)) return false;
363 return asn1_pop_tag(data);
366 /* write a BOOLEAN */
367 bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
369 if (!asn1_push_tag(data, ASN1_BOOLEAN)) return false;
370 if (!asn1_write_uint8(data, v ? 0xFF : 0)) return false;
371 return asn1_pop_tag(data);
374 bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
377 if (!asn1_start_tag(data, ASN1_BOOLEAN)) return false;
379 if (!asn1_read_uint8(data, &tmp)) return false;
383 return asn1_end_tag(data);
386 /* write a BOOLEAN in a simple context */
387 bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
389 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context))) return false;
390 if (!asn1_write_uint8(data, v ? 0xFF : 0)) return false;
391 return asn1_pop_tag(data);
394 bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
397 if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context))) return false;
399 if (!asn1_read_uint8(data, &tmp)) return false;
403 return asn1_end_tag(data);
406 /* check a BOOLEAN */
407 bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
411 if (!asn1_read_uint8(data, &b)) return false;
412 if (b != ASN1_BOOLEAN) {
413 data->has_error = true;
416 if (!asn1_read_uint8(data, &b)) return false;
418 data->has_error = true;
421 return !data->has_error;
425 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
426 bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
429 data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
431 data->has_error = true;
434 data->length = blob.length;
438 /* Peek into an ASN1 buffer, not advancing the pointer */
439 bool asn1_peek(struct asn1_data *data, void *p, int len)
444 if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
447 if (data->ofs + len > data->length) {
448 /* we need to mark the buffer as consumed, so the caller knows
449 this was an out of data error, and not a decode error */
450 data->ofs = data->length;
454 memcpy(p, data->data + data->ofs, len);
458 /* read from a ASN1 buffer, advancing the buffer pointer */
459 bool asn1_read(struct asn1_data *data, void *p, int len)
461 if (!asn1_peek(data, p, len)) {
462 data->has_error = true;
470 /* read a uint8_t from a ASN1 buffer */
471 bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
473 return asn1_read(data, v, 1);
476 bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
478 return asn1_peek(data, v, 1);
481 bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
485 if (asn1_tag_remaining(data) <= 0) {
489 if (!asn1_peek_uint8(data, &b))
496 * just get the needed size the tag would consume
498 bool asn1_peek_tag_needed_size(struct asn1_data *data, uint8_t tag, size_t *size)
500 off_t start_ofs = data->ofs;
504 if (data->has_error) {
508 if (!asn1_read_uint8(data, &b)) {
509 data->ofs = start_ofs;
510 data->has_error = false;
515 data->ofs = start_ofs;
516 data->has_error = false;
520 if (!asn1_read_uint8(data, &b)) {
521 data->ofs = start_ofs;
522 data->has_error = false;
528 if (!asn1_read_uint8(data, &b)) {
529 data->ofs = start_ofs;
530 data->has_error = false;
535 * We should not allow more than 4 bytes
536 * for the encoding of the tag length.
538 * Otherwise we'd overflow the taglen
539 * variable on 32 bit systems.
541 data->ofs = start_ofs;
542 data->has_error = false;
547 if (!asn1_read_uint8(data, &b)) {
548 data->ofs = start_ofs;
549 data->has_error = false;
552 taglen = (taglen << 8) | b;
559 *size = (data->ofs - start_ofs) + taglen;
561 data->ofs = start_ofs;
562 data->has_error = false;
566 /* start reading a nested asn1 structure */
567 bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
570 struct nesting *nesting;
572 if (!asn1_read_uint8(data, &b))
576 data->has_error = true;
579 nesting = talloc(data, struct nesting);
581 data->has_error = true;
585 if (!asn1_read_uint8(data, &b)) {
591 if (!asn1_read_uint8(data, &b))
595 if (!asn1_read_uint8(data, &b))
597 nesting->taglen = (nesting->taglen << 8) | b;
603 nesting->start = data->ofs;
604 nesting->next = data->nesting;
605 data->nesting = nesting;
606 if (asn1_tag_remaining(data) == -1) {
609 return !data->has_error;
612 /* stop reading a tag */
613 bool asn1_end_tag(struct asn1_data *data)
615 struct nesting *nesting;
617 /* make sure we read it all */
618 if (asn1_tag_remaining(data) != 0) {
619 data->has_error = true;
623 nesting = data->nesting;
626 data->has_error = true;
630 data->nesting = nesting->next;
631 talloc_free(nesting);
635 /* work out how many bytes are left in this nested tag */
636 int asn1_tag_remaining(struct asn1_data *data)
639 if (data->has_error) {
643 if (!data->nesting) {
644 data->has_error = true;
647 remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
648 if (remaining > (data->length - data->ofs)) {
649 data->has_error = true;
656 * Internal implementation for reading binary OIDs
657 * Reading is done as far in the buffer as valid OID
658 * till buffer ends or not valid sub-identifier is found.
660 static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
661 char **OID, size_t *bytes_eaten)
666 char *tmp_oid = NULL;
668 if (blob.length < 2) return false;
672 tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
673 if (!tmp_oid) goto nomem;
674 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
675 if (!tmp_oid) goto nomem;
677 if (bytes_eaten != NULL) {
681 for(i = 1, v = 0; i < blob.length; i++) {
682 v = (v<<7) | (b[i]&0x7f);
683 if ( ! (b[i] & 0x80)) {
684 tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
689 if (!tmp_oid) goto nomem;
699 /* read an object ID from a data blob */
700 bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, char **OID)
704 if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten))
707 return (bytes_eaten == blob.length);
711 * Deserialize partial OID string.
712 * Partial OIDs are in the form:
716 bool ber_read_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
721 char *identifier = NULL;
722 char *tmp_oid = NULL;
724 if (!_ber_read_OID_String_impl(mem_ctx, blob, &tmp_oid, &bytes_eaten))
727 if (bytes_eaten < blob.length) {
728 bytes_left = blob.length - bytes_eaten;
729 identifier = hex_encode_talloc(mem_ctx, &blob.data[bytes_eaten], bytes_left);
730 if (!identifier) goto nomem;
732 *partial_oid = talloc_asprintf_append_buffer(tmp_oid, ":0x%s", identifier);
733 if (!*partial_oid) goto nomem;
734 TALLOC_FREE(identifier);
736 *partial_oid = tmp_oid;
742 TALLOC_FREE(identifier);
743 TALLOC_FREE(tmp_oid);
747 /* read an object ID from a ASN1 buffer */
748 bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **OID)
753 if (!asn1_start_tag(data, ASN1_OID)) return false;
755 len = asn1_tag_remaining(data);
757 data->has_error = true;
761 blob = data_blob(NULL, len);
763 data->has_error = true;
767 if (!asn1_read(data, blob.data, len)) return false;
768 if (!asn1_end_tag(data)) {
769 data_blob_free(&blob);
773 if (!ber_read_OID_String(mem_ctx, blob, OID)) {
774 data->has_error = true;
775 data_blob_free(&blob);
779 data_blob_free(&blob);
783 /* check that the next object ID is correct */
784 bool asn1_check_OID(struct asn1_data *data, const char *OID)
788 if (!asn1_read_OID(data, data, &id)) return false;
790 if (strcmp(id, OID) != 0) {
792 data->has_error = true;
799 /* read a LDAPString from a ASN1 buffer */
800 bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
803 len = asn1_tag_remaining(data);
805 data->has_error = true;
808 *s = talloc_array(mem_ctx, char, len+1);
810 data->has_error = true;
814 return asn1_read(data, *s, len);
818 /* read a GeneralString from a ASN1 buffer */
819 bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
821 if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
822 if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
823 return asn1_end_tag(data);
827 /* read a octet string blob */
828 bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
832 if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
833 len = asn1_tag_remaining(data);
835 data->has_error = true;
838 *blob = data_blob_talloc(mem_ctx, NULL, len+1);
839 if (!blob->data || blob->length < len) {
840 data->has_error = true;
843 if (!asn1_read(data, blob->data, len)) goto err;
844 if (!asn1_end_tag(data)) goto err;
851 data_blob_free(blob);
852 *blob = data_blob_null;
856 bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
860 if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
861 len = asn1_tag_remaining(data);
863 data->has_error = true;
866 *blob = data_blob(NULL, len);
867 if ((len != 0) && (!blob->data)) {
868 data->has_error = true;
871 if (!asn1_read(data, blob->data, len)) return false;
872 return asn1_end_tag(data);
875 /* read an integer without tag*/
876 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
879 bool first_byte = true;
882 while (!data->has_error && asn1_tag_remaining(data)>0) {
883 if (!asn1_read_uint8(data, &b)) return false;
886 /* Number is negative.
887 Set i to -1 for sign extend. */
894 return !data->has_error;
898 /* read an integer */
899 bool asn1_read_Integer(struct asn1_data *data, int *i)
903 if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
904 if (!asn1_read_implicit_Integer(data, i)) return false;
905 return asn1_end_tag(data);
908 /* read a BIT STRING */
909 bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
913 if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
914 len = asn1_tag_remaining(data);
916 data->has_error = true;
919 if (!asn1_read_uint8(data, padding)) return false;
921 *blob = data_blob_talloc(mem_ctx, NULL, len+1);
922 if (!blob->data || blob->length < len) {
923 data->has_error = true;
926 if (asn1_read(data, blob->data, len - 1)) {
932 if (data->has_error) {
933 data_blob_free(blob);
934 *blob = data_blob_null;
941 /* read an integer */
942 bool asn1_read_enumerated(struct asn1_data *data, int *v)
946 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
947 while (!data->has_error && asn1_tag_remaining(data)>0) {
949 if (!asn1_read_uint8(data, &b)) {
954 return asn1_end_tag(data);
957 /* check a enumerated value is correct */
958 bool asn1_check_enumerated(struct asn1_data *data, int v)
961 if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
962 if (!asn1_read_uint8(data, &b)) return false;
963 if (!asn1_end_tag(data)) return false;
966 data->has_error = false;
968 return !data->has_error;
971 /* write an enumerated value to the stream */
972 bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
974 if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
975 if (!asn1_write_uint8(data, v)) return false;
976 return asn1_pop_tag(data);
980 Get us the data just written without copying
982 bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
984 if (asn1->has_error) {
987 if (asn1->nesting != NULL) {
990 blob->data = asn1->data;
991 blob->length = asn1->length;
996 Fill in an asn1 struct without making a copy
998 void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
1005 int asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
1007 struct asn1_data asn1;
1012 asn1.data = blob.data;
1013 asn1.length = blob.length;
1015 ok = asn1_peek_tag_needed_size(&asn1, tag, &size);
1020 if (size > blob.length) {
1021 *packet_size = size;
1025 *packet_size = size;