cert_write.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * Certificate generation and signing
  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_CRT_WRITE_C) || \
  22. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  23. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  24. !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
  25. !defined(MBEDTLS_PEM_WRITE_C)
  26. int main(void)
  27. {
  28. mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
  29. "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
  30. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  31. "MBEDTLS_ERROR_C not defined.\n");
  32. mbedtls_exit(0);
  33. }
  34. #else
  35. #include "mbedtls/x509_crt.h"
  36. #include "mbedtls/x509_csr.h"
  37. #include "mbedtls/oid.h"
  38. #include "mbedtls/entropy.h"
  39. #include "mbedtls/ctr_drbg.h"
  40. #include "mbedtls/md.h"
  41. #include "mbedtls/error.h"
  42. #include "test/helpers.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #define SET_OID(x, oid) \
  48. do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
  49. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  50. #define USAGE_CSR \
  51. " request_file=%%s default: (empty)\n" \
  52. " If request_file is specified, subject_key,\n" \
  53. " subject_pwd and subject_name are ignored!\n"
  54. #else
  55. #define USAGE_CSR ""
  56. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  57. #define FORMAT_PEM 0
  58. #define FORMAT_DER 1
  59. #define DFL_ISSUER_CRT ""
  60. #define DFL_REQUEST_FILE ""
  61. #define DFL_SUBJECT_KEY "subject.key"
  62. #define DFL_ISSUER_KEY "ca.key"
  63. #define DFL_SUBJECT_PWD ""
  64. #define DFL_ISSUER_PWD ""
  65. #define DFL_OUTPUT_FILENAME "cert.crt"
  66. #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
  67. #define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
  68. #define DFL_NOT_BEFORE "20010101000000"
  69. #define DFL_NOT_AFTER "20301231235959"
  70. #define DFL_SERIAL "1"
  71. #define DFL_SERIAL_HEX "1"
  72. #define DFL_SELFSIGN 0
  73. #define DFL_IS_CA 0
  74. #define DFL_MAX_PATHLEN -1
  75. #define DFL_SIG_ALG MBEDTLS_MD_SHA256
  76. #define DFL_KEY_USAGE 0
  77. #define DFL_EXT_KEY_USAGE NULL
  78. #define DFL_NS_CERT_TYPE 0
  79. #define DFL_VERSION 3
  80. #define DFL_AUTH_IDENT 1
  81. #define DFL_SUBJ_IDENT 1
  82. #define DFL_CONSTRAINTS 1
  83. #define DFL_DIGEST MBEDTLS_MD_SHA256
  84. #define DFL_FORMAT FORMAT_PEM
  85. #define USAGE \
  86. "\n usage: cert_write param=<>...\n" \
  87. "\n acceptable parameters:\n" \
  88. USAGE_CSR \
  89. " subject_key=%%s default: subject.key\n" \
  90. " subject_pwd=%%s default: (empty)\n" \
  91. " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
  92. "\n" \
  93. " issuer_crt=%%s default: (empty)\n" \
  94. " If issuer_crt is specified, issuer_name is\n" \
  95. " ignored!\n" \
  96. " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
  97. "\n" \
  98. " selfsign=%%d default: 0 (false)\n" \
  99. " If selfsign is enabled, issuer_name and\n" \
  100. " issuer_key are required (issuer_crt and\n" \
  101. " subject_* are ignored\n" \
  102. " issuer_key=%%s default: ca.key\n" \
  103. " issuer_pwd=%%s default: (empty)\n" \
  104. " output_file=%%s default: cert.crt\n" \
  105. " serial=%%s default: 1\n" \
  106. " In decimal format; it can be used as\n" \
  107. " alternative to serial_hex, but it's\n" \
  108. " limited in max length to\n" \
  109. " unsigned long long int\n" \
  110. " serial_hex=%%s default: 1\n" \
  111. " In hex format; it can be used as\n" \
  112. " alternative to serial\n" \
  113. " not_before=%%s default: 20010101000000\n" \
  114. " not_after=%%s default: 20301231235959\n" \
  115. " is_ca=%%d default: 0 (disabled)\n" \
  116. " max_pathlen=%%d default: -1 (none)\n" \
  117. " md=%%s default: SHA256\n" \
  118. " Supported values (if enabled):\n" \
  119. " MD5, RIPEMD160, SHA1,\n" \
  120. " SHA224, SHA256, SHA384, SHA512\n" \
  121. " version=%%d default: 3\n" \
  122. " Possible values: 1, 2, 3\n" \
  123. " subject_identifier=%%s default: 1\n" \
  124. " Possible values: 0, 1\n" \
  125. " (Considered for v3 only)\n" \
  126. " authority_identifier=%%s default: 1\n" \
  127. " Possible values: 0, 1\n" \
  128. " (Considered for v3 only)\n" \
  129. " basic_constraints=%%d default: 1\n" \
  130. " Possible values: 0, 1\n" \
  131. " (Considered for v3 only)\n" \
  132. " key_usage=%%s default: (empty)\n" \
  133. " Comma-separated-list of values:\n" \
  134. " digital_signature\n" \
  135. " non_repudiation\n" \
  136. " key_encipherment\n" \
  137. " data_encipherment\n" \
  138. " key_agreement\n" \
  139. " key_cert_sign\n" \
  140. " crl_sign\n" \
  141. " (Considered for v3 only)\n" \
  142. " ext_key_usage=%%s default: (empty)\n" \
  143. " Comma-separated-list of values:\n" \
  144. " serverAuth\n" \
  145. " clientAuth\n" \
  146. " codeSigning\n" \
  147. " emailProtection\n" \
  148. " timeStamping\n" \
  149. " OCSPSigning\n" \
  150. " ns_cert_type=%%s default: (empty)\n" \
  151. " Comma-separated-list of values:\n" \
  152. " ssl_client\n" \
  153. " ssl_server\n" \
  154. " email\n" \
  155. " object_signing\n" \
  156. " ssl_ca\n" \
  157. " email_ca\n" \
  158. " object_signing_ca\n" \
  159. " format=pem|der default: pem\n" \
  160. "\n"
  161. typedef enum {
  162. SERIAL_FRMT_UNSPEC,
  163. SERIAL_FRMT_DEC,
  164. SERIAL_FRMT_HEX
  165. } serial_format_t;
  166. /*
  167. * global options
  168. */
  169. struct options {
  170. const char *issuer_crt; /* filename of the issuer certificate */
  171. const char *request_file; /* filename of the certificate request */
  172. const char *subject_key; /* filename of the subject key file */
  173. const char *issuer_key; /* filename of the issuer key file */
  174. const char *subject_pwd; /* password for the subject key file */
  175. const char *issuer_pwd; /* password for the issuer key file */
  176. const char *output_file; /* where to store the constructed CRT */
  177. const char *subject_name; /* subject name for certificate */
  178. const char *issuer_name; /* issuer name for certificate */
  179. const char *not_before; /* validity period not before */
  180. const char *not_after; /* validity period not after */
  181. const char *serial; /* serial number string (decimal) */
  182. const char *serial_hex; /* serial number string (hex) */
  183. int selfsign; /* selfsign the certificate */
  184. int is_ca; /* is a CA certificate */
  185. int max_pathlen; /* maximum CA path length */
  186. int authority_identifier; /* add authority identifier to CRT */
  187. int subject_identifier; /* add subject identifier to CRT */
  188. int basic_constraints; /* add basic constraints ext to CRT */
  189. int version; /* CRT version */
  190. mbedtls_md_type_t md; /* Hash used for signing */
  191. unsigned char key_usage; /* key usage flags */
  192. mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
  193. unsigned char ns_cert_type; /* NS cert type */
  194. int format; /* format */
  195. } opt;
  196. int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
  197. int (*f_rng)(void *, unsigned char *, size_t),
  198. void *p_rng)
  199. {
  200. int ret;
  201. FILE *f;
  202. unsigned char output_buf[4096];
  203. unsigned char *output_start;
  204. size_t len = 0;
  205. memset(output_buf, 0, 4096);
  206. if (opt.format == FORMAT_DER) {
  207. ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
  208. f_rng, p_rng);
  209. if (ret < 0) {
  210. return ret;
  211. }
  212. len = ret;
  213. output_start = output_buf + 4096 - len;
  214. } else {
  215. ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
  216. f_rng, p_rng);
  217. if (ret < 0) {
  218. return ret;
  219. }
  220. len = strlen((char *) output_buf);
  221. output_start = output_buf;
  222. }
  223. if ((f = fopen(output_file, "w")) == NULL) {
  224. return -1;
  225. }
  226. if (fwrite(output_start, 1, len, f) != len) {
  227. fclose(f);
  228. return -1;
  229. }
  230. fclose(f);
  231. return 0;
  232. }
  233. int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
  234. const char *ibuf, size_t *len)
  235. {
  236. unsigned long long int dec;
  237. unsigned int remaining_bytes = sizeof(dec);
  238. unsigned char *p = obuf;
  239. unsigned char val;
  240. char *end_ptr = NULL;
  241. errno = 0;
  242. dec = strtoull(ibuf, &end_ptr, 10);
  243. if ((errno != 0) || (end_ptr == ibuf)) {
  244. return -1;
  245. }
  246. *len = 0;
  247. while (remaining_bytes > 0) {
  248. if (obufmax < (*len + 1)) {
  249. return -1;
  250. }
  251. val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
  252. /* Skip leading zeros */
  253. if ((val != 0) || (*len != 0)) {
  254. *p = val;
  255. (*len)++;
  256. p++;
  257. }
  258. remaining_bytes--;
  259. }
  260. return 0;
  261. }
  262. int main(int argc, char *argv[])
  263. {
  264. int ret = 1;
  265. int exit_code = MBEDTLS_EXIT_FAILURE;
  266. mbedtls_x509_crt issuer_crt;
  267. mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
  268. mbedtls_pk_context *issuer_key = &loaded_issuer_key,
  269. *subject_key = &loaded_subject_key;
  270. char buf[1024];
  271. char issuer_name[256];
  272. int i;
  273. char *p, *q, *r;
  274. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  275. char subject_name[256];
  276. mbedtls_x509_csr csr;
  277. #endif
  278. mbedtls_x509write_cert crt;
  279. serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
  280. unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
  281. size_t serial_len;
  282. mbedtls_asn1_sequence *ext_key_usage;
  283. mbedtls_entropy_context entropy;
  284. mbedtls_ctr_drbg_context ctr_drbg;
  285. const char *pers = "crt example app";
  286. /*
  287. * Set to sane values
  288. */
  289. mbedtls_x509write_crt_init(&crt);
  290. mbedtls_pk_init(&loaded_issuer_key);
  291. mbedtls_pk_init(&loaded_subject_key);
  292. mbedtls_ctr_drbg_init(&ctr_drbg);
  293. mbedtls_entropy_init(&entropy);
  294. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  295. mbedtls_x509_csr_init(&csr);
  296. #endif
  297. mbedtls_x509_crt_init(&issuer_crt);
  298. memset(buf, 0, sizeof(buf));
  299. memset(serial, 0, sizeof(serial));
  300. if (argc < 2) {
  301. usage:
  302. mbedtls_printf(USAGE);
  303. goto exit;
  304. }
  305. opt.issuer_crt = DFL_ISSUER_CRT;
  306. opt.request_file = DFL_REQUEST_FILE;
  307. opt.subject_key = DFL_SUBJECT_KEY;
  308. opt.issuer_key = DFL_ISSUER_KEY;
  309. opt.subject_pwd = DFL_SUBJECT_PWD;
  310. opt.issuer_pwd = DFL_ISSUER_PWD;
  311. opt.output_file = DFL_OUTPUT_FILENAME;
  312. opt.subject_name = DFL_SUBJECT_NAME;
  313. opt.issuer_name = DFL_ISSUER_NAME;
  314. opt.not_before = DFL_NOT_BEFORE;
  315. opt.not_after = DFL_NOT_AFTER;
  316. opt.serial = DFL_SERIAL;
  317. opt.serial_hex = DFL_SERIAL_HEX;
  318. opt.selfsign = DFL_SELFSIGN;
  319. opt.is_ca = DFL_IS_CA;
  320. opt.max_pathlen = DFL_MAX_PATHLEN;
  321. opt.key_usage = DFL_KEY_USAGE;
  322. opt.ext_key_usage = DFL_EXT_KEY_USAGE;
  323. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  324. opt.version = DFL_VERSION - 1;
  325. opt.md = DFL_DIGEST;
  326. opt.subject_identifier = DFL_SUBJ_IDENT;
  327. opt.authority_identifier = DFL_AUTH_IDENT;
  328. opt.basic_constraints = DFL_CONSTRAINTS;
  329. opt.format = DFL_FORMAT;
  330. for (i = 1; i < argc; i++) {
  331. p = argv[i];
  332. if ((q = strchr(p, '=')) == NULL) {
  333. goto usage;
  334. }
  335. *q++ = '\0';
  336. if (strcmp(p, "request_file") == 0) {
  337. opt.request_file = q;
  338. } else if (strcmp(p, "subject_key") == 0) {
  339. opt.subject_key = q;
  340. } else if (strcmp(p, "issuer_key") == 0) {
  341. opt.issuer_key = q;
  342. } else if (strcmp(p, "subject_pwd") == 0) {
  343. opt.subject_pwd = q;
  344. } else if (strcmp(p, "issuer_pwd") == 0) {
  345. opt.issuer_pwd = q;
  346. } else if (strcmp(p, "issuer_crt") == 0) {
  347. opt.issuer_crt = q;
  348. } else if (strcmp(p, "output_file") == 0) {
  349. opt.output_file = q;
  350. } else if (strcmp(p, "subject_name") == 0) {
  351. opt.subject_name = q;
  352. } else if (strcmp(p, "issuer_name") == 0) {
  353. opt.issuer_name = q;
  354. } else if (strcmp(p, "not_before") == 0) {
  355. opt.not_before = q;
  356. } else if (strcmp(p, "not_after") == 0) {
  357. opt.not_after = q;
  358. } else if (strcmp(p, "serial") == 0) {
  359. if (serial_frmt != SERIAL_FRMT_UNSPEC) {
  360. mbedtls_printf("Invalid attempt to set the serial more than once\n");
  361. goto usage;
  362. }
  363. serial_frmt = SERIAL_FRMT_DEC;
  364. opt.serial = q;
  365. } else if (strcmp(p, "serial_hex") == 0) {
  366. if (serial_frmt != SERIAL_FRMT_UNSPEC) {
  367. mbedtls_printf("Invalid attempt to set the serial more than once\n");
  368. goto usage;
  369. }
  370. serial_frmt = SERIAL_FRMT_HEX;
  371. opt.serial_hex = q;
  372. } else if (strcmp(p, "authority_identifier") == 0) {
  373. opt.authority_identifier = atoi(q);
  374. if (opt.authority_identifier != 0 &&
  375. opt.authority_identifier != 1) {
  376. mbedtls_printf("Invalid argument for option %s\n", p);
  377. goto usage;
  378. }
  379. } else if (strcmp(p, "subject_identifier") == 0) {
  380. opt.subject_identifier = atoi(q);
  381. if (opt.subject_identifier != 0 &&
  382. opt.subject_identifier != 1) {
  383. mbedtls_printf("Invalid argument for option %s\n", p);
  384. goto usage;
  385. }
  386. } else if (strcmp(p, "basic_constraints") == 0) {
  387. opt.basic_constraints = atoi(q);
  388. if (opt.basic_constraints != 0 &&
  389. opt.basic_constraints != 1) {
  390. mbedtls_printf("Invalid argument for option %s\n", p);
  391. goto usage;
  392. }
  393. } else if (strcmp(p, "md") == 0) {
  394. const mbedtls_md_info_t *md_info =
  395. mbedtls_md_info_from_string(q);
  396. if (md_info == NULL) {
  397. mbedtls_printf("Invalid argument for option %s\n", p);
  398. goto usage;
  399. }
  400. opt.md = mbedtls_md_get_type(md_info);
  401. } else if (strcmp(p, "version") == 0) {
  402. opt.version = atoi(q);
  403. if (opt.version < 1 || opt.version > 3) {
  404. mbedtls_printf("Invalid argument for option %s\n", p);
  405. goto usage;
  406. }
  407. opt.version--;
  408. } else if (strcmp(p, "selfsign") == 0) {
  409. opt.selfsign = atoi(q);
  410. if (opt.selfsign < 0 || opt.selfsign > 1) {
  411. mbedtls_printf("Invalid argument for option %s\n", p);
  412. goto usage;
  413. }
  414. } else if (strcmp(p, "is_ca") == 0) {
  415. opt.is_ca = atoi(q);
  416. if (opt.is_ca < 0 || opt.is_ca > 1) {
  417. mbedtls_printf("Invalid argument for option %s\n", p);
  418. goto usage;
  419. }
  420. } else if (strcmp(p, "max_pathlen") == 0) {
  421. opt.max_pathlen = atoi(q);
  422. if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
  423. mbedtls_printf("Invalid argument for option %s\n", p);
  424. goto usage;
  425. }
  426. } else if (strcmp(p, "key_usage") == 0) {
  427. while (q != NULL) {
  428. if ((r = strchr(q, ',')) != NULL) {
  429. *r++ = '\0';
  430. }
  431. if (strcmp(q, "digital_signature") == 0) {
  432. opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  433. } else if (strcmp(q, "non_repudiation") == 0) {
  434. opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
  435. } else if (strcmp(q, "key_encipherment") == 0) {
  436. opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  437. } else if (strcmp(q, "data_encipherment") == 0) {
  438. opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
  439. } else if (strcmp(q, "key_agreement") == 0) {
  440. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  441. } else if (strcmp(q, "key_cert_sign") == 0) {
  442. opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
  443. } else if (strcmp(q, "crl_sign") == 0) {
  444. opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
  445. } else {
  446. mbedtls_printf("Invalid argument for option %s\n", p);
  447. goto usage;
  448. }
  449. q = r;
  450. }
  451. } else if (strcmp(p, "ext_key_usage") == 0) {
  452. mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
  453. while (q != NULL) {
  454. if ((r = strchr(q, ',')) != NULL) {
  455. *r++ = '\0';
  456. }
  457. ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
  458. ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
  459. if (strcmp(q, "serverAuth") == 0) {
  460. SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
  461. } else if (strcmp(q, "clientAuth") == 0) {
  462. SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
  463. } else if (strcmp(q, "codeSigning") == 0) {
  464. SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
  465. } else if (strcmp(q, "emailProtection") == 0) {
  466. SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
  467. } else if (strcmp(q, "timeStamping") == 0) {
  468. SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
  469. } else if (strcmp(q, "OCSPSigning") == 0) {
  470. SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
  471. } else {
  472. mbedtls_printf("Invalid argument for option %s\n", p);
  473. goto usage;
  474. }
  475. *tail = ext_key_usage;
  476. tail = &ext_key_usage->next;
  477. q = r;
  478. }
  479. } else if (strcmp(p, "ns_cert_type") == 0) {
  480. while (q != NULL) {
  481. if ((r = strchr(q, ',')) != NULL) {
  482. *r++ = '\0';
  483. }
  484. if (strcmp(q, "ssl_client") == 0) {
  485. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
  486. } else if (strcmp(q, "ssl_server") == 0) {
  487. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  488. } else if (strcmp(q, "email") == 0) {
  489. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
  490. } else if (strcmp(q, "object_signing") == 0) {
  491. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
  492. } else if (strcmp(q, "ssl_ca") == 0) {
  493. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
  494. } else if (strcmp(q, "email_ca") == 0) {
  495. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
  496. } else if (strcmp(q, "object_signing_ca") == 0) {
  497. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
  498. } else {
  499. mbedtls_printf("Invalid argument for option %s\n", p);
  500. goto usage;
  501. }
  502. q = r;
  503. }
  504. } else if (strcmp(p, "format") == 0) {
  505. if (strcmp(q, "der") == 0) {
  506. opt.format = FORMAT_DER;
  507. } else if (strcmp(q, "pem") == 0) {
  508. opt.format = FORMAT_PEM;
  509. } else {
  510. mbedtls_printf("Invalid argument for option %s\n", p);
  511. goto usage;
  512. }
  513. } else {
  514. goto usage;
  515. }
  516. }
  517. mbedtls_printf("\n");
  518. /*
  519. * 0. Seed the PRNG
  520. */
  521. mbedtls_printf(" . Seeding the random number generator...");
  522. fflush(stdout);
  523. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  524. (const unsigned char *) pers,
  525. strlen(pers))) != 0) {
  526. mbedtls_strerror(ret, buf, sizeof(buf));
  527. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
  528. ret, buf);
  529. goto exit;
  530. }
  531. mbedtls_printf(" ok\n");
  532. // Parse serial to MPI
  533. //
  534. mbedtls_printf(" . Reading serial number...");
  535. fflush(stdout);
  536. if (serial_frmt == SERIAL_FRMT_HEX) {
  537. ret = mbedtls_test_unhexify(serial, sizeof(serial),
  538. opt.serial_hex, &serial_len);
  539. } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
  540. ret = parse_serial_decimal_format(serial, sizeof(serial),
  541. opt.serial, &serial_len);
  542. }
  543. if (ret != 0) {
  544. mbedtls_printf(" failed\n ! Unable to parse serial\n");
  545. goto exit;
  546. }
  547. mbedtls_printf(" ok\n");
  548. // Parse issuer certificate if present
  549. //
  550. if (!opt.selfsign && strlen(opt.issuer_crt)) {
  551. /*
  552. * 1.0.a. Load the certificates
  553. */
  554. mbedtls_printf(" . Loading the issuer certificate ...");
  555. fflush(stdout);
  556. if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
  557. mbedtls_strerror(ret, buf, sizeof(buf));
  558. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
  559. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  560. goto exit;
  561. }
  562. ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
  563. &issuer_crt.subject);
  564. if (ret < 0) {
  565. mbedtls_strerror(ret, buf, sizeof(buf));
  566. mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
  567. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  568. goto exit;
  569. }
  570. opt.issuer_name = issuer_name;
  571. mbedtls_printf(" ok\n");
  572. }
  573. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  574. // Parse certificate request if present
  575. //
  576. if (!opt.selfsign && strlen(opt.request_file)) {
  577. /*
  578. * 1.0.b. Load the CSR
  579. */
  580. mbedtls_printf(" . Loading the certificate request ...");
  581. fflush(stdout);
  582. if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
  583. mbedtls_strerror(ret, buf, sizeof(buf));
  584. mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
  585. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  586. goto exit;
  587. }
  588. ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
  589. &csr.subject);
  590. if (ret < 0) {
  591. mbedtls_strerror(ret, buf, sizeof(buf));
  592. mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
  593. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  594. goto exit;
  595. }
  596. opt.subject_name = subject_name;
  597. subject_key = &csr.pk;
  598. mbedtls_printf(" ok\n");
  599. }
  600. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  601. /*
  602. * 1.1. Load the keys
  603. */
  604. if (!opt.selfsign && !strlen(opt.request_file)) {
  605. mbedtls_printf(" . Loading the subject key ...");
  606. fflush(stdout);
  607. ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
  608. opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
  609. if (ret != 0) {
  610. mbedtls_strerror(ret, buf, sizeof(buf));
  611. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
  612. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  613. goto exit;
  614. }
  615. mbedtls_printf(" ok\n");
  616. }
  617. mbedtls_printf(" . Loading the issuer key ...");
  618. fflush(stdout);
  619. ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
  620. opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
  621. if (ret != 0) {
  622. mbedtls_strerror(ret, buf, sizeof(buf));
  623. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
  624. "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
  625. goto exit;
  626. }
  627. // Check if key and issuer certificate match
  628. //
  629. if (strlen(opt.issuer_crt)) {
  630. if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
  631. mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
  632. mbedtls_printf(" failed\n ! issuer_key does not match "
  633. "issuer certificate\n\n");
  634. goto exit;
  635. }
  636. }
  637. mbedtls_printf(" ok\n");
  638. if (opt.selfsign) {
  639. opt.subject_name = opt.issuer_name;
  640. subject_key = issuer_key;
  641. }
  642. mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
  643. mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
  644. /*
  645. * 1.0. Check the names for validity
  646. */
  647. if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
  648. mbedtls_strerror(ret, buf, sizeof(buf));
  649. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
  650. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  651. goto exit;
  652. }
  653. if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
  654. mbedtls_strerror(ret, buf, sizeof(buf));
  655. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
  656. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  657. goto exit;
  658. }
  659. mbedtls_printf(" . Setting certificate values ...");
  660. fflush(stdout);
  661. mbedtls_x509write_crt_set_version(&crt, opt.version);
  662. mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
  663. ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
  664. if (ret != 0) {
  665. mbedtls_strerror(ret, buf, sizeof(buf));
  666. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
  667. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  668. goto exit;
  669. }
  670. ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
  671. if (ret != 0) {
  672. mbedtls_strerror(ret, buf, sizeof(buf));
  673. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
  674. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  675. goto exit;
  676. }
  677. mbedtls_printf(" ok\n");
  678. if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
  679. opt.basic_constraints != 0) {
  680. mbedtls_printf(" . Adding the Basic Constraints extension ...");
  681. fflush(stdout);
  682. ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
  683. opt.max_pathlen);
  684. if (ret != 0) {
  685. mbedtls_strerror(ret, buf, sizeof(buf));
  686. mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
  687. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  688. goto exit;
  689. }
  690. mbedtls_printf(" ok\n");
  691. }
  692. #if defined(MBEDTLS_SHA1_C)
  693. if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
  694. opt.subject_identifier != 0) {
  695. mbedtls_printf(" . Adding the Subject Key Identifier ...");
  696. fflush(stdout);
  697. ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
  698. if (ret != 0) {
  699. mbedtls_strerror(ret, buf, sizeof(buf));
  700. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
  701. "_key_identifier returned -0x%04x - %s\n\n",
  702. (unsigned int) -ret, buf);
  703. goto exit;
  704. }
  705. mbedtls_printf(" ok\n");
  706. }
  707. if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
  708. opt.authority_identifier != 0) {
  709. mbedtls_printf(" . Adding the Authority Key Identifier ...");
  710. fflush(stdout);
  711. ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
  712. if (ret != 0) {
  713. mbedtls_strerror(ret, buf, sizeof(buf));
  714. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
  715. "key_identifier returned -0x%04x - %s\n\n",
  716. (unsigned int) -ret, buf);
  717. goto exit;
  718. }
  719. mbedtls_printf(" ok\n");
  720. }
  721. #endif /* MBEDTLS_SHA1_C */
  722. if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
  723. opt.key_usage != 0) {
  724. mbedtls_printf(" . Adding the Key Usage extension ...");
  725. fflush(stdout);
  726. ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
  727. if (ret != 0) {
  728. mbedtls_strerror(ret, buf, sizeof(buf));
  729. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
  730. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  731. goto exit;
  732. }
  733. mbedtls_printf(" ok\n");
  734. }
  735. if (opt.ext_key_usage) {
  736. mbedtls_printf(" . Adding the Extended Key Usage extension ...");
  737. fflush(stdout);
  738. ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
  739. if (ret != 0) {
  740. mbedtls_strerror(ret, buf, sizeof(buf));
  741. mbedtls_printf(
  742. " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
  743. (unsigned int) -ret,
  744. buf);
  745. goto exit;
  746. }
  747. mbedtls_printf(" ok\n");
  748. }
  749. if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
  750. opt.ns_cert_type != 0) {
  751. mbedtls_printf(" . Adding the NS Cert Type extension ...");
  752. fflush(stdout);
  753. ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
  754. if (ret != 0) {
  755. mbedtls_strerror(ret, buf, sizeof(buf));
  756. mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
  757. "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
  758. goto exit;
  759. }
  760. mbedtls_printf(" ok\n");
  761. }
  762. /*
  763. * 1.2. Writing the certificate
  764. */
  765. mbedtls_printf(" . Writing the certificate...");
  766. fflush(stdout);
  767. if ((ret = write_certificate(&crt, opt.output_file,
  768. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  769. mbedtls_strerror(ret, buf, sizeof(buf));
  770. mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
  771. (unsigned int) -ret, buf);
  772. goto exit;
  773. }
  774. mbedtls_printf(" ok\n");
  775. exit_code = MBEDTLS_EXIT_SUCCESS;
  776. exit:
  777. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  778. mbedtls_x509_csr_free(&csr);
  779. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  780. mbedtls_x509_crt_free(&issuer_crt);
  781. mbedtls_x509write_crt_free(&crt);
  782. mbedtls_pk_free(&loaded_subject_key);
  783. mbedtls_pk_free(&loaded_issuer_key);
  784. mbedtls_ctr_drbg_free(&ctr_drbg);
  785. mbedtls_entropy_free(&entropy);
  786. mbedtls_exit(exit_code);
  787. }
  788. #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
  789. MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
  790. MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */