cert_write.c 29 KB

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