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_printable_string.c
14   ASN.1 DER, encode a printable STRING, Tom St Denis
15 */
16 
17 
18 #ifdef LTC_DER
19 
20 /**
21   Store a printable STRING
22   @param in      The DER encoded printable STRING
23   @param inlen   The size of the DER printable STRING
24   @param out     [out] The array of octets stored (one per char)
25   @param outlen  [in/out] The number of octets stored
26   @return CRYPT_OK if successful
27 */
der_decode_printable_string(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)28 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
29                                 unsigned char *out, unsigned long *outlen)
30 {
31    unsigned long x, y, len;
32    int           t, err;
33 
34    LTC_ARGCHK(in     != NULL);
35    LTC_ARGCHK(out    != NULL);
36    LTC_ARGCHK(outlen != NULL);
37 
38    /* must have header at least */
39    if (inlen < 2) {
40       return CRYPT_INVALID_PACKET;
41    }
42 
43    /* check for 0x13 */
44    if ((in[0] & 0x1F) != 0x13) {
45       return CRYPT_INVALID_PACKET;
46    }
47    x = 1;
48 
49    /* get the length of the data */
50    y = inlen - x;
51    if ((err = der_decode_asn1_length(in + x, &y, &len)) != CRYPT_OK) {
52       return err;
53    }
54    x += y;
55 
56    /* is it too long? */
57    if (len > *outlen) {
58       *outlen = len;
59       return CRYPT_BUFFER_OVERFLOW;
60    }
61 
62    if (len > (inlen - x)) {
63       return CRYPT_INVALID_PACKET;
64    }
65 
66    /* read the data */
67    for (y = 0; y < len; y++) {
68        t = der_printable_value_decode(in[x++]);
69        if (t == -1) {
70            return CRYPT_INVALID_ARG;
71        }
72        out[y] = t;
73    }
74 
75    *outlen = y;
76 
77    return CRYPT_OK;
78 }
79 
80 #endif
81 
82 /* ref:         $Format:%D$ */
83 /* git commit:  $Format:%H$ */
84 /* commit time: $Format:%ai$ */
85