key_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Key reading application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_BIGNUM_C) && \
  22. defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
  23. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
  24. #include "mbedtls/error.h"
  25. #include "mbedtls/rsa.h"
  26. #include "mbedtls/pk.h"
  27. #include "mbedtls/entropy.h"
  28. #include "mbedtls/ctr_drbg.h"
  29. #include <string.h>
  30. #endif
  31. #define MODE_NONE 0
  32. #define MODE_PRIVATE 1
  33. #define MODE_PUBLIC 2
  34. #define DFL_MODE MODE_NONE
  35. #define DFL_FILENAME "keyfile.key"
  36. #define DFL_PASSWORD ""
  37. #define DFL_PASSWORD_FILE ""
  38. #define DFL_DEBUG_LEVEL 0
  39. #define USAGE \
  40. "\n usage: key_app param=<>...\n" \
  41. "\n acceptable parameters:\n" \
  42. " mode=private|public default: none\n" \
  43. " filename=%%s default: keyfile.key\n" \
  44. " password=%%s default: \"\"\n" \
  45. " password_file=%%s default: \"\"\n" \
  46. "\n"
  47. #if !defined(MBEDTLS_BIGNUM_C) || \
  48. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  49. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
  50. int main(void)
  51. {
  52. mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
  53. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  54. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  55. mbedtls_exit(0);
  56. }
  57. #else
  58. /*
  59. * global options
  60. */
  61. struct options {
  62. int mode; /* the mode to run the application in */
  63. const char *filename; /* filename of the key file */
  64. const char *password; /* password for the private key */
  65. const char *password_file; /* password_file for the private key */
  66. } opt;
  67. int main(int argc, char *argv[])
  68. {
  69. int ret = 1;
  70. int exit_code = MBEDTLS_EXIT_FAILURE;
  71. char buf[1024];
  72. int i;
  73. char *p, *q;
  74. const char *pers = "pkey/key_app";
  75. mbedtls_entropy_context entropy;
  76. mbedtls_ctr_drbg_context ctr_drbg;
  77. mbedtls_pk_context pk;
  78. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  79. /*
  80. * Set to sane values
  81. */
  82. mbedtls_entropy_init(&entropy);
  83. mbedtls_ctr_drbg_init(&ctr_drbg);
  84. mbedtls_pk_init(&pk);
  85. memset(buf, 0, sizeof(buf));
  86. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  87. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
  88. mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
  89. if (argc < 2) {
  90. usage:
  91. mbedtls_printf(USAGE);
  92. goto cleanup;
  93. }
  94. opt.mode = DFL_MODE;
  95. opt.filename = DFL_FILENAME;
  96. opt.password = DFL_PASSWORD;
  97. opt.password_file = DFL_PASSWORD_FILE;
  98. for (i = 1; i < argc; i++) {
  99. p = argv[i];
  100. if ((q = strchr(p, '=')) == NULL) {
  101. goto usage;
  102. }
  103. *q++ = '\0';
  104. if (strcmp(p, "mode") == 0) {
  105. if (strcmp(q, "private") == 0) {
  106. opt.mode = MODE_PRIVATE;
  107. } else if (strcmp(q, "public") == 0) {
  108. opt.mode = MODE_PUBLIC;
  109. } else {
  110. goto usage;
  111. }
  112. } else if (strcmp(p, "filename") == 0) {
  113. opt.filename = q;
  114. } else if (strcmp(p, "password") == 0) {
  115. opt.password = q;
  116. } else if (strcmp(p, "password_file") == 0) {
  117. opt.password_file = q;
  118. } else {
  119. goto usage;
  120. }
  121. }
  122. if (opt.mode == MODE_PRIVATE) {
  123. if (strlen(opt.password) && strlen(opt.password_file)) {
  124. mbedtls_printf("Error: cannot have both password and password_file\n");
  125. goto usage;
  126. }
  127. if (strlen(opt.password_file)) {
  128. FILE *f;
  129. mbedtls_printf("\n . Loading the password file ...");
  130. if ((f = fopen(opt.password_file, "rb")) == NULL) {
  131. mbedtls_printf(" failed\n ! fopen returned NULL\n");
  132. goto cleanup;
  133. }
  134. if (fgets(buf, sizeof(buf), f) == NULL) {
  135. fclose(f);
  136. mbedtls_printf("Error: fgets() failed to retrieve password\n");
  137. goto cleanup;
  138. }
  139. fclose(f);
  140. i = (int) strlen(buf);
  141. if (buf[i - 1] == '\n') {
  142. buf[i - 1] = '\0';
  143. }
  144. if (buf[i - 2] == '\r') {
  145. buf[i - 2] = '\0';
  146. }
  147. opt.password = buf;
  148. }
  149. /*
  150. * 1.1. Load the key
  151. */
  152. mbedtls_printf("\n . Loading the private key ...");
  153. fflush(stdout);
  154. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  155. (const unsigned char *) pers,
  156. strlen(pers))) != 0) {
  157. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  158. (unsigned int) -ret);
  159. goto cleanup;
  160. }
  161. ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
  162. mbedtls_ctr_drbg_random, &ctr_drbg);
  163. if (ret != 0) {
  164. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
  165. (unsigned int) -ret);
  166. goto cleanup;
  167. }
  168. mbedtls_printf(" ok\n");
  169. /*
  170. * 1.2 Print the key
  171. */
  172. mbedtls_printf(" . Key information ...\n");
  173. #if defined(MBEDTLS_RSA_C)
  174. if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
  175. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
  176. if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
  177. (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
  178. mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
  179. goto cleanup;
  180. }
  181. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
  182. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
  183. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
  184. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
  185. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
  186. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
  187. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
  188. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
  189. } else
  190. #endif
  191. #if defined(MBEDTLS_ECP_C)
  192. if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
  193. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
  194. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
  195. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
  196. NULL));
  197. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
  198. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
  199. NULL));
  200. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
  201. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
  202. NULL));
  203. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL));
  204. } else
  205. #endif
  206. {
  207. mbedtls_printf("Do not know how to print key information for this type\n");
  208. goto cleanup;
  209. }
  210. } else if (opt.mode == MODE_PUBLIC) {
  211. /*
  212. * 1.1. Load the key
  213. */
  214. mbedtls_printf("\n . Loading the public key ...");
  215. fflush(stdout);
  216. ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
  217. if (ret != 0) {
  218. mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
  219. (unsigned int) -ret);
  220. goto cleanup;
  221. }
  222. mbedtls_printf(" ok\n");
  223. mbedtls_printf(" . Key information ...\n");
  224. #if defined(MBEDTLS_RSA_C)
  225. if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
  226. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
  227. if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
  228. NULL, &E)) != 0) {
  229. mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
  230. goto cleanup;
  231. }
  232. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
  233. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
  234. } else
  235. #endif
  236. #if defined(MBEDTLS_ECP_C)
  237. if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
  238. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
  239. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ",
  240. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16,
  241. NULL));
  242. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ",
  243. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16,
  244. NULL));
  245. MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ",
  246. &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16,
  247. NULL));
  248. } else
  249. #endif
  250. {
  251. mbedtls_printf("Do not know how to print key information for this type\n");
  252. goto cleanup;
  253. }
  254. } else {
  255. goto usage;
  256. }
  257. exit_code = MBEDTLS_EXIT_SUCCESS;
  258. cleanup:
  259. #if defined(MBEDTLS_ERROR_C)
  260. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  261. mbedtls_strerror(ret, buf, sizeof(buf));
  262. mbedtls_printf(" ! Last error was: %s\n", buf);
  263. }
  264. #endif
  265. mbedtls_ctr_drbg_free(&ctr_drbg);
  266. mbedtls_entropy_free(&entropy);
  267. mbedtls_pk_free(&pk);
  268. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  269. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
  270. mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
  271. mbedtls_exit(exit_code);
  272. }
  273. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  274. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */