Andrzej Kurek | c508dc2 | 2023-07-07 08:20:02 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * X.509 internal, common functions for writing |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 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. |
| 18 | */ |
| 19 | #include "common.h" |
| 20 | #if defined(MBEDTLS_X509_CSR_WRITE_C) || defined(MBEDTLS_X509_CRT_WRITE_C) |
| 21 | |
| 22 | #include "mbedtls/x509_crt.h" |
| 23 | #include "mbedtls/asn1write.h" |
| 24 | #include "mbedtls/error.h" |
| 25 | #include "mbedtls/oid.h" |
| 26 | #include "mbedtls/platform.h" |
| 27 | #include "mbedtls/platform_util.h" |
| 28 | #include "mbedtls/md.h" |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #if defined(MBEDTLS_PEM_WRITE_C) |
| 34 | #include "mbedtls/pem.h" |
| 35 | #endif /* MBEDTLS_PEM_WRITE_C */ |
| 36 | |
| 37 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 38 | #include "psa/crypto.h" |
| 39 | #include "mbedtls/psa_util.h" |
| 40 | #include "md_psa.h" |
| 41 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 42 | |
| 43 | #define CHECK_OVERFLOW_ADD(a, b) \ |
| 44 | do \ |
| 45 | { \ |
| 46 | if (a > SIZE_MAX - (b)) \ |
| 47 | { \ |
| 48 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; \ |
| 49 | } \ |
| 50 | a += b; \ |
| 51 | } while (0) |
| 52 | |
| 53 | int mbedtls_x509_write_set_san_common(mbedtls_asn1_named_data **extensions, |
| 54 | const mbedtls_x509_san_list *san_list) |
| 55 | { |
| 56 | int ret = 0; |
| 57 | const mbedtls_x509_san_list *cur; |
| 58 | unsigned char *buf; |
| 59 | unsigned char *p; |
| 60 | size_t len; |
| 61 | size_t buflen = 0; |
| 62 | |
| 63 | /* Determine the maximum size of the SubjectAltName list */ |
| 64 | for (cur = san_list; cur != NULL; cur = cur->next) { |
| 65 | /* Calculate size of the required buffer */ |
| 66 | switch (cur->node.type) { |
| 67 | case MBEDTLS_X509_SAN_DNS_NAME: |
| 68 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 69 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
| 70 | case MBEDTLS_X509_SAN_RFC822_NAME: |
| 71 | /* length of value for each name entry, |
| 72 | * maximum 4 bytes for the length field, |
| 73 | * 1 byte for the tag/type. |
| 74 | */ |
| 75 | CHECK_OVERFLOW_ADD(buflen, cur->node.san.unstructured_name.len); |
| 76 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); |
| 77 | break; |
| 78 | case MBEDTLS_X509_SAN_DIRECTORY_NAME: |
| 79 | { |
| 80 | const mbedtls_asn1_named_data *chunk = &cur->node.san.directory_name; |
| 81 | while (chunk != NULL) { |
| 82 | // Max 4 bytes for length, +1 for tag, |
| 83 | // additional 4 max for length, +1 for tag. |
| 84 | // See x509_write_name for more information. |
| 85 | CHECK_OVERFLOW_ADD(buflen, 4 + 1 + 4 + 1); |
| 86 | CHECK_OVERFLOW_ADD(buflen, chunk->oid.len); |
| 87 | CHECK_OVERFLOW_ADD(buflen, chunk->val.len); |
| 88 | chunk = chunk->next; |
| 89 | } |
| 90 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); |
| 91 | break; |
| 92 | } |
| 93 | default: |
| 94 | /* Not supported - return. */ |
| 95 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* Add the extra length field and tag */ |
| 100 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); |
| 101 | |
| 102 | /* Allocate buffer */ |
| 103 | buf = mbedtls_calloc(1, buflen); |
| 104 | if (buf == NULL) { |
| 105 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; |
| 106 | } |
| 107 | p = buf + buflen; |
| 108 | |
| 109 | /* Write ASN.1-based structure */ |
| 110 | cur = san_list; |
| 111 | len = 0; |
| 112 | while (cur != NULL) { |
| 113 | size_t single_san_len = 0; |
| 114 | switch (cur->node.type) { |
| 115 | case MBEDTLS_X509_SAN_DNS_NAME: |
| 116 | case MBEDTLS_X509_SAN_RFC822_NAME: |
| 117 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 118 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
| 119 | { |
| 120 | const unsigned char *unstructured_name = |
| 121 | (const unsigned char *) cur->node.san.unstructured_name.p; |
| 122 | size_t unstructured_name_len = cur->node.san.unstructured_name.len; |
| 123 | |
| 124 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, |
| 125 | mbedtls_asn1_write_raw_buffer( |
| 126 | &p, buf, |
| 127 | unstructured_name, unstructured_name_len)); |
| 128 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len( |
| 129 | &p, buf, unstructured_name_len)); |
| 130 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, |
| 131 | mbedtls_asn1_write_tag( |
| 132 | &p, buf, |
| 133 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type)); |
| 134 | } |
| 135 | break; |
| 136 | case MBEDTLS_X509_SAN_DIRECTORY_NAME: |
| 137 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, |
| 138 | mbedtls_x509_write_names(&p, buf, |
| 139 | (mbedtls_asn1_named_data *) & |
| 140 | cur->node |
| 141 | .san.directory_name)); |
| 142 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, |
| 143 | mbedtls_asn1_write_len(&p, buf, single_san_len)); |
| 144 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, |
| 145 | mbedtls_asn1_write_tag(&p, buf, |
| 146 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 147 | MBEDTLS_ASN1_CONSTRUCTED | |
| 148 | MBEDTLS_X509_SAN_DIRECTORY_NAME)); |
| 149 | break; |
| 150 | default: |
| 151 | /* Error out on an unsupported SAN */ |
| 152 | ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 153 | goto cleanup; |
| 154 | } |
| 155 | cur = cur->next; |
| 156 | /* check for overflow */ |
| 157 | if (len > SIZE_MAX - single_san_len) { |
| 158 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 159 | goto cleanup; |
| 160 | } |
| 161 | len += single_san_len; |
| 162 | } |
| 163 | |
| 164 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); |
| 165 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, |
| 166 | mbedtls_asn1_write_tag(&p, buf, |
| 167 | MBEDTLS_ASN1_CONSTRUCTED | |
| 168 | MBEDTLS_ASN1_SEQUENCE)); |
| 169 | |
| 170 | ret = mbedtls_x509_set_extension(extensions, |
| 171 | MBEDTLS_OID_SUBJECT_ALT_NAME, |
| 172 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME), |
| 173 | 0, |
| 174 | buf + buflen - len, len); |
| 175 | |
| 176 | /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list |
| 177 | * was incorrectly calculated and memory is corrupted. */ |
| 178 | if (p < buf) { |
| 179 | ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 180 | } |
| 181 | cleanup: |
| 182 | mbedtls_free(buf); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | #endif /* MBEDTLS_X509_CSR_WRITE_C || MBEDTLS_X509_CRT_WRITE_C */ |