~ubuntu-branches/ubuntu/natty/gnupg2/natty-security

« back to all changes in this revision

Viewing changes to scd/tlv.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-12-08 22:13:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208221321-4rvs2vu835iam5wv
Tags: 1.9.19-2
* Convert debian/changelog to UTF-8.
* Put gnupg-agent and gpgsm lintian overrides in the respectively
  right package.  Closes: #335066
* Added debhelper tokens to maintainer scripts.
* xsession fixes:
  o Added host name to gpg-agent PID file name.  Closes: #312717
  o Fixed xsession script to be able to run under zsh.  Closes: #308516
  o Don't run gpg-agent if one is already running.  Closes: #336480
* debian/control:
  o Fixed package description of gpgsm package.  Closes: #299842
  o Added mention of gpg-agent to description of gnupg-agent package.
    Closes: #304355
* Thanks to Peter Eisentraut <petere@debian.org> for all of the above.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* tlv.c - Tag-Length-Value Utilities
2
 
 *      Copyright (C) 2003, 2004 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
25
25
#include <string.h>
26
26
#include <assert.h>
27
27
 
 
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)
 
34
#else
28
35
#include <gpg-error.h>
 
36
#endif
29
37
 
30
38
#include "tlv.h"
31
39
 
113
121
 
114
122
/* Locate a TLV encoded data object in BUFFER of LENGTH and
115
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)
 
128
{
 
129
  const unsigned char *p;
 
130
 
 
131
  p = do_find_tlv (buffer, length, tag, nbytes, 0);
 
132
  if (p && *nbytes > (length - (p-buffer)))
 
133
    p = NULL; /* Object longer than buffer. */
 
134
  return p;
 
135
}
 
136
 
 
137
 
 
138
 
 
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
116
141
   NULL if it was not found.  Note, that the function does not check
117
142
   whether the value fits into the provided buffer. */
118
143
const unsigned char *
119
 
find_tlv (const unsigned char *buffer, size_t length,
120
 
          int tag, size_t *nbytes)
 
144
find_tlv_unchecked (const unsigned char *buffer, size_t length,
 
145
                    int tag, size_t *nbytes)
121
146
{
122
147
  return do_find_tlv (buffer, length, tag, nbytes, 0);
123
148
}
124
149
 
125
150
 
126
 
 
127
 
 
128
151
/* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
129
152
   and the length part from the TLV triplet.  Update BUFFER and SIZE
130
153
   on success. */
206
229
  *size = length;
207
230
  return 0;
208
231
}
 
232
 
 
233
 
 
234
/* FIXME: The following function should not go into this file but for
 
235
   now it is easier to keep it here. */
 
236
 
 
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
 
248
   successful return:
 
249
 
 
250
   depth = 0;
 
251
   while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
 
252
          && depth)
 
253
     process_token (tok, toklen);
 
254
   if (err)  
 
255
     handle_error ();
 
256
 */
 
257
gpg_error_t
 
258
parse_sexp (unsigned char const **buf, size_t *buflen,
 
259
            int *depth, unsigned char const **tok, size_t *toklen)
 
260
{
 
261
  const unsigned char *s;
 
262
  size_t n, vlen;
 
263
 
 
264
  s = *buf;
 
265
  n = *buflen;
 
266
  *tok = NULL;
 
267
  *toklen = 0;
 
268
  if (!n)
 
269
    return *depth ? gpg_error (GPG_ERR_INV_SEXP) : 0;
 
270
  if (*s == '(')
 
271
    {
 
272
      s++; n--;
 
273
      (*depth)++;
 
274
      *buf = s;
 
275
      *buflen = n;
 
276
      return 0;
 
277
    }
 
278
  if (*s == ')')
 
279
    {
 
280
      if (!*depth)
 
281
        return gpg_error (GPG_ERR_INV_SEXP);
 
282
      *toklen = 1;
 
283
      s++; n--;
 
284
      (*depth)--;
 
285
      *buf = s;
 
286
      *buflen = n;
 
287
      return 0;
 
288
    }
 
289
  for (vlen=0; n && *s && *s != ':' && (*s >= '0' && *s <= '9'); s++, n--)
 
290
    vlen = vlen*10 + (*s - '0');
 
291
  if (!n || *s != ':')
 
292
    return gpg_error (GPG_ERR_INV_SEXP);
 
293
  s++; n--;
 
294
  if (vlen > n)
 
295
    return gpg_error (GPG_ERR_INV_SEXP);
 
296
  *tok = s;
 
297
  *toklen = vlen;
 
298
  s += vlen;
 
299
  n -= vlen;
 
300
  *buf = s;
 
301
  *buflen = n;
 
302
  return 0;
 
303
}
 
304