1a8e1175bSopenharmony_ci/* 2a8e1175bSopenharmony_ci * Key reading application 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 5a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci#include "mbedtls/build_info.h" 9a8e1175bSopenharmony_ci 10a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 11a8e1175bSopenharmony_ci 12a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) && \ 13a8e1175bSopenharmony_ci defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \ 14a8e1175bSopenharmony_ci defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) 15a8e1175bSopenharmony_ci#include "mbedtls/error.h" 16a8e1175bSopenharmony_ci#include "mbedtls/rsa.h" 17a8e1175bSopenharmony_ci#include "mbedtls/pk.h" 18a8e1175bSopenharmony_ci#include "mbedtls/entropy.h" 19a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h" 20a8e1175bSopenharmony_ci 21a8e1175bSopenharmony_ci#include <string.h> 22a8e1175bSopenharmony_ci#endif 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci#define MODE_NONE 0 25a8e1175bSopenharmony_ci#define MODE_PRIVATE 1 26a8e1175bSopenharmony_ci#define MODE_PUBLIC 2 27a8e1175bSopenharmony_ci 28a8e1175bSopenharmony_ci#define DFL_MODE MODE_NONE 29a8e1175bSopenharmony_ci#define DFL_FILENAME "keyfile.key" 30a8e1175bSopenharmony_ci#define DFL_PASSWORD "" 31a8e1175bSopenharmony_ci#define DFL_PASSWORD_FILE "" 32a8e1175bSopenharmony_ci#define DFL_DEBUG_LEVEL 0 33a8e1175bSopenharmony_ci 34a8e1175bSopenharmony_ci#define USAGE \ 35a8e1175bSopenharmony_ci "\n usage: key_app param=<>...\n" \ 36a8e1175bSopenharmony_ci "\n acceptable parameters:\n" \ 37a8e1175bSopenharmony_ci " mode=private|public default: none\n" \ 38a8e1175bSopenharmony_ci " filename=%%s default: keyfile.key\n" \ 39a8e1175bSopenharmony_ci " password=%%s default: \"\"\n" \ 40a8e1175bSopenharmony_ci " password_file=%%s default: \"\"\n" \ 41a8e1175bSopenharmony_ci "\n" 42a8e1175bSopenharmony_ci 43a8e1175bSopenharmony_ci#if !defined(MBEDTLS_BIGNUM_C) || \ 44a8e1175bSopenharmony_ci !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ 45a8e1175bSopenharmony_ci !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) 46a8e1175bSopenharmony_ciint main(void) 47a8e1175bSopenharmony_ci{ 48a8e1175bSopenharmony_ci mbedtls_printf("MBEDTLS_BIGNUM_C and/or " 49a8e1175bSopenharmony_ci "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or " 50a8e1175bSopenharmony_ci "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n"); 51a8e1175bSopenharmony_ci mbedtls_exit(0); 52a8e1175bSopenharmony_ci} 53a8e1175bSopenharmony_ci#else 54a8e1175bSopenharmony_ci 55a8e1175bSopenharmony_ci 56a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_C) 57a8e1175bSopenharmony_cistatic int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private) 58a8e1175bSopenharmony_ci{ 59a8e1175bSopenharmony_ci int ret = 0; 60a8e1175bSopenharmony_ci 61a8e1175bSopenharmony_ci const mbedtls_ecp_curve_info *curve_info = 62a8e1175bSopenharmony_ci mbedtls_ecp_curve_info_from_grp_id( 63a8e1175bSopenharmony_ci mbedtls_ecp_keypair_get_group_id(ecp)); 64a8e1175bSopenharmony_ci mbedtls_printf("curve: %s\n", curve_info->name); 65a8e1175bSopenharmony_ci 66a8e1175bSopenharmony_ci mbedtls_ecp_group grp; 67a8e1175bSopenharmony_ci mbedtls_ecp_group_init(&grp); 68a8e1175bSopenharmony_ci mbedtls_mpi D; 69a8e1175bSopenharmony_ci mbedtls_mpi_init(&D); 70a8e1175bSopenharmony_ci mbedtls_ecp_point pt; 71a8e1175bSopenharmony_ci mbedtls_ecp_point_init(&pt); 72a8e1175bSopenharmony_ci mbedtls_mpi X, Y; 73a8e1175bSopenharmony_ci mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); 74a8e1175bSopenharmony_ci 75a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp, 76a8e1175bSopenharmony_ci (has_private ? &D : NULL), 77a8e1175bSopenharmony_ci &pt)); 78a8e1175bSopenharmony_ci 79a8e1175bSopenharmony_ci unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN]; 80a8e1175bSopenharmony_ci size_t len = 0; 81a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary( 82a8e1175bSopenharmony_ci &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED, 83a8e1175bSopenharmony_ci &len, point_bin, sizeof(point_bin))); 84a8e1175bSopenharmony_ci switch (mbedtls_ecp_get_type(&grp)) { 85a8e1175bSopenharmony_ci case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS: 86a8e1175bSopenharmony_ci if ((len & 1) == 0 || point_bin[0] != 0x04) { 87a8e1175bSopenharmony_ci /* Point in an unxepected format. This shouldn't happen. */ 88a8e1175bSopenharmony_ci ret = -1; 89a8e1175bSopenharmony_ci goto cleanup; 90a8e1175bSopenharmony_ci } 91a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK( 92a8e1175bSopenharmony_ci mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2)); 93a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK( 94a8e1175bSopenharmony_ci mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2)); 95a8e1175bSopenharmony_ci mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL); 96a8e1175bSopenharmony_ci mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL); 97a8e1175bSopenharmony_ci break; 98a8e1175bSopenharmony_ci case MBEDTLS_ECP_TYPE_MONTGOMERY: 99a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len)); 100a8e1175bSopenharmony_ci mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL); 101a8e1175bSopenharmony_ci break; 102a8e1175bSopenharmony_ci default: 103a8e1175bSopenharmony_ci mbedtls_printf( 104a8e1175bSopenharmony_ci "This program does not yet support listing coordinates for this curve type.\n"); 105a8e1175bSopenharmony_ci break; 106a8e1175bSopenharmony_ci } 107a8e1175bSopenharmony_ci 108a8e1175bSopenharmony_ci if (has_private) { 109a8e1175bSopenharmony_ci mbedtls_mpi_write_file("D: ", &D, 16, NULL); 110a8e1175bSopenharmony_ci } 111a8e1175bSopenharmony_ci 112a8e1175bSopenharmony_cicleanup: 113a8e1175bSopenharmony_ci mbedtls_ecp_group_free(&grp); 114a8e1175bSopenharmony_ci mbedtls_mpi_free(&D); 115a8e1175bSopenharmony_ci mbedtls_ecp_point_free(&pt); 116a8e1175bSopenharmony_ci mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); 117a8e1175bSopenharmony_ci return ret; 118a8e1175bSopenharmony_ci} 119a8e1175bSopenharmony_ci#endif 120a8e1175bSopenharmony_ci 121a8e1175bSopenharmony_ci/* 122a8e1175bSopenharmony_ci * global options 123a8e1175bSopenharmony_ci */ 124a8e1175bSopenharmony_cistruct options { 125a8e1175bSopenharmony_ci int mode; /* the mode to run the application in */ 126a8e1175bSopenharmony_ci const char *filename; /* filename of the key file */ 127a8e1175bSopenharmony_ci const char *password; /* password for the private key */ 128a8e1175bSopenharmony_ci const char *password_file; /* password_file for the private key */ 129a8e1175bSopenharmony_ci} opt; 130a8e1175bSopenharmony_ci 131a8e1175bSopenharmony_ciint main(int argc, char *argv[]) 132a8e1175bSopenharmony_ci{ 133a8e1175bSopenharmony_ci int ret = 1; 134a8e1175bSopenharmony_ci int exit_code = MBEDTLS_EXIT_FAILURE; 135a8e1175bSopenharmony_ci char buf[1024]; 136a8e1175bSopenharmony_ci int i; 137a8e1175bSopenharmony_ci char *p, *q; 138a8e1175bSopenharmony_ci 139a8e1175bSopenharmony_ci const char *pers = "pkey/key_app"; 140a8e1175bSopenharmony_ci mbedtls_entropy_context entropy; 141a8e1175bSopenharmony_ci mbedtls_ctr_drbg_context ctr_drbg; 142a8e1175bSopenharmony_ci 143a8e1175bSopenharmony_ci mbedtls_pk_context pk; 144a8e1175bSopenharmony_ci mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; 145a8e1175bSopenharmony_ci 146a8e1175bSopenharmony_ci /* 147a8e1175bSopenharmony_ci * Set to sane values 148a8e1175bSopenharmony_ci */ 149a8e1175bSopenharmony_ci mbedtls_entropy_init(&entropy); 150a8e1175bSopenharmony_ci mbedtls_ctr_drbg_init(&ctr_drbg); 151a8e1175bSopenharmony_ci 152a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 153a8e1175bSopenharmony_ci memset(buf, 0, sizeof(buf)); 154a8e1175bSopenharmony_ci 155a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 156a8e1175bSopenharmony_ci psa_status_t status = psa_crypto_init(); 157a8e1175bSopenharmony_ci if (status != PSA_SUCCESS) { 158a8e1175bSopenharmony_ci mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", 159a8e1175bSopenharmony_ci (int) status); 160a8e1175bSopenharmony_ci goto cleanup; 161a8e1175bSopenharmony_ci } 162a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 163a8e1175bSopenharmony_ci 164a8e1175bSopenharmony_ci mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); 165a8e1175bSopenharmony_ci mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP); 166a8e1175bSopenharmony_ci mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP); 167a8e1175bSopenharmony_ci 168a8e1175bSopenharmony_ci if (argc < 2) { 169a8e1175bSopenharmony_ciusage: 170a8e1175bSopenharmony_ci mbedtls_printf(USAGE); 171a8e1175bSopenharmony_ci goto cleanup; 172a8e1175bSopenharmony_ci } 173a8e1175bSopenharmony_ci 174a8e1175bSopenharmony_ci opt.mode = DFL_MODE; 175a8e1175bSopenharmony_ci opt.filename = DFL_FILENAME; 176a8e1175bSopenharmony_ci opt.password = DFL_PASSWORD; 177a8e1175bSopenharmony_ci opt.password_file = DFL_PASSWORD_FILE; 178a8e1175bSopenharmony_ci 179a8e1175bSopenharmony_ci for (i = 1; i < argc; i++) { 180a8e1175bSopenharmony_ci p = argv[i]; 181a8e1175bSopenharmony_ci if ((q = strchr(p, '=')) == NULL) { 182a8e1175bSopenharmony_ci goto usage; 183a8e1175bSopenharmony_ci } 184a8e1175bSopenharmony_ci *q++ = '\0'; 185a8e1175bSopenharmony_ci 186a8e1175bSopenharmony_ci if (strcmp(p, "mode") == 0) { 187a8e1175bSopenharmony_ci if (strcmp(q, "private") == 0) { 188a8e1175bSopenharmony_ci opt.mode = MODE_PRIVATE; 189a8e1175bSopenharmony_ci } else if (strcmp(q, "public") == 0) { 190a8e1175bSopenharmony_ci opt.mode = MODE_PUBLIC; 191a8e1175bSopenharmony_ci } else { 192a8e1175bSopenharmony_ci goto usage; 193a8e1175bSopenharmony_ci } 194a8e1175bSopenharmony_ci } else if (strcmp(p, "filename") == 0) { 195a8e1175bSopenharmony_ci opt.filename = q; 196a8e1175bSopenharmony_ci } else if (strcmp(p, "password") == 0) { 197a8e1175bSopenharmony_ci opt.password = q; 198a8e1175bSopenharmony_ci } else if (strcmp(p, "password_file") == 0) { 199a8e1175bSopenharmony_ci opt.password_file = q; 200a8e1175bSopenharmony_ci } else { 201a8e1175bSopenharmony_ci goto usage; 202a8e1175bSopenharmony_ci } 203a8e1175bSopenharmony_ci } 204a8e1175bSopenharmony_ci 205a8e1175bSopenharmony_ci if (opt.mode == MODE_PRIVATE) { 206a8e1175bSopenharmony_ci if (strlen(opt.password) && strlen(opt.password_file)) { 207a8e1175bSopenharmony_ci mbedtls_printf("Error: cannot have both password and password_file\n"); 208a8e1175bSopenharmony_ci goto usage; 209a8e1175bSopenharmony_ci } 210a8e1175bSopenharmony_ci 211a8e1175bSopenharmony_ci if (strlen(opt.password_file)) { 212a8e1175bSopenharmony_ci FILE *f; 213a8e1175bSopenharmony_ci 214a8e1175bSopenharmony_ci mbedtls_printf("\n . Loading the password file ..."); 215a8e1175bSopenharmony_ci if ((f = fopen(opt.password_file, "rb")) == NULL) { 216a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! fopen returned NULL\n"); 217a8e1175bSopenharmony_ci goto cleanup; 218a8e1175bSopenharmony_ci } 219a8e1175bSopenharmony_ci if (fgets(buf, sizeof(buf), f) == NULL) { 220a8e1175bSopenharmony_ci fclose(f); 221a8e1175bSopenharmony_ci mbedtls_printf("Error: fgets() failed to retrieve password\n"); 222a8e1175bSopenharmony_ci goto cleanup; 223a8e1175bSopenharmony_ci } 224a8e1175bSopenharmony_ci fclose(f); 225a8e1175bSopenharmony_ci 226a8e1175bSopenharmony_ci i = (int) strlen(buf); 227a8e1175bSopenharmony_ci if (buf[i - 1] == '\n') { 228a8e1175bSopenharmony_ci buf[i - 1] = '\0'; 229a8e1175bSopenharmony_ci } 230a8e1175bSopenharmony_ci if (buf[i - 2] == '\r') { 231a8e1175bSopenharmony_ci buf[i - 2] = '\0'; 232a8e1175bSopenharmony_ci } 233a8e1175bSopenharmony_ci opt.password = buf; 234a8e1175bSopenharmony_ci } 235a8e1175bSopenharmony_ci 236a8e1175bSopenharmony_ci /* 237a8e1175bSopenharmony_ci * 1.1. Load the key 238a8e1175bSopenharmony_ci */ 239a8e1175bSopenharmony_ci mbedtls_printf("\n . Loading the private key ..."); 240a8e1175bSopenharmony_ci fflush(stdout); 241a8e1175bSopenharmony_ci 242a8e1175bSopenharmony_ci if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, 243a8e1175bSopenharmony_ci (const unsigned char *) pers, 244a8e1175bSopenharmony_ci strlen(pers))) != 0) { 245a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", 246a8e1175bSopenharmony_ci (unsigned int) -ret); 247a8e1175bSopenharmony_ci goto cleanup; 248a8e1175bSopenharmony_ci } 249a8e1175bSopenharmony_ci 250a8e1175bSopenharmony_ci ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password, 251a8e1175bSopenharmony_ci mbedtls_ctr_drbg_random, &ctr_drbg); 252a8e1175bSopenharmony_ci 253a8e1175bSopenharmony_ci if (ret != 0) { 254a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", 255a8e1175bSopenharmony_ci (unsigned int) -ret); 256a8e1175bSopenharmony_ci goto cleanup; 257a8e1175bSopenharmony_ci } 258a8e1175bSopenharmony_ci 259a8e1175bSopenharmony_ci mbedtls_printf(" ok\n"); 260a8e1175bSopenharmony_ci 261a8e1175bSopenharmony_ci /* 262a8e1175bSopenharmony_ci * 1.2 Print the key 263a8e1175bSopenharmony_ci */ 264a8e1175bSopenharmony_ci mbedtls_printf(" . Key information ...\n"); 265a8e1175bSopenharmony_ci#if defined(MBEDTLS_RSA_C) 266a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) { 267a8e1175bSopenharmony_ci mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk); 268a8e1175bSopenharmony_ci 269a8e1175bSopenharmony_ci if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 || 270a8e1175bSopenharmony_ci (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) { 271a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! could not export RSA parameters\n\n"); 272a8e1175bSopenharmony_ci goto cleanup; 273a8e1175bSopenharmony_ci } 274a8e1175bSopenharmony_ci 275a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL)); 276a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL)); 277a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL)); 278a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL)); 279a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL)); 280a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL)); 281a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL)); 282a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL)); 283a8e1175bSopenharmony_ci } else 284a8e1175bSopenharmony_ci#endif 285a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_C) 286a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) { 287a8e1175bSopenharmony_ci if (show_ecp_key(mbedtls_pk_ec(pk), 1) != 0) { 288a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! could not export ECC parameters\n\n"); 289a8e1175bSopenharmony_ci goto cleanup; 290a8e1175bSopenharmony_ci } 291a8e1175bSopenharmony_ci } else 292a8e1175bSopenharmony_ci#endif 293a8e1175bSopenharmony_ci { 294a8e1175bSopenharmony_ci mbedtls_printf("Do not know how to print key information for this type\n"); 295a8e1175bSopenharmony_ci goto cleanup; 296a8e1175bSopenharmony_ci } 297a8e1175bSopenharmony_ci } else if (opt.mode == MODE_PUBLIC) { 298a8e1175bSopenharmony_ci /* 299a8e1175bSopenharmony_ci * 1.1. Load the key 300a8e1175bSopenharmony_ci */ 301a8e1175bSopenharmony_ci mbedtls_printf("\n . Loading the public key ..."); 302a8e1175bSopenharmony_ci fflush(stdout); 303a8e1175bSopenharmony_ci 304a8e1175bSopenharmony_ci ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename); 305a8e1175bSopenharmony_ci 306a8e1175bSopenharmony_ci if (ret != 0) { 307a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", 308a8e1175bSopenharmony_ci (unsigned int) -ret); 309a8e1175bSopenharmony_ci goto cleanup; 310a8e1175bSopenharmony_ci } 311a8e1175bSopenharmony_ci 312a8e1175bSopenharmony_ci mbedtls_printf(" ok\n"); 313a8e1175bSopenharmony_ci 314a8e1175bSopenharmony_ci mbedtls_printf(" . Key information ...\n"); 315a8e1175bSopenharmony_ci#if defined(MBEDTLS_RSA_C) 316a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) { 317a8e1175bSopenharmony_ci mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk); 318a8e1175bSopenharmony_ci 319a8e1175bSopenharmony_ci if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL, 320a8e1175bSopenharmony_ci NULL, &E)) != 0) { 321a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! could not export RSA parameters\n\n"); 322a8e1175bSopenharmony_ci goto cleanup; 323a8e1175bSopenharmony_ci } 324a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL)); 325a8e1175bSopenharmony_ci MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL)); 326a8e1175bSopenharmony_ci } else 327a8e1175bSopenharmony_ci#endif 328a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_C) 329a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) { 330a8e1175bSopenharmony_ci if (show_ecp_key(mbedtls_pk_ec(pk), 0) != 0) { 331a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! could not export ECC parameters\n\n"); 332a8e1175bSopenharmony_ci goto cleanup; 333a8e1175bSopenharmony_ci } 334a8e1175bSopenharmony_ci } else 335a8e1175bSopenharmony_ci#endif 336a8e1175bSopenharmony_ci { 337a8e1175bSopenharmony_ci mbedtls_printf("Do not know how to print key information for this type\n"); 338a8e1175bSopenharmony_ci goto cleanup; 339a8e1175bSopenharmony_ci } 340a8e1175bSopenharmony_ci } else { 341a8e1175bSopenharmony_ci goto usage; 342a8e1175bSopenharmony_ci } 343a8e1175bSopenharmony_ci 344a8e1175bSopenharmony_ci exit_code = MBEDTLS_EXIT_SUCCESS; 345a8e1175bSopenharmony_ci 346a8e1175bSopenharmony_cicleanup: 347a8e1175bSopenharmony_ci 348a8e1175bSopenharmony_ci#if defined(MBEDTLS_ERROR_C) 349a8e1175bSopenharmony_ci if (exit_code != MBEDTLS_EXIT_SUCCESS) { 350a8e1175bSopenharmony_ci mbedtls_strerror(ret, buf, sizeof(buf)); 351a8e1175bSopenharmony_ci mbedtls_printf(" ! Last error was: %s\n", buf); 352a8e1175bSopenharmony_ci } 353a8e1175bSopenharmony_ci#endif 354a8e1175bSopenharmony_ci 355a8e1175bSopenharmony_ci mbedtls_ctr_drbg_free(&ctr_drbg); 356a8e1175bSopenharmony_ci mbedtls_entropy_free(&entropy); 357a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 358a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 359a8e1175bSopenharmony_ci mbedtls_psa_crypto_free(); 360a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 361a8e1175bSopenharmony_ci mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); 362a8e1175bSopenharmony_ci mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP); 363a8e1175bSopenharmony_ci mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP); 364a8e1175bSopenharmony_ci 365a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 366a8e1175bSopenharmony_ci} 367a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && 368a8e1175bSopenharmony_ci MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ 369