blob: e2faab43b53704b1c0feead7d74bd2ad4052ac36 [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
Paul Bakker80d44fe2013-09-09 15:59:20 +020038#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
39 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
40int 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 "
46 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
47 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;
73 int i, j, n;
74 char *p, *q;
75
76 /*
77 * Set to sane values
78 */
79 memset( &csr, 0, sizeof( x509_csr ) );
80
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 {
92 n = strlen( argv[i] );
93
94 for( j = 0; j < n; j++ )
95 {
96 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
97 argv[i][j] |= 0x20;
98 }
99
100 p = argv[i];
101 if( ( q = strchr( p, '=' ) ) == NULL )
102 goto usage;
103 *q++ = '\0';
104
105 if( strcmp( p, "filename" ) == 0 )
106 opt.filename = q;
107 else
108 goto usage;
109 }
110
111 /*
112 * 1.1. Load the CSR
113 */
114 printf( "\n . Loading the CSR ..." );
115 fflush( stdout );
116
117 ret = x509parse_csrfile( &csr, opt.filename );
118
119 if( ret != 0 )
120 {
121 printf( " failed\n ! x509parse_csr returned %d\n\n", ret );
122 x509_csr_free( &csr );
123 goto exit;
124 }
125
126 printf( " ok\n" );
127
128 /*
129 * 1.2 Print the CSR
130 */
131 printf( " . CSR information ...\n" );
132 ret = x509parse_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr );
133 if( ret == -1 )
134 {
135 printf( " failed\n ! x509parse_csr_info returned %d\n\n", ret );
136 x509_csr_free( &csr );
137 goto exit;
138 }
139
140 printf( "%s\n", buf );
141
142exit:
143 x509_csr_free( &csr );
144
145#if defined(_WIN32)
146 printf( " + Press Enter to exit this program.\n" );
147 fflush( stdout ); getchar();
148#endif
149
150 return( ret );
151}
152#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C &&
153 POLARSSL_FS_IO */