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;
43 if (data
->length
< data
->ofs
+len
) {
45 newp
= talloc_realloc(data
, data
->data
, uint8_t, data
->ofs
+len
);
48 data
->has_error
= true;
52 data
->length
= data
->ofs
+len
;
54 memcpy(data
->data
+ data
->ofs
, p
, len
);
59 /* useful fn for writing a uint8_t */
60 bool asn1_write_uint8(struct asn1_data
*data
, uint8_t v
)
62 return asn1_write(data
, &v
, 1);
65 /* push a tag onto the asn1 data buffer. Used for nested structures */
66 bool asn1_push_tag(struct asn1_data
*data
, uint8_t tag
)
68 struct nesting
*nesting
;
70 asn1_write_uint8(data
, tag
);
71 nesting
= talloc(data
, struct nesting
);
73 data
->has_error
= true;
77 nesting
->start
= data
->ofs
;
78 nesting
->next
= data
->nesting
;
79 data
->nesting
= nesting
;
80 return asn1_write_uint8(data
, 0xff);
84 bool asn1_pop_tag(struct asn1_data
*data
)
86 struct nesting
*nesting
;
89 nesting
= data
->nesting
;
92 data
->has_error
= true;
95 len
= data
->ofs
- (nesting
->start
+1);
96 /* yes, this is ugly. We don't know in advance how many bytes the length
97 of a tag will take, so we assumed 1 byte. If we were wrong then we
98 need to correct our mistake */
100 data
->data
[nesting
->start
] = 0x84;
101 if (!asn1_write_uint8(data
, 0)) return false;
102 if (!asn1_write_uint8(data
, 0)) return false;
103 if (!asn1_write_uint8(data
, 0)) return false;
104 if (!asn1_write_uint8(data
, 0)) return false;
105 memmove(data
->data
+nesting
->start
+5, data
->data
+nesting
->start
+1, len
);
106 data
->data
[nesting
->start
+1] = (len
>>24) & 0xFF;
107 data
->data
[nesting
->start
+2] = (len
>>16) & 0xFF;
108 data
->data
[nesting
->start
+3] = (len
>>8) & 0xFF;
109 data
->data
[nesting
->start
+4] = len
&0xff;
110 } else if (len
> 0xFFFF) {
111 data
->data
[nesting
->start
] = 0x83;
112 if (!asn1_write_uint8(data
, 0)) return false;
113 if (!asn1_write_uint8(data
, 0)) return false;
114 if (!asn1_write_uint8(data
, 0)) return false;
115 memmove(data
->data
+nesting
->start
+4, data
->data
+nesting
->start
+1, len
);
116 data
->data
[nesting
->start
+1] = (len
>>16) & 0xFF;
117 data
->data
[nesting
->start
+2] = (len
>>8) & 0xFF;
118 data
->data
[nesting
->start
+3] = len
&0xff;
119 } else if (len
> 255) {
120 data
->data
[nesting
->start
] = 0x82;
121 if (!asn1_write_uint8(data
, 0)) return false;
122 if (!asn1_write_uint8(data
, 0)) return false;
123 memmove(data
->data
+nesting
->start
+3, data
->data
+nesting
->start
+1, len
);
124 data
->data
[nesting
->start
+1] = len
>>8;
125 data
->data
[nesting
->start
+2] = len
&0xff;
126 } else if (len
> 127) {
127 data
->data
[nesting
->start
] = 0x81;
128 if (!asn1_write_uint8(data
, 0)) return false;
129 memmove(data
->data
+nesting
->start
+2, data
->data
+nesting
->start
+1, len
);
130 data
->data
[nesting
->start
+1] = len
;
132 data
->data
[nesting
->start
] = len
;
135 data
->nesting
= nesting
->next
;
136 talloc_free(nesting
);
140 /* "i" is the one's complement representation, as is the normal result of an
141 * implicit signed->unsigned conversion */
143 static bool push_int_bigendian(struct asn1_data
*data
, unsigned int i
, bool negative
)
145 uint8_t lowest
= i
& 0xFF;
149 if (!push_int_bigendian(data
, i
, negative
))
152 if (data
->nesting
->start
+1 == data
->ofs
) {
154 /* We did not write anything yet, looking at the highest
158 /* Don't write leading 0xff's */
162 if ((lowest
& 0x80) == 0) {
163 /* The only exception for a leading 0xff is if
164 * the highest bit is 0, which would indicate
165 * a positive value */
166 if (!asn1_write_uint8(data
, 0xff))
171 /* The highest bit of a positive integer is 1,
172 * this would indicate a negative number. Push
173 * a 0 to indicate a positive one */
174 if (!asn1_write_uint8(data
, 0))
180 return asn1_write_uint8(data
, lowest
);
183 /* write an Integer without the tag framing. Needed for example for the LDAP
184 * Abandon Operation */
186 bool asn1_write_implicit_Integer(struct asn1_data
*data
, int i
)
189 /* -1 is special as it consists of all-0xff bytes. In
190 push_int_bigendian this is the only case that is not
191 properly handled, as all 0xff bytes would be handled as
192 leading ones to be ignored. */
193 return asn1_write_uint8(data
, 0xff);
195 return push_int_bigendian(data
, i
, i
<0);
200 /* write an integer */
201 bool asn1_write_Integer(struct asn1_data
*data
, int i
)
203 if (!asn1_push_tag(data
, ASN1_INTEGER
)) return false;
204 if (!asn1_write_implicit_Integer(data
, i
)) return false;
205 return asn1_pop_tag(data
);
208 bool ber_write_OID_String(DATA_BLOB
*blob
, const char *OID
)
211 const char *p
= (const char *)OID
;
215 v
= strtoul(p
, &newp
, 10);
216 if (newp
[0] != '.') return false;
219 v2
= strtoul(p
, &newp
, 10);
220 if (newp
[0] != '.') return false;
223 /*the ber representation can't use more space then the string one */
224 *blob
= data_blob(NULL
, strlen(OID
));
225 if (!blob
->data
) return false;
227 blob
->data
[0] = 40*v
+ v2
;
231 v
= strtoul(p
, &newp
, 10);
232 if (newp
[0] == '.') {
234 } else if (newp
[0] == '\0') {
237 data_blob_free(blob
);
240 if (v
>= (1<<28)) blob
->data
[i
++] = (0x80 | ((v
>>28)&0x7f));
241 if (v
>= (1<<21)) blob
->data
[i
++] = (0x80 | ((v
>>21)&0x7f));
242 if (v
>= (1<<14)) blob
->data
[i
++] = (0x80 | ((v
>>14)&0x7f));
243 if (v
>= (1<<7)) blob
->data
[i
++] = (0x80 | ((v
>>7)&0x7f));
244 blob
->data
[i
++] = (v
&0x7f);
252 /* write an object ID to a ASN1 buffer */
253 bool asn1_write_OID(struct asn1_data
*data
, const char *OID
)
257 if (!asn1_push_tag(data
, ASN1_OID
)) return false;
259 if (!ber_write_OID_String(&blob
, OID
)) {
260 data
->has_error
= true;
264 if (!asn1_write(data
, blob
.data
, blob
.length
)) {
265 data_blob_free(&blob
);
266 data
->has_error
= true;
269 data_blob_free(&blob
);
270 return asn1_pop_tag(data
);
273 /* write an octet string */
274 bool asn1_write_OctetString(struct asn1_data
*data
, const void *p
, size_t length
)
276 asn1_push_tag(data
, ASN1_OCTET_STRING
);
277 asn1_write(data
, p
, length
);
279 return !data
->has_error
;
282 /* write a LDAP string */
283 bool asn1_write_LDAPString(struct asn1_data
*data
, const char *s
)
285 asn1_write(data
, s
, strlen(s
));
286 return !data
->has_error
;
289 /* write a LDAP string from a DATA_BLOB */
290 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data
*data
, const DATA_BLOB
*s
)
292 asn1_write(data
, s
->data
, s
->length
);
293 return !data
->has_error
;
296 /* write a general string */
297 bool asn1_write_GeneralString(struct asn1_data
*data
, const char *s
)
299 asn1_push_tag(data
, ASN1_GENERAL_STRING
);
300 asn1_write_LDAPString(data
, s
);
302 return !data
->has_error
;
305 bool asn1_write_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
307 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(num
));
308 asn1_write(data
, blob
->data
, blob
->length
);
310 return !data
->has_error
;
313 /* write a BOOLEAN */
314 bool asn1_write_BOOLEAN(struct asn1_data
*data
, bool v
)
316 asn1_push_tag(data
, ASN1_BOOLEAN
);
317 asn1_write_uint8(data
, v
? 0xFF : 0);
319 return !data
->has_error
;
322 bool asn1_read_BOOLEAN(struct asn1_data
*data
, bool *v
)
325 asn1_start_tag(data
, ASN1_BOOLEAN
);
326 asn1_read_uint8(data
, &tmp
);
333 return !data
->has_error
;
336 /* write a BOOLEAN in a simple context */
337 bool asn1_write_BOOLEAN_context(struct asn1_data
*data
, bool v
, int context
)
339 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(context
));
340 asn1_write_uint8(data
, v
? 0xFF : 0);
342 return !data
->has_error
;
345 bool asn1_read_BOOLEAN_context(struct asn1_data
*data
, bool *v
, int context
)
348 asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(context
));
349 asn1_read_uint8(data
, &tmp
);
356 return !data
->has_error
;
359 /* check a BOOLEAN */
360 bool asn1_check_BOOLEAN(struct asn1_data
*data
, bool v
)
364 asn1_read_uint8(data
, &b
);
365 if (b
!= ASN1_BOOLEAN
) {
366 data
->has_error
= true;
369 asn1_read_uint8(data
, &b
);
371 data
->has_error
= true;
374 return !data
->has_error
;
378 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
379 bool asn1_load(struct asn1_data
*data
, DATA_BLOB blob
)
382 data
->data
= (uint8_t *)talloc_memdup(data
, blob
.data
, blob
.length
);
384 data
->has_error
= true;
387 data
->length
= blob
.length
;
391 /* Peek into an ASN1 buffer, not advancing the pointer */
392 bool asn1_peek(struct asn1_data
*data
, void *p
, int len
)
397 if (len
< 0 || data
->ofs
+ len
< data
->ofs
|| data
->ofs
+ len
< len
)
400 if (data
->ofs
+ len
> data
->length
) {
401 /* we need to mark the buffer as consumed, so the caller knows
402 this was an out of data error, and not a decode error */
403 data
->ofs
= data
->length
;
407 memcpy(p
, data
->data
+ data
->ofs
, len
);
411 /* read from a ASN1 buffer, advancing the buffer pointer */
412 bool asn1_read(struct asn1_data
*data
, void *p
, int len
)
414 if (!asn1_peek(data
, p
, len
)) {
415 data
->has_error
= true;
423 /* read a uint8_t from a ASN1 buffer */
424 bool asn1_read_uint8(struct asn1_data
*data
, uint8_t *v
)
426 return asn1_read(data
, v
, 1);
429 bool asn1_peek_uint8(struct asn1_data
*data
, uint8_t *v
)
431 return asn1_peek(data
, v
, 1);
434 bool asn1_peek_tag(struct asn1_data
*data
, uint8_t tag
)
438 if (asn1_tag_remaining(data
) <= 0) {
442 if (!asn1_peek_uint8(data
, &b
))
448 /* start reading a nested asn1 structure */
449 bool asn1_start_tag(struct asn1_data
*data
, uint8_t tag
)
452 struct nesting
*nesting
;
454 if (!asn1_read_uint8(data
, &b
))
458 data
->has_error
= true;
461 nesting
= talloc(data
, struct nesting
);
463 data
->has_error
= true;
467 if (!asn1_read_uint8(data
, &b
)) {
473 if (!asn1_read_uint8(data
, &b
))
477 if (!asn1_read_uint8(data
, &b
))
479 nesting
->taglen
= (nesting
->taglen
<< 8) | b
;
485 nesting
->start
= data
->ofs
;
486 nesting
->next
= data
->nesting
;
487 data
->nesting
= nesting
;
488 if (asn1_tag_remaining(data
) == -1) {
491 return !data
->has_error
;
494 /* stop reading a tag */
495 bool asn1_end_tag(struct asn1_data
*data
)
497 struct nesting
*nesting
;
499 /* make sure we read it all */
500 if (asn1_tag_remaining(data
) != 0) {
501 data
->has_error
= true;
505 nesting
= data
->nesting
;
508 data
->has_error
= true;
512 data
->nesting
= nesting
->next
;
513 talloc_free(nesting
);
517 /* work out how many bytes are left in this nested tag */
518 int asn1_tag_remaining(struct asn1_data
*data
)
521 if (data
->has_error
) {
525 if (!data
->nesting
) {
526 data
->has_error
= true;
529 remaining
= data
->nesting
->taglen
- (data
->ofs
- data
->nesting
->start
);
530 if (remaining
> (data
->length
- data
->ofs
)) {
531 data
->has_error
= true;
537 /* read an object ID from a data blob */
538 bool ber_read_OID_String(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
, const char **OID
)
543 char *tmp_oid
= NULL
;
545 if (blob
.length
< 2) return false;
549 tmp_oid
= talloc_asprintf(mem_ctx
, "%u", b
[0]/40);
550 if (!tmp_oid
) goto nomem
;
551 tmp_oid
= talloc_asprintf_append_buffer(tmp_oid
, ".%u", b
[0]%40);
552 if (!tmp_oid
) goto nomem
;
554 for(i
= 1, v
= 0; i
< blob
.length
; i
++) {
555 v
= (v
<<7) | (b
[i
]&0x7f);
556 if ( ! (b
[i
] & 0x80)) {
557 tmp_oid
= talloc_asprintf_append_buffer(tmp_oid
, ".%u", v
);
560 if (!tmp_oid
) goto nomem
;
564 talloc_free(tmp_oid
);
575 /* read an object ID from a ASN1 buffer */
576 bool asn1_read_OID(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, const char **OID
)
581 if (!asn1_start_tag(data
, ASN1_OID
)) return false;
583 len
= asn1_tag_remaining(data
);
585 data
->has_error
= true;
589 blob
= data_blob(NULL
, len
);
591 data
->has_error
= true;
595 asn1_read(data
, blob
.data
, len
);
597 if (data
->has_error
) {
598 data_blob_free(&blob
);
602 if (!ber_read_OID_String(mem_ctx
, blob
, OID
)) {
603 data
->has_error
= true;
604 data_blob_free(&blob
);
608 data_blob_free(&blob
);
612 /* check that the next object ID is correct */
613 bool asn1_check_OID(struct asn1_data
*data
, const char *OID
)
617 if (!asn1_read_OID(data
, data
, &id
)) return false;
619 if (strcmp(id
, OID
) != 0) {
620 talloc_free(discard_const(id
));
621 data
->has_error
= true;
624 talloc_free(discard_const(id
));
628 /* read a LDAPString from a ASN1 buffer */
629 bool asn1_read_LDAPString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
632 len
= asn1_tag_remaining(data
);
634 data
->has_error
= true;
637 *s
= talloc_array(mem_ctx
, char, len
+1);
639 data
->has_error
= true;
642 asn1_read(data
, *s
, len
);
644 return !data
->has_error
;
648 /* read a GeneralString from a ASN1 buffer */
649 bool asn1_read_GeneralString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, char **s
)
651 if (!asn1_start_tag(data
, ASN1_GENERAL_STRING
)) return false;
652 if (!asn1_read_LDAPString(data
, mem_ctx
, s
)) return false;
653 return asn1_end_tag(data
);
657 /* read a octet string blob */
658 bool asn1_read_OctetString(struct asn1_data
*data
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
662 if (!asn1_start_tag(data
, ASN1_OCTET_STRING
)) return false;
663 len
= asn1_tag_remaining(data
);
665 data
->has_error
= true;
668 *blob
= data_blob_talloc(mem_ctx
, NULL
, len
+1);
670 data
->has_error
= true;
673 asn1_read(data
, blob
->data
, len
);
678 if (data
->has_error
) {
679 data_blob_free(blob
);
680 *blob
= data_blob_null
;
686 bool asn1_read_ContextSimple(struct asn1_data
*data
, uint8_t num
, DATA_BLOB
*blob
)
690 if (!asn1_start_tag(data
, ASN1_CONTEXT_SIMPLE(num
))) return false;
691 len
= asn1_tag_remaining(data
);
693 data
->has_error
= true;
696 *blob
= data_blob(NULL
, len
);
697 if ((len
!= 0) && (!blob
->data
)) {
698 data
->has_error
= true;
701 asn1_read(data
, blob
->data
, len
);
703 return !data
->has_error
;
706 /* read an integer without tag*/
707 bool asn1_read_implicit_Integer(struct asn1_data
*data
, int *i
)
712 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
713 if (!asn1_read_uint8(data
, &b
)) return false;
716 return !data
->has_error
;
720 /* read an integer */
721 bool asn1_read_Integer(struct asn1_data
*data
, int *i
)
725 if (!asn1_start_tag(data
, ASN1_INTEGER
)) return false;
726 if (!asn1_read_implicit_Integer(data
, i
)) return false;
727 return asn1_end_tag(data
);
730 /* read an integer */
731 bool asn1_read_enumerated(struct asn1_data
*data
, int *v
)
735 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
736 while (!data
->has_error
&& asn1_tag_remaining(data
)>0) {
738 asn1_read_uint8(data
, &b
);
741 return asn1_end_tag(data
);
744 /* check a enumerated value is correct */
745 bool asn1_check_enumerated(struct asn1_data
*data
, int v
)
748 if (!asn1_start_tag(data
, ASN1_ENUMERATED
)) return false;
749 asn1_read_uint8(data
, &b
);
753 data
->has_error
= false;
755 return !data
->has_error
;
758 /* write an enumerated value to the stream */
759 bool asn1_write_enumerated(struct asn1_data
*data
, uint8_t v
)
761 if (!asn1_push_tag(data
, ASN1_ENUMERATED
)) return false;
762 asn1_write_uint8(data
, v
);
764 return !data
->has_error
;
768 Get us the data just written without copying
770 bool asn1_blob(const struct asn1_data
*asn1
, DATA_BLOB
*blob
)
772 if (asn1
->has_error
) {
775 if (asn1
->nesting
!= NULL
) {
778 blob
->data
= asn1
->data
;
779 blob
->length
= asn1
->length
;
784 Fill in an asn1 struct without making a copy
786 void asn1_load_nocopy(struct asn1_data
*data
, uint8_t *buf
, size_t len
)
794 check if a ASN.1 blob is a full tag
796 NTSTATUS
asn1_full_tag(DATA_BLOB blob
, uint8_t tag
, size_t *packet_size
)
798 struct asn1_data
*asn1
= asn1_init(NULL
);
801 NT_STATUS_HAVE_NO_MEMORY(asn1
);
803 asn1
->data
= blob
.data
;
804 asn1
->length
= blob
.length
;
805 asn1_start_tag(asn1
, tag
);
806 if (asn1
->has_error
) {
808 return STATUS_MORE_ENTRIES
;
810 size
= asn1_tag_remaining(asn1
) + asn1
->ofs
;
814 if (size
> blob
.length
) {
815 return STATUS_MORE_ENTRIES
;