Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate request reading application |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | #include "polarssl/x509_csr.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 34 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 35 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 36 | !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 37 | int main( int argc, char *argv[] ) |
| 38 | { |
| 39 | ((void) argc); |
| 40 | ((void) argv); |
| 41 | |
| 42 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 43 | "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] | 44 | return( 0 ); |
| 45 | } |
| 46 | #else |
| 47 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 48 | #define DFL_FILENAME "cert.req" |
| 49 | #define DFL_DEBUG_LEVEL 0 |
| 50 | |
| 51 | /* |
| 52 | * global options |
| 53 | */ |
| 54 | struct options |
| 55 | { |
| 56 | const char *filename; /* filename of the certificate request */ |
| 57 | } opt; |
| 58 | |
| 59 | #define USAGE \ |
| 60 | "\n usage: req_app param=<>...\n" \ |
| 61 | "\n acceptable parameters:\n" \ |
| 62 | " filename=%%s default: cert.req\n" \ |
| 63 | "\n" |
| 64 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 65 | int main( int argc, char *argv[] ) |
| 66 | { |
| 67 | int ret = 0; |
| 68 | unsigned char buf[100000]; |
| 69 | x509_csr csr; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 70 | int i; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 71 | char *p, *q; |
| 72 | |
| 73 | /* |
| 74 | * Set to sane values |
| 75 | */ |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 76 | x509_csr_init( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 77 | |
| 78 | if( argc == 0 ) |
| 79 | { |
| 80 | usage: |
| 81 | printf( USAGE ); |
| 82 | goto exit; |
| 83 | } |
| 84 | |
| 85 | opt.filename = DFL_FILENAME; |
| 86 | |
| 87 | for( i = 1; i < argc; i++ ) |
| 88 | { |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 89 | p = argv[i]; |
| 90 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 91 | goto usage; |
| 92 | *q++ = '\0'; |
| 93 | |
| 94 | if( strcmp( p, "filename" ) == 0 ) |
| 95 | opt.filename = q; |
| 96 | else |
| 97 | goto usage; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * 1.1. Load the CSR |
| 102 | */ |
| 103 | printf( "\n . Loading the CSR ..." ); |
| 104 | fflush( stdout ); |
| 105 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 106 | ret = x509_csr_parse_file( &csr, opt.filename ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 107 | |
| 108 | if( ret != 0 ) |
| 109 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 110 | printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 111 | x509_csr_free( &csr ); |
| 112 | goto exit; |
| 113 | } |
| 114 | |
| 115 | printf( " ok\n" ); |
| 116 | |
| 117 | /* |
| 118 | * 1.2 Print the CSR |
| 119 | */ |
| 120 | printf( " . CSR information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 121 | ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 122 | if( ret == -1 ) |
| 123 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 124 | printf( " failed\n ! x509_csr_info returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 125 | x509_csr_free( &csr ); |
| 126 | goto exit; |
| 127 | } |
| 128 | |
| 129 | printf( "%s\n", buf ); |
| 130 | |
| 131 | exit: |
| 132 | x509_csr_free( &csr ); |
| 133 | |
| 134 | #if defined(_WIN32) |
| 135 | printf( " + Press Enter to exit this program.\n" ); |
| 136 | fflush( stdout ); getchar(); |
| 137 | #endif |
| 138 | |
| 139 | return( ret ); |
| 140 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 141 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C && |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 142 | POLARSSL_FS_IO */ |