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