blob: 8c0e3fed501f94dc902ba0f740916872f62e99a3 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/config.h"
35
36#include "polarssl/x509.h"
37
38#define DFL_FILENAME "cert.req"
39#define DFL_DEBUG_LEVEL 0
40
41/*
42 * global options
43 */
44struct options
45{
46 const char *filename; /* filename of the certificate request */
47} opt;
48
49#define USAGE \
50 "\n usage: req_app param=<>...\n" \
51 "\n acceptable parameters:\n" \
52 " filename=%%s default: cert.req\n" \
53 "\n"
54
55#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
56 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
57int main( int argc, char *argv[] )
58{
59 ((void) argc);
60 ((void) argv);
61
62 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
63 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
64 return( 0 );
65}
66#else
67int main( int argc, char *argv[] )
68{
69 int ret = 0;
70 unsigned char buf[100000];
71 x509_csr csr;
72 int i, j, n;
73 char *p, *q;
74
75 /*
76 * Set to sane values
77 */
78 memset( &csr, 0, sizeof( x509_csr ) );
79
80 if( argc == 0 )
81 {
82 usage:
83 printf( USAGE );
84 goto exit;
85 }
86
87 opt.filename = DFL_FILENAME;
88
89 for( i = 1; i < argc; i++ )
90 {
91 n = strlen( argv[i] );
92
93 for( j = 0; j < n; j++ )
94 {
95 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
96 argv[i][j] |= 0x20;
97 }
98
99 p = argv[i];
100 if( ( q = strchr( p, '=' ) ) == NULL )
101 goto usage;
102 *q++ = '\0';
103
104 if( strcmp( p, "filename" ) == 0 )
105 opt.filename = q;
106 else
107 goto usage;
108 }
109
110 /*
111 * 1.1. Load the CSR
112 */
113 printf( "\n . Loading the CSR ..." );
114 fflush( stdout );
115
116 ret = x509parse_csrfile( &csr, opt.filename );
117
118 if( ret != 0 )
119 {
120 printf( " failed\n ! x509parse_csr returned %d\n\n", ret );
121 x509_csr_free( &csr );
122 goto exit;
123 }
124
125 printf( " ok\n" );
126
127 /*
128 * 1.2 Print the CSR
129 */
130 printf( " . CSR information ...\n" );
131 ret = x509parse_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
132 if( ret == -1 )
133 {
134 printf( " failed\n ! x509parse_csr_info returned %d\n\n", ret );
135 x509_csr_free( &csr );
136 goto exit;
137 }
138
139 printf( "%s\n", buf );
140
141exit:
142 x509_csr_free( &csr );
143
144#if defined(_WIN32)
145 printf( " + Press Enter to exit this program.\n" );
146 fflush( stdout ); getchar();
147#endif
148
149 return( ret );
150}
151#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C &&
152 POLARSSL_FS_IO */