aboutsummaryrefslogtreecommitdiffstats
path: root/common/tlv-parser.c
blob: b52cc7a218d54cce88577e074f02d2371bea47be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/* tlv-parser.c - Parse BER encoded objects
 * Copyright (C) 2023, 2024 g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gpg-error.h>

#include "util.h"
#include "tlv.h"


#define TLV_MAX_DEPTH 25



/* An object to control the ASN.1 parsing.  */
struct tlv_parser_s
{
  /* The original buffer with the entire pkcs#12 object and its length.  */
  unsigned char *origbuffer;
  size_t origbufsize;

  /* The original offset for debugging.  */
  size_t origoff;

  /* Here we keep a copy of the former TLV.  This is returned by
   * tlv_parser_release.  */
  tlv_parser_t lasttlv;

  /* The current buffer we are working on and its length. */
  unsigned char *buffer;
  size_t bufsize;

  size_t crammed;      /* 0 or actual length of crammed octet strings.  */
  int in_ndef;         /* Flag indicating that we are in a NDEF. */
  int pending;         /* The last tlv_next has not yet been processed.  */

  struct tag_info ti;  /* The current tag.  */
  gpg_error_t lasterr; /* Last error from tlv function.  */
  const char *lastfunc;/* Name of last called function.  */
  int verbosity;       /* Arg from tlv_parser_new.       */

  unsigned int stacklen; /* Used size of the stack.  */
  struct {
    unsigned char *buffer;        /* Saved value of BUFFER.   */
    size_t bufsize;               /* Saved value of BUFSIZE.  */
    size_t length;                /* Length of the container (ti.length). */
    size_t crammed;               /* Saved CRAMMED value.                 */
    int    in_ndef;               /* Saved IN_NDEF flag (ti.ndef).        */
  } stack[TLV_MAX_DEPTH];
};


static size_t cram_octet_string (tlv_parser_t tlv, int testmode);



void
_tlv_parser_dump_tag (const char *text, int lno, tlv_parser_t tlv)
{
  struct tag_info *ti;

  if (!tlv || tlv->verbosity < 2)
    return;

  ti = &tlv->ti;

  log_debug ("%s:%d: %zu@%04zu class=%d tag=%lu %c len=%zu%s nhdr=%zu\n",
             text, lno, tlv->origoff, tlv_parser_offset (tlv) - ti->nhdr,
             ti->class, ti->tag, ti->is_constructed?'c':'p',
             ti->length,ti->ndef?" ndef":"", ti->nhdr);
}


void
_tlv_parser_dump_state (const char *text, const char *text2,
                        int lno, tlv_parser_t tlv)
{
  if (!tlv || tlv->verbosity < 2)
    return;

  log_debug ("p12_parse:%s%s%s:%d: %zu@%04zu lvl=%u %s\n",
             text,
             text2? "/":"", text2? text2:"",
             lno, tlv->origoff, tlv_parser_offset (tlv),
             tlv->stacklen,
             tlv->in_ndef? " in-ndef":"");
}


static void
dump_to_file (const void *s, size_t n, const char *name)
{
#if 0
  FILE *fp;
  char fname[100];
  static int fcount;

  snprintf (fname, sizeof fname, "tmp-%03d-%s", ++fcount, name);
  log_debug ("dumping %zu bytes to '%s'\n", n, fname);
  fp = fopen (fname, "wb");
  if (!fp || fwrite (s, n, 1, fp) != 1)
    exit (2);
  fclose (fp);
#else
  (void)s;
  (void)n;
  (void)name;
#endif
}


/* Parse the buffer at the address BUFFER which is of SIZE and return
 * the tag and the length part from the TLV triplet.  Update BUFFER
 * and SIZE on success.  Checks that the encoded length does not
 * exhaust the length of the provided buffer.  */
static int
parse_tag (unsigned char const **buffer, size_t *size, struct tag_info *ti)
{
  gpg_error_t err;
  int tag;

  err = parse_ber_header (buffer, size,
                          &ti->class, &tag,
                          &ti->is_constructed, &ti->ndef,
                          &ti->length, &ti->nhdr);
  if (err)
    return err;
  if (tag < 0)
    return gpg_error (GPG_ERR_EOVERFLOW);
  ti->tag = tag;

  if (ti->length > *size)
    {
      /* data larger than buffer. */
      log_debug ("%s: ti->length=%zu for a buffer of size=%zu\n",
                 __func__, ti->length, *size);
      return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
    }

  return 0;
}

