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 | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 133 | static char *x509_oid_from_numericoid(const char *numericoid, |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 134 | size_t numericoid_len) |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 135 | { |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 136 | char *oid; |
| 137 | mbedtls_asn1_buf *oid_buf = mbedtls_calloc(1, sizeof(mbedtls_asn1_buf)); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 138 | int ret; |
| 139 | |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 140 | ret = mbedtls_oid_from_numeric_string(oid_buf, numericoid, numericoid_len); |
| 141 | if (ret != 0) { |
| 142 | if(ret != MBEDTLS_ERR_ASN1_ALLOC_FAILED) { |
| 143 | mbedtls_free(oid_buf->p); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 144 | } |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 145 | mbedtls_free(oid_buf); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 148 | oid = calloc(1, oid_buf->len + 1); |
| 149 | memcpy(oid, oid_buf->p, oid_buf->len); |
| 150 | oid[oid_buf->len + 1] = '\0'; |
| 151 | mbedtls_free(oid_buf->p); |
| 152 | mbedtls_free(oid_buf); |
| 153 | return oid; |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 156 | static int hex_to_int(char c) |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 157 | { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 158 | return ('0' <= c && c <= '9') ? (c - '0') : |
| 159 | ('a' <= c && c <= 'f') ? (c - 'a' + 10) : |
| 160 | ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1; |
| 161 | } |
| 162 | |
| 163 | static int hexpair_to_int(char c1, char c2) |
| 164 | { |
| 165 | int n1 = hex_to_int(c1); |
| 166 | int n2 = hex_to_int(c2); |
| 167 | if (n1 != -1 && n2 != -1) { |
| 168 | return (n1 << 4) | n2; |
| 169 | } else { |
| 170 | return -1; |
| 171 | } |
| 172 | } |
| 173 | |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 174 | static int parse_attribute_value_string(const char *s, |
| 175 | int len, |
| 176 | unsigned char *data, |
| 177 | size_t *data_len) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 178 | { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 179 | const char *c = s; |
| 180 | const char *end = c + len; |
| 181 | int hexpair = 0; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 182 | unsigned char *d = data; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 183 | int n; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 184 | while (c < end) { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 185 | if (*c == '\\') { |
| 186 | c++; |
| 187 | |
| 188 | /* Check for valid escaped characters in RFC 4514 in Section 3*/ |
| 189 | if (c + 1 < end && (n = hexpair_to_int(*c, *(c+1))) != -1) { |
| 190 | hexpair = 1; |
| 191 | *(d++) = n; |
| 192 | c++; |
Agathiyan Bragadeesh | a7f9630 | 2023-08-10 16:03:27 +0100 | [diff] [blame] | 193 | } else if (c == end || !strchr(" ,=+<>#;\"\\", *c)) { |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 194 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 195 | } |
| 196 | } |
| 197 | if (!hexpair) { |
| 198 | *(d++) = *c; |
| 199 | } |
| 200 | if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) { |
| 201 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 202 | } |
| 203 | |
| 204 | hexpair = 0; |
| 205 | c++; |
| 206 | } |
| 207 | *data_len = d - data; |
| 208 | return 0; |
| 209 | } |
| 210 | |
Agathiyan Bragadeesh | 0eb6673 | 2023-07-31 16:10:07 +0100 | [diff] [blame] | 211 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 212 | static int parse_attribute_value_ber_encoded(const char *s, |
| 213 | int len, |
| 214 | unsigned char *data, |
| 215 | size_t *data_len, |
| 216 | int *tag) |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 217 | { |
| 218 | const char *c = s; |
| 219 | const char *end = c + len; |
Agathiyan Bragadeesh | e18a178 | 2023-08-10 14:12:28 +0100 | [diff] [blame] | 220 | unsigned char asn1_der_buf[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 221 | unsigned char *asn1_der_end; |
| 222 | unsigned char *p; |
Agathiyan Bragadeesh | e18a178 | 2023-08-10 14:12:28 +0100 | [diff] [blame] | 223 | unsigned char *d = data; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 224 | int n; |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 225 | /* Converting from hexstring to raw binary so we can use asn1parse.c*/ |
| 226 | if ((len < 5) || (*c != '#')) { |
| 227 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 228 | } |
| 229 | c++; |
| 230 | if ((*tag = hexpair_to_int(*c, *(c+1))) == -1) { |
| 231 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 232 | } |
| 233 | c += 2; |
| 234 | p = asn1_der_buf; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 235 | for (p = asn1_der_buf; c < end; c += 2) { |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 236 | if ((c + 1 >= end) || (n = hexpair_to_int(*c, *(c+1))) == -1) { |
| 237 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 238 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 239 | *(p++) = n; |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 240 | } |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 241 | asn1_der_end = p; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 242 | |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 243 | p = asn1_der_buf; |
| 244 | if (mbedtls_asn1_get_len(&p, asn1_der_end, data_len) != 0) { |
| 245 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 246 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 247 | |
Agathiyan Bragadeesh | e18a178 | 2023-08-10 14:12:28 +0100 | [diff] [blame] | 248 | while (p < asn1_der_end) { |
| 249 | *(d++) = *(p++); |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 250 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 251 | |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 252 | return 0; |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 253 | } |
Agathiyan Bragadeesh | 0eb6673 | 2023-07-31 16:10:07 +0100 | [diff] [blame] | 254 | #endif |
Agathiyan Bragadeesh | ef2decb | 2023-07-21 15:47:47 +0100 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | 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] | 257 | { |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 258 | int ret = MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 259 | int parse_ret = 0; |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 260 | const char *s = name, *c = s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | const char *end = s + strlen(s); |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 262 | char *oid = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | const x509_attr_descriptor_t *attr_descr = NULL; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 264 | int in_attr_type = 1; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 265 | int tag; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 266 | int numericoid = 0; |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 267 | unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE]; |
| 268 | size_t data_len = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 269 | |
| 270 | /* Clear existing chain if present */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | mbedtls_asn1_free_named_data_list(head); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | while (c <= end) { |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 274 | if (in_attr_type && *c == '=') { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) { |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 276 | if ((oid = x509_oid_from_numericoid(s, c - s)) == NULL) { |
Agathiyan Bragadeesh | 1798487 | 2023-08-11 12:42:03 +0100 | [diff] [blame] | 277 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 278 | } else { |
| 279 | numericoid = 1; |
| 280 | } |
| 281 | } else { |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 282 | oid = malloc(strlen(attr_descr->oid)); |
| 283 | strcpy(oid,attr_descr->oid); |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 284 | numericoid = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 288 | in_attr_type = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 291 | if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) { |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame^] | 292 | #if defined(MBEDTLS_ASN1_PARSE_C) |
| 293 | if ((parse_ret = |
| 294 | parse_attribute_value_ber_encoded(s, (int) (c - s), data, &data_len, |
| 295 | &tag)) != 0) { |
| 296 | if(numericoid) { |
| 297 | return MBEDTLS_ERR_X509_INVALID_NAME; |
| 298 | } |
| 299 | else { |
| 300 | if ((parse_ret = |
| 301 | parse_attribute_value_string(s, (int) (c - s), data, &data_len)) != 0) { |
| 302 | return parse_ret; |
| 303 | } |
| 304 | tag = attr_descr->default_tag; |
| 305 | } |
| 306 | } |
| 307 | #else |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 308 | if (!numericoid) { |
Agathiyan Bragadeesh | 97178f2 | 2023-08-07 12:19:43 +0100 | [diff] [blame] | 309 | if ((parse_ret = |
| 310 | parse_attribute_value_string(s, (int) (c - s), data, &data_len)) != 0) { |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 311 | return parse_ret; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 312 | } |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 313 | tag = attr_descr->default_tag; |
Paul Bakker | 8dcb2d7 | 2014-08-08 12:22:30 +0200 | [diff] [blame] | 314 | } |
Agathiyan Bragadeesh | b73778d | 2023-07-26 11:55:31 +0100 | [diff] [blame] | 315 | if (numericoid) { |
Agathiyan Bragadeesh | 4987c8f | 2023-08-01 11:10:52 +0100 | [diff] [blame] | 316 | return MBEDTLS_ERR_X509_INVALID_NAME; |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 317 | } |
Agathiyan Bragadeesh | 957ca05 | 2023-08-11 14:58:14 +0100 | [diff] [blame^] | 318 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | mbedtls_asn1_named_data *cur = |
| 320 | mbedtls_asn1_store_named_data(head, oid, strlen(oid), |
| 321 | (unsigned char *) data, |
Agathiyan Bragadeesh | e119f3c | 2023-07-24 17:21:14 +0100 | [diff] [blame] | 322 | data_len); |
Agathiyan Bragadeesh | f88bd5a | 2023-08-11 11:48:26 +0100 | [diff] [blame] | 323 | mbedtls_free(oid); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | if (cur == NULL) { |
| 325 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 328 | // set tagType |
Agathiyan Bragadeesh | 6cbfae5 | 2023-07-27 14:34:11 +0100 | [diff] [blame] | 329 | cur->val.tag = tag; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | while (c < end && *(c + 1) == ' ') { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 332 | c++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 334 | |
| 335 | s = c + 1; |
Agathiyan Bragadeesh | ed88eef | 2023-08-10 13:51:38 +0100 | [diff] [blame] | 336 | in_attr_type = 1; |
David Horstmann | 8fd98d6 | 2023-06-27 15:17:44 +0100 | [diff] [blame] | 337 | |
| 338 | /* Successfully parsed one name, update ret to success */ |
| 339 | ret = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 340 | } |
| 341 | c++; |
| 342 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | /* 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] | 347 | * to store the critical boolean for us |
| 348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len, |
| 350 | int critical, const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len, |
| 355 | NULL, val_len + 1)) == NULL) { |
| 356 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | cur->val.p[0] = critical; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | memcpy(cur->val.p + 1, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
| 366 | * RelativeDistinguishedName ::= |
| 367 | * SET OF AttributeTypeAndValue |
| 368 | * |
| 369 | * AttributeTypeAndValue ::= SEQUENCE { |
| 370 | * type AttributeType, |
| 371 | * value AttributeValue } |
| 372 | * |
| 373 | * AttributeType ::= OBJECT IDENTIFIER |
| 374 | * |
| 375 | * AttributeValue ::= ANY DEFINED BY AttributeType |
| 376 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | static int x509_write_name(unsigned char **p, |
| 378 | unsigned char *start, |
| 379 | mbedtls_asn1_named_data *cur_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 381 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 382 | size_t len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | const char *oid = (const char *) cur_name->oid.p; |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 384 | size_t oid_len = cur_name->oid.len; |
| 385 | const unsigned char *name = cur_name->val.p; |
| 386 | size_t name_len = cur_name->val.len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 387 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 388 | // Write correct string tag and value |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start, |
| 390 | cur_name->val.tag, |
| 391 | (const char *) name, |
| 392 | name_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | // Write OID |
| 394 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, |
| 396 | oid_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 399 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 400 | MBEDTLS_ASN1_CONSTRUCTED | |
| 401 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 404 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 405 | MBEDTLS_ASN1_CONSTRUCTED | |
| 406 | MBEDTLS_ASN1_SET)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | int mbedtls_x509_write_names(unsigned char **p, unsigned char *start, |
| 412 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 413 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 414 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 415 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | mbedtls_asn1_named_data *cur = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | while (cur != NULL) { |
| 419 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 420 | cur = cur->next; |
| 421 | } |
| 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 424 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 425 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start, |
| 431 | const char *oid, size_t oid_len, |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 432 | unsigned char *sig, size_t size, |
| 433 | mbedtls_pk_type_t pk_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 434 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 435 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 436 | int write_null_par; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 437 | size_t len = 0; |
| 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | if (*p < start || (size_t) (*p - start) < size) { |
| 440 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 441 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 442 | |
| 443 | len = size; |
| 444 | (*p) -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | memcpy(*p, sig, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | if (*p - start < 1) { |
| 448 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 449 | } |
Manuel Pégourié-Gonnard | 4dc9b39 | 2015-10-21 12:23:09 +0200 | [diff] [blame] | 450 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 451 | *--(*p) = 0; |
| 452 | len += 1; |
| 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 455 | 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] | 456 | |
| 457 | // Write OID |
| 458 | // |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 459 | if (pk_alg == MBEDTLS_PK_ECDSA) { |
| 460 | /* |
| 461 | * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature |
| 462 | * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and |
| 463 | * https://www.rfc-editor.org/rfc/rfc5758#section-3. |
| 464 | */ |
| 465 | write_null_par = 0; |
| 466 | } else { |
| 467 | write_null_par = 1; |
| 468 | } |
| 469 | MBEDTLS_ASN1_CHK_ADD(len, |
| 470 | mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, |
| 471 | 0, write_null_par)); |
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 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | static int x509_write_extension(unsigned char **p, unsigned char *start, |
| 477 | mbedtls_asn1_named_data *ext) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 478 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 479 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
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 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1, |
| 483 | ext->val.len - 1)); |
| 484 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1)); |
| 485 | 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] | 486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | if (ext->val.p[0] != 0) { |
| 488 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p, |
| 492 | ext->oid.len)); |
| 493 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len)); |
| 494 | 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] | 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 497 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED | |
| 498 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Extension ::= SEQUENCE { |
| 505 | * extnID OBJECT IDENTIFIER, |
| 506 | * critical BOOLEAN DEFAULT FALSE, |
| 507 | * extnValue OCTET STRING |
| 508 | * -- contains the DER encoding of an ASN.1 value |
| 509 | * -- corresponding to the extension type identified |
| 510 | * -- by extnID |
| 511 | * } |
| 512 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start, |
| 514 | mbedtls_asn1_named_data *first) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 515 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 516 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | mbedtls_asn1_named_data *cur_ext = first; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | while (cur_ext != NULL) { |
| 521 | MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 522 | cur_ext = cur_ext->next; |
| 523 | } |
| 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | #endif /* MBEDTLS_X509_CREATE_C */ |