• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Certificate request reading application
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  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.
18  */
19 
20 #include "mbedtls/build_info.h"
21 
22 #include "mbedtls/platform.h"
23 
24 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
25     !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
26     defined(MBEDTLS_X509_REMOVE_INFO)
main(void)27 int main(void)
28 {
29     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
30                    "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);
33 }
34 #else
35 
36 #include "mbedtls/x509_csr.h"
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
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 
51 
52 /*
53  * global options
54  */
55 struct options {
56     const char *filename;       /* filename of the certificate request  */
57 } opt;
58 
main(int argc,char * argv[])59 int main(int argc, char *argv[])
60 {
61     int ret = 1;
62     int exit_code = MBEDTLS_EXIT_FAILURE;
63     unsigned char buf[100000];
64     mbedtls_x509_csr csr;
65     int i;
66     char *p, *q;
67 
68     /*
69      * Set to sane values
70      */
71     mbedtls_x509_csr_init(&csr);
72 
73     if (argc < 2) {
74 usage:
75         mbedtls_printf(USAGE);
76         goto exit;
77     }
78 
79     opt.filename            = DFL_FILENAME;
80 
81     for (i = 1; i < argc; i++) {
82         p = argv[i];
83         if ((q = strchr(p, '=')) == NULL) {
84             goto usage;
85         }
86         *q++ = '\0';
87 
88         if (strcmp(p, "filename") == 0) {
89             opt.filename = q;
90         } else {
91             goto usage;
92         }
93     }
94 
95     /*
96      * 1.1. Load the CSR
97      */
98     mbedtls_printf("\n  . Loading the CSR ...");
99     fflush(stdout);
100 
101     ret = mbedtls_x509_csr_parse_file(&csr, opt.filename);
102 
103     if (ret != 0) {
104         mbedtls_printf(" failed\n  !  mbedtls_x509_csr_parse_file returned %d\n\n", ret);
105         mbedtls_x509_csr_free(&csr);
106         goto exit;
107     }
108 
109     mbedtls_printf(" ok\n");
110 
111     /*
112      * 1.2 Print the CSR
113      */
114     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);
119         goto exit;
120     }
121 
122     mbedtls_printf("%s\n", buf);
123 
124     exit_code = MBEDTLS_EXIT_SUCCESS;
125 
126 exit:
127     mbedtls_x509_csr_free(&csr);
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