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"
28 size_t taglen
; /* for parsing */
37 struct nesting
*nesting
;
41 /* allocate an asn1 structure */
42 struct asn1_data
*asn1_init(TALLOC_CTX
*mem_ctx
)
44 struct asn1_data
*ret
= talloc_zero(mem_ctx
, struct asn1_data
);
46 DEBUG(0,("asn1_init failed! out of memory\n"));
51 /* free an asn1 structure */
52 void asn1_free(struct asn1_data
*data
)
57 bool asn1_has_error(const struct asn1_data
*data
)
59 return data
->has_error
;
62 void asn1_set_error(struct asn1_data
*data
)
64 data
->has_error
= true;
67 bool asn1_has_nesting(const struct asn1_data
*data
)
69 return data
->nesting
!= NULL
;
72 off_t
asn1_current_ofs(const struct asn1_data
*data
)
77 /* write to the ASN1 buffer, advancing the buffer pointer */
78 bool asn1_write(struct asn1_data
*data
, const void *p
, int len
)
80 if (data
->has_error
) return false;
82 if ((len
< 0) || (data
->ofs
+ (size_t)len
< data
->ofs
)) {
83 data
->has_error
= true;
87 if (data
->length
< data
->ofs
+len
) {
89 newp
= talloc_realloc(data
, data
->data
, uint8_t, data
->ofs
+len
);
91 data
->has_error
= true;
95 data
->length
= data
->ofs
+len
;
97 memcpy(data
->data
+ data
->ofs
, p
, len
);
102 /* useful fn for writing a uint8_t */
103 bool asn1_write_uint8(struct asn1_data
*data
, uint8_t v
)
105 return asn1_write(data
, &v
, 1);
108 /* push a tag onto the asn1 data buffer. Used for nested structures */
109 bool asn1_push_tag(struct asn1_data
*data
, uint8_t tag
)
111 struct nesting
*nesting
;
113 if (!asn1_write_uint8(data
, tag
)) {
116 nesting
= talloc(data
, struct nesting
);
118 data
->has_error
= true;
122 nesting
->start
= data
->ofs
;
123 nesting
->next
= data
->nesting
;
124 data
->nesting
= nesting
;
125 return asn1_write_uint8(data
, 0xff);
129 bool asn1_pop_tag(struct asn1_data
*data
)
131 struct nesting
*nesting
;
134 if (data
->has_error
) {
138 nesting
= data
->nesting
;
141 data
->has_error
= true;
144 len
= data
->ofs
- (nesting
->start
+1);
145 /* yes, this is ugly. We don't know in advance how many bytes the length
146 of a tag will take, so we assumed 1 byte. If we were wrong then we
147 need to correct our mistake */
148 if (len
> 0xFFFFFF) {
149 data
->data
[nesting
->start
] = 0x84;
150 if (!asn1_write_uint8(data
, 0)) return false;
151 if (!asn1_write_uint8(data
, 0)) return false;
152 if (!asn1_write_uint8(data
, 0)) return false;
153 if (!asn1_write_uint8(data
, 0)) return false;
154 memmove(data
->data
+nesting
->start
+5, data
->data
+nesting
->start
+1, len
);
155 data
->data
[nesting
->start
+1] = (len
>>24) & 0xFF;
156 data
->data
[nesting
->start
+2] = (len
>>16) & 0xFF;
157 data
->data
[nesting
->start
+3] = (len
>>8) & 0xFF;
158 data
->data
[nesting
->start
+4] = len
&0xff;
159 } else if (len
> 0xFFFF) {
160 data
->data
[nesting
->start
] = 0x83;
161 if (!asn1_write_uint8(data
, 0)) return false;
162 if (!asn1_write_uint8(data
, 0)) return false;
163 if (!asn1_write_uint8(data
, 0)) return false;
164 memmove(data
->data
+nesting
->start
+4, data
->data
+nesting
->start
+1, len
);
165 data
->data
[nesting
->start
+1] = (len
>>16) & 0xFF;
166 data
->data
[nesting
->start
+2] = (len
>>8) & 0xFF;
167 data
->data
[nesting
->start
+3] = len
&0xff;
168 } else if (len
> 255) {
169 data
->data
[nesting
->start
] = 0x82;
170 if (!asn1_write_uint8(data
, 0)) return false;
171 if (!asn1_write_uint8(data
, 0)) return false;
172 memmove(data
->data
+nesting
->start
+3, data
->data
+nesting
->start
+1, len
);
173 data
->data
[nesting
->start
+1] = len
>>8;
174 data
->data
[nesting
->start
+2] = len
&0xff;
175 } else if (len
> 127) {
176 data
->data
[nesting
->start
] = 0x81;
177 if (!asn1_write_uint8(data
, 0)) return false;
178 memmove(data
->data
+nesting
->start
+2, data
->data
+nesting
->start
+1, len
);
179 data
->data
[nesting
->start
+1] = len
;
181 data
->data
[nesting
->start
] = len
;
184 data
->nesting
= nesting
->next
;
185 talloc_free(nesting
);
189 /* "i" is the one's complement representation, as is the normal result of an
190 * implicit signed->unsigned conversion */
192 static bool push_int_bigendian(struct asn1_data
*data
, unsigned int i
, bool negative
)
194 uint8_t lowest
= i
& 0xFF;
198 if (!push_int_bigendian(data
, i
, negative
))
201 if (data
->nesting
->start
+1 == data
->ofs
) {
203 /* We did not write anything yet, looking at the highest
207 /* Don't write leading 0xff's */
211 if ((lowest
& 0x80) == 0) {
212 /* The only exception for a leading 0xff is if
213 * the highest bit is 0, which would indicate
214 * a positive value */
215 if (!asn1_write_uint8(data
, 0xff))
220 /* The highest bit of a positive integer is 1,
221 * this would indicate a negative number. Push
222 * a 0 to indicate a positive one */
223 if (!asn1_write_uint8(data
, 0))
229 return asn1_write_uint8(data
, lowest
);
232 /* write an Integer without the tag framing. Needed for example for the LDAP
233 * Abandon Operation */
235 bool asn1_write_implicit_Integer(struct asn1_data
*data
, int i
)
237 if (data
->has_error
) {
242 /* -1 is special as it consists of all-0xff bytes. In
243 push_int_bigendian this is the only case that is not
244 properly handled, as all 0xff bytes would be handled as
245 leading ones to be ignored. */
246 return asn1_write_uint8(data
, 0xff);
248 return push_int_bigendian(data
, i
, i
<0);
253 /* write an integer */
254 bool asn1_write_Integer(struct asn1_data
*data
, int i
)
256 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return false;
257 if (!asn1_write_implicit_Integer(data
, i
)) return false;
258 return asn1_pop_tag(data
);
261 /* write a BIT STRING */
262 bool asn1_write_BitString(struct asn1_data
*data
, const void *p
, size_t length
, uint8_t padding
)
264 if (!asn1_push_tag(data
, ASN1_BIT_STRING
)) return false;
265 if (!asn1_write_uint8(data
, padding
)) return false;
266 if (!asn1_write(data
, p
, length
)) return false;
267 return asn1_pop_tag(data
);
270 bool ber_write_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *OID
)
273 const char *p
= (const char *)OID
;
277 if (!isdigit(*p
)) return false;
278 v
= strtoul(p
, &newp
, 10);
279 if (newp
[0] != '.') return false;
282 if (!isdigit(*p
)) return false;
283 v2
= strtoul(p
, &newp
, 10);
284 if (newp
[0] != '.') return false;
287 /*the ber representation can't use more space than the string one */
288 *blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(OID
));
289 if (!blob
->data
) return false;
291 blob
->data
[0] = 40*v
+ v2
;
295 if (!isdigit(*p
)) return false;
296 v
= strtoul(p
, &newp
, 10);
297 if (newp
[0] == '.') {
299 /* check for empty last component */
300 if (!*p
) return false;
301 } else if (newp
[0] == '\0') {
304 data_blob_free(blob
);
307 if (v
>= (1<<28)) blob
->data
[i
++] = (0x80 | ((v
>>28)&0x7f));
308 if (v
>= (1<<21)) blob
->data
[i
++] = (0x80 | ((v
>>21)&0x7f));
309 if (v
>= (1<<14)) blob
->data
[i
++] = (0x80 | ((v
>>14)&0x7f));
310 if (v
>= (1<<7)) blob
->data
[i
++] = (0x80 | ((v
>>7)&0x7f));
311 blob
->data
[i
++] = (v
&0x7f);
320 * Serialize partial OID string.
321 * Partial OIDs are in the form:
325 bool ber_write_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *partial_oid
)
327 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
328 char *oid
= talloc_strdup(tmp_ctx
, partial_oid
);
331 /* truncate partial part so ber_write_OID_String() works */
332 p
= strchr(oid
, ':');
338 if (!ber_write_OID_String(mem_ctx
, blob
, oid
)) {
339 talloc_free(tmp_ctx
);
343 /* Add partially encoded sub-identifier */
345 DATA_BLOB tmp_blob
= strhex_to_data_blob(tmp_ctx
, p
);
346 if (!data_blob_append(mem_ctx
, blob
, tmp_blob
.data
,
348 talloc_free(tmp_ctx
);
353 talloc_free(tmp_ctx
);
358 /* write an object ID to a ASN1 buffer */
359 bool asn1_write_OID(struct asn1_data
*data
, const char *OID
)
363 if (!asn1_push_tag(data
, ASN1_OID
)) return false;
365 if (!ber_write_OID_String(NULL
, &blob
, OID
)) {
366 data
->has_error
= true;
370 if (!asn1_write(data
, blob
.data
, blob
.length
)) {
371 data_blob_free(&blob
);
372 data
->has_error
= true;
375 data_blob_free(&blob
);
376 return asn1_pop_tag(data
);
379 /* write an octet string */
380 bool asn1_write_OctetString(struct asn1_data
*data
, const void *p
, size_t length
)
382 if (!asn1_push_tag(data
, ASN1_OCTET_STRING
)) return false;
383 if (!asn1_write(data
, p
, length
)) return false;
384 return asn1_pop_tag(data
);
387 /* write a LDAP string */
388 bool asn1_write_LDAPString(struct asn1_data
*data
, const char *s
)
390 return asn1_write(data
, s
, strlen(s
));
393 /* write a LDAP string from a DATA_BLOB */
394 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data
*data
, const DATA_BLOB
*s
)
396 return asn1_write(data
, s
->data
, s
->length
);
399 /* write a general string */
400 bool asn1_write_GeneralString(struct asn1_data
*data
, const char *s
)
402 if (!asn1_push_tag(data
, ASN1_GENERAL_STRING
)) return false;
403 if (!asn1_write_LDAPString(data
, s
)) return false;
404 return asn1_pop_tag(data
);
407 bool asn1_write_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
409 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
410 if (!asn1_write(data
, blob
->data
, blob
->length
)) return false;
411 return asn1_pop_tag(data
);
414 /* write a BOOLEAN */
415 bool asn1_write_BOOLEAN(struct asn1_data
*data
, bool v
)
417 if (!asn1_push_tag(data
, ASN1_BOOLEAN
)) return false;
418 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
419 return asn1_pop_tag(data
);
422 bool asn1_read_BOOLEAN(struct asn1_data
*data
, bool *v
)
425 if (!asn1_start_tag(data
, ASN1_BOOLEAN
)) return false;
427 if (!asn1_read_uint8(data
, &tmp
)) return false;
431 return asn1_end_tag(data
);
434 /* write a BOOLEAN in a simple context */
435 bool asn1_write_BOOLEAN_context(struct asn1_data
*data
, bool v
, int context
)
437 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
438 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
439 return asn1_pop_tag(data
);
442 bool asn1_read_BOOLEAN_context(struct asn1_data
*data
, bool *v
, int context
)
445 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
447 if (!asn1_read_uint8(data
, &tmp
)) return false;
451 return asn1_end_tag(data
);
454 /* check a BOOLEAN */
455 bool asn1_check_BOOLEAN(struct asn1_data
*data
, bool v
)
459 if (!asn1_read_uint8(data
, &b
)) return false;
460 if (b
!= ASN1_BOOLEAN
) {
461 data
->has_error
= true;
464 if (!asn1_read_uint8(data
, &b
)) return false;
466 data
->has_error
= true;
469 return !data
->has_error
;
473 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
474 bool asn1_load(struct asn1_data
*data
, DATA_BLOB blob
)
477 data
->data
= (uint8_t *)talloc_memdup(data
, blob
.data
, blob
.length
);
479 data
->has_error
= true;
482 data
->length
= blob
.length
;
486 /* Peek into an ASN1 buffer, not advancing the pointer */
487 bool asn1_peek(struct asn1_data
*data
, void *p
, int len
)
492 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
)
495 if (data
->ofs
+ len
> data
->length
) {
496 /* we need to mark the buffer as consumed, so the caller knows
497 this was an out of data error, and not a decode error */
498 data
->ofs
= data
->length
;
502 memcpy(p
, data
->data
+ data
->ofs
, len
);
506 /* read from a ASN1 buffer, advancing the buffer pointer */
507 bool asn1_read(struct asn1_data
*data
, void *p
, int len
)
509 if (!asn1_peek(data
, p
, len
)) {
510 data
->has_error
= true;
518 /* read a uint8_t from a ASN1 buffer */
519 bool asn1_read_uint8(struct asn1_data
*data
, uint8_t *v
)
521 return asn1_read(data
, v
, 1);
524 bool asn1_peek_uint8(struct asn1_data
*data
, uint8_t *v
)
526 return asn1_peek(data
, v
, 1);
529 bool asn1_peek_tag(struct asn1_data
*data
, uint8_t tag
)
533 if (asn1_tag_remaining(data
) <= 0) {
537 if (!asn1_peek_uint8(data
, &b
))
544 * just get the needed size the tag would consume
546 static bool asn1_peek_tag_needed_size(struct asn1_data
*data
, uint8_t tag
,
549 off_t start_ofs
= data
->ofs
;
553 if (data
->has_error
) {
557 if (!asn1_read_uint8(data
, &b
)) {
558 data
->ofs
= start_ofs
;
559 data
->has_error
= false;
564 data
->ofs
= start_ofs
;
565 data
->has_error
= false;
569 if (!asn1_read_uint8(data
, &b
)) {
570 data
->ofs
= start_ofs
;
571 data
->has_error
= false;
577 if (!asn1_read_uint8(data
, &b
)) {
578 data
->ofs
= start_ofs
;
579 data
->has_error
= false;
584 * We should not allow more than 4 bytes
585 * for the encoding of the tag length.
587 * Otherwise we'd overflow the taglen
588 * variable on 32 bit systems.
590 data
->ofs
= start_ofs
;
591 data
->has_error
= false;
598 if (!asn1_read_uint8(data
, &b
)) {
599 data
->ofs
= start_ofs
;
600 data
->has_error
= false;
604 tmp_taglen
= (taglen
<< 8) | b
;
606 if ((tmp_taglen
>> 8) != taglen
) {
608 data
->ofs
= start_ofs
;
609 data
->has_error
= false;
620 *size
= (data
->ofs
- start_ofs
) + taglen
;
622 data
->ofs
= start_ofs
;
623 data
->has_error
= false;
627 /* start reading a nested asn1 structure */
628 bool asn1_start_tag(struct asn1_data
*data
, uint8_t tag
)
631 struct nesting
*nesting
;
633 if (!asn1_read_uint8(data
, &b
))
637 data
->has_error
= true;
640 nesting
= talloc(data
, struct nesting
);
642 data
->has_error
= true;
646 if (!asn1_read_uint8(data
, &b
)) {
652 if (!asn1_read_uint8(data
, &b
))
658 if (!asn1_read_uint8(data
, &b
))
661 taglen
= (nesting
->taglen
<< 8) | b
;
663 if ((taglen
>> 8) != nesting
->taglen
) {
665 data
->has_error
= true;
668 nesting
->taglen
= taglen
;
675 nesting
->start
= data
->ofs
;
676 nesting
->next
= data
->nesting
;
677 data
->nesting
= nesting
;
678 if (asn1_tag_remaining(data
) == -1) {
681 return !data
->has_error
;
684 /* stop reading a tag */
685 bool asn1_end_tag(struct asn1_data
*data
)
687 struct nesting
*nesting
;
689 /* make sure we read it all */
690 if (asn1_tag_remaining(data
) != 0) {
691 data
->has_error
= true;
695 nesting
= data
->nesting
;
698 data
->has_error
= true;
702 data
->nesting
= nesting
->next
;
703 talloc_free(nesting
);
707 /* work out how many bytes are left in this nested tag */
708 int asn1_tag_remaining(struct asn1_data
*data
)
711 if (data
->has_error
) {
715 if (!data
->nesting
) {
716 data
->has_error
= true;
719 remaining
= data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
720 if (remaining
> (data
->length
- data
->ofs
)) {
721 data
->has_error
= true;
725 data
->has_error
= true;
732 * Internal implementation for reading binary OIDs
733 * Reading is done as far in the buffer as valid OID
734 * till buffer ends or not valid sub-identifier is found.
736 static bool _ber_read_OID_String_impl(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
737 char **OID
, size_t *bytes_eaten
)
742 char *tmp_oid
= NULL
;
744 if (blob
.length
< 2) return false;
748 tmp_oid
= talloc_asprintf(mem_ctx
, "%u.%u", b
[0]/40, b
[0]%40);
749 if (!tmp_oid
) goto nomem
;
751 if (bytes_eaten
!= NULL
) {
755 for(i
= 1, v
= 0; i
< blob
.length
; i
++) {
756 v
= (v
<<7) | (b
[i
]&0x7f);
757 if ( ! (b
[i
] & 0x80)) {
758 tmp_oid
= talloc_asprintf_append_buffer(tmp_oid
, ".%u", v
);
763 if (!tmp_oid
) goto nomem
;
773 /* read an object ID from a data blob */
774 bool ber_read_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
, char **OID
)
778 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, OID
, &bytes_eaten
))
781 return (bytes_eaten
== blob
.length
);
785 * Deserialize partial OID string.
786 * Partial OIDs are in the form:
790 bool ber_read_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
795 char *identifier
= NULL
;
796 char *tmp_oid
= NULL
;
798 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, &tmp_oid
, &bytes_eaten
))
801 if (bytes_eaten
< blob
.length
) {
802 bytes_left
= blob
.length
- bytes_eaten
;
803 identifier
= hex_encode_talloc(mem_ctx
, &blob
.data
[bytes_eaten
], bytes_left
);
804 if (!identifier
) goto nomem
;
806 *partial_oid
= talloc_asprintf_append_buffer(tmp_oid
, ":0x%s", identifier
);
807 if (!*partial_oid
) goto nomem
;
808 TALLOC_FREE(identifier
);
810 *partial_oid
= tmp_oid
;
816 TALLOC_FREE(identifier
);
817 TALLOC_FREE(tmp_oid
);
821 /* read an object ID from a ASN1 buffer */
822 bool asn1_read_OID(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **OID
)
827 if (!asn1_start_tag(data
, ASN1_OID
)) return false;
829 len
= asn1_tag_remaining(data
);
831 data
->has_error
= true;
835 blob
= data_blob(NULL
, len
);
837 data
->has_error
= true;
841 if (!asn1_read(data
, blob
.data
, len
)) return false;
842 if (!asn1_end_tag(data
)) {
843 data_blob_free(&blob
);
847 if (!ber_read_OID_String(mem_ctx
, blob
, OID
)) {
848 data
->has_error
= true;
849 data_blob_free(&blob
);
853 data_blob_free(&blob
);
857 /* check that the next object ID is correct */
858 bool asn1_check_OID(struct asn1_data
*data
, const char *OID
)
862 if (!asn1_read_OID(data
, data
, &id
)) return false;
864 if (strcmp(id
, OID
) != 0) {
866 data
->has_error
= true;
873 /* read a LDAPString from a ASN1 buffer */
874 bool asn1_read_LDAPString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
877 len
= asn1_tag_remaining(data
);
879 data
->has_error
= true;
882 *s
= talloc_array(mem_ctx
, char, len
+1);
884 data
->has_error
= true;
888 return asn1_read(data
, *s
, len
);
892 /* read a GeneralString from a ASN1 buffer */
893 bool asn1_read_GeneralString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
895 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return false;
896 if (!asn1_read_LDAPString(data
, mem_ctx
, s
)) return false;
897 return asn1_end_tag(data
);
901 /* read a octet string blob */
902 bool asn1_read_OctetString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
906 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return false;
907 len
= asn1_tag_remaining(data
);
909 data
->has_error
= true;
912 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
913 if (!blob
->data
|| blob
->length
< len
) {
914 data
->has_error
= true;
917 if (!asn1_read(data
, blob
->data
, len
)) goto err
;
918 if (!asn1_end_tag(data
)) goto err
;
925 data_blob_free(blob
);
926 *blob
= data_blob_null
;
930 bool asn1_read_ContextSimple(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, uint8_t num
,
935 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
936 len
= asn1_tag_remaining(data
);
938 data
->has_error
= true;
941 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+ 1);
942 if ((len
!= 0) && (!blob
->data
)) {
943 data
->has_error
= true;
946 if (!asn1_read(data
, blob
->data
, len
)) return false;
949 return asn1_end_tag(data
);
952 /* read an integer without tag*/
953 bool asn1_read_implicit_Integer(struct asn1_data
*data
, int *i
)
956 bool first_byte
= true;
959 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
960 if (!asn1_read_uint8(data
, &b
)) return false;
963 /* Number is negative.
964 Set i to -1 for sign extend. */
971 return !data
->has_error
;
974 /* read an integer */
975 bool asn1_read_Integer(struct asn1_data
*data
, int *i
)
979 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return false;
980 if (!asn1_read_implicit_Integer(data
, i
)) return false;
981 return asn1_end_tag(data
);
984 /* read a BIT STRING */
985 bool asn1_read_BitString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, uint8_t *padding
)
989 if (!asn1_start_tag(data
, ASN1_BIT_STRING
)) return false;
990 len
= asn1_tag_remaining(data
);
992 data
->has_error
= true;
995 if (!asn1_read_uint8(data
, padding
)) return false;
997 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
998 if (!blob
->data
|| blob
->length
< len
) {
999 data
->has_error
= true;
1002 if (asn1_read(data
, blob
->data
, len
- 1)) {
1004 blob
->data
[len
] = 0;
1008 if (data
->has_error
) {
1009 data_blob_free(blob
);
1010 *blob
= data_blob_null
;
1017 /* read an integer */
1018 bool asn1_read_enumerated(struct asn1_data
*data
, int *v
)
1022 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1023 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
1025 if (!asn1_read_uint8(data
, &b
)) {
1030 return asn1_end_tag(data
);
1033 /* check a enumerated value is correct */
1034 bool asn1_check_enumerated(struct asn1_data
*data
, int v
)
1037 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1038 if (!asn1_read_uint8(data
, &b
)) return false;
1039 if (!asn1_end_tag(data
)) return false;
1042 data
->has_error
= false;
1044 return !data
->has_error
;
1047 /* write an enumerated value to the stream */
1048 bool asn1_write_enumerated(struct asn1_data
*data
, uint8_t v
)
1050 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return false;
1051 if (!asn1_write_uint8(data
, v
)) return false;
1052 return asn1_pop_tag(data
);
1056 Get us the data just written without copying
1058 bool asn1_blob(const struct asn1_data
*asn1
, DATA_BLOB
*blob
)
1060 if (asn1
->has_error
) {
1063 if (asn1
->nesting
!= NULL
) {
1066 blob
->data
= asn1
->data
;
1067 blob
->length
= asn1
->length
;
1071 bool asn1_extract_blob(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
1076 if (!asn1_blob(asn1
, &blob
)) {
1080 *pblob
= (DATA_BLOB
) { .length
= blob
.length
};
1081 pblob
->data
= talloc_move(mem_ctx
, &blob
.data
);
1084 * Stop access from here on
1086 asn1
->has_error
= true;
1092 Fill in an asn1 struct without making a copy
1094 void asn1_load_nocopy(struct asn1_data
*data
, uint8_t *buf
, size_t len
)
1101 int asn1_peek_full_tag(DATA_BLOB blob
, uint8_t tag
, size_t *packet_size
)
1103 struct asn1_data asn1
;
1108 asn1
.data
= blob
.data
;
1109 asn1
.length
= blob
.length
;
1111 ok
= asn1_peek_tag_needed_size(&asn1
, tag
, &size
);
1116 if (size
> blob
.length
) {
1117 *packet_size
= size
;
1121 *packet_size
= size
;