| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Certificate request reading application | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> | 
| Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 32 | #include <stdlib.h> | 
|  | 33 | #define mbedtls_printf          printf | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 34 | #define mbedtls_exit            exit | 
| Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
| Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 36 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
|  | 37 | #endif /* MBEDTLS_PLATFORM_C */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \ | 
|  | 40 | !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) | 
| Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 41 | int main( void ) | 
|  | 42 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " | 
|  | 44 | "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] | 45 | return( 0 ); | 
|  | 46 | } | 
|  | 47 | #else | 
|  | 48 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/x509_csr.h" | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 50 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 51 | #include <stdio.h> | 
|  | 52 | #include <stdlib.h> | 
|  | 53 | #include <string.h> | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 54 |  | 
|  | 55 | #define DFL_FILENAME            "cert.req" | 
|  | 56 | #define DFL_DEBUG_LEVEL         0 | 
|  | 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 |  | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 64 | #if defined(MBEDTLS_CHECK_PARAMS) | 
|  | 65 | #define mbedtls_exit            exit | 
|  | 66 | void mbedtls_param_failed( const char *failure_condition, | 
|  | 67 | const char *file, | 
|  | 68 | int line ) | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 69 | { | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 70 | mbedtls_printf( "%s:%i: Input param failed - %s\n", | 
|  | 71 | file, line, failure_condition ); | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 72 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); | 
|  | 73 | } | 
|  | 74 | #endif | 
|  | 75 |  | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 76 | /* | 
|  | 77 | * global options | 
|  | 78 | */ | 
|  | 79 | struct options | 
|  | 80 | { | 
|  | 81 | const char *filename;       /* filename of the certificate request  */ | 
|  | 82 | } opt; | 
|  | 83 |  | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 84 | int main( int argc, char *argv[] ) | 
|  | 85 | { | 
| Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 86 | int ret = 1; | 
|  | 87 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 88 | unsigned char buf[100000]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | mbedtls_x509_csr csr; | 
| Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 90 | int i; | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 91 | char *p, *q; | 
|  | 92 |  | 
|  | 93 | /* | 
|  | 94 | * Set to sane values | 
|  | 95 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | mbedtls_x509_csr_init( &csr ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 97 |  | 
|  | 98 | if( argc == 0 ) | 
|  | 99 | { | 
|  | 100 | usage: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | mbedtls_printf( USAGE ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 102 | goto exit; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | opt.filename            = DFL_FILENAME; | 
|  | 106 |  | 
|  | 107 | for( i = 1; i < argc; i++ ) | 
|  | 108 | { | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 109 | p = argv[i]; | 
|  | 110 | if( ( q = strchr( p, '=' ) ) == NULL ) | 
|  | 111 | goto usage; | 
|  | 112 | *q++ = '\0'; | 
|  | 113 |  | 
|  | 114 | if( strcmp( p, "filename" ) == 0 ) | 
|  | 115 | opt.filename = q; | 
|  | 116 | else | 
|  | 117 | goto usage; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | /* | 
|  | 121 | * 1.1. Load the CSR | 
|  | 122 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | mbedtls_printf( "\n  . Loading the CSR ..." ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 124 | fflush( stdout ); | 
|  | 125 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | ret = mbedtls_x509_csr_parse_file( &csr, opt.filename ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 127 |  | 
|  | 128 | if( ret != 0 ) | 
|  | 129 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_printf( " failed\n  !  mbedtls_x509_csr_parse_file returned %d\n\n", ret ); | 
|  | 131 | mbedtls_x509_csr_free( &csr ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 132 | goto exit; | 
|  | 133 | } | 
|  | 134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 136 |  | 
|  | 137 | /* | 
|  | 138 | * 1.2 Print the CSR | 
|  | 139 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_printf( "  . CSR information    ...\n" ); | 
|  | 141 | ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, "      ", &csr ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 142 | if( ret == -1 ) | 
|  | 143 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_printf( " failed\n  !  mbedtls_x509_csr_info returned %d\n\n", ret ); | 
|  | 145 | mbedtls_x509_csr_free( &csr ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 146 | goto exit; | 
|  | 147 | } | 
|  | 148 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | mbedtls_printf( "%s\n", buf ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 150 |  | 
| Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 151 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 152 |  | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 153 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | mbedtls_x509_csr_free( &csr ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 155 |  | 
|  | 156 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 158 | fflush( stdout ); getchar(); | 
|  | 159 | #endif | 
|  | 160 |  | 
| Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 161 | return( exit_code ); | 
| Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 162 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C && | 
|  | 164 | MBEDTLS_FS_IO */ |