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