mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
mbed-os5 only for TYBLE16

Who changed what in which revision?

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