/* Public version of parse_tag.  */
gpg_error_t
tlv_parse_tag (unsigned char const **buffer, size_t *size, struct tag_info *ti)
{
  return parse_tag (buffer, size, ti);
}


/* Create a new TLV object.  */
tlv_parser_t
_tlv_parser_new (const unsigned char *buffer, size_t bufsize,
                int verbosity, tlv_parser_t lasttlv, int lno)
{
  tlv_parser_t tlv;

  if (verbosity > 1)
    log_debug ("%s:%d: %zu@%zu (%p,%zu)\n", __func__, lno,
               lasttlv?lasttlv->origoff:0, tlv_parser_offset (lasttlv),
               buffer, bufsize);

  tlv = xtrycalloc (1, sizeof *tlv);
  if (tlv)
    {
      char *mybuf = xtrymalloc ( bufsize + 1);
      if (!mybuf)
        {
          xfree (tlv);
          return NULL;
        }
      memcpy (mybuf, buffer, bufsize);
      mybuf[bufsize] = 0;
      tlv->origbuffer = mybuf;
      tlv->origbufsize = bufsize;
      tlv->origoff = tlv_parser_offset (lasttlv);
      tlv->buffer = mybuf;
      tlv->bufsize = bufsize;
      tlv->crammed = 0;
      tlv->verbosity = verbosity;
      tlv->lasttlv = lasttlv;
      dump_to_file (mybuf, bufsize, "context");
    }
  return tlv;
}


/* Free the TLV object and returns the last TLV object stored in this
 * TLV.  */
tlv_parser_t
_tlv_parser_release (tlv_parser_t tlv, int lno)
{
  tlv_parser_t result;

  if (!tlv)
    return NULL;
  result = tlv->lasttlv;
  if (tlv->verbosity > 1)
    {
      if (result)
        log_debug ("%s:%d: done; returning last TLV %zu@%zu (%p,%zu)\n",
                   __func__, lno, result->origoff,
                   tlv_parser_offset (result), result->buffer, result->bufsize);
      else
        log_debug ("%s:%d: done\n", __func__, lno);
    }
  xfree (tlv->origbuffer);
  xfree (tlv);
  return result;
}


/* Helper for tlv_expect_sequence and tlv_expect_context_tag.  */
static gpg_error_t
_tlv_push (tlv_parser_t tlv)
{

  /* Right now our pointer is at the value of the current container.
   * We push that info onto the stack.  */
  if (tlv->stacklen >= TLV_MAX_DEPTH)
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_MANY));
  tlv->stack[tlv->stacklen].buffer  = tlv->buffer;
  tlv->stack[tlv->stacklen].bufsize = tlv->bufsize;
  tlv->stack[tlv->stacklen].in_ndef = tlv->in_ndef;
  tlv->stack[tlv->stacklen].length  = tlv->ti.length;
  tlv->stack[tlv->stacklen].crammed = tlv->crammed;
  tlv->stacklen++;

  tlv->in_ndef = tlv->ti.ndef;

  /* We set the size of the buffer to the TLV length if it is known or
   * else to the size of the remaining entire buffer.  */
  if (tlv->in_ndef)
    {
      if ((tlv->buffer - tlv->origbuffer) > tlv->origbufsize)
        return (tlv->lasterr = gpg_error (GPG_ERR_BUG));
      tlv->bufsize = tlv->origbufsize - (tlv->buffer - tlv->origbuffer);
    }
  else
    tlv->bufsize = tlv->ti.length;

  _tlv_parser_dump_state (__func__, NULL, 0, tlv);
  return 0;
}


