cert_write.c 28 KB

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