cert_req.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Certificate request generation
  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_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
  22. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
  23. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  24. !defined(MBEDTLS_PEM_WRITE_C)
  25. int main(void)
  26. {
  27. mbedtls_printf("MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
  28. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
  29. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
  30. "not defined.\n");
  31. mbedtls_exit(0);
  32. }
  33. #else
  34. #include "mbedtls/x509_csr.h"
  35. #include "mbedtls/entropy.h"
  36. #include "mbedtls/ctr_drbg.h"
  37. #include "mbedtls/error.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #define DFL_FILENAME "keyfile.key"
  42. #define DFL_PASSWORD NULL
  43. #define DFL_DEBUG_LEVEL 0
  44. #define DFL_OUTPUT_FILENAME "cert.req"
  45. #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
  46. #define DFL_KEY_USAGE 0
  47. #define DFL_FORCE_KEY_USAGE 0
  48. #define DFL_NS_CERT_TYPE 0
  49. #define DFL_FORCE_NS_CERT_TYPE 0
  50. #define DFL_MD_ALG MBEDTLS_MD_SHA256
  51. #define USAGE \
  52. "\n usage: cert_req param=<>...\n" \
  53. "\n acceptable parameters:\n" \
  54. " filename=%%s default: keyfile.key\n" \
  55. " password=%%s default: NULL\n" \
  56. " debug_level=%%d default: 0 (disabled)\n" \
  57. " output_file=%%s default: cert.req\n" \
  58. " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
  59. " san=%%s default: (none)\n" \
  60. " Comma-separated-list of values:\n" \
  61. " DNS:value\n" \
  62. " URI:value\n" \
  63. " IP:value (Only IPv4 is supported)\n" \
  64. " key_usage=%%s default: (empty)\n" \
  65. " Comma-separated-list of values:\n" \
  66. " digital_signature\n" \
  67. " non_repudiation\n" \
  68. " key_encipherment\n" \
  69. " data_encipherment\n" \
  70. " key_agreement\n" \
  71. " key_cert_sign\n" \
  72. " crl_sign\n" \
  73. " force_key_usage=0/1 default: off\n" \
  74. " Add KeyUsage even if it is empty\n" \
  75. " ns_cert_type=%%s default: (empty)\n" \
  76. " Comma-separated-list of values:\n" \
  77. " ssl_client\n" \
  78. " ssl_server\n" \
  79. " email\n" \
  80. " object_signing\n" \
  81. " ssl_ca\n" \
  82. " email_ca\n" \
  83. " object_signing_ca\n" \
  84. " force_ns_cert_type=0/1 default: off\n" \
  85. " Add NsCertType even if it is empty\n" \
  86. " md=%%s default: SHA256\n" \
  87. " possible values:\n" \
  88. " MD5, RIPEMD160, SHA1,\n" \
  89. " SHA224, SHA256, SHA384, SHA512\n" \
  90. "\n"
  91. /*
  92. * global options
  93. */
  94. struct options {
  95. const char *filename; /* filename of the key file */
  96. const char *password; /* password for the key file */
  97. int debug_level; /* level of debugging */
  98. const char *output_file; /* where to store the constructed key file */
  99. const char *subject_name; /* subject name for certificate request */
  100. mbedtls_x509_san_list *san_list; /* subjectAltName for certificate request */
  101. unsigned char key_usage; /* key usage flags */
  102. int force_key_usage; /* Force adding the KeyUsage extension */
  103. unsigned char ns_cert_type; /* NS cert type */
  104. int force_ns_cert_type; /* Force adding NsCertType extension */
  105. mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
  106. } opt;
  107. static void ip_string_to_bytes(const char *str, uint8_t *bytes, int maxBytes)
  108. {
  109. for (int i = 0; i < maxBytes; i++) {
  110. bytes[i] = (uint8_t) strtoul(str, NULL, 16);
  111. str = strchr(str, '.');
  112. if (str == NULL || *str == '\0') {
  113. break;
  114. }
  115. str++;
  116. }
  117. }
  118. int write_certificate_request(mbedtls_x509write_csr *req, const char *output_file,
  119. int (*f_rng)(void *, unsigned char *, size_t),
  120. void *p_rng)
  121. {
  122. int ret;
  123. FILE *f;
  124. unsigned char output_buf[4096];
  125. size_t len = 0;
  126. memset(output_buf, 0, 4096);
  127. if ((ret = mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng)) < 0) {
  128. return ret;
  129. }
  130. len = strlen((char *) output_buf);
  131. if ((f = fopen(output_file, "w")) == NULL) {
  132. return -1;
  133. }
  134. if (fwrite(output_buf, 1, len, f) != len) {
  135. fclose(f);
  136. return -1;
  137. }
  138. fclose(f);
  139. return 0;
  140. }
  141. int main(int argc, char *argv[])
  142. {
  143. int ret = 1;
  144. int exit_code = MBEDTLS_EXIT_FAILURE;
  145. mbedtls_pk_context key;
  146. char buf[1024];
  147. int i;
  148. char *p, *q, *r, *r2;
  149. mbedtls_x509write_csr req;
  150. mbedtls_entropy_context entropy;
  151. mbedtls_ctr_drbg_context ctr_drbg;
  152. const char *pers = "csr example app";
  153. mbedtls_x509_san_list *cur, *prev;
  154. /*
  155. * Set to sane values
  156. */
  157. mbedtls_x509write_csr_init(&req);
  158. mbedtls_pk_init(&key);
  159. mbedtls_ctr_drbg_init(&ctr_drbg);
  160. memset(buf, 0, sizeof(buf));
  161. if (argc < 2) {
  162. usage:
  163. mbedtls_printf(USAGE);
  164. goto exit;
  165. }
  166. opt.filename = DFL_FILENAME;
  167. opt.password = DFL_PASSWORD;
  168. opt.debug_level = DFL_DEBUG_LEVEL;
  169. opt.output_file = DFL_OUTPUT_FILENAME;
  170. opt.subject_name = DFL_SUBJECT_NAME;
  171. opt.key_usage = DFL_KEY_USAGE;
  172. opt.force_key_usage = DFL_FORCE_KEY_USAGE;
  173. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  174. opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
  175. opt.md_alg = DFL_MD_ALG;
  176. opt.san_list = NULL;
  177. for (i = 1; i < argc; i++) {
  178. p = argv[i];
  179. if ((q = strchr(p, '=')) == NULL) {
  180. goto usage;
  181. }
  182. *q++ = '\0';
  183. if (strcmp(p, "filename") == 0) {
  184. opt.filename = q;
  185. } else if (strcmp(p, "password") == 0) {
  186. opt.password = q;
  187. } else if (strcmp(p, "output_file") == 0) {
  188. opt.output_file = q;
  189. } else if (strcmp(p, "debug_level") == 0) {
  190. opt.debug_level = atoi(q);
  191. if (opt.debug_level < 0 || opt.debug_level > 65535) {
  192. goto usage;
  193. }
  194. } else if (strcmp(p, "subject_name") == 0) {
  195. opt.subject_name = q;
  196. } else if (strcmp(p, "san") == 0) {
  197. prev = NULL;
  198. while (q != NULL) {
  199. uint8_t ip[4] = { 0 };
  200. if ((r = strchr(q, ';')) != NULL) {
  201. *r++ = '\0';
  202. }
  203. cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
  204. if (cur == NULL) {
  205. mbedtls_printf("Not enough memory for subjectAltName list\n");
  206. goto usage;
  207. }
  208. cur->next = NULL;
  209. if ((r2 = strchr(q, ':')) != NULL) {
  210. *r2++ = '\0';
  211. }
  212. if (strcmp(q, "URI") == 0) {
  213. cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
  214. } else if (strcmp(q, "DNS") == 0) {
  215. cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
  216. } else if (strcmp(q, "IP") == 0) {
  217. cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
  218. ip_string_to_bytes(r2, ip, 4);
  219. } else {
  220. mbedtls_free(cur);
  221. goto usage;
  222. }
  223. if (strcmp(q, "IP") == 0) {
  224. cur->node.san.unstructured_name.p = (unsigned char *) ip;
  225. cur->node.san.unstructured_name.len = sizeof(ip);
  226. } else {
  227. q = r2;
  228. cur->node.san.unstructured_name.p = (unsigned char *) q;
  229. cur->node.san.unstructured_name.len = strlen(q);
  230. }
  231. if (prev == NULL) {
  232. opt.san_list = cur;
  233. } else {
  234. prev->next = cur;
  235. }
  236. prev = cur;
  237. q = r;
  238. }
  239. } else if (strcmp(p, "md") == 0) {
  240. const mbedtls_md_info_t *md_info =
  241. mbedtls_md_info_from_string(q);
  242. if (md_info == NULL) {
  243. mbedtls_printf("Invalid argument for option %s\n", p);
  244. goto usage;
  245. }
  246. opt.md_alg = mbedtls_md_get_type(md_info);
  247. } else if (strcmp(p, "key_usage") == 0) {
  248. while (q != NULL) {
  249. if ((r = strchr(q, ',')) != NULL) {
  250. *r++ = '\0';
  251. }
  252. if (strcmp(q, "digital_signature") == 0) {
  253. opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  254. } else if (strcmp(q, "non_repudiation") == 0) {
  255. opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
  256. } else if (strcmp(q, "key_encipherment") == 0) {
  257. opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  258. } else if (strcmp(q, "data_encipherment") == 0) {
  259. opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
  260. } else if (strcmp(q, "key_agreement") == 0) {
  261. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  262. } else if (strcmp(q, "key_cert_sign") == 0) {
  263. opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
  264. } else if (strcmp(q, "crl_sign") == 0) {
  265. opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
  266. } else {
  267. goto usage;
  268. }
  269. q = r;
  270. }
  271. } else if (strcmp(p, "force_key_usage") == 0) {
  272. switch (atoi(q)) {
  273. case 0: opt.force_key_usage = 0; break;
  274. case 1: opt.force_key_usage = 1; break;
  275. default: goto usage;
  276. }
  277. } else if (strcmp(p, "ns_cert_type") == 0) {
  278. while (q != NULL) {
  279. if ((r = strchr(q, ',')) != NULL) {
  280. *r++ = '\0';
  281. }
  282. if (strcmp(q, "ssl_client") == 0) {
  283. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
  284. } else if (strcmp(q, "ssl_server") == 0) {
  285. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  286. } else if (strcmp(q, "email") == 0) {
  287. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
  288. } else if (strcmp(q, "object_signing") == 0) {
  289. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
  290. } else if (strcmp(q, "ssl_ca") == 0) {
  291. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
  292. } else if (strcmp(q, "email_ca") == 0) {
  293. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
  294. } else if (strcmp(q, "object_signing_ca") == 0) {
  295. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
  296. } else {
  297. goto usage;
  298. }
  299. q = r;
  300. }
  301. } else if (strcmp(p, "force_ns_cert_type") == 0) {
  302. switch (atoi(q)) {
  303. case 0: opt.force_ns_cert_type = 0; break;
  304. case 1: opt.force_ns_cert_type = 1; break;
  305. default: goto usage;
  306. }
  307. } else {
  308. goto usage;
  309. }
  310. }
  311. /* Set the MD algorithm to use for the signature in the CSR */
  312. mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
  313. /* Set the Key Usage Extension flags in the CSR */
  314. if (opt.key_usage || opt.force_key_usage == 1) {
  315. ret = mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
  316. if (ret != 0) {
  317. mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
  318. goto exit;
  319. }
  320. }
  321. /* Set the Cert Type flags in the CSR */
  322. if (opt.ns_cert_type || opt.force_ns_cert_type == 1) {
  323. ret = mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
  324. if (ret != 0) {
  325. mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d", ret);
  326. goto exit;
  327. }
  328. }
  329. /* Set the SubjectAltName in the CSR */
  330. if (opt.san_list != NULL) {
  331. ret = mbedtls_x509write_csr_set_subject_alternative_name(&req, opt.san_list);
  332. if (ret != 0) {
  333. mbedtls_printf(
  334. " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
  335. ret);
  336. goto exit;
  337. }
  338. }
  339. /*
  340. * 0. Seed the PRNG
  341. */
  342. mbedtls_printf(" . Seeding the random number generator...");
  343. fflush(stdout);
  344. mbedtls_entropy_init(&entropy);
  345. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  346. (const unsigned char *) pers,
  347. strlen(pers))) != 0) {
  348. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
  349. goto exit;
  350. }
  351. mbedtls_printf(" ok\n");
  352. /*
  353. * 1.0. Check the subject name for validity
  354. */
  355. mbedtls_printf(" . Checking subject name...");
  356. fflush(stdout);
  357. if ((ret = mbedtls_x509write_csr_set_subject_name(&req, opt.subject_name)) != 0) {
  358. mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
  359. goto exit;
  360. }
  361. mbedtls_printf(" ok\n");
  362. /*
  363. * 1.1. Load the key
  364. */
  365. mbedtls_printf(" . Loading the private key ...");
  366. fflush(stdout);
  367. ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
  368. mbedtls_ctr_drbg_random, &ctr_drbg);
  369. if (ret != 0) {
  370. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d", ret);
  371. goto exit;
  372. }
  373. mbedtls_x509write_csr_set_key(&req, &key);
  374. mbedtls_printf(" ok\n");
  375. /*
  376. * 1.2. Writing the request
  377. */
  378. mbedtls_printf(" . Writing the certificate request ...");
  379. fflush(stdout);
  380. if ((ret = write_certificate_request(&req, opt.output_file,
  381. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  382. mbedtls_printf(" failed\n ! write_certificate_request %d", ret);
  383. goto exit;
  384. }
  385. mbedtls_printf(" ok\n");
  386. exit_code = MBEDTLS_EXIT_SUCCESS;
  387. exit:
  388. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  389. #ifdef MBEDTLS_ERROR_C
  390. mbedtls_strerror(ret, buf, sizeof(buf));
  391. mbedtls_printf(" - %s\n", buf);
  392. #else
  393. mbedtls_printf("\n");
  394. #endif
  395. }
  396. mbedtls_x509write_csr_free(&req);
  397. mbedtls_pk_free(&key);
  398. mbedtls_ctr_drbg_free(&ctr_drbg);
  399. mbedtls_entropy_free(&entropy);
  400. cur = opt.san_list;
  401. while (cur != NULL) {
  402. prev = cur;
  403. cur = cur->next;
  404. mbedtls_free(prev);
  405. }
  406. mbedtls_exit(exit_code);
  407. }
  408. #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  409. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */