blob: 1f9d62841fe5054280171f155a494a37ab606874 [file] [log] [blame]
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001/*
2 * Certificate request reading application
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#include "polarssl/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020033
Paul Bakker80d44fe2013-09-09 15:59:20 +020034#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035 !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakker80d44fe2013-09-09 15:59:20 +020036int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042 "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020043 return( 0 );
44}
45#else
46
Paul Bakkerf9f377e2013-09-09 15:35:10 +020047#define DFL_FILENAME "cert.req"
48#define DFL_DEBUG_LEVEL 0
49
50/*
51 * global options
52 */
53struct options
54{
55 const char *filename; /* filename of the certificate request */
56} opt;
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 +020064int main( int argc, char *argv[] )
65{
66 int ret = 0;
67 unsigned char buf[100000];
68 x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010069 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020070 char *p, *q;
71
72 /*
73 * Set to sane values
74 */
Paul Bakker369d2eb2013-09-18 11:58:25 +020075 x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020076
77 if( argc == 0 )
78 {
79 usage:
80 printf( USAGE );
81 goto exit;
82 }
83
84 opt.filename = DFL_FILENAME;
85
86 for( i = 1; i < argc; i++ )
87 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020088 p = argv[i];
89 if( ( q = strchr( p, '=' ) ) == NULL )
90 goto usage;
91 *q++ = '\0';
92
93 if( strcmp( p, "filename" ) == 0 )
94 opt.filename = q;
95 else
96 goto usage;
97 }
98
99 /*
100 * 1.1. Load the CSR
101 */
102 printf( "\n . Loading the CSR ..." );
103 fflush( stdout );
104
Paul Bakkerddf26b42013-09-18 13:46:23 +0200105 ret = x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200106
107 if( ret != 0 )
108 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110 x509_csr_free( &csr );
111 goto exit;
112 }
113
114 printf( " ok\n" );
115
116 /*
117 * 1.2 Print the CSR
118 */
119 printf( " . CSR information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200120 ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200121 if( ret == -1 )
122 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200123 printf( " failed\n ! x509_csr_info returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200124 x509_csr_free( &csr );
125 goto exit;
126 }
127
128 printf( "%s\n", buf );
129
130exit:
131 x509_csr_free( &csr );
132
133#if defined(_WIN32)
134 printf( " + Press Enter to exit this program.\n" );
135 fflush( stdout ); getchar();
136#endif
137
138 return( ret );
139}
Paul Bakker36713e82013-09-17 13:25:29 +0200140#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C &&
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200141 POLARSSL_FS_IO */