Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /*
bryantaylor 0:eafc3fd41f75 2 * X.509 base functions for creating certificates / CSRs
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
bryantaylor 0:eafc3fd41f75 5 * SPDX-License-Identifier: Apache-2.0
bryantaylor 0:eafc3fd41f75 6 *
bryantaylor 0:eafc3fd41f75 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
bryantaylor 0:eafc3fd41f75 8 * not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 9 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 10 *
bryantaylor 0:eafc3fd41f75 11 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 12 *
bryantaylor 0:eafc3fd41f75 13 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
bryantaylor 0:eafc3fd41f75 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 16 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 17 * limitations under the License.
bryantaylor 0:eafc3fd41f75 18 *
bryantaylor 0:eafc3fd41f75 19 * This file is part of mbed TLS (https://tls.mbed.org)
bryantaylor 0:eafc3fd41f75 20 */
bryantaylor 0:eafc3fd41f75 21
bryantaylor 0:eafc3fd41f75 22 #if !defined(MBEDTLS_CONFIG_FILE)
bryantaylor 0:eafc3fd41f75 23 #include "mbedtls/config.h"
bryantaylor 0:eafc3fd41f75 24 #else
bryantaylor 0:eafc3fd41f75 25 #include MBEDTLS_CONFIG_FILE
bryantaylor 0:eafc3fd41f75 26 #endif
bryantaylor 0:eafc3fd41f75 27
bryantaylor 0:eafc3fd41f75 28 #if defined(MBEDTLS_X509_CREATE_C)
bryantaylor 0:eafc3fd41f75 29
bryantaylor 0:eafc3fd41f75 30 #include "mbedtls/x509.h"
bryantaylor 0:eafc3fd41f75 31 #include "mbedtls/asn1write.h"
bryantaylor 0:eafc3fd41f75 32 #include "mbedtls/oid.h"
bryantaylor 0:eafc3fd41f75 33
bryantaylor 0:eafc3fd41f75 34 #include <string.h>
bryantaylor 0:eafc3fd41f75 35
bryantaylor 0:eafc3fd41f75 36 typedef struct {
bryantaylor 0:eafc3fd41f75 37 const char *name;
bryantaylor 0:eafc3fd41f75 38 size_t name_len;
bryantaylor 0:eafc3fd41f75 39 const char*oid;
bryantaylor 0:eafc3fd41f75 40 } x509_attr_descriptor_t;
bryantaylor 0:eafc3fd41f75 41
bryantaylor 0:eafc3fd41f75 42 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
bryantaylor 0:eafc3fd41f75 43
bryantaylor 0:eafc3fd41f75 44 static const x509_attr_descriptor_t x509_attrs[] =
bryantaylor 0:eafc3fd41f75 45 {
bryantaylor 0:eafc3fd41f75 46 { ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN },
bryantaylor 0:eafc3fd41f75 47 { ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN },
bryantaylor 0:eafc3fd41f75 48 { ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY },
bryantaylor 0:eafc3fd41f75 49 { ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY },
bryantaylor 0:eafc3fd41f75 50 { ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION },
bryantaylor 0:eafc3fd41f75 51 { ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION },
bryantaylor 0:eafc3fd41f75 52 { ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY },
bryantaylor 0:eafc3fd41f75 53 { ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY },
bryantaylor 0:eafc3fd41f75 54 { ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL },
bryantaylor 0:eafc3fd41f75 55 { ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT },
bryantaylor 0:eafc3fd41f75 56 { ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT },
bryantaylor 0:eafc3fd41f75 57 { ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE },
bryantaylor 0:eafc3fd41f75 58 { ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE },
bryantaylor 0:eafc3fd41f75 59 { ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL },
bryantaylor 0:eafc3fd41f75 60 { ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER },
bryantaylor 0:eafc3fd41f75 61 { ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS },
bryantaylor 0:eafc3fd41f75 62 { ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE },
bryantaylor 0:eafc3fd41f75 63 { ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER },
bryantaylor 0:eafc3fd41f75 64 { ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE },
bryantaylor 0:eafc3fd41f75 65 { ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME },
bryantaylor 0:eafc3fd41f75 66 { ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME },
bryantaylor 0:eafc3fd41f75 67 { ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME },
bryantaylor 0:eafc3fd41f75 68 { ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME },
bryantaylor 0:eafc3fd41f75 69 { ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS },
bryantaylor 0:eafc3fd41f75 70 { ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM },
bryantaylor 0:eafc3fd41f75 71 { ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER },
bryantaylor 0:eafc3fd41f75 72 { ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT },
bryantaylor 0:eafc3fd41f75 73 { ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT },
bryantaylor 0:eafc3fd41f75 74 { NULL, 0, NULL }
bryantaylor 0:eafc3fd41f75 75 };
bryantaylor 0:eafc3fd41f75 76
bryantaylor 0:eafc3fd41f75 77 static const char *x509_at_oid_from_name( const char *name, size_t name_len )
bryantaylor 0:eafc3fd41f75 78 {
bryantaylor 0:eafc3fd41f75 79 const x509_attr_descriptor_t *cur;
bryantaylor 0:eafc3fd41f75 80
bryantaylor 0:eafc3fd41f75 81 for( cur = x509_attrs; cur->name != NULL; cur++ )
bryantaylor 0:eafc3fd41f75 82 if( cur->name_len == name_len &&
bryantaylor 0:eafc3fd41f75 83 strncmp( cur->name, name, name_len ) == 0 )
bryantaylor 0:eafc3fd41f75 84 break;
bryantaylor 0:eafc3fd41f75 85
bryantaylor 0:eafc3fd41f75 86 return( cur->oid );
bryantaylor 0:eafc3fd41f75 87 }
bryantaylor 0:eafc3fd41f75 88
bryantaylor 0:eafc3fd41f75 89 int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
bryantaylor 0:eafc3fd41f75 90 {
bryantaylor 0:eafc3fd41f75 91 int ret = 0;
bryantaylor 0:eafc3fd41f75 92 const char *s = name, *c = s;
bryantaylor 0:eafc3fd41f75 93 const char *end = s + strlen( s );
bryantaylor 0:eafc3fd41f75 94 const char *oid = NULL;
bryantaylor 0:eafc3fd41f75 95 int in_tag = 1;
bryantaylor 0:eafc3fd41f75 96 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
bryantaylor 0:eafc3fd41f75 97 char *d = data;
bryantaylor 0:eafc3fd41f75 98
bryantaylor 0:eafc3fd41f75 99 /* Clear existing chain if present */
bryantaylor 0:eafc3fd41f75 100 mbedtls_asn1_free_named_data_list( head );
bryantaylor 0:eafc3fd41f75 101
bryantaylor 0:eafc3fd41f75 102 while( c <= end )
bryantaylor 0:eafc3fd41f75 103 {
bryantaylor 0:eafc3fd41f75 104 if( in_tag && *c == '=' )
bryantaylor 0:eafc3fd41f75 105 {
bryantaylor 0:eafc3fd41f75 106 if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
bryantaylor 0:eafc3fd41f75 107 {
bryantaylor 0:eafc3fd41f75 108 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
bryantaylor 0:eafc3fd41f75 109 goto exit;
bryantaylor 0:eafc3fd41f75 110 }
bryantaylor 0:eafc3fd41f75 111
bryantaylor 0:eafc3fd41f75 112 s = c + 1;
bryantaylor 0:eafc3fd41f75 113 in_tag = 0;
bryantaylor 0:eafc3fd41f75 114 d = data;
bryantaylor 0:eafc3fd41f75 115 }
bryantaylor 0:eafc3fd41f75 116
bryantaylor 0:eafc3fd41f75 117 if( !in_tag && *c == '\\' && c != end )
bryantaylor 0:eafc3fd41f75 118 {
bryantaylor 0:eafc3fd41f75 119 c++;
bryantaylor 0:eafc3fd41f75 120
bryantaylor 0:eafc3fd41f75 121 /* Check for valid escaped characters */
bryantaylor 0:eafc3fd41f75 122 if( c == end || *c != ',' )
bryantaylor 0:eafc3fd41f75 123 {
bryantaylor 0:eafc3fd41f75 124 ret = MBEDTLS_ERR_X509_INVALID_NAME;
bryantaylor 0:eafc3fd41f75 125 goto exit;
bryantaylor 0:eafc3fd41f75 126 }
bryantaylor 0:eafc3fd41f75 127 }
bryantaylor 0:eafc3fd41f75 128 else if( !in_tag && ( *c == ',' || c == end ) )
bryantaylor 0:eafc3fd41f75 129 {
bryantaylor 0:eafc3fd41f75 130 if( mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
bryantaylor 0:eafc3fd41f75 131 (unsigned char *) data,
bryantaylor 0:eafc3fd41f75 132 d - data ) == NULL )
bryantaylor 0:eafc3fd41f75 133 {
bryantaylor 0:eafc3fd41f75 134 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
bryantaylor 0:eafc3fd41f75 135 }
bryantaylor 0:eafc3fd41f75 136
bryantaylor 0:eafc3fd41f75 137 while( c < end && *(c + 1) == ' ' )
bryantaylor 0:eafc3fd41f75 138 c++;
bryantaylor 0:eafc3fd41f75 139
bryantaylor 0:eafc3fd41f75 140 s = c + 1;
bryantaylor 0:eafc3fd41f75 141 in_tag = 1;
bryantaylor 0:eafc3fd41f75 142 }
bryantaylor 0:eafc3fd41f75 143
bryantaylor 0:eafc3fd41f75 144 if( !in_tag && s != c + 1 )
bryantaylor 0:eafc3fd41f75 145 {
bryantaylor 0:eafc3fd41f75 146 *(d++) = *c;
bryantaylor 0:eafc3fd41f75 147
bryantaylor 0:eafc3fd41f75 148 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
bryantaylor 0:eafc3fd41f75 149 {
bryantaylor 0:eafc3fd41f75 150 ret = MBEDTLS_ERR_X509_INVALID_NAME;
bryantaylor 0:eafc3fd41f75 151 goto exit;
bryantaylor 0:eafc3fd41f75 152 }
bryantaylor 0:eafc3fd41f75 153 }
bryantaylor 0:eafc3fd41f75 154
bryantaylor 0:eafc3fd41f75 155 c++;
bryantaylor 0:eafc3fd41f75 156 }
bryantaylor 0:eafc3fd41f75 157
bryantaylor 0:eafc3fd41f75 158 exit:
bryantaylor 0:eafc3fd41f75 159
bryantaylor 0:eafc3fd41f75 160 return( ret );
bryantaylor 0:eafc3fd41f75 161 }
bryantaylor 0:eafc3fd41f75 162
bryantaylor 0:eafc3fd41f75 163 /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
bryantaylor 0:eafc3fd41f75 164 * to store the critical boolean for us
bryantaylor 0:eafc3fd41f75 165 */
bryantaylor 0:eafc3fd41f75 166 int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
bryantaylor 0:eafc3fd41f75 167 int critical, const unsigned char *val, size_t val_len )
bryantaylor 0:eafc3fd41f75 168 {
bryantaylor 0:eafc3fd41f75 169 mbedtls_asn1_named_data *cur;
bryantaylor 0:eafc3fd41f75 170
bryantaylor 0:eafc3fd41f75 171 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
bryantaylor 0:eafc3fd41f75 172 NULL, val_len + 1 ) ) == NULL )
bryantaylor 0:eafc3fd41f75 173 {
bryantaylor 0:eafc3fd41f75 174 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
bryantaylor 0:eafc3fd41f75 175 }
bryantaylor 0:eafc3fd41f75 176
bryantaylor 0:eafc3fd41f75 177 cur->val.p[0] = critical;
bryantaylor 0:eafc3fd41f75 178 memcpy( cur->val.p + 1, val, val_len );
bryantaylor 0:eafc3fd41f75 179
bryantaylor 0:eafc3fd41f75 180 return( 0 );
bryantaylor 0:eafc3fd41f75 181 }
bryantaylor 0:eafc3fd41f75 182
bryantaylor 0:eafc3fd41f75 183 /*
bryantaylor 0:eafc3fd41f75 184 * RelativeDistinguishedName ::=
bryantaylor 0:eafc3fd41f75 185 * SET OF AttributeTypeAndValue
bryantaylor 0:eafc3fd41f75 186 *
bryantaylor 0:eafc3fd41f75 187 * AttributeTypeAndValue ::= SEQUENCE {
bryantaylor 0:eafc3fd41f75 188 * type AttributeType,
bryantaylor 0:eafc3fd41f75 189 * value AttributeValue }
bryantaylor 0:eafc3fd41f75 190 *
bryantaylor 0:eafc3fd41f75 191 * AttributeType ::= OBJECT IDENTIFIER
bryantaylor 0:eafc3fd41f75 192 *
bryantaylor 0:eafc3fd41f75 193 * AttributeValue ::= ANY DEFINED BY AttributeType
bryantaylor 0:eafc3fd41f75 194 */
bryantaylor 0:eafc3fd41f75 195 static int x509_write_name( unsigned char **p, unsigned char *start,
bryantaylor 0:eafc3fd41f75 196 const char *oid, size_t oid_len,
bryantaylor 0:eafc3fd41f75 197 const unsigned char *name, size_t name_len )
bryantaylor 0:eafc3fd41f75 198 {
bryantaylor 0:eafc3fd41f75 199 int ret;
bryantaylor 0:eafc3fd41f75 200 size_t len = 0;
bryantaylor 0:eafc3fd41f75 201
bryantaylor 0:eafc3fd41f75 202 // Write PrintableString for all except MBEDTLS_OID_PKCS9_EMAIL
bryantaylor 0:eafc3fd41f75 203 //
bryantaylor 0:eafc3fd41f75 204 if( MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_EMAIL ) == oid_len &&
bryantaylor 0:eafc3fd41f75 205 memcmp( oid, MBEDTLS_OID_PKCS9_EMAIL, oid_len ) == 0 )
bryantaylor 0:eafc3fd41f75 206 {
bryantaylor 0:eafc3fd41f75 207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
bryantaylor 0:eafc3fd41f75 208 (const char *) name,
bryantaylor 0:eafc3fd41f75 209 name_len ) );
bryantaylor 0:eafc3fd41f75 210 }
bryantaylor 0:eafc3fd41f75 211 else
bryantaylor 0:eafc3fd41f75 212 {
bryantaylor 0:eafc3fd41f75 213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start,
bryantaylor 0:eafc3fd41f75 214 (const char *) name,
bryantaylor 0:eafc3fd41f75 215 name_len ) );
bryantaylor 0:eafc3fd41f75 216 }
bryantaylor 0:eafc3fd41f75 217
bryantaylor 0:eafc3fd41f75 218 // Write OID
bryantaylor 0:eafc3fd41f75 219 //
bryantaylor 0:eafc3fd41f75 220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
bryantaylor 0:eafc3fd41f75 221
bryantaylor 0:eafc3fd41f75 222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
bryantaylor 0:eafc3fd41f75 223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
bryantaylor 0:eafc3fd41f75 224 MBEDTLS_ASN1_SEQUENCE ) );
bryantaylor 0:eafc3fd41f75 225
bryantaylor 0:eafc3fd41f75 226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
bryantaylor 0:eafc3fd41f75 227 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
bryantaylor 0:eafc3fd41f75 228 MBEDTLS_ASN1_SET ) );
bryantaylor 0:eafc3fd41f75 229
bryantaylor 0:eafc3fd41f75 230 return( (int) len );
bryantaylor 0:eafc3fd41f75 231 }
bryantaylor 0:eafc3fd41f75 232
bryantaylor 0:eafc3fd41f75 233 int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
bryantaylor 0:eafc3fd41f75 234 mbedtls_asn1_named_data *first )
bryantaylor 0:eafc3fd41f75 235 {
bryantaylor 0:eafc3fd41f75 236 int ret;
bryantaylor 0:eafc3fd41f75 237 size_t len = 0;
bryantaylor 0:eafc3fd41f75 238 mbedtls_asn1_named_data *cur = first;
bryantaylor 0:eafc3fd41f75 239
bryantaylor 0:eafc3fd41f75 240 while( cur != NULL )
bryantaylor 0:eafc3fd41f75 241 {
bryantaylor 0:eafc3fd41f75 242 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
bryantaylor 0:eafc3fd41f75 243 cur->oid.len,
bryantaylor 0:eafc3fd41f75 244 cur->val.p, cur->val.len ) );
bryantaylor 0:eafc3fd41f75 245 cur = cur->next;
bryantaylor 0:eafc3fd41f75 246 }
bryantaylor 0:eafc3fd41f75 247
bryantaylor 0:eafc3fd41f75 248 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
bryantaylor 0:eafc3fd41f75 249 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
bryantaylor 0:eafc3fd41f75 250 MBEDTLS_ASN1_SEQUENCE ) );
bryantaylor 0:eafc3fd41f75 251
bryantaylor 0:eafc3fd41f75 252 return( (int) len );
bryantaylor 0:eafc3fd41f75 253 }
bryantaylor 0:eafc3fd41f75 254
bryantaylor 0:eafc3fd41f75 255 int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
bryantaylor 0:eafc3fd41f75 256 const char *oid, size_t oid_len,
bryantaylor 0:eafc3fd41f75 257 unsigned char *sig, size_t size )
bryantaylor 0:eafc3fd41f75 258 {
bryantaylor 0:eafc3fd41f75 259 int ret;
bryantaylor 0:eafc3fd41f75 260 size_t len = 0;
bryantaylor 0:eafc3fd41f75 261
bryantaylor 0:eafc3fd41f75 262 if( *p < start || (size_t)( *p - start ) < size )
bryantaylor 0:eafc3fd41f75 263 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
bryantaylor 0:eafc3fd41f75 264
bryantaylor 0:eafc3fd41f75 265 len = size;
bryantaylor 0:eafc3fd41f75 266 (*p) -= len;
bryantaylor 0:eafc3fd41f75 267 memcpy( *p, sig, len );
bryantaylor 0:eafc3fd41f75 268
bryantaylor 0:eafc3fd41f75 269 if( *p - start < 1 )
bryantaylor 0:eafc3fd41f75 270 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
bryantaylor 0:eafc3fd41f75 271
bryantaylor 0:eafc3fd41f75 272 *--(*p) = 0;
bryantaylor 0:eafc3fd41f75 273 len += 1;
bryantaylor 0:eafc3fd41f75 274
bryantaylor 0:eafc3fd41f75 275 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
bryantaylor 0:eafc3fd41f75 276 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
bryantaylor 0:eafc3fd41f75 277
bryantaylor 0:eafc3fd41f75 278 // Write OID
bryantaylor 0:eafc3fd41f75 279 //
bryantaylor 0:eafc3fd41f75 280 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
bryantaylor 0:eafc3fd41f75 281 oid_len, 0 ) );
bryantaylor 0:eafc3fd41f75 282
bryantaylor 0:eafc3fd41f75 283 return( (int) len );
bryantaylor 0:eafc3fd41f75 284 }
bryantaylor 0:eafc3fd41f75 285
bryantaylor 0:eafc3fd41f75 286 static int x509_write_extension( unsigned char **p, unsigned char *start,
bryantaylor 0:eafc3fd41f75 287 mbedtls_asn1_named_data *ext )
bryantaylor 0:eafc3fd41f75 288 {
bryantaylor 0:eafc3fd41f75 289 int ret;
bryantaylor 0:eafc3fd41f75 290 size_t len = 0;
bryantaylor 0:eafc3fd41f75 291
bryantaylor 0:eafc3fd41f75 292 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
bryantaylor 0:eafc3fd41f75 293 ext->val.len - 1 ) );
bryantaylor 0:eafc3fd41f75 294 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
bryantaylor 0:eafc3fd41f75 295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
bryantaylor 0:eafc3fd41f75 296
bryantaylor 0:eafc3fd41f75 297 if( ext->val.p[0] != 0 )
bryantaylor 0:eafc3fd41f75 298 {
bryantaylor 0:eafc3fd41f75 299 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
bryantaylor 0:eafc3fd41f75 300 }
bryantaylor 0:eafc3fd41f75 301
bryantaylor 0:eafc3fd41f75 302 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
bryantaylor 0:eafc3fd41f75 303 ext->oid.len ) );
bryantaylor 0:eafc3fd41f75 304 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
bryantaylor 0:eafc3fd41f75 305 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
bryantaylor 0:eafc3fd41f75 306
bryantaylor 0:eafc3fd41f75 307 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
bryantaylor 0:eafc3fd41f75 308 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
bryantaylor 0:eafc3fd41f75 309 MBEDTLS_ASN1_SEQUENCE ) );
bryantaylor 0:eafc3fd41f75 310
bryantaylor 0:eafc3fd41f75 311 return( (int) len );
bryantaylor 0:eafc3fd41f75 312 }
bryantaylor 0:eafc3fd41f75 313
bryantaylor 0:eafc3fd41f75 314 /*
bryantaylor 0:eafc3fd41f75 315 * Extension ::= SEQUENCE {
bryantaylor 0:eafc3fd41f75 316 * extnID OBJECT IDENTIFIER,
bryantaylor 0:eafc3fd41f75 317 * critical BOOLEAN DEFAULT FALSE,
bryantaylor 0:eafc3fd41f75 318 * extnValue OCTET STRING
bryantaylor 0:eafc3fd41f75 319 * -- contains the DER encoding of an ASN.1 value
bryantaylor 0:eafc3fd41f75 320 * -- corresponding to the extension type identified
bryantaylor 0:eafc3fd41f75 321 * -- by extnID
bryantaylor 0:eafc3fd41f75 322 * }
bryantaylor 0:eafc3fd41f75 323 */
bryantaylor 0:eafc3fd41f75 324 int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
bryantaylor 0:eafc3fd41f75 325 mbedtls_asn1_named_data *first )
bryantaylor 0:eafc3fd41f75 326 {
bryantaylor 0:eafc3fd41f75 327 int ret;
bryantaylor 0:eafc3fd41f75 328 size_t len = 0;
bryantaylor 0:eafc3fd41f75 329 mbedtls_asn1_named_data *cur_ext = first;
bryantaylor 0:eafc3fd41f75 330
bryantaylor 0:eafc3fd41f75 331 while( cur_ext != NULL )
bryantaylor 0:eafc3fd41f75 332 {
bryantaylor 0:eafc3fd41f75 333 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
bryantaylor 0:eafc3fd41f75 334 cur_ext = cur_ext->next;
bryantaylor 0:eafc3fd41f75 335 }
bryantaylor 0:eafc3fd41f75 336
bryantaylor 0:eafc3fd41f75 337 return( (int) len );
bryantaylor 0:eafc3fd41f75 338 }
bryantaylor 0:eafc3fd41f75 339
bryantaylor 0:eafc3fd41f75 340 #endif /* MBEDTLS_X509_CREATE_C */