• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.com
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 */
der_decode_object_identifier(const unsigned char * in,unsigned long inlen,unsigned long * words,unsigned long * outlen)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 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               return CRYPT_BUFFER_OVERFLOW;
79            }
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            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.5 $ */
99 /* $Date: 2006/11/21 00:18:23 $ */
100