blob: bf9610af2a5423c6710cd47e660aecd2dbbc0b06 [file] [log] [blame]
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001/*
2 * Certificate request reading application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerf9f377e2013-09-09 15:35:10 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerf9f377e2013-09-09 15:35:10 +02007 *
Paul Bakkerf9f377e2013-09-09 15:35:10 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakkerf9f377e2013-09-09 15:35:10 +020035#include <string.h>
36#include <stdlib.h>
37#include <stdio.h>
38
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039#include "polarssl/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020040
Paul Bakker80d44fe2013-09-09 15:59:20 +020041#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042 !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakker80d44fe2013-09-09 15:59:20 +020043int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
Rich Evansf90016a2015-01-19 14:26:37 +000048 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049 "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020050 return( 0 );
51}
52#else
53
Paul Bakkerf9f377e2013-09-09 15:35:10 +020054#define DFL_FILENAME "cert.req"
55#define DFL_DEBUG_LEVEL 0
56
57/*
58 * global options
59 */
60struct options
61{
62 const char *filename; /* filename of the certificate request */
63} opt;
64
65#define USAGE \
66 "\n usage: req_app param=<>...\n" \
67 "\n acceptable parameters:\n" \
68 " filename=%%s default: cert.req\n" \
69 "\n"
70
Paul Bakkerf9f377e2013-09-09 15:35:10 +020071int main( int argc, char *argv[] )
72{
73 int ret = 0;
74 unsigned char buf[100000];
75 x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010076 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020077 char *p, *q;
78
79 /*
80 * Set to sane values
81 */
Paul Bakker369d2eb2013-09-18 11:58:25 +020082 x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020083
84 if( argc == 0 )
85 {
86 usage:
Rich Evansf90016a2015-01-19 14:26:37 +000087 polarssl_printf( USAGE );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020088 goto exit;
89 }
90
91 opt.filename = DFL_FILENAME;
92
93 for( i = 1; i < argc; i++ )
94 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020095 p = argv[i];
96 if( ( q = strchr( p, '=' ) ) == NULL )
97 goto usage;
98 *q++ = '\0';
99
100 if( strcmp( p, "filename" ) == 0 )
101 opt.filename = q;
102 else
103 goto usage;
104 }
105
106 /*
107 * 1.1. Load the CSR
108 */
Rich Evansf90016a2015-01-19 14:26:37 +0000109 polarssl_printf( "\n . Loading the CSR ..." );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110 fflush( stdout );
111
Paul Bakkerddf26b42013-09-18 13:46:23 +0200112 ret = x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200113
114 if( ret != 0 )
115 {
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200117 x509_csr_free( &csr );
118 goto exit;
119 }
120
Rich Evansf90016a2015-01-19 14:26:37 +0000121 polarssl_printf( " ok\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200122
123 /*
124 * 1.2 Print the CSR
125 */
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " . CSR information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200128 if( ret == -1 )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " failed\n ! x509_csr_info returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200131 x509_csr_free( &csr );
132 goto exit;
133 }
134
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( "%s\n", buf );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200136
137exit:
138 x509_csr_free( &csr );
139
140#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200142 fflush( stdout ); getchar();
143#endif
144
145 return( ret );
146}
Paul Bakker36713e82013-09-17 13:25:29 +0200147#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C &&
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200148 POLARSSL_FS_IO */