asn1parse.c 9.33 KB
Newer Older
1 2 3
/*
 *  Generic ASN.1 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
 *
 *  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.
 */

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

29
#if defined(MBEDTLS_ASN1_PARSE_C)
30

31
#include "mbedtls/asn1.h"
32

33 34
#include <string.h>

35
#if defined(MBEDTLS_BIGNUM_C)
36
#include "mbedtls/bignum.h"
37 38
#endif

39
#if defined(MBEDTLS_PLATFORM_C)
40
#include "mbedtls/platform.h"
41
#else
42
#include <stdlib.h>
43 44
#define mbedtls_malloc     malloc
#define mbedtls_free       free
45 46
#endif

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

52 53 54
/*
 * ASN.1 DER decoding routines
 */
55
int mbedtls_asn1_get_len( unsigned char **p,
56 57 58 59
                  const unsigned char *end,
                  size_t *len )
{
    if( ( end - *p ) < 1 )
60
        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
61 62 63 64 65 66 67 68 69

    if( ( **p & 0x80 ) == 0 )
        *len = *(*p)++;
    else
    {
        switch( **p & 0x7F )
        {
        case 1:
            if( ( end - *p ) < 2 )
70
                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
71 72 73 74 75 76 77

            *len = (*p)[1];
            (*p) += 2;
            break;

        case 2:
            if( ( end - *p ) < 3 )
78
                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
79

80
            *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
81 82 83 84 85
            (*p) += 3;
            break;

        case 3:
            if( ( end - *p ) < 4 )
86
                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
87

88 89
            *len = ( (size_t)(*p)[1] << 16 ) |
                   ( (size_t)(*p)[2] << 8  ) | (*p)[3];
90 91 92 93 94
            (*p) += 4;
            break;

        case 4:
            if( ( end - *p ) < 5 )
95
                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
96

97 98
            *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
                   ( (size_t)(*p)[3] << 8  ) |           (*p)[4];
99 100 101 102
            (*p) += 5;
            break;

        default:
103
            return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
104 105 106 107
        }
    }

    if( *len > (size_t) ( end - *p ) )
108
        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
109 110 111 112

    return( 0 );
}

113
int mbedtls_asn1_get_tag( unsigned char **p,
114 115 116 117
                  const unsigned char *end,
                  size_t *len, int tag )
{
    if( ( end - *p ) < 1 )
118
        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
119 120

    if( **p != tag )
121
        return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
122 123 124

    (*p)++;

125
    return( mbedtls_asn1_get_len( p, end, len ) );
126 127
}

128
int mbedtls_asn1_get_bool( unsigned char **p,
129 130 131 132 133 134
                   const unsigned char *end,
                   int *val )
{
    int ret;
    size_t len;

135
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
136 137 138
        return( ret );

    if( len != 1 )
139
        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
140 141 142 143 144 145 146

    *val = ( **p != 0 ) ? 1 : 0;
    (*p)++;

    return( 0 );
}

147
int mbedtls_asn1_get_int( unsigned char **p,
148 149 150 151 152 153
                  const unsigned char *end,
                  int *val )
{
    int ret;
    size_t len;

154
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
155 156 157
        return( ret );

    if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
158
        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
159 160 161 162 163 164 165 166 167 168 169 170

    *val = 0;

    while( len-- > 0 )
    {
        *val = ( *val << 8 ) | **p;
        (*p)++;
    }

    return( 0 );
}

171 172
#if defined(MBEDTLS_BIGNUM_C)
int mbedtls_asn1_get_mpi( unsigned char **p,
173
                  const unsigned char *end,
174
                  mbedtls_mpi *X )
175 176 177 178
{
    int ret;
    size_t len;

179
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
180 181
        return( ret );

182
    ret = mbedtls_mpi_read_binary( X, *p, len );
183 184 185 186 187

    *p += len;

    return( ret );
}
188
#endif /* MBEDTLS_BIGNUM_C */
189

190 191
int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
                        mbedtls_asn1_bitstring *bs)
