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
)
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) {
311 /* check for empty last component */
312 if (!*p
) return false;
313 } else if (newp
[0] == '\0') {
316 data_blob_free(blob
);
319 if (v
>= (1<<28)) blob
->data
[i
++] = (0x80 | ((v
>>28)&0x7f));
320 if (v
>= (1<<21)) blob
->data
[i
++] = (0x80 | ((v
>>21)&0x7f));
321 if (v
>= (1<<14)) blob
->data
[i
++] = (0x80 | ((v
>>14)&0x7f));
322 if (v
>= (1<<7)) blob
->data
[i
++] = (0x80 | ((v
>>7)&0x7f));
323 blob
->data
[i
++] = (v
&0x7f);
332 * Serialize partial OID string.
333 * Partial OIDs are in the form:
337 bool ber_write_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *partial_oid
)
339 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
340 char *oid
= talloc_strdup(tmp_ctx
, partial_oid
);
343 /* truncate partial part so ber_write_OID_String() works */
344 p
= strchr(oid
, ':');
350 if (!ber_write_OID_String(mem_ctx
, blob
, oid
)) {
351 talloc_free(tmp_ctx
);
355 /* Add partially encoded sub-identifier */
357 DATA_BLOB tmp_blob
= strhex_to_data_blob(tmp_ctx
, p
);
358 if (!data_blob_append(mem_ctx
, blob
, tmp_blob
.data
,
360 talloc_free(tmp_ctx
);
365 talloc_free(tmp_ctx
);
370 /* write an object ID to a ASN1 buffer */
371 bool asn1_write_OID(struct asn1_data
*data
, const char *OID
)
375 if (!asn1_push_tag(data
, ASN1_OID
)) return false;
377 if (!ber_write_OID_String(NULL
, &blob
, OID
)) {
378 data
->has_error
= true;
382 if (!asn1_write(data
, blob
.data
, blob
.length
)) {
383 data_blob_free(&blob
);
384 data
->has_error
= true;
387 data_blob_free(&blob
);
388 return asn1_pop_tag(data
);
391 /* write an octet string */
392 bool asn1_write_OctetString(struct asn1_data
*data
, const void *p
, size_t length
)
394 if (!asn1_push_tag(data
, ASN1_OCTET_STRING
)) return false;
395 if (!asn1_write(data
, p
, length
)) return false;
396 return asn1_pop_tag(data
);
399 /* write a LDAP string */
400 bool asn1_write_LDAPString(struct asn1_data
*data
, const char *s
)
402 return asn1_write(data
, s
, strlen(s
));
405 /* write a LDAP string from a DATA_BLOB */
406 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data
*data
, const DATA_BLOB
*s
)
408 return asn1_write(data
, s
->data
, s
->length
);
411 /* write a general string */
412 bool asn1_write_GeneralString(struct asn1_data
*data
, const char *s
)
414 if (!asn1_push_tag(data
, ASN1_GENERAL_STRING
)) return false;
415 if (!asn1_write_LDAPString(data
, s
)) return false;
416 return asn1_pop_tag(data
);
419 bool asn1_write_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
421 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
422 if (!asn1_write(data
, blob
->data
, blob
->length
)) return false;
423 return asn1_pop_tag(data
);
426 /* write a BOOLEAN */
427 bool asn1_write_BOOLEAN(struct asn1_data
*data
, bool v
)
429 if (!asn1_push_tag(data
, ASN1_BOOLEAN
)) return false;
430 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
431 return asn1_pop_tag(data
);
434 bool asn1_read_BOOLEAN(struct asn1_data
*data
, bool *v
)
437 if (!asn1_start_tag(data
, ASN1_BOOLEAN
)) return false;
439 if (!asn1_read_uint8(data
, &tmp
)) return false;
443 return asn1_end_tag(data
);
446 /* write a BOOLEAN in a simple context */
447 bool asn1_write_BOOLEAN_context(struct asn1_data
*data
, bool v
, int context
)
449 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
450 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
451 return asn1_pop_tag(data
);
454 bool asn1_read_BOOLEAN_context(struct asn1_data
*data
, bool *v
, int context
)
457 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
459 if (!asn1_read_uint8(data
, &tmp
)) return false;
463 return asn1_end_tag(data
);
466 /* check a BOOLEAN */
467 bool asn1_check_BOOLEAN(struct asn1_data
*data
, bool v
)
471 if (!asn1_read_uint8(data
, &b
)) return false;
472 if (b
!= ASN1_BOOLEAN
) {
473 data
->has_error
= true;
476 if (!asn1_read_uint8(data
, &b
)) return false;
478 data
->has_error
= true;
481 return !data
->has_error
;
485 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
486 bool asn1_load(struct asn1_data
*data
, DATA_BLOB blob
)
489 * Save the maximum depth
491 unsigned max_depth
= data
->max_depth
;
494 data
->data
= (uint8_t *)talloc_memdup(data
, blob
.data
, blob
.length
);
496 data
->has_error
= true;
499 data
->length
= blob
.length
;
500 data
->max_depth
= max_depth
;
504 /* Peek into an ASN1 buffer, not advancing the pointer */
505 bool asn1_peek(struct asn1_data
*data
, void *p
, int len
)
510 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
)
513 if (data
->ofs
+ len
> data
->length
) {
514 /* we need to mark the buffer as consumed, so the caller knows
515 this was an out of data error, and not a decode error */
516 data
->ofs
= data
->length
;
520 memcpy(p
, data
->data
+ data
->ofs
, len
);
524 /* read from a ASN1 buffer, advancing the buffer pointer */
525 bool asn1_read(struct asn1_data
*data
, void *p
, int len
)
527 if (!asn1_peek(data
, p
, len
)) {
528 data
->has_error
= true;
536 /* read a uint8_t from a ASN1 buffer */
537 bool asn1_read_uint8(struct asn1_data
*data
, uint8_t *v
)
539 return asn1_read(data
, v
, 1);
542 bool asn1_peek_uint8(struct asn1_data
*data
, uint8_t *v
)
544 return asn1_peek(data
, v
, 1);
547 bool asn1_peek_tag(struct asn1_data
*data
, uint8_t tag
)
551 if (asn1_tag_remaining(data
) <= 0) {
555 if (!asn1_peek_uint8(data
, &b
))
562 * just get the needed size the tag would consume
564 static bool asn1_peek_tag_needed_size(struct asn1_data
*data
, uint8_t tag
,
567 off_t start_ofs
= data
->ofs
;
571 if (data
->has_error
) {
575 if (!asn1_read_uint8(data
, &b
)) {
576 data
->ofs
= start_ofs
;
577 data
->has_error
= false;
582 data
->ofs
= start_ofs
;
583 data
->has_error
= false;
587 if (!asn1_read_uint8(data
, &b
)) {
588 data
->ofs
= start_ofs
;
589 data
->has_error
= false;
595 if (!asn1_read_uint8(data
, &b
)) {
596 data
->ofs
= start_ofs
;
597 data
->has_error
= false;
602 * We should not allow more than 4 bytes
603 * for the encoding of the tag length.
605 * Otherwise we'd overflow the taglen
606 * variable on 32 bit systems.
608 data
->ofs
= start_ofs
;
609 data
->has_error
= false;
616 if (!asn1_read_uint8(data
, &b
)) {
617 data
->ofs
= start_ofs
;
618 data
->has_error
= false;
622 tmp_taglen
= (taglen
<< 8) | b
;
624 if ((tmp_taglen
>> 8) != taglen
) {
626 data
->ofs
= start_ofs
;
627 data
->has_error
= false;
638 *size
= (data
->ofs
- start_ofs
) + taglen
;
640 data
->ofs
= start_ofs
;
641 data
->has_error
= false;
645 /* start reading a nested asn1 structure */
646 bool asn1_start_tag(struct asn1_data
*data
, uint8_t tag
)
649 struct nesting
*nesting
;
652 * Check the depth of the parse tree and prevent it from growing
656 if (data
->depth
> data
->max_depth
) {
657 data
->has_error
= true;
661 if (!asn1_read_uint8(data
, &b
))
665 data
->has_error
= true;
668 nesting
= talloc(data
, struct nesting
);
670 data
->has_error
= true;
674 if (!asn1_read_uint8(data
, &b
)) {
680 if (!asn1_read_uint8(data
, &b
))
686 if (!asn1_read_uint8(data
, &b
))
689 taglen
= (nesting
->taglen
<< 8) | b
;
691 if ((taglen
>> 8) != nesting
->taglen
) {
693 data
->has_error
= true;
696 nesting
->taglen
= taglen
;
703 nesting
->start
= data
->ofs
;
704 nesting
->next
= data
->nesting
;
705 data
->nesting
= nesting
;
706 if (asn1_tag_remaining(data
) == -1) {
709 return !data
->has_error
;
712 /* stop reading a tag */
713 bool asn1_end_tag(struct asn1_data
*data
)
715 struct nesting
*nesting
;
717 if (data
->depth
== 0) {
718 smb_panic("Unbalanced ASN.1 Tag nesting");
721 /* make sure we read it all */
722 if (asn1_tag_remaining(data
) != 0) {
723 data
->has_error
= true;
727 nesting
= data
->nesting
;
730 data
->has_error
= true;
734 data
->nesting
= nesting
->next
;
735 talloc_free(nesting
);
739 /* work out how many bytes are left in this nested tag */
740 int asn1_tag_remaining(struct asn1_data
*data
)
743 if (data
->has_error
) {
747 if (!data
->nesting
) {
748 data
->has_error
= true;
751 remaining
= data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
752 if (remaining
> (data
->length
- data
->ofs
)) {
753 data
->has_error
= true;
757 data
->has_error
= true;
764 * Internal implementation for reading binary OIDs
765 * Reading is done as far in the buffer as valid OID
766 * till buffer ends or not valid sub-identifier is found.
768 static bool _ber_read_OID_String_impl(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
769 char **OID
, size_t *bytes_eaten
)
774 char *tmp_oid
= NULL
;
776 if (blob
.length
< 2) return false;
780 tmp_oid
= talloc_asprintf(mem_ctx
, "%u.%u", b
[0]/40, b
[0]%40);
781 if (!tmp_oid
) goto nomem
;
783 if (bytes_eaten
!= NULL
) {
787 for(i
= 1, v
= 0; i
< blob
.length
; i
++) {
788 v
= (v
<<7) | (b
[i
]&0x7f);
789 if ( ! (b
[i
] & 0x80)) {
790 tmp_oid
= talloc_asprintf_append_buffer(tmp_oid
, ".%u", v
);
795 if (!tmp_oid
) goto nomem
;
805 /* read an object ID from a data blob */
806 bool ber_read_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
, char **OID
)
810 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, OID
, &bytes_eaten
))
813 return (bytes_eaten
== blob
.length
);
817 * Deserialize partial OID string.
818 * Partial OIDs are in the form:
822 bool ber_read_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
827 char *identifier
= NULL
;
828 char *tmp_oid
= NULL
;
830 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, &tmp_oid
, &bytes_eaten
))
833 if (bytes_eaten
< blob
.length
) {
834 bytes_left
= blob
.length
- bytes_eaten
;
835 identifier
= hex_encode_talloc(mem_ctx
, &blob
.data
[bytes_eaten
], bytes_left
);
836 if (!identifier
) goto nomem
;
838 *partial_oid
= talloc_asprintf_append_buffer(tmp_oid
, ":0x%s", identifier
);
839 if (!*partial_oid
) goto nomem
;
840 TALLOC_FREE(identifier
);
842 *partial_oid
= tmp_oid
;
848 TALLOC_FREE(identifier
);
849 TALLOC_FREE(tmp_oid
);
853 /* read an object ID from a ASN1 buffer */
854 bool asn1_read_OID(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **OID
)
859 if (!asn1_start_tag(data
, ASN1_OID
)) return false;
861 len
= asn1_tag_remaining(data
);
863 data
->has_error
= true;
867 blob
= data_blob(NULL
, len
);
869 data
->has_error
= true;
873 if (!asn1_read(data
, blob
.data
, len
)) return false;
874 if (!asn1_end_tag(data
)) {
875 data_blob_free(&blob
);
879 if (!ber_read_OID_String(mem_ctx
, blob
, OID
)) {
880 data
->has_error
= true;
881 data_blob_free(&blob
);
885 data_blob_free(&blob
);
889 /* check that the next object ID is correct */
890 bool asn1_check_OID(struct asn1_data
*data
, const char *OID
)
894 if (!asn1_read_OID(data
, data
, &id
)) return false;
896 if (strcmp(id
, OID
) != 0) {
898 data
->has_error
= true;
905 /* read a LDAPString from a ASN1 buffer */
906 bool asn1_read_LDAPString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
909 len
= asn1_tag_remaining(data
);
911 data
->has_error
= true;
914 *s
= talloc_array(mem_ctx
, char, len
+1);
916 data
->has_error
= true;
920 return asn1_read(data
, *s
, len
);
924 /* read a GeneralString from a ASN1 buffer */
925 bool asn1_read_GeneralString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
927 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return false;
928 if (!asn1_read_LDAPString(data
, mem_ctx
, s
)) return false;
929 return asn1_end_tag(data
);
933 /* read a octet string blob */
934 bool asn1_read_OctetString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
938 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return false;
939 len
= asn1_tag_remaining(data
);
941 data
->has_error
= true;
944 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
945 if (!blob
->data
|| blob
->length
< len
) {
946 data
->has_error
= true;
949 if (!asn1_read(data
, blob
->data
, len
)) goto err
;
950 if (!asn1_end_tag(data
)) goto err
;
957 data_blob_free(blob
);
958 *blob
= data_blob_null
;
962 bool asn1_read_ContextSimple(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, uint8_t num
,
967 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
968 len
= asn1_tag_remaining(data
);
970 data
->has_error
= true;
973 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+ 1);
974 if ((len
!= 0) && (!blob
->data
)) {
975 data
->has_error
= true;
978 if (!asn1_read(data
, blob
->data
, len
)) return false;
981 return asn1_end_tag(data
);
984 /* read an integer without tag*/
985 bool asn1_read_implicit_Integer(struct asn1_data
*data
, int *i
)
989 bool first_byte
= true;
993 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
994 if (!asn1_read_uint8(data
, &b
)) return false;
997 /* Number is negative. */
1006 return !data
->has_error
;
1009 /* read an integer */
1010 bool asn1_read_Integer(struct asn1_data
*data
, int *i
)
1014 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return false;
1015 if (!asn1_read_implicit_Integer(data
, i
)) return false;
1016 return asn1_end_tag(data
);
1019 /* read a BIT STRING */
1020 bool asn1_read_BitString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, uint8_t *padding
)
1024 if (!asn1_start_tag(data
, ASN1_BIT_STRING
)) return false;
1025 len
= asn1_tag_remaining(data
);
1027 data
->has_error
= true;
1030 if (!asn1_read_uint8(data
, padding
)) return false;
1032 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
1033 if (!blob
->data
|| blob
->length
< len
) {
1034 data
->has_error
= true;
1037 if (asn1_read(data
, blob
->data
, len
- 1)) {
1039 blob
->data
[len
] = 0;
1043 if (data
->has_error
) {
1044 data_blob_free(blob
);
1045 *blob
= data_blob_null
;
1052 /* read a non-negative enumerated value */
1053 bool asn1_read_enumerated(struct asn1_data
*data
, int *v
)
1055 unsigned int val_will_wrap
= (0xFFU
<< ((sizeof(int)*8)-8));
1058 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1059 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
1061 if (!asn1_read_uint8(data
, &b
)) {
1064 if (*v
& val_will_wrap
) {
1066 * There is something already in
1067 * the top byte of the int. If we
1068 * shift left by 8 it's going to
1069 * wrap. Prevent this.
1071 data
->has_error
= true;
1075 * To please/fool the Undefined Behaviour Sanitizer we cast to
1076 * unsigned for the left shift.
1078 *v
= ((unsigned int)*v
<< 8) + b
;
1080 /* ASN1_ENUMERATED can't be -ve. */
1081 data
->has_error
= true;
1085 return asn1_end_tag(data
);
1088 /* write an enumerated value to the stream */
1089 bool asn1_write_enumerated(struct asn1_data
*data
, uint8_t v
)
1091 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return false;
1092 if (!asn1_write_uint8(data
, v
)) return false;
1093 return asn1_pop_tag(data
);
1097 Get us the data just written without copying
1099 bool asn1_blob(const struct asn1_data
*asn1
, DATA_BLOB
*blob
)
1101 if (asn1
->has_error
) {
1104 if (asn1
->nesting
!= NULL
) {
1107 blob
->data
= asn1
->data
;
1108 blob
->length
= asn1
->length
;
1112 bool asn1_extract_blob(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
1117 if (!asn1_blob(asn1
, &blob
)) {
1121 *pblob
= (DATA_BLOB
) { .length
= blob
.length
};
1122 pblob
->data
= talloc_move(mem_ctx
, &blob
.data
);
1125 * Stop access from here on
1127 asn1
->has_error
= true;
1133 Fill in an asn1 struct without making a copy
1135 void asn1_load_nocopy(struct asn1_data
*data
, uint8_t *buf
, size_t len
)
1140 unsigned max_depth
= data
->max_depth
;
1144 data
->max_depth
= max_depth
;
1147 int asn1_peek_full_tag(DATA_BLOB blob
, uint8_t tag
, size_t *packet_size
)
1149 struct asn1_data asn1
;
1154 asn1
.data
= blob
.data
;
1155 asn1
.length
= blob
.length
;
1157 ok
= asn1_peek_tag_needed_size(&asn1
, tag
, &size
);
1162 if (size
> blob
.length
) {
1163 *packet_size
= size
;
1167 *packet_size
= size
;
1172 * Get the length of the ASN.1 data
1174 size_t asn1_get_length(const struct asn1_data
*asn1
) {
1175 return asn1
->length
;