/* Helper for tlv_next.  */
static gpg_error_t
_tlv_pop (tlv_parser_t tlv)
{
  size_t length;

  /* We reached the end of a container, either due to the size limit
   * or due to an end tag.  Now we pop the last container so that we
   * are positioned at the value of the last container. */
  if (!tlv->stacklen)
    return gpg_error (GPG_ERR_EOF);

  tlv->stacklen--;
  tlv->in_ndef = tlv->stack[tlv->stacklen].in_ndef;
  length = tlv->ti.length = tlv->stack[tlv->stacklen].length;
  tlv->crammed = tlv->stack[tlv->stacklen].crammed;
  if (tlv->in_ndef)
    {
      /* We keep buffer but adjust bufsize to the end of the origbuffer. */
      if ((tlv->buffer - tlv->origbuffer) > tlv->origbufsize)
        return (tlv->lasterr = gpg_error (GPG_ERR_BUG));
      tlv->bufsize = tlv->origbufsize - (tlv->buffer - tlv->origbuffer);
    }
  else
    {
      tlv->buffer  = tlv->stack[tlv->stacklen].buffer;
      tlv->bufsize = tlv->stack[tlv->stacklen].bufsize;
      if (length > tlv->bufsize)
        {
          if (tlv->verbosity > 1)
            log_debug ("%s: container larger than buffer (%zu/%zu)\n",
                       __func__, length, tlv->bufsize);
          return gpg_error (GPG_ERR_INV_BER);
        }
      tlv->buffer += length;
      tlv->bufsize -= length;

    }
  _tlv_parser_dump_state (__func__, NULL, 0, tlv);
  return 0;
}


/* Parse the next tag and value.  Also detect the end of a
 * container.  The caller should use the tlv_next macro. */
gpg_error_t
_tlv_parser_next (tlv_parser_t tlv, unsigned int flag, int lno)
{
  gpg_error_t err;
  const unsigned char *buffer;
  size_t save_bufsize;
  const unsigned char *save_buffer;
  int i;

  tlv->lasterr = 0;
  tlv->lastfunc = __func__;

  if (tlv->pending)
    {
      tlv->pending = 0;
      if (tlv->verbosity > 1)
        log_debug ("%s:%d: skipped\n", __func__, lno);
      return 0;
    }

  if (tlv->verbosity > 1)
    log_debug ("%s:%d: called (%p,%zu)\n", __func__, lno,
               tlv->buffer, tlv->bufsize);
  /* If we are at the end of an ndef container pop the stack.  */
  if (!tlv->in_ndef && !tlv->bufsize)
    {
      if (tlv->verbosity > 1)
        for (i=0; i < tlv->stacklen; i++)
          log_debug ("%s: stack[%d] (%p,@%zu,%zu) len=%zu (%zu) %s\n",
                     __func__, i,
                     tlv->stack[i].buffer,
                     tlv->stack[i].buffer - tlv->origbuffer,
                     tlv->stack[i].bufsize,
                     tlv->stack[i].length,
                     tlv->stack[i].crammed,
                     tlv->stack[i].in_ndef? " ndef":"");
      do
        err = _tlv_pop (tlv);
      while (!err && !tlv->in_ndef && !tlv->bufsize);

      if (err)
        return (tlv->lasterr = err);
      if (tlv->verbosity > 1)
        log_debug ("%s: container(s) closed due to size (lvl=%d)\n",
                   __func__, tlv->stacklen);
    }

 again:
  /* Get the next tag.  */
  save_buffer = buffer = tlv->buffer;
  save_bufsize = tlv->bufsize;
  err = parse_tag (&buffer, &tlv->bufsize, &tlv->ti);
  tlv->buffer = (unsigned char *)buffer;
  if (err)
    {
      if (tlv->verbosity > 1)
        {
          log_debug ("%s: reading tag returned err=%d\n", __func__, err);
          log_printhex (save_buffer, save_bufsize > 40? 40: save_bufsize,
                        "%s: data was\n", __func__);
          dump_to_file (tlv->origbuffer, save_buffer - tlv->origbuffer,
                        "parseerr");
        }
      return err;
    }

  if ( ( (tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_OCTET_STRING)
         || ((flag & TLV_PARSER_FLAG_T5793)
             && tlv->ti.class == CLASS_CONTEXT && tlv->ti.tag == 0))
       && tlv->ti.is_constructed && cram_octet_string (tlv, 1))
    {
      if (tlv->verbosity > 1)
        log_debug ("%s: cramming %s\n", __func__,
                   tlv->ti.tag? "constructed octet strings":"for Mozilla bug");
      if (!cram_octet_string (tlv, 0))
        return (tlv->lasterr = gpg_error (GPG_ERR_BAD_BER));
    }

  /* If there is an end tag in an ndef container pop the stack.  Also
   * pop other containers which are fully consumed. */
  if (tlv->in_ndef && (tlv->ti.class == CLASS_UNIVERSAL
                       && !tlv->ti.tag && !tlv->ti.is_constructed))
    {
      do
        err = _tlv_pop (tlv);
      while (!err && !tlv->in_ndef && !tlv->bufsize);
      if (err)
        return (tlv->lasterr = err);
      if (tlv->verbosity > 1)
        log_debug ("%s: container(s) closed due to end tag (lvl=%d)\n",
                   __func__, tlv->stacklen);
      goto again;
    }

  _tlv_parser_dump_tag (__func__, lno, tlv);
  return 0;
}


