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 #include "mbedtls/build_info.h"
9
10 #include "mbedtls/platform.h"
11
12 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
13 !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
14 defined(MBEDTLS_X509_REMOVE_INFO)
main(void)15 int main(void)
16 {
17 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
18 "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
19 "MBEDTLS_X509_REMOVE_INFO defined.\n");
20 mbedtls_exit(0);
21 }
22 #else
23
24 #include "mbedtls/x509_csr.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define DFL_FILENAME "cert.req"
31 #define DFL_DEBUG_LEVEL 0
32
33 #define USAGE \
34 "\n usage: req_app param=<>...\n" \
35 "\n acceptable parameters:\n" \
36 " filename=%%s default: cert.req\n" \
37 "\n"
38
39
40 /*
41 * global options
42 */
43 struct options {
44 const char *filename; /* filename of the certificate request */
45 } opt;
46
main(int argc,char * argv[])47 int main(int argc, char *argv[])
48 {
49 int ret = 1;
50 int exit_code = MBEDTLS_EXIT_FAILURE;
51 unsigned char buf[100000];
52 mbedtls_x509_csr csr;
53 int i;
54 char *p, *q;
55
56 /*
57 * Set to sane values
58 */
59 mbedtls_x509_csr_init(&csr);
60
61 #if defined(MBEDTLS_USE_PSA_CRYPTO)
62 psa_status_t status = psa_crypto_init();
63 if (status != PSA_SUCCESS) {
64 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
65 (int) status);
66 goto exit;
67 }
68 #endif /* MBEDTLS_USE_PSA_CRYPTO */
69
70 if (argc < 2) {
71 usage:
72 mbedtls_printf(USAGE);
73 goto exit;
74 }
75
76 opt.filename = DFL_FILENAME;
77
78 for (i = 1; i < argc; i++) {
79 p = argv[i];
80 if ((q = strchr(p, '=')) == NULL) {
81 goto usage;
82 }
83 *q++ = '\0';
84
85 if (strcmp(p, "filename") == 0) {
86 opt.filename = q;
87 } else {
88 goto usage;
89 }
90 }
91
92 /*
93 * 1.1. Load the CSR
94 */
95 mbedtls_printf("\n . Loading the CSR ...");
96 fflush(stdout);
97
98 ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
99
100 if (ret != 0) {
101 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file returned %d\n\n", ret);
102 mbedtls_x509_csr_free(&csr);
103 goto exit;
104 }
105
106 mbedtls_printf(" ok\n");
107
108 /*
109 * 1.2 Print the CSR
110 */
111 mbedtls_printf(" . CSR information ...\n");
112 ret = mbedtls_x509_csr_info((char *) buf, sizeof(buf) - 1, " ", &csr);
113 if (ret == -1) {
114 mbedtls_printf(" failed\n ! mbedtls_x509_csr_info returned %d\n\n", ret);
115 mbedtls_x509_csr_free(&csr);
116 goto exit;
117 }
118
119 mbedtls_printf("%s\n", buf);
120
121 exit_code = MBEDTLS_EXIT_SUCCESS;
122
123 exit:
124 mbedtls_x509_csr_free(&csr);
125 #if defined(MBEDTLS_USE_PSA_CRYPTO)
126 mbedtls_psa_crypto_free();
127 #endif /* MBEDTLS_USE_PSA_CRYPTO */
128
129 mbedtls_exit(exit_code);
130 }
131 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
132 MBEDTLS_FS_IO */
133