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