blob: f502a12cbf22320aa9cf9db8f08a2794db855ba6 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerf9f377e2013-09-09 15:35:10 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakkerf9f377e2013-09-09 15:35:10 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020029
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020035
Paul Bakker80d44fe2013-09-09 15:59:20 +020036#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037 !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakker80d44fe2013-09-09 15:59:20 +020038int main( int argc, char *argv[] )
39{
40 ((void) argc);
41 ((void) argv);
42
43 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044 "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020045 return( 0 );
46}
47#else
48
Paul Bakkerf9f377e2013-09-09 15:35:10 +020049#define DFL_FILENAME "cert.req"
50#define DFL_DEBUG_LEVEL 0
51
52/*
53 * global options
54 */
55struct options
56{
57 const char *filename; /* filename of the certificate request */
58} opt;
59
60#define USAGE \
61 "\n usage: req_app param=<>...\n" \
62 "\n acceptable parameters:\n" \
63 " filename=%%s default: cert.req\n" \
64 "\n"
65
Paul Bakkerf9f377e2013-09-09 15:35:10 +020066int main( int argc, char *argv[] )
67{
68 int ret = 0;
69 unsigned char buf[100000];
70 x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010071 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020072 char *p, *q;
73
74 /*
75 * Set to sane values
76 */
Paul Bakker369d2eb2013-09-18 11:58:25 +020077 x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020078
79 if( argc == 0 )
80 {
81 usage:
82 printf( USAGE );
83 goto exit;
84 }
85
86 opt.filename = DFL_FILENAME;
87
88 for( i = 1; i < argc; i++ )
89 {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020090 p = argv[i];
91 if( ( q = strchr( p, '=' ) ) == NULL )
92 goto usage;
93 *q++ = '\0';
94
95 if( strcmp( p, "filename" ) == 0 )
96 opt.filename = q;
97 else
98 goto usage;
99 }
100
101 /*
102 * 1.1. Load the CSR
103 */
104 printf( "\n . Loading the CSR ..." );
105 fflush( stdout );
106
Paul Bakkerddf26b42013-09-18 13:46:23 +0200107 ret = x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200108
109 if( ret != 0 )
110 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200111 printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200112 x509_csr_free( &csr );
113 goto exit;
114 }
115
116 printf( " ok\n" );
117
118 /*
119 * 1.2 Print the CSR
120 */
121 printf( " . CSR information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200122 ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200123 if( ret == -1 )
124 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200125 printf( " failed\n ! x509_csr_info returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200126 x509_csr_free( &csr );
127 goto exit;
128 }
129
130 printf( "%s\n", buf );
131
132exit:
133 x509_csr_free( &csr );
134
135#if defined(_WIN32)
136 printf( " + Press Enter to exit this program.\n" );
137 fflush( stdout ); getchar();
138#endif
139
140 return( ret );
141}
Paul Bakker36713e82013-09-17 13:25:29 +0200142#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C &&
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200143 POLARSSL_FS_IO */