x509_create.c 12.5 KB
Newer Older
1 2 3
/*
 *  X.509 base functions for creating certificates / CSRs
 *
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
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
22
#if !defined(MBEDTLS_CONFIG_FILE)
23
#include "mbedtls/config.h"
24
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
25
#include MBEDTLS_CONFIG_FILE
26
#endif
27

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
28
#if defined(MBEDTLS_X509_CREATE_C)
29

30 31 32
#include "mbedtls/x509.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/oid.h"
33

34 35
#include <string.h>

36 37
/* Structure linking OIDs for X.509 DN AttributeTypes to their
 * string representations and default string encodings used by Mbed TLS. */
38
typedef struct {
39 40
   const char *name; /* String representation of AttributeType, e.g.
                      * "CN" or "emailAddress". */
41
   size_t name_len;  /* Length of 'name', without trailing 0 byte. */
42 43
   const char *oid;  /* String representation of OID of AttributeType,
                      * as per RFC 5280, Appendix A.1. */
44
   int default_tag;  /* The default character encoding used for the
45
                      * given attribute type, e.g.
46
                      * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
47 48 49 50
} x509_attr_descriptor_t;

#define ADD_STRLEN( s )     s, sizeof( s ) - 1

51
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
52 53
static const x509_attr_descriptor_t x509_attrs[] =
{
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    { ADD_STRLEN( "CN" ),
      MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "commonName" ),
      MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "C" ),
      MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "countryName" ),
      MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "O" ),
      MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "organizationName" ),
      MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "L" ),
      MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "locality" ),
      MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "R" ),
      MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
    { ADD_STRLEN( "OU" ),
      MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "organizationalUnitName" ),
      MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "ST" ),
      MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "stateOrProvinceName" ),
      MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "emailAddress" ),
      MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
    { ADD_STRLEN( "serialNumber" ),
      MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "postalAddress" ),
      MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "postalCode" ),
      MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "dnQualifier" ),
      MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
    { ADD_STRLEN( "title" ),
      MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "surName" ),
      MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "SN" ),
      MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "givenName" ),
      MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "GN" ),
      MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "initials" ),
      MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "pseudonym" ),
      MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "generationQualifier" ),
      MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
    { ADD_STRLEN( "domainComponent" ),
      MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
    { ADD_STRLEN( "DC" ),
      MBEDTLS_OID_DOMAIN_COMPONENT,   MBEDTLS_ASN1_IA5_STRING },
tdoe's avatar
tdoe committed
110
    { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
111 112
};

thomas-dee's avatar
thomas-dee committed
113
static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
114 115 116 117 118
{
    const x509_attr_descriptor_t *cur;

    for( cur = x509_attrs; cur->name != NULL; cur++ )
        if( cur->name_len == name_len &&
119
            strncmp( cur->name, name, name_len ) == 0 )
120 121
            break;

tdoe's avatar
tdoe committed
122 123
    if ( cur->name == NULL )
        return( NULL );
124

125
    return( cur );
126 127
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
128
int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
129 130
{
    int ret = 0;
Paul Bakker's avatar
Paul Bakker committed
131 132
    const char *s = name, *c = s;
    const char *end = s + strlen( s );
Paul Bakker's avatar
Paul Bakker committed
133
    const char *oid = NULL;
thomas-dee's avatar
thomas-dee committed
134
    const x509_attr_descriptor_t* attr_descr = NULL;
135
    int in_tag = 1;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
136
    char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
137
    char *d = data;
138 139

    /* Clear existing chain if present */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
140
    mbedtls_asn1_free_named_data_list( head );
141 142 143 144 145

    while( c <= end )
    {
        if( in_tag && *c == '=' )
        {
thomas-dee's avatar
thomas-dee committed
146
            if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
147
            {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
148
                ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
149 150 151
                goto exit;
            }

thomas-dee's avatar
thomas-dee committed
152
            oid = attr_descr->oid;
153 154
            s = c + 1;
            in_tag = 0;
155
            d = data;
156 157
        }

158 159 160 161 162 163 164
        if( !in_tag && *c == '\\' && c != end )
        {
            c++;

            /* Check for valid escaped characters */
            if( c == end || *c != ',' )
            {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
165
                ret = MBEDTLS_ERR_X509_INVALID_NAME;
166 167 168 169
                goto exit;
            }
        }
        else if( !in_tag && ( *c == ',' || c == end ) )
170
        {
171 172 173 174
            mbedtls_asn1_named_data* cur =
                mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
                                               (unsigned char *) data,
                                               d - data );
175 176

            if(cur == NULL )
177
            {
178
                return( MBEDTLS_ERR_X509_ALLOC_FAILED );
179 180
            }

181
            // set tagType
182
            cur->val.tag = attr_descr->default_tag;
183

184 185 186 187 188 189
            while( c < end && *(c + 1) == ' ' )
                c++;

            s = c + 1;
            in_tag = 1;
        }
190 191 192 193 194

        if( !in_tag && s != c + 1 )
        {
            *(d++) = *c;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
195
            if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
196
            {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
197
                ret = MBEDTLS_ERR_X509_INVALID_NAME;
198 199 200 201
                goto exit;
            }
        }

202 203 204 205 206 207 208 209
        c++;
    }

