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