blob: 8410a5371e853b97a54a23f8a5b3d9fc415b4ae2 [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
36 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020037int main( void )
38{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
40 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020041 return( 0 );
42}
43#else
44
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000050
51#define DFL_FILENAME "cert.req"
52#define DFL_DEBUG_LEVEL 0
53
54#define USAGE \
55 "\n usage: req_app param=<>...\n" \
56 "\n acceptable parameters:\n" \
57 " filename=%%s default: cert.req\n" \
58 "\n"
59
Paul Bakkerf9f377e2013-09-09 15:35:10 +020060/*
61 * global options
62 */
63struct options
64{
65 const char *filename; /* filename of the certificate request */
66} opt;
67
Paul Bakkerf9f377e2013-09-09 15:35:10 +020068int main( int argc, char *argv[] )
69{
70 int ret = 0;
71 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010073 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020074 char *p, *q;
75
76 /*
77 * Set to sane values
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020080
81 if( argc == 0 )
82 {
83 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020085 goto exit;
86 }
87
88 opt.filename = DFL_FILENAME;
89
90 for( i = 1; i < argc; i++ )
91 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020092 p = argv[i];
93 if( ( q = strchr( p, '=' ) ) == NULL )
94 goto usage;
95 *q++ = '\0';
96
97 if( strcmp( p, "filename" ) == 0 )
98 opt.filename = q;
99 else
100 goto usage;
101 }
102
103 /*
104 * 1.1. Load the CSR
105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200107 fflush( stdout );
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110
111 if( ret != 0 )
112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret );
114 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200115 goto exit;
116 }
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200119
120 /*
121 * 1.2 Print the CSR
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_printf( " . CSR information ...\n" );
124 ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200125 if( ret == -1 )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret );
128 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200129 goto exit;
130 }
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200133
134exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_x509_csr_free( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200136
137#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200139 fflush( stdout ); getchar();
140#endif
141
142 return( ret );
143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
145 MBEDTLS_FS_IO */