]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/ia5/der_decode_ia5_string.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / ia5 / der_decode_ia5_string.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_ia5_string.c
13   ASN.1 DER, encode a IA5 STRING, Tom St Denis
14 */
15
16
17 #ifdef LTC_DER
18
19 /**
20   Store a IA5 STRING
21   @param in      The DER encoded IA5 STRING
22   @param inlen   The size of the DER IA5 STRING
23   @param out     [out] The array of octets stored (one per char)
24   @param outlen  [in/out] The number of octets stored
25   @return CRYPT_OK if successful
26 */
27 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
28                                 unsigned char *out, unsigned long *outlen)
29 {
30    unsigned long x, y, len;
31    int           t;
32
33    LTC_ARGCHK(in     != NULL);
34    LTC_ARGCHK(out    != NULL);
35    LTC_ARGCHK(outlen != NULL);
36
37    /* must have header at least */
38    if (inlen < 2) {
39       return CRYPT_INVALID_PACKET;
40    }
41
42    /* check for 0x16 */
43    if ((in[0] & 0x1F) != 0x16) {
44       return CRYPT_INVALID_PACKET;
45    }
46    x = 1;
47
48    /* decode the length */
49    if (in[x] & 0x80) {
50       /* valid # of bytes in length are 1,2,3 */
51       y = in[x] & 0x7F;
52       if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
53          return CRYPT_INVALID_PACKET;
54       }
55
56       /* read the length in */
57       len = 0;
58       ++x;
59       while (y--) {
60          len = (len << 8) | in[x++];
61       }
62    } else {
63       len = in[x++] & 0x7F;
64    }
65
66    /* is it too long? */
67    if (len > *outlen) {
68       *outlen = len;
69       return CRYPT_BUFFER_OVERFLOW;
70    }
71
72    if (len + x > inlen) {
73       return CRYPT_INVALID_PACKET;
74    }
75
76    /* read the data */
77    for (y = 0; y < len; y++) {
78        t = der_ia5_value_decode(in[x++]);
79        if (t == -1) {
80            return CRYPT_INVALID_ARG;
81        }
82        out[y] = t;
83    }
84
85    *outlen = y;
86
87    return CRYPT_OK;
88 }
89
90 #endif
91
92 /* ref:         $Format:%D$ */
93 /* git commit:  $Format:%H$ */
94 /* commit time: $Format:%ai$ */