blob: bc9f67fbb314715b3edda66032c72603efd82cb3 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
29 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010030int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
34 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020035}
36#else
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020039
Rich Evans18b78c72015-02-11 14:06:19 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043
44#define DFL_FILENAME "cert.req"
45#define DFL_DEBUG_LEVEL 0
46
47#define USAGE \
48 "\n usage: req_app param=<>...\n" \
49 "\n acceptable parameters:\n" \
50 " filename=%%s default: cert.req\n" \
51 "\n"
52
Simon Butcher63cb97e2018-12-06 17:43:31 +000053
Paul Bakkerf9f377e2013-09-09 15:35:10 +020054/*
55 * global options
56 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057struct options {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020058 const char *filename; /* filename of the certificate request */
59} opt;
60
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061int main(int argc, char *argv[])
Paul Bakkerf9f377e2013-09-09 15:35:10 +020062{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010063 int ret = 1;
64 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020065 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010067 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020068 char *p, *q;
69
70 /*
71 * Set to sane values
72 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_x509_csr_init(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 if (argc == 0) {
76usage:
77 mbedtls_printf(USAGE);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020078 goto exit;
79 }
80
81 opt.filename = DFL_FILENAME;
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 for (i = 1; i < argc; i++) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020084 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020086 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020088 *q++ = '\0';
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 if (strcmp(p, "filename") == 0) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020091 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 } else {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020093 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020095 }
96
97 /*
98 * 1.1. Load the CSR
99 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 mbedtls_printf("\n . Loading the CSR ...");
101 fflush(stdout);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if (ret != 0) {
106 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret);
107 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200108 goto exit;
109 }
110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 mbedtls_printf(" ok\n");
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200112
113 /*
114 * 1.2 Print the CSR
115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_printf(" . CSR information ...\n");
117 ret = mbedtls_x509_csr_info((char *) buf, sizeof(buf) - 1, " ", &csr);
118 if (ret == -1) {
119 mbedtls_printf(" failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret);
120 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200121 goto exit;
122 }
123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 mbedtls_printf("%s\n", buf);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200125
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100126 exit_code = MBEDTLS_EXIT_SUCCESS;
127
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200128exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200130
131#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 mbedtls_printf(" + Press Enter to exit this program.\n");
133 fflush(stdout); getchar();
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200134#endif
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_exit(exit_code);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
139 MBEDTLS_FS_IO */