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