mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * X.509 base functions for creating certificates / CSRs
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22
ansond 0:137634ff4186 23 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 24 #include "polarssl/config.h"
ansond 0:137634ff4186 25 #else
ansond 0:137634ff4186 26 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 27 #endif
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if defined(POLARSSL_X509_CREATE_C)
ansond 0:137634ff4186 30
ansond 0:137634ff4186 31 #include "polarssl/x509.h"
ansond 0:137634ff4186 32 #include "polarssl/asn1write.h"
ansond 0:137634ff4186 33 #include "polarssl/oid.h"
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #include <string.h>
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #if defined(_MSC_VER) && !defined strncasecmp && !defined(EFIX64) && \
ansond 0:137634ff4186 38 !defined(EFI32)
ansond 0:137634ff4186 39 #define strncasecmp _strnicmp
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 typedef struct {
ansond 0:137634ff4186 43 const char *name;
ansond 0:137634ff4186 44 size_t name_len;
ansond 0:137634ff4186 45 const char*oid;
ansond 0:137634ff4186 46 } x509_attr_descriptor_t;
ansond 0:137634ff4186 47
ansond 0:137634ff4186 48 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
ansond 0:137634ff4186 49
ansond 0:137634ff4186 50 static const x509_attr_descriptor_t x509_attrs[] =
ansond 0:137634ff4186 51 {
ansond 0:137634ff4186 52 { ADD_STRLEN( "CN" ), OID_AT_CN },
ansond 0:137634ff4186 53 { ADD_STRLEN( "commonName" ), OID_AT_CN },
ansond 0:137634ff4186 54 { ADD_STRLEN( "C" ), OID_AT_COUNTRY },
ansond 0:137634ff4186 55 { ADD_STRLEN( "countryName" ), OID_AT_COUNTRY },
ansond 0:137634ff4186 56 { ADD_STRLEN( "O" ), OID_AT_ORGANIZATION },
ansond 0:137634ff4186 57 { ADD_STRLEN( "organizationName" ), OID_AT_ORGANIZATION },
ansond 0:137634ff4186 58 { ADD_STRLEN( "L" ), OID_AT_LOCALITY },
ansond 0:137634ff4186 59 { ADD_STRLEN( "locality" ), OID_AT_LOCALITY },
ansond 0:137634ff4186 60 { ADD_STRLEN( "R" ), OID_PKCS9_EMAIL },
ansond 0:137634ff4186 61 { ADD_STRLEN( "OU" ), OID_AT_ORG_UNIT },
ansond 0:137634ff4186 62 { ADD_STRLEN( "organizationalUnitName" ), OID_AT_ORG_UNIT },
ansond 0:137634ff4186 63 { ADD_STRLEN( "ST" ), OID_AT_STATE },
ansond 0:137634ff4186 64 { ADD_STRLEN( "stateOrProvinceName" ), OID_AT_STATE },
ansond 0:137634ff4186 65 { ADD_STRLEN( "emailAddress" ), OID_PKCS9_EMAIL },
ansond 0:137634ff4186 66 { ADD_STRLEN( "serialNumber" ), OID_AT_SERIAL_NUMBER },
ansond 0:137634ff4186 67 { ADD_STRLEN( "postalAddress" ), OID_AT_POSTAL_ADDRESS },
ansond 0:137634ff4186 68 { ADD_STRLEN( "postalCode" ), OID_AT_POSTAL_CODE },
ansond 0:137634ff4186 69 { ADD_STRLEN( "dnQualifier" ), OID_AT_DN_QUALIFIER },
ansond 0:137634ff4186 70 { ADD_STRLEN( "title" ), OID_AT_TITLE },
ansond 0:137634ff4186 71 { ADD_STRLEN( "surName" ), OID_AT_SUR_NAME },
ansond 0:137634ff4186 72 { ADD_STRLEN( "SN" ), OID_AT_SUR_NAME },
ansond 0:137634ff4186 73 { ADD_STRLEN( "givenName" ), OID_AT_GIVEN_NAME },
ansond 0:137634ff4186 74 { ADD_STRLEN( "GN" ), OID_AT_GIVEN_NAME },
ansond 0:137634ff4186 75 { ADD_STRLEN( "initials" ), OID_AT_INITIALS },
ansond 0:137634ff4186 76 { ADD_STRLEN( "pseudonym" ), OID_AT_PSEUDONYM },
ansond 0:137634ff4186 77 { ADD_STRLEN( "generationQualifier" ), OID_AT_GENERATION_QUALIFIER },
ansond 0:137634ff4186 78 { ADD_STRLEN( "domainComponent" ), OID_DOMAIN_COMPONENT },
ansond 0:137634ff4186 79 { ADD_STRLEN( "DC" ), OID_DOMAIN_COMPONENT },
ansond 0:137634ff4186 80 { NULL, 0, NULL }
ansond 0:137634ff4186 81 };
ansond 0:137634ff4186 82
ansond 0:137634ff4186 83 static const char *x509_at_oid_from_name( const char *name, size_t name_len )
ansond 0:137634ff4186 84 {
ansond 0:137634ff4186 85 const x509_attr_descriptor_t *cur;
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 for( cur = x509_attrs; cur->name != NULL; cur++ )
ansond 0:137634ff4186 88 if( cur->name_len == name_len &&
ansond 0:137634ff4186 89 strncasecmp( cur->name, name, name_len ) == 0 )
ansond 0:137634ff4186 90 break;
ansond 0:137634ff4186 91
ansond 0:137634ff4186 92 return( cur->oid );
ansond 0:137634ff4186 93 }
ansond 0:137634ff4186 94
ansond 0:137634ff4186 95 int x509_string_to_names( asn1_named_data **head, const char *name )
ansond 0:137634ff4186 96 {
ansond 0:137634ff4186 97 int ret = 0;
ansond 0:137634ff4186 98 const char *s = name, *c = s;
ansond 0:137634ff4186 99 const char *end = s + strlen( s );
ansond 0:137634ff4186 100 const char *oid = NULL;
ansond 0:137634ff4186 101 int in_tag = 1;
ansond 0:137634ff4186 102 char data[X509_MAX_DN_NAME_SIZE];
ansond 0:137634ff4186 103 char *d = data;
ansond 0:137634ff4186 104
ansond 0:137634ff4186 105 /* Clear existing chain if present */
ansond 0:137634ff4186 106 asn1_free_named_data_list( head );
ansond 0:137634ff4186 107
ansond 0:137634ff4186 108 while( c <= end )
ansond 0:137634ff4186 109 {
ansond 0:137634ff4186 110 if( in_tag && *c == '=' )
ansond 0:137634ff4186 111 {
ansond 0:137634ff4186 112 if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
ansond 0:137634ff4186 113 {
ansond 0:137634ff4186 114 ret = POLARSSL_ERR_X509_UNKNOWN_OID;
ansond 0:137634ff4186 115 goto exit;
ansond 0:137634ff4186 116 }
ansond 0:137634ff4186 117
ansond 0:137634ff4186 118 s = c + 1;
ansond 0:137634ff4186 119 in_tag = 0;
ansond 0:137634ff4186 120 d = data;
ansond 0:137634ff4186 121 }
ansond 0:137634ff4186 122
ansond 0:137634ff4186 123 if( !in_tag && *c == '\\' && c != end )
ansond 0:137634ff4186 124 {
ansond 0:137634ff4186 125 c++;
ansond 0:137634ff4186 126
ansond 0:137634ff4186 127 /* Check for valid escaped characters */
ansond 0:137634ff4186 128 if( c == end || *c != ',' )
ansond 0:137634ff4186 129 {
ansond 0:137634ff4186 130 ret = POLARSSL_ERR_X509_INVALID_NAME;
ansond 0:137634ff4186 131 goto exit;
ansond 0:137634ff4186 132 }
ansond 0:137634ff4186 133 }
ansond 0:137634ff4186 134 else if( !in_tag && ( *c == ',' || c == end ) )
ansond 0:137634ff4186 135 {
ansond 0:137634ff4186 136 if( asn1_store_named_data( head, oid, strlen( oid ),
ansond 0:137634ff4186 137 (unsigned char *) data,
ansond 0:137634ff4186 138 d - data ) == NULL )
ansond 0:137634ff4186 139 {
ansond 0:137634ff4186 140 return( POLARSSL_ERR_X509_MALLOC_FAILED );
ansond 0:137634ff4186 141 }
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 while( c < end && *(c + 1) == ' ' )
ansond 0:137634ff4186 144 c++;
ansond 0:137634ff4186 145
ansond 0:137634ff4186 146 s = c + 1;
ansond 0:137634ff4186 147 in_tag = 1;
ansond 0:137634ff4186 148 }
ansond 0:137634ff4186 149
ansond 0:137634ff4186 150 if( !in_tag && s != c + 1 )
ansond 0:137634ff4186 151 {
ansond 0:137634ff4186 152 *(d++) = *c;
ansond 0:137634ff4186 153
ansond 0:137634ff4186 154 if( d - data == X509_MAX_DN_NAME_SIZE )
ansond 0:137634ff4186 155 {
ansond 0:137634ff4186 156 ret = POLARSSL_ERR_X509_INVALID_NAME;
ansond 0:137634ff4186 157 goto exit;
ansond 0:137634ff4186 158 }
ansond 0:137634ff4186 159 }
ansond 0:137634ff4186 160
ansond 0:137634ff4186 161 c++;
ansond 0:137634ff4186 162 }
ansond 0:137634ff4186 163
ansond 0:137634ff4186 164 exit:
ansond 0:137634ff4186 165
ansond 0:137634ff4186 166 return( ret );
ansond 0:137634ff4186 167 }
ansond 0:137634ff4186 168
ansond 0:137634ff4186 169 /* The first byte of the value in the asn1_named_data structure is reserved
ansond 0:137634ff4186 170 * to store the critical boolean for us
ansond 0:137634ff4186 171 */
ansond 0:137634ff4186 172 int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len,
ansond 0:137634ff4186 173 int critical, const unsigned char *val, size_t val_len )
ansond 0:137634ff4186 174 {
ansond 0:137634ff4186 175 asn1_named_data *cur;
ansond 0:137634ff4186 176
ansond 0:137634ff4186 177 if( ( cur = asn1_store_named_data( head, oid, oid_len,
ansond 0:137634ff4186 178 NULL, val_len + 1 ) ) == NULL )
ansond 0:137634ff4186 179 {
ansond 0:137634ff4186 180 return( POLARSSL_ERR_X509_MALLOC_FAILED );
ansond 0:137634ff4186 181 }
ansond 0:137634ff4186 182
ansond 0:137634ff4186 183 cur->val.p[0] = critical;
ansond 0:137634ff4186 184 memcpy( cur->val.p + 1, val, val_len );
ansond 0:137634ff4186 185
ansond 0:137634ff4186 186 return( 0 );
ansond 0:137634ff4186 187 }
ansond 0:137634ff4186 188
ansond 0:137634ff4186 189 /*
ansond 0:137634ff4186 190 * RelativeDistinguishedName ::=
ansond 0:137634ff4186 191 * SET OF AttributeTypeAndValue
ansond 0:137634ff4186 192 *
ansond 0:137634ff4186 193 * AttributeTypeAndValue ::= SEQUENCE {
ansond 0:137634ff4186 194 * type AttributeType,
ansond 0:137634ff4186 195 * value AttributeValue }
ansond 0:137634ff4186 196 *
ansond 0:137634ff4186 197 * AttributeType ::= OBJECT IDENTIFIER
ansond 0:137634ff4186 198 *
ansond 0:137634ff4186 199 * AttributeValue ::= ANY DEFINED BY AttributeType
ansond 0:137634ff4186 200 */
ansond 0:137634ff4186 201 static int x509_write_name( unsigned char **p, unsigned char *start,
ansond 0:137634ff4186 202 const char *oid, size_t oid_len,
ansond 0:137634ff4186 203 const unsigned char *name, size_t name_len )
ansond 0:137634ff4186 204 {
ansond 0:137634ff4186 205 int ret;
ansond 0:137634ff4186 206 size_t len = 0;
ansond 0:137634ff4186 207
ansond 0:137634ff4186 208 // Write PrintableString for all except OID_PKCS9_EMAIL
ansond 0:137634ff4186 209 //
ansond 0:137634ff4186 210 if( OID_SIZE( OID_PKCS9_EMAIL ) == oid_len &&
ansond 0:137634ff4186 211 memcmp( oid, OID_PKCS9_EMAIL, oid_len ) == 0 )
ansond 0:137634ff4186 212 {
ansond 0:137634ff4186 213 ASN1_CHK_ADD( len, asn1_write_ia5_string( p, start,
ansond 0:137634ff4186 214 (const char *) name,
ansond 0:137634ff4186 215 name_len ) );
ansond 0:137634ff4186 216 }
ansond 0:137634ff4186 217 else
ansond 0:137634ff4186 218 {
ansond 0:137634ff4186 219 ASN1_CHK_ADD( len, asn1_write_printable_string( p, start,
ansond 0:137634ff4186 220 (const char *) name,
ansond 0:137634ff4186 221 name_len ) );
ansond 0:137634ff4186 222 }
ansond 0:137634ff4186 223
ansond 0:137634ff4186 224 // Write OID
ansond 0:137634ff4186 225 //
ansond 0:137634ff4186 226 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ansond 0:137634ff4186 229 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 230 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ansond 0:137634ff4186 233 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 234 ASN1_SET ) );
ansond 0:137634ff4186 235
ansond 0:137634ff4186 236 return( (int) len );
ansond 0:137634ff4186 237 }
ansond 0:137634ff4186 238
ansond 0:137634ff4186 239 int x509_write_names( unsigned char **p, unsigned char *start,
ansond 0:137634ff4186 240 asn1_named_data *first )
ansond 0:137634ff4186 241 {
ansond 0:137634ff4186 242 int ret;
ansond 0:137634ff4186 243 size_t len = 0;
ansond 0:137634ff4186 244 asn1_named_data *cur = first;
ansond 0:137634ff4186 245
ansond 0:137634ff4186 246 while( cur != NULL )
ansond 0:137634ff4186 247 {
ansond 0:137634ff4186 248 ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
ansond 0:137634ff4186 249 cur->oid.len,
ansond 0:137634ff4186 250 cur->val.p, cur->val.len ) );
ansond 0:137634ff4186 251 cur = cur->next;
ansond 0:137634ff4186 252 }
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ansond 0:137634ff4186 255 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 256 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 257
ansond 0:137634ff4186 258 return( (int) len );
ansond 0:137634ff4186 259 }
ansond 0:137634ff4186 260
ansond 0:137634ff4186 261 int x509_write_sig( unsigned char **p, unsigned char *start,
ansond 0:137634ff4186 262 const char *oid, size_t oid_len,
ansond 0:137634ff4186 263 unsigned char *sig, size_t size )
ansond 0:137634ff4186 264 {
ansond 0:137634ff4186 265 int ret;
ansond 0:137634ff4186 266 size_t len = 0;
ansond 0:137634ff4186 267
ansond 0:137634ff4186 268 if( *p - start < (int) size + 1 )
ansond 0:137634ff4186 269 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
ansond 0:137634ff4186 270
ansond 0:137634ff4186 271 len = size;
ansond 0:137634ff4186 272 (*p) -= len;
ansond 0:137634ff4186 273 memcpy( *p, sig, len );
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 *--(*p) = 0;
ansond 0:137634ff4186 276 len += 1;
ansond 0:137634ff4186 277
ansond 0:137634ff4186 278 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ansond 0:137634ff4186 279 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
ansond 0:137634ff4186 280
ansond 0:137634ff4186 281 // Write OID
ansond 0:137634ff4186 282 //
ansond 0:137634ff4186 283 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid,
ansond 0:137634ff4186 284 oid_len, 0 ) );
ansond 0:137634ff4186 285
ansond 0:137634ff4186 286 return( (int) len );
ansond 0:137634ff4186 287 }
ansond 0:137634ff4186 288
ansond 0:137634ff4186 289 static int x509_write_extension( unsigned char **p, unsigned char *start,
ansond 0:137634ff4186 290 asn1_named_data *ext )
ansond 0:137634ff4186 291 {
ansond 0:137634ff4186 292 int ret;
ansond 0:137634ff4186 293 size_t len = 0;
ansond 0:137634ff4186 294
ansond 0:137634ff4186 295 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->val.p + 1,
ansond 0:137634ff4186 296 ext->val.len - 1 ) );
ansond 0:137634ff4186 297 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->val.len - 1 ) );
ansond 0:137634ff4186 298 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
ansond 0:137634ff4186 299
ansond 0:137634ff4186 300 if( ext->val.p[0] != 0 )
ansond 0:137634ff4186 301 {
ansond 0:137634ff4186 302 ASN1_CHK_ADD( len, asn1_write_bool( p, start, 1 ) );
ansond 0:137634ff4186 303 }
ansond 0:137634ff4186 304
ansond 0:137634ff4186 305 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, ext->oid.p,
ansond 0:137634ff4186 306 ext->oid.len ) );
ansond 0:137634ff4186 307 ASN1_CHK_ADD( len, asn1_write_len( p, start, ext->oid.len ) );
ansond 0:137634ff4186 308 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OID ) );
ansond 0:137634ff4186 309
ansond 0:137634ff4186 310 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
ansond 0:137634ff4186 311 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 312 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 313
ansond 0:137634ff4186 314 return( (int) len );
ansond 0:137634ff4186 315 }
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 /*
ansond 0:137634ff4186 318 * Extension ::= SEQUENCE {
ansond 0:137634ff4186 319 * extnID OBJECT IDENTIFIER,
ansond 0:137634ff4186 320 * critical BOOLEAN DEFAULT FALSE,
ansond 0:137634ff4186 321 * extnValue OCTET STRING
ansond 0:137634ff4186 322 * -- contains the DER encoding of an ASN.1 value
ansond 0:137634ff4186 323 * -- corresponding to the extension type identified
ansond 0:137634ff4186 324 * -- by extnID
ansond 0:137634ff4186 325 * }
ansond 0:137634ff4186 326 */
ansond 0:137634ff4186 327 int x509_write_extensions( unsigned char **p, unsigned char *start,
ansond 0:137634ff4186 328 asn1_named_data *first )
ansond 0:137634ff4186 329 {
ansond 0:137634ff4186 330 int ret;
ansond 0:137634ff4186 331 size_t len = 0;
ansond 0:137634ff4186 332 asn1_named_data *cur_ext = first;
ansond 0:137634ff4186 333
ansond 0:137634ff4186 334 while( cur_ext != NULL )
ansond 0:137634ff4186 335 {
ansond 0:137634ff4186 336 ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
ansond 0:137634ff4186 337 cur_ext = cur_ext->next;
ansond 0:137634ff4186 338 }
ansond 0:137634ff4186 339
ansond 0:137634ff4186 340 return( (int) len );
ansond 0:137634ff4186 341 }
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 #endif /* POLARSSL_X509_CREATE_C */
ansond 0:137634ff4186 344