blob: ed8015574d017e25121a2a40aae3c5617f112c33 [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
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbusdc470ae2018-12-11 13:55:56 -060040 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Hanno Becker02a21932019-06-10 15:08:43 +010041 defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042int main( void )
43{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Peter Kolbusdc470ae2018-12-11 13:55:56 -060045 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO and/or "
Hanno Becker02a21932019-06-10 15:08:43 +010046 "MBEDTLS_X509_REMOVE_INFO not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020047 return( 0 );
48}
49#else
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020052
Rich Evans18b78c72015-02-11 14:06:19 +000053#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000056
57#define DFL_FILENAME "cert.req"
58#define DFL_DEBUG_LEVEL 0
59
60#define USAGE \
61 "\n usage: req_app param=<>...\n" \
62 "\n acceptable parameters:\n" \
63 " filename=%%s default: cert.req\n" \
64 "\n"
65
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Paul Bakkerf9f377e2013-09-09 15:35:10 +020067/*
68 * global options
69 */
70struct options
71{
72 const char *filename; /* filename of the certificate request */
73} opt;
74
Paul Bakkerf9f377e2013-09-09 15:35:10 +020075int main( int argc, char *argv[] )
76{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010077 int ret = 1;
78 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020079 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010081 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020082 char *p, *q;
83
84 /*
85 * Set to sane values
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020088
89 if( argc == 0 )
90 {
91 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020093 goto exit;
94 }
95
96 opt.filename = DFL_FILENAME;
97
98 for( i = 1; i < argc; i++ )
99 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200100 p = argv[i];
101 if( ( q = strchr( p, '=' ) ) == NULL )
102 goto usage;
103 *q++ = '\0';
104
105 if( strcmp( p, "filename" ) == 0 )
106 opt.filename = q;
107 else
108 goto usage;
109 }
110
111 /*
112 * 1.1. Load the CSR
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200115 fflush( stdout );
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200118
119 if( ret != 0 )
120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
122 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200123 goto exit;
124 }
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200127
128 /*
129 * 1.2 Print the CSR
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf( " . CSR information ...\n" );
132 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200133 if( ret == -1 )
134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
136 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200137 goto exit;
138 }
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200141
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100142 exit_code = MBEDTLS_EXIT_SUCCESS;
143
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200144exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200146
147#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200149 fflush( stdout ); getchar();
150#endif
151
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100152 return( exit_code );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
155 MBEDTLS_FS_IO */