cert_req.c 13 KB

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