x509_csr.c 12 KB
Newer Older
1 2 3
/*
 *  X.509 Certificate Signing Request (CSR) parsing
 *
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
 *
 *  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.
 */
/*
 *  The ITU-T X.509 standard defines a certificate format for PKI.
 *
25 26 27
 *  http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
 *  http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
 *  http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
28 29 30 31 32
 *
 *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
 *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
 */

33
#if !defined(MBEDTLS_CONFIG_FILE)
34
#include "mbedtls/config.h"
35
#else
36
#include MBEDTLS_CONFIG_FILE
37
#endif
38

39
#if defined(MBEDTLS_X509_CSR_PARSE_C)
40

41 42
#include "mbedtls/x509_csr.h"
#include "mbedtls/oid.h"
43 44 45

#include <string.h>

46
#if defined(MBEDTLS_PEM_PARSE_C)
47
#include "mbedtls/pem.h"
48 49
#endif

50
#if defined(MBEDTLS_PLATFORM_C)
51
#include "mbedtls/platform.h"
52
#else
53
#include <stdlib.h>
54
#include <stdio.h>
55 56 57
#define mbedtls_free       free
#define mbedtls_malloc     malloc
#define mbedtls_snprintf   snprintf
58 59
#endif

60
#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
61 62 63
#include <stdio.h>
#endif

64
/* Implementation that should never be optimized out by the compiler */
65
static void mbedtls_zeroize( void *v, size_t n ) {
66 67 68
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

69 70 71 72 73 74 75 76 77
/*
 *  Version  ::=  INTEGER  {  v1(0)  }
 */
static int x509_csr_get_version( unsigned char **p,
                             const unsigned char *end,
                             int *ver )
{
    int ret;

78
    if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
79
    {
80
        if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
81 82 83 84 85
        {
            *ver = 0;
            return( 0 );
        }

86
        return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
87 88 89 90 91 92
    }

    return( 0 );
}

/*
93
 * Parse a CSR in DER format
94
 */
95
int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
96
                        const unsigned char *buf, size_t buflen )
97 98 99 100
{
    int ret;
    size_t len;
    unsigned char *p, *end;
101
    mbedtls_x509_buf sig_params;
102

103
    memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
104

105 106 107 108
    /*
     * Check for valid input
     */
    if( csr == NULL || buf == NULL )
109
        return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
110

111
    mbedtls_x509_csr_init( csr );
112

113 114 115
    /*
     * first copy the raw DER data
     */
116
    p = mbedtls_malloc( len = buflen );
117

118
    if( p == NULL )
119
        return( MBEDTLS_ERR_X509_MALLOC_FAILED );
120

121
    memcpy( p, buf, buflen );
122 123 124 125 126 127 128 129 130 131 132 133

    csr->raw.p = p;
    csr->raw.len = len;
    end = p + len;

    /*
     *  CertificationRequest ::= SEQUENCE {
     *       certificationRequestInfo CertificationRequestInfo,
     *       signatureAlgorithm AlgorithmIdentifier,
     *       signature          BIT STRING
     *  }
     */
134 135
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
136
    {
137 138
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT );
139 140 141 142
    }

    if( len != (size_t) ( end - p ) )
    {
143 144 145
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
146 147 148 149 150 151 152
    }

    /*
     *  CertificationRequestInfo ::= SEQUENCE {
     */
    csr->cri.p = p;

153 154
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
155
    {
156 157
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
158 159 160 161 162 163 164 165 166 167
    }

    end = p + len;
    csr->cri.len = end - csr->cri.p;

    /*
     *  Version  ::=  INTEGER {  v1(0) }
     */
    if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
    {
168
        mbedtls_x509_csr_free( csr );
169 170 171 172 173 174 175
        return( ret );
    }

    csr->version++;

    if( csr->version != 1 )
    {
176 177
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
178 179 180 181 182 183 184
    }

    /*
     *  subject               Name
     */
    csr->subject_raw.p = p;

185 186
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
187
    {
188 189
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
190 191
    }

192
    if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
193
    {
194
        mbedtls_x509_csr_free( csr );
195 196 197 198 199 200 201 202
        return( ret );
    }

    csr->subject_raw.len = p - csr->subject_raw.p;

    /*
     *  subjectPKInfo SubjectPublicKeyInfo
     */
203
    if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
204
    {
205
        mbedtls_x509_csr_free( csr );
206 207 208 209 210 211
        return( ret );
    }

    /*
     *  attributes    [0] Attributes
     */
212 213
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
214
    {
215 216
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
217 218 219 220 221 222 223 224 225 226 227
    }
    // TODO Parse Attributes / extension requests

    p += len;

    end = csr->raw.p + csr->raw.len;

    /*
     *  signatureAlgorithm   AlgorithmIdentifier,
     *  signature            BIT STRING
     */
228
    if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
229
    {
230
        mbedtls_x509_csr_free( csr );
231 232 233
        return( ret );
    }

234
    if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
235 236
                                  &csr->sig_md, &csr->sig_pk,
                                  &csr->sig_opts ) ) != 0 )
237
    {
238 239
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
240 241
    }

242
    if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
243
    {
244
        mbedtls_x509_csr_free( csr );
245 246 247 248 249
        return( ret );
    }

    if( p != end )
    {
250 251 252
        mbedtls_x509_csr_free( csr );
        return( MBEDTLS_ERR_X509_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
253 254 255 256 257
    }

    return( 0 );
}

258 259 260
/*
 * Parse a CSR, allowing for PEM or raw DER encoding
 */
