1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file der_decode_teletex_string.c
7   ASN.1 DER, encode a teletex STRING
8 */
9 
10 #ifdef LTC_DER
11 
12 /**
13   Store a teletex STRING
14   @param in      The DER encoded teletex STRING
15   @param inlen   The size of the DER teletex STRING
16   @param out     [out] The array of octets stored (one per char)
17   @param outlen  [in/out] The number of octets stored
18   @return CRYPT_OK if successful
19 */
der_decode_teletex_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)20 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
21                                 unsigned char *out, unsigned long *outlen)
22 {
23    unsigned long x, y, len;
24    int           t, err;
25 
26    LTC_ARGCHK(in     != NULL);
27    LTC_ARGCHK(out    != NULL);
28    LTC_ARGCHK(outlen != NULL);
29 
30    /* must have header at least */
31    if (inlen < 2) {
32       return CRYPT_INVALID_PACKET;
33    }
34 
35    /* check for 0x14 */
36    if ((in[0] & 0x1F) != 0x14) {
37       return CRYPT_INVALID_PACKET;
38    }
39    x = 1;
40 
41    /* get the length of the data */
42    y = inlen - x;
43    if ((err = der_decode_asn1_length(in + x, &y, &len)) != CRYPT_OK) {
44       return err;
45    }
46    x += y;
47 
48    /* is it too long? */
49    if (len > *outlen) {
50       *outlen = len;
51       return CRYPT_BUFFER_OVERFLOW;
52    }
53 
54    if (len > (inlen - x)) {
55       return CRYPT_INVALID_PACKET;
56    }
57 
58    /* read the data */
59    for (y = 0; y < len; y++) {
60        t = der_teletex_value_decode(in[x++]);
61        if (t == -1) {
62            return CRYPT_INVALID_ARG;
63        }
64        out[y] = t;
65    }
66 
67    *outlen = y;
68 
69    return CRYPT_OK;
70 }
71 
72 #endif
73