2 * Certificate request generation
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
33 #define mbedtls_printf printf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
39 #if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
42 !defined(MBEDTLS_PEM_WRITE_C)
45 mbedtls_printf( "MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
47 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
53 #include "mbedtls/x509_csr.h"
54 #include "mbedtls/entropy.h"
55 #include "mbedtls/ctr_drbg.h"
56 #include "mbedtls/error.h"
62 #define DFL_FILENAME "keyfile.key"
63 #define DFL_PASSWORD NULL
64 #define DFL_DEBUG_LEVEL 0
65 #define DFL_OUTPUT_FILENAME "cert.req"
66 #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
67 #define DFL_KEY_USAGE 0
68 #define DFL_FORCE_KEY_USAGE 0
69 #define DFL_NS_CERT_TYPE 0
70 #define DFL_FORCE_NS_CERT_TYPE 0
71 #define DFL_MD_ALG MBEDTLS_MD_SHA256
74 "\n usage: cert_req param=<>...\n" \
75 "\n acceptable parameters:\n" \
76 " filename=%%s default: keyfile.key\n" \
77 " password=%%s default: NULL\n" \
78 " debug_level=%%d default: 0 (disabled)\n" \
79 " output_file=%%s default: cert.req\n" \
80 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
81 " key_usage=%%s default: (empty)\n" \
82 " Comma-separated-list of values:\n" \
83 " digital_signature\n" \
84 " non_repudiation\n" \
85 " key_encipherment\n" \
86 " data_encipherment\n" \
90 " force_key_usage=0/1 default: off\n" \
91 " Add KeyUsage even if it is empty\n" \
92 " ns_cert_type=%%s default: (empty)\n" \
93 " Comma-separated-list of values:\n" \
100 " object_signing_ca\n" \
101 " force_ns_cert_type=0/1 default: off\n" \
102 " Add NsCertType even if it is empty\n" \
103 " md=%%s default: SHA256\n" \
104 " possible values:\n" \
105 " MD4, MD5, SHA1\n" \
106 " SHA224, SHA256\n" \
107 " SHA384, SHA512\n" \
110 #if defined(MBEDTLS_CHECK_PARAMS)
111 void mbedtls_param_failed( const char *failure_condition,
115 mbedtls_printf( "%s:%i: Input param failed - %s\n",
116 file, line, failure_condition );
117 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
126 const char *filename; /* filename of the key file */
127 const char *password; /* password for the key file */
128 int debug_level; /* level of debugging */
129 const char *output_file; /* where to store the constructed key file */
130 const char *subject_name; /* subject name for certificate request */
131 unsigned char key_usage; /* key usage flags */
132 int force_key_usage; /* Force adding the KeyUsage extension */
133 unsigned char ns_cert_type; /* NS cert type */
134 int force_ns_cert_type; /* Force adding NsCertType extension */
135 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
138 int write_certificate_request( mbedtls_x509write_csr *req, const char *output_file,
139 int (*f_rng)(void *, unsigned char *, size_t),
144 unsigned char output_buf[4096];
147 memset( output_buf, 0, 4096 );
148 if( ( ret = mbedtls_x509write_csr_pem( req, output_buf, 4096, f_rng, p_rng ) ) < 0 )
151 len = strlen( (char *) output_buf );
153 if( ( f = fopen( output_file, "w" ) ) == NULL )
156 if( fwrite( output_buf, 1, len, f ) != len )
167 int main( int argc, char *argv[] )
170 int exit_code = MBEDTLS_EXIT_FAILURE;
171 mbedtls_pk_context key;
175 mbedtls_x509write_csr req;
176 mbedtls_entropy_context entropy;
177 mbedtls_ctr_drbg_context ctr_drbg;
178 const char *pers = "csr example app";
183 mbedtls_x509write_csr_init( &req );
184 mbedtls_pk_init( &key );
185 mbedtls_ctr_drbg_init( &ctr_drbg );
186 memset( buf, 0, sizeof( buf ) );
191 mbedtls_printf( USAGE );
195 opt.filename = DFL_FILENAME;
196 opt.password = DFL_PASSWORD;
197 opt.debug_level = DFL_DEBUG_LEVEL;
198 opt.output_file = DFL_OUTPUT_FILENAME;
199 opt.subject_name = DFL_SUBJECT_NAME;
200 opt.key_usage = DFL_KEY_USAGE;
201 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
202 opt.ns_cert_type = DFL_NS_CERT_TYPE;
203 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
204 opt.md_alg = DFL_MD_ALG;
206 for( i = 1; i < argc; i++ )
210 if( ( q = strchr( p, '=' ) ) == NULL )
214 if( strcmp( p, "filename" ) == 0 )
216 else if( strcmp( p, "password" ) == 0 )
218 else if( strcmp( p, "output_file" ) == 0 )
220 else if( strcmp( p, "debug_level" ) == 0 )
222 opt.debug_level = atoi( q );
223 if( opt.debug_level < 0 || opt.debug_level > 65535 )
226 else if( strcmp( p, "subject_name" ) == 0 )
228 opt.subject_name = q;
230 else if( strcmp( p, "md" ) == 0 )
232 if( strcmp( q, "SHA256" ) == 0 )
234 opt.md_alg = MBEDTLS_MD_SHA256;
236 else if( strcmp( q, "SHA224" ) == 0 )
238 opt.md_alg = MBEDTLS_MD_SHA224;
241 #if defined(MBEDTLS_MD5_C)
242 if( strcmp( q, "MD5" ) == 0 )
244 opt.md_alg = MBEDTLS_MD_MD5;
247 #endif /* MBEDTLS_MD5_C */
248 #if defined(MBEDTLS_MD4_C)
249 if( strcmp( q, "MD4" ) == 0 )
251 opt.md_alg = MBEDTLS_MD_MD4;
254 #endif /* MBEDTLS_MD5_C */
255 #if defined(MBEDTLS_SHA1_C)
256 if( strcmp( q, "SHA1" ) == 0 )
258 opt.md_alg = MBEDTLS_MD_SHA1;
261 #endif /* MBEDTLS_SHA1_C */
262 #if defined(MBEDTLS_SHA512_C)
263 if( strcmp( q, "SHA384" ) == 0 )
265 opt.md_alg = MBEDTLS_MD_SHA384;
268 if( strcmp( q, "SHA512" ) == 0 )
270 opt.md_alg = MBEDTLS_MD_SHA512;
273 #endif /* MBEDTLS_SHA512_C */
278 else if( strcmp( p, "key_usage" ) == 0 )
282 if( ( r = strchr( q, ',' ) ) != NULL )
285 if( strcmp( q, "digital_signature" ) == 0 )
286 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
287 else if( strcmp( q, "non_repudiation" ) == 0 )
288 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
289 else if( strcmp( q, "key_encipherment" ) == 0 )
290 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
291 else if( strcmp( q, "data_encipherment" ) == 0 )
292 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
293 else if( strcmp( q, "key_agreement" ) == 0 )
294 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
295 else if( strcmp( q, "key_cert_sign" ) == 0 )
296 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
297 else if( strcmp( q, "crl_sign" ) == 0 )
298 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
305 else if( strcmp( p, "force_key_usage" ) == 0 )
309 case 0: opt.force_key_usage = 0; break;
310 case 1: opt.force_key_usage = 1; break;
314 else if( strcmp( p, "ns_cert_type" ) == 0 )
318 if( ( r = strchr( q, ',' ) ) != NULL )
321 if( strcmp( q, "ssl_client" ) == 0 )
322 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
323 else if( strcmp( q, "ssl_server" ) == 0 )
324 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
325 else if( strcmp( q, "email" ) == 0 )
326 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
327 else if( strcmp( q, "object_signing" ) == 0 )
328 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
329 else if( strcmp( q, "ssl_ca" ) == 0 )
330 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
331 else if( strcmp( q, "email_ca" ) == 0 )
332 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
333 else if( strcmp( q, "object_signing_ca" ) == 0 )
334 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
341 else if( strcmp( p, "force_ns_cert_type" ) == 0 )
345 case 0: opt.force_ns_cert_type = 0; break;
346 case 1: opt.force_ns_cert_type = 1; break;
354 mbedtls_x509write_csr_set_md_alg( &req, opt.md_alg );
356 if( opt.key_usage || opt.force_key_usage == 1 )
357 mbedtls_x509write_csr_set_key_usage( &req, opt.key_usage );
359 if( opt.ns_cert_type || opt.force_ns_cert_type == 1 )
360 mbedtls_x509write_csr_set_ns_cert_type( &req, opt.ns_cert_type );
365 mbedtls_printf( " . Seeding the random number generator..." );
368 mbedtls_entropy_init( &entropy );
369 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
370 (const unsigned char *) pers,
371 strlen( pers ) ) ) != 0 )
373 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d", ret );
377 mbedtls_printf( " ok\n" );
380 * 1.0. Check the subject name for validity
382 mbedtls_printf( " . Checking subject name..." );
385 if( ( ret = mbedtls_x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
387 mbedtls_printf( " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret );
391 mbedtls_printf( " ok\n" );
396 mbedtls_printf( " . Loading the private key ..." );
399 ret = mbedtls_pk_parse_keyfile( &key, opt.filename, opt.password );
403 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned %d", ret );
407 mbedtls_x509write_csr_set_key( &req, &key );
409 mbedtls_printf( " ok\n" );
412 * 1.2. Writing the request
414 mbedtls_printf( " . Writing the certificate request ..." );
417 if( ( ret = write_certificate_request( &req, opt.output_file,
418 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
420 mbedtls_printf( " failed\n ! write_certifcate_request %d", ret );
424 mbedtls_printf( " ok\n" );
426 exit_code = MBEDTLS_EXIT_SUCCESS;
430 if( exit_code != MBEDTLS_EXIT_SUCCESS )
432 #ifdef MBEDTLS_ERROR_C
433 mbedtls_strerror( ret, buf, sizeof( buf ) );
434 mbedtls_printf( " - %s\n", buf );
436 mbedtls_printf("\n");
440 mbedtls_x509write_csr_free( &req );
441 mbedtls_pk_free( &key );
442 mbedtls_ctr_drbg_free( &ctr_drbg );
443 mbedtls_entropy_free( &entropy );
446 mbedtls_printf( " + Press Enter to exit this program.\n" );
447 fflush( stdout ); getchar();
452 #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
453 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */