r25554: Convert last instances of BOOL, True and False to the standard types.
[gd/samba-autobuild/.git] / source4 / libcli / util / asn1.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple SPNEGO 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 "libcli/util/asn_1.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 bool ber_write_OID_String(DATA_BLOB *blob, const char *OID)
209 {
210         uint_t v, v2;
211         const char *p = (const char *)OID;
212         char *newp;
213         int i;
214
215         v = strtoul(p, &newp, 10);
216         if (newp[0] != '.') return false;
217         p = newp + 1;
218
219         v2 = strtoul(p, &newp, 10);
220         if (newp[0] != '.') return false;
221         p = newp + 1;
222
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;
226
227         blob->data[0] = 40*v + v2;
228
229         i = 1;
230         while (*p) {
231                 v = strtoul(p, &newp, 10);
232                 if (newp[0] == '.') {
233                         p = newp + 1;
234                 } else if (newp[0] == '\0') {
235                         p = newp;
236                 } else {
237                         data_blob_free(blob);
238                         return false;
239                 }
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);
245         }
246
247         blob->length = i;
248
249         return true;
250 }
251
252 /* write an object ID to a ASN1 buffer */
253 bool asn1_write_OID(struct asn1_data *data, const char *OID)
254 {
255         DATA_BLOB blob;
256
257         if (!asn1_push_tag(data, ASN1_OID)) return false;
258
259         if (!ber_write_OID_String(&blob, OID)) {
260                 data->has_error = true;
261                 return false;
262         }
263
264         if (!asn1_write(data, blob.data, blob.length)) {
265                 data->has_error = true;
266                 return false;
267         }
268         data_blob_free(&blob);
269         return asn1_pop_tag(data);
270 }
271
272 /* write an octet string */
273 bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
274 {
275         asn1_push_tag(data, ASN1_OCTET_STRING);
276         asn1_write(data, p, length);
277         asn1_pop_tag(data);
278         return !data->has_error;
279 }
280
281 /* write a LDAP string */
282 bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
283 {
284         asn1_write(data, s, strlen(s));
285         return !data->has_error;
286 }
287
288 /* write a general string */
289 bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
290 {
291         asn1_push_tag(data, ASN1_GENERAL_STRING);
292         asn1_write_LDAPString(data, s);
293         asn1_pop_tag(data);
294         return !data->has_error;
295 }
296
297 bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
298 {
299         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
300         asn1_write(data, blob->data, blob->length);
301         asn1_pop_tag(data);
302         return !data->has_error;
303 }
304
305 /* write a BOOLEAN */
306 bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
307 {
308         asn1_push_tag(data, ASN1_BOOLEAN);
309         asn1_write_uint8(data, v ? 0xFF : 0);
310         asn1_pop_tag(data);
311         return !data->has_error;
312 }
313
314 bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
315 {
316         uint8_t tmp = 0;
317         asn1_start_tag(data, ASN1_BOOLEAN);
318         asn1_read_uint8(data, &tmp);
319         if (tmp == 0xFF) {
320                 *v = true;
321         } else {
322                 *v = false;
323         }
324         asn1_end_tag(data);
325         return !data->has_error;
326 }
327
328 /* check a BOOLEAN */
329 bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
330 {
331         uint8_t b = 0;
332
333         asn1_read_uint8(data, &b);
334         if (b != ASN1_BOOLEAN) {
335                 data->has_error = true;
336                 return false;
337         }
338         asn1_read_uint8(data, &b);
339         if (b != v) {
340                 data->has_error = true;
341                 return false;
342         }
343         return !data->has_error;
344 }
345
346
347 /* load a struct asn1_data structure with a lump of data, ready to be parsed */
348 bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
349 {
350         ZERO_STRUCTP(data);
351         data->data = talloc_memdup(data, blob.data, blob.length);
352         if (!data->data) {
353                 data->has_error = true;
354                 return false;
355         }
356         data->length = blob.length;
357         return true;
358 }
359
360 /* Peek into an ASN1 buffer, not advancing the pointer */
361 bool asn1_peek(struct asn1_data *data, void *p, int len)
362 {
363         if (data->has_error)
364                 return false;
365
366         if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
367                 return false;
368
369         if (data->ofs + len > data->length) {
370                 /* we need to mark the buffer as consumed, so the caller knows
371                    this was an out of data error, and not a decode error */
372                 data->ofs = data->length;
373                 return false;
374         }
375
376         memcpy(p, data->data + data->ofs, len);
377         return true;
378 }
379
380 /* read from a ASN1 buffer, advancing the buffer pointer */
381 bool asn1_read(struct asn1_data *data, void *p, int len)
382 {
383         if (!asn1_peek(data, p, len)) {
384                 data->has_error = true;
385                 return false;
386         }
387
388         data->ofs += len;
389         return true;
390 }
391
392 /* read a uint8_t from a ASN1 buffer */
393 bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
394 {
395         return asn1_read(data, v, 1);
396 }
397
398 bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
399 {
400         return asn1_peek(data, v, 1);
401 }
402
403 bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
404 {
405         uint8_t b;
406
407         if (asn1_tag_remaining(data) <= 0) {
408                 return false;
409         }
410
411         if (!asn1_peek_uint8(data, &b))
412                 return false;
413
414         return (b == tag);
415 }
416
417 /* start reading a nested asn1 structure */
418 bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
419 {
420         uint8_t b;
421         struct nesting *nesting;
422         
423         if (!asn1_read_uint8(data, &b))
424                 return false;
425
426         if (b != tag) {
427                 data->has_error = true;
428                 return false;
429         }
430         nesting = talloc(data, struct nesting);
431         if (!nesting) {
432                 data->has_error = true;
433                 return false;
434         }
435
436         if (!asn1_read_uint8(data, &b)) {
437                 return false;
438         }
439
440         if (b & 0x80) {
441                 int n = b & 0x7f;
442                 if (!asn1_read_uint8(data, &b))
443                         return false;
444                 nesting->taglen = b;
445                 while (n > 1) {
446                         if (!asn1_read_uint8(data, &b)) 
447                                 return false;
448                         nesting->taglen = (nesting->taglen << 8) | b;
449                         n--;
450                 }
451         } else {
452                 nesting->taglen = b;
453         }
454         nesting->start = data->ofs;
455         nesting->next = data->nesting;
456         data->nesting = nesting;
457         if (asn1_tag_remaining(data) == -1) {
458                 return false;
459         }
460         return !data->has_error;
461 }
462
463 /* stop reading a tag */
464 bool asn1_end_tag(struct asn1_data *data)
465 {
466         struct nesting *nesting;
467
468         /* make sure we read it all */
469         if (asn1_tag_remaining(data) != 0) {
470                 data->has_error = true;
471                 return false;
472         }
473
474         nesting = data->nesting;
475
476         if (!nesting) {
477                 data->has_error = true;
478                 return false;
479         }
480
481         data->nesting = nesting->next;
482         talloc_free(nesting);
483         return true;
484 }
485
486 /* work out how many bytes are left in this nested tag */
487 int asn1_tag_remaining(struct asn1_data *data)
488 {
489         int remaining;
490         if (data->has_error) {
491                 return -1;
492         }
493
494         if (!data->nesting) {
495                 data->has_error = true;
496                 return -1;
497         }
498         remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
499         if (remaining > (data->length - data->ofs)) {
500                 data->has_error = true;
501                 return -1;
502         }
503         return remaining;
504 }
505
506 /* read an object ID from a data blob */
507 bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
508 {
509         int i;
510         uint8_t *b;
511         uint_t v;
512         char *tmp_oid = NULL;
513
514         if (blob.length < 2) return false;
515
516         b = blob.data;
517
518         tmp_oid = talloc_asprintf(mem_ctx, "%u",  b[0]/40);
519         if (!tmp_oid) goto nomem;
520         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  b[0]%40);
521         if (!tmp_oid) goto nomem;
522
523         for(i = 1, v = 0; i < blob.length; i++) {
524                 v = (v<<7) | (b[i]&0x7f);
525                 if ( ! (b[i] & 0x80)) {
526                         tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  v);
527                         v = 0;
528                 }
529                 if (!tmp_oid) goto nomem;
530         }
531
532         if (v != 0) {
533                 talloc_free(tmp_oid);
534                 return false;
535         }
536
537         *OID = tmp_oid;
538         return true;
539
540 nomem:  
541         return false;
542 }
543
544 /* read an object ID from a ASN1 buffer */
545 bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID)
546 {
547         DATA_BLOB blob;
548         int len;
549
550         if (!asn1_start_tag(data, ASN1_OID)) return false;
551
552         len = asn1_tag_remaining(data);
553         if (len < 0) {
554                 data->has_error = true;
555                 return false;
556         }
557
558         blob = data_blob(NULL, len);
559         if (!blob.data) {
560                 data->has_error = true;
561                 return false;
562         }
563
564         asn1_read(data, blob.data, len);
565         asn1_end_tag(data);
566         if (data->has_error) {
567                 data_blob_free(&blob);
568                 return false;
569         }
570
571         if (!ber_read_OID_String(mem_ctx, blob, OID)) {
572                 data->has_error = true;
573                 data_blob_free(&blob);
574                 return false;
575         }
576
577         data_blob_free(&blob);
578         return true;
579 }
580
581 /* check that the next object ID is correct */
582 bool asn1_check_OID(struct asn1_data *data, const char *OID)
583 {
584         const char *id;
585
586         if (!asn1_read_OID(data, data, &id)) return false;
587
588         if (strcmp(id, OID) != 0) {
589                 talloc_free(discard_const(id));
590                 data->has_error = true;
591                 return false;
592         }
593         talloc_free(discard_const(id));
594         return true;
595 }
596
597 /* read a LDAPString from a ASN1 buffer */
598 bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
599 {
600         int len;
601         len = asn1_tag_remaining(data);
602         if (len < 0) {
603                 data->has_error = true;
604                 return false;
605         }
606         *s = talloc_array(mem_ctx, char, len+1);
607         if (! *s) {
608                 data->has_error = true;
609                 return false;
610         }
611         asn1_read(data, *s, len);
612         (*s)[len] = 0;
613         return !data->has_error;
614 }
615
616
617 /* read a GeneralString from a ASN1 buffer */
618 bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
619 {
620         if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
621         if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
622         return asn1_end_tag(data);
623 }
624
625
626 /* read a octet string blob */
627 bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
628 {
629         int len;
630         ZERO_STRUCTP(blob);
631         if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
632         len = asn1_tag_remaining(data);
633         if (len < 0) {
634                 data->has_error = true;
635                 return false;
636         }
637         *blob = data_blob_talloc(mem_ctx, NULL, len+1);
638         if (!blob->data) {
639                 data->has_error = true;
640                 return false;
641         }
642         asn1_read(data, blob->data, len);
643         asn1_end_tag(data);
644         blob->length--;
645         blob->data[len] = 0;
646         
647         if (data->has_error) {
648                 data_blob_free(blob);
649                 *blob = data_blob(NULL, 0);
650                 return false;
651         }
652         return true;
653 }
654
655 bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
656 {
657         int len;
658         ZERO_STRUCTP(blob);
659         if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
660         len = asn1_tag_remaining(data);
661         if (len < 0) {
662                 data->has_error = true;
663                 return false;
664         }
665         *blob = data_blob(NULL, len);
666         if ((len != 0) && (!blob->data)) {
667                 data->has_error = true;
668                 return false;
669         }
670         asn1_read(data, blob->data, len);
671         asn1_end_tag(data);
672         return !data->has_error;
673 }
674
675 /* read an interger without tag*/
676 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
677 {
678         uint8_t b;
679         *i = 0;
680
681         while (!data->has_error && asn1_tag_remaining(data)>0) {
682                 if (!asn1_read_uint8(data, &b)) return false;
683                 *i = (*i << 8) + b;
684         }
685         return !data->has_error;        
686         
687 }
688
689 /* read an interger */
690 bool asn1_read_Integer(struct asn1_data *data, int *i)
691 {
692         *i = 0;
693
694         if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
695         if (!asn1_read_implicit_Integer(data, i)) return false;
696         return asn1_end_tag(data);      
697 }
698
699 /* read an interger */
700 bool asn1_read_enumerated(struct asn1_data *data, int *v)
701 {
702         *v = 0;
703         
704         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
705         while (!data->has_error && asn1_tag_remaining(data)>0) {
706                 uint8_t b;
707                 asn1_read_uint8(data, &b);
708                 *v = (*v << 8) + b;
709         }
710         return asn1_end_tag(data);      
711 }
712
713 /* check a enumarted value is correct */
714 bool asn1_check_enumerated(struct asn1_data *data, int v)
715 {
716         uint8_t b;
717         if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
718         asn1_read_uint8(data, &b);
719         asn1_end_tag(data);
720
721         if (v != b)
722                 data->has_error = false;
723
724         return !data->has_error;
725 }
726
727 /* write an enumarted value to the stream */
728 bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
729 {
730         if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
731         asn1_write_uint8(data, v);
732         asn1_pop_tag(data);
733         return !data->has_error;
734 }
735
736 /*
737   check if a ASN.1 blob is a full tag
738 */
739 NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
740 {
741         struct asn1_data *asn1 = asn1_init(NULL);
742         int size;
743
744         NT_STATUS_HAVE_NO_MEMORY(asn1);
745
746         asn1->data = blob.data;
747         asn1->length = blob.length;
748         asn1_start_tag(asn1, tag);
749         if (asn1->has_error) {
750                 talloc_free(asn1);
751                 return STATUS_MORE_ENTRIES;
752         }
753         size = asn1_tag_remaining(asn1) + asn1->ofs;
754
755         talloc_free(asn1);
756
757         if (size > blob.length) {
758                 return STATUS_MORE_ENTRIES;
759         }               
760
761         *packet_size = size;
762         return NT_STATUS_OK;
763 }