Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 32 | #include "polarssl/x509_csr.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 33 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 34 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 36 | int 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 42 | "POLARSSL_X509_CSR_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 43 | return( 0 ); |
| 44 | } |
| 45 | #else |
| 46 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 47 | #define DFL_FILENAME "cert.req" |
| 48 | #define DFL_DEBUG_LEVEL 0 |
| 49 | |
| 50 | /* |
| 51 | * global options |
| 52 | */ |
| 53 | struct 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 Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 64 | int main( int argc, char *argv[] ) |
| 65 | { |
| 66 | int ret = 0; |
| 67 | unsigned char buf[100000]; |
| 68 | x509_csr csr; |
| 69 | int i, j, n; |
| 70 | char *p, *q; |
| 71 | |
| 72 | /* |
| 73 | * Set to sane values |
| 74 | */ |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 75 | x509_csr_init( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 76 | |
| 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 | { |
| 88 | n = strlen( argv[i] ); |
| 89 | |
| 90 | for( j = 0; j < n; j++ ) |
| 91 | { |
| 92 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 93 | argv[i][j] |= 0x20; |
| 94 | } |
| 95 | |
| 96 | p = argv[i]; |
| 97 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 98 | goto usage; |
| 99 | *q++ = '\0'; |
| 100 | |
| 101 | if( strcmp( p, "filename" ) == 0 ) |
| 102 | opt.filename = q; |
| 103 | else |
| 104 | goto usage; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * 1.1. Load the CSR |
| 109 | */ |
| 110 | printf( "\n . Loading the CSR ..." ); |
| 111 | fflush( stdout ); |
| 112 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 113 | ret = x509_csr_parse_file( &csr, opt.filename ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 114 | |
| 115 | if( ret != 0 ) |
| 116 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 117 | printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 118 | x509_csr_free( &csr ); |
| 119 | goto exit; |
| 120 | } |
| 121 | |
| 122 | printf( " ok\n" ); |
| 123 | |
| 124 | /* |
| 125 | * 1.2 Print the CSR |
| 126 | */ |
| 127 | printf( " . CSR information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 128 | ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 129 | if( ret == -1 ) |
| 130 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 131 | printf( " failed\n ! x509_csr_info returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 132 | x509_csr_free( &csr ); |
| 133 | goto exit; |
| 134 | } |
| 135 | |
| 136 | printf( "%s\n", buf ); |
| 137 | |
| 138 | exit: |
| 139 | x509_csr_free( &csr ); |
| 140 | |
| 141 | #if defined(_WIN32) |
| 142 | printf( " + Press Enter to exit this program.\n" ); |
| 143 | fflush( stdout ); getchar(); |
| 144 | #endif |
| 145 | |
| 146 | return( ret ); |
| 147 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 148 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C && |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 149 | POLARSSL_FS_IO */ |