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