/* Return the current neting level of the TLV object.  */
unsigned int
tlv_parser_level (tlv_parser_t tlv)
{
  return tlv? tlv->stacklen : 0;
}

/* Returns the current offset of the parser.  */
size_t
tlv_parser_offset (tlv_parser_t tlv)
{
  return tlv? (size_t)(tlv->buffer - tlv->origbuffer) : 0;
}


/* Return a string with the last function used.  If TLV is NULL an
 * empty string is returned.  */
const char *
tlv_parser_lastfunc (tlv_parser_t tlv)
{
  return tlv? tlv->lastfunc:"";
}


const char *
tlv_parser_lasterrstr (tlv_parser_t tlv)
{
  return tlv? gpg_strerror (tlv->lasterr) : "tlv parser not yet initialized";
}


/* Set a flag to indicate that the last tlv_next has not yet been
 * consumed.  */
void
tlv_parser_set_pending (tlv_parser_t tlv)
{
  tlv->pending = 1;
}


/* Return the length of the last read tag.  If with_header is 1 the
 * lengtb of the header is added to the returned length.  */
size_t
tlv_parser_tag_length (tlv_parser_t tlv, int with_header)
{
  if (with_header)
    return tlv->ti.length + tlv->ti.nhdr;
  else
    return tlv->ti.length;
}


/* Skip over the value of the current tag.  Does not yet work for ndef
 * containers.  */
void
tlv_parser_skip (tlv_parser_t tlv)
{
  tlv->lastfunc = __func__;
  log_assert (tlv->bufsize >= tlv->ti.length);
  tlv->buffer += tlv->ti.length;
  tlv->bufsize -= tlv->ti.length;
}


/* Expect that the current tag is a sequence and setup the context for
 * processing.  */
gpg_error_t
tlv_expect_sequence (tlv_parser_t tlv)
{
  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_SEQUENCE
        && tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  return _tlv_push (tlv);
}


/* Expect that the current tag is a context tag and setup the context
 * for processing.  The tag of the context is returned at R_TAG.  */
gpg_error_t
tlv_expect_context_tag (tlv_parser_t tlv, int *r_tag)
{
  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_CONTEXT && tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  *r_tag = tlv->ti.tag;
  return _tlv_push (tlv);
}


/* Expect that the current tag is a SET and setup the context for
 * processing.  */
gpg_error_t
tlv_expect_set (tlv_parser_t tlv)
{
  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_SET
        && tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  return _tlv_push (tlv);
}


/* Expect an object of CLASS with TAG and store its value at
 * (R_DATA,R_DATALEN).  Then skip over its value to the next tag.
 * Note that the stored value is not allocated but points into
 * TLV.  */
gpg_error_t
tlv_expect_object (tlv_parser_t tlv, int class, int tag,
                   unsigned char const **r_data, size_t *r_datalen)
{
  const unsigned char *p;
  size_t n;
  int needpush = 0;

  tlv->lastfunc = __func__;
  /* Note that the parser has already crammed the octet strings for a
   * [0] to workaround the Mozilla bug.  */
  if (!(tlv->ti.class == class && tlv->ti.tag == tag))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  p = tlv->buffer;
  n = tlv->ti.length;
  if (!n && tlv->ti.ndef)
    {
      n = tlv->bufsize;
      needpush = 1;
    }
  else if (!tlv->ti.length)
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));

  if (tlv->verbosity > 1)
    log_debug ("%s: %zu@%zu %c len=%zu (%zu) bufsize=%zu of %zu\n",
               __func__,
               tlv->origoff, tlv_parser_offset (tlv),
               tlv->ti.is_constructed? 'c':'p',
               n, tlv->crammed,
               tlv->bufsize, tlv->origbufsize);

  if (r_data)
    *r_data = p;
  if (r_datalen)
    *r_datalen = tlv->crammed? tlv->crammed : n;

  if (needpush)
    return _tlv_push (tlv);

  if (!(tlv->bufsize >= n))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));
  tlv->buffer += n;
  tlv->bufsize -= n;
  tlv->crammed = 0;
  return 0;
}


/* Expect that the current tag is an object string and store its value
 * at (R_DATA,R_DATALEN).  Then skip over its value to the next tag.
 * Note that the stored value are not allocated but point into TLV. */
