blob: 91bb2dc2647d9cbb48da74e9b1c6568004d9c1b4 [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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036#include "polarssl/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020037
Paul Bakker80d44fe2013-09-09 15:59:20 +020038#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039 !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakker80d44fe2013-09-09 15:59:20 +020040int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
45 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046 "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker80d44fe2013-09-09 15:59:20 +020047 return( 0 );
48}
49#else
50
Paul Bakkerf9f377e2013-09-09 15:35:10 +020051#define DFL_FILENAME "cert.req"
52#define DFL_DEBUG_LEVEL 0
53
54/*
55 * global options
56 */
57struct options
58{
59 const char *filename; /* filename of the certificate request */
60} opt;
61
62#define USAGE \
63 "\n usage: req_app param=<>...\n" \
64 "\n acceptable parameters:\n" \
65 " filename=%%s default: cert.req\n" \
66 "\n"
67
Paul Bakkerf9f377e2013-09-09 15:35:10 +020068int main( int argc, char *argv[] )
69{
70 int ret = 0;
71 unsigned char buf[100000];
72 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 */
Paul Bakker369d2eb2013-09-18 11:58:25 +020079 x509_csr_init( &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +020080
81 if( argc == 0 )
82 {
83 usage:
84 printf( USAGE );
85 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 */
106 printf( "\n . Loading the CSR ..." );
107 fflush( stdout );
108
Paul Bakkerddf26b42013-09-18 13:46:23 +0200109 ret = x509_csr_parse_file( &csr, opt.filename );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110
111 if( ret != 0 )
112 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200113 printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200114 x509_csr_free( &csr );
115 goto exit;
116 }
117
118 printf( " ok\n" );
119
120 /*
121 * 1.2 Print the CSR
122 */
123 printf( " . CSR information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200125 if( ret == -1 )
126 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 printf( " failed\n ! x509_csr_info returned %d\n\n", ret );
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200128 x509_csr_free( &csr );
129 goto exit;
130 }
131
132 printf( "%s\n", buf );
133
134exit:
135 x509_csr_free( &csr );
136
137#if defined(_WIN32)
138 printf( " + Press Enter to exit this program.\n" );
139 fflush( stdout ); getchar();
140#endif
141
142 return( ret );
143}
Paul Bakker36713e82013-09-17 13:25:29 +0200144#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C &&
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200145 POLARSSL_FS_IO */