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