req_app.c 3.44 KB
Newer Older
1 2 3
/*
 *  Certificate request reading application
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7 8 9
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13 14 15 16 17
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
18
 *
19
 *  This file is part of mbed TLS (https://tls.mbed.org)
20 21
 */

22
#if !defined(MBEDTLS_CONFIG_FILE)
23
#include "mbedtls/config.h"
24
#else
25
#include MBEDTLS_CONFIG_FILE
26
#endif
27

28
#if defined(MBEDTLS_PLATFORM_C)
29
#include "mbedtls/platform.h"
30
#else
Rich Evans's avatar
Rich Evans committed
31
#include <stdio.h>
32
#define mbedtls_printf     printf
33 34
#endif

35 36
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
    !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO)
37 38
int main( void )
{
39 40
    mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
           "MBEDTLS_X509_CSR_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
41 42 43 44
    return( 0 );
}
#else

45
#include "mbedtls/x509_csr.h"
Rich Evans's avatar
Rich Evans committed
46

47
#include <stdio.h>
Rich Evans's avatar
Rich Evans committed
48 49
#include <stdlib.h>
#include <string.h>
50

Rich Evans's avatar
Rich Evans committed
51 52 53 54 55 56 57 58
#define DFL_FILENAME            "cert.req"
#define DFL_DEBUG_LEVEL         0

#define USAGE \
    "\n usage: req_app param=<>...\n"                   \
    "\n acceptable parameters:\n"                       \
    "    filename=%%s         default: cert.req\n"      \
    "\n"
59 60 61 62 63 64 65 66 67 68 69 70 71

/*
 * global options
 */
struct options
{
    const char *filename;       /* filename of the certificate request  */
} opt;

int main( int argc, char *argv[] )
{
    int ret = 0;
    unsigned char buf[100000];
72
    mbedtls_x509_csr csr;
73
    int i;
74 75 76 77 78
    char *p, *q;

    /*
     * Set to sane values
     */
79
    mbedtls_x509_csr_init( &csr );
80 81 82 83

    if( argc == 0 )
    {
    usage:
84
        mbedtls_printf( USAGE );
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        goto exit;
    }

    opt.filename            = DFL_FILENAME;

    for( i = 1; i < argc; i++ )
    {
        p = argv[i];
        if( ( q = strchr( p, '=' ) ) == NULL )
            goto usage;
        *q++ = '\0';

        if( strcmp( p, "filename" ) == 0 )
            opt.filename = q;
        else
            goto usage;
    }

    /*
     * 1.1. Load the CSR
     */
106
    mbedtls_printf( "\n  . Loading the CSR ..." );
107 108
    fflush( stdout );

109
    ret = mbedtls_x509_csr_parse_file( &csr, opt.filename );
110 111 112

    if( ret != 0 )
    {
113 114
        mbedtls_printf( " failed\n  !  mbedtls_x509_csr_parse_file returned %d\n\n", ret );
        mbedtls_x509_csr_free( &csr );
115 116 117
        goto exit;
    }

118
    mbedtls_printf( " ok\n" );
119 120 121 122

    /*
     * 1.2 Print the CSR
     */
123 124
    mbedtls_printf( "  . CSR information    ...\n" );
    ret = mbedtls_x509_csr_info( (char *) buf, sizeof( buf ) - 1, "      ", &csr );
125 126
    if( ret == -1 )
    {
127 128
        mbedtls_printf( " failed\n  !  mbedtls_x509_csr_info returned %d\n\n", ret );
        mbedtls_x509_csr_free( &csr );
129 130 131
        goto exit;
    }

132
    mbedtls_printf( "%s\n", buf );
133 134

exit:
135
    mbedtls_x509_csr_free( &csr );
136 137

#if defined(_WIN32)
138
    mbedtls_printf( "  + Press Enter to exit this program.\n" );
139 140 141 142 143
    fflush( stdout ); getchar();
#endif

    return( ret );
}
144 145
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C &&
          MBEDTLS_FS_IO */