cert_req.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Certificate request generation
  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_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
  37. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
  38. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  39. !defined(MBEDTLS_PEM_WRITE_C)
  40. int main( void )
  41. {
  42. mbedtls_printf( "MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
  43. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
  44. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
  45. "not defined.\n");
  46. return( 0 );
  47. }
  48. #else
  49. #include "mbedtls/x509_csr.h"
  50. #include "mbedtls/entropy.h"
  51. #include "mbedtls/ctr_drbg.h"
  52. #include "mbedtls/error.h"
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #define DFL_FILENAME "keyfile.key"
  57. #define DFL_PASSWORD NULL
  58. #define DFL_DEBUG_LEVEL 0
  59. #define DFL_OUTPUT_FILENAME "cert.req"
  60. #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
  61. #define DFL_KEY_USAGE 0
  62. #define DFL_FORCE_KEY_USAGE 0
  63. #define DFL_NS_CERT_TYPE 0
  64. #define DFL_FORCE_NS_CERT_TYPE 0
  65. #define DFL_MD_ALG MBEDTLS_MD_SHA256
  66. #define USAGE \
  67. "\n usage: cert_req param=<>...\n" \
  68. "\n acceptable parameters:\n" \
  69. " filename=%%s default: keyfile.key\n" \
  70. " password=%%s default: NULL\n" \
  71. " debug_level=%%d default: 0 (disabled)\n" \
  72. " output_file=%%s default: cert.req\n" \
  73. " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
  74. " key_usage=%%s default: (empty)\n" \
  75. " Comma-separated-list of values:\n" \
  76. " digital_signature\n" \
  77. " non_repudiation\n" \
  78. " key_encipherment\n" \
  79. " data_encipherment\n" \
  80. " key_agreement\n" \
  81. " key_cert_sign\n" \
  82. " crl_sign\n" \
  83. " force_key_usage=0/1 default: off\n" \
  84. " Add KeyUsage even if it is empty\n" \
  85. " ns_cert_type=%%s default: (empty)\n" \
  86. " Comma-separated-list of values:\n" \
  87. " ssl_client\n" \
  88. " ssl_server\n" \
  89. " email\n" \
  90. " object_signing\n" \
  91. " ssl_ca\n" \
  92. " email_ca\n" \
  93. " object_signing_ca\n" \
  94. " force_ns_cert_type=0/1 default: off\n" \
  95. " Add NsCertType even if it is empty\n" \
  96. " md=%%s default: SHA256\n" \
  97. " possible values:\n" \
  98. " MD2, MD4, MD5, SHA1\n" \
  99. " SHA224, SHA256\n" \
  100. " SHA384, SHA512\n" \
  101. "\n"
  102. /*
  103. * global options
  104. */
  105. struct options
  106. {
  107. const char *filename; /* filename of the key file */
  108. const char *password; /* password for the key file */
  109. int debug_level; /* level of debugging */
  110. const char *output_file; /* where to store the constructed key file */
  111. const char *subject_name; /* subject name for certificate request */
  112. unsigned char key_usage; /* key usage flags */
  113. int force_key_usage; /* Force adding the KeyUsage extension */
  114. unsigned char ns_cert_type; /* NS cert type */
  115. int force_ns_cert_type; /* Force adding NsCertType extension */
  116. mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
  117. } opt;
  118. int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
  119. int (*f_rng)(void *, unsigned char *, size_t),
  120. void *p_rng )
  121. {
  122. int ret;
  123. FILE *f;
  124. unsigned char output_buf[4096];
  125. size_t len = 0;
  126. memset( output_buf, 0, 4096 );
  127. if( ( ret = mbedtls_x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
  128. return( ret );
  129. len = strlen( (char *) output_buf );
  130. if( ( f = fopen( output_file, "w" ) ) == NULL )
  131. return( -1 );
  132. if( fwrite( output_buf, 1, len, f ) != len )
  133. {
  134. fclose( f );
  135. return( -1 );
  136. }
  137. fclose( f );
  138. return( 0 );
  139. }
  140. int main( int argc, char *argv[] )
  141. {
  142. int ret = 1;
  143. int exit_code = MBEDTLS_EXIT_FAILURE;
  144. mbedtls_pk_context key;
  145. char buf[1024];
  146. int i;
  147. char *p, *q, *r;
  148. mbedtls_x509write_csr req;
  149. mbedtls_entropy_context entropy;
  150. mbedtls_ctr_drbg_context ctr_drbg;
  151. const char *pers = "csr example app";
  152. /*
  153. * Set to sane values
  154. */
  155. mbedtls_x509write_csr_init( &req );
  156. mbedtls_pk_init( &key );
  157. mbedtls_ctr_drbg_init( &ctr_drbg );
  158. memset( buf, 0, sizeof( buf ) );
  159. if( argc == 0 )
  160. {
  161. usage:
  162. mbedtls_printf( USAGE );
  163. goto exit;
  164. }
  165. opt.filename = DFL_FILENAME;
  166. opt.password = DFL_PASSWORD;
  167. opt.debug_level = DFL_DEBUG_LEVEL;
  168. opt.output_file = DFL_OUTPUT_FILENAME;
  169. opt.subject_name = DFL_SUBJECT_NAME;
  170. opt.key_usage = DFL_KEY_USAGE;
  171. opt.force_key_usage = DFL_FORCE_KEY_USAGE;
  172. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  173. opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
  174. opt.md_alg = DFL_MD_ALG;
  175. for( i = 1; i < argc; i++ )
  176. {
  177. p = argv[i];
  178. if( ( q = strchr( p, '=' ) ) == NULL )
  179. goto usage;
  180. *q++ = '\0';
  181. if( strcmp( p, "filename" ) == 0 )
  182. opt.filename = q;
  183. else if( strcmp( p, "password" ) == 0 )
  184. opt.password = q;
  185. else if( strcmp( p, "output_file" ) == 0 )
  186. opt.output_file = q;
  187. else if( strcmp( p, "debug_level" ) == 0 )
  188. {
  189. opt.debug_level = atoi( q );
  190. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  191. goto usage;
  192. }
  193. else if( strcmp( p, "subject_name" ) == 0 )
  194. {
  195. opt.subject_name = q;
  196. }
  197. else if( strcmp( p, "md" ) == 0 )
  198. {
  199. if( strcmp( q, "SHA256" ) == 0 )
  200. {
  201. opt.md_alg = MBEDTLS_MD_SHA256;
  202. }
  203. else if( strcmp( q, "SHA224" ) == 0 )
  204. {
  205. opt.md_alg = MBEDTLS_MD_SHA224;
  206. }
  207. else
  208. #if defined(MBEDTLS_MD5_C)
  209. if( strcmp( q, "MD5" ) == 0 )
  210. {
  211. opt.md_alg = MBEDTLS_MD_MD5;
  212. }
  213. else
  214. #endif /* MBEDTLS_MD5_C */
  215. #if defined(MBEDTLS_MD4_C)
  216. if( strcmp( q, "MD4" ) == 0 )
  217. {
  218. opt.md_alg = MBEDTLS_MD_MD4;
  219. }
  220. else
  221. #endif /* MBEDTLS_MD5_C */
  222. #if defined(MBEDTLS_MD2_C)
  223. if( strcmp( q, "MD2" ) == 0 )
  224. {
  225. opt.md_alg = MBEDTLS_MD_MD2;
  226. }
  227. else
  228. #endif /* MBEDTLS_MD2_C */
  229. #if defined(MBEDTLS_SHA1_C)
  230. if( strcmp( q, "SHA1" ) == 0 )
  231. {
  232. opt.md_alg = MBEDTLS_MD_SHA1;
  233. }
  234. else
  235. #endif /* MBEDTLS_SHA1_C */
  236. #if defined(MBEDTLS_SHA512_C)
  237. if( strcmp( q, "SHA384" ) == 0 )
  238. {
  239. opt.md_alg = MBEDTLS_MD_SHA384;
  240. }
  241. else
  242. if( strcmp( q, "SHA512" ) == 0 )
  243. {
  244. opt.md_alg = MBEDTLS_MD_SHA512;
  245. }
  246. else
  247. #endif /* MBEDTLS_SHA512_C */
  248. {
  249. goto usage;
  250. }
  251. }
  252. else if( strcmp( p, "key_usage" ) == 0 )
  253. {
  254. while( q != NULL )
  255. {
  256. if( ( r = strchr( q, ',' ) ) != NULL )
  257. *r++ = '\0';
  258. if( strcmp( q, "digital_signature" ) == 0 )
  259. opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  260. else if( strcmp( q, "non_repudiation" ) == 0 )
  261. opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
  262. else if( strcmp( q, "key_encipherment" ) == 0 )
  263. opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  264. else if( strcmp( q, "data_encipherment" ) == 0 )
  265. opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
  266. else if( strcmp( q, "key_agreement" ) == 0 )
  267. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  268. else if( strcmp( q, "key_cert_sign" ) == 0 )
  269. opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
  270. else if( strcmp( q, "crl_sign" ) == 0 )
  271. opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
  272. else
  273. goto usage;
  274. q = r;
  275. }
  276. }
  277. else if( strcmp( p, "force_key_usage" ) == 0 )
  278. {
  279. switch( atoi( q ) )
  280. {
  281. case 0: opt.force_key_usage = 0; break;
  282. case 1: opt.force_key_usage = 1; break;
  283. default: goto usage;
  284. }
  285. }
  286. else if( strcmp( p, "ns_cert_type" ) == 0 )
  287. {
  288. while( q != NULL )
  289. {
  290. if( ( r = strchr( q, ',' ) ) != NULL )
  291. *r++ = '\0';
  292. if( strcmp( q, "ssl_client" ) == 0 )
  293. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
  294. else if( strcmp( q, "ssl_server" ) == 0 )
  295. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  296. else if( strcmp( q, "email" ) == 0 )
  297. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
  298. else if( strcmp( q, "object_signing" ) == 0 )
  299. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
  300. else if( strcmp( q, "ssl_ca" ) == 0 )
  301. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
  302. else if( strcmp( q, "email_ca" ) == 0 )
  303. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
  304. else if( strcmp( q, "object_signing_ca" ) == 0 )
  305. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
  306. else
  307. goto usage;
  308. q = r;
  309. }
  310. }
  311. else if( strcmp( p, "force_ns_cert_type" ) == 0 )
  312. {
  313. switch( atoi( q ) )
  314. {
  315. case 0: opt.force_ns_cert_type = 0; break;
  316. case 1: opt.force_ns_cert_type = 1; break;
  317. default: goto usage;
  318. }
  319. }
  320. else
  321. goto usage;
  322. }
  323. mbedtls_x509write_csr_set_md_alg( &req, opt.md_alg );
  324. if( opt.key_usage || opt.force_key_usage == 1 )
  325. mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
  326. if( opt.ns_cert_type || opt.force_ns_cert_type == 1 )
  327. mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
  328. /*
  329. * 0. Seed the PRNG
  330. */
  331. mbedtls_printf( " . Seeding the random number generator..." );
  332. fflush( stdout );
  333. mbedtls_entropy_init( &entropy );
  334. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  335. (const unsigned char *) pers,
  336. strlen( pers ) ) ) != 0 )
  337. {
  338. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d", ret );
  339. goto exit;
  340. }
  341. mbedtls_printf( " ok\n" );
  342. /*
  343. * 1.0. Check the subject name for validity
  344. */
  345. mbedtls_printf( " . Checking subject name..." );
  346. fflush( stdout );
  347. if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
  348. {
  349. mbedtls_printf( " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
  350. goto exit;
  351. }
  352. mbedtls_printf( " ok\n" );
  353. /*
  354. * 1.1. Load the key
  355. */
  356. mbedtls_printf( " . Loading the private key ..." );
  357. fflush( stdout );
  358. ret = mbedtls_pk_parse_keyfile( &key, opt.filename, opt.password );
  359. if( ret != 0 )
  360. {
  361. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d", ret );
  362. goto exit;
  363. }
  364. mbedtls_x509write_csr_set_key( &req, &key );
  365. mbedtls_printf( " ok\n" );
  366. /*
  367. * 1.2. Writing the request
  368. */
  369. mbedtls_printf( " . Writing the certificate request ..." );
  370. fflush( stdout );
  371. if( ( ret = write_certificate_request( &req, opt.output_file,
  372. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  373. {
  374. mbedtls_printf( " failed\n ! write_certifcate_request %d", ret );
  375. goto exit;
  376. }
  377. mbedtls_printf( " ok\n" );
  378. exit_code = MBEDTLS_EXIT_SUCCESS;
  379. exit:
  380. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  381. {
  382. #ifdef MBEDTLS_ERROR_C
  383. mbedtls_strerror( ret, buf, sizeof( buf ) );
  384. mbedtls_printf( " - %s\n", buf );
  385. #else
  386. mbedtls_printf("\n");
  387. #endif
  388. }
  389. mbedtls_x509write_csr_free( &req );
  390. mbedtls_pk_free( &key );
  391. mbedtls_ctr_drbg_free( &ctr_drbg );
  392. mbedtls_entropy_free( &entropy );
  393. #if defined(_WIN32)
  394. mbedtls_printf( " + Press Enter to exit this program.\n" );
  395. fflush( stdout ); getchar();
  396. #endif
  397. return( exit_code );
  398. }
  399. #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  400. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */