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 #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9 
10 #include "mbedtls/build_info.h"
11 
12 #include "mbedtls/platform.h"
13 
14 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
15     !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
16     defined(MBEDTLS_X509_REMOVE_INFO)
main(void)17 int main(void)
18 {
19     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
20                    "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
21                    "MBEDTLS_X509_REMOVE_INFO 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     psa_status_t status = psa_crypto_init();
64     if (status != PSA_SUCCESS) {
65         mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
66                         (int) status);
67         goto exit;
68     }
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     mbedtls_psa_crypto_free();
126 
127     mbedtls_exit(exit_code);
128 }
129 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
130           MBEDTLS_FS_IO */
131