exit:

    return( ret );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
210
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
211 212
 * to store the critical boolean for us
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
213
int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
214 215
                        int critical, const unsigned char *val, size_t val_len )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
216
    mbedtls_asn1_named_data *cur;
217

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
218
    if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
219 220
                                       NULL, val_len + 1 ) ) == NULL )
    {
221
        return( MBEDTLS_ERR_X509_ALLOC_FAILED );
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    }

    cur->val.p[0] = critical;
    memcpy( cur->val.p + 1, val, val_len );

    return( 0 );
}

/*
 *  RelativeDistinguishedName ::=
 *    SET OF AttributeTypeAndValue
 *
 *  AttributeTypeAndValue ::= SEQUENCE {
 *    type     AttributeType,
 *    value    AttributeValue }
 *
 *  AttributeType ::= OBJECT IDENTIFIER
 *
 *  AttributeValue ::= ANY DEFINED BY AttributeType
 */
242
static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
243 244 245
{
    int ret;
    size_t len = 0;
246 247 248 249 250 251
    const char *oid             = (const char*)cur_name->oid.p;
    size_t oid_len              = cur_name->oid.len;
    const unsigned char *name   = cur_name->val.p;
    size_t name_len             = cur_name->val.len;

    // Write correct string tag and value
252 253 254 255
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
                                                       cur_name->val.tag,
                                                       (const char *) name,
                                                       name_len ) );
256 257
    // Write OID
    //
258 259
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
                                                       oid_len ) );
260

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
261
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
262 263 264
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
                                                    MBEDTLS_ASN1_CONSTRUCTED |
                                                    MBEDTLS_ASN1_SEQUENCE ) );
265

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
266
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
267 268
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
                                                 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
269
                                                 MBEDTLS_ASN1_SET ) );
270

271
    return( (int) len );
272 273
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
274
int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
275
                              mbedtls_asn1_named_data *first )
276 277 278
{
    int ret;
    size_t len = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
279
    mbedtls_asn1_named_data *cur = first;
280 281 282

    while( cur != NULL )
    {
283
        MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
284 285 286
        cur = cur->next;
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
287 288 289
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
                                                 MBEDTLS_ASN1_SEQUENCE ) );
290

291
    return( (int) len );
292 293
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
294
int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
295 296 297 298 299 300
                    const char *oid, size_t oid_len,
                    unsigned char *sig, size_t size )
{
    int ret;
    size_t len = 0;

301
    if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
302
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
303 304 305 306 307

    len = size;
    (*p) -= len;
    memcpy( *p, sig, len );

308 309 310
    if( *p - start < 1 )
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );

311 312 313
    *--(*p) = 0;
    len += 1;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
314 315
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
316 317 318

    // Write OID
    //
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
319
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
320 321
                                                        oid_len, 0 ) );

322
    return( (int) len );
323 324 325
}

static int x509_write_extension( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
326
                                 mbedtls_asn1_named_data *ext )
327 328 329 330
{
    int ret;
    size_t len = 0;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
331
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
332
                                              ext->val.len - 1 ) );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
333 334
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
335 336 337

    if( ext->val.p[0] != 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
338
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
339 340
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
341
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
342
                                              ext->oid.len ) );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
343 344
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
345

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
346 347 348
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
                                                 MBEDTLS_ASN1_SEQUENCE ) );
349

350
    return( (int) len );
351 352 353 354 355 356 357 358 359 360 361 362
}

/*
 * Extension  ::=  SEQUENCE  {
 *     extnID      OBJECT IDENTIFIER,
 *     critical    BOOLEAN DEFAULT FALSE,
 *     extnValue   OCTET STRING
 *                 -- contains the DER encoding of an ASN.1 value
 *                 -- corresponding to the extension type identified
 *                 -- by extnID
 *     }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
363 364
int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
                           mbedtls_asn1_named_data *first )
365 366 367
{
    int ret;
    size_t len = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
368
    mbedtls_asn1_named_data *cur_ext = first;
369 370 371

    while( cur_ext != NULL )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
372
        MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
373 374 375
        cur_ext = cur_ext->next;
    }

376
    return( (int) len );
377 378
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
379
#endif /* MBEDTLS_X509_CREATE_C */