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 "system/locale.h"
22 #include "lib/util/asn1.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/samba_util.h"
25 #include "lib/util/smb_strtox.h"
29 size_t taglen
; /* for parsing */
38 struct nesting
*nesting
;
44 /* allocate an asn1 structure */
45 struct asn1_data
*asn1_init(TALLOC_CTX
*mem_ctx
, unsigned max_depth
)
47 struct asn1_data
*ret
= talloc_zero(mem_ctx
, struct asn1_data
);
49 DBG_ERR("asn1_init failed! out of memory\n");
52 ret
->max_depth
= max_depth
;
56 /* free an asn1 structure */
57 void asn1_free(struct asn1_data
*data
)
62 bool asn1_has_error(const struct asn1_data
*data
)
64 return data
->has_error
;
67 void asn1_set_error(struct asn1_data
*data
)
69 data
->has_error
= true;
72 bool asn1_has_nesting(const struct asn1_data
*data
)
74 return data
->nesting
!= NULL
;
77 off_t
asn1_current_ofs(const struct asn1_data
*data
)
82 /* write to the ASN1 buffer, advancing the buffer pointer */
83 bool asn1_write(struct asn1_data
*data
, const void *p
, int len
)
85 if (data
->has_error
) return false;
87 if ((len
< 0) || (data
->ofs
+ (size_t)len
< data
->ofs
)) {
88 data
->has_error
= true;
92 if (data
->length
< data
->ofs
+len
) {
94 newp
= talloc_realloc(data
, data
->data
, uint8_t, data
->ofs
+len
);
96 data
->has_error
= true;
100 data
->length
= data
->ofs
+len
;
103 memcpy(data
->data
+ data
->ofs
, p
, len
);
109 /* useful fn for writing a uint8_t */
110 bool asn1_write_uint8(struct asn1_data
*data
, uint8_t v
)
112 return asn1_write(data
, &v
, 1);
115 /* push a tag onto the asn1 data buffer. Used for nested structures */
116 bool asn1_push_tag(struct asn1_data
*data
, uint8_t tag
)
118 struct nesting
*nesting
;
120 if (!asn1_write_uint8(data
, tag
)) {
123 nesting
= talloc(data
, struct nesting
);
125 data
->has_error
= true;
129 nesting
->start
= data
->ofs
;
130 nesting
->next
= data
->nesting
;
131 data
->nesting
= nesting
;
132 return asn1_write_uint8(data
, 0xff);
136 bool asn1_pop_tag(struct asn1_data
*data
)
138 struct nesting
*nesting
;
141 if (data
->has_error
) {
145 nesting
= data
->nesting
;
148 data
->has_error
= true;
151 len
= data
->ofs
- (nesting
->start
+1);
152 /* yes, this is ugly. We don't know in advance how many bytes the length
153 of a tag will take, so we assumed 1 byte. If we were wrong then we
154 need to correct our mistake */
155 if (len
> 0xFFFFFF) {
156 data
->data
[nesting
->start
] = 0x84;
157 if (!asn1_write_uint8(data
, 0)) return false;
158 if (!asn1_write_uint8(data
, 0)) return false;
159 if (!asn1_write_uint8(data
, 0)) return false;
160 if (!asn1_write_uint8(data
, 0)) return false;
161 memmove(data
->data
+nesting
->start
+5, data
->data
+nesting
->start
+1, len
);
162 data
->data
[nesting
->start
+1] = (len
>>24) & 0xFF;
163 data
->data
[nesting
->start
+2] = (len
>>16) & 0xFF;
164 data
->data
[nesting
->start
+3] = (len
>>8) & 0xFF;
165 data
->data
[nesting
->start
+4] = len
&0xff;
166 } else if (len
> 0xFFFF) {
167 data
->data
[nesting
->start
] = 0x83;
168 if (!asn1_write_uint8(data
, 0)) return false;
169 if (!asn1_write_uint8(data
, 0)) return false;
170 if (!asn1_write_uint8(data
, 0)) return false;
171 memmove(data
->data
+nesting
->start
+4, data
->data
+nesting
->start
+1, len
);
172 data
->data
[nesting
->start
+1] = (len
>>16) & 0xFF;
173 data
->data
[nesting
->start
+2] = (len
>>8) & 0xFF;
174 data
->data
[nesting
->start
+3] = len
&0xff;
175 } else if (len
> 255) {
176 data
->data
[nesting
->start
] = 0x82;
177 if (!asn1_write_uint8(data
, 0)) return false;
178 if (!asn1_write_uint8(data
, 0)) return false;
179 memmove(data
->data
+nesting
->start
+3, data
->data
+nesting
->start
+1, len
);
180 data
->data
[nesting
->start
+1] = len
>>8;
181 data
->data
[nesting
->start
+2] = len
&0xff;
182 } else if (len
> 127) {
183 data
->data
[nesting
->start
] = 0x81;
184 if (!asn1_write_uint8(data
, 0)) return false;
185 memmove(data
->data
+nesting
->start
+2, data
->data
+nesting
->start
+1, len
);
186 data
->data
[nesting
->start
+1] = len
;
188 data
->data
[nesting
->start
] = len
;
191 data
->nesting
= nesting
->next
;
192 talloc_free(nesting
);
196 /* "i" is the one's complement representation, as is the normal result of an
197 * implicit signed->unsigned conversion */
199 static bool push_int_bigendian(struct asn1_data
*data
, unsigned int i
, bool negative
)
201 uint8_t lowest
= i
& 0xFF;
205 if (!push_int_bigendian(data
, i
, negative
))
208 if (data
->nesting
->start
+1 == data
->ofs
) {
210 /* We did not write anything yet, looking at the highest
214 /* Don't write leading 0xff's */
218 if ((lowest
& 0x80) == 0) {
219 /* The only exception for a leading 0xff is if
220 * the highest bit is 0, which would indicate
221 * a positive value */
222 if (!asn1_write_uint8(data
, 0xff))
227 /* The highest bit of a positive integer is 1,
228 * this would indicate a negative number. Push
229 * a 0 to indicate a positive one */
230 if (!asn1_write_uint8(data
, 0))
236 return asn1_write_uint8(data
, lowest
);
239 /* write an Integer without the tag framing. Needed for example for the LDAP
240 * Abandon Operation */
242 bool asn1_write_implicit_Integer(struct asn1_data
*data
, int i
)
244 if (data
->has_error
) {
249 /* -1 is special as it consists of all-0xff bytes. In
250 push_int_bigendian this is the only case that is not
251 properly handled, as all 0xff bytes would be handled as
252 leading ones to be ignored. */
253 return asn1_write_uint8(data
, 0xff);
255 return push_int_bigendian(data
, i
, i
<0);
260 /* write an integer */
261 bool asn1_write_Integer(struct asn1_data
*data
, int i
)
263 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return false;
264 if (!asn1_write_implicit_Integer(data
, i
)) return false;
265 return asn1_pop_tag(data
);
268 /* write a BIT STRING */
269 bool asn1_write_BitString(struct asn1_data
*data
, const void *p
, size_t length
, uint8_t padding
)
271 if (!asn1_push_tag(data
, ASN1_BIT_STRING
)) return false;
272 if (!asn1_write_uint8(data
, padding
)) return false;
273 if (!asn1_write(data
, p
, length
)) return false;
274 return asn1_pop_tag(data
);
277 bool ber_write_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *OID
)
279 unsigned long int v
, v2
;
280 const char *p
= (const char *)OID
;
285 if (!isdigit(*p
)) return false;
286 v
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
287 if (newp
[0] != '.' || error
!= 0) {
292 if (!isdigit(*p
)) return false;
293 v2
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
294 if (newp
[0] != '.' || error
!= 0) {
299 /*the ber representation can't use more space than the string one */
300 *blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(OID
));
301 if (!blob
->data
) return false;
303 blob
->data
[0] = 40*v
+ v2
;
307 if (!isdigit(*p
)) return false;
308 v
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
309 if (newp
[0] == '.' || error
!= 0) {
312 /* empty last component */
313 data_blob_free(blob
);
316 } else if (newp
[0] == '\0') {
319 data_blob_free(blob
);
322 if (v
>= (1<<28)) blob
->data
[i
++] = (0x80 | ((v
>>28)&0x7f));
323 if (v
>= (1<<21)) blob
->data
[i
++] = (0x80 | ((v
>>21)&0x7f));
324 if (v
>= (1<<14)) blob
->data
[i
++] = (0x80 | ((v
>>14)&0x7f));
325 if (v
>= (1<<7)) blob
->data
[i
++] = (0x80 | ((v
>>7)&0x7f));
326 blob
->data
[i
++] = (v
&0x7f);
335 * Serialize partial OID string.
336 * Partial OIDs are in the form:
340 bool ber_write_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *partial_oid
)
342 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
343 char *oid
= talloc_strdup(tmp_ctx
, partial_oid
);
346 /* truncate partial part so ber_write_OID_String() works */
347 p
= strchr(oid
, ':');
353 if (!ber_write_OID_String(mem_ctx
, blob
, oid
)) {
354 talloc_free(tmp_ctx
);
358 /* Add partially encoded sub-identifier */
360 DATA_BLOB tmp_blob
= strhex_to_data_blob(tmp_ctx
, p
);
361 if (!data_blob_append(mem_ctx
, blob
, tmp_blob
.data
,
363 talloc_free(tmp_ctx
);
368 talloc_free(tmp_ctx
);
373 /* write an object ID to a ASN1 buffer */
374 bool asn1_write_OID(struct asn1_data
*data
, const char *OID
)
378 if (!asn1_push_tag(data
, ASN1_OID
)) return false;
380 if (!ber_write_OID_String(NULL
, &blob
, OID
)) {
381 data
->has_error
= true;
385 if (!asn1_write(data
, blob
.data
, blob
.length
)) {
386 data_blob_free(&blob
);
387 data
->has_error
= true;
390 data_blob_free(&blob
);
391 return asn1_pop_tag(data
);
394 /* write an octet string */
395 bool asn1_write_OctetString(struct asn1_data
*data
, const void *p
, size_t length
)
397 if (!asn1_push_tag(data
, ASN1_OCTET_STRING
)) return false;
398 if (!asn1_write(data
, p
, length
)) return false;
399 return asn1_pop_tag(data
);
402 /* write a LDAP string */
403 bool asn1_write_LDAPString(struct asn1_data
*data
, const char *s
)
405 return asn1_write(data
, s
, strlen(s
));
408 /* write a LDAP string from a DATA_BLOB */
409 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data
*data
, const DATA_BLOB
*s
)
411 return asn1_write(data
, s
->data
, s
->length
);
414 /* write a general string */
415 bool asn1_write_GeneralString(struct asn1_data
*data
, const char *s
)
417 if (!asn1_push_tag(data
, ASN1_GENERAL_STRING
)) return false;
418 if (!asn1_write_LDAPString(data
, s
)) return false;
419 return asn1_pop_tag(data
);
422 bool asn1_write_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
424 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
425 if (!asn1_write(data
, blob
->data
, blob
->length
)) return false;
426 return asn1_pop_tag(data
);
429 /* write a BOOLEAN */
430 bool asn1_write_BOOLEAN(struct asn1_data
*data
, bool v
)
432 if (!asn1_push_tag(data
, ASN1_BOOLEAN
)) return false;
433 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
434 return asn1_pop_tag(data
);
437 bool asn1_read_BOOLEAN(struct asn1_data
*data
, bool *v
)
440 if (!asn1_start_tag(data
, ASN1_BOOLEAN
)) return false;
442 if (!asn1_read_uint8(data
, &tmp
)) return false;
446 return asn1_end_tag(data
);
449 /* write a BOOLEAN in a simple context */
450 bool asn1_write_BOOLEAN_context(struct asn1_data
*data
, bool v
, int context
)
452 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
453 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
454 return asn1_pop_tag(data
);
457 bool asn1_read_BOOLEAN_context(struct asn1_data
*data
, bool *v
, int context
)
460 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
462 if (!asn1_read_uint8(data
, &tmp
)) return false;
466 return asn1_end_tag(data
);
469 /* check a BOOLEAN */
470 bool asn1_check_BOOLEAN(struct asn1_data
*data
, bool v
)
474 if (!asn1_read_uint8(data
, &b
)) return false;
475 if (b
!= ASN1_BOOLEAN
) {
476 data
->has_error
= true;
479 if (!asn1_read_uint8(data
, &b
)) return false;
481 data
->has_error
= true;
484 return !data
->has_error
;
488 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
489 bool asn1_load(struct asn1_data
*data
, DATA_BLOB blob
)
492 * Save the maximum depth
494 unsigned max_depth
= data
->max_depth
;
497 data
->data
= (uint8_t *)talloc_memdup(data
, blob
.data
, blob
.length
);
499 data
->has_error
= true;
502 data
->length
= blob
.length
;
503 data
->max_depth
= max_depth
;
507 /* Peek into an ASN1 buffer, not advancing the pointer */
508 bool asn1_peek(struct asn1_data
*data
, void *p
, int len
)
513 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
)
516 if (data
->ofs
+ len
> data
->length
) {
517 /* we need to mark the buffer as consumed, so the caller knows
518 this was an out of data error, and not a decode error */
519 data
->ofs
= data
->length
;
523 memcpy(p
, data
->data
+ data
->ofs
, len
);
527 /* read from a ASN1 buffer, advancing the buffer pointer */
528 bool asn1_read(struct asn1_data
*data
, void *p
, int len
)
530 if (!asn1_peek(data
, p
, len
)) {
531 data
->has_error
= true;
539 /* read a uint8_t from a ASN1 buffer */
540 bool asn1_read_uint8(struct asn1_data
*data
, uint8_t *v
)
542 return asn1_read(data
, v
, 1);
545 bool asn1_peek_uint8(struct asn1_data
*data
, uint8_t *v
)
547 return asn1_peek(data
, v
, 1);
550 bool asn1_peek_tag(struct asn1_data
*data
, uint8_t tag
)
554 if (asn1_tag_remaining(data
) <= 0) {
558 if (!asn1_peek_uint8(data
, &b
))
565 * just get the needed size the tag would consume
567 static bool asn1_peek_tag_needed_size(struct asn1_data
*data
, uint8_t tag
,
570 off_t start_ofs
= data
->ofs
;
574 if (data
->has_error
) {
578 if (!asn1_read_uint8(data
, &b
)) {
579 data
->ofs
= start_ofs
;
580 data
->has_error
= false;
585 data
->ofs
= start_ofs
;
586 data
->has_error
= false;
590 if (!asn1_read_uint8(data
, &b
)) {
591 data
->ofs
= start_ofs
;
592 data
->has_error
= false;
598 if (!asn1_read_uint8(data
, &b
)) {
599 data
->ofs
= start_ofs
;
600 data
->has_error
= false;
605 * We should not allow more than 4 bytes
606 * for the encoding of the tag length.
608 * Otherwise we'd overflow the taglen
609 * variable on 32 bit systems.
611 data
->ofs
= start_ofs
;
612 data
->has_error
= false;
619 if (!asn1_read_uint8(data
, &b
)) {
620 data
->ofs
= start_ofs
;
621 data
->has_error
= false;
625 tmp_taglen
= (taglen
<< 8) | b
;
627 if ((tmp_taglen
>> 8) != taglen
) {
629 data
->ofs
= start_ofs
;
630 data
->has_error
= false;
641 *size
= (data
->ofs
- start_ofs
) + taglen
;
643 data
->ofs
= start_ofs
;
644 data
->has_error
= false;
648 /* start reading a nested asn1 structure */
649 bool asn1_start_tag(struct asn1_data
*data
, uint8_t tag
)
652 struct nesting
*nesting
;
655 * Check the depth of the parse tree and prevent it from growing
659 if (data
->depth
> data
->max_depth
) {
660 data
->has_error
= true;
664 if (!asn1_read_uint8(data
, &b
))
668 data
->has_error
= true;
671 nesting
= talloc(data
, struct nesting
);
673 data
->has_error
= true;
677 if (!asn1_read_uint8(data
, &b
)) {
683 if (!asn1_read_uint8(data
, &b
))
689 if (!asn1_read_uint8(data
, &b
))
692 taglen
= (nesting
->taglen
<< 8) | b
;
694 if ((taglen
>> 8) != nesting
->taglen
) {
696 data
->has_error
= true;
699 nesting
->taglen
= taglen
;
706 nesting
->start
= data
->ofs
;
707 nesting
->next
= data
->nesting
;
708 data
->nesting
= nesting
;
709 if (asn1_tag_remaining(data
) == -1) {
712 return !data
->has_error
;
715 /* stop reading a tag */
716 bool asn1_end_tag(struct asn1_data
*data
)
718 struct nesting
*nesting
;
720 if (data
->depth
== 0) {
721 smb_panic("Unbalanced ASN.1 Tag nesting");
724 /* make sure we read it all */
725 if (asn1_tag_remaining(data
) != 0) {
726 data
->has_error
= true;
730 nesting
= data
->nesting
;
733 data
->has_error
= true;
737 data
->nesting
= nesting
->next
;
738 talloc_free(nesting
);
742 /* work out how many bytes are left in this nested tag */
743 int asn1_tag_remaining(struct asn1_data
*data
)
746 if (data
->has_error
) {
750 if (!data
->nesting
) {
751 data
->has_error
= true;
754 remaining
= data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
755 if (remaining
> (data
->length
- data
->ofs
)) {
756 data
->has_error
= true;
760 data
->has_error
= true;
767 * Internal implementation for reading binary OIDs
768 * Reading is done as far in the buffer as valid OID
769 * till buffer ends or not valid sub-identifier is found.
771 static bool _ber_read_OID_String_impl(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
772 char **OID
, size_t *bytes_eaten
)
777 char *tmp_oid
= NULL
;
779 if (blob
.length
< 2) return false;
783 tmp_oid
= talloc_asprintf(mem_ctx
, "%u.%u", b
[0]/40, b
[0]%40);
785 if (bytes_eaten
!= NULL
) {
789 for(i
= 1, v
= 0; i
< blob
.length
; i
++) {
790 v
= (v
<<7) | (b
[i
]&0x7f);
791 if ( ! (b
[i
] & 0x80)) {
792 talloc_asprintf_addbuf(&tmp_oid
, ".%u", v
);
799 if (tmp_oid
== NULL
) {
810 /* read an object ID from a data blob */
811 bool ber_read_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
, char **OID
)
815 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, OID
, &bytes_eaten
))
818 return (bytes_eaten
== blob
.length
);
822 * Deserialize partial OID string.
823 * Partial OIDs are in the form:
827 bool ber_read_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
832 char *identifier
= NULL
;
833 char *tmp_oid
= NULL
;
835 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, &tmp_oid
, &bytes_eaten
))
838 if (bytes_eaten
< blob
.length
) {
839 bytes_left
= blob
.length
- bytes_eaten
;
840 identifier
= hex_encode_talloc(mem_ctx
, &blob
.data
[bytes_eaten
], bytes_left
);
841 if (!identifier
) goto nomem
;
843 *partial_oid
= talloc_asprintf_append_buffer(tmp_oid
, ":0x%s", identifier
);
844 if (!*partial_oid
) goto nomem
;
845 TALLOC_FREE(identifier
);
847 *partial_oid
= tmp_oid
;
853 TALLOC_FREE(identifier
);
854 TALLOC_FREE(tmp_oid
);
858 /* read an object ID from a ASN1 buffer */
859 bool asn1_read_OID(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **OID
)
864 if (!asn1_start_tag(data
, ASN1_OID
)) return false;
866 len
= asn1_tag_remaining(data
);
868 data
->has_error
= true;
872 blob
= data_blob(NULL
, len
);
874 data
->has_error
= true;
878 if (!asn1_read(data
, blob
.data
, len
)) return false;
879 if (!asn1_end_tag(data
)) {
880 data_blob_free(&blob
);
884 if (!ber_read_OID_String(mem_ctx
, blob
, OID
)) {
885 data
->has_error
= true;
886 data_blob_free(&blob
);
890 data_blob_free(&blob
);
894 /* check that the next object ID is correct */
895 bool asn1_check_OID(struct asn1_data
*data
, const char *OID
)
899 if (!asn1_read_OID(data
, data
, &id
)) return false;
901 if (strcmp(id
, OID
) != 0) {
903 data
->has_error
= true;
910 /* read a LDAPString from a ASN1 buffer */
911 bool asn1_read_LDAPString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
914 len
= asn1_tag_remaining(data
);
916 data
->has_error
= true;
919 *s
= talloc_array(mem_ctx
, char, len
+1);
921 data
->has_error
= true;
925 return asn1_read(data
, *s
, len
);
929 /* read a GeneralString from a ASN1 buffer */
930 bool asn1_read_GeneralString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
932 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return false;
933 if (!asn1_read_LDAPString(data
, mem_ctx
, s
)) return false;
934 return asn1_end_tag(data
);
938 /* read a octet string blob */
939 bool asn1_read_OctetString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
943 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return false;
944 len
= asn1_tag_remaining(data
);
946 data
->has_error
= true;
949 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
950 if (!blob
->data
|| blob
->length
< len
) {
951 data
->has_error
= true;
954 if (!asn1_read(data
, blob
->data
, len
)) goto err
;
955 if (!asn1_end_tag(data
)) goto err
;
962 data_blob_free(blob
);
963 *blob
= data_blob_null
;
967 bool asn1_read_ContextSimple(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, uint8_t num
,
972 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
973 len
= asn1_tag_remaining(data
);
975 data
->has_error
= true;
978 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+ 1);
979 if ((len
!= 0) && (!blob
->data
)) {
980 data
->has_error
= true;
983 if (!asn1_read(data
, blob
->data
, len
)) return false;
986 return asn1_end_tag(data
);
989 /* read an integer without tag*/
990 bool asn1_read_implicit_Integer(struct asn1_data
*data
, int *i
)
994 bool first_byte
= true;
998 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
999 if (!asn1_read_uint8(data
, &b
)) return false;
1002 /* Number is negative. */
1011 return !data
->has_error
;
1014 /* read an integer */
1015 bool asn1_read_Integer(struct asn1_data
*data
, int *i
)
1019 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return false;
1020 if (!asn1_read_implicit_Integer(data
, i
)) return false;
1021 return asn1_end_tag(data
);
1024 /* read a BIT STRING */
1025 bool asn1_read_BitString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, uint8_t *padding
)
1029 if (!asn1_start_tag(data
, ASN1_BIT_STRING
)) return false;
1030 len
= asn1_tag_remaining(data
);
1032 data
->has_error
= true;
1035 if (!asn1_read_uint8(data
, padding
)) return false;
1037 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
1038 if (!blob
->data
|| blob
->length
< len
) {
1039 data
->has_error
= true;
1042 if (asn1_read(data
, blob
->data
, len
- 1)) {
1044 blob
->data
[len
] = 0;
1048 if (data
->has_error
) {
1049 data_blob_free(blob
);
1050 *blob
= data_blob_null
;
1057 /* read a non-negative enumerated value */
1058 bool asn1_read_enumerated(struct asn1_data
*data
, int *v
)
1060 unsigned int val_will_wrap
= (0xFFU
<< ((sizeof(int)*8)-8));
1063 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1064 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
1066 if (!asn1_read_uint8(data
, &b
)) {
1069 if (*v
& val_will_wrap
) {
1071 * There is something already in
1072 * the top byte of the int. If we
1073 * shift left by 8 it's going to
1074 * wrap. Prevent this.
1076 data
->has_error
= true;
1080 * To please/fool the Undefined Behaviour Sanitizer we cast to
1081 * unsigned for the left shift.
1083 *v
= ((unsigned int)*v
<< 8) + b
;
1085 /* ASN1_ENUMERATED can't be -ve. */
1086 data
->has_error
= true;
1090 return asn1_end_tag(data
);
1093 /* write an enumerated value to the stream */
1094 bool asn1_write_enumerated(struct asn1_data
*data
, uint8_t v
)
1096 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return false;
1097 if (!asn1_write_uint8(data
, v
)) return false;
1098 return asn1_pop_tag(data
);
1102 Get us the data just written without copying
1104 bool asn1_blob(const struct asn1_data
*asn1
, DATA_BLOB
*blob
)
1106 if (asn1
->has_error
) {
1109 if (asn1
->nesting
!= NULL
) {
1112 blob
->data
= asn1
->data
;
1113 blob
->length
= asn1
->length
;
1117 bool asn1_extract_blob(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
1122 if (!asn1_blob(asn1
, &blob
)) {
1126 *pblob
= (DATA_BLOB
) { .length
= blob
.length
};
1127 pblob
->data
= talloc_move(mem_ctx
, &blob
.data
);
1130 * Stop access from here on
1132 asn1
->has_error
= true;
1138 Fill in an asn1 struct without making a copy
1140 void asn1_load_nocopy(struct asn1_data
*data
, uint8_t *buf
, size_t len
)
1145 unsigned max_depth
= data
->max_depth
;
1149 data
->max_depth
= max_depth
;
1152 int asn1_peek_full_tag(DATA_BLOB blob
, uint8_t tag
, size_t *packet_size
)
1154 struct asn1_data asn1
;
1159 asn1
.data
= blob
.data
;
1160 asn1
.length
= blob
.length
;
1162 ok
= asn1_peek_tag_needed_size(&asn1
, tag
, &size
);
1167 if (size
> blob
.length
) {
1168 *packet_size
= size
;
1172 *packet_size
= size
;
1177 * Get the length of the ASN.1 data
1179 size_t asn1_get_length(const struct asn1_data
*asn1
) {
1180 return asn1
->length
;