blob: 0ddece44314ae1900ae5ad42c1b834067a0f806d [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 Garcia12ab7a62018-04-29 21:51:47 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020034#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia12ab7a62018-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) || \
40 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020041int main( void )
42{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
44 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020045 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046}
47#else
48
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020050
Rich Evans18b78c72015-02-11 14:06:19 +000051#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000054
55#define DFL_FILENAME "cert.req"
56#define DFL_DEBUG_LEVEL 0
57
58#define USAGE \
59 "\n usage: req_app param=<>...\n" \
60 "\n acceptable parameters:\n" \
61 " filename=%%s default: cert.req\n" \
62 "\n"
63
Paul Bakkerf9f377e2013-09-09 15:35:10 +020064/*
65 * global options
66 */
67struct options
68{
69 const char *filename; /* filename of the certificate request */
70} opt;
71
Paul Bakkerf9f377e2013-09-09 15:35:10 +020072int main( int argc, char *argv[] )
73{
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +010074 int ret = 1;
75 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020076 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010078 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020079 char *p, *q;
80
81 /*
82 * Set to sane values
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020085
86 if( argc == 0 )
87 {
88 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020090 goto exit;
91 }
92
93 opt.filename = DFL_FILENAME;
94
95 for( i = 1; i < argc; i++ )
96 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020097 p = argv[i];
98 if( ( q = strchr( p, '=' ) ) == NULL )
99 goto usage;
100 *q++ = '\0';
101
102 if( strcmp( p, "filename" ) == 0 )
103 opt.filename = q;
104 else
105 goto usage;
106 }
107
108 /*
109 * 1.1. Load the CSR
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200112 fflush( stdout );
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200115
116 if( ret != 0 )
117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
119 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200120 goto exit;
121 }
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200124
125 /*
126 * 1.2 Print the CSR
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " . CSR information ...\n" );
129 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200130 if( ret == -1 )
131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
133 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200134 goto exit;
135 }
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200138
Andres Amaya Garcia12ab7a62018-04-29 21:51:47 +0100139 exit_code = MBEDTLS_EXIT_SUCCESS;
140
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200141exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200143
144#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200146 fflush( stdout ); getchar();
147#endif
148
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200149 mbedtls_exit( exit_code );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
152 MBEDTLS_FS_IO */