key_app_writer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Key writing 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_PK_PARSE_C) && defined(MBEDTLS_PK_WRITE_C) && \
  22. defined(MBEDTLS_FS_IO) && \
  23. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
  24. #include "mbedtls/error.h"
  25. #include "mbedtls/pk.h"
  26. #include "mbedtls/error.h"
  27. #include "mbedtls/entropy.h"
  28. #include "mbedtls/ctr_drbg.h"
  29. #include <stdio.h>
  30. #include <string.h>
  31. #endif
  32. #if defined(MBEDTLS_PEM_WRITE_C)
  33. #define USAGE_OUT \
  34. " output_file=%%s default: keyfile.pem\n" \
  35. " output_format=pem|der default: pem\n"
  36. #else
  37. #define USAGE_OUT \
  38. " output_file=%%s default: keyfile.der\n" \
  39. " output_format=der default: der\n"
  40. #endif
  41. #if defined(MBEDTLS_PEM_WRITE_C)
  42. #define DFL_OUTPUT_FILENAME "keyfile.pem"
  43. #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
  44. #else
  45. #define DFL_OUTPUT_FILENAME "keyfile.der"
  46. #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
  47. #endif
  48. #define DFL_MODE MODE_NONE
  49. #define DFL_FILENAME "keyfile.key"
  50. #define DFL_DEBUG_LEVEL 0
  51. #define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
  52. #define MODE_NONE 0
  53. #define MODE_PRIVATE 1
  54. #define MODE_PUBLIC 2
  55. #define OUTPUT_MODE_NONE 0
  56. #define OUTPUT_MODE_PRIVATE 1
  57. #define OUTPUT_MODE_PUBLIC 2
  58. #define OUTPUT_FORMAT_PEM 0
  59. #define OUTPUT_FORMAT_DER 1
  60. #define USAGE \
  61. "\n usage: key_app_writer param=<>...\n" \
  62. "\n acceptable parameters:\n" \
  63. " mode=private|public default: none\n" \
  64. " filename=%%s default: keyfile.key\n" \
  65. " output_mode=private|public default: none\n" \
  66. USAGE_OUT \
  67. "\n"
  68. #if !defined(MBEDTLS_PK_PARSE_C) || \
  69. !defined(MBEDTLS_PK_WRITE_C) || \
  70. !defined(MBEDTLS_FS_IO) || \
  71. !defined(MBEDTLS_ENTROPY_C) || \
  72. !defined(MBEDTLS_CTR_DRBG_C)
  73. int main(void)
  74. {
  75. mbedtls_printf("MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or "
  76. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  77. "MBEDTLS_FS_IO not defined.\n");
  78. mbedtls_exit(0);
  79. }
  80. #else
  81. /*
  82. * global options
  83. */
  84. struct options {
  85. int mode; /* the mode to run the application in */
  86. const char *filename; /* filename of the key file */
  87. int output_mode; /* the output mode to use */
  88. const char *output_file; /* where to store the constructed key file */
  89. int output_format; /* the output format to use */
  90. } opt;
  91. static int write_public_key(mbedtls_pk_context *key, const char *output_file)
  92. {
  93. int ret;
  94. FILE *f;
  95. unsigned char output_buf[16000];
  96. unsigned char *c = output_buf;
  97. size_t len = 0;
  98. memset(output_buf, 0, 16000);
  99. #if defined(MBEDTLS_PEM_WRITE_C)
  100. if (opt.output_format == OUTPUT_FORMAT_PEM) {
  101. if ((ret = mbedtls_pk_write_pubkey_pem(key, output_buf, 16000)) != 0) {
  102. return ret;
  103. }
  104. len = strlen((char *) output_buf);
  105. } else
  106. #endif
  107. {
  108. if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf, 16000)) < 0) {
  109. return ret;
  110. }
  111. len = ret;
  112. c = output_buf + sizeof(output_buf) - len;
  113. }
  114. if ((f = fopen(output_file, "w")) == NULL) {
  115. return -1;
  116. }
  117. if (fwrite(c, 1, len, f) != len) {
  118. fclose(f);
  119. return -1;
  120. }
  121. fclose(f);
  122. return 0;
  123. }
  124. static int write_private_key(mbedtls_pk_context *key, const char *output_file)
  125. {
  126. int ret;
  127. FILE *f;
  128. unsigned char output_buf[16000];
  129. unsigned char *c = output_buf;
  130. size_t len = 0;
  131. memset(output_buf, 0, 16000);
  132. #if defined(MBEDTLS_PEM_WRITE_C)
  133. if (opt.output_format == OUTPUT_FORMAT_PEM) {
  134. if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
  135. return ret;
  136. }
  137. len = strlen((char *) output_buf);
  138. } else
  139. #endif
  140. {
  141. if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
  142. return ret;
  143. }
  144. len = ret;
  145. c = output_buf + sizeof(output_buf) - len;
  146. }
  147. if ((f = fopen(output_file, "w")) == NULL) {
  148. return -1;
  149. }
  150. if (fwrite(c, 1, len, f) != len) {
  151. fclose(f);
  152. return -1;
  153. }
  154. fclose(f);
  155. return 0;
  156. }
  157. int main(int argc, char *argv[])
  158. {
  159. int ret = 1;
  160. int exit_code = MBEDTLS_EXIT_FAILURE;
  161. #if defined(MBEDTLS_ERROR_C)
  162. char buf[200];
  163. #endif
  164. int i;
  165. char *p, *q;
  166. const char *pers = "pkey/key_app";
  167. mbedtls_entropy_context entropy;
  168. mbedtls_ctr_drbg_context ctr_drbg;
  169. mbedtls_pk_context key;
  170. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  171. /*
  172. * Set to sane values
  173. */
  174. mbedtls_entropy_init(&entropy);
  175. mbedtls_ctr_drbg_init(&ctr_drbg);
  176. mbedtls_pk_init(&key);
  177. #if defined(MBEDTLS_ERROR_C)
  178. memset(buf, 0, sizeof(buf));
  179. #endif
  180. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  181. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
  182. mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
  183. if (argc < 2) {
  184. usage:
  185. mbedtls_printf(USAGE);
  186. goto exit;
  187. }
  188. opt.mode = DFL_MODE;
  189. opt.filename = DFL_FILENAME;
  190. opt.output_mode = DFL_OUTPUT_MODE;
  191. opt.output_file = DFL_OUTPUT_FILENAME;
  192. opt.output_format = DFL_OUTPUT_FORMAT;
  193. for (i = 1; i < argc; i++) {
  194. p = argv[i];
  195. if ((q = strchr(p, '=')) == NULL) {
  196. goto usage;
  197. }
  198. *q++ = '\0';
  199. if (strcmp(p, "mode") == 0) {
  200. if (strcmp(q, "private") == 0) {
  201. opt.mode = MODE_PRIVATE;
  202. } else if (strcmp(q, "public") == 0) {
  203. opt.mode = MODE_PUBLIC;
  204. } else {
  205. goto usage;
  206. }
  207. } else if (strcmp(p, "output_mode") == 0) {
  208. if (strcmp(q, "private") == 0) {
  209. opt.output_mode = OUTPUT_MODE_PRIVATE;
  210. } else if (strcmp(q, "public") == 0) {
  211. opt.output_mode = OUTPUT_MODE_PUBLIC;
  212. } else {
  213. goto usage;
  214. }
  215. } else if (strcmp(p, "output_format") == 0) {
  216. #if defined(MBEDTLS_PEM_WRITE_C)
  217. if (strcmp(q, "pem") == 0) {
  218. opt.output_format = OUTPUT_FORMAT_PEM;
  219. } else
  220. #endif
  221. if (strcmp(q, "der") == 0) {
  222. opt.output_format = OUTPUT_FORMAT_DER;
  223. } else {
  224. goto usage;
  225. }
  226. } else if (strcmp(p, "filename") == 0) {
  227. opt.filename = q;
  228. } else if (strcmp(p, "output_file") == 0) {
  229. opt.output_file = q;
  230. } else {
  231. goto usage;
  232. }
  233. }
  234. if (opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE) {
  235. mbedtls_printf("\nCannot output a key without reading one.\n");
  236. goto exit;
  237. }
  238. if (opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE) {
  239. mbedtls_printf("\nCannot output a private key from a public key.\n");
  240. goto exit;
  241. }
  242. if (opt.mode == MODE_PRIVATE) {
  243. /*
  244. * 1.1. Load the key
  245. */
  246. mbedtls_printf("\n . Loading the private key ...");
  247. fflush(stdout);
  248. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  249. (const unsigned char *) pers,
  250. strlen(pers))) != 0) {
  251. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  252. (unsigned int) -ret);
  253. goto exit;
  254. }
  255. ret = mbedtls_pk_parse_keyfile(&key, opt.filename, NULL,
  256. mbedtls_ctr_drbg_random, &ctr_drbg);
  257. if (ret != 0) {
  258. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x",
  259. (unsigned int) -ret);
  260. goto exit;
  261. }
  262. mbedtls_printf(" ok\n");
  263. /*
  264. * 1.2 Print the key
  265. */
  266. mbedtls_printf(" . Key information ...\n");
  267. #if defined(MBEDTLS_RSA_C)
  268. if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
  269. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
  270. if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
  271. (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
  272. mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
  273. goto exit;
  274. }
  275. mbedtls_mpi_write_file("N: ", &N, 16, NULL);
  276. mbedtls_mpi_write_file("E: ", &E, 16, NULL);
  277. mbedtls_mpi_write_file("D: ", &D, 16, NULL);
  278. mbedtls_mpi_write_file("P: ", &P, 16, NULL);
  279. mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
  280. mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
  281. mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
  282. mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
  283. } else
  284. #endif
  285. #if defined(MBEDTLS_ECP_C)
  286. if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
  287. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
  288. mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
  289. mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
  290. mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
  291. mbedtls_mpi_write_file("D : ", &ecp->MBEDTLS_PRIVATE(d), 16, NULL);
  292. } else
  293. #endif
  294. mbedtls_printf("key type not supported yet\n");
  295. } else if (opt.mode == MODE_PUBLIC) {
  296. /*
  297. * 1.1. Load the key
  298. */
  299. mbedtls_printf("\n . Loading the public key ...");
  300. fflush(stdout);
  301. ret = mbedtls_pk_parse_public_keyfile(&key, opt.filename);
  302. if (ret != 0) {
  303. mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_key returned -0x%04x",
  304. (unsigned int) -ret);
  305. goto exit;
  306. }
  307. mbedtls_printf(" ok\n");
  308. /*
  309. * 1.2 Print the key
  310. */
  311. mbedtls_printf(" . Key information ...\n");
  312. #if defined(MBEDTLS_RSA_C)
  313. if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
  314. mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
  315. if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
  316. NULL, &E)) != 0) {
  317. mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
  318. goto exit;
  319. }
  320. mbedtls_mpi_write_file("N: ", &N, 16, NULL);
  321. mbedtls_mpi_write_file("E: ", &E, 16, NULL);
  322. } else
  323. #endif
  324. #if defined(MBEDTLS_ECP_C)
  325. if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
  326. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(key);
  327. mbedtls_mpi_write_file("Q(X): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, NULL);
  328. mbedtls_mpi_write_file("Q(Y): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, NULL);
  329. mbedtls_mpi_write_file("Q(Z): ", &ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, NULL);
  330. } else
  331. #endif
  332. mbedtls_printf("key type not supported yet\n");
  333. } else {
  334. goto usage;
  335. }
  336. if (opt.output_mode == OUTPUT_MODE_PUBLIC) {
  337. write_public_key(&key, opt.output_file);
  338. }
  339. if (opt.output_mode == OUTPUT_MODE_PRIVATE) {
  340. write_private_key(&key, opt.output_file);
  341. }
  342. exit_code = MBEDTLS_EXIT_SUCCESS;
  343. exit:
  344. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  345. #ifdef MBEDTLS_ERROR_C
  346. mbedtls_strerror(ret, buf, sizeof(buf));
  347. mbedtls_printf(" - %s\n", buf);
  348. #else
  349. mbedtls_printf("\n");
  350. #endif
  351. }
  352. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  353. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
  354. mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
  355. mbedtls_pk_free(&key);
  356. mbedtls_ctr_drbg_free(&ctr_drbg);
  357. mbedtls_entropy_free(&entropy);
  358. mbedtls_exit(exit_code);
  359. }
  360. #endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO &&
  361. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */