blob: 83e2546eda586cb1213bc5478088e418d10396fe [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é-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Peter Kolbus9a969b62018-12-11 13:55:56 -060025 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000026 defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010030 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
31 "MBEDTLS_X509_REMOVE_INFO defined.\n");
32 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020033}
34#else
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_csr.h"
Paul Bakkerf9f377e2013-09-09 15:35:10 +020037
Rich Evans18b78c72015-02-11 14:06:19 +000038#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041
42#define DFL_FILENAME "cert.req"
43#define DFL_DEBUG_LEVEL 0
44
45#define USAGE \
46 "\n usage: req_app param=<>...\n" \
47 "\n acceptable parameters:\n" \
48 " filename=%%s default: cert.req\n" \
49 "\n"
50
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Paul Bakkerf9f377e2013-09-09 15:35:10 +020052/*
53 * global options
54 */
Gilles Peskine449bd832023-01-11 14:50:10 +010055struct options {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020056 const char *filename; /* filename of the certificate request */
57} opt;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059int main(int argc, char *argv[])
Paul Bakkerf9f377e2013-09-09 15:35:10 +020060{
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +010061 int ret = 1;
62 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020063 unsigned char buf[100000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_x509_csr csr;
Paul Bakkerc97f9f62013-11-30 15:13:02 +010065 int i;
Paul Bakkerf9f377e2013-09-09 15:35:10 +020066 char *p, *q;
67
68 /*
69 * Set to sane values
70 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_x509_csr_init(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020072
Aditya Deshpande644a5c02023-01-30 15:58:50 +000073 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010074usage:
75 mbedtls_printf(USAGE);
Paul Bakkerf9f377e2013-09-09 15:35:10 +020076 goto exit;
77 }
78
79 opt.filename = DFL_FILENAME;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 for (i = 1; i < argc; i++) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020082 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020084 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020086 *q++ = '\0';
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (strcmp(p, "filename") == 0) {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020089 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 } else {
Paul Bakkerf9f377e2013-09-09 15:35:10 +020091 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +020093 }
94
95 /*
96 * 1.1. Load the CSR
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_printf("\n . Loading the CSR ...");
99 fflush(stdout);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (ret != 0) {
104 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret);
105 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200106 goto exit;
107 }
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_printf(" ok\n");
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200110
111 /*
112 * 1.2 Print the CSR
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_printf(" . CSR information ...\n");
115 ret = mbedtls_x509_csr_info((char *) buf, sizeof(buf) - 1, " ", &csr);
116 if (ret == -1) {
117 mbedtls_printf(" failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret);
118 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf("%s\n", buf);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200123
Andres Amaya Garcia57a0c9b2018-04-29 21:51:47 +0100124 exit_code = MBEDTLS_EXIT_SUCCESS;
125
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_x509_csr_free(&csr);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_exit(exit_code);
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
132 MBEDTLS_FS_IO */