2020-05-26 00:33:31 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2020-05-26 00:33:31 +02:00
|
|
|
*/
|
|
|
|
|
2019-02-13 18:42:06 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2018-07-16 23:13:37 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2018-10-17 15:48:27 +02:00
|
|
|
/* This block is present to support Visual Studio builds prior to 2015 */
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1900
|
|
|
|
#include <stdarg.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
int snprintf(char *s, size_t n, const char *fmt, ...)
|
2018-10-17 15:48:27 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
/* Avoid calling the invalid parameter handler by checking ourselves */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s == NULL || n == 0 || fmt == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-17 15:48:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
va_start(argp, fmt);
|
2018-10-17 15:48:27 +02:00
|
|
|
#if defined(_TRUNCATE) && !defined(__MINGW32__)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = _vsnprintf_s(s, n, _TRUNCATE, fmt, argp);
|
2018-10-17 15:48:27 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = _vsnprintf(s, n, fmt, argp);
|
|
|
|
if (ret < 0 || (size_t) ret == n) {
|
2018-10-17 15:48:27 +02:00
|
|
|
s[n-1] = '\0';
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
va_end(argp);
|
2018-10-17 15:48:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2018-10-17 15:48:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-16 23:13:37 +02:00
|
|
|
static void append(char **buffer, size_t buffer_size,
|
|
|
|
size_t *required_size,
|
|
|
|
const char *string, size_t length)
|
|
|
|
{
|
|
|
|
*required_size += length;
|
|
|
|
if (*required_size < buffer_size) {
|
|
|
|
memcpy(*buffer, string, length);
|
|
|
|
*buffer += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 15:06:39 +02:00
|
|
|
static void append_integer(char **buffer, size_t buffer_size,
|
|
|
|
size_t *required_size,
|
|
|
|
const char *format /*printf format for value*/,
|
|
|
|
unsigned long value)
|
|
|
|
{
|
|
|
|
size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (n < buffer_size - *required_size) {
|
|
|
|
*buffer += n;
|
|
|
|
}
|
2018-08-20 15:06:39 +02:00
|
|
|
*required_size += n;
|
|
|
|
}
|
|
|
|
|
2018-07-16 23:13:37 +02:00
|
|
|
/* The code of these function is automatically generated and included below. */
|
2020-06-02 18:19:28 +02:00
|
|
|
static const char *psa_ecc_family_name(psa_ecc_family_t curve);
|
2020-06-03 16:17:39 +02:00
|
|
|
static const char *psa_dh_family_name(psa_dh_family_t group);
|
2018-07-16 23:13:37 +02:00
|
|
|
static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
|
|
|
|
|
|
|
|
static void append_with_curve(char **buffer, size_t buffer_size,
|
|
|
|
size_t *required_size,
|
|
|
|
const char *string, size_t length,
|
2020-06-02 18:19:28 +02:00
|
|
|
psa_ecc_family_t curve)
|
2018-07-16 23:13:37 +02:00
|
|
|
{
|
2020-06-02 18:19:28 +02:00
|
|
|
const char *family_name = psa_ecc_family_name(curve);
|
2018-07-16 23:13:37 +02:00
|
|
|
append(buffer, buffer_size, required_size, string, length);
|
|
|
|
append(buffer, buffer_size, required_size, "(", 1);
|
2020-06-02 18:19:28 +02:00
|
|
|
if (family_name != NULL) {
|
2018-07-16 23:13:37 +02:00
|
|
|
append(buffer, buffer_size, required_size,
|
2020-06-02 18:19:28 +02:00
|
|
|
family_name, strlen(family_name));
|
2018-07-16 23:13:37 +02:00
|
|
|
} else {
|
2018-08-20 15:06:39 +02:00
|
|
|
append_integer(buffer, buffer_size, required_size,
|
2019-12-04 17:18:41 +01:00
|
|
|
"0x%02x", curve);
|
2018-07-16 23:13:37 +02:00
|
|
|
}
|
|
|
|
append(buffer, buffer_size, required_size, ")", 1);
|
|
|
|
}
|
|
|
|
|
2019-05-16 12:55:35 +02:00
|
|
|
static void append_with_group(char **buffer, size_t buffer_size,
|
|
|
|
size_t *required_size,
|
|
|
|
const char *string, size_t length,
|
2020-06-03 16:17:39 +02:00
|
|
|
psa_dh_family_t group)
|
2019-05-16 12:55:35 +02:00
|
|
|
{
|
2020-06-03 16:17:39 +02:00
|
|
|
const char *group_name = psa_dh_family_name(group);
|
2019-05-16 12:55:35 +02:00
|
|
|
append(buffer, buffer_size, required_size, string, length);
|
|
|
|
append(buffer, buffer_size, required_size, "(", 1);
|
|
|
|
if (group_name != NULL) {
|
|
|
|
append(buffer, buffer_size, required_size,
|
|
|
|
group_name, strlen(group_name));
|
|
|
|
} else {
|
|
|
|
append_integer(buffer, buffer_size, required_size,
|
2019-12-04 17:18:41 +01:00
|
|
|
"0x%02x", group);
|
2019-05-16 12:55:35 +02:00
|
|
|
}
|
|
|
|
append(buffer, buffer_size, required_size, ")", 1);
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:12:07 +02:00
|
|
|
typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
|
|
|
|
|
|
|
|
static void 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)
|
2018-07-16 23:13:37 +02:00
|
|
|
{
|
2019-04-12 00:12:07 +02:00
|
|
|
const char *name = get_name(alg);
|
|
|
|
if (name != NULL) {
|
2018-07-16 23:13:37 +02:00
|
|
|
append(buffer, buffer_size, required_size,
|
2019-04-12 00:12:07 +02:00
|
|
|
name, strlen(name));
|
2018-07-16 23:13:37 +02:00
|
|
|
} else {
|
2018-08-20 15:06:39 +02:00
|
|
|
append_integer(buffer, buffer_size, required_size,
|
2019-04-12 00:12:07 +02:00
|
|
|
"0x%08lx", alg);
|
2018-07-16 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "psa_constant_names_generated.c"
|
|
|
|
|
|
|
|
static int psa_snprint_status(char *buffer, size_t buffer_size,
|
|
|
|
psa_status_t status)
|
|
|
|
{
|
|
|
|
const char *name = psa_strerror(status);
|
|
|
|
if (name == NULL) {
|
|
|
|
return snprintf(buffer, buffer_size, "%ld", (long) status);
|
|
|
|
} else {
|
|
|
|
size_t length = strlen(name);
|
|
|
|
if (length < buffer_size) {
|
|
|
|
memcpy(buffer, name, length + 1);
|
2018-10-17 16:01:45 +02:00
|
|
|
return (int) length;
|
2018-07-16 23:13:37 +02:00
|
|
|
} else {
|
2018-10-17 16:01:45 +02:00
|
|
|
return (int) buffer_size;
|
2018-07-16 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
|
2020-06-02 18:19:28 +02:00
|
|
|
psa_ecc_family_t curve)
|
2018-07-16 23:13:37 +02:00
|
|
|
{
|
2020-06-02 18:19:28 +02:00
|
|
|
const char *name = psa_ecc_family_name(curve);
|
2018-07-16 23:13:37 +02:00
|
|
|
if (name == NULL) {
|
2019-12-04 17:18:41 +01:00
|
|
|
return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
|
2018-07-16 23:13:37 +02:00
|
|
|
} else {
|
|
|
|
size_t length = strlen(name);
|
|
|
|
if (length < buffer_size) {
|
|
|
|
memcpy(buffer, name, length + 1);
|
2018-10-17 16:01:45 +02:00
|
|
|
return (int) length;
|
2018-07-16 23:13:37 +02:00
|
|
|
} else {
|
2018-10-17 16:01:45 +02:00
|
|
|
return (int) buffer_size;
|
2018-07-16 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 12:55:35 +02:00
|
|
|
static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
|
2020-06-03 16:17:39 +02:00
|
|
|
psa_dh_family_t group)
|
2019-05-16 12:55:35 +02:00
|
|
|
{
|
2020-06-03 16:17:39 +02:00
|
|
|
const char *name = psa_dh_family_name(group);
|
2019-05-16 12:55:35 +02:00
|
|
|
if (name == NULL) {
|
2019-12-04 17:18:41 +01:00
|
|
|
return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
|
2019-05-16 12:55:35 +02:00
|
|
|
} else {
|
|
|
|
size_t length = strlen(name);
|
|
|
|
if (length < buffer_size) {
|
|
|
|
memcpy(buffer, name, length + 1);
|
|
|
|
return (int) length;
|
|
|
|
} else {
|
|
|
|
return (int) buffer_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 23:13:37 +02:00
|
|
|
static void usage(const char *program_name)
|
|
|
|
{
|
2018-09-25 18:27:53 +02:00
|
|
|
printf("Usage: %s TYPE VALUE [VALUE...]\n",
|
2018-07-16 23:13:37 +02:00
|
|
|
program_name == NULL ? "psa_constant_names" : program_name);
|
|
|
|
printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
|
|
|
|
printf("Supported types (with = between aliases):\n");
|
2018-08-20 15:07:37 +02:00
|
|
|
printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
|
2020-06-02 18:19:28 +02:00
|
|
|
printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
|
2020-06-03 16:17:39 +02:00
|
|
|
printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
|
2018-08-20 15:07:37 +02:00
|
|
|
printf(" type=key_type Key type (psa_key_type_t)\n");
|
2018-07-16 23:13:37 +02:00
|
|
|
printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
|
|
|
|
printf(" error=status Status code (psa_status_t)\n");
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:27:53 +02:00
|
|
|
typedef enum {
|
|
|
|
TYPE_STATUS,
|
2019-02-13 18:42:06 +01:00
|
|
|
} signed_value_type;
|
|
|
|
|
|
|
|
int process_signed(signed_value_type type, long min, long max, char **argp)
|
|
|
|
{
|
|
|
|
for (; *argp != NULL; argp++) {
|
|
|
|
char buffer[200];
|
|
|
|
char *end;
|
|
|
|
long value = strtol(*argp, &end, 0);
|
|
|
|
if (*end) {
|
|
|
|
printf("Non-numeric value: %s\n", *argp);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (value < min || (errno == ERANGE && value < 0)) {
|
|
|
|
printf("Value too small: %s\n", *argp);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (value > max || (errno == ERANGE && value > 0)) {
|
|
|
|
printf("Value too large: %s\n", *argp);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_STATUS:
|
|
|
|
psa_snprint_status(buffer, sizeof(buffer),
|
|
|
|
(psa_status_t) value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
puts(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
2018-09-25 18:27:53 +02:00
|
|
|
TYPE_ALGORITHM,
|
|
|
|
TYPE_ECC_CURVE,
|
2019-05-16 12:55:35 +02:00
|
|
|
TYPE_DH_GROUP,
|
2018-09-25 18:27:53 +02:00
|
|
|
TYPE_KEY_TYPE,
|
|
|
|
TYPE_KEY_USAGE,
|
2019-02-13 18:40:50 +01:00
|
|
|
} unsigned_value_type;
|
2018-09-25 18:27:53 +02:00
|
|
|
|
2019-02-13 18:40:50 +01:00
|
|
|
int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
|
2018-07-16 23:13:37 +02:00
|
|
|
{
|
2019-02-13 18:40:50 +01:00
|
|
|
for (; *argp != NULL; argp++) {
|
2018-09-25 18:27:53 +02:00
|
|
|
char buffer[200];
|
|
|
|
char *end;
|
2019-02-13 18:40:50 +01:00
|
|
|
unsigned long value = strtoul(*argp, &end, 0);
|
2018-09-25 18:27:53 +02:00
|
|
|
if (*end) {
|
2019-02-13 18:40:50 +01:00
|
|
|
printf("Non-numeric value: %s\n", *argp);
|
2018-09-25 18:27:53 +02:00
|
|
|
return EXIT_FAILURE;
|
2018-10-31 14:52:28 +01:00
|
|
|
}
|
2019-02-13 18:42:06 +01:00
|
|
|
if (value > max || errno == ERANGE) {
|
2019-02-13 18:40:50 +01:00
|
|
|
printf("Value out of range: %s\n", *argp);
|
2018-10-31 14:52:28 +01:00
|
|
|
return EXIT_FAILURE;
|
2018-09-25 18:27:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_ALGORITHM:
|
|
|
|
psa_snprint_algorithm(buffer, sizeof(buffer),
|
|
|
|
(psa_algorithm_t) value);
|
|
|
|
break;
|
|
|
|
case TYPE_ECC_CURVE:
|
|
|
|
psa_snprint_ecc_curve(buffer, sizeof(buffer),
|
2020-06-02 18:19:28 +02:00
|
|
|
(psa_ecc_family_t) value);
|
2018-09-25 18:27:53 +02:00
|
|
|
break;
|
2019-05-16 12:55:35 +02:00
|
|
|
case TYPE_DH_GROUP:
|
|
|
|
psa_snprint_dh_group(buffer, sizeof(buffer),
|
2020-06-03 16:17:39 +02:00
|
|
|
(psa_dh_family_t) value);
|
2019-05-16 12:55:35 +02:00
|
|
|
break;
|
2018-09-25 18:27:53 +02:00
|
|
|
case TYPE_KEY_TYPE:
|
|
|
|
psa_snprint_key_type(buffer, sizeof(buffer),
|
|
|
|
(psa_key_type_t) value);
|
|
|
|
break;
|
|
|
|
case TYPE_KEY_USAGE:
|
|
|
|
psa_snprint_key_usage(buffer, sizeof(buffer),
|
|
|
|
(psa_key_usage_t) value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
puts(buffer);
|
|
|
|
}
|
|
|
|
|
2018-07-16 23:13:37 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2019-02-13 18:40:50 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc <= 1 ||
|
|
|
|
!strcmp(argv[1], "help") ||
|
2023-01-11 14:50:10 +01:00
|
|
|
!strcmp(argv[1], "--help")) {
|
2019-02-13 18:40:50 +01:00
|
|
|
usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
|
2019-02-13 18:42:06 +01:00
|
|
|
/* There's no way to obtain the actual range of a signed type,
|
|
|
|
* so hard-code it here: psa_status_t is int32_t. */
|
|
|
|
return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
|
|
|
|
argv + 2);
|
2019-02-13 18:40:50 +01:00
|
|
|
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
|
|
|
|
return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
|
|
|
|
argv + 2);
|
|
|
|
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
|
2020-06-02 18:19:28 +02:00
|
|
|
return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
|
2019-02-13 18:40:50 +01:00
|
|
|
argv + 2);
|
2019-05-16 12:55:35 +02:00
|
|
|
} else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
|
2020-06-03 16:17:39 +02:00
|
|
|
return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
|
2019-05-16 12:55:35 +02:00
|
|
|
argv + 2);
|
2019-02-13 18:40:50 +01:00
|
|
|
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
|
|
|
|
return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
|
|
|
|
argv + 2);
|
|
|
|
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
|
|
|
|
return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
|
|
|
|
argv + 2);
|
|
|
|
} else {
|
|
|
|
printf("Unknown type: %s\n", argv[1]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|