gpg_error_t
tlv_expect_octet_string (tlv_parser_t tlv,
                         unsigned char const **r_data, size_t *r_datalen)
{
  size_t n;

  tlv->lastfunc = __func__;
  /* The parser has already crammed constructed octet strings.  */
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_OCTET_STRING))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  if (!(n=tlv->ti.length) || tlv->ti.ndef )
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));

  if (tlv->verbosity > 1)
    log_debug ("%s: %zu@%zu %c len=%zu (%zu) bufsize=%zu of %zu\n",
               __func__,
               tlv->origoff, tlv_parser_offset (tlv),
               tlv->ti.is_constructed? 'c':'p',
               n, tlv->crammed,
               tlv->bufsize, tlv->origbufsize);

  if (r_data)
    *r_data = tlv->buffer;
  if (r_datalen)
    *r_datalen = tlv->crammed? tlv->crammed : tlv->ti.length;

  if (!(tlv->bufsize >= tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));
  tlv->buffer += tlv->ti.length;
  tlv->bufsize -= tlv->ti.length;
  tlv->crammed = 0;
  return 0;
}


/* Expect that the current tag is an integer and return its value at
 * R_VALUE.  Then skip over its value to the next tag. */
gpg_error_t
tlv_expect_integer (tlv_parser_t tlv, int *r_value)
{
  const unsigned char *p;
  size_t n;
  int value;

  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_INTEGER
        && !tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  p = tlv->buffer;
  if (!(n=tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));

  /* We currently support only positive values.  */
  if ((*p & 0x80))
    return (tlv->lasterr = gpg_error (GPG_ERR_ERANGE));

  for (value = 0; n; n--)
    {
      value <<= 8;
      value |= (*p++) & 0xff;
      if (value < 0)
        return (tlv->lasterr = gpg_error (GPG_ERR_EOVERFLOW));
    }
  *r_value = value;
  if (!(tlv->bufsize >= tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));
  tlv->buffer += tlv->ti.length;
  tlv->bufsize -= tlv->ti.length;
  return 0;
}


/* Variant of tlv_expect_integer which returns an MPI.  If IGNORE_ZERO
 * is set a value of 0 is ignored and R_VALUE not changed and the
 * function returns GPG_ERR_FALSE.  No check for negative encoded
 * integers is done because the old code here worked the same and we
 * can't foreclose invalid encoded PKCS#12 stuff - after all it is
 * PKCS#12 see https://www.cs.auckland.ac.nz/~pgut001/pubs/pfx.html */
#ifdef GCRYPT_VERSION
gpg_error_t
tlv_expect_mpinteger (tlv_parser_t tlv, int ignore_zero,
                      gcry_mpi_t *r_value)
{
  const unsigned char *p;
  size_t n;

  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_INTEGER
        && !tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  p = tlv->buffer;
  if (!(n=tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));

  if (!(tlv->bufsize >= tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));
  tlv->buffer += tlv->ti.length;
  tlv->bufsize -= tlv->ti.length;
  if (ignore_zero && n == 1 && !*p)
    return gpg_error (GPG_ERR_FALSE);

  return gcry_mpi_scan (r_value, GCRYMPI_FMT_USG, p, n, NULL);
}
#endif /*GCRYPT_VERSION*/


/* Expect that the current tag is an object id and store its value at
 * (R_OID,R_OIDLEN).  Then skip over its value to the next tag.  Note
 * that the stored value is not allocated but points into TLV. */
gpg_error_t
tlv_expect_object_id (tlv_parser_t tlv,
                      unsigned char const **r_oid, size_t *r_oidlen)
{
  const unsigned char *p;
  size_t n;

  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_OBJECT_ID
        && !tlv->ti.is_constructed))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  p = tlv->buffer;
  if (!(n=tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));

  *r_oid = p;
  *r_oidlen = tlv->ti.length;
  if (!(tlv->bufsize >= tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_TOO_SHORT));
  tlv->buffer += tlv->ti.length;
  tlv->bufsize -= tlv->ti.length;
  return 0;
}


/* Expect a NULL tag.  */
gpg_error_t
tlv_expect_null (tlv_parser_t tlv)
{
  tlv->lastfunc = __func__;
  if (!(tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_NULL
        && !tlv->ti.is_constructed && !tlv->ti.length))
    return (tlv->lasterr = gpg_error (GPG_ERR_INV_OBJ));
  return 0;
}


