blob: 3126c194fcf3d3da163ff67033d3b59c2d7b69de [file] [log] [blame]
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001/*
2 * Certificate request reading application
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerf9f377e2013-09-09 15:35:10 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# 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 Garcia57a0c9b2018-04-29 21:51:47 +010031#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060034 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000035 defined(MBEDTLS_X509_REMOVE_INFO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020037{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038 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é-Gonnard8d649c62015-03-31 15:10:03 +020043}
44#else
45
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046# include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020047
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# include <stdio.h>
49# include <stdlib.h>
50# include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# define DFL_FILENAME "cert.req"
53# define DFL_DEBUG_LEVEL 0
Rich Evans18b78c72015-02-11 14:06:19 +000054
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055# define USAGE \
56 "\n usage: req_app param=<>...\n" \
57 "\n acceptable parameters:\n" \
58 " filename=%%s default: cert.req\n" \
59 "\n"
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Paul Bakkerf9f377e2013-09-09 15:35:10 +020061/*
62 * global options
63 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064struct options {
65 const char *filename; /* filename of the certificate request */
Paul Bakkerf9f377e2013-09-09 15:35:10 +020066} opt;
67
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068int main(int argc, char *argv[])
Paul Bakkerf9f377e2013-09-09 15:35:10 +020069{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010070 int ret = 1;
71 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020072 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010074 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020075 char *p, *q;
76
77 /*
78 * Set to sane values
79 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 mbedtls_x509_csr_init(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020081
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082 if (argc == 0) {
83usage:
84 mbedtls_printf(USAGE);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020085 goto exit;
86 }
87
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020088 opt.filename = DFL_FILENAME;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020089
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090 for (i = 1; i < argc; i++) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020091 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092 if ((q = strchr(p, '=')) == NULL)
Paul Bakkerf9f377e2013-09-09 15:35:10 +020093 goto usage;
94 *q++ = '\0';
95
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if (strcmp(p, "filename") == 0)
Paul Bakkerf9f377e2013-09-09 15:35:10 +020097 opt.filename = q;
98 else
99 goto usage;
100 }
101
102 /*
103 * 1.1. Load the CSR
104 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 mbedtls_printf("\n . Loading the CSR ...");
106 fflush(stdout);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200109
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110 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 Bakkerf9f377e2013-09-09 15:35:10 +0200114 goto exit;
115 }
116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 mbedtls_printf(" ok\n");
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200118
119 /*
120 * 1.2 Print the CSR
121 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122 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 Bakkerf9f377e2013-09-09 15:35:10 +0200128 goto exit;
129 }
130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 mbedtls_printf("%s\n", buf);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200132
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100133 exit_code = MBEDTLS_EXIT_SUCCESS;
134
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200135exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200137
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138# if defined(_WIN32)
139 mbedtls_printf(" + Press Enter to exit this program.\n");
140 fflush(stdout);
141 getchar();
142# endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 mbedtls_exit(exit_code);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200145}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200146#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 MBEDTLS_FS_IO */