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
;
98 memcpy(data
->data
+ data
->ofs
, p
, len
);
104 /* useful fn for writing a uint8_t */
105 bool asn1_write_uint8(struct asn1_data
*data
, uint8_t v
)
107 return asn1_write(data
, &v
, 1);
110 /* push a tag onto the asn1 data buffer. Used for nested structures */
111 bool asn1_push_tag(struct asn1_data
*data
, uint8_t tag
)
113 struct nesting
*nesting
;
115 if (!asn1_write_uint8(data
, tag
)) {
118 nesting
= talloc(data
, struct nesting
);
120 data
->has_error
= true;
124 nesting
->start
= data
->ofs
;
125 nesting
->next
= data
->nesting
;
126 data
->nesting
= nesting
;
127 return asn1_write_uint8(data
, 0xff);
131 bool asn1_pop_tag(struct asn1_data
*data
)
133 struct nesting
*nesting
;
136 if (data
->has_error
) {
140 nesting
= data
->nesting
;
143 data
->has_error
= true;
146 len
= data
->ofs
- (nesting
->start
+1);
147 /* yes, this is ugly. We don't know in advance how many bytes the length
148 of a tag will take, so we assumed 1 byte. If we were wrong then we
149 need to correct our mistake */
150 if (len
> 0xFFFFFF) {
151 data
->data
[nesting
->start
] = 0x84;
152 if (!asn1_write_uint8(data
, 0)) return false;
153 if (!asn1_write_uint8(data
, 0)) return false;
154 if (!asn1_write_uint8(data
, 0)) return false;
155 if (!asn1_write_uint8(data
, 0)) return false;
156 memmove(data
->data
+nesting
->start
+5, data
->data
+nesting
->start
+1, len
);
157 data
->data
[nesting
->start
+1] = (len
>>24) & 0xFF;
158 data
->data
[nesting
->start
+2] = (len
>>16) & 0xFF;
159 data
->data
[nesting
->start
+3] = (len
>>8) & 0xFF;
160 data
->data
[nesting
->start
+4] = len
&0xff;
161 } else if (len
> 0xFFFF) {
162 data
->data
[nesting
->start
] = 0x83;
163 if (!asn1_write_uint8(data
, 0)) return false;
164 if (!asn1_write_uint8(data
, 0)) return false;
165 if (!asn1_write_uint8(data
, 0)) return false;
166 memmove(data
->data
+nesting
->start
+4, data
->data
+nesting
->start
+1, len
);
167 data
->data
[nesting
->start
+1] = (len
>>16) & 0xFF;
168 data
->data
[nesting
->start
+2] = (len
>>8) & 0xFF;
169 data
->data
[nesting
->start
+3] = len
&0xff;
170 } else if (len
> 255) {
171 data
->data
[nesting
->start
] = 0x82;
172 if (!asn1_write_uint8(data
, 0)) return false;
173 if (!asn1_write_uint8(data
, 0)) return false;
174 memmove(data
->data
+nesting
->start
+3, data
->data
+nesting
->start
+1, len
);
175 data
->data
[nesting
->start
+1] = len
>>8;
176 data
->data
[nesting
->start
+2] = len
&0xff;
177 } else if (len
> 127) {
178 data
->data
[nesting
->start
] = 0x81;
179 if (!asn1_write_uint8(data
, 0)) return false;
180 memmove(data
->data
+nesting
->start
+2, data
->data
+nesting
->start
+1, len
);
181 data
->data
[nesting
->start
+1] = len
;
183 data
->data
[nesting
->start
] = len
;
186 data
->nesting
= nesting
->next
;
187 talloc_free(nesting
);
191 /* "i" is the one's complement representation, as is the normal result of an
192 * implicit signed->unsigned conversion */
194 static bool push_int_bigendian(struct asn1_data
*data
, unsigned int i
, bool negative
)
196 uint8_t lowest
= i
& 0xFF;
200 if (!push_int_bigendian(data
, i
, negative
))
203 if (data
->nesting
->start
+1 == data
->ofs
) {
205 /* We did not write anything yet, looking at the highest
209 /* Don't write leading 0xff's */
213 if ((lowest
& 0x80) == 0) {
214 /* The only exception for a leading 0xff is if
215 * the highest bit is 0, which would indicate
216 * a positive value */
217 if (!asn1_write_uint8(data
, 0xff))
222 /* The highest bit of a positive integer is 1,
223 * this would indicate a negative number. Push
224 * a 0 to indicate a positive one */
225 if (!asn1_write_uint8(data
, 0))
231 return asn1_write_uint8(data
, lowest
);
234 /* write an Integer without the tag framing. Needed for example for the LDAP
235 * Abandon Operation */
237 bool asn1_write_implicit_Integer(struct asn1_data
*data
, int i
)
239 if (data
->has_error
) {
244 /* -1 is special as it consists of all-0xff bytes. In
245 push_int_bigendian this is the only case that is not
246 properly handled, as all 0xff bytes would be handled as
247 leading ones to be ignored. */
248 return asn1_write_uint8(data
, 0xff);
250 return push_int_bigendian(data
, i
, i
<0);
255 /* write an integer */
256 bool asn1_write_Integer(struct asn1_data
*data
, int i
)
258 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return false;
259 if (!asn1_write_implicit_Integer(data
, i
)) return false;
260 return asn1_pop_tag(data
);
263 /* write a BIT STRING */
264 bool asn1_write_BitString(struct asn1_data
*data
, const void *p
, size_t length
, uint8_t padding
)
266 if (!asn1_push_tag(data
, ASN1_BIT_STRING
)) return false;
267 if (!asn1_write_uint8(data
, padding
)) return false;
268 if (!asn1_write(data
, p
, length
)) return false;
269 return asn1_pop_tag(data
);
272 bool ber_write_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *OID
)
275 const char *p
= (const char *)OID
;
280 if (!isdigit(*p
)) return false;
281 v
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
282 if (newp
[0] != '.' || error
!= 0) {
287 if (!isdigit(*p
)) return false;
288 v2
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
289 if (newp
[0] != '.' || error
!= 0) {
294 /*the ber representation can't use more space than the string one */
295 *blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(OID
));
296 if (!blob
->data
) return false;
298 blob
->data
[0] = 40*v
+ v2
;
302 if (!isdigit(*p
)) return false;
303 v
= smb_strtoul(p
, &newp
, 10, &error
, SMB_STR_STANDARD
);
304 if (newp
[0] == '.' || error
!= 0) {
306 /* check for empty last component */
307 if (!*p
) return false;
308 } else if (newp
[0] == '\0') {
311 data_blob_free(blob
);
314 if (v
>= (1<<28)) blob
->data
[i
++] = (0x80 | ((v
>>28)&0x7f));
315 if (v
>= (1<<21)) blob
->data
[i
++] = (0x80 | ((v
>>21)&0x7f));
316 if (v
>= (1<<14)) blob
->data
[i
++] = (0x80 | ((v
>>14)&0x7f));
317 if (v
>= (1<<7)) blob
->data
[i
++] = (0x80 | ((v
>>7)&0x7f));
318 blob
->data
[i
++] = (v
&0x7f);
327 * Serialize partial OID string.
328 * Partial OIDs are in the form:
332 bool ber_write_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, const char *partial_oid
)
334 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
335 char *oid
= talloc_strdup(tmp_ctx
, partial_oid
);
338 /* truncate partial part so ber_write_OID_String() works */
339 p
= strchr(oid
, ':');
345 if (!ber_write_OID_String(mem_ctx
, blob
, oid
)) {
346 talloc_free(tmp_ctx
);
350 /* Add partially encoded sub-identifier */
352 DATA_BLOB tmp_blob
= strhex_to_data_blob(tmp_ctx
, p
);
353 if (!data_blob_append(mem_ctx
, blob
, tmp_blob
.data
,
355 talloc_free(tmp_ctx
);
360 talloc_free(tmp_ctx
);
365 /* write an object ID to a ASN1 buffer */
366 bool asn1_write_OID(struct asn1_data
*data
, const char *OID
)
370 if (!asn1_push_tag(data
, ASN1_OID
)) return false;
372 if (!ber_write_OID_String(NULL
, &blob
, OID
)) {
373 data
->has_error
= true;
377 if (!asn1_write(data
, blob
.data
, blob
.length
)) {
378 data_blob_free(&blob
);
379 data
->has_error
= true;
382 data_blob_free(&blob
);
383 return asn1_pop_tag(data
);
386 /* write an octet string */
387 bool asn1_write_OctetString(struct asn1_data
*data
, const void *p
, size_t length
)
389 if (!asn1_push_tag(data
, ASN1_OCTET_STRING
)) return false;
390 if (!asn1_write(data
, p
, length
)) return false;
391 return asn1_pop_tag(data
);
394 /* write a LDAP string */
395 bool asn1_write_LDAPString(struct asn1_data
*data
, const char *s
)
397 return asn1_write(data
, s
, strlen(s
));
400 /* write a LDAP string from a DATA_BLOB */
401 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data
*data
, const DATA_BLOB
*s
)
403 return asn1_write(data
, s
->data
, s
->length
);
406 /* write a general string */
407 bool asn1_write_GeneralString(struct asn1_data
*data
, const char *s
)
409 if (!asn1_push_tag(data
, ASN1_GENERAL_STRING
)) return false;
410 if (!asn1_write_LDAPString(data
, s
)) return false;
411 return asn1_pop_tag(data
);
414 bool asn1_write_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
416 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
417 if (!asn1_write(data
, blob
->data
, blob
->length
)) return false;
418 return asn1_pop_tag(data
);
421 /* write a BOOLEAN */
422 bool asn1_write_BOOLEAN(struct asn1_data
*data
, bool v
)
424 if (!asn1_push_tag(data
, ASN1_BOOLEAN
)) return false;
425 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
426 return asn1_pop_tag(data
);
429 bool asn1_read_BOOLEAN(struct asn1_data
*data
, bool *v
)
432 if (!asn1_start_tag(data
, ASN1_BOOLEAN
)) return false;
434 if (!asn1_read_uint8(data
, &tmp
)) return false;
438 return asn1_end_tag(data
);
441 /* write a BOOLEAN in a simple context */
442 bool asn1_write_BOOLEAN_context(struct asn1_data
*data
, bool v
, int context
)
444 if (!asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
445 if (!asn1_write_uint8(data
, v
? 0xFF : 0)) return false;
446 return asn1_pop_tag(data
);
449 bool asn1_read_BOOLEAN_context(struct asn1_data
*data
, bool *v
, int context
)
452 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(context
))) return false;
454 if (!asn1_read_uint8(data
, &tmp
)) return false;
458 return asn1_end_tag(data
);
461 /* check a BOOLEAN */
462 bool asn1_check_BOOLEAN(struct asn1_data
*data
, bool v
)
466 if (!asn1_read_uint8(data
, &b
)) return false;
467 if (b
!= ASN1_BOOLEAN
) {
468 data
->has_error
= true;
471 if (!asn1_read_uint8(data
, &b
)) return false;
473 data
->has_error
= true;
476 return !data
->has_error
;
480 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
481 bool asn1_load(struct asn1_data
*data
, DATA_BLOB blob
)
484 data
->data
= (uint8_t *)talloc_memdup(data
, blob
.data
, blob
.length
);
486 data
->has_error
= true;
489 data
->length
= blob
.length
;
493 /* Peek into an ASN1 buffer, not advancing the pointer */
494 bool asn1_peek(struct asn1_data
*data
, void *p
, int len
)
499 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
)
502 if (data
->ofs
+ len
> data
->length
) {
503 /* we need to mark the buffer as consumed, so the caller knows
504 this was an out of data error, and not a decode error */
505 data
->ofs
= data
->length
;
509 memcpy(p
, data
->data
+ data
->ofs
, len
);
513 /* read from a ASN1 buffer, advancing the buffer pointer */
514 bool asn1_read(struct asn1_data
*data
, void *p
, int len
)
516 if (!asn1_peek(data
, p
, len
)) {
517 data
->has_error
= true;
525 /* read a uint8_t from a ASN1 buffer */
526 bool asn1_read_uint8(struct asn1_data
*data
, uint8_t *v
)
528 return asn1_read(data
, v
, 1);
531 bool asn1_peek_uint8(struct asn1_data
*data
, uint8_t *v
)
533 return asn1_peek(data
, v
, 1);
536 bool asn1_peek_tag(struct asn1_data
*data
, uint8_t tag
)
540 if (asn1_tag_remaining(data
) <= 0) {
544 if (!asn1_peek_uint8(data
, &b
))
551 * just get the needed size the tag would consume
553 static bool asn1_peek_tag_needed_size(struct asn1_data
*data
, uint8_t tag
,
556 off_t start_ofs
= data
->ofs
;
560 if (data
->has_error
) {
564 if (!asn1_read_uint8(data
, &b
)) {
565 data
->ofs
= start_ofs
;
566 data
->has_error
= false;
571 data
->ofs
= start_ofs
;
572 data
->has_error
= false;
576 if (!asn1_read_uint8(data
, &b
)) {
577 data
->ofs
= start_ofs
;
578 data
->has_error
= false;
584 if (!asn1_read_uint8(data
, &b
)) {
585 data
->ofs
= start_ofs
;
586 data
->has_error
= false;
591 * We should not allow more than 4 bytes
592 * for the encoding of the tag length.
594 * Otherwise we'd overflow the taglen
595 * variable on 32 bit systems.
597 data
->ofs
= start_ofs
;
598 data
->has_error
= false;
605 if (!asn1_read_uint8(data
, &b
)) {
606 data
->ofs
= start_ofs
;
607 data
->has_error
= false;
611 tmp_taglen
= (taglen
<< 8) | b
;
613 if ((tmp_taglen
>> 8) != taglen
) {
615 data
->ofs
= start_ofs
;
616 data
->has_error
= false;
627 *size
= (data
->ofs
- start_ofs
) + taglen
;
629 data
->ofs
= start_ofs
;
630 data
->has_error
= false;
634 /* start reading a nested asn1 structure */
635 bool asn1_start_tag(struct asn1_data
*data
, uint8_t tag
)
638 struct nesting
*nesting
;
640 if (!asn1_read_uint8(data
, &b
))
644 data
->has_error
= true;
647 nesting
= talloc(data
, struct nesting
);
649 data
->has_error
= true;
653 if (!asn1_read_uint8(data
, &b
)) {
659 if (!asn1_read_uint8(data
, &b
))
665 if (!asn1_read_uint8(data
, &b
))
668 taglen
= (nesting
->taglen
<< 8) | b
;
670 if ((taglen
>> 8) != nesting
->taglen
) {
672 data
->has_error
= true;
675 nesting
->taglen
= taglen
;
682 nesting
->start
= data
->ofs
;
683 nesting
->next
= data
->nesting
;
684 data
->nesting
= nesting
;
685 if (asn1_tag_remaining(data
) == -1) {
688 return !data
->has_error
;
691 /* stop reading a tag */
692 bool asn1_end_tag(struct asn1_data
*data
)
694 struct nesting
*nesting
;
696 /* make sure we read it all */
697 if (asn1_tag_remaining(data
) != 0) {
698 data
->has_error
= true;
702 nesting
= data
->nesting
;
705 data
->has_error
= true;
709 data
->nesting
= nesting
->next
;
710 talloc_free(nesting
);
714 /* work out how many bytes are left in this nested tag */
715 int asn1_tag_remaining(struct asn1_data
*data
)
718 if (data
->has_error
) {
722 if (!data
->nesting
) {
723 data
->has_error
= true;
726 remaining
= data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
727 if (remaining
> (data
->length
- data
->ofs
)) {
728 data
->has_error
= true;
732 data
->has_error
= true;
739 * Internal implementation for reading binary OIDs
740 * Reading is done as far in the buffer as valid OID
741 * till buffer ends or not valid sub-identifier is found.
743 static bool _ber_read_OID_String_impl(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
744 char **OID
, size_t *bytes_eaten
)
749 char *tmp_oid
= NULL
;
751 if (blob
.length
< 2) return false;
755 tmp_oid
= talloc_asprintf(mem_ctx
, "%u.%u", b
[0]/40, b
[0]%40);
756 if (!tmp_oid
) goto nomem
;
758 if (bytes_eaten
!= NULL
) {
762 for(i
= 1, v
= 0; i
< blob
.length
; i
++) {
763 v
= (v
<<7) | (b
[i
]&0x7f);
764 if ( ! (b
[i
] & 0x80)) {
765 tmp_oid
= talloc_asprintf_append_buffer(tmp_oid
, ".%u", v
);
770 if (!tmp_oid
) goto nomem
;
780 /* read an object ID from a data blob */
781 bool ber_read_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
, char **OID
)
785 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, OID
, &bytes_eaten
))
788 return (bytes_eaten
== blob
.length
);
792 * Deserialize partial OID string.
793 * Partial OIDs are in the form:
797 bool ber_read_partial_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
,
802 char *identifier
= NULL
;
803 char *tmp_oid
= NULL
;
805 if (!_ber_read_OID_String_impl(mem_ctx
, blob
, &tmp_oid
, &bytes_eaten
))
808 if (bytes_eaten
< blob
.length
) {
809 bytes_left
= blob
.length
- bytes_eaten
;
810 identifier
= hex_encode_talloc(mem_ctx
, &blob
.data
[bytes_eaten
], bytes_left
);
811 if (!identifier
) goto nomem
;
813 *partial_oid
= talloc_asprintf_append_buffer(tmp_oid
, ":0x%s", identifier
);
814 if (!*partial_oid
) goto nomem
;
815 TALLOC_FREE(identifier
);
817 *partial_oid
= tmp_oid
;
823 TALLOC_FREE(identifier
);
824 TALLOC_FREE(tmp_oid
);
828 /* read an object ID from a ASN1 buffer */
829 bool asn1_read_OID(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **OID
)
834 if (!asn1_start_tag(data
, ASN1_OID
)) return false;
836 len
= asn1_tag_remaining(data
);
838 data
->has_error
= true;
842 blob
= data_blob(NULL
, len
);
844 data
->has_error
= true;
848 if (!asn1_read(data
, blob
.data
, len
)) return false;
849 if (!asn1_end_tag(data
)) {
850 data_blob_free(&blob
);
854 if (!ber_read_OID_String(mem_ctx
, blob
, OID
)) {
855 data
->has_error
= true;
856 data_blob_free(&blob
);
860 data_blob_free(&blob
);
864 /* check that the next object ID is correct */
865 bool asn1_check_OID(struct asn1_data
*data
, const char *OID
)
869 if (!asn1_read_OID(data
, data
, &id
)) return false;
871 if (strcmp(id
, OID
) != 0) {
873 data
->has_error
= true;
880 /* read a LDAPString from a ASN1 buffer */
881 bool asn1_read_LDAPString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
884 len
= asn1_tag_remaining(data
);
886 data
->has_error
= true;
889 *s
= talloc_array(mem_ctx
, char, len
+1);
891 data
->has_error
= true;
895 return asn1_read(data
, *s
, len
);
899 /* read a GeneralString from a ASN1 buffer */
900 bool asn1_read_GeneralString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
902 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return false;
903 if (!asn1_read_LDAPString(data
, mem_ctx
, s
)) return false;
904 return asn1_end_tag(data
);
908 /* read a octet string blob */
909 bool asn1_read_OctetString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
913 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return false;
914 len
= asn1_tag_remaining(data
);
916 data
->has_error
= true;
919 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
920 if (!blob
->data
|| blob
->length
< len
) {
921 data
->has_error
= true;
924 if (!asn1_read(data
, blob
->data
, len
)) goto err
;
925 if (!asn1_end_tag(data
)) goto err
;
932 data_blob_free(blob
);
933 *blob
= data_blob_null
;
937 bool asn1_read_ContextSimple(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, uint8_t num
,
942 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
943 len
= asn1_tag_remaining(data
);
945 data
->has_error
= true;
948 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+ 1);
949 if ((len
!= 0) && (!blob
->data
)) {
950 data
->has_error
= true;
953 if (!asn1_read(data
, blob
->data
, len
)) return false;
956 return asn1_end_tag(data
);
959 /* read an integer without tag*/
960 bool asn1_read_implicit_Integer(struct asn1_data
*data
, int *i
)
964 bool first_byte
= true;
968 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
969 if (!asn1_read_uint8(data
, &b
)) return false;
972 /* Number is negative. */
981 return !data
->has_error
;
984 /* read an integer */
985 bool asn1_read_Integer(struct asn1_data
*data
, int *i
)
989 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return false;
990 if (!asn1_read_implicit_Integer(data
, i
)) return false;
991 return asn1_end_tag(data
);
994 /* read a BIT STRING */
995 bool asn1_read_BitString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, uint8_t *padding
)
999 if (!asn1_start_tag(data
, ASN1_BIT_STRING
)) return false;
1000 len
= asn1_tag_remaining(data
);
1002 data
->has_error
= true;
1005 if (!asn1_read_uint8(data
, padding
)) return false;
1007 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
1008 if (!blob
->data
|| blob
->length
< len
) {
1009 data
->has_error
= true;
1012 if (asn1_read(data
, blob
->data
, len
- 1)) {
1014 blob
->data
[len
] = 0;
1018 if (data
->has_error
) {
1019 data_blob_free(blob
);
1020 *blob
= data_blob_null
;
1027 /* read an integer */
1028 bool asn1_read_enumerated(struct asn1_data
*data
, int *v
)
1032 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1033 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
1035 if (!asn1_read_uint8(data
, &b
)) {
1040 return asn1_end_tag(data
);
1043 /* check a enumerated value is correct */
1044 bool asn1_check_enumerated(struct asn1_data
*data
, int v
)
1047 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
1048 if (!asn1_read_uint8(data
, &b
)) return false;
1049 if (!asn1_end_tag(data
)) return false;
1052 data
->has_error
= false;
1054 return !data
->has_error
;
1057 /* write an enumerated value to the stream */
1058 bool asn1_write_enumerated(struct asn1_data
*data
, uint8_t v
)
1060 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return false;
1061 if (!asn1_write_uint8(data
, v
)) return false;
1062 return asn1_pop_tag(data
);
1066 Get us the data just written without copying
1068 bool asn1_blob(const struct asn1_data
*asn1
, DATA_BLOB
*blob
)
1070 if (asn1
->has_error
) {
1073 if (asn1
->nesting
!= NULL
) {
1076 blob
->data
= asn1
->data
;
1077 blob
->length
= asn1
->length
;
1081 bool asn1_extract_blob(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
1086 if (!asn1_blob(asn1
, &blob
)) {
1090 *pblob
= (DATA_BLOB
) { .length
= blob
.length
};
1091 pblob
->data
= talloc_move(mem_ctx
, &blob
.data
);
1094 * Stop access from here on
1096 asn1
->has_error
= true;
1102 Fill in an asn1 struct without making a copy
1104 void asn1_load_nocopy(struct asn1_data
*data
, uint8_t *buf
, size_t len
)
1111 int asn1_peek_full_tag(DATA_BLOB blob
, uint8_t tag
, size_t *packet_size
)
1113 struct asn1_data asn1
;
1118 asn1
.data
= blob
.data
;
1119 asn1
.length
= blob
.length
;
1121 ok
= asn1_peek_tag_needed_size(&asn1
, tag
, &size
);
1126 if (size
> blob
.length
) {
1127 *packet_size
= size
;
1131 *packet_size
= size
;