x509write_csr.c 8.4 KB
Newer Older
1 2 3
/*
 *  X.509 Certificate Signing Request writing
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
/*
 * References:
 * - CSRs: PKCS#10 v1.7 aka RFC 2986
 * - attributes: PKCS#9 v2.0 aka RFC 2985
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
28
#if !defined(MBEDTLS_CONFIG_FILE)
29
#include "mbedtls/config.h"
30
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
31
#include MBEDTLS_CONFIG_FILE
32
#endif
33

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
34
#if defined(MBEDTLS_X509_CSR_WRITE_C)
35

36 37 38
#include "mbedtls/x509_csr.h"
#include "mbedtls/oid.h"
#include "mbedtls/asn1write.h"
39

40 41 42
#include <string.h>
#include <stdlib.h>

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
43
#if defined(MBEDTLS_PEM_WRITE_C)
44
#include "mbedtls/pem.h"
45 46
#endif

47
/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
48
static void mbedtls_zeroize( void *v, size_t n ) {
49 50 51
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
52
void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
53
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
54
    memset( ctx, 0, sizeof(mbedtls_x509write_csr) );
55 56
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
57
void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
58
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
59 60
    mbedtls_asn1_free_named_data_list( &ctx->subject );
    mbedtls_asn1_free_named_data_list( &ctx->extensions );
61

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
62
    mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_csr) );
63 64
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
65
void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
66 67 68 69
{
    ctx->md_alg = md_alg;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
70
void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
71 72 73 74
{
    ctx->key = key;
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
75
int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker's avatar
Paul Bakker committed
76
                                    const char *subject_name )
77
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
78
    return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
79 80
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
81
int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
82 83 84
                                 const char *oid, size_t oid_len,
                                 const unsigned char *val, size_t val_len )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
85
    return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
86 87 88
                               0, val, val_len );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
89
int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
90 91 92 93 94 95 96
{
    unsigned char buf[4];
    unsigned char *c;
    int ret;

    c = buf + 4;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
97
    if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
98 99
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
100 101
    ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
                                       MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
102 103 104 105 106 107 108
                                       buf, 4 );
    if( ret != 0 )
        return( ret );

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
109
int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
110 111 112 113 114 115 116 117
                                    unsigned char ns_cert_type )
{
    unsigned char buf[4];
    unsigned char *c;
    int ret;

    c = buf + 4;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
118
    if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
119 120
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
121 122
    ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
                                       MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
123 124 125 126 127 128 129
                                       buf, 4 );
    if( ret != 0 )
        return( ret );

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
130
int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
131 132 133 134 135 136 137 138
                       int (*f_rng)(void *, unsigned char *, size_t),
                       void *p_rng )
{
    int ret;
    const char *sig_oid;
    size_t sig_oid_len = 0;
    unsigned char *c, *c2;
    unsigned char hash[64];
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
139
    unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
140 141 142
    unsigned char tmp_buf[2048];
    size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
    size_t len = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
143
    mbedtls_pk_type_t pk_alg;
144 145 146 147 148 149

    /*
     * Prepare data to be signed in tmp_buf
     */
    c = tmp_buf + sizeof( tmp_buf );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
150
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
151 152 153

    if( len )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
154 155 156
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                        MBEDTLS_ASN1_SEQUENCE ) );
157

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
158 159 160
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                        MBEDTLS_ASN1_SET ) );
161

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
162 163
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
                                          MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
164

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
165 166 167
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                        MBEDTLS_ASN1_SEQUENCE ) );
168 169
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
170 171 172
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                    MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
173

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
174
    MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
175 176 177 178 179 180 181
                                                tmp_buf, c - tmp_buf ) );
    c -= pub_len;
    len += pub_len;

    /*
     *  Subject  ::=  Name
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
183 184 185 186

    /*
     *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
188

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
189 190 191
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                    MBEDTLS_ASN1_SEQUENCE ) );
192 193 194 195

    /*
     * Prepare signature
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
196
    mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
197

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
198 199 200
    pk_alg = mbedtls_pk_get_type( ctx->key );
    if( pk_alg == MBEDTLS_PK_ECKEY )
        pk_alg = MBEDTLS_PK_ECDSA;
201

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
202
    if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
203
                         f_rng, p_rng ) ) != 0 ||
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
204
        ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
205 206 207 208 209 210 211 212 213
                                        &sig_oid, &sig_oid_len ) ) != 0 )
    {
        return( ret );
    }

    /*
     * Write data to output buffer
     */
    c2 = buf + size;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
214
    MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
215 216 217 218 219 220
                                        sig_oid, sig_oid_len, sig, sig_len ) );

    c2 -= len;
    memcpy( c2, c, len );

    len += sig_and_oid_len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
221 222 223
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                 MBEDTLS_ASN1_SEQUENCE ) );
224

225
    return( (int) len );
226 227 228 229 230
}

#define PEM_BEGIN_CSR           "-----BEGIN CERTIFICATE REQUEST-----\n"
#define PEM_END_CSR             "-----END CERTIFICATE REQUEST-----\n"

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
231 232
#if defined(MBEDTLS_PEM_WRITE_C)
int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
233 234 235 236 237 238 239
                       int (*f_rng)(void *, unsigned char *, size_t),
                       void *p_rng )
{
    int ret;
    unsigned char output_buf[4096];
    size_t olen = 0;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
240
    if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
241 242 243 244 245
                                   f_rng, p_rng ) ) < 0 )
    {
        return( ret );
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
246
    if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
247 248 249 250 251 252 253 254
                                  output_buf + sizeof(output_buf) - ret,
                                  ret, buf, size, &olen ) ) != 0 )
    {
        return( ret );
    }

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
255
#endif /* MBEDTLS_PEM_WRITE_C */
256

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
257
#endif /* MBEDTLS_X509_CSR_WRITE_C */