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 | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://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 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 36 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 37 | defined(POLARSSL_X509_CSR_PARSE_C) && defined(POLARSSL_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 38 | #include "polarssl/x509_csr.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 39 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #endif |
| 44 | |
| 45 | #define DFL_FILENAME "cert.req" |
| 46 | #define DFL_DEBUG_LEVEL 0 |
| 47 | |
| 48 | #define USAGE \ |
| 49 | "\n usage: req_app param=<>...\n" \ |
| 50 | "\n acceptable parameters:\n" \ |
| 51 | " filename=%%s default: cert.req\n" \ |
| 52 | "\n" |
| 53 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 54 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | !defined(POLARSSL_X509_CSR_PARSE_C) || !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 56 | int main( void ) |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 57 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 58 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 59 | "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] | 60 | return( 0 ); |
| 61 | } |
| 62 | #else |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 63 | /* |
| 64 | * global options |
| 65 | */ |
| 66 | struct options |
| 67 | { |
| 68 | const char *filename; /* filename of the certificate request */ |
| 69 | } opt; |
| 70 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 71 | int main( int argc, char *argv[] ) |
| 72 | { |
| 73 | int ret = 0; |
| 74 | unsigned char buf[100000]; |
| 75 | x509_csr csr; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 76 | int i; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 77 | char *p, *q; |
| 78 | |
| 79 | /* |
| 80 | * Set to sane values |
| 81 | */ |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 82 | x509_csr_init( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 83 | |
| 84 | if( argc == 0 ) |
| 85 | { |
| 86 | usage: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 87 | polarssl_printf( USAGE ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 88 | goto exit; |
| 89 | } |
| 90 | |
| 91 | opt.filename = DFL_FILENAME; |
| 92 | |
| 93 | for( i = 1; i < argc; i++ ) |
| 94 | { |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 95 | p = argv[i]; |
| 96 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 97 | goto usage; |
| 98 | *q++ = '\0'; |
| 99 | |
| 100 | if( strcmp( p, "filename" ) == 0 ) |
| 101 | opt.filename = q; |
| 102 | else |
| 103 | goto usage; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * 1.1. Load the CSR |
| 108 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 109 | polarssl_printf( "\n . Loading the CSR ..." ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 110 | fflush( stdout ); |
| 111 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 112 | ret = x509_csr_parse_file( &csr, opt.filename ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 113 | |
| 114 | if( ret != 0 ) |
| 115 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 116 | polarssl_printf( " failed\n ! x509_csr_parse_file returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 117 | x509_csr_free( &csr ); |
| 118 | goto exit; |
| 119 | } |
| 120 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 121 | polarssl_printf( " ok\n" ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * 1.2 Print the CSR |
| 125 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 126 | polarssl_printf( " . CSR information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 127 | ret = x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 128 | if( ret == -1 ) |
| 129 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 130 | polarssl_printf( " failed\n ! x509_csr_info returned %d\n\n", ret ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 131 | x509_csr_free( &csr ); |
| 132 | goto exit; |
| 133 | } |
| 134 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 135 | polarssl_printf( "%s\n", buf ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 136 | |
| 137 | exit: |
| 138 | x509_csr_free( &csr ); |
| 139 | |
| 140 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 141 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 142 | fflush( stdout ); getchar(); |
| 143 | #endif |
| 144 | |
| 145 | return( ret ); |
| 146 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 147 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_CSR_PARSE_C && |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 148 | POLARSSL_FS_IO */ |