1 /*
2 * Copyright The Mbed TLS Contributors
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "psa/crypto.h"
25
26 /* This block is present to support Visual Studio builds prior to 2015 */
27 #if defined(_MSC_VER) && _MSC_VER < 1900
28 #include <stdarg.h>
snprintf(char * s,size_t n,const char * fmt,...)29 int snprintf( char *s, size_t n, const char *fmt, ... )
30 {
31 int ret;
32 va_list argp;
33
34 /* Avoid calling the invalid parameter handler by checking ourselves */
35 if( s == NULL || n == 0 || fmt == NULL )
36 return( -1 );
37
38 va_start( argp, fmt );
39 #if defined(_TRUNCATE) && !defined(__MINGW32__)
40 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
41 #else
42 ret = _vsnprintf( s, n, fmt, argp );
43 if( ret < 0 || (size_t) ret == n )
44 {
45 s[n-1] = '\0';
46 ret = -1;
47 }
48 #endif
49 va_end( argp );
50
51 return( ret );
52 }
53 #endif
54
append(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length)55 static void append(char **buffer, size_t buffer_size,
56 size_t *required_size,
57 const char *string, size_t length)
58 {
59 *required_size += length;
60 if (*required_size < buffer_size) {
61 memcpy(*buffer, string, length);
62 *buffer += length;
63 }
64 }
65
append_integer(char ** buffer,size_t buffer_size,size_t * required_size,const char * format,unsigned long value)66 static void append_integer(char **buffer, size_t buffer_size,
67 size_t *required_size,
68 const char *format /*printf format for value*/,
69 unsigned long value)
70 {
71 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
72 if (n < buffer_size - *required_size) *buffer += n;
73 *required_size += n;
74 }
75
76 /* The code of these function is automatically generated and included below. */
77 static const char *psa_ecc_family_name(psa_ecc_family_t curve);
78 static const char *psa_dh_family_name(psa_dh_family_t group);
79 static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
80
append_with_curve(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length,psa_ecc_family_t curve)81 static void append_with_curve(char **buffer, size_t buffer_size,
82 size_t *required_size,
83 const char *string, size_t length,
84 psa_ecc_family_t curve)
85 {
86 const char *family_name = psa_ecc_family_name(curve);
87 append(buffer, buffer_size, required_size, string, length);
88 append(buffer, buffer_size, required_size, "(", 1);
89 if (family_name != NULL) {
90 append(buffer, buffer_size, required_size,
91 family_name, strlen(family_name));
92 } else {
93 append_integer(buffer, buffer_size, required_size,
94 "0x%02x", curve);
95 }
96 append(buffer, buffer_size, required_size, ")", 1);
97 }
98
append_with_group(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length,psa_dh_family_t group)99 static void append_with_group(char **buffer, size_t buffer_size,
100 size_t *required_size,
101 const char *string, size_t length,
102 psa_dh_family_t group)
103 {
104 const char *group_name = psa_dh_family_name(group);
105 append(buffer, buffer_size, required_size, string, length);
106 append(buffer, buffer_size, required_size, "(", 1);
107 if (group_name != NULL) {
108 append(buffer, buffer_size, required_size,
109 group_name, strlen(group_name));
110 } else {
111 append_integer(buffer, buffer_size, required_size,
112 "0x%02x", group);
113 }
114 append(buffer, buffer_size, required_size, ")", 1);
115 }
116
117 typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
118
append_with_alg(char ** buffer,size_t buffer_size,size_t * required_size,psa_get_algorithm_name_func_ptr get_name,psa_algorithm_t alg)119 static void append_with_alg(char **buffer, size_t buffer_size,
120 size_t *required_size,
121 psa_get_algorithm_name_func_ptr get_name,
122 psa_algorithm_t alg)
123 {
124 const char *name = get_name(alg);
125 if (name != NULL) {
126 append(buffer, buffer_size, required_size,
127 name, strlen(name));
128 } else {
129 append_integer(buffer, buffer_size, required_size,
130 "0x%08lx", alg);
131 }
132 }
133
134 #include "psa_constant_names_generated.c"
135
psa_snprint_status(char * buffer,size_t buffer_size,psa_status_t status)136 static int psa_snprint_status(char *buffer, size_t buffer_size,
137 psa_status_t status)
138 {
139 const char *name = psa_strerror(status);
140 if (name == NULL) {
141 return snprintf(buffer, buffer_size, "%ld", (long) status);
142 } else {
143 size_t length = strlen(name);
144 if (length < buffer_size) {
145 memcpy(buffer, name, length + 1);
146 return (int) length;
147 } else {
148 return (int) buffer_size;
149 }
150 }
151 }
152
psa_snprint_ecc_curve(char * buffer,size_t buffer_size,psa_ecc_family_t curve)153 static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
154 psa_ecc_family_t curve)
155 {
156 const char *name = psa_ecc_family_name(curve);
157 if (name == NULL) {
158 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
159 } else {
160 size_t length = strlen(name);
161 if (length < buffer_size) {
162 memcpy(buffer, name, length + 1);
163 return (int) length;
164 } else {
165 return (int) buffer_size;
166 }
167 }
168 }
169
psa_snprint_dh_group(char * buffer,size_t buffer_size,psa_dh_family_t group)170 static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
171 psa_dh_family_t group)
172 {
173 const char *name = psa_dh_family_name(group);
174 if (name == NULL) {
175 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
176 } else {
177 size_t length = strlen(name);
178 if (length < buffer_size) {
179 memcpy(buffer, name, length + 1);
180 return (int) length;
181 } else {
182 return (int) buffer_size;
183 }
184 }
185 }
186
usage(const char * program_name)187 static void usage(const char *program_name)
188 {
189 printf("Usage: %s TYPE VALUE [VALUE...]\n",
190 program_name == NULL ? "psa_constant_names" : program_name);
191 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
192 printf("Supported types (with = between aliases):\n");
193 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
194 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
195 printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
196 printf(" type=key_type Key type (psa_key_type_t)\n");
197 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
198 printf(" error=status Status code (psa_status_t)\n");
199 }
200
201 typedef enum {
202 TYPE_STATUS,
203 } signed_value_type;
204
process_signed(signed_value_type type,long min,long max,char ** argp)205 int process_signed(signed_value_type type, long min, long max, char **argp)
206 {
207 for (; *argp != NULL; argp++) {
208 char buffer[200];
209 char *end;
210 long value = strtol(*argp, &end, 0);
211 if (*end) {
212 printf("Non-numeric value: %s\n", *argp);
213 return EXIT_FAILURE;
214 }
215 if (value < min || (errno == ERANGE && value < 0)) {
216 printf("Value too small: %s\n", *argp);
217 return EXIT_FAILURE;
218 }
219 if (value > max || (errno == ERANGE && value > 0)) {
220 printf("Value too large: %s\n", *argp);
221 return EXIT_FAILURE;
222 }
223
224 switch (type) {
225 case TYPE_STATUS:
226 psa_snprint_status(buffer, sizeof(buffer),
227 (psa_status_t) value);
228 break;
229 }
230 puts(buffer);
231 }
232
233 return EXIT_SUCCESS;
234 }
235
236 typedef enum {
237 TYPE_ALGORITHM,
238 TYPE_ECC_CURVE,
239 TYPE_DH_GROUP,
240 TYPE_KEY_TYPE,
241 TYPE_KEY_USAGE,
242 } unsigned_value_type;
243
process_unsigned(unsigned_value_type type,unsigned long max,char ** argp)244 int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
245 {
246 for (; *argp != NULL; argp++) {
247 char buffer[200];
248 char *end;
249 unsigned long value = strtoul(*argp, &end, 0);
250 if (*end) {
251 printf("Non-numeric value: %s\n", *argp);
252 return EXIT_FAILURE;
253 }
254 if (value > max || errno == ERANGE) {
255 printf("Value out of range: %s\n", *argp);
256 return EXIT_FAILURE;
257 }
258
259 switch (type) {
260 case TYPE_ALGORITHM:
261 psa_snprint_algorithm(buffer, sizeof(buffer),
262 (psa_algorithm_t) value);
263 break;
264 case TYPE_ECC_CURVE:
265 psa_snprint_ecc_curve(buffer, sizeof(buffer),
266 (psa_ecc_family_t) value);
267 break;
268 case TYPE_DH_GROUP:
269 psa_snprint_dh_group(buffer, sizeof(buffer),
270 (psa_dh_family_t) value);
271 break;
272 case TYPE_KEY_TYPE:
273 psa_snprint_key_type(buffer, sizeof(buffer),
274 (psa_key_type_t) value);
275 break;
276 case TYPE_KEY_USAGE:
277 psa_snprint_key_usage(buffer, sizeof(buffer),
278 (psa_key_usage_t) value);
279 break;
280 }
281 puts(buffer);
282 }
283
284 return EXIT_SUCCESS;
285 }
286
main(int argc,char * argv[])287 int main(int argc, char *argv[])
288 {
289 if (argc <= 1 ||
290 !strcmp(argv[1], "help") ||
291 !strcmp(argv[1], "--help"))
292 {
293 usage(argv[0]);
294 return EXIT_FAILURE;
295 }
296
297 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
298 /* There's no way to obtain the actual range of a signed type,
299 * so hard-code it here: psa_status_t is int32_t. */
300 return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
301 argv + 2);
302 } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
303 return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
304 argv + 2);
305 } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
306 return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
307 argv + 2);
308 } else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
309 return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
310 argv + 2);
311 } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
312 return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
313 argv + 2);
314 } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
315 return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
316 argv + 2);
317 } else {
318 printf("Unknown type: %s\n", argv[1]);
319 return EXIT_FAILURE;
320 }
321 }
322