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