Debian Codegraph @

src/ber-help.c

raw
1 /* ber-help.c - BER herlper functions
2 * Copyright (C) 2001, 2012 g10 Code GmbH
3 *
4 * This file is part of KSBA.
5 *
6 * KSBA is free software; you can redistribute it and/or modify
7 * it under the terms of either
8 *
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
12 *
13 * or
14 *
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
18 *
19 * or both in parallel, as here.
20 *
21 * KSBA is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
25 *
26 * You should have received a copies of the GNU General Public License
27 * and the GNU Lesser General Public License along with this program;
28 * if not, see <http://www.gnu.org/licenses/>.
29 */
30
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
36 #include "util.h"
37
38 #include "asn1-func.h" /* need some constants */
39 #include "convert.h"
40 #include "ber-help.h"
41
42 /* Fixme: The parser functions should check that primitive types don't
43 have the constructed bit set (which is not allowed). This saves us
44 some work when using these parsers */
45
46 static int
47 read_byte (ksba_reader_t reader)
48 {
49 unsigned char buf;
50 size_t nread;
51 int rc;
52
53 do
54 rc = ksba_reader_read (reader, &buf, 1, &nread);
55 while (!rc && !nread);
56 return rc? -1: buf;
57 }
58
59
60 static int
61 premature_eof (struct tag_info *ti)
62 {
63 /* Note: We do an strcmp on this string at other places. */
64 ti->err_string = "premature EOF";
65 return gpg_error (GPG_ERR_BAD_BER);
66 }
67
68
69
70 static gpg_error_t
71 eof_or_error (ksba_reader_t reader, struct tag_info *ti, int premature)
72 {
73 gpg_error_t err;
74
75 err = ksba_reader_error (reader);
76 if (err)
77 {
78 ti->err_string = "read error";
79 return err;
80 }
81 if (premature)
82 return premature_eof (ti);
83
84 return gpg_error (GPG_ERR_EOF);
85 }
86
87
88
89 /*
90 Read the tag and the length part from the TLV triplet.
91 */
92 gpg_error_t
93 _ksba_ber_read_tl (ksba_reader_t reader, struct tag_info *ti)
94 {
95 int c;
96 unsigned long tag;
97
98 ti->length = 0;
99 ti->ndef = 0;
100 ti->nhdr = 0;
101 ti->err_string = NULL;
102 ti->non_der = 0;
103
104 /* Get the tag */
105 c = read_byte (reader);
106 if (c==-1)
107 return eof_or_error (reader, ti, 0);
108
109 ti->buf[ti->nhdr++] = c;
110 ti->class = (c & 0xc0) >> 6;
111 ti->is_constructed = !!(c & 0x20);
112 tag = c & 0x1f;
113
114 if (tag == 0x1f)
115 {
116 tag = 0;
117 do
118 {
119 /* We silently ignore an overflow in the tag value. It is
120 not worth checking for it. */
121 tag <<= 7;
122 c = read_byte (reader);
123 if (c == -1)
124 return eof_or_error (reader, ti, 1);
125 if (ti->nhdr >= DIM (ti->buf))
126 {
127 ti->err_string = "tag+length header too large";
128 return gpg_error (GPG_ERR_BAD_BER);
129 }
130 ti->buf[ti->nhdr++] = c;
131 tag |= c & 0x7f;
132 }
133 while (c & 0x80);
134 }
135 ti->tag = tag;
136
137 /* Get the length */
138 c = read_byte (reader);
139 if (c == -1)
140 return eof_or_error (reader, ti, 1);
141 if (ti->nhdr >= DIM (ti->buf))
142 {
143 ti->err_string = "tag+length header too large";
144 return gpg_error (GPG_ERR_BAD_BER);
145 }
146 ti->buf[ti->nhdr++] = c;
147
148 if ( !(c & 0x80) )
149 ti->length = c;
150 else if (c == 0x80)
151 {
152 ti->ndef = 1;
153 ti->non_der = 1;
154 }
155 else if (c == 0xff)
156 {
157 ti->err_string = "forbidden length value";
158 return gpg_error (GPG_ERR_BAD_BER);
159 }
160 else
161 {
162 unsigned long len = 0;
163 int count = c & 0x7f;
164
165 if (count > sizeof (len) || count > sizeof (size_t))
166 return gpg_error (GPG_ERR_BAD_BER);
167
168 for (; count; count--)
169 {
170 len <<= 8;
171 c = read_byte (reader);
172 if (c == -1)
173 return eof_or_error (reader, ti, 1);
174 if (ti->nhdr >= DIM (ti->buf))
175 {
176 ti->err_string = "tag+length header too large";
177 return gpg_error (GPG_ERR_BAD_BER);
178 }
179 ti->buf[ti->nhdr++] = c;
180 len |= c & 0xff;
181 }
182 ti->length = len;
183 }
184
185 if ((ti->nhdr + ti->length) < ti->length)
186 {
187 ti->err_string = "header+length would overflow";
188 return gpg_error (GPG_ERR_EOVERFLOW);
189 }
190
191 /* Without this kludge some example certs can't be parsed */
192 if (ti->class == CLASS_UNIVERSAL && !ti->tag)
193 ti->length = 0;
194
195 return 0;
196 }
197
198
199 /* Parse the buffer at the address BUFFER which of SIZE and return the
200 * tag and the length part from the TLV triplet. Update BUFFER and
201 * SIZE on success. Note that this function will never return
202 * GPG_ERR_INV_OBJ so that this error code can be used by the parse_foo
203 * functions below to return an error for unexpected tags and the
204 * caller is able to backoff in that case. */
205 gpg_error_t
206 _ksba_ber_parse_tl (unsigned char const **buffer, size_t *size,
207 struct tag_info *ti)
208 {
209 int c;
210 unsigned long tag;
211 const unsigned char *buf = *buffer;
212 size_t length = *size;
213
214 ti->length = 0;
215 ti->ndef = 0;
216 ti->nhdr = 0;
217 ti->err_string = NULL;
218 ti->non_der = 0;
219
220 /* Get the tag */
221 if (!length)
222 return premature_eof (ti);
223 c = *buf++; length--;
224
225 ti->buf[ti->nhdr++] = c;
226 ti->class = (c & 0xc0) >> 6;
227 ti->is_constructed = !!(c & 0x20);
228 tag = c & 0x1f;
229
230 if (tag == 0x1f)
231 {
232 tag = 0;
233 do
234 {
235 /* We silently ignore an overflow in the tag value. It is
236 not worth checking for it. */
237 tag <<= 7;
238 if (!length)
239 return premature_eof (ti);
240 c = *buf++; length--;
241 if (ti->nhdr >= DIM (ti->buf))
242 {
243 ti->err_string = "tag+length header too large";
244 return gpg_error (GPG_ERR_BAD_BER);
245 }
246 ti->buf[ti->nhdr++] = c;
247 tag |= c & 0x7f;
248 }
249 while (c & 0x80);
250 }
251 ti->tag = tag;
252
253 /* Get the length */
254 if (!length)
255 return premature_eof (ti);
256 c = *buf++; length--;
257 if (ti->nhdr >= DIM (ti->buf))
258 {
259 ti->err_string = "tag+length header too large";
260 return gpg_error (GPG_ERR_BAD_BER);
261 }
262 ti->buf[ti->nhdr++] = c;
263
264 if ( !(c & 0x80) )
265 ti->length = c;
266 else if (c == 0x80)
267 {
268 ti->ndef = 1;
269 ti->non_der = 1;
270 }
271 else if (c == 0xff)
272 {
273 ti->err_string = "forbidden length value";
274 return gpg_error (GPG_ERR_BAD_BER);
275 }
276 else
277 {
278 unsigned long len = 0;
279 int count = c & 0x7f;
280
281 if (count > sizeof (len) || count > sizeof (size_t))
282 return gpg_error (GPG_ERR_BAD_BER);
283
284 for (; count; count--)
285 {
286 len <<= 8;
287 if (!length)
288 return premature_eof (ti);
289 c = *buf++; length--;
290 if (ti->nhdr >= DIM (ti->buf))
291 {
292 ti->err_string = "tag+length header too large";
293 return gpg_error (GPG_ERR_BAD_BER);
294 }
295 ti->buf[ti->nhdr++] = c;
296 len |= c & 0xff;
297 }
298 /* Sanity check for the length: This is done so that we can take
299 * the value for malloc plus some additional bytes without
300 * risking an overflow. */
301 if (len > (1 << 30))
302 return gpg_error (GPG_ERR_BAD_BER);
303 ti->length = len;
304 }
305
306
307 /* Without this kludge some example certs can't be parsed */
308 if (ti->class == CLASS_UNIVERSAL && !ti->tag)
309 ti->length = 0;
310
311 *buffer = buf;
312 *size = length;
313 return 0;
314 }
315
316
317 /* Write TAG of CLASS to WRITER. constructed is a flag telling
318 whether the value is a constructed one. length gives the length of
319 the value, if it is 0 undefinite length is assumed. length is
320 ignored for the NULL tag. */
321 gpg_error_t
322 _ksba_ber_write_tl (ksba_writer_t writer,
323 unsigned long tag,
324 enum tag_class class,
325 int constructed,
326 unsigned long length)
327 {
328 unsigned char buf[50];
329 int buflen = 0;
330
331 if (tag < 0x1f)
332 {
333 *buf = (class << 6) | tag;
334 if (constructed)
335 *buf |= 0x20;
336 buflen++;
337 }
338 else
339 {
340 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
341 }
342
343 if (!tag && !class)
344 buf[buflen++] = 0; /* end tag */
345 else if (tag == TYPE_NULL && !class)
346 buf[buflen++] = 0; /* NULL tag */
347 else if (!length)
348 buf[buflen++] = 0x80; /* indefinite length */
349 else if (length < 128)
350 buf[buflen++] = length;
351 else
352 {
353 int i;
354
355 /* fixme: if we know the sizeof an ulong we could support larger
356 objects - however this is pretty ridiculous */
357 i = (length <= 0xff ? 1:
358 length <= 0xffff ? 2:
359 length <= 0xffffff ? 3: 4);
360
361 buf[buflen++] = (0x80 | i);
362 if (i > 3)
363 buf[buflen++] = length >> 24;
364 if (i > 2)
365 buf[buflen++] = length >> 16;
366 if (i > 1)
367 buf[buflen++] = length >> 8;
368 buf[buflen++] = length;
369 }
370
371 return ksba_writer_write (writer, buf, buflen);
372 }
373
374 /* Encode TAG of CLASS in BUFFER. CONSTRUCTED is a flag telling
375 whether the value is a constructed one. LENGTH gives the length of
376 the value, if it is 0 undefinite length is assumed. LENGTH is
377 ignored for the NULL tag. It is assumed that the provide buffer is
378 large enough for storing the result - this is usually achieved by
379 using _ksba_ber_count_tl() in advance. Returns 0 in case of an
380 error or the length of the encoding.*/
381 size_t
382 _ksba_ber_encode_tl (unsigned char *buffer,
383 unsigned long tag,
384 enum tag_class class,
385 int constructed,
386 unsigned long length)
387 {
388 unsigned char *buf = buffer;
389
390 if (tag < 0x1f)
391 {
392 *buf = (class << 6) | tag;
393 if (constructed)
394 *buf |= 0x20;
395 buf++;
396 }
397 else
398 {
399 return 0; /*Not implemented*/
400 }
401
402 if (!tag && !class)
403 *buf++ = 0; /* end tag */
404 else if (tag == TYPE_NULL && !class)
405 *buf++ = 0; /* NULL tag */
406 else if (!length)
407 *buf++ = 0x80; /* indefinite length */
408 else if (length < 128)
409 *buf++ = length;
410 else
411 {
412 int i;
413
414 /* fixme: if we know the sizeof an ulong we could support larger
415 objetcs - however this is pretty ridiculous */
416 i = (length <= 0xff ? 1:
417 length <= 0xffff ? 2:
418 length <= 0xffffff ? 3: 4);
419
420 *buf++ = (0x80 | i);
421 if (i > 3)
422 *buf++ = length >> 24;
423 if (i > 2)
424 *buf++ = length >> 16;
425 if (i > 1)
426 *buf++ = length >> 8;
427 *buf++ = length;
428 }
429
430 return buf - buffer;
431 }
432
433
434 /* Calculate the length of the TL needed to encode a TAG of CLASS.
435 CONSTRUCTED is a flag telling whether the value is a constructed
436 one. LENGTH gives the length of the value; if it is 0 an
437 indefinite length is assumed. LENGTH is ignored for the NULL
438 tag. */
439 size_t
440 _ksba_ber_count_tl (unsigned long tag,
441 enum tag_class class,
442 int constructed,
443 unsigned long length)
444 {
445 int buflen = 0;
446
447 (void)constructed; /* Not used, but passed for uniformity of such calls. */
448
449 /* coverity[identical_branches] */
450 if (tag < 0x1f)
451 {
452 buflen++;
453 }
454 else
455 {
456 buflen++; /* assume one and let the actual write function bail out */
457 }
458
459 if (!tag && !class)
460 buflen++; /* end tag */
461 else if (tag == TYPE_NULL && !class)
462 buflen++; /* NULL tag */
463 else if (!length)
464 buflen++; /* indefinite length */
465 else if (length < 128)
466 buflen++;
467 else
468 {
469 int i;
470
471 /* fixme: if we know the sizeof an ulong we could support larger
472 objetcs - however this is pretty ridiculous */
473 i = (length <= 0xff ? 1:
474 length <= 0xffff ? 2:
475 length <= 0xffffff ? 3: 4);
476
477 buflen++;
478 if (i > 3)
479 buflen++;
480 if (i > 2)
481 buflen++;
482 if (i > 1)
483 buflen++;
484 buflen++;
485 }
486
487 return buflen;
488 }
489
490
491 gpg_error_t
492 _ksba_parse_sequence (unsigned char const **buf, size_t *len,
493 struct tag_info *ti)
494 {
495 gpg_error_t err;
496
497 err = _ksba_ber_parse_tl (buf, len, ti);
498 if (err)
499 ;
500 else if (!(ti->class == CLASS_UNIVERSAL && ti->tag == TYPE_SEQUENCE
501 && ti->is_constructed) )
502 err = gpg_error (GPG_ERR_INV_OBJ);
503 else if (ti->length > *len)
504 err = gpg_error (GPG_ERR_BAD_BER);
505 return err;
506 }
507
508
509 /* Note that this function returns GPG_ERR_FALSE if the TLV is valid
510 * but the tag does not match. The caller may thus check for this
511 * error code and compare against other tag values. */
512 gpg_error_t
513 _ksba_parse_context_tag (unsigned char const **buf, size_t *len,
514 struct tag_info *ti, int tag)
515 {
516 gpg_error_t err;
517
518 err = _ksba_ber_parse_tl (buf, len, ti);
519 if (err)
520 ;
521 else if (!(ti->class == CLASS_CONTEXT && ti->is_constructed))
522 err = gpg_error (GPG_ERR_INV_OBJ);
523 else if (ti->length > *len)
524 err = gpg_error (GPG_ERR_BAD_BER);
525 else if (ti->tag != tag)
526 err = gpg_error (GPG_ERR_FALSE);
527
528 return err;
529 }
530
531
532 gpg_error_t
533 _ksba_parse_enumerated (unsigned char const **buf, size_t *len,
534 struct tag_info *ti, size_t maxlen)
535 {
536 gpg_error_t err;
537
538 err = _ksba_ber_parse_tl (buf, len, ti);
539 if (err)
540 ;
541 else if (!(ti->class == CLASS_UNIVERSAL && ti->tag == TYPE_ENUMERATED
542 && !ti->is_constructed) )
543 err = gpg_error (GPG_ERR_INV_OBJ);
544 else if (!ti->length)
545 err = gpg_error (GPG_ERR_TOO_SHORT);
546 else if (maxlen && ti->length > maxlen)
547 err = gpg_error (GPG_ERR_TOO_LARGE);
548 else if (ti->length > *len)
549 err = gpg_error (GPG_ERR_BAD_BER);
550
551 return err;
552 }
553
554
555 gpg_error_t
556 _ksba_parse_integer (unsigned char const **buf, size_t *len,
557 struct tag_info *ti)
558 {
559 gpg_error_t err;
560
561 err = _ksba_ber_parse_tl (buf, len, ti);
562 if (err)
563 ;
564 else if (!(ti->class == CLASS_UNIVERSAL && ti->tag == TYPE_INTEGER
565 && !ti->is_constructed) )
566 err = gpg_error (GPG_ERR_INV_OBJ);
567 else if (!ti->length)
568 err = gpg_error (GPG_ERR_TOO_SHORT);
569 else if (ti->length > *len)
570 err = gpg_error (GPG_ERR_BAD_BER);
571
572 return err;
573 }
574
575
576 gpg_error_t
577 _ksba_parse_octet_string (unsigned char const **buf, size_t *len,
578 struct tag_info *ti)
579 {
580 gpg_error_t err;
581
582 err= _ksba_ber_parse_tl (buf, len, ti);
583 if (err)
584 ;
585 else if (!(ti->class == CLASS_UNIVERSAL && ti->tag == TYPE_OCTET_STRING
586 && !ti->is_constructed) )
587 err = gpg_error (GPG_ERR_INV_OBJ);
588 else if (!ti->length)
589 err = gpg_error (GPG_ERR_TOO_SHORT);
590 else if (ti->length > *len)
591 err = gpg_error (GPG_ERR_BAD_BER);
592
593 return err;
594 }
595
596
597 /* Note that R_BOOL will only be set if a value has been given. Thus
598 the caller should set it to the default value prior to calling this
599 function. Obviously no call to parse_skip is required after
600 calling this function. */
601 gpg_error_t
602 _ksba_parse_optional_boolean (unsigned char const **buf, size_t *len,
603 int *r_bool)
604 {
605 gpg_error_t err;
606 struct tag_info ti;
607
608 err = _ksba_ber_parse_tl (buf, len, &ti);
609 if (err)
610 ;
611 else if (!ti.length)
612 err = gpg_error (GPG_ERR_TOO_SHORT);
613 else if (ti.length > *len)
614 err = gpg_error (GPG_ERR_BAD_BER);
615 else if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_BOOLEAN
616 && !ti.is_constructed)
617 {
618 if (ti.length != 1)
619 err = gpg_error (GPG_ERR_BAD_BER);
620 *r_bool = !!**buf;
621 parse_skip (buf, len, &ti);
622 }
623 else
624 { /* Undo the read. */
625 *buf -= ti.nhdr;
626 *len += ti.nhdr;
627 }
628
629 return err;
630 }
631
632
633 /* Parse an optional Null tag. Ir R_SEEN is not NULL it is set to
634 * true if a NULL tag was encountered. */
635 gpg_error_t
636 _ksba_parse_optional_null (unsigned char const **buf, size_t *len,
637 int *r_seen)
638 {
639 gpg_error_t err;
640 struct tag_info ti;
641
642 if (r_seen)
643 *r_seen = 0;
644 err = _ksba_ber_parse_tl (buf, len, &ti);
645 if (err)
646 ;
647 else if (ti.length > *len)
648 err = gpg_error (GPG_ERR_BAD_BER);
649 else if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_NULL
650 && !ti.is_constructed)
651 {
652 if (ti.length)
653 err = gpg_error (GPG_ERR_BAD_BER);
654 if (r_seen)
655 *r_seen = 1;
656 parse_skip (buf, len, &ti);
657 }
658 else
659 { /* Undo the read. */
660 *buf -= ti.nhdr;
661 *len += ti.nhdr;
662 }
663
664 return err;
665 }
666
667
668
669 gpg_error_t
670 _ksba_parse_object_id_into_str (unsigned char const **buf, size_t *len,
671 char **oid)
672 {
673 struct tag_info ti;
674 gpg_error_t err;
675
676 *oid = NULL;
677 err = _ksba_ber_parse_tl (buf, len, &ti);
678 if (err)
679 ;
680 else if (!(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OBJECT_ID
681 && !ti.is_constructed) )
682 err = gpg_error (GPG_ERR_INV_OBJ);
683 else if (!ti.length)
684 err = gpg_error (GPG_ERR_TOO_SHORT);
685 else if (ti.length > *len)
686 err = gpg_error (GPG_ERR_BAD_BER);
687 else if (!(*oid = ksba_oid_to_str (*buf, ti.length)))
688 err = gpg_error_from_syserror ();
689 else
690 {
691 *buf += ti.length;
692 *len -= ti.length;
693 }
694 return err;
695 }
696
697
698 gpg_error_t
699 _ksba_parse_asntime_into_isotime (unsigned char const **buf, size_t *len,
700 ksba_isotime_t isotime)
701 {
702 struct tag_info ti;
703 gpg_error_t err;
704
705 err = _ksba_ber_parse_tl (buf, len, &ti);
706 if (err)
707 ;
708 else if ( !(ti.class == CLASS_UNIVERSAL
709 && (ti.tag == TYPE_UTC_TIME || ti.tag == TYPE_GENERALIZED_TIME)
710 && !ti.is_constructed) )
711 err = gpg_error (GPG_ERR_INV_OBJ);
712 else if (ti.length > *len)
713 err = gpg_error (GPG_ERR_INV_BER);
714 else if (!(err = _ksba_asntime_to_iso (*buf, ti.length,
715 ti.tag == TYPE_UTC_TIME, isotime)))
716 parse_skip (buf, len, &ti);
717
718 return err;
719 }