1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_decode_teletex_string.c
14   ASN.1 DER, encode a teletex STRING
15 */
16 
17 #ifdef LTC_DER
18 
19 /**
20   Store a teletex STRING
21   @param in      The DER encoded teletex STRING
22   @param inlen   The size of the DER teletex 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 */
der_decode_teletex_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)27 int der_decode_teletex_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, err;
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 0x14 */
43    if ((in[0] & 0x1F) != 0x14) {
44       return CRYPT_INVALID_PACKET;
45    }
46    x = 1;
47 
48    /* get the length of the data */
49    y = inlen - x;
50    if ((err = der_decode_asn1_length(in + x, &y, &len)) != CRYPT_OK) {
51       return err;
52    }
53    x += y;
54 
55    /* is it too long? */
56    if (len > *outlen) {
57       *outlen = len;
58       return CRYPT_BUFFER_OVERFLOW;
59    }
60 
61    if (len > (inlen - x)) {
62       return CRYPT_INVALID_PACKET;
63    }
64 
65    /* read the data */
66    for (y = 0; y < len; y++) {
67        t = der_teletex_value_decode(in[x++]);
68        if (t == -1) {
69            return CRYPT_INVALID_ARG;
70        }
71        out[y] = t;
72    }
73 
74    *outlen = y;
75 
76    return CRYPT_OK;
77 }
78 
79 #endif
80 
81 /* ref:         $Format:%D$ */
82 /* git commit:  $Format:%H$ */
83 /* commit time: $Format:%ai$ */
84