s4/drsuapi: Internal implementation for ber_read_OID_String
[samba.git] / lib / util / asn1.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple ASN1 routines
4    Copyright (C) Andrew Tridgell 2001
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/util/asn1.h"
22
23 /* allocate an asn1 structure */
24 struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx)
25 {
26         struct asn1_data *ret = talloc_zero(mem_ctx, struct asn1_data);
27         if (ret == NULL) {
28                 DEBUG(0,("asn1_init failed! out of memory\n"));
29         }
30         return ret;
31 }
32
33 /* free an asn1 structure */
34 void asn1_free(struct asn1_data *data)
35 {
36         talloc_free(data);
37 }
38
39 /* write to the ASN1 buffer, advancing the buffer pointer */
40 bool asn1_write(struct asn1_data *data, const void *p, int len)
41 {
42         if (data->has_error) return false;
43         if (data->length < data->ofs+len) {
44                 uint8_t *newp;
45                 newp = talloc_realloc(data, data->data, uint8_t, data->ofs+len);
46                 if (!newp) {
47                         asn1_free(data);
48                         data->has_error = true;
49                         return false;
50                 }
51                 data->data = newp;
52                 data->length = data->ofs+len;
53         }
54         memcpy(data->data + data->ofs, p, len);
55         data->ofs += len;
56         return true;
57 }
58
59 /* useful fn for writing a uint8_t */
60 bool asn1_write_uint8(struct asn1_data *data, uint8_t v)
61 {
62         return asn1_write(data, &v, 1);
63 }
64
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)
67 {
68         struct nesting *nesting;
69
70         asn1_write_uint8(data, tag);
71         nesting = talloc(data, struct nesting);
72         if (!nesting) {
73                 data->has_error = true;
74                 return false;
75         }
76
77         nesting->start = data->ofs;
78         nesting->next = data->nesting;
79         data->nesting = nesting;
80         return asn1_write_uint8(data, 0xff);
81 }
82
83 /* pop a tag */
84 bool asn1_pop_tag(struct asn1_data *data)
85 {
86         struct nesting *nesting;
87         size_t len;
88
89         nesting = data->nesting;
90
91         if (!nesting) {
92                 data->has_error = true;
93                 return false;
94         }
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 */
99         if (len > 0xFFFFFF) {
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;
131         } else {
132                 data->data[nesting->start] = len;
133         }
134
135         data->nesting = nesting->next;
136         talloc_free(nesting);
137         return true;
138 }
139
140 /* "i" is the one's complement representation, as is the normal result of an
141  * implicit signed->unsigned conversion */
142
143 static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
144 {
145         uint8_t lowest = i & 0xFF;
146
147         i = i >> 8;
148         if (i != 0)
149                 if (!push_int_bigendian(data, i, negative))
150                         return false;
151
152         if (data->nesting->start+1 == data->ofs) {
153
154                 /* We did not write anything yet, looking at the highest
155                  * valued byte */
156
157                 if (negative) {
158                         /* Don't write leading 0xff's */
159                         if (lowest == 0xFF)
160                                 return true;
161
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))
167                                         return false;
168                         }
169                 } else {
170                         if (lowest & 0x80) {
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))
175                                         return false;
176                         }
177                 }
178         }
179
180         return asn1_write_uint8(data, lowest);
181 }
182
183 /* write an Integer without the tag framing. Needed for example for the LDAP
184  * Abandon Operation */
185
186 bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
187 {
188         if (i == -1) {
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);
194         } else {
195                 return push_int_bigendian(data, i, i<0);
196         }
197 }
198
199
200 /* write an integer */
201 bool asn1_write_Integer(struct asn1_data *data, int i)
202 {
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);
206 }
207
208 /* write a BIT STRING */
209 bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
210 {
211         if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
212         if (!asn1_write_uint8(data, padding)) return false;
213         if (!asn1_write(data, p, length)) return false;
214         return asn1_pop_tag(data);
215 }
216
217 bool ber_write_OID_String(DATA_BLOB *blob, const char *OID)
218 {
219         uint_t v, v2;
220         const char *p = (const char *)OID;
221         char *newp;
222         int i;
223
224         v = strtoul(p, &newp, 10);
225         if (newp[0] != '.') return false;
226         p = newp + 1;
227
228         v2 = strtoul(p, &newp, 10);
229         if (newp[0] != '.') return false;
230         p = newp + 1;
231
232         /*the ber representation can't use more space then the string one */
233         *blob = data_blob(NULL, strlen(OID));
234         if (!blob->data) return false;
235
236         blob->data[0] = 40*v + v2;
237
238         i = 1;
239         while (*p) {
240                 v = strtoul(p, &newp, 10);
241                 if (newp[0] == '.') {
242                         p = newp + 1;
243                 } else if (newp[0] == '\0') {
244                         p = newp;
245                 } else {
246                         data_blob_free(blob);
247                         return false;
248                 }
249                 if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
250                 if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
251                 if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
252                 if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
253                 blob->data[i++] = (v&0x7f);
254         }
255
256         blob->length = i;
257
258         return true;
259 }
260
261 /**
262  * Serialize partial OID string.
263  * Partial OIDs are in the form:
264  *   1:2.5.6:0x81
265  *   1:2.5.6:0x8182
266  */
267 bool ber_write_partial_OID_String(DATA_BLOB *blob, const char *partial_oid)
268 {
269         TALLOC_CTX *mem_ctx = talloc_new(NULL);
270         char *oid = talloc_strdup(mem_ctx, partial_oid);
271         char *p;
272
273         /* truncate partial part so ber_write_OID_String() works */
274         p = strchr(oid, ':');
275         if (p) {
276                 *p = '\0';
277                 p++;
278         }
279
280         if (!ber_write_OID_String(blob, oid)) {
281                 talloc_free(mem_ctx);
282                 return false;
283         }
284
285         /* Add partially endcoded subidentifier */
286         if (p) {
287                 DATA_BLOB tmp_blob = strhex_to_data_blob(mem_ctx, p);
288                 data_blob_append(NULL, blob, tmp_blob.data, tmp_blob.length);
289         }
290
291         talloc_free(mem_ctx);
292
293         return true;
294 }
295
296 /* write an object ID to a ASN1 buffer */
297 bool asn1_write_OID(struct asn1_data *data, const char *OID)
298 {
299         DATA_BLOB blob;
300
301         if (!asn1_push_tag(data, ASN1_OID)) return false;
302
303         if (!ber_write_OID_String(&blob, OID)) {
304                 data->has_error = true;
305                 return false;
306         }
307
308         if (!asn1_write(data, blob.data, blob.length)) {
309                 data_blob_free(&blob);
310                 data->has_error = true;
311                 return false;
312         }
313         data_blob_free(&blob);
314         return asn1_pop_tag(data);
315 }
316
317 /* write an octet string */
318 bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
319 {
320         asn1_push_tag(data, ASN1_OCTET_STRING);
321         asn1_write(data, p, length);
322         asn1_pop_tag(data);
323         return !data->has_error;
324 }
325
326 /* write a LDAP string */
327 bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
328 {
329         asn1_write(data, s, strlen(s));
330         return !data->has_error;
331 }
332
333 /* write a LDAP string from a DATA_BLOB */
334 bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
335 {
336         asn1_write(data, s->data, s->length);
337         return !data->has_error;
338 }
339
340 /* write a general string */
341 bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
342 {
343         asn1_push_tag(data, ASN1_GENERAL_STRING);
344         asn1_write_LDAPString(data, s);
345         asn1_pop_tag(data);
346         return !data->has_error;
347 }
348
349 bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
350 {
351         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
352         asn1_write(data, blob->data, blob->length);
353         asn1_pop_tag(data);
354         return !data->has_error;
355 }
356
357 /* write a BOOLEAN */
358 bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
359 {
360         asn1_push_tag(data, ASN1_BOOLEAN);
361         asn1_write_uint8(data, v ? 0xFF : 0);
362         asn1_pop_tag(data);
363         return !data->has_error;
364 }
365
366 bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
367 {
368         uint8_t tmp = 0;
369         asn1_start_tag(data, ASN1_BOOLEAN);
370         asn1_read_uint8(data, &tmp);
371         if (tmp == 0xFF) {
372                 *v = true;
373         } else {
374                 *v = false;
375         }
376         asn1_end_tag(data);
377         return !data->has_error;
378 }
379
380 /* write a BOOLEAN in a simple context */
381 bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
382 {
383         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context));
384         asn1_write_uint8(data, v ? 0xFF : 0);
385         asn1_pop_tag(data);
386         return !data->has_error;
387 }
388
389 bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
390 {
391         uint8_t tmp = 0;
392         asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context));
393         asn1_read_uint8(data, &tmp);
394         if (tmp == 0xFF) {
395                 *v = true;
396         } else {
397                 *v = false;
398         }
399         asn1_end_tag(data);
400         return !data->has_error;
401 }
402
403 /* check a BOOLEAN */
404 bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
405 {
406         uint8_t b = 0;
407
408         asn1_read_uint8(data, &b);
409         if (b != ASN1_BOOLEAN) {
410                 data->has_error = true;
411                 return false;
412         }
413         asn1_read_uint8(data, &b);
414         if (b != v) {
415                 data->has_error = true;
416                 return false;
417         }
418         return !data->has_error;
419 }
420
421
422 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
423 bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
424 {
425         ZERO_STRUCTP(data);
426         data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
427         if (!data->data) {
428                 data->has_error = true;
429                 return false;
430         }
431         data->length = blob.length;
432         return true;
433 }
434
435 /* Peek into an ASN1 buffer, not advancing the pointer */
436 bool asn1_peek(struct asn1_data *data, void *p, int len)
437 {
438         if (data->has_error)
439                 return false;
440
441         if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
442                 return false;
443
444         if (data->ofs + len > data->length) {
445                 /* we need to mark the buffer as consumed, so the caller knows
446                    this was an out of data error, and not a decode error */
447                 data->ofs = data->length;
448                 return false;
449         }
450
451         memcpy(p, data->data + data->ofs, len);
452         return true;
453 }
454
455 /* read from a ASN1 buffer, advancing the buffer pointer */
456 bool asn1_read(struct asn1_data *data, void *p, int len)
457 {
458         if (!asn1_peek(data, p, len)) {
459                 data->has_error = true;
460                 return false;
461         }
462
463         data->ofs += len;
464         return true;
465 }
466
467 /* read a uint8_t from a ASN1 buffer */
468 bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
469 {
470         return asn1_read(data, v, 1);
471 }
472
473 bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
474 {
475         return asn1_peek(data, v, 1);
476 }
477
478 bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
479 {
480         uint8_t b;
481
482         if (asn1_tag_remaining(data) <= 0) {
483                 return false;
484         }
485
486         if (!asn1_peek_uint8(data, &b))
487                 return false;
488
489         return (b == tag);
490 }
491
492 /* start reading a nested asn1 structure */
493 bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
494 {
495         uint8_t b;
496         struct nesting *nesting;
497         
498         if (!asn1_read_uint8(data, &b))
499                 return false;
500
501         if (b != tag) {
502                 data->has_error = true;
503                 return false;
504         }
505         nesting = talloc(data, struct nesting);
506         if (!nesting) {
507                 data->has_error = true;
508                 return false;
509         }
510
511         if (!asn1_read_uint8(data, &b)) {
512                 return false;
513         }
514
515         if (b & 0x80) {
516                 int n = b & 0x7f;
517                 if (!asn1_read_uint8(data, &b))
518                         return false;
519                 nesting->taglen = b;
520                 while (n > 1) {
521                         if (!asn1_read_uint8(data, &b)) 
522                                 return false;
523                         nesting->taglen = (nesting->taglen << 8) | b;
524                         n--;
525                 }
526         } else {
527                 nesting->taglen = b;
528         }
529         nesting->start = data->ofs;
530         nesting->next = data->nesting;
531         data->nesting = nesting;
532         if (asn1_tag_remaining(data) == -1) {
533                 return false;
534         }
535         return !data->has_error;
536 }
537
538 /* stop reading a tag */
539 bool asn1_end_tag(struct asn1_data *data)
540 {
541         struct nesting *nesting;
542
543         /* make sure we read it all */
544         if (asn1_tag_remaining(data) != 0) {
545                 data->has_error = true;
546                 return false;
547         }
548
549         nesting = data->nesting;
550
551         if (!nesting) {
552                 data->has_error = true;
553                 return false;
554         }
555
556         data->nesting = nesting->next;
557         talloc_free(nesting);
558         return true;
559 }
560
561 /* work out how many bytes are left in this nested tag */
562 int asn1_tag_remaining(struct asn1_data *data)
563 {
564         int remaining;
565         if (data->has_error) {
566                 return -1;
567         }
568
569         if (!data->nesting) {
570                 data->has_error = true;
571                 return -1;
572         }
573         remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
574         if (remaining > (data->length - data->ofs)) {
575                 data->has_error = true;
576                 return -1;
577         }
578         return remaining;
579 }
580
581 /**
582  * Internal implementation for reading binary OIDs
583  * Reading is done as far in the buffer as valid OID
584  * till buffer ends or not valid sub-identifier is found.
585  */
586 static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
587                                         const char **OID, size_t *bytes_eaten)
588 {
589         int i;
590         uint8_t *b;
591         uint_t v;
592         char *tmp_oid = NULL;
593
594         if (blob.length < 2) return false;
595
596         b = blob.data;
597
598         tmp_oid = talloc_asprintf(mem_ctx, "%u",  b[0]/40);
599         if (!tmp_oid) goto nomem;
600         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  b[0]%40);
601         if (!tmp_oid) goto nomem;
602
603         for(i = 1, v = 0; i < blob.length; i++) {
604                 v = (v<<7) | (b[i]&0x7f);
605                 if ( ! (b[i] & 0x80)) {
606                         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  v);
607                         v = 0;
608                         if (bytes_eaten)
609                                 *bytes_eaten = i+1;
610                 }
611                 if (!tmp_oid) goto nomem;
612         }
613
614         *OID = tmp_oid;
615         return true;
616
617 nomem:
618         return false;
619 }
620
621 /* read an object ID from a data blob */
622 bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
623 {
624         int i;
625         uint8_t *b;
626         uint_t v;
627         char *tmp_oid = NULL;
628
629         if (blob.length < 2) return false;
630
631         b = blob.data;
632
633         tmp_oid = talloc_asprintf(mem_ctx, "%u",  b[0]/40);
634         if (!tmp_oid) goto nomem;
635         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  b[0]%40);
636         if (!tmp_oid) goto nomem;
637
638         for(i = 1, v = 0; i < blob.length; i++) {
639                 v = (v<<7) | (b[i]&0x7f);
640                 if ( ! (b[i] & 0x80)) {
641                         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  v);
642                         v = 0;
643                 }
644                 if (!tmp_oid) goto nomem;
645         }
646
647         if (v != 0) {
648                 talloc_free(tmp_oid);
649                 return false;
650         }
651
652         *OID = tmp_oid;
653         return true;
654
655 nomem:  
656         return false;
657 }
658
659 /* read an object ID from a ASN1 buffer */
660 bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID)
661 {
662         DATA_BLOB blob;
663         int len;
664
665         if (!asn1_start_tag(data, ASN1_OID)) return false;
666
667         len = asn1_tag_remaining(data);
668         if (len < 0) {
669                 data->has_error = true;
670                 return false;
671         }
672
673         blob = data_blob(NULL, len);
674         if (!blob.data) {
675                 data->has_error = true;
676                 return false;
677         }
678
679         asn1_read(data, blob.data, len);
680         asn1_end_tag(data);
681         if (data->has_error) {
682                 data_blob_free(&blob);
683                 return false;
684         }
685
686         if (!ber_read_OID_String(mem_ctx, blob, OID)) {
687                 data->has_error = true;
688                 data_blob_free(&blob);
689                 return false;
690         }
691
692         data_blob_free(&blob);
693         return true;
694 }
695
696 /* check that the next object ID is correct */
697 bool asn1_check_OID(struct asn1_data *data, const char *OID)
698 {
699         const char *id;
700
701         if (!asn1_read_OID(data, data, &id)) return false;
702
703         if (strcmp(id, OID) != 0) {
704                 talloc_free(discard_const(id));
705                 data->has_error = true;
706                 return false;
707         }
708         talloc_free(discard_const(id));
709         return true;
710 }
711
712 /* read a LDAPString from a ASN1 buffer */
713 bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
714 {
715         int len;
716         len = asn1_tag_remaining(data);
717         if (len < 0) {
718                 data->has_error = true;
719                 return false;
720         }
721         *s = talloc_array(mem_ctx, char, len+1);
722         if (! *s) {
723                 data->has_error = true;
724                 return false;
725         }
726         asn1_read(data, *s, len);
727         (*s)[len] = 0;
728         return !data->has_error;
729 }
730
731
732 /* read a GeneralString from a ASN1 buffer */
733 bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
734 {
735         if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
736         if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
737         return asn1_end_tag(data);
738 }
739
740
741 /* read a octet string blob */
742 bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
743 {
744         int len;
745         ZERO_STRUCTP(blob);
746         if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
747         len = asn1_tag_remaining(data);
748         if (len < 0) {
749                 data->has_error = true;
750                 return false;
751         }
752         *blob = data_blob_talloc(mem_ctx, NULL, len+1);
753         if (!blob->data) {
754                 data->has_error = true;
755                 return false;
756         }
757         asn1_read(data, blob->data, len);
758         asn1_end_tag(data);
759         blob->length--;
760         blob->data[len] = 0;
761         
762         if (data->has_error) {
763                 data_blob_free(blob);
764                 *blob = data_blob_null;
765                 return false;
766         }
767         return true;
768 }
769
770 bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
771 {
772         int len;
773         ZERO_STRUCTP(blob);
774         if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
775         len = asn1_tag_remaining(data);
776         if (len < 0) {
777                 data->has_error = true;
778                 return false;
779         }
780         *blob = data_blob(NULL, len);
781         if ((len != 0) && (!blob->data)) {
782                 data->has_error = true;
783                 return false;
784         }
785         asn1_read(data, blob->data, len);
786         asn1_end_tag(data);
787         return !data->has_error;
788 }
789
790 /* read an integer without tag*/
791 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
792 {
793         uint8_t b;
794         *i = 0;
795
796         while (!data->has_error && asn1_tag_remaining(data)>0) {
797                 if (!asn1_read_uint8(data, &b)) return false;
798                 *i = (*i << 8) + b;
799         }
800         return !data->has_error;        
801         
802 }
803
804 /* read an integer */
805 bool asn1_read_Integer(struct asn1_data *data, int *i)
806 {
807         *i = 0;
808
809         if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
810         if (!asn1_read_implicit_Integer(data, i)) return false;
811         return asn1_end_tag(data);      
812 }
813
814 /* read a BIT STRING */
815 bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
816 {
817         int len;
818         ZERO_STRUCTP(blob);
819         if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
820         len = asn1_tag_remaining(data);
821         if (len < 0) {
822                 data->has_error = true;
823                 return false;
824         }
825         if (!asn1_read_uint8(data, padding)) return false;
826
827         *blob = data_blob_talloc(mem_ctx, NULL, len);
828         if (!blob->data) {
829                 data->has_error = true;
830                 return false;
831         }
832         if (asn1_read(data, blob->data, len - 1)) {
833                 blob->length--;
834                 blob->data[len] = 0;
835                 asn1_end_tag(data);
836         }
837
838         if (data->has_error) {
839                 data_blob_free(blob);
840                 *blob = data_blob_null;
841                 *padding = 0;
842                 return false;
843         }
844         return true;
845 }
846
847 /* read an integer */
848 bool asn1_read_enumerated(struct asn1_data *data, int *v)
849 {
850         *v = 0;
851         
852         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
853         while (!data->has_error && asn1_tag_remaining(data)>0) {
854                 uint8_t b;
855                 asn1_read_uint8(data, &b);
856                 *v = (*v << 8) + b;
857         }
858         return asn1_end_tag(data);      
859 }
860
861 /* check a enumerated value is correct */
862 bool asn1_check_enumerated(struct asn1_data *data, int v)
863 {
864         uint8_t b;
865         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
866         asn1_read_uint8(data, &b);
867         asn1_end_tag(data);
868
869         if (v != b)
870                 data->has_error = false;
871
872         return !data->has_error;
873 }
874
875 /* write an enumerated value to the stream */
876 bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
877 {
878         if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
879         asn1_write_uint8(data, v);
880         asn1_pop_tag(data);
881         return !data->has_error;
882 }
883
884 /*
885   Get us the data just written without copying
886 */
887 bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
888 {
889         if (asn1->has_error) {
890                 return false;
891         }
892         if (asn1->nesting != NULL) {
893                 return false;
894         }
895         blob->data = asn1->data;
896         blob->length = asn1->length;
897         return true;
898 }
899
900 /*
901   Fill in an asn1 struct without making a copy
902 */
903 void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
904 {
905         ZERO_STRUCTP(data);
906         data->data = buf;
907         data->length = len;
908 }
909
910 /*
911   check if a ASN.1 blob is a full tag
912 */
913 NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
914 {
915         struct asn1_data *asn1 = asn1_init(NULL);
916         int size;
917
918         NT_STATUS_HAVE_NO_MEMORY(asn1);
919
920         asn1->data = blob.data;
921         asn1->length = blob.length;
922         asn1_start_tag(asn1, tag);
923         if (asn1->has_error) {
924                 talloc_free(asn1);
925                 return STATUS_MORE_ENTRIES;
926         }
927         size = asn1_tag_remaining(asn1) + asn1->ofs;
928
929         talloc_free(asn1);
930
931         if (size > blob.length) {
932                 return STATUS_MORE_ENTRIES;
933         }               
934
935         *packet_size = size;
936         return NT_STATUS_OK;
937 }