261
int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
262 263
{
    int ret;
264
#if defined(MBEDTLS_PEM_PARSE_C)
265
    size_t use_len;
266
    mbedtls_pem_context pem;
267 268 269 270 271 272
#endif

    /*
     * Check for valid input
     */
    if( csr == NULL || buf == NULL )
273
        return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
274

275 276
#if defined(MBEDTLS_PEM_PARSE_C)
    mbedtls_pem_init( &pem );
277 278

    /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
279
    if( buflen == 0 || buf[buflen - 1] != '\0' )
280 281 282 283 284 285
        ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
    else
        ret = mbedtls_pem_read_buffer( &pem,
                               "-----BEGIN CERTIFICATE REQUEST-----",
                               "-----END CERTIFICATE REQUEST-----",
                               buf, NULL, 0, &use_len );
286 287 288 289 290 291

    if( ret == 0 )
    {
        /*
         * Was PEM encoded, parse the result
         */
292
        if( ( ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
293 294
            return( ret );

295
        mbedtls_pem_free( &pem );
296 297
        return( 0 );
    }
298
    else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
299
    {
300
        mbedtls_pem_free( &pem );
301 302 303
        return( ret );
    }
    else
304 305
#endif /* MBEDTLS_PEM_PARSE_C */
    return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
306 307
}

308
#if defined(MBEDTLS_FS_IO)
309 310 311
/*
 * Load a CSR into the structure
 */
312
int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
313 314 315 316 317
{
    int ret;
    size_t n;
    unsigned char *buf;

318
    if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
319 320
        return( ret );

321
    ret = mbedtls_x509_csr_parse( csr, buf, n );
322

323
    mbedtls_zeroize( buf, n );
324
    mbedtls_free( buf );
325 326 327

    return( ret );
}
328
#endif /* MBEDTLS_FS_IO */
329

330 331
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
    !defined(EFI32)
332 333 334 335 336 337 338 339 340 341 342 343 344
#include <stdarg.h>

#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif // vsnprintf

/*
 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
 * Result value is not size of buffer needed, but -1 if no fit is possible.
 *
 * This fuction tries to 'fix' this by at least suggesting enlarging the
 * size by 20.
 */
345
static int compat_snprintf( char *str, size_t size, const char *format, ... )
346 347 348 349 350 351 352 353 354 355 356
{
    va_list ap;
    int res = -1;

    va_start( ap, format );

    res = vsnprintf( str, size, format, ap );

    va_end( ap );

    // No quick fix possible
357
    if( res < 0 )
358 359
        return( (int) size + 20 );

360
    return( res );
361 362 363
}

#define snprintf compat_snprintf
364
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
365

366
#define ERR_BUF_TOO_SMALL    -2
367

368 369 370 371 372
#define SAFE_SNPRINTF()                             \
{                                                   \
    if( ret == -1 )                                 \
        return( -1 );                               \
                                                    \
373
    if( (unsigned int) ret > n ) {                  \
374
        p[n - 1] = '\0';                            \
375
        return( ERR_BUF_TOO_SMALL ); \
376 377 378 379
    }                                               \
                                                    \
    n -= (unsigned int) ret;                        \
    p += (unsigned int) ret;                        \
380 381 382 383 384 385 386
}

#define BEFORE_COLON    14
#define BC              "14"
/*
 * Return an informational string about the CSR.
 */
387 388
int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
                   const mbedtls_x509_csr *csr )
389 390 391 392 393 394 395 396 397
{
    int ret;
    size_t n;
    char *p;
    char key_size_str[BEFORE_COLON];

    p = buf;
    n = size;

398
    ret = mbedtls_snprintf( p, n, "%sCSR version   : %d",
399 400 401
                               prefix, csr->version );
    SAFE_SNPRINTF();

402
    ret = mbedtls_snprintf( p, n, "\n%ssubject name  : ", prefix );
403
    SAFE_SNPRINTF();
404
    ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
405 406
    SAFE_SNPRINTF();

407
    ret = mbedtls_snprintf( p, n, "\n%ssigned using  : ", prefix );
408 409
    SAFE_SNPRINTF();

410
    ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
411
                             csr->sig_opts );
412 413
    SAFE_SNPRINTF();

414 415
    if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
                                      mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
416 417 418 419
    {
        return( ret );
    }

420 421
    ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
                          (int) mbedtls_pk_get_size( &csr->pk ) );
422 423 424 425 426
    SAFE_SNPRINTF();

    return( (int) ( size - n ) );
}

427 428 429
/*
 * Initialize a CSR
 */
430
void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
431
{
432
    memset( csr, 0, sizeof(mbedtls_x509_csr) );
433 434
}

435 436 437
/*
 * Unallocate all CSR data
 */
438
void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
439
{
440 441
    mbedtls_x509_name *name_cur;
    mbedtls_x509_name *name_prv;
442 443 444 445

    if( csr == NULL )
        return;

446
    mbedtls_pk_free( &csr->pk );
447

448 449
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
    mbedtls_free( csr->sig_opts );
450 451
#endif

452 453 454 455 456
    name_cur = csr->subject.next;
    while( name_cur != NULL )
    {
        name_prv = name_cur;
        name_cur = name_cur->next;
457 458
        mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
        mbedtls_free( name_prv );
459 460 461 462
    }

    if( csr->raw.p != NULL )
    {
463 464
        mbedtls_zeroize( csr->raw.p, csr->raw.len );
        mbedtls_free( csr->raw.p );
465 466
    }

467
    mbedtls_zeroize( csr, sizeof( mbedtls_x509_csr ) );
468 469
}

470
#endif /* MBEDTLS_X509_CSR_PARSE_C */