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