blob: 8646c04b5e48c98b3081bb252feb221b9d22c79b [file] [log] [blame]
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001/*
2 * Certificate request reading application
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * 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:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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.
Paul Bakkerf9f377e2013-09-09 15:35:10 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
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 * **********
Paul Bakkerf9f377e2013-09-09 15:35:10 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
65 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020066int main( void )
67{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
69 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020070 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020071}
72#else
73
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020075
Rich Evans18b78c72015-02-11 14:06:19 +000076#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000079
80#define DFL_FILENAME "cert.req"
81#define DFL_DEBUG_LEVEL 0
82
83#define USAGE \
84 "\n usage: req_app param=<>...\n" \
85 "\n acceptable parameters:\n" \
86 " filename=%%s default: cert.req\n" \
87 "\n"
88
Paul Bakkerf9f377e2013-09-09 15:35:10 +020089/*
90 * global options
91 */
92struct options
93{
94 const char *filename; /* filename of the certificate request */
95} opt;
96
Paul Bakkerf9f377e2013-09-09 15:35:10 +020097int main( int argc, char *argv[] )
98{
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +010099 int ret = 1;
100 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200101 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100103 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200104 char *p, *q;
105
106 /*
107 * Set to sane values
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110
111 if( argc == 0 )
112 {
113 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200115 goto exit;
116 }
117
118 opt.filename = DFL_FILENAME;
119
120 for( i = 1; i < argc; i++ )
121 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200122 p = argv[i];
123 if( ( q = strchr( p, '=' ) ) == NULL )
124 goto usage;
125 *q++ = '\0';
126
127 if( strcmp( p, "filename" ) == 0 )
128 opt.filename = q;
129 else
130 goto usage;
131 }
132
133 /*
134 * 1.1. Load the CSR
135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200137 fflush( stdout );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200140
141 if( ret != 0 )
142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
144 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200145 goto exit;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200149
150 /*
151 * 1.2 Print the CSR
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " . CSR information ...\n" );
154 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200155 if( ret == -1 )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
158 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200163
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +0100164 exit_code = MBEDTLS_EXIT_SUCCESS;
165
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200166exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200168
169#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200171 fflush( stdout ); getchar();
172#endif
173
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200174 mbedtls_exit( exit_code );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
177 MBEDTLS_FS_IO */