Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate request reading application |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 55 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 56 | #include <stdio.h> |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 57 | #include <stdlib.h> |
| 58 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 59 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 60 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 61 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 62 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ |
| 65 | !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 66 | int main( void ) |
| 67 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " |
| 69 | "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n"); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 70 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 71 | } |
| 72 | #else |
| 73 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 74 | #include "mbedtls/x509_csr.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 75 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 76 | #include <stdio.h> |
| 77 | #include <stdlib.h> |
| 78 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 79 | |
| 80 | #define DFL_FILENAME "cert.req" |
| 81 | #define DFL_DEBUG_LEVEL 0 |
| 82 | |
| 83 | #define USAGE \ |
| 84 | "\n usage: req_app param=<>...\n" \ |
| 85 | "\n acceptable parameters:\n" \ |
| 86 | " filename=%%s default: cert.req\n" \ |
| 87 | "\n" |
| 88 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 89 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 90 | /* |
| 91 | * global options |
| 92 | */ |
| 93 | struct options |
| 94 | { |
| 95 | const char *filename; /* filename of the certificate request */ |
| 96 | } opt; |
| 97 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 98 | int main( int argc, char *argv[] ) |
| 99 | { |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 100 | int ret = 1; |
| 101 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 102 | unsigned char buf[100000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | mbedtls_x509_csr csr; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 104 | int i; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 105 | char *p, *q; |
| 106 | |
| 107 | /* |
| 108 | * Set to sane values |
| 109 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | mbedtls_x509_csr_init( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 111 | |
| 112 | if( argc == 0 ) |
| 113 | { |
| 114 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_printf( USAGE ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 116 | goto exit; |
| 117 | } |
| 118 | |
| 119 | opt.filename = DFL_FILENAME; |
| 120 | |
| 121 | for( i = 1; i < argc; i++ ) |
| 122 | { |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 123 | p = argv[i]; |
| 124 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 125 | goto usage; |
| 126 | *q++ = '\0'; |
| 127 | |
| 128 | if( strcmp( p, "filename" ) == 0 ) |
| 129 | opt.filename = q; |
| 130 | else |
| 131 | goto usage; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * 1.1. Load the CSR |
| 136 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | mbedtls_printf( "\n . Loading the CSR ..." ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 138 | fflush( stdout ); |
| 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | ret = mbedtls_x509_csr_parse_file( &csr, opt.filename ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 141 | |
| 142 | if( ret != 0 ) |
| 143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_printf( " failed\n ! mbedtls_x509_csr_parse_file 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( " ok\n" ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * 1.2 Print the CSR |
| 153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | mbedtls_printf( " . CSR information ...\n" ); |
| 155 | ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, " ", &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 156 | if( ret == -1 ) |
| 157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_printf( " failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret ); |
| 159 | mbedtls_x509_csr_free( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 160 | goto exit; |
| 161 | } |
| 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_printf( "%s\n", buf ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 164 | |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 165 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 166 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 167 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | mbedtls_x509_csr_free( &csr ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 169 | |
| 170 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 172 | fflush( stdout ); getchar(); |
| 173 | #endif |
| 174 | |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 175 | mbedtls_exit( exit_code ); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 176 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C && |
| 178 | MBEDTLS_FS_IO */ |