~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/pk/asn1/der/object_identifier/der_decode_object_identifier.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
 
 
13
/**
 
14
  @file der_decode_object_identifier.c
 
15
  ASN.1 DER, Decode Object Identifier, Tom St Denis
 
16
*/
 
17
 
 
18
#ifdef LTC_DER
 
19
/**
 
20
  Decode OID data and store the array of integers in words
 
21
  @param in      The OID DER encoded data
 
22
  @param inlen   The length of the OID data
 
23
  @param words   [out] The destination of the OID words
 
24
  @param outlen  [in/out] The number of OID words
 
25
  @return CRYPT_OK if successful
 
26
*/
 
27
int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
 
28
                                       unsigned long *words, unsigned long *outlen)
 
29
{
 
30
   unsigned long x, y, t, len;
 
31
 
 
32
   LTC_ARGCHK(in     != NULL);
 
33
   LTC_ARGCHK(words  != NULL);
 
34
   LTC_ARGCHK(outlen != NULL);
 
35
 
 
36
   /* header is at least 3 bytes */
 
37
   if (inlen < 3) {
 
38
      return CRYPT_INVALID_PACKET;
 
39
   }
 
40
 
 
41
   /* must be room for at least two words */
 
42
   if (*outlen < 2) {
 
43
      return CRYPT_BUFFER_OVERFLOW;
 
44
   }
 
45
 
 
46
   /* decode the packet header */
 
47
   x = 0;
 
48
   if ((in[x++] & 0x1F) != 0x06) {
 
49
      return CRYPT_INVALID_PACKET;
 
50
   }
 
51
   
 
52
   /* get the length */
 
53
   if (in[x] < 128) {
 
54
      len = in[x++]; 
 
55
   } else {
 
56
       if (in[x] < 0x81 || in[x] > 0x82) {
 
57
          return CRYPT_INVALID_PACKET;
 
58
       }
 
59
       y   = in[x++] & 0x7F;
 
60
       len = 0;
 
61
       while (y--) {
 
62
          len = (len << 8) | (unsigned long)in[x++];
 
63
       }
 
64
   }
 
65
 
 
66
   if (len < 1 || (len + x) > inlen) {
 
67
      return CRYPT_INVALID_PACKET;
 
68
   }
 
69
 
 
70
   /* decode word1 and word2 */
 
71
   --len;
 
72
   t = in[x++];
 
73
   words[0] = t/40;
 
74
   words[1] = t%40;
 
75
 
 
76
   /* decode rest */
 
77
   y = 2;
 
78
   t = 0;
 
79
   while (len--) {
 
80
       t = (t << 7) | (in[x] & 0x7F);
 
81
       if (!(in[x++] & 0x80)) {
 
82
           /* store t */
 
83
           if (y >= *outlen) {
 
84
              return CRYPT_BUFFER_OVERFLOW;
 
85
           }
 
86
           words[y++] = t;
 
87
           t          = 0;
 
88
       }
 
89
   }
 
90
       
 
91
   *outlen = y;
 
92
   return CRYPT_OK;
 
93
}
 
94
 
 
95
#endif
 
96
 
 
97
/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_decode_object_identifier.c,v $ */
 
98
/* $Revision: 1.1 $ */
 
99
/* $Date: 2005/05/16 15:08:11 $ */