blob: ced2d3ecc5b9076c8105a6feffd0227ee6d63b80 [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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#include "polarssl/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020034
Paul Bakker80d44fe2013-09-09 15:59:20 +020035#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036 !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakker80d44fe2013-09-09 15:59:20 +020037int main( int argc, char *argv[] )
38{
39 ((void) argc);
40 ((void) argv);
41
42 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043 "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020044 return( 0 );
45}
46#else
47
Paul Bakkerf9f377e2013-09-09 15:35:10 +020048#define DFL_FILENAME "cert.req"
49#define DFL_DEBUG_LEVEL 0
50
51/*
52 * global options
53 */
54struct options
55{
56 const char *filename; /* filename of the certificate request */
57} opt;
58
59#define USAGE \
60 "\n usage: req_app param=<>...\n" \
61 "\n acceptable parameters:\n" \
62 " filename=%%s default: cert.req\n" \
63 "\n"
64
Paul Bakkerf9f377e2013-09-09 15:35:10 +020065int main( int argc, char *argv[] )
66{
67 int ret = 0;
68 unsigned char buf[100000];
69 x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010070 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020071 char *p, *q;
72
73 /*
74 * Set to sane values
75 */
Paul Bakker369d2eb2013-09-18 11:58:25 +020076 x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020077
78 if( argc == 0 )
79 {
80 usage:
81 printf( USAGE );
82 goto exit;
83 }
84
85 opt.filename = DFL_FILENAME;
86
87 for( i = 1; i < argc; i++ )
88 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020089 p = argv[i];
90 if( ( q = strchr( p, '=' ) ) == NULL )
91 goto usage;
92 *q++ = '\0';
93
94 if( strcmp( p, "filename" ) == 0 )
95 opt.filename = q;
96 else
97 goto usage;
98 }
99
100 /*
101 * 1.1. Load the CSR
102 */
103 printf( "\n . Loading the CSR ..." );
104 fflush( stdout );
105
Paul Bakkerddf26b42013-09-18 13:46:23 +0200106 ret = x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200107
108 if( ret != 0 )
109 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200110 printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200111 x509_csr_free( &csr );
112 goto exit;
113 }
114
115 printf( " ok\n" );
116
117 /*
118 * 1.2 Print the CSR
119 */
120 printf( " . CSR information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200121 ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200122 if( ret == -1 )
123 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 printf( " failed\n ! x509_csr_info returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200125 x509_csr_free( &csr );
126 goto exit;
127 }
128
129 printf( "%s\n", buf );
130
131exit:
132 x509_csr_free( &csr );
133
134#if defined(_WIN32)
135 printf( " + Press Enter to exit this program.\n" );
136 fflush( stdout ); getchar();
137#endif
138
139 return( ret );
140}
Paul Bakker36713e82013-09-17 13:25:29 +0200141#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C &&
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200142 POLARSSL_FS_IO */