cert_req.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Certificate request generation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  59. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  60. #endif /* MBEDTLS_PLATFORM_C */
  61. #if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
  62. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
  63. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  64. !defined(MBEDTLS_PEM_WRITE_C)
  65. int main( void )
  66. {
  67. mbedtls_printf( "MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
  68. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
  69. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
  70. "not defined.\n");
  71. mbedtls_exit( 0 );
  72. }
  73. #else
  74. #include "mbedtls/x509_csr.h"
  75. #include "mbedtls/entropy.h"
  76. #include "mbedtls/ctr_drbg.h"
  77. #include "mbedtls/error.h"
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #define DFL_FILENAME "keyfile.key"
  82. #define DFL_PASSWORD NULL
  83. #define DFL_DEBUG_LEVEL 0
  84. #define DFL_OUTPUT_FILENAME "cert.req"
  85. #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
  86. #define DFL_KEY_USAGE 0
  87. #define DFL_FORCE_KEY_USAGE 0
  88. #define DFL_NS_CERT_TYPE 0
  89. #define DFL_FORCE_NS_CERT_TYPE 0
  90. #define DFL_MD_ALG MBEDTLS_MD_SHA256
  91. #define USAGE \
  92. "\n usage: cert_req param=<>...\n" \
  93. "\n acceptable parameters:\n" \
  94. " filename=%%s default: keyfile.key\n" \
  95. " password=%%s default: NULL\n" \
  96. " debug_level=%%d default: 0 (disabled)\n" \
  97. " output_file=%%s default: cert.req\n" \
  98. " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
  99. " key_usage=%%s default: (empty)\n" \
  100. " Comma-separated-list of values:\n" \
  101. " digital_signature\n" \
  102. " non_repudiation\n" \
  103. " key_encipherment\n" \
  104. " data_encipherment\n" \
  105. " key_agreement\n" \
  106. " key_cert_sign\n" \
  107. " crl_sign\n" \
  108. " force_key_usage=0/1 default: off\n" \
  109. " Add KeyUsage even if it is empty\n" \
  110. " ns_cert_type=%%s default: (empty)\n" \
  111. " Comma-separated-list of values:\n" \
  112. " ssl_client\n" \
  113. " ssl_server\n" \
  114. " email\n" \
  115. " object_signing\n" \
  116. " ssl_ca\n" \
  117. " email_ca\n" \
  118. " object_signing_ca\n" \
  119. " force_ns_cert_type=0/1 default: off\n" \
  120. " Add NsCertType even if it is empty\n" \
  121. " md=%%s default: SHA256\n" \
  122. " possible values:\n" \
  123. " MD2, MD4, MD5, RIPEMD160, SHA1,\n" \
  124. " SHA224, SHA256, SHA384, SHA512\n" \
  125. "\n"
  126. /*
  127. * global options
  128. */
  129. struct options
  130. {
  131. const char *filename; /* filename of the key file */
  132. const char *password; /* password for the key file */
  133. int debug_level; /* level of debugging */
  134. const char *output_file; /* where to store the constructed key file */
  135. const char *subject_name; /* subject name for certificate request */
  136. unsigned char key_usage; /* key usage flags */
  137. int force_key_usage; /* Force adding the KeyUsage extension */
  138. unsigned char ns_cert_type; /* NS cert type */
  139. int force_ns_cert_type; /* Force adding NsCertType extension */
  140. mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
  141. } opt;
  142. int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
  143. int (*f_rng)(void *, unsigned char *, size_t),
  144. void *p_rng )
  145. {
  146. int ret;
  147. FILE *f;
  148. unsigned char output_buf[4096];
  149. size_t len = 0;
  150. memset( output_buf, 0, 4096 );
  151. if( ( ret = mbedtls_x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
  152. return( ret );
  153. len = strlen( (char *) output_buf );
  154. if( ( f = fopen( output_file, "w" ) ) == NULL )
  155. return( -1 );
  156. if( fwrite( output_buf, 1, len, f ) != len )
  157. {
  158. fclose( f );
  159. return( -1 );
  160. }
  161. fclose( f );
  162. return( 0 );
  163. }
  164. int main( int argc, char *argv[] )
  165. {
  166. int ret = 1;
  167. int exit_code = MBEDTLS_EXIT_FAILURE;
  168. mbedtls_pk_context key;
  169. char buf[1024];
  170. int i;
  171. char *p, *q, *r;
  172. mbedtls_x509write_csr req;
  173. mbedtls_entropy_context entropy;
  174. mbedtls_ctr_drbg_context ctr_drbg;
  175. const char *pers = "csr example app";
  176. /*
  177. * Set to sane values
  178. */
  179. mbedtls_x509write_csr_init( &req );
  180. mbedtls_pk_init( &key );
  181. mbedtls_ctr_drbg_init( &ctr_drbg );
  182. memset( buf, 0, sizeof( buf ) );
  183. if( argc == 0 )
  184. {
  185. usage:
  186. mbedtls_printf( USAGE );
  187. goto exit;
  188. }
  189. opt.filename = DFL_FILENAME;
  190. opt.password = DFL_PASSWORD;
  191. opt.debug_level = DFL_DEBUG_LEVEL;
  192. opt.output_file = DFL_OUTPUT_FILENAME;
  193. opt.subject_name = DFL_SUBJECT_NAME;
  194. opt.key_usage = DFL_KEY_USAGE;
  195. opt.force_key_usage = DFL_FORCE_KEY_USAGE;
  196. opt.ns_cert_type = DFL_NS_CERT_TYPE;
  197. opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
  198. opt.md_alg = DFL_MD_ALG;
  199. for( i = 1; i < argc; i++ )
  200. {
  201. p = argv[i];
  202. if( ( q = strchr( p, '=' ) ) == NULL )
  203. goto usage;
  204. *q++ = '\0';
  205. if( strcmp( p, "filename" ) == 0 )
  206. opt.filename = q;
  207. else if( strcmp( p, "password" ) == 0 )
  208. opt.password = q;
  209. else if( strcmp( p, "output_file" ) == 0 )
  210. opt.output_file = q;
  211. else if( strcmp( p, "debug_level" ) == 0 )
  212. {
  213. opt.debug_level = atoi( q );
  214. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  215. goto usage;
  216. }
  217. else if( strcmp( p, "subject_name" ) == 0 )
  218. {
  219. opt.subject_name = q;
  220. }
  221. else if( strcmp( p, "md" ) == 0 )
  222. {
  223. const mbedtls_md_info_t *md_info =
  224. mbedtls_md_info_from_string( q );
  225. if( md_info == NULL )
  226. {
  227. mbedtls_printf( "Invalid argument for option %s\n", p );
  228. goto usage;
  229. }
  230. opt.md_alg = mbedtls_md_get_type( md_info );
  231. }
  232. else if( strcmp( p, "key_usage" ) == 0 )
  233. {
  234. while( q != NULL )
  235. {
  236. if( ( r = strchr( q, ',' ) ) != NULL )
  237. *r++ = '\0';
  238. if( strcmp( q, "digital_signature" ) == 0 )
  239. opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
  240. else if( strcmp( q, "non_repudiation" ) == 0 )
  241. opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
  242. else if( strcmp( q, "key_encipherment" ) == 0 )
  243. opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
  244. else if( strcmp( q, "data_encipherment" ) == 0 )
  245. opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
  246. else if( strcmp( q, "key_agreement" ) == 0 )
  247. opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
  248. else if( strcmp( q, "key_cert_sign" ) == 0 )
  249. opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
  250. else if( strcmp( q, "crl_sign" ) == 0 )
  251. opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
  252. else
  253. goto usage;
  254. q = r;
  255. }
  256. }
  257. else if( strcmp( p, "force_key_usage" ) == 0 )
  258. {
  259. switch( atoi( q ) )
  260. {
  261. case 0: opt.force_key_usage = 0; break;
  262. case 1: opt.force_key_usage = 1; break;
  263. default: goto usage;
  264. }
  265. }
  266. else if( strcmp( p, "ns_cert_type" ) == 0 )
  267. {
  268. while( q != NULL )
  269. {
  270. if( ( r = strchr( q, ',' ) ) != NULL )
  271. *r++ = '\0';
  272. if( strcmp( q, "ssl_client" ) == 0 )
  273. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
  274. else if( strcmp( q, "ssl_server" ) == 0 )
  275. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
  276. else if( strcmp( q, "email" ) == 0 )
  277. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
  278. else if( strcmp( q, "object_signing" ) == 0 )
  279. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
  280. else if( strcmp( q, "ssl_ca" ) == 0 )
  281. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
  282. else if( strcmp( q, "email_ca" ) == 0 )
  283. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
  284. else if( strcmp( q, "object_signing_ca" ) == 0 )
  285. opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
  286. else
  287. goto usage;
  288. q = r;
  289. }
  290. }
  291. else if( strcmp( p, "force_ns_cert_type" ) == 0 )
  292. {
  293. switch( atoi( q ) )
  294. {
  295. case 0: opt.force_ns_cert_type = 0; break;
  296. case 1: opt.force_ns_cert_type = 1; break;
  297. default: goto usage;
  298. }
  299. }
  300. else
  301. goto usage;
  302. }
  303. mbedtls_x509write_csr_set_md_alg( &req, opt.md_alg );
  304. if( opt.key_usage || opt.force_key_usage == 1 )
  305. mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
  306. if( opt.ns_cert_type || opt.force_ns_cert_type == 1 )
  307. mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
  308. /*
  309. * 0. Seed the PRNG
  310. */
  311. mbedtls_printf( " . Seeding the random number generator..." );
  312. fflush( stdout );
  313. mbedtls_entropy_init( &entropy );
  314. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  315. (const unsigned char *) pers,
  316. strlen( pers ) ) ) != 0 )
  317. {
  318. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d", ret );
  319. goto exit;
  320. }
  321. mbedtls_printf( " ok\n" );
  322. /*
  323. * 1.0. Check the subject name for validity
  324. */
  325. mbedtls_printf( " . Checking subject name..." );
  326. fflush( stdout );
  327. if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
  328. {
  329. mbedtls_printf( " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
  330. goto exit;
  331. }
  332. mbedtls_printf( " ok\n" );
  333. /*
  334. * 1.1. Load the key
  335. */
  336. mbedtls_printf( " . Loading the private key ..." );
  337. fflush( stdout );
  338. ret = mbedtls_pk_parse_keyfile( &key, opt.filename, opt.password );
  339. if( ret != 0 )
  340. {
  341. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d", ret );
  342. goto exit;
  343. }
  344. mbedtls_x509write_csr_set_key( &req, &key );
  345. mbedtls_printf( " ok\n" );
  346. /*
  347. * 1.2. Writing the request
  348. */
  349. mbedtls_printf( " . Writing the certificate request ..." );
  350. fflush( stdout );
  351. if( ( ret = write_certificate_request( &req, opt.output_file,
  352. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  353. {
  354. mbedtls_printf( " failed\n ! write_certifcate_request %d", ret );
  355. goto exit;
  356. }
  357. mbedtls_printf( " ok\n" );
  358. exit_code = MBEDTLS_EXIT_SUCCESS;
  359. exit:
  360. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  361. {
  362. #ifdef MBEDTLS_ERROR_C
  363. mbedtls_strerror( ret, buf, sizeof( buf ) );
  364. mbedtls_printf( " - %s\n", buf );
  365. #else
  366. mbedtls_printf("\n");
  367. #endif
  368. }
  369. mbedtls_x509write_csr_free( &req );
  370. mbedtls_pk_free( &key );
  371. mbedtls_ctr_drbg_free( &ctr_drbg );
  372. mbedtls_entropy_free( &entropy );
  373. #if defined(_WIN32)
  374. mbedtls_printf( " + Press Enter to exit this program.\n" );
  375. fflush( stdout ); getchar();
  376. #endif
  377. mbedtls_exit( exit_code );
  378. }
  379. #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  380. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */