cert_write.c 23 KB

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