1
/* tlv.c - Tag-Length-Value Utilities
2
* Copyright (C) 2003, 2004, 2005 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
#if GNUPG_MAJOR_VERSION == 1
29
#define GPG_ERR_EOF (-1)
30
#define GPG_ERR_BAD_BER (1) /*G10ERR_GENERAL*/
31
#define GPG_ERR_INV_SEXP (45) /*G10ERR_INV_ARG*/
32
typedef int gpg_error_t;
33
#define gpg_error(n) (n)
35
#include <gpg-error.h>
40
static const unsigned char *
41
do_find_tlv (const unsigned char *buffer, size_t length,
42
int tag, size_t *nbytes, int nestlevel)
44
const unsigned char *s = buffer;
54
return NULL; /* Buffer definitely too short for tag and length. */
55
if (!*s || *s == 0xff)
56
{ /* Skip optional filler between TLV objects. */
61
composite = !!(*s & 0x20);
62
if ((*s & 0x1f) == 0x1f)
63
{ /* more tag bytes to follow */
67
return NULL; /* buffer definitely too short for tag and length. */
68
if ((*s & 0x1f) == 0x1f)
69
return NULL; /* We support only up to 2 bytes. */
70
this_tag = (s[-1] << 8) | (s[0] & 0x7f);
79
{ /* One byte length follows. */
81
return NULL; /* we expected 1 more bytes with the length. */
86
{ /* Two byte length follows. */
88
return NULL; /* We expected 2 more bytes with the length. */
89
len = (s[0] << 8) | s[1];
93
return NULL; /* APDU limit is 65535, thus it does not make
94
sense to assume longer length fields. */
96
if (composite && nestlevel < 100)
97
{ /* Dive into this composite DO after checking for a too deep
99
const unsigned char *tmp_s;
102
tmp_s = do_find_tlv (s, len, tag, &tmp_len, nestlevel+1);
116
return NULL; /* Buffer too short to skip to the next tag. */
122
/* Locate a TLV encoded data object in BUFFER of LENGTH and
123
return a pointer to value as well as its length in NBYTES. Return
124
NULL if it was not found or if the object does not fit into the buffer. */
125
const unsigned char *
126
find_tlv (const unsigned char *buffer, size_t length,
127
int tag, size_t *nbytes)
129
const unsigned char *p;
131
p = do_find_tlv (buffer, length, tag, nbytes, 0);
132
if (p && *nbytes > (length - (p-buffer)))
133
p = NULL; /* Object longer than buffer. */
139
/* Locate a TLV encoded data object in BUFFER of LENGTH and
140
return a pointer to value as well as its length in NBYTES. Return
141
NULL if it was not found. Note, that the function does not check
142
whether the value fits into the provided buffer. */
143
const unsigned char *
144
find_tlv_unchecked (const unsigned char *buffer, size_t length,
145
int tag, size_t *nbytes)
147
return do_find_tlv (buffer, length, tag, nbytes, 0);
151
/* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
152
and the length part from the TLV triplet. Update BUFFER and SIZE
155
parse_ber_header (unsigned char const **buffer, size_t *size,
156
int *r_class, int *r_tag,
157
int *r_constructed, int *r_ndef,
158
size_t *r_length, size_t *r_nhdr)
162
const unsigned char *buf = *buffer;
163
size_t length = *size;
171
return gpg_error (GPG_ERR_EOF);
172
c = *buf++; length--; ++*r_nhdr;
174
*r_class = (c & 0xc0) >> 6;
175
*r_constructed = !!(c & 0x20);
185
return gpg_error (GPG_ERR_EOF);
186
c = *buf++; length--; ++*r_nhdr;
194
/* Get the length. */
196
return gpg_error (GPG_ERR_EOF);
197
c = *buf++; length--; ++*r_nhdr;
204
return gpg_error (GPG_ERR_BAD_BER);
207
unsigned long len = 0;
208
int count = c & 0x7f;
210
if (count > sizeof (len) || count > sizeof (size_t))
211
return gpg_error (GPG_ERR_BAD_BER);
213
for (; count; count--)
217
return gpg_error (GPG_ERR_EOF);
218
c = *buf++; length--; ++*r_nhdr;
224
/* Without this kludge some example certs can't be parsed. */
225
if (*r_class == CLASS_UNIVERSAL && !*r_tag)
234
/* FIXME: The following function should not go into this file but for
235
now it is easier to keep it here. */
237
/* Return the next token of an canconical encoded S-expression. BUF
238
is the pointer to the S-expression and BUFLEN is a pointer to the
239
length of this S-expression (used to validate the syntax). Both
240
are updated to reflect the new position. The token itself is
241
returned as a pointer into the orginal buffer at TOK and TOKLEN.
242
If a parentheses is the next token, TOK will be set to NULL.
243
TOKLEN is checked to be within the bounds. On error a error code
244
is returned and all pointers should are not guaranteed to point to
245
a meanigful value. DEPTH should be initialized to 0 and will
246
reflect on return the actual depth of the tree. To detect the end
247
of the S-expression it is advisable to check DEPTH after a
251
while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
253
process_token (tok, toklen);
258
parse_sexp (unsigned char const **buf, size_t *buflen,
259
int *depth, unsigned char const **tok, size_t *toklen)
261
const unsigned char *s;
269
return *depth ? gpg_error (GPG_ERR_INV_SEXP) : 0;
281
return gpg_error (GPG_ERR_INV_SEXP);
289
for (vlen=0; n && *s && *s != ':' && (*s >= '0' && *s <= '9'); s++, n--)
290
vlen = vlen*10 + (*s - '0');
292
return gpg_error (GPG_ERR_INV_SEXP);
295
return gpg_error (GPG_ERR_INV_SEXP);