1 /*
2 * Certificate request reading application
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
17 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)18 int main(void)
19 {
20 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
21 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
22 mbedtls_exit(0);
23 }
24 #else
25
26 #include "mbedtls/x509_csr.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
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
41
42 /*
43 * global options
44 */
45 struct options {
46 const char *filename; /* filename of the certificate request */
47 } opt;
48
main(int argc,char * argv[])49 int main(int argc, char *argv[])
50 {
51 int ret = 1;
52 int exit_code = MBEDTLS_EXIT_FAILURE;
53 unsigned char buf[100000];
54 mbedtls_x509_csr csr;
55 int i;
56 char *p, *q;
57
58 /*
59 * Set to sane values
60 */
61 mbedtls_x509_csr_init(&csr);
62
63 #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
72 if (argc < 2) {
73 usage:
74 mbedtls_printf(USAGE);
75 goto exit;
76 }
77
78 opt.filename = DFL_FILENAME;
79
80 for (i = 1; i < argc; i++) {
81 p = argv[i];
82 if ((q = strchr(p, '=')) == NULL) {
83 goto usage;
84 }
85 *q++ = '\0';
86
87 if (strcmp(p, "filename") == 0) {
88 opt.filename = q;
89 } else {
90 goto usage;
91 }
92 }
93
94 /*
95 * 1.1. Load the CSR
96 */
97 mbedtls_printf("\n . Loading the CSR ...");
98 fflush(stdout);
99
100 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
101
102 if (ret != 0) {
103 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret);
104 mbedtls_x509_csr_free(&csr);
105 goto exit;
106 }
107
108 mbedtls_printf(" ok\n");
109
110 /*
111 * 1.2 Print the CSR
112 */
113 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);
118 goto exit;
119 }
120
121 mbedtls_printf("%s\n", buf);
122
123 exit_code = MBEDTLS_EXIT_SUCCESS;
124
125 exit:
126 mbedtls_x509_csr_free(&csr);
127 #if defined(MBEDTLS_USE_PSA_CRYPTO)
128 mbedtls_psa_crypto_free();
129 #endif /* MBEDTLS_USE_PSA_CRYPTO */
130
131 #if defined(_WIN32)
132 mbedtls_printf(" + Press Enter to exit this program.\n");
133 fflush(stdout); getchar();
134 #endif
135
136 mbedtls_exit(exit_code);
137 }
138 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
139 MBEDTLS_FS_IO */
140