cert_write.c 28 KB

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