Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate request reading application |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 20 | #include "mbedtls/build_info.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_PLATFORM_C) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 23 | # include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 24 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | # include <stdio.h> |
| 26 | # include <stdlib.h> |
| 27 | # define mbedtls_printf printf |
| 28 | # define mbedtls_exit exit |
| 29 | # define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 30 | # define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 31 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 32 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 33 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ |
Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 34 | !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
Chris Jones | ee33c60 | 2020-12-16 11:52:37 +0000 | [diff] [blame] | 35 | defined(MBEDTLS_X509_REMOVE_INFO) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 36 | int main(void) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 37 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | mbedtls_printf( |
| 39 | "MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " |
| 40 | "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined and/or " |
| 41 | "MBEDTLS_X509_REMOVE_INFO defined.\n"); |
| 42 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 43 | } |
| 44 | #else |
| 45 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 46 | # include "mbedtls/x509_csr.h" |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 52 | # define DFL_FILENAME "cert.req" |
| 53 | # define DFL_DEBUG_LEVEL 0 |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 54 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 55 | # define USAGE \ |
| 56 | "\n usage: req_app param=<>...\n" \ |
| 57 | "\n acceptable parameters:\n" \ |
| 58 | " filename=%%s default: cert.req\n" \ |
| 59 | "\n" |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 60 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 61 | /* |
| 62 | * global options |
| 63 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | struct options { |
| 65 | const char *filename; /* filename of the certificate request */ |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 66 | } opt; |
| 67 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | int main(int argc, char *argv[]) |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 69 | { |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 70 | int ret = 1; |
| 71 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 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 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 80 | mbedtls_x509_csr_init(&csr); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 81 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | if (argc == 0) { |
| 83 | usage: |
| 84 | mbedtls_printf(USAGE); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 85 | goto exit; |
| 86 | } |
| 87 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 88 | opt.filename = DFL_FILENAME; |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 89 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 90 | for (i = 1; i < argc; i++) { |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 91 | p = argv[i]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 92 | if ((q = strchr(p, '=')) == NULL) |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 93 | goto usage; |
| 94 | *q++ = '\0'; |
| 95 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 96 | if (strcmp(p, "filename") == 0) |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 97 | opt.filename = q; |
| 98 | else |
| 99 | goto usage; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * 1.1. Load the CSR |
| 104 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 105 | mbedtls_printf("\n . Loading the CSR ..."); |
| 106 | fflush(stdout); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 107 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | ret = mbedtls_x509_csr_parse_file(&csr, opt.filename); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 109 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 110 | if (ret != 0) { |
| 111 | mbedtls_printf( |
| 112 | " failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret); |
| 113 | mbedtls_x509_csr_free(&csr); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 114 | goto exit; |
| 115 | } |
| 116 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 117 | mbedtls_printf(" ok\n"); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * 1.2 Print the CSR |
| 121 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | mbedtls_printf(" . CSR information ...\n"); |
| 123 | ret = mbedtls_x509_csr_info((char *)buf, sizeof(buf) - 1, " ", &csr); |
| 124 | if (ret == -1) { |
| 125 | mbedtls_printf(" failed\n ! mbedtls_x509_csr_info returned %d\n\n", |
| 126 | ret); |
| 127 | mbedtls_x509_csr_free(&csr); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 128 | goto exit; |
| 129 | } |
| 130 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 131 | mbedtls_printf("%s\n", buf); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 132 | |
Andres Amaya Garcia | 57a0c9b | 2018-04-29 21:51:47 +0100 | [diff] [blame] | 133 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 134 | |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 135 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 136 | mbedtls_x509_csr_free(&csr); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 137 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 138 | # if defined(_WIN32) |
| 139 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 140 | fflush(stdout); |
| 141 | getchar(); |
| 142 | # endif |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 144 | mbedtls_exit(exit_code); |
Paul Bakker | f9f377e | 2013-09-09 15:35:10 +0200 | [diff] [blame] | 145 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 146 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C && \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | MBEDTLS_FS_IO */ |