192 193 194 195
{
    int ret;

    /* Certificate type is a single byte bitstring */
196
    if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
197 198 199
        return( ret );

    /* Check length, subtract one for actual bit string length */
200
    if( bs->len < 1 )
201
        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
202 203 204 205 206
    bs->len -= 1;

    /* Get number of unused bits, ensure unused bits <= 7 */
    bs->unused_bits = **p;
    if( bs->unused_bits > 7 )
207
        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
208 209 210 211 212 213 214
    (*p)++;

    /* Get actual bitstring */
    bs->p = *p;
    *p += bs->len;

    if( *p != end )
215
        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
216

217
    return( 0 );
218 219
}

220 221 222
/*
 * Get a bit string without unused bits
 */
223
int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
224 225 226 227
                             size_t *len )
{
    int ret;

228
    if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
229 230
        return( ret );

231
    if( (*len)-- < 2 || *(*p)++ != 0 )
232
        return( MBEDTLS_ERR_ASN1_INVALID_DATA );
233 234 235 236 237

    return( 0 );
}


238 239 240 241

/*
 *  Parses and splits an ASN.1 "SEQUENCE OF <tag>"
 */
242
int mbedtls_asn1_get_sequence_of( unsigned char **p,
243
                          const unsigned char *end,
244
                          mbedtls_asn1_sequence *cur,
245 246 247 248
                          int tag)
{
    int ret;
    size_t len;
249
    mbedtls_asn1_buf *buf;
250 251

    /* Get main sequence tag */
252 253
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
254 255 256
        return( ret );

    if( *p + len != end )
257
        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
258 259 260 261 262 263

    while( *p < end )
    {
        buf = &(cur->buf);
        buf->tag = **p;

264
        if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
265 266 267 268 269 270
            return( ret );

        buf->p = *p;
        *p += buf->len;

        /* Allocate and assign next pointer */
271
        if( *p < end )
272
        {
273
            cur->next = mbedtls_malloc( sizeof( mbedtls_asn1_sequence ) );
274 275

            if( cur->next == NULL )
276
                return( MBEDTLS_ERR_ASN1_MALLOC_FAILED );
277

278
            memset( cur->next, 0, sizeof( mbedtls_asn1_sequence ) );
279

280 281 282 283 284 285 286 287
            cur = cur->next;
        }
    }

    /* Set final sequence entry's next pointer to NULL */
    cur->next = NULL;

    if( *p != end )
288
        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
289 290 291 292

    return( 0 );
}

293
int mbedtls_asn1_get_alg( unsigned char **p,
294
                  const unsigned char *end,
295
                  mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
296 297 298 299
{
    int ret;
    size_t len;

300 301
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
302 303
        return( ret );

304
    if( ( end - *p ) < 1 )
305
        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
306

307
    alg->tag = **p;
308
    end = *p + len;
309

310
    if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
311 312 313 314 315 316 317
        return( ret );

    alg->p = *p;
    *p += alg->len;

    if( *p == end )
    {
318
        mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) );
319 320 321 322 323 324
        return( 0 );
    }

    params->tag = **p;
    (*p)++;

325
    if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
326 327 328 329 330 331
        return( ret );

    params->p = *p;
    *p += params->len;

    if( *p != end )
332
        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
333 334 335 336

    return( 0 );
}

337
int mbedtls_asn1_get_alg_null( unsigned char **p,
338
                       const unsigned char *end,
339
                       mbedtls_asn1_buf *alg )
340 341
{
    int ret;
342
    mbedtls_asn1_buf params;
343

344
    memset( &params, 0, sizeof(mbedtls_asn1_buf) );
345

346
    if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
347 348
        return( ret );

349 350
    if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
        return( MBEDTLS_ERR_ASN1_INVALID_DATA );
351 352 353 354

    return( 0 );
}

355
void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
356 357 358 359
{
    if( cur == NULL )
        return;

360 361
    mbedtls_free( cur->oid.p );
    mbedtls_free( cur->val.p );
362

363
    mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
364 365
}

366
void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
367
{
368
    mbedtls_asn1_named_data *cur;
369 370 371 372

    while( ( cur = *head ) != NULL )
    {
        *head = cur->next;
373 374
        mbedtls_asn1_free_named_data( cur );
        mbedtls_free( cur );
375 376 377
    }
}

378
mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                                       const char *oid, size_t len )
{
    while( list != NULL )
    {
        if( list->oid.len == len &&
            memcmp( list->oid.p, oid, len ) == 0 )
        {
            break;
        }

        list = list->next;
    }

    return( list );
}

395
#endif /* MBEDTLS_ASN1_PARSE_C */