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