blob: c17ef7554feee35dde8da2f1842e6e92c25c662e [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerf9f377e2013-09-09 15:35:10 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
17 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010018int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
22 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020023}
24#else
25
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020027
Rich Evans18b78c72015-02-11 14:06:19 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000031
32#define DFL_FILENAME "cert.req"
33#define DFL_DEBUG_LEVEL 0
34
35#define USAGE \
36 "\n usage: req_app param=<>...\n" \
37 "\n acceptable parameters:\n" \
38 " filename=%%s default: cert.req\n" \
39 "\n"
40
Simon Butcher63cb97e2018-12-06 17:43:31 +000041
Paul Bakkerf9f377e2013-09-09 15:35:10 +020042/*
43 * global options
44 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045struct options {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020046 const char *filename; /* filename of the certificate request */
47} opt;
48
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049int main(int argc, char *argv[])
Paul Bakkerf9f377e2013-09-09 15:35:10 +020050{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010051 int ret = 1;
52 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020053 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010055 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020056 char *p, *q;
57
Przemek Stekielb00688f2023-04-17 11:10:05 +020058 /*
59 * Set to sane values
60 */
61 mbedtls_x509_csr_init(&csr);
62
Przemek Stekield381d2d2023-04-14 09:26:39 +020063#if defined(MBEDTLS_USE_PSA_CRYPTO)
64 psa_status_t status = psa_crypto_init();
65 if (status != PSA_SUCCESS) {
66 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
67 (int) status);
68 goto exit;
69 }
70#endif /* MBEDTLS_USE_PSA_CRYPTO */
71
Aditya Deshpande0504ac22023-01-30 15:58:50 +000072 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073usage:
74 mbedtls_printf(USAGE);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020075 goto exit;
76 }
77
78 opt.filename = DFL_FILENAME;
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 for (i = 1; i < argc; i++) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020081 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020083 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020085 *q++ = '\0';
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 if (strcmp(p, "filename") == 0) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020088 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 } else {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020090 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020092 }
93
94 /*
95 * 1.1. Load the CSR
96 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 mbedtls_printf("\n . Loading the CSR ...");
98 fflush(stdout);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if (ret != 0) {
103 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret);
104 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200105 goto exit;
106 }
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 mbedtls_printf(" ok\n");
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200109
110 /*
111 * 1.2 Print the CSR
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 mbedtls_printf(" . CSR information ...\n");
114 ret = mbedtls_x509_csr_info((char *) buf, sizeof(buf) - 1, " ", &csr);
115 if (ret == -1) {
116 mbedtls_printf(" failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret);
117 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200118 goto exit;
119 }
120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_printf("%s\n", buf);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200122
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100123 exit_code = MBEDTLS_EXIT_SUCCESS;
124
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200125exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_x509_csr_free(&csr);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200128 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200129#endif /* MBEDTLS_USE_PSA_CRYPTO */
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 */