cert_req.c 13 KB

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