blob: fc1da0d739b652b0956c8bc89f86fe9b94ee2f42 [file] [log] [blame]
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001/*
2 * Certificate request reading application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerf9f377e2013-09-09 15:35:10 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerf9f377e2013-09-09 15:35:10 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010034#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010035#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
39 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020040int main( void )
41{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
43 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020044 return( 0 );
45}
46#else
47
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020049
Rich Evans18b78c72015-02-11 14:06:19 +000050#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000053
54#define DFL_FILENAME "cert.req"
55#define DFL_DEBUG_LEVEL 0
56
57#define USAGE \
58 "\n usage: req_app param=<>...\n" \
59 "\n acceptable parameters:\n" \
60 " filename=%%s default: cert.req\n" \
61 "\n"
62
Simon Butcher63cb97e2018-12-06 17:43:31 +000063#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
64void mbedtls_param_failed( char* failure_condition, char* file, int line )
65{
66 mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition );
67 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
68}
69#endif
70
Paul Bakkerf9f377e2013-09-09 15:35:10 +020071/*
72 * global options
73 */
74struct options
75{
76 const char *filename; /* filename of the certificate request */
77} opt;
78
Paul Bakkerf9f377e2013-09-09 15:35:10 +020079int main( int argc, char *argv[] )
80{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010081 int ret = 1;
82 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020083 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010085 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020086 char *p, *q;
87
88 /*
89 * Set to sane values
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020092
93 if( argc == 0 )
94 {
95 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020097 goto exit;
98 }
99
100 opt.filename = DFL_FILENAME;
101
102 for( i = 1; i < argc; i++ )
103 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200104 p = argv[i];
105 if( ( q = strchr( p, '=' ) ) == NULL )
106 goto usage;
107 *q++ = '\0';
108
109 if( strcmp( p, "filename" ) == 0 )
110 opt.filename = q;
111 else
112 goto usage;
113 }
114
115 /*
116 * 1.1. Load the CSR
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200119 fflush( stdout );
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200122
123 if( ret != 0 )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
126 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200127 goto exit;
128 }
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200131
132 /*
133 * 1.2 Print the CSR
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " . CSR information ...\n" );
136 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200137 if( ret == -1 )
138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
140 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200141 goto exit;
142 }
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200145
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100146 exit_code = MBEDTLS_EXIT_SUCCESS;
147
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200150
151#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200153 fflush( stdout ); getchar();
154#endif
155
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100156 return( exit_code );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
159 MBEDTLS_FS_IO */