1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file der_length_ia5_string.c
7   ASN.1 DER, get length of IA5 STRING, Tom St Denis
8 */
9 
10 #ifdef LTC_DER
11 
12 static const struct {
13    int code, value;
14 } ia5_table[] = {
15 { '\0', 0 },
16 { '\a', 7 },
17 { '\b', 8 },
18 { '\t', 9 },
19 { '\n', 10 },
20 { '\f', 12 },
21 { '\r', 13 },
22 { ' ', 32 },
23 { '!', 33 },
24 { '"', 34 },
25 { '#', 35 },
26 { '$', 36 },
27 { '%', 37 },
28 { '&', 38 },
29 { '\'', 39 },
30 { '(', 40 },
31 { ')', 41 },
32 { '*', 42 },
33 { '+', 43 },
34 { ',', 44 },
35 { '-', 45 },
36 { '.', 46 },
37 { '/', 47 },
38 { '0', 48 },
39 { '1', 49 },
40 { '2', 50 },
41 { '3', 51 },
42 { '4', 52 },
43 { '5', 53 },
44 { '6', 54 },
45 { '7', 55 },
46 { '8', 56 },
47 { '9', 57 },
48 { ':', 58 },
49 { ';', 59 },
50 { '<', 60 },
51 { '=', 61 },
52 { '>', 62 },
53 { '?', 63 },
54 { '@', 64 },
55 { 'A', 65 },
56 { 'B', 66 },
57 { 'C', 67 },
58 { 'D', 68 },
59 { 'E', 69 },
60 { 'F', 70 },
61 { 'G', 71 },
62 { 'H', 72 },
63 { 'I', 73 },
64 { 'J', 74 },
65 { 'K', 75 },
66 { 'L', 76 },
67 { 'M', 77 },
68 { 'N', 78 },
69 { 'O', 79 },
70 { 'P', 80 },
71 { 'Q', 81 },
72 { 'R', 82 },
73 { 'S', 83 },
74 { 'T', 84 },
75 { 'U', 85 },
76 { 'V', 86 },
77 { 'W', 87 },
78 { 'X', 88 },
79 { 'Y', 89 },
80 { 'Z', 90 },
81 { '[', 91 },
82 { '\\', 92 },
83 { ']', 93 },
84 { '^', 94 },
85 { '_', 95 },
86 { '`', 96 },
87 { 'a', 97 },
88 { 'b', 98 },
89 { 'c', 99 },
90 { 'd', 100 },
91 { 'e', 101 },
92 { 'f', 102 },
93 { 'g', 103 },
94 { 'h', 104 },
95 { 'i', 105 },
96 { 'j', 106 },
97 { 'k', 107 },
98 { 'l', 108 },
99 { 'm', 109 },
100 { 'n', 110 },
101 { 'o', 111 },
102 { 'p', 112 },
103 { 'q', 113 },
104 { 'r', 114 },
105 { 's', 115 },
106 { 't', 116 },
107 { 'u', 117 },
108 { 'v', 118 },
109 { 'w', 119 },
110 { 'x', 120 },
111 { 'y', 121 },
112 { 'z', 122 },
113 { '{', 123 },
114 { '|', 124 },
115 { '}', 125 },
116 { '~', 126 }
117 };
118 
der_ia5_char_encode(int c)119 int der_ia5_char_encode(int c)
120 {
121    int x;
122    for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
123        if (ia5_table[x].code == c) {
124           return ia5_table[x].value;
125        }
126    }
127    return -1;
128 }
129 
der_ia5_value_decode(int v)130 int der_ia5_value_decode(int v)
131 {
132    int x;
133    for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
134        if (ia5_table[x].value == v) {
135           return ia5_table[x].code;
136        }
137    }
138    return -1;
139 }
140 
141 /**
142   Gets length of DER encoding of IA5 STRING
143   @param octets   The values you want to encode
144   @param noctets  The number of octets in the string to encode
145   @param outlen   [out] The length of the DER encoding for the given string
146   @return CRYPT_OK if successful
147 */
der_length_ia5_string(const unsigned char * octets,unsigned long noctets,unsigned long * outlen)148 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
149 {
150    unsigned long x;
151    int err;
152 
153    LTC_ARGCHK(outlen != NULL);
154    LTC_ARGCHK(octets != NULL);
155 
156    /* scan string for validity */
157    for (x = 0; x < noctets; x++) {
158        if (der_ia5_char_encode(octets[x]) == -1) {
159           return CRYPT_INVALID_ARG;
160        }
161    }
162 
163    if ((err = der_length_asn1_length(noctets, &x)) != CRYPT_OK) {
164       return err;
165    }
166    *outlen = 1 + x + noctets;
167 
168    return CRYPT_OK;
169 }
170 
171 #endif
172 
173