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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Harry Ramsey | 0f6bc41 | 2024-10-04 10:36:54 +0100 | [diff] [blame] | 8 | #include "x509_internal.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_X509_CREATE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/asn1write.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 13 | #include "mbedtls/error.h" |
Gilles Peskine | 86a47f8 | 2025-05-07 20:20:12 +0200 | [diff] [blame^] | 14 | #include "x509_oid.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 15 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 16 | #include <string.h> |
| 17 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 18 | #include "mbedtls/platform.h" |
| 19 | |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 20 | #include "mbedtls/asn1.h" |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 21 | |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 22 | /* Structure linking OIDs for X.509 DN AttributeTypes to their |
| 23 | * string representations and default string encodings used by Mbed TLS. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 24 | typedef struct { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | const char *name; /* String representation of AttributeType, e.g. |
| 26 | * "CN" or "emailAddress". */ |
| 27 | size_t name_len; /* Length of 'name', without trailing 0 byte. */ |
| 28 | const char *oid; /* String representation of OID of AttributeType, |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 29 | * as per RFC 5280, Appendix A.1. encoded as per |
| 30 | * X.690 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | int default_tag; /* The default character encoding used for the |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 32 | * given attribute type, e.g. |
Hanno Becker | ee334a3 | 2018-10-24 12:33:07 +0100 | [diff] [blame] | 33 | * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 34 | } x509_attr_descriptor_t; |
| 35 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | #define ADD_STRLEN(s) s, sizeof(s) - 1 |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 37 | |
Hanno Becker | 35b6854 | 2018-10-08 14:47:38 +0100 | [diff] [blame] | 38 | /* X.509 DN attributes from RFC 5280, Appendix A.1. */ |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 39 | static const x509_attr_descriptor_t x509_attrs[] = |
| 40 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | { ADD_STRLEN("CN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 42 | MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | { ADD_STRLEN("commonName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 44 | MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | { ADD_STRLEN("C"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 46 | MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | { ADD_STRLEN("countryName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 48 | MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | { ADD_STRLEN("O"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 50 | MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | { ADD_STRLEN("organizationName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 52 | MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | { ADD_STRLEN("L"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 54 | MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | { ADD_STRLEN("locality"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 56 | MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | { ADD_STRLEN("R"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 58 | MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | { ADD_STRLEN("OU"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 60 | MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | { ADD_STRLEN("organizationalUnitName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 62 | MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | { ADD_STRLEN("ST"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 64 | MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | { ADD_STRLEN("stateOrProvinceName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 66 | MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | { ADD_STRLEN("emailAddress"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 68 | MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | { ADD_STRLEN("serialNumber"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 70 | MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | { ADD_STRLEN("postalAddress"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 72 | MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | { ADD_STRLEN("postalCode"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 74 | MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | { ADD_STRLEN("dnQualifier"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 76 | MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | { ADD_STRLEN("title"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 78 | MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | { ADD_STRLEN("surName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 80 | MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | { ADD_STRLEN("SN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 82 | MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | { ADD_STRLEN("givenName"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 84 | MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | { ADD_STRLEN("GN"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 86 | MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | { ADD_STRLEN("initials"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 88 | MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | { ADD_STRLEN("pseudonym"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 90 | MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | { ADD_STRLEN("generationQualifier"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 92 | MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | { ADD_STRLEN("domainComponent"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 94 | MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | { ADD_STRLEN("DC"), |
Hanno Becker | 1624e2e | 2018-10-08 14:52:20 +0100 | [diff] [blame] | 96 | MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING }, |
tdoe | c150f0d | 2018-05-18 12:12:45 +0200 | [diff] [blame] | 97 | { NULL, 0, NULL, MBEDTLS_ASN1_NULL } |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | 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] | 101 | { |
| 102 | const x509_attr_descriptor_t *cur; |
| 103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | for (cur = x509_attrs; cur->name != NULL; cur++) { |
| 105 | if (cur->name_len == name_len && |
| 106 | strncmp(cur->name, name, name_len) == 0) { |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 107 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 110 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | if (cur->name == NULL) { |
| 112 | return NULL; |
| 113 | } |
Hanno Becker | d2c9009 | 2018-10-08 14:32:55 +0100 | [diff] [blame] | 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | return cur; |
Manuel Pégourié-Gonnard | f3e5c22 | 2014-06-12 11:06:36 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 118 | static int hex_to_int(char c) |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 119 | { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 120 | return ('0' <= c && c <= '9') ? (c - '0') : |
| 121 | ('a' <= c && c <= 'f') ? (c - 'a' + 10) : |
| 122 | ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1; |
| 123 | } |
| 124 | |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 125 | static int hexpair_to_int(const char *hexpair) |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 126 | { |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 127 | int n1 = hex_to_int(*hexpair); |
| 128 | int n2 = hex_to_int(*(hexpair + 1)); |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 129 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 130 | if (n1 != -1 && n2 != -1) { |
| 131 | return (n1 << 4) | n2; |
| 132 | } else { |
| 133 | return -1; |
| 134 | } |
| 135 | } |
| 136 | |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 137 | static int parse_attribute_value_string(const char *s, |
| 138 | int len, |
| 139 | unsigned char *data, |
| 140 | size_t *data_len) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 141 | { |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 142 | const char *c; |
| 143 | const char *end = s + len; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 144 | unsigned char *d = data; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 145 | int n; |
Agathiyan Bragadeesh | a2423de | 2023-08-30 16:24:31 +0100 | [diff] [blame] | 146 | |
Agathiyan Bragadeesh | de02ee2 | 2023-08-30 16:12:57 +0100 | [diff] [blame] | 147 | for (c = s; c < end; c++) { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 148 | if (*c == '\\') { |
| 149 | c++; |
| 150 | |
Agathiyan Bragadeesh | e9d1c8e | 2023-08-30 15:50:12 +0100 | [diff] [blame] | 151 | /* Check for valid escaped characters as per RFC 4514 Section 3 */ |
Agathiyan Bragadeesh | 1aece47 | 2023-08-30 16:04:16 +0100 | [diff] [blame] | 152 | if (c + 1 < end && (n = hexpair_to_int(c)) != -1) { |
Agathiyan Bragadeesh | eb55867 | 2023-08-14 16:31:11 +0100 | [diff] [blame] | 153 | if (n == 0) { |
Agathiyan Bragadeesh | 9caaa6d | 2023-08-14 15:38:39 +0100 | [diff] [blame] | 154 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 155 | } |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 156 | *(d++) = n; |
| 157 | c++; |
Agathiyan Bragadeesh | c34804d | 2023-09-08 11:32:19 +0100 | [diff] [blame] | 158 | } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) { |
| 159 | *(d++) = *c; |
| 160 | } else { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 161 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 162 | } |
Agathiyan Bragadeesh | 706a1c3 | 2023-09-08 12:04:41 +0100 | [diff] [blame] | 163 | } else { |
Agathiyan Bragadeesh | c34804d | 2023-09-08 11:32:19 +0100 | [diff] [blame] | 164 | *(d++) = *c; |
| 165 | } |
Agathiyan Bragadeesh | a2423de | 2023-08-30 16:24:31 +0100 | [diff] [blame] | 166 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 167 | if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) { |
| 168 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 169 | } |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 170 | } |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 171 | *data_len = (size_t) (d - data); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 175 | /** Parse a hexstring containing a DER-encoded string. |
| 176 | * |
| 177 | * \param s A string of \p len bytes hexadecimal digits. |
| 178 | * \param len Number of bytes to read from \p s. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 179 | * \param data Output buffer of size \p data_size. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 180 | * On success, it contains the payload that's DER-encoded |
| 181 | * in the input (content without the tag and length). |
| 182 | * If the DER tag is a string tag, the payload is guaranteed |
| 183 | * not to contain null bytes. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 184 | * \param data_size Length of the \p data buffer. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 185 | * \param data_len On success, the length of the parsed string. |
| 186 | * It is guaranteed to be less than |
| 187 | * #MBEDTLS_X509_MAX_DN_NAME_SIZE. |
| 188 | * \param tag The ASN.1 tag that the payload in \p data is encoded in. |
| 189 | * |
| 190 | * \retval 0 on success. |
| 191 | * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain |
| 192 | * a valid hexstring, |
| 193 | * or if the decoded hexstring is not valid DER, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 194 | * or if the payload does not fit in \p data, |
| 195 | * or if the payload is more than |
| 196 | * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 197 | * of if \p *tag is an ASN.1 string tag and the payload |
| 198 | * contains a null byte. |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 199 | * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory. |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 200 | */ |
| 201 | static int parse_attribute_value_hex_der_encoded(const char *s, |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 202 | size_t len, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 203 | unsigned char *data, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 204 | size_t data_size, |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 205 | size_t *data_len, |
| 206 | int *tag) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 207 | { |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 208 | /* Step 1: preliminary length checks. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 209 | /* Each byte is encoded by exactly two hexadecimal digits. */ |
| 210 | if (len % 2 != 0) { |
| 211 | /* Odd number of hex digits */ |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 212 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 213 | } |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 214 | size_t const der_length = len / 2; |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 215 | if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) { |
| 216 | /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE |
| 217 | * (after subtracting the ASN.1 tag and length). Reject this early |
| 218 | * to avoid allocating a large intermediate buffer. */ |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 219 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 220 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 221 | if (der_length < 1) { |
| 222 | /* Avoid empty-buffer shenanigans. A valid DER encoding is never |
| 223 | * empty. */ |
| 224 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 225 | } |
| 226 | |
| 227 | /* Step 2: Decode the hex string into an intermediate buffer. */ |
| 228 | unsigned char *der = mbedtls_calloc(1, der_length); |
| 229 | if (der == NULL) { |
| 230 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 231 | } |
| 232 | /* Beyond this point, der needs to be freed on exit. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 233 | for (size_t i = 0; i < der_length; i++) { |
| 234 | int c = hexpair_to_int(s + 2 * i); |
| 235 | if (c < 0) { |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 236 | goto error; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 237 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 238 | der[i] = c; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 239 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 240 | |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 241 | /* Step 3: decode the DER. */ |
| 242 | /* We've checked that der_length >= 1 above. */ |
| 243 | *tag = der[0]; |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 244 | { |
| 245 | unsigned char *p = der + 1; |
| 246 | if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) { |
| 247 | goto error; |
| 248 | } |
| 249 | /* Now p points to the first byte of the payload inside der, |
| 250 | * and *data_len is the length of the payload. */ |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 251 | |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 252 | /* Step 4: payload validation */ |
| 253 | if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) { |
| 254 | goto error; |
| 255 | } |
| 256 | /* Strings must not contain null bytes. */ |
| 257 | if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) { |
| 258 | for (size_t i = 0; i < *data_len; i++) { |
| 259 | if (p[i] == 0) { |
| 260 | goto error; |
| 261 | } |
Gilles Peskine | 2566578 | 2023-09-21 14:03:52 +0200 | [diff] [blame] | 262 | } |
| 263 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 264 | |
Dave Rodgman | 515af1d | 2023-10-13 14:40:14 +0100 | [diff] [blame] | 265 | /* Step 5: output the payload. */ |
| 266 | if (*data_len > data_size) { |
| 267 | goto error; |
| 268 | } |
| 269 | memcpy(data, p, *data_len); |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 270 | } |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 271 | mbedtls_free(der); |
| 272 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 273 | return 0; |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 274 | |
| 275 | error: |
| 276 | mbedtls_free(der); |
| 277 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Sam Berry | 3da783b | 2024-09-13 15:09:24 +0100 | [diff] [blame] | 280 | static int oid_parse_number(unsigned int *num, const char **p, const char *bound) |
| 281 | { |
| 282 | int ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 283 | |
| 284 | *num = 0; |
| 285 | |
| 286 | while (*p < bound && **p >= '0' && **p <= '9') { |
| 287 | ret = 0; |
| 288 | if (*num > (UINT_MAX / 10)) { |
| 289 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 290 | } |
| 291 | *num *= 10; |
| 292 | *num += **p - '0'; |
| 293 | (*p)++; |
| 294 | } |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static size_t oid_subidentifier_num_bytes(unsigned int value) |
| 299 | { |
| 300 | size_t num_bytes = 0; |
| 301 | |
| 302 | do { |
| 303 | value >>= 7; |
| 304 | num_bytes++; |
| 305 | } while (value != 0); |
| 306 | |
| 307 | return num_bytes; |
| 308 | } |
| 309 | |
| 310 | static int oid_subidentifier_encode_into(unsigned char **p, |
| 311 | unsigned char *bound, |
| 312 | unsigned int value) |
| 313 | { |
| 314 | size_t num_bytes = oid_subidentifier_num_bytes(value); |
| 315 | |
| 316 | if ((size_t) (bound - *p) < num_bytes) { |
| 317 | return MBEDTLS_ERR_OID_BUF_TOO_SMALL; |
| 318 | } |
| 319 | (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f); |
| 320 | value >>= 7; |
| 321 | |
| 322 | for (size_t i = 2; i <= num_bytes; i++) { |
| 323 | (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f); |
| 324 | value >>= 7; |
| 325 | } |
| 326 | *p += num_bytes; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Sam Berry | c71abc3 | 2024-07-19 15:11:10 +0100 | [diff] [blame] | 331 | /* Return the OID for the given x.y.z.... style numeric string */ |
| 332 | int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid, |
| 333 | const char *oid_str, size_t size) |
| 334 | { |
| 335 | int ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 336 | const char *str_ptr = oid_str; |
| 337 | const char *str_bound = oid_str + size; |
| 338 | unsigned int val = 0; |
| 339 | unsigned int component1, component2; |
| 340 | size_t encoded_len; |
| 341 | unsigned char *resized_mem; |
| 342 | |
| 343 | /* Count the number of dots to get a worst-case allocation size. */ |
| 344 | size_t num_dots = 0; |
| 345 | for (size_t i = 0; i < size; i++) { |
| 346 | if (oid_str[i] == '.') { |
| 347 | num_dots++; |
| 348 | } |
| 349 | } |
| 350 | /* Allocate maximum possible required memory: |
| 351 | * There are (num_dots + 1) integer components, but the first 2 share the |
| 352 | * same subidentifier, so we only need num_dots subidentifiers maximum. */ |
| 353 | if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) { |
| 354 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 355 | } |
| 356 | /* Each byte can store 7 bits, calculate number of bytes for a |
| 357 | * subidentifier: |
| 358 | * |
| 359 | * bytes = ceil(subidentifer_size * 8 / 7) |
| 360 | */ |
| 361 | size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7) |
| 362 | + 1; |
| 363 | size_t max_possible_bytes = num_dots * bytes_per_subidentifier; |
| 364 | oid->p = mbedtls_calloc(max_possible_bytes, 1); |
| 365 | if (oid->p == NULL) { |
| 366 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 367 | } |
| 368 | unsigned char *out_ptr = oid->p; |
| 369 | unsigned char *out_bound = oid->p + max_possible_bytes; |
| 370 | |
| 371 | ret = oid_parse_number(&component1, &str_ptr, str_bound); |
| 372 | if (ret != 0) { |
| 373 | goto error; |
| 374 | } |
| 375 | if (component1 > 2) { |
| 376 | /* First component can't be > 2 */ |
| 377 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 378 | goto error; |
| 379 | } |
| 380 | if (str_ptr >= str_bound || *str_ptr != '.') { |
| 381 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 382 | goto error; |
| 383 | } |
| 384 | str_ptr++; |
| 385 | |
| 386 | ret = oid_parse_number(&component2, &str_ptr, str_bound); |
| 387 | if (ret != 0) { |
| 388 | goto error; |
| 389 | } |
| 390 | if ((component1 < 2) && (component2 > 39)) { |
| 391 | /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */ |
| 392 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 393 | goto error; |
| 394 | } |
| 395 | if (str_ptr < str_bound) { |
| 396 | if (*str_ptr == '.') { |
| 397 | str_ptr++; |
| 398 | } else { |
| 399 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 400 | goto error; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (component2 > (UINT_MAX - (component1 * 40))) { |
| 405 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 406 | goto error; |
| 407 | } |
| 408 | ret = oid_subidentifier_encode_into(&out_ptr, out_bound, |
| 409 | (component1 * 40) + component2); |
| 410 | if (ret != 0) { |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | while (str_ptr < str_bound) { |
| 415 | ret = oid_parse_number(&val, &str_ptr, str_bound); |
| 416 | if (ret != 0) { |
| 417 | goto error; |
| 418 | } |
| 419 | if (str_ptr < str_bound) { |
| 420 | if (*str_ptr == '.') { |
| 421 | str_ptr++; |
| 422 | } else { |
| 423 | ret = MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 424 | goto error; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val); |
| 429 | if (ret != 0) { |
| 430 | goto error; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | encoded_len = (size_t) (out_ptr - oid->p); |
| 435 | resized_mem = mbedtls_calloc(encoded_len, 1); |
| 436 | if (resized_mem == NULL) { |
| 437 | ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 438 | goto error; |
| 439 | } |
| 440 | memcpy(resized_mem, oid->p, encoded_len); |
| 441 | mbedtls_free(oid->p); |
| 442 | oid->p = resized_mem; |
| 443 | oid->len = encoded_len; |
| 444 | |
| 445 | oid->tag = MBEDTLS_ASN1_OID; |
| 446 | |
| 447 | return 0; |
| 448 | |
| 449 | error: |
| 450 | mbedtls_free(oid->p); |
| 451 | oid->p = NULL; |
| 452 | oid->len = 0; |
| 453 | return ret; |
| 454 | } |
| 455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | 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] | 457 | { |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 458 | int ret = MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 459 | int parse_ret = 0; |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 460 | const char *s = name, *c = s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | const char *end = s + strlen(s); |
Agathiyan Bragadeesh | ba386ec | 2023-08-16 11:31:17 +0100 | [diff] [blame] | 462 | mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | const x509_attr_descriptor_t *attr_descr = NULL; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 464 | int in_attr_type = 1; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 465 | int tag; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 466 | int numericoid = 0; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 467 | unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
| 468 | size_t data_len = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 469 | |
| 470 | /* Clear existing chain if present */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | mbedtls_asn1_free_named_data_list(head); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | while (c <= end) { |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 474 | if (in_attr_type && *c == '=') { |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 475 | if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) { |
| 476 | if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) { |
Agathiyan Bragadeesh | 1798487 | 2023-08-11 12:42:03 +0100 | [diff] [blame] | 477 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 478 | } else { |
| 479 | numericoid = 1; |
| 480 | } |
| 481 | } else { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 482 | oid.len = strlen(attr_descr->oid); |
| 483 | oid.p = mbedtls_calloc(1, oid.len); |
| 484 | memcpy(oid.p, attr_descr->oid, oid.len); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 485 | numericoid = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 489 | in_attr_type = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 492 | if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) { |
Agathiyan Bragadeesh | 457ac84 | 2023-08-23 11:35:26 +0100 | [diff] [blame] | 493 | if (s == c) { |
Agathiyan Bragadeesh | 4c7d7bf | 2023-08-23 11:28:30 +0100 | [diff] [blame] | 494 | mbedtls_free(oid.p); |
| 495 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 496 | } else if (*s == '#') { |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 497 | /* We know that c >= s (loop invariant) and c != s (in this |
| 498 | * else branch), hence c - s - 1 >= 0. */ |
| 499 | parse_ret = parse_attribute_value_hex_der_encoded( |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 500 | s + 1, (size_t) (c - s) - 1, |
Gilles Peskine | 7f420fa | 2023-09-21 18:13:17 +0200 | [diff] [blame] | 501 | data, sizeof(data), &data_len, &tag); |
Gilles Peskine | 7077781 | 2023-09-21 16:50:40 +0200 | [diff] [blame] | 502 | if (parse_ret != 0) { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 503 | mbedtls_free(oid.p); |
Gilles Peskine | 391dd7f | 2023-09-21 18:51:35 +0200 | [diff] [blame] | 504 | return parse_ret; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 505 | } |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 506 | } else { |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 507 | if (numericoid) { |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 508 | mbedtls_free(oid.p); |
| 509 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | 15df012 | 2023-08-22 17:50:00 +0100 | [diff] [blame] | 510 | } else { |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame] | 511 | if ((parse_ret = |
Agathiyan Bragadeesh | eb55867 | 2023-08-14 16:31:11 +0100 | [diff] [blame] | 512 | parse_attribute_value_string(s, (int) (c - s), data, |
| 513 | &data_len)) != 0) { |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 514 | mbedtls_free(oid.p); |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame] | 515 | return parse_ret; |
| 516 | } |
| 517 | tag = attr_descr->default_tag; |
| 518 | } |
| 519 | } |
Agathiyan Bragadeesh | 4606bf3 | 2023-08-22 17:29:18 +0100 | [diff] [blame] | 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | mbedtls_asn1_named_data *cur = |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 522 | mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | (unsigned char *) data, |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 524 | data_len); |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 525 | mbedtls_free(oid.p); |
| 526 | oid.p = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | if (cur == NULL) { |
| 528 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 531 | // set tagType |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 532 | cur->val.tag = tag; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | while (c < end && *(c + 1) == ' ') { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | c++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 537 | |
| 538 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 539 | in_attr_type = 1; |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 540 | |
| 541 | /* Successfully parsed one name, update ret to success */ |
| 542 | ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 543 | } |
| 544 | c++; |
| 545 | } |
Agathiyan Bragadeesh | 12b9d70 | 2023-08-15 17:42:33 +0100 | [diff] [blame] | 546 | if (oid.p != NULL) { |
| 547 | mbedtls_free(oid.p); |
Agathiyan Bragadeesh | 55d9319 | 2023-08-15 15:05:03 +0100 | [diff] [blame] | 548 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | /* 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] | 553 | * to store the critical boolean for us |
| 554 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, |
| 556 | int critical, const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 557 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 559 | |
Jonathan Winzig | 5caf20e | 2024-01-09 16:41:10 +0100 | [diff] [blame] | 560 | if (val_len > (SIZE_MAX - 1)) { |
Jonathan Winzig | 05c722b | 2024-01-09 15:20:03 +0100 | [diff] [blame] | 561 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 562 | } |
| 563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len, |
| 565 | NULL, val_len + 1)) == NULL) { |
| 566 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | cur->val.p[0] = critical; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | memcpy(cur->val.p + 1, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
| 576 | * RelativeDistinguishedName ::= |
| 577 | * SET OF AttributeTypeAndValue |
| 578 | * |
| 579 | * AttributeTypeAndValue ::= SEQUENCE { |
| 580 | * type AttributeType, |
| 581 | * value AttributeValue } |
| 582 | * |
| 583 | * AttributeType ::= OBJECT IDENTIFIER |
| 584 | * |
| 585 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 586 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | static int x509_write_name(unsigned char **p, |
| 588 | unsigned char *start, |
| 589 | mbedtls_asn1_named_data *cur_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 590 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 592 | size_t len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | const char *oid = (const char *) cur_name->oid.p; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 594 | size_t oid_len = cur_name->oid.len; |
| 595 | const unsigned char *name = cur_name->val.p; |
| 596 | size_t name_len = cur_name->val.len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 597 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 598 | // Write correct string tag and value |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start, |
| 600 | cur_name->val.tag, |
| 601 | (const char *) name, |
| 602 | name_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 603 | // Write OID |
| 604 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, |
| 606 | oid_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 609 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 610 | MBEDTLS_ASN1_CONSTRUCTED | |
| 611 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 614 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 615 | MBEDTLS_ASN1_CONSTRUCTED | |
| 616 | MBEDTLS_ASN1_SET)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 618 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | int mbedtls_x509_write_names(unsigned char **p, unsigned char *start, |
| 622 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 623 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 624 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 625 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | mbedtls_asn1_named_data *cur = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | while (cur != NULL) { |
| 629 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 630 | cur = cur->next; |
| 631 | } |
| 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 634 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 635 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 638 | } |
| 639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start, |
| 641 | const char *oid, size_t oid_len, |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 642 | unsigned char *sig, size_t size, |
| 643 | mbedtls_pk_type_t pk_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 644 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 645 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 646 | int write_null_par; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 647 | size_t len = 0; |
| 648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | if (*p < start || (size_t) (*p - start) < size) { |
| 650 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 651 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 652 | |
| 653 | len = size; |
| 654 | (*p) -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | memcpy(*p, sig, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | if (*p - start < 1) { |
| 658 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 659 | } |
Manuel Pégourié-Gonnard | 4dc9b39 | 2015-10-21 12:23:09 +0200 | [diff] [blame] | 660 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 661 | *--(*p) = 0; |
| 662 | len += 1; |
| 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 665 | 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] | 666 | |
| 667 | // Write OID |
| 668 | // |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 669 | if (pk_alg == MBEDTLS_PK_ECDSA) { |
| 670 | /* |
| 671 | * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature |
| 672 | * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and |
| 673 | * https://www.rfc-editor.org/rfc/rfc5758#section-3. |
| 674 | */ |
| 675 | write_null_par = 0; |
| 676 | } else { |
| 677 | write_null_par = 1; |
| 678 | } |
| 679 | MBEDTLS_ASN1_CHK_ADD(len, |
| 680 | mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, |
| 681 | 0, write_null_par)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | static int x509_write_extension(unsigned char **p, unsigned char *start, |
| 687 | mbedtls_asn1_named_data *ext) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 688 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 689 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 690 | size_t len = 0; |
| 691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1, |
| 693 | ext->val.len - 1)); |
| 694 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1)); |
| 695 | 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] | 696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | if (ext->val.p[0] != 0) { |
| 698 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p, |
| 702 | ext->oid.len)); |
| 703 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len)); |
| 704 | 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] | 705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 707 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 708 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Extension ::= SEQUENCE { |
| 715 | * extnID OBJECT IDENTIFIER, |
| 716 | * critical BOOLEAN DEFAULT FALSE, |
| 717 | * extnValue OCTET STRING |
| 718 | * -- contains the DER encoding of an ASN.1 value |
| 719 | * -- corresponding to the extension type identified |
| 720 | * -- by extnID |
| 721 | * } |
| 722 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 723 | int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start, |
| 724 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 725 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 726 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 727 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | mbedtls_asn1_named_data *cur_ext = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | while (cur_ext != NULL) { |
| 731 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 732 | cur_ext = cur_ext->next; |
| 733 | } |
| 734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | #endif /* MBEDTLS_X509_CREATE_C */ |