1
/* tlv.c - Tag-Length-Value Utilities
2
* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG 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 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28
#include <gpg-error.h>
32
static const unsigned char *
33
do_find_tlv (const unsigned char *buffer, size_t length,
34
int tag, size_t *nbytes, int nestlevel)
36
const unsigned char *s = buffer;
46
return NULL; /* Buffer definitely too short for tag and length. */
47
if (!*s || *s == 0xff)
48
{ /* Skip optional filler between TLV objects. */
53
composite = !!(*s & 0x20);
54
if ((*s & 0x1f) == 0x1f)
55
{ /* more tag bytes to follow */
59
return NULL; /* buffer definitely too short for tag and length. */
60
if ((*s & 0x1f) == 0x1f)
61
return NULL; /* We support only up to 2 bytes. */
62
this_tag = (s[-1] << 8) | (s[0] & 0x7f);
71
{ /* One byte length follows. */
73
return NULL; /* we expected 1 more bytes with the length. */
78
{ /* Two byte length follows. */
80
return NULL; /* We expected 2 more bytes with the length. */
81
len = (s[0] << 8) | s[1];
85
return NULL; /* APDU limit is 65535, thus it does not make
86
sense to assume longer length fields. */
88
if (composite && nestlevel < 100)
89
{ /* Dive into this composite DO after checking for a too deep
91
const unsigned char *tmp_s;
94
tmp_s = do_find_tlv (s, len, tag, &tmp_len, nestlevel+1);
108
return NULL; /* Buffer too short to skip to the next tag. */
114
/* Locate a TLV encoded data object in BUFFER of LENGTH and
115
return a pointer to value as well as its length in NBYTES. Return
116
NULL if it was not found. Note, that the function does not check
117
whether the value fits into the provided buffer. */
118
const unsigned char *
119
find_tlv (const unsigned char *buffer, size_t length,
120
int tag, size_t *nbytes)
122
return do_find_tlv (buffer, length, tag, nbytes, 0);
128
/* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
129
and the length part from the TLV triplet. Update BUFFER and SIZE
132
parse_ber_header (unsigned char const **buffer, size_t *size,
133
int *r_class, int *r_tag,
134
int *r_constructed, int *r_ndef,
135
size_t *r_length, size_t *r_nhdr)
139
const unsigned char *buf = *buffer;
140
size_t length = *size;
148
return gpg_error (GPG_ERR_EOF);
149
c = *buf++; length--; ++*r_nhdr;
151
*r_class = (c & 0xc0) >> 6;
152
*r_constructed = !!(c & 0x20);
162
return gpg_error (GPG_ERR_EOF);
163
c = *buf++; length--; ++*r_nhdr;
171
/* Get the length. */
173
return gpg_error (GPG_ERR_EOF);
174
c = *buf++; length--; ++*r_nhdr;
181
return gpg_error (GPG_ERR_BAD_BER);
184
unsigned long len = 0;
185
int count = c & 0x7f;
187
if (count > sizeof (len) || count > sizeof (size_t))
188
return gpg_error (GPG_ERR_BAD_BER);
190
for (; count; count--)
194
return gpg_error (GPG_ERR_EOF);
195
c = *buf++; length--; ++*r_nhdr;
201
/* Without this kludge some example certs can't be parsed. */
202
if (*r_class == CLASS_UNIVERSAL && !*r_tag)