cert_req.c 11 KB

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