/* Given a BER encoded constructed octet string like the example below
 * from Mozilla Firefox 1.0.4 (which actually exports certs as single
 * byte chunks of octet strings) in the buffer described by TLV.
 * Although the example uses an ndef for the length of the constructed
 * octet string, a fixed length is also allowed.
 *
 *   24 NDEF:       OCTET STRING
 *   04    1:         OCTET STRING  -- TLV->buffer points to here
 *          :           30
 *   04    1:         OCTET STRING
 *          :           80
 *        [...]
 *   04    2:         OCTET STRING
 *          :           00 00
 *          :         } -- This denotes a Null tag and are the last
 *                      -- two bytes in INPUT.
 *
 * Turn it into a primitive octet string of this form:
 *
 *   24    2:       OCTET STRING
 *          :         30 80
 *
 * and fill it up with FE to the original length.  Unless TESTMODE is
 * true the TLV object including the the member and the data is
 * adjusted accordingly; however the intiial tag is not changed (in
 * the example the "24 NDEF") because this is not needed anymore.
 *
 * On error 0 is returned and in this case the buffer might have
 * already been modified and thus the caller should better stop
 * parsing - unless TESTMODE was used.  */
static size_t
cram_octet_string (tlv_parser_t tlv, int testmode)
{
  gpg_error_t err;
  size_t totallen;    /* Length of the non-crammed octet strings.  */
  size_t crammedlen;  /* Length of the crammed octet strings.  */
  const unsigned char *s, *save_s;
  unsigned char *d;
  size_t n, save_n;
  struct tag_info ti;

  if (tlv->ti.class == CLASS_UNIVERSAL && tlv->ti.tag == TAG_OCTET_STRING)
    ;  /* Okay.  */
  else if (tlv->ti.class == CLASS_CONTEXT && tlv->ti.tag == 0)
    ;  /* Workaround for Mozilla bug; see T5793  */
  else
    return 0;  /* Oops - we should not have been called.  */
  if (!tlv->ti.is_constructed)
    return 0;  /* Oops - Not a constructed octet string.  */
  if (!tlv->ti.ndef && tlv->ti.length < 4)
    return 0;  /* Fixed length but too short.  */

  /* Let S point to the first octet string chunk.  */
  s = tlv->buffer;
  n = tlv->ti.ndef? tlv->bufsize : tlv->ti.length;

  d = (unsigned char *)s;
  totallen = crammedlen = 0;
  while (n)
    {
      save_s = s;
      save_n = n;
      if ((err=parse_tag (&s, &n, &ti)))
        {
          if (tlv->verbosity > 1)
            {
              log_debug ("%s: parse_tag(n=%zu) failed : %s\n",
                         __func__, save_n, gpg_strerror (err));
              log_printhex (save_s, save_n > 40? 40:save_n, "%s: data was",
                            __func__);
            }
          return 0;
        }
      if (tlv->verbosity > 1)
        log_debug ("%s:%s ti.ndef=%d ti.tag=%lu ti.length=%zu (n %zu->%zu)\n",
                   __func__, testmode?"test:":"",
                   ti.ndef, ti.tag, ti.length, save_n, n);
      if (ti.class == CLASS_UNIVERSAL && ti.tag == TAG_OCTET_STRING
          && !ti.ndef && !ti.is_constructed)
        {
          if (!testmode)
            memmove (d, s, ti.length);
          d += ti.length;  /* Update destination */
          totallen += ti.length + ti.nhdr;
          crammedlen += ti.length;
          s += ti.length;  /* Skip to next tag.  */
          n -= ti.length;
        }
      else if (ti.class == CLASS_UNIVERSAL && !ti.tag && !ti.is_constructed)
        {
          totallen += ti.nhdr;
          break; /* EOC - Ready */
        }
      else
        return 0; /* Invalid data.  */
    }
  if (!testmode)
    {
      memset (d, '\xfe', totallen - crammedlen);
      tlv->ti.length = totallen;
      tlv->ti.is_constructed = 0;
      tlv->ti.ndef = 0;
      tlv->crammed = crammedlen;
      if (tlv->verbosity > 1)
        {
          log_debug ("%s: crammed length is %zu\n", __func__, crammedlen);
          log_debug ("%s:   total length is %zu\n", __func__, totallen);
        }
      dump_to_file (tlv->buffer, totallen, "crammed");
    }
  return totallen;
}