Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 base functions for creating certificates / CSRs |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_X509_CREATE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/x509.h" |
| 25 | #include "mbedtls/asn1write.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/oid.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 28 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 31 | #include "mbedtls/platform.h" |
| 32 | |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 33 | #include "mbedtls/asn1.h" |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 34 | |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 35 | /* Structure linking OIDs for X.509 DN AttributeTypes to their |
| 36 | * string representations and default string encodings used by Mbed TLS. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 37 | typedef struct { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | const char *name; /* String representation of AttributeType, e.g. |
| 39 | * "CN" or "emailAddress". */ |
| 40 | size_t name_len; /* Length of 'name', without trailing 0 byte. */ |
| 41 | const char *oid; /* String representation of OID of AttributeType, |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 42 | * as per RFC 5280, Appendix A.1. encoded as per |
| 43 | * X.690 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | int default_tag; /* The default character encoding used for the |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 45 | * given attribute type, e.g. |
Hanno Becker | ee334a3 | 2018-10-24 12:33:07 +0100 | [diff] [blame] | 46 | * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 47 | } x509_attr_descriptor_t; |
| 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | #define ADD_STRLEN(s) s, sizeof(s) - 1 |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 50 | |
Hanno Becker | 35b6854 | 2018-10-08 14:47:38 +0100 | [diff] [blame] | 51 | /* X.509 DN attributes from RFC 5280, Appendix A.1. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 52 | static const x509_attr_descriptor_t x509_attrs[] = |
| 53 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | { ADD_STRLEN("CN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 55 | MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | { ADD_STRLEN("commonName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 57 | MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | { ADD_STRLEN("C"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 59 | MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | { ADD_STRLEN("countryName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 61 | MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | { ADD_STRLEN("O"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 63 | MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | { ADD_STRLEN("organizationName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 65 | MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | { ADD_STRLEN("L"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 67 | MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | { ADD_STRLEN("locality"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 69 | MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | { ADD_STRLEN("R"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 71 | MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | { ADD_STRLEN("OU"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 73 | MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | { ADD_STRLEN("organizationalUnitName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 75 | MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | { ADD_STRLEN("ST"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 77 | MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | { ADD_STRLEN("stateOrProvinceName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 79 | MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | { ADD_STRLEN("emailAddress"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 81 | MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | { ADD_STRLEN("serialNumber"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 83 | MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | { ADD_STRLEN("postalAddress"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 85 | MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | { ADD_STRLEN("postalCode"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 87 | MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | { ADD_STRLEN("dnQualifier"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 89 | MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | { ADD_STRLEN("title"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 91 | MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | { ADD_STRLEN("surName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 93 | MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | { ADD_STRLEN("SN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 95 | MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | { ADD_STRLEN("givenName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 97 | MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | { ADD_STRLEN("GN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 99 | MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | { ADD_STRLEN("initials"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 101 | MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | { ADD_STRLEN("pseudonym"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 103 | MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | { ADD_STRLEN("generationQualifier"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 105 | MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | { ADD_STRLEN("domainComponent"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 107 | MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | { ADD_STRLEN("DC"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 109 | MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, |
tdoe | c150f0d | 2018-05-18 12:12:45 +0200 | [diff] [blame] | 110 | { NULL, 0, NULL, MBEDTLS_ASN1_NULL } |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | 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] | 114 | { |
| 115 | const x509_attr_descriptor_t *cur; |
| 116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | for (cur = x509_attrs; cur->name != NULL; cur++) { |
| 118 | if (cur->name_len == name_len && |
| 119 | strncmp(cur->name, name, name_len) == 0) { |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 120 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | if (cur->name == NULL) { |
| 125 | return NULL; |
| 126 | } |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | return cur; |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 131 | static int hex_to_int(char c) |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 132 | { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 133 | return ('0' <= c && c <= '9') ? (c - '0') : |
| 134 | ('a' <= c && c <= 'f') ? (c - 'a' + 10) : |
| 135 | ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1; |
| 136 | } |
| 137 | |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 138 | static int hexpair_to_int(const char *hexpair) |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 139 | { |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 140 | int n1 = hex_to_int(*hexpair); |
| 141 | int n2 = hex_to_int(*(hexpair + 1)); |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 142 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 143 | if (n1 != -1 && n2 != -1) { |
| 144 | return (n1 << 4) | n2; |
| 145 | } else { |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 150 | static int parse_attribute_value_string(const char *s, |
| 151 | int len, |
| 152 | unsigned char *data, |
| 153 | size_t *data_len) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 154 | { |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 155 | const char *c; |
| 156 | const char *end = s + len; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 157 | unsigned char *d = data; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 158 | int n; |
Agathiyan Bragadeesh | a2423de | 2023-08-30 16:24:31 +0100 | [diff] [blame] | 159 | |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 160 | for (c = s; c < end; c++) { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 161 | if (*c == '\\') { |
| 162 | c++; |
| 163 | |
Agathiyan Bragadeesh | e9d1c8e | 2023-08-30 15:50:12 +0100 | [diff] [blame] | 164 | /* Check for valid escaped characters as per RFC 4514 Section 3 */ |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 165 | if (c + 1 < end && (n = hexpair_to_int(c)) != -1) { |
Agathiyan Bragadeesh | eb55867 | 2023-08-14 16:31:11 +0100 | [diff] [blame] | 166 | if (n == 0) { |
Agathiyan Bragadeesh | 9caaa6d | 2023-08-14 15:38:39 +0100 | [diff] [blame] | 167 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 168 | } |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 169 | *(d++) = n; |
| 170 | c++; |
Agathiyan Bragadeesh | c34804d | 2023-09-08 11:32:19 +0100 | [diff] [blame] | 171 | } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) { |
| 172 | *(d++) = *c; |
| 173 | } else { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 174 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 175 | } |
Agathiyan Bragadeesh | 706a1c3 | 2023-09-08 12:04:41 +0100 | [diff] [blame] | 176 | } else { |
Agathiyan Bragadeesh | c34804d | 2023-09-08 11:32:19 +0100 | [diff] [blame] | 177 | *(d++) = *c; |
| 178 | } |
Agathiyan Bragadeesh | a2423de | 2023-08-30 16:24:31 +0100 | [diff] [blame] | 179 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 180 | if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) { |
| 181 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 182 | } |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 183 | } |
| 184 | *data_len = d - data; |
| 185 | return 0; |
| 186 | } |
| 187 | |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 188 | /** Parse a hexstring containing a DER-encoded string. |
| 189 | * |
| 190 | * \param s A string of \p len bytes hexadecimal digits. |
| 191 | * \param len Number of bytes to read from \p s. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 192 | * \param data Output buffer of size \p data_size. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 193 | * On success, it contains the payload that's DER-encoded |
| 194 | * in the input (content without the tag and length). |
| 195 | * If the DER tag is a string tag, the payload is guaranteed |
| 196 | * not to contain null bytes. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 197 | * \param data_size Length of the \p data buffer. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 198 | * \param data_len On success, the length of the parsed string. |
| 199 | * It is guaranteed to be less than |
| 200 | * #MBEDTLS_X509_MAX_DN_NAME_SIZE. |
| 201 | * \param tag The ASN.1 tag that the payload in \p data is encoded in. |
| 202 | * |
| 203 | * \retval 0 on success. |
| 204 | * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain |
| 205 | * a valid hexstring, |
| 206 | * or if the decoded hexstring is not valid DER, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 207 | * or if the payload does not fit in \p data, |
| 208 | * or if the payload is more than |
| 209 | * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 210 | * of if \p *tag is an ASN.1 string tag and the payload |
| 211 | * contains a null byte. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 212 | * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 213 | */ |
| 214 | static int parse_attribute_value_hex_der_encoded(const char *s, |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 215 | size_t len, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 216 | unsigned char *data, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 217 | size_t data_size, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 218 | size_t *data_len, |
| 219 | int *tag) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 220 | { |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 221 | /* Step 1: preliminary length checks. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 222 | /* Each byte is encoded by exactly two hexadecimal digits. */ |
| 223 | if (len % 2 != 0) { |
| 224 | /* Odd number of hex digits */ |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 225 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 226 | } |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 227 | size_t const der_length = len / 2; |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 228 | if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) { |
| 229 | /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE |
| 230 | * (after subtracting the ASN.1 tag and length). Reject this early |
| 231 | * to avoid allocating a large intermediate buffer. */ |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 232 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 233 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 234 | if (der_length < 1) { |
| 235 | /* Avoid empty-buffer shenanigans. A valid DER encoding is never |
| 236 | * empty. */ |
| 237 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 238 | } |
| 239 | |
| 240 | /* Step 2: Decode the hex string into an intermediate buffer. */ |
| 241 | unsigned char *der = mbedtls_calloc(1, der_length); |
| 242 | if (der == NULL) { |
| 243 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 244 | } |
| 245 | /* Beyond this point, der needs to be freed on exit. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 246 | for (size_t i = 0; i < der_length; i++) { |
| 247 | int c = hexpair_to_int(s + 2 * i); |
| 248 | if (c < 0) { |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 249 | goto error; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 250 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 251 | der[i] = c; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 252 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 253 | |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 254 | /* Step 3: decode the DER. */ |
| 255 | /* We've checked that der_length >= 1 above. */ |
| 256 | *tag = der[0]; |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 257 | { |
| 258 | unsigned char *p = der + 1; |
| 259 | if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) { |
| 260 | goto error; |
| 261 | } |
| 262 | /* Now p points to the first byte of the payload inside der, |
| 263 | * and *data_len is the length of the payload. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 264 | |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 265 | /* Step 4: payload validation */ |
| 266 | if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) { |
| 267 | goto error; |
| 268 | } |
| 269 | /* Strings must not contain null bytes. */ |
| 270 | if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) { |
| 271 | for (size_t i = 0; i < *data_len; i++) { |
| 272 | if (p[i] == 0) { |
| 273 | goto error; |
| 274 | } |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 277 | |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 278 | /* Step 5: output the payload. */ |
| 279 | if (*data_len > data_size) { |
| 280 | goto error; |
| 281 | } |
| 282 | memcpy(data, p, *data_len); |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 283 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 284 | mbedtls_free(der); |
| 285 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 286 | return 0; |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 287 | |
| 288 | error: |
| 289 | mbedtls_free(der); |
| 290 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | 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] | 294 | { |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 295 | int ret = MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 296 | int parse_ret = 0; |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 297 | const char *s = name, *c = s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | const char *end = s + strlen(s); |
Agathiyan Bragadeesh | ba386ec | 2023-08-16 11:31:17 +0100 | [diff] [blame] | 299 | mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | const x509_attr_descriptor_t *attr_descr = NULL; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 301 | int in_attr_type = 1; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 302 | int tag; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 303 | int numericoid = 0; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 304 | unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
| 305 | size_t data_len = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 306 | |
| 307 | /* Clear existing chain if present */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | mbedtls_asn1_free_named_data_list(head); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | while (c <= end) { |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 311 | if (in_attr_type && *c == '=') { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 313 | if ((mbedtls_oid_from_numeric_string(&oid, s, c - s)) != 0) { |
Agathiyan Bragadeesh | 1798487 | 2023-08-11 12:42:03 +0100 | [diff] [blame] | 314 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 315 | } else { |
| 316 | numericoid = 1; |
| 317 | } |
| 318 | } else { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 319 | oid.len = strlen(attr_descr->oid); |
| 320 | oid.p = mbedtls_calloc(1, oid.len); |
| 321 | memcpy(oid.p, attr_descr->oid, oid.len); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 322 | numericoid = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 326 | in_attr_type = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 329 | if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) { |
Agathiyan Bragadeesh | 457ac84 | 2023-08-23 11:35:26 +0100 | [diff] [blame] | 330 | if (s == c) { |
Agathiyan Bragadeesh | 4c7d7bf | 2023-08-23 11:28:30 +0100 | [diff] [blame] | 331 | mbedtls_free(oid.p); |
| 332 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 333 | } else if (*s == '#') { |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 334 | /* We know that c >= s (loop invariant) and c != s (in this |
| 335 | * else branch), hence c - s - 1 >= 0. */ |
| 336 | parse_ret = parse_attribute_value_hex_der_encoded( |
| 337 | s + 1, c - s - 1, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 338 | data, sizeof(data), &data_len, &tag); |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 339 | if (parse_ret != 0) { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 340 | mbedtls_free(oid.p); |
Gilles Peskine | 391dd7f | 2023-09-21 18:51:35 +0200 | [diff] [blame] | 341 | return parse_ret; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 342 | } |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 343 | } else { |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 344 | if (numericoid) { |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 345 | mbedtls_free(oid.p); |
| 346 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 347 | } else { |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame] | 348 | if ((parse_ret = |
Agathiyan Bragadeesh | eb55867 | 2023-08-14 16:31:11 +0100 | [diff] [blame] | 349 | parse_attribute_value_string(s, (int) (c - s), data, |
| 350 | &data_len)) != 0) { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 351 | mbedtls_free(oid.p); |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame] | 352 | return parse_ret; |
| 353 | } |
| 354 | tag = attr_descr->default_tag; |
| 355 | } |
| 356 | } |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | mbedtls_asn1_named_data *cur = |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 359 | mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | (unsigned char *) data, |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 361 | data_len); |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 362 | mbedtls_free(oid.p); |
| 363 | oid.p = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | if (cur == NULL) { |
| 365 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 368 | // set tagType |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 369 | cur->val.tag = tag; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | while (c < end && *(c + 1) == ' ') { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 372 | c++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 374 | |
| 375 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 376 | in_attr_type = 1; |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 377 | |
| 378 | /* Successfully parsed one name, update ret to success */ |
| 379 | ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | } |
| 381 | c++; |
| 382 | } |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 383 | if (oid.p != NULL) { |
| 384 | mbedtls_free(oid.p); |
Agathiyan Bragadeesh | 55d9319 | 2023-08-15 15:05:03 +0100 | [diff] [blame] | 385 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | /* 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] | 390 | * to store the critical boolean for us |
| 391 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, |
| 393 | int critical, const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len, |
| 398 | NULL, val_len + 1)) == NULL) { |
| 399 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | cur->val.p[0] = critical; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | memcpy(cur->val.p + 1, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
| 409 | * RelativeDistinguishedName ::= |
| 410 | * SET OF AttributeTypeAndValue |
| 411 | * |
| 412 | * AttributeTypeAndValue ::= SEQUENCE { |
| 413 | * type AttributeType, |
| 414 | * value AttributeValue } |
| 415 | * |
| 416 | * AttributeType ::= OBJECT IDENTIFIER |
| 417 | * |
| 418 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 419 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | static int x509_write_name(unsigned char **p, |
| 421 | unsigned char *start, |
| 422 | mbedtls_asn1_named_data *cur_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 423 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 424 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 425 | size_t len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | const char *oid = (const char *) cur_name->oid.p; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 427 | size_t oid_len = cur_name->oid.len; |
| 428 | const unsigned char *name = cur_name->val.p; |
| 429 | size_t name_len = cur_name->val.len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 430 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 431 | // Write correct string tag and value |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start, |
| 433 | cur_name->val.tag, |
| 434 | (const char *) name, |
| 435 | name_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 436 | // Write OID |
| 437 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, |
| 439 | oid_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 442 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 443 | MBEDTLS_ASN1_CONSTRUCTED | |
| 444 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 447 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 448 | MBEDTLS_ASN1_CONSTRUCTED | |
| 449 | MBEDTLS_ASN1_SET)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | int mbedtls_x509_write_names(unsigned char **p, unsigned char *start, |
| 455 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 456 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 457 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 458 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_asn1_named_data *cur = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | while (cur != NULL) { |
| 462 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 463 | cur = cur->next; |
| 464 | } |
| 465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 467 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 468 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start, |
| 474 | const char *oid, size_t oid_len, |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 475 | unsigned char *sig, size_t size, |
| 476 | mbedtls_pk_type_t pk_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 477 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 478 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 479 | int write_null_par; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 480 | size_t len = 0; |
| 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | if (*p < start || (size_t) (*p - start) < size) { |
| 483 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 484 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 485 | |
| 486 | len = size; |
| 487 | (*p) -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | memcpy(*p, sig, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | if (*p - start < 1) { |
| 491 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 492 | } |
Manuel Pégourié-Gonnard | 4dc9b39 | 2015-10-21 12:23:09 +0200 | [diff] [blame] | 493 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 494 | *--(*p) = 0; |
| 495 | len += 1; |
| 496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 498 | 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] | 499 | |
| 500 | // Write OID |
| 501 | // |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 502 | if (pk_alg == MBEDTLS_PK_ECDSA) { |
| 503 | /* |
| 504 | * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature |
| 505 | * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and |
| 506 | * https://www.rfc-editor.org/rfc/rfc5758#section-3. |
| 507 | */ |
| 508 | write_null_par = 0; |
| 509 | } else { |
| 510 | write_null_par = 1; |
| 511 | } |
| 512 | MBEDTLS_ASN1_CHK_ADD(len, |
| 513 | mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, |
| 514 | 0, write_null_par)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | static int x509_write_extension(unsigned char **p, unsigned char *start, |
| 520 | mbedtls_asn1_named_data *ext) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 521 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 522 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 523 | size_t len = 0; |
| 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1, |
| 526 | ext->val.len - 1)); |
| 527 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1)); |
| 528 | 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] | 529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | if (ext->val.p[0] != 0) { |
| 531 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p, |
| 535 | ext->oid.len)); |
| 536 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len)); |
| 537 | 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] | 538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 540 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 541 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Extension ::= SEQUENCE { |
| 548 | * extnID OBJECT IDENTIFIER, |
| 549 | * critical BOOLEAN DEFAULT FALSE, |
| 550 | * extnValue OCTET STRING |
| 551 | * -- contains the DER encoding of an ASN.1 value |
| 552 | * -- corresponding to the extension type identified |
| 553 | * -- by extnID |
| 554 | * } |
| 555 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start, |
| 557 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 558 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 559 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 560 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | mbedtls_asn1_named_data *cur_ext = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | while (cur_ext != NULL) { |
| 564 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 565 | cur_ext = cur_ext->next; |
| 566 | } |
| 567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | #endif /* MBEDTLS_X509_CREATE_C */ |