]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/object_identifier/der_decode_object_identifier.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / object_identifier / der_decode_object_identifier.c
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 #include "tomcrypt.h"
10
11 /**
12   @file der_decode_object_identifier.c
13   ASN.1 DER, Decode Object Identifier, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17 /**
18   Decode OID data and store the array of integers in words
19   @param in      The OID DER encoded data
20   @param inlen   The length of the OID data
21   @param words   [out] The destination of the OID words
22   @param outlen  [in/out] The number of OID words
23   @return CRYPT_OK if successful
24 */
25 int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
26                                        unsigned long *words, unsigned long *outlen)
27 {
28    unsigned long x, y, t, len;
29    int err;
30
31    LTC_ARGCHK(in     != NULL);
32    LTC_ARGCHK(words  != NULL);
33    LTC_ARGCHK(outlen != NULL);
34
35    /* header is at least 3 bytes */
36    if (inlen < 3) {
37       return CRYPT_INVALID_PACKET;
38    }
39
40    /* must be room for at least two words */
41    if (*outlen < 2) {
42       *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 words */
71    y = 0;
72    t = 0;
73    while (len--) {
74       t = (t << 7) | (in[x] & 0x7F);
75       if (!(in[x++] & 0x80)) {
76          /* store t */
77          if (y >= *outlen) {
78             y++;
79          } else {
80             if (y == 0) {
81                words[0] = t / 40;
82                words[1] = t % 40;
83                y = 2;
84             } else {
85                words[y++] = t;
86             }
87          }
88          t = 0;
89       }
90    }
91
92    if (y > *outlen) {
93       err =  CRYPT_BUFFER_OVERFLOW;
94    } else {
95       err =  CRYPT_OK;
96    }
97
98    *outlen = y;
99    return err;
100 }
101
102 #endif
103
104 /* ref:         $Format:%D$ */
105 /* git commit:  $Format:%H$ */
106 /* commit time: $Format:%ai$ */