Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # if defined(MBEDTLS_PLATFORM_C) |
| 29 | # include "mbedtls/platform.h" |
| 30 | # else |
| 31 | # include <stdlib.h> |
| 32 | # define mbedtls_calloc calloc |
| 33 | # define mbedtls_free free |
| 34 | # endif |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 36 | # include "mbedtls/ssl.h" |
| 37 | # include "ssl_misc.h" |
| 38 | # include "mbedtls/debug.h" |
| 39 | # include "mbedtls/error.h" |
| 40 | # include "mbedtls/platform_util.h" |
| 41 | # include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 43 | # include <string.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 45 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 46 | # include "mbedtls/psa_util.h" |
| 47 | # include "psa/crypto.h" |
| 48 | # endif |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 49 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 50 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 51 | # include "mbedtls/oid.h" |
| 52 | # endif |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 54 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 55 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 56 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 57 | /* Top-level Connection ID API */ |
| 58 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf, |
| 60 | size_t len, |
| 61 | int ignore_other_cid) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 62 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 63 | if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) |
| 64 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 65 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 66 | if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 67 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 68 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 72 | conf->cid_len = len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | return 0; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 76 | int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl, |
| 77 | int enable, |
| 78 | unsigned char const *own_cid, |
| 79 | size_t own_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 80 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 81 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
| 82 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 83 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 84 | ssl->negotiate_cid = enable; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 85 | if (enable == MBEDTLS_SSL_CID_DISABLED) { |
| 86 | MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension.")); |
| 87 | return 0; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 88 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 89 | MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension.")); |
| 90 | MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 91 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 92 | if (own_cid_len != ssl->conf->cid_len) { |
| 93 | MBEDTLS_SSL_DEBUG_MSG( |
| 94 | 3, ("CID length %u does not match CID length %u in config", |
| 95 | (unsigned)own_cid_len, (unsigned)ssl->conf->cid_len)); |
| 96 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 99 | memcpy(ssl->own_cid, own_cid, own_cid_len); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 100 | /* Truncation is not an issue here because |
| 101 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 102 | ssl->own_cid_len = (uint8_t)own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 103 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 107 | int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl, |
| 108 | int *enabled, |
| 109 | unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 110 | size_t *peer_cid_len) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 111 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 112 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 113 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 114 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 115 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 116 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 117 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 118 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 119 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 120 | * were used, but client and server requested the empty CID. |
| 121 | * This is indistinguishable from not using the CID extension |
| 122 | * in the first place. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 123 | if (ssl->transform_in->in_cid_len == 0 && |
| 124 | ssl->transform_in->out_cid_len == 0) { |
| 125 | return 0; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 128 | if (peer_cid_len != NULL) { |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 129 | *peer_cid_len = ssl->transform_in->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 130 | if (peer_cid != NULL) { |
| 131 | memcpy(peer_cid, ssl->transform_in->out_cid, |
| 132 | ssl->transform_in->out_cid_len); |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 135 | |
| 136 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 137 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 138 | return 0; |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 139 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 140 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 141 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 142 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 144 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Convert max_fragment_length codes to length. |
| 147 | * RFC 6066 says: |
| 148 | * enum{ |
| 149 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 150 | * } MaxFragmentLength; |
| 151 | * and we add 0 -> extension unused |
| 152 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 153 | static unsigned int ssl_mfl_code_to_length(int mfl) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 154 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 155 | switch (mfl) { |
| 156 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 157 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
| 158 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 159 | return 512; |
| 160 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 161 | return 1024; |
| 162 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 163 | return 2048; |
| 164 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 165 | return 4096; |
| 166 | default: |
| 167 | return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 168 | } |
| 169 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 170 | # endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 172 | int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst, |
| 173 | const mbedtls_ssl_session *src) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 174 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 175 | mbedtls_ssl_session_free(dst); |
| 176 | memcpy(dst, src, sizeof(mbedtls_ssl_session)); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 177 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 178 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 179 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 180 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 181 | if (src->peer_cert != NULL) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 182 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 183 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 184 | dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 185 | if (dst->peer_cert == NULL) |
| 186 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 187 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 188 | mbedtls_x509_crt_init(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 189 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 190 | if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, |
| 191 | src->peer_cert->raw.p, |
| 192 | src->peer_cert->raw.len)) != 0) { |
| 193 | mbedtls_free(dst->peer_cert); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 194 | dst->peer_cert = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 195 | return ret; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 198 | # else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 199 | if (src->peer_cert_digest != NULL) { |
| 200 | dst->peer_cert_digest = mbedtls_calloc(1, src->peer_cert_digest_len); |
| 201 | if (dst->peer_cert_digest == NULL) |
| 202 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 203 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 204 | memcpy(dst->peer_cert_digest, src->peer_cert_digest, |
| 205 | src->peer_cert_digest_len); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 206 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 207 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 208 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 209 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 211 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 212 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 213 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 214 | if (src->ticket != NULL) { |
| 215 | dst->ticket = mbedtls_calloc(1, src->ticket_len); |
| 216 | if (dst->ticket == NULL) |
| 217 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 218 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 219 | memcpy(dst->ticket, src->ticket, src->ticket_len); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 220 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 221 | # endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 222 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 223 | return 0; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 226 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 227 | static int |
| 228 | resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 229 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 230 | unsigned char *resized_buffer = mbedtls_calloc(1, len_new); |
| 231 | if (resized_buffer == NULL) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 232 | return -1; |
| 233 | |
| 234 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 235 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 236 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 237 | * lost, are done outside of this function. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 238 | memcpy(resized_buffer, *buffer, (len_new < *len_old) ? len_new : *len_old); |
| 239 | mbedtls_platform_zeroize(*buffer, *len_old); |
| 240 | mbedtls_free(*buffer); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 241 | |
| 242 | *buffer = resized_buffer; |
| 243 | *len_old = len_new; |
| 244 | |
| 245 | return 0; |
| 246 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 247 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | static void handle_buffer_resizing(mbedtls_ssl_context *ssl, |
| 249 | int downsizing, |
| 250 | size_t in_buf_new_len, |
| 251 | size_t out_buf_new_len) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 252 | { |
| 253 | int modified = 0; |
| 254 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 255 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 256 | if (ssl->in_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 257 | written_in = ssl->in_msg - ssl->in_buf; |
| 258 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 259 | len_offset_in = ssl->in_len - ssl->in_buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 260 | if (downsizing ? ssl->in_buf_len > in_buf_new_len && |
| 261 | ssl->in_left < in_buf_new_len : |
| 262 | ssl->in_buf_len < in_buf_new_len) { |
| 263 | if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != |
| 264 | 0) { |
| 265 | MBEDTLS_SSL_DEBUG_MSG( |
| 266 | 1, ("input buffer resizing failed - out of memory")); |
| 267 | } else { |
| 268 | MBEDTLS_SSL_DEBUG_MSG( |
| 269 | 2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 270 | in_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 271 | modified = 1; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 276 | if (ssl->out_buf != NULL) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 277 | written_out = ssl->out_msg - ssl->out_buf; |
| 278 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 279 | len_offset_out = ssl->out_len - ssl->out_buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 280 | if (downsizing ? ssl->out_buf_len > out_buf_new_len && |
| 281 | ssl->out_left < out_buf_new_len : |
| 282 | ssl->out_buf_len < out_buf_new_len) { |
| 283 | if (resize_buffer(&ssl->out_buf, out_buf_new_len, |
| 284 | &ssl->out_buf_len) != 0) { |
| 285 | MBEDTLS_SSL_DEBUG_MSG( |
| 286 | 1, ("output buffer resizing failed - out of memory")); |
| 287 | } else { |
| 288 | MBEDTLS_SSL_DEBUG_MSG( |
| 289 | 2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 290 | out_buf_new_len)); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 291 | modified = 1; |
| 292 | } |
| 293 | } |
| 294 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 295 | if (modified) { |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 296 | /* Update pointers here to avoid doing it twice. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 297 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 298 | /* Fields below might not be properly updated with record |
| 299 | * splitting or with CID, so they are manually updated here. */ |
| 300 | ssl->out_msg = ssl->out_buf + written_out; |
| 301 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 302 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 303 | |
| 304 | ssl->in_msg = ssl->in_buf + written_in; |
| 305 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 306 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 307 | } |
| 308 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 309 | # endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 310 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 311 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 312 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 314 | static psa_status_t |
| 315 | setup_psa_key_derivation(psa_key_derivation_operation_t *derivation, |
| 316 | psa_key_id_t key, |
| 317 | psa_algorithm_t alg, |
| 318 | const unsigned char *seed, |
| 319 | size_t seed_length, |
| 320 | const unsigned char *label, |
| 321 | size_t label_length, |
| 322 | size_t capacity) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 323 | { |
| 324 | psa_status_t status; |
| 325 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 326 | status = psa_key_derivation_setup(derivation, alg); |
| 327 | if (status != PSA_SUCCESS) |
| 328 | return status; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 330 | if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 331 | status = psa_key_derivation_input_bytes( |
| 332 | derivation, PSA_KEY_DERIVATION_INPUT_SEED, seed, seed_length); |
| 333 | if (status != PSA_SUCCESS) |
| 334 | return status; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 335 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 336 | if (mbedtls_svc_key_id_is_null(key)) { |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 337 | status = psa_key_derivation_input_bytes( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 338 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, NULL, 0); |
| 339 | } else { |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 340 | status = psa_key_derivation_input_key( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 341 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 342 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 343 | if (status != PSA_SUCCESS) |
| 344 | return status; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 345 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 346 | status = psa_key_derivation_input_bytes( |
| 347 | derivation, PSA_KEY_DERIVATION_INPUT_LABEL, label, label_length); |
| 348 | if (status != PSA_SUCCESS) |
| 349 | return status; |
| 350 | } else { |
| 351 | return PSA_ERROR_NOT_SUPPORTED; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 354 | status = psa_key_derivation_set_capacity(derivation, capacity); |
| 355 | if (status != PSA_SUCCESS) |
| 356 | return status; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 357 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 358 | return PSA_SUCCESS; |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 361 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 362 | const unsigned char *secret, |
| 363 | size_t slen, |
| 364 | const char *label, |
| 365 | const unsigned char *random, |
| 366 | size_t rlen, |
| 367 | unsigned char *dstbuf, |
| 368 | size_t dlen) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 369 | { |
| 370 | psa_status_t status; |
| 371 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 372 | psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 373 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 374 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 376 | if (md_type == MBEDTLS_MD_SHA384) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 377 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 378 | else |
| 379 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 380 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 381 | /* Normally a "secret" should be long enough to be impossible to |
| 382 | * find by brute force, and in particular should not be empty. But |
| 383 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 384 | * and for this use case it makes sense to have a 0-length "secret". |
| 385 | * Since the key API doesn't allow importing a key of length 0, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 386 | * keep master_key=0, which setup_psa_key_derivation() understands |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 387 | * to mean a 0-length "secret" input. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 388 | if (slen != 0) { |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 389 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 390 | psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 391 | psa_set_key_algorithm(&key_attributes, alg); |
| 392 | psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 393 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 394 | status = psa_import_key(&key_attributes, secret, slen, &master_key); |
| 395 | if (status != PSA_SUCCESS) |
| 396 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 397 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 398 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 399 | status = setup_psa_key_derivation(&derivation, master_key, alg, random, |
| 400 | rlen, (unsigned char const *)label, |
| 401 | (size_t)strlen(label), dlen); |
| 402 | if (status != PSA_SUCCESS) { |
| 403 | psa_key_derivation_abort(&derivation); |
| 404 | psa_destroy_key(master_key); |
| 405 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 408 | status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen); |
| 409 | if (status != PSA_SUCCESS) { |
| 410 | psa_key_derivation_abort(&derivation); |
| 411 | psa_destroy_key(master_key); |
| 412 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 415 | status = psa_key_derivation_abort(&derivation); |
| 416 | if (status != PSA_SUCCESS) { |
| 417 | psa_destroy_key(master_key); |
| 418 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 419 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 421 | if (!mbedtls_svc_key_id_is_null(master_key)) |
| 422 | status = psa_destroy_key(master_key); |
| 423 | if (status != PSA_SUCCESS) |
| 424 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 425 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 426 | return 0; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 429 | # else /* MBEDTLS_USE_PSA_CRYPTO */ |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 430 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 431 | static int tls_prf_generic(mbedtls_md_type_t md_type, |
| 432 | const unsigned char *secret, |
| 433 | size_t slen, |
| 434 | const char *label, |
| 435 | const unsigned char *random, |
| 436 | size_t rlen, |
| 437 | unsigned char *dstbuf, |
| 438 | size_t dlen) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 439 | { |
| 440 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 441 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 442 | unsigned char *tmp; |
| 443 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 445 | const mbedtls_md_info_t *md_info; |
| 446 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 447 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 449 | mbedtls_md_init(&md_ctx); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 450 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 451 | if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) |
| 452 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 453 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 454 | md_len = mbedtls_md_get_size(md_info); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 455 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 456 | tmp_len = md_len + strlen(label) + rlen; |
| 457 | tmp = mbedtls_calloc(1, tmp_len); |
| 458 | if (tmp == NULL) { |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 459 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 460 | goto exit; |
| 461 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 462 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 463 | nb = strlen(label); |
| 464 | memcpy(tmp + md_len, label, nb); |
| 465 | memcpy(tmp + md_len + nb, random, rlen); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 466 | nb += rlen; |
| 467 | |
| 468 | /* |
| 469 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 470 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 471 | if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 472 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 473 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 474 | mbedtls_md_hmac_starts(&md_ctx, secret, slen); |
| 475 | mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb); |
| 476 | mbedtls_md_hmac_finish(&md_ctx, tmp); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 477 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 478 | for (i = 0; i < dlen; i += md_len) { |
| 479 | mbedtls_md_hmac_reset(&md_ctx); |
| 480 | mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb); |
| 481 | mbedtls_md_hmac_finish(&md_ctx, h_i); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 482 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 483 | mbedtls_md_hmac_reset(&md_ctx); |
| 484 | mbedtls_md_hmac_update(&md_ctx, tmp, md_len); |
| 485 | mbedtls_md_hmac_finish(&md_ctx, tmp); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 486 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 487 | k = (i + md_len > dlen) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 488 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 489 | for (j = 0; j < k; j++) |
| 490 | dstbuf[i + j] = h_i[j]; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 493 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 494 | mbedtls_md_free(&md_ctx); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 495 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 496 | mbedtls_platform_zeroize(tmp, tmp_len); |
| 497 | mbedtls_platform_zeroize(h_i, sizeof(h_i)); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 498 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 499 | mbedtls_free(tmp); |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 500 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 501 | return ret; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 502 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 503 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 504 | # if defined(MBEDTLS_SHA256_C) |
| 505 | static int tls_prf_sha256(const unsigned char *secret, |
| 506 | size_t slen, |
| 507 | const char *label, |
| 508 | const unsigned char *random, |
| 509 | size_t rlen, |
| 510 | unsigned char *dstbuf, |
| 511 | size_t dlen) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 512 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 513 | return (tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen, label, random, |
| 514 | rlen, dstbuf, dlen)); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 515 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 516 | # endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 517 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 518 | # if defined(MBEDTLS_SHA384_C) |
| 519 | static int tls_prf_sha384(const unsigned char *secret, |
| 520 | size_t slen, |
| 521 | const char *label, |
| 522 | const unsigned char *random, |
| 523 | size_t rlen, |
| 524 | unsigned char *dstbuf, |
| 525 | size_t dlen) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 526 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 527 | return (tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen, label, random, |
| 528 | rlen, dstbuf, dlen)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 529 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 530 | # endif /* MBEDTLS_SHA384_C */ |
| 531 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 532 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 533 | static void |
| 534 | ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 535 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 536 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 537 | # if defined(MBEDTLS_SHA256_C) |
| 538 | static void ssl_update_checksum_sha256(mbedtls_ssl_context *, |
| 539 | const unsigned char *, |
| 540 | size_t); |
| 541 | static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, |
| 542 | unsigned char *, |
| 543 | size_t *); |
| 544 | static void |
| 545 | ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int); |
| 546 | # endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 547 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 548 | # if defined(MBEDTLS_SHA384_C) |
| 549 | static void ssl_update_checksum_sha384(mbedtls_ssl_context *, |
| 550 | const unsigned char *, |
| 551 | size_t); |
| 552 | static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, |
| 553 | unsigned char *, |
| 554 | size_t *); |
| 555 | static void |
| 556 | ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int); |
| 557 | # endif |
| 558 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 559 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 560 | # if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
| 561 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 562 | static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 563 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 564 | if (ssl->conf->f_psk != NULL) { |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 565 | /* If we've used a callback to select the PSK, |
| 566 | * the static configuration is irrelevant. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 567 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) |
| 568 | return 1; |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 569 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 570 | return 0; |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 573 | if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) |
| 574 | return 1; |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 575 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 576 | return 0; |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 577 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 578 | # endif /* MBEDTLS_USE_PSA_CRYPTO && \ |
| 579 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 580 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 581 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 582 | static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf) |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 583 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 584 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 585 | # if defined(MBEDTLS_SHA384_C) |
| 586 | if (tls_prf == tls_prf_sha384) { |
| 587 | return MBEDTLS_SSL_TLS_PRF_SHA384; |
| 588 | } else |
| 589 | # endif |
| 590 | # if defined(MBEDTLS_SHA256_C) |
| 591 | if (tls_prf == tls_prf_sha256) { |
| 592 | return MBEDTLS_SSL_TLS_PRF_SHA256; |
| 593 | } else |
| 594 | # endif |
| 595 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 596 | return MBEDTLS_SSL_TLS_PRF_NONE; |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 597 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 598 | # endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 599 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 600 | int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf, |
| 601 | const unsigned char *secret, |
| 602 | size_t slen, |
| 603 | const char *label, |
| 604 | const unsigned char *random, |
| 605 | size_t rlen, |
| 606 | unsigned char *dstbuf, |
| 607 | size_t dlen) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 608 | { |
| 609 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 610 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 611 | switch (prf) { |
| 612 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 613 | # if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 614 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 615 | tls_prf = tls_prf_sha384; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 616 | break; |
| 617 | # endif /* MBEDTLS_SHA384_C */ |
| 618 | # if defined(MBEDTLS_SHA256_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 619 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 620 | tls_prf = tls_prf_sha256; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 621 | break; |
| 622 | # endif /* MBEDTLS_SHA256_C */ |
| 623 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 624 | default: |
| 625 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 626 | } |
| 627 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 628 | return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen); |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 629 | } |
| 630 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 631 | /* Type for the TLS PRF */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 632 | typedef int ssl_tls_prf_t(const unsigned char *, |
| 633 | size_t, |
| 634 | const char *, |
| 635 | const unsigned char *, |
| 636 | size_t, |
| 637 | unsigned char *, |
| 638 | size_t); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 639 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 640 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 641 | * Populate a transform structure with session keys and all the other |
| 642 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 643 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 644 | * Parameters: |
| 645 | * - [in/out]: transform: structure to populate |
| 646 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 647 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 648 | * - [in] ciphersuite |
| 649 | * - [in] master |
| 650 | * - [in] encrypt_then_mac |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 651 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 652 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 653 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 654 | * - [in] minor_ver: SSL/TLS minor version |
| 655 | * - [in] endpoint: client or server |
| 656 | * - [in] ssl: optionally used for: |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 657 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 658 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 659 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 660 | static int ssl_populate_transform(mbedtls_ssl_transform *transform, |
| 661 | int ciphersuite, |
| 662 | const unsigned char master[48], |
| 663 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 664 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 665 | int encrypt_then_mac, |
| 666 | # endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 667 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 668 | ssl_tls_prf_t tls_prf, |
| 669 | const unsigned char randbytes[64], |
| 670 | int minor_ver, |
| 671 | unsigned endpoint, |
| 672 | const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 673 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 674 | int ret = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 675 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 676 | int psa_fallthrough; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 677 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 678 | unsigned char keyblk[256]; |
| 679 | unsigned char *key1; |
| 680 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 681 | unsigned char *mac_enc; |
| 682 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 683 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 684 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 685 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 686 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | const mbedtls_cipher_info_t *cipher_info; |
| 688 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 689 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 690 | # if !defined(MBEDTLS_SSL_EXPORT_KEYS) && !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 691 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 692 | (void)ssl; |
| 693 | # endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 694 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 695 | /* |
| 696 | * Some data just needs copying into the structure |
| 697 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 698 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 699 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 700 | transform->encrypt_then_mac = encrypt_then_mac; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 701 | # endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 702 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 703 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 704 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 705 | memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes)); |
| 706 | # endif |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 707 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 708 | /* |
| 709 | * Get various info structures |
| 710 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 711 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); |
| 712 | if (ciphersuite_info == NULL) { |
| 713 | MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found", |
| 714 | ciphersuite)); |
| 715 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 718 | cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher); |
| 719 | if (cipher_info == NULL) { |
| 720 | MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found", |
| 721 | ciphersuite_info->cipher)); |
| 722 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 725 | md_info = mbedtls_md_info_from_type(ciphersuite_info->mac); |
| 726 | if (md_info == NULL) { |
| 727 | MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found", |
| 728 | (unsigned)ciphersuite_info->mac)); |
| 729 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 732 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 733 | /* Copy own and peer's CID if the use of the CID |
| 734 | * extension has been negotiated. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 735 | if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) { |
| 736 | MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform")); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 737 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 738 | transform->in_cid_len = ssl->own_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 739 | memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len); |
| 740 | MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid, |
| 741 | transform->in_cid_len); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 742 | |
| 743 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 744 | memcpy(transform->out_cid, ssl->handshake->peer_cid, |
| 745 | ssl->handshake->peer_cid_len); |
| 746 | MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid, |
| 747 | transform->out_cid_len); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 748 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 749 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 750 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 751 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 752 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 753 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 754 | ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256); |
| 755 | if (ret != 0) { |
| 756 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 757 | return ret; |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 758 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 759 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 760 | MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s", |
| 761 | mbedtls_ssl_get_ciphersuite_name(ciphersuite))); |
| 762 | MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48); |
| 763 | MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64); |
| 764 | MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 765 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 766 | /* |
| 767 | * Determine the appropriate key, IV and MAC length. |
| 768 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 769 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 770 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 771 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 772 | # if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || \ |
| 773 | defined(MBEDTLS_CHACHAPOLY_C) |
| 774 | if (cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 775 | cipher_info->mode == MBEDTLS_MODE_CCM || |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 776 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 777 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 778 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 779 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 780 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 781 | transform->taglen = |
| 782 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 783 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 784 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 785 | * with mode and version: |
| 786 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 787 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 788 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 789 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 790 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 791 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 792 | transform->ivlen = 12; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 793 | # if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 794 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 795 | transform->fixed_ivlen = 12; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 796 | } else |
| 797 | # endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 798 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 799 | if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 800 | transform->fixed_ivlen = 12; |
| 801 | else |
| 802 | transform->fixed_ivlen = 4; |
| 803 | } |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 804 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 805 | /* Minimum length of encrypted record */ |
| 806 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 807 | transform->minlen = explicit_ivlen + transform->taglen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 808 | } else |
| 809 | # endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 810 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 811 | if (cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 812 | cipher_info->mode == MBEDTLS_MODE_CBC) { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 813 | /* Initialize HMAC contexts */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 814 | if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 || |
| 815 | (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) { |
| 816 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 817 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 818 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 819 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 820 | /* Get MAC length */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 821 | mac_key_len = mbedtls_md_get_size(md_info); |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 822 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 823 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 824 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 825 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 826 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 827 | /* Minimum length */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 828 | if (cipher_info->mode == MBEDTLS_MODE_STREAM) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 829 | transform->minlen = transform->maclen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 830 | else { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 831 | /* |
| 832 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 833 | * 1. if EtM is in use: one block plus MAC |
| 834 | * otherwise: * first multiple of blocklen greater than maclen |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 835 | * 2. IV |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 836 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 837 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 838 | if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) { |
| 839 | transform->minlen = transform->maclen + cipher_info->block_size; |
| 840 | } else |
| 841 | # endif |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 842 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 843 | transform->minlen = transform->maclen + |
| 844 | cipher_info->block_size - |
| 845 | transform->maclen % cipher_info->block_size; |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 846 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 847 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 848 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 849 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 850 | transform->minlen += transform->ivlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 851 | } else |
| 852 | # endif |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 853 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 854 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 855 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 856 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 857 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 858 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 859 | } else |
| 860 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 861 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 862 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 863 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 864 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 865 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 866 | MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 867 | (unsigned)keylen, (unsigned)transform->minlen, |
| 868 | (unsigned)transform->ivlen, |
| 869 | (unsigned)transform->maclen)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 873 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 874 | # if defined(MBEDTLS_SSL_CLI_C) |
| 875 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 876 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 877 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 878 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 879 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 880 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 881 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 882 | /* |
| 883 | * This is not used in TLS v1.1. |
| 884 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 885 | iv_copy_len = (transform->fixed_ivlen) ? transform->fixed_ivlen : |
| 886 | transform->ivlen; |
| 887 | memcpy(transform->iv_enc, key2 + keylen, iv_copy_len); |
| 888 | memcpy(transform->iv_dec, key2 + keylen + iv_copy_len, iv_copy_len); |
| 889 | } else |
| 890 | # endif /* MBEDTLS_SSL_CLI_C */ |
| 891 | # if defined(MBEDTLS_SSL_SRV_C) |
| 892 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 893 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 894 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 895 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 896 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 897 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 898 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 899 | /* |
| 900 | * This is not used in TLS v1.1. |
| 901 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 902 | iv_copy_len = (transform->fixed_ivlen) ? transform->fixed_ivlen : |
| 903 | transform->ivlen; |
| 904 | memcpy(transform->iv_dec, key1 + keylen, iv_copy_len); |
| 905 | memcpy(transform->iv_enc, key1 + keylen + iv_copy_len, iv_copy_len); |
| 906 | } else |
| 907 | # endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 908 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 909 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 910 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 911 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 912 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 913 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 914 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 915 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 916 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 917 | For AEAD-based ciphersuites, there is nothing to do here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 918 | if (mac_key_len != 0) { |
| 919 | mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len); |
| 920 | mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 921 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 922 | # endif |
| 923 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 924 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 925 | ((void)mac_dec); |
| 926 | ((void)mac_enc); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 927 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 928 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 929 | if (ssl->f_export_keys != NULL) { |
| 930 | ssl->f_export_keys(ssl->p_export_keys, |
| 931 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, master, |
| 932 | 48, randbytes + 32, randbytes, |
| 933 | tls_prf_get_type(tls_prf)); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 934 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 935 | # endif |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 936 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 937 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 938 | |
| 939 | /* Only use PSA-based ciphers for TLS-1.2. |
| 940 | * That's relevant at least for TLS-1.0, where |
| 941 | * we assume that mbedtls_cipher_crypt() updates |
| 942 | * the structure field for the IV, which the PSA-based |
| 943 | * implementation currently doesn't. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 944 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 945 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) { |
| 946 | ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc, cipher_info, |
| 947 | transform->taglen); |
| 948 | if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) { |
| 949 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 950 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 953 | if (ret == 0) { |
| 954 | MBEDTLS_SSL_DEBUG_MSG( |
| 955 | 3, ("Successfully setup PSA-based encryption cipher context")); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 956 | psa_fallthrough = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 957 | } else { |
| 958 | MBEDTLS_SSL_DEBUG_MSG( |
| 959 | 1, |
| 960 | ("Failed to setup PSA-based cipher context for record encryption - fall through to default setup.")); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 961 | psa_fallthrough = 1; |
| 962 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 963 | } else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 964 | psa_fallthrough = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 965 | # else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 966 | psa_fallthrough = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 967 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 968 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 969 | if (psa_fallthrough == 1) |
| 970 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 971 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc, |
| 972 | cipher_info)) != 0) { |
| 973 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 974 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 977 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 978 | /* Only use PSA-based ciphers for TLS-1.2. |
| 979 | * That's relevant at least for TLS-1.0, where |
| 980 | * we assume that mbedtls_cipher_crypt() updates |
| 981 | * the structure field for the IV, which the PSA-based |
| 982 | * implementation currently doesn't. */ |
| 983 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 984 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) { |
| 985 | ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec, cipher_info, |
| 986 | transform->taglen); |
| 987 | if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) { |
| 988 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret); |
| 989 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 990 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 991 | |
| 992 | if (ret == 0) { |
| 993 | MBEDTLS_SSL_DEBUG_MSG( |
| 994 | 3, ("Successfully setup PSA-based decryption cipher context")); |
| 995 | psa_fallthrough = 0; |
| 996 | } else { |
| 997 | MBEDTLS_SSL_DEBUG_MSG( |
| 998 | 1, |
| 999 | ("Failed to setup PSA-based cipher context for record decryption - fall through to default setup.")); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1000 | psa_fallthrough = 1; |
| 1001 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1002 | } else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1003 | psa_fallthrough = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1004 | # else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1005 | psa_fallthrough = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1006 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1007 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1008 | if (psa_fallthrough == 1) |
| 1009 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1010 | if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec, |
| 1011 | cipher_info)) != 0) { |
| 1012 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1013 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1014 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1016 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1, |
| 1017 | cipher_info->key_bitlen, |
| 1018 | MBEDTLS_ENCRYPT)) != 0) { |
| 1019 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
| 1020 | goto end; |
| 1021 | } |
| 1022 | |
| 1023 | if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2, |
| 1024 | cipher_info->key_bitlen, |
| 1025 | MBEDTLS_DECRYPT)) != 0) { |
| 1026 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret); |
| 1027 | goto end; |
| 1028 | } |
| 1029 | |
| 1030 | # if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1031 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1032 | if ((ret = mbedtls_cipher_set_padding_mode( |
| 1033 | &transform->cipher_ctx_enc, MBEDTLS_PADDING_NONE)) != 0) { |
| 1034 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
| 1035 | goto end; |
| 1036 | } |
| 1037 | |
| 1038 | if ((ret = mbedtls_cipher_set_padding_mode( |
| 1039 | &transform->cipher_ctx_dec, MBEDTLS_PADDING_NONE)) != 0) { |
| 1040 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1041 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1042 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1043 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1044 | # endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1045 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1046 | end: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1047 | mbedtls_platform_zeroize(keyblk, sizeof(keyblk)); |
| 1048 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1051 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1052 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1053 | * |
| 1054 | * Inputs: |
| 1055 | * - SSL/TLS minor version |
| 1056 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1057 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1058 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1059 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1060 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1061 | static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake, |
| 1062 | int minor_ver, |
| 1063 | mbedtls_md_type_t hash) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1064 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1065 | # if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA384_C) |
| 1066 | (void)hash; |
| 1067 | # endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1068 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1069 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1070 | # if defined(MBEDTLS_SHA384_C) |
| 1071 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && hash == MBEDTLS_MD_SHA384) { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1072 | handshake->tls_prf = tls_prf_sha384; |
| 1073 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1074 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1075 | } else |
| 1076 | # endif |
| 1077 | # if defined(MBEDTLS_SHA256_C) |
| 1078 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1079 | handshake->tls_prf = tls_prf_sha256; |
| 1080 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1081 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1082 | } else |
| 1083 | # endif |
| 1084 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1085 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1086 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1089 | return 0; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1092 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1093 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1094 | * |
| 1095 | * Parameters: |
| 1096 | * [in/out] handshake |
| 1097 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1098 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1099 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1100 | * [out] master |
| 1101 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1102 | * debug: conf->f_dbg, conf->p_dbg |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1103 | * EMS: passed to calc_verify (debug + session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1104 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1105 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1106 | static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake, |
| 1107 | unsigned char *master, |
| 1108 | const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1109 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1110 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1111 | |
| 1112 | /* cf. RFC 5246, Section 8.1: |
| 1113 | * "The master secret is always exactly 48 bytes in length." */ |
| 1114 | size_t const master_secret_len = 48; |
| 1115 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1116 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1117 | unsigned char session_hash[48]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1118 | # endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1119 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1120 | /* The label for the KDF used for key expansion. |
| 1121 | * This is either "master secret" or "extended master secret" |
| 1122 | * depending on whether the Extended Master Secret extension |
| 1123 | * is used. */ |
| 1124 | char const *lbl = "master secret"; |
| 1125 | |
| 1126 | /* The salt for the KDF used for key expansion. |
| 1127 | * - If the Extended Master Secret extension is not used, |
| 1128 | * this is ClientHello.Random + ServerHello.Random |
| 1129 | * (see Sect. 8.1 in RFC 5246). |
| 1130 | * - If the Extended Master Secret extension is used, |
| 1131 | * this is the transcript of the handshake so far. |
| 1132 | * (see Sect. 4 in RFC 7627). */ |
| 1133 | unsigned char const *salt = handshake->randbytes; |
| 1134 | size_t salt_len = 64; |
| 1135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1136 | # if !defined(MBEDTLS_DEBUG_C) && \ |
| 1137 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1138 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1139 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1140 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1141 | (void)ssl; |
| 1142 | # endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1144 | if (handshake->resume != 0) { |
| 1145 | MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)")); |
| 1146 | return 0; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1147 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1148 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1149 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1150 | if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) { |
| 1151 | lbl = "extended master secret"; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1152 | salt = session_hash; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1153 | handshake->calc_verify(ssl, session_hash, &salt_len); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1155 | MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret", |
| 1156 | session_hash, salt_len); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1157 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1158 | # endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1160 | # if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1161 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1162 | if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1163 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1164 | ssl_use_opaque_psk(ssl) == 1) { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1165 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1166 | psa_status_t status; |
| 1167 | psa_algorithm_t alg; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1168 | psa_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1169 | psa_key_derivation_operation_t derivation = |
| 1170 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1171 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1173 | MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion")); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1174 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1175 | psk = mbedtls_ssl_get_opaque_psk(ssl); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1176 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1177 | if (hash_alg == MBEDTLS_MD_SHA384) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1178 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1179 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1180 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1181 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1182 | status = setup_psa_key_derivation(&derivation, psk, alg, salt, salt_len, |
| 1183 | (unsigned char const *)lbl, |
| 1184 | (size_t)strlen(lbl), |
| 1185 | master_secret_len); |
| 1186 | if (status != PSA_SUCCESS) { |
| 1187 | psa_key_derivation_abort(&derivation); |
| 1188 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1189 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1190 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1191 | status = psa_key_derivation_output_bytes(&derivation, master, |
| 1192 | master_secret_len); |
| 1193 | if (status != PSA_SUCCESS) { |
| 1194 | psa_key_derivation_abort(&derivation); |
| 1195 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1198 | status = psa_key_derivation_abort(&derivation); |
| 1199 | if (status != PSA_SUCCESS) |
| 1200 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1201 | } else |
| 1202 | # endif |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1203 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1204 | ret = handshake->tls_prf(handshake->premaster, handshake->pmslen, lbl, |
| 1205 | salt, salt_len, master, master_secret_len); |
| 1206 | if (ret != 0) { |
| 1207 | MBEDTLS_SSL_DEBUG_RET(1, "prf", ret); |
| 1208 | return ret; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1211 | MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret", handshake->premaster, |
| 1212 | handshake->pmslen); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1213 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1214 | mbedtls_platform_zeroize(handshake->premaster, |
| 1215 | sizeof(handshake->premaster)); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1218 | return 0; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1219 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1221 | int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1222 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1223 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1224 | const mbedtls_ssl_ciphersuite_t *const ciphersuite_info = |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1225 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1226 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1227 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys")); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1228 | |
| 1229 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1230 | ret = ssl_set_handshake_prfs(ssl->handshake, ssl->minor_ver, |
| 1231 | ciphersuite_info->mac); |
| 1232 | if (ret != 0) { |
| 1233 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret); |
| 1234 | return ret; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1235 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1236 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1237 | /* Compute master secret if needed */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1238 | ret = |
| 1239 | ssl_compute_master(ssl->handshake, ssl->session_negotiate->master, ssl); |
| 1240 | if (ret != 0) { |
| 1241 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret); |
| 1242 | return ret; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1245 | /* Swap the client and server random values: |
| 1246 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1247 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1248 | { |
| 1249 | unsigned char tmp[64]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1250 | memcpy(tmp, ssl->handshake->randbytes, 64); |
| 1251 | memcpy(ssl->handshake->randbytes, tmp + 32, 32); |
| 1252 | memcpy(ssl->handshake->randbytes + 32, tmp, 32); |
| 1253 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* Populate transform structure */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1257 | ret = ssl_populate_transform(ssl->transform_negotiate, |
| 1258 | ssl->session_negotiate->ciphersuite, |
| 1259 | ssl->session_negotiate->master, |
| 1260 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1261 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1262 | ssl->session_negotiate->encrypt_then_mac, |
| 1263 | # endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 1264 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1265 | ssl->handshake->tls_prf, |
| 1266 | ssl->handshake->randbytes, ssl->minor_ver, |
| 1267 | ssl->conf->endpoint, ssl); |
| 1268 | if (ret != 0) { |
| 1269 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret); |
| 1270 | return ret; |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /* We no longer need Server/ClientHello.random values */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1274 | mbedtls_platform_zeroize(ssl->handshake->randbytes, |
| 1275 | sizeof(ssl->handshake->randbytes)); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1276 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1277 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys")); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1279 | return 0; |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1282 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1283 | # if defined(MBEDTLS_SHA256_C) |
| 1284 | void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl, |
| 1285 | unsigned char *hash, |
| 1286 | size_t *hlen) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1287 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1288 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1289 | size_t hash_size; |
| 1290 | psa_status_t status; |
| 1291 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1292 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1293 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256")); |
| 1294 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 1295 | if (status != PSA_SUCCESS) { |
| 1296 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1300 | status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size); |
| 1301 | if (status != PSA_SUCCESS) { |
| 1302 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1303 | return; |
| 1304 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1305 | |
| 1306 | *hlen = 32; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1307 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 1308 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 1309 | # else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1310 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1311 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1312 | mbedtls_sha256_init(&sha256); |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1314 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256")); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1315 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1316 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
| 1317 | mbedtls_sha256_finish(&sha256, hash); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1318 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1319 | *hlen = 32; |
| 1320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1321 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 1322 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1323 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1324 | mbedtls_sha256_free(&sha256); |
| 1325 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1326 | return; |
| 1327 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1328 | # endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1330 | # if defined(MBEDTLS_SHA384_C) |
| 1331 | void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl, |
| 1332 | unsigned char *hash, |
| 1333 | size_t *hlen) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1334 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1335 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1336 | size_t hash_size; |
| 1337 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1338 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1339 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1340 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384")); |
| 1341 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 1342 | if (status != PSA_SUCCESS) { |
| 1343 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1344 | return; |
| 1345 | } |
| 1346 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1347 | status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size); |
| 1348 | if (status != PSA_SUCCESS) { |
| 1349 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1350 | return; |
| 1351 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1352 | |
| 1353 | *hlen = 48; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1354 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen); |
| 1355 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify")); |
| 1356 | # else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1357 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1358 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1359 | mbedtls_sha512_init(&sha512); |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1361 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384")); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1362 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1363 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512); |
| 1364 | mbedtls_sha512_finish(&sha512, hash); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1365 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1366 | *hlen = 48; |
| 1367 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1368 | MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen); |
| 1369 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1370 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1371 | mbedtls_sha512_free(&sha512); |
| 1372 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | return; |
| 1374 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1375 | # endif /* MBEDTLS_SHA384_C */ |
| 1376 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1377 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1378 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 1379 | int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, |
| 1380 | mbedtls_key_exchange_type_t key_ex) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1381 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1382 | unsigned char *p = ssl->handshake->premaster; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1383 | unsigned char *end = p + sizeof(ssl->handshake->premaster); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1384 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1385 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1386 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1387 | if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len) == |
| 1388 | MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * This should never happen because the existence of a PSK is always |
| 1391 | * checked before calling this function |
| 1392 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1393 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1394 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1395 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1396 | |
| 1397 | /* |
| 1398 | * PMS = struct { |
| 1399 | * opaque other_secret<0..2^16-1>; |
| 1400 | * opaque psk<0..2^16-1>; |
| 1401 | * }; |
| 1402 | * with "other_secret" depending on the particular key exchange |
| 1403 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1404 | # if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1405 | if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) { |
| 1406 | if (end - p < 2) |
| 1407 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1408 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1409 | *(p++) = (unsigned char)(psk_len >> 8); |
| 1410 | *(p++) = (unsigned char)(psk_len); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1411 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1412 | if (end < p || (size_t)(end - p) < psk_len) |
| 1413 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1414 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1415 | memset(p, 0, psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1416 | p += psk_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1417 | } else |
| 1418 | # endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1419 | # if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1420 | if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) { |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1421 | /* |
| 1422 | * other_secret already set by the ClientKeyExchange message, |
| 1423 | * and is 48 bytes long |
| 1424 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1425 | if (end - p < 2) |
| 1426 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1427 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1428 | *p++ = 0; |
| 1429 | *p++ = 48; |
| 1430 | p += 48; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1431 | } else |
| 1432 | # endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1433 | # if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1434 | if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1435 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1436 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1437 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1438 | /* Write length only when we know the actual value */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1439 | if ((ret = mbedtls_dhm_calc_secret( |
| 1440 | &ssl->handshake->dhm_ctx, p + 2, end - (p + 2), &len, |
| 1441 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 1442 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret); |
| 1443 | return ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1444 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1445 | *(p++) = (unsigned char)(len >> 8); |
| 1446 | *(p++) = (unsigned char)(len); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1447 | p += len; |
| 1448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1449 | MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K); |
| 1450 | } else |
| 1451 | # endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1452 | # if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1453 | if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1455 | size_t zlen; |
| 1456 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1457 | if ((ret = mbedtls_ecdh_calc_secret( |
| 1458 | &ssl->handshake->ecdh_ctx, &zlen, p + 2, end - (p + 2), |
| 1459 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 1460 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret); |
| 1461 | return ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1464 | *(p++) = (unsigned char)(zlen >> 8); |
| 1465 | *(p++) = (unsigned char)(zlen); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1466 | p += zlen; |
| 1467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1468 | MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx, |
| 1469 | MBEDTLS_DEBUG_ECDH_Z); |
| 1470 | } else |
| 1471 | # endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1472 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1473 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1474 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | /* opaque psk<0..2^16-1>; */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1478 | if (end - p < 2) |
| 1479 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1480 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1481 | *(p++) = (unsigned char)(psk_len >> 8); |
| 1482 | *(p++) = (unsigned char)(psk_len); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1483 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1484 | if (end < p || (size_t)(end - p) < psk_len) |
| 1485 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1486 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1487 | memcpy(p, psk, psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1488 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1489 | |
| 1490 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1491 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1492 | return 0; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1493 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1494 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1495 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1496 | # if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1497 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1498 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1499 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1500 | int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1501 | { |
| 1502 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1503 | * timeout if we were using the usual handshake doubling scheme */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1504 | if (ssl->conf->renego_max_records < 0) { |
| 1505 | uint32_t ratio = |
| 1506 | ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1507 | unsigned char doublings = 1; |
| 1508 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1509 | while (ratio != 0) { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1510 | ++doublings; |
| 1511 | ratio >>= 1; |
| 1512 | } |
| 1513 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1514 | if (++ssl->renego_records_seen > doublings) { |
| 1515 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 1516 | ("no longer retransmitting hello request")); |
| 1517 | return 0; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1521 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1522 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1523 | # endif |
| 1524 | # endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1525 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1526 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1527 | static void ssl_clear_peer_cert(mbedtls_ssl_session *session) |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1528 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1529 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1530 | if (session->peer_cert != NULL) { |
| 1531 | mbedtls_x509_crt_free(session->peer_cert); |
| 1532 | mbedtls_free(session->peer_cert); |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1533 | session->peer_cert = NULL; |
| 1534 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1535 | # else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1536 | if (session->peer_cert_digest != NULL) { |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1537 | /* Zeroization is not necessary. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1538 | mbedtls_free(session->peer_cert_digest); |
| 1539 | session->peer_cert_digest = NULL; |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1540 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1541 | session->peer_cert_digest_len = 0; |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1542 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1543 | # endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1544 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1545 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 1546 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1547 | /* |
| 1548 | * Handshake functions |
| 1549 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1550 | # if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1551 | /* No certificate support -> dummy functions */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1552 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1553 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1554 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1555 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1556 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1557 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1558 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1559 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 1560 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1561 | ssl->state++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1562 | return 0; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1565 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1566 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1569 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1570 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1571 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1572 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1573 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1574 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1575 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1576 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 1577 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1578 | ssl->state++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1579 | return 0; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1582 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1583 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1584 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1585 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1586 | # else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1587 | /* Some certificate support -> implement write and parse */ |
| 1588 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1589 | int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1590 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1591 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1592 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1594 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1595 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1596 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1597 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate")); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1598 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1599 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) { |
| 1600 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 1601 | ssl->state++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1602 | return 0; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1605 | # if defined(MBEDTLS_SSL_CLI_C) |
| 1606 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
| 1607 | if (ssl->client_auth == 0) { |
| 1608 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1609 | ssl->state++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1610 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1611 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1612 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1613 | # endif /* MBEDTLS_SSL_CLI_C */ |
| 1614 | # if defined(MBEDTLS_SSL_SRV_C) |
| 1615 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1616 | if (mbedtls_ssl_own_cert(ssl) == NULL) { |
Hanno Becker | 9cfe6e9 | 2021-04-30 05:38:24 +0100 | [diff] [blame] | 1617 | /* Should never happen because we shouldn't have picked the |
| 1618 | * ciphersuite if we don't have a certificate. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1619 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1622 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1623 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1624 | MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1625 | |
| 1626 | /* |
| 1627 | * 0 . 0 handshake type |
| 1628 | * 1 . 3 handshake length |
| 1629 | * 4 . 6 length of all certs |
| 1630 | * 7 . 9 length of cert. 1 |
| 1631 | * 10 . n-1 peer certificate |
| 1632 | * n . n+2 length of cert. 2 |
| 1633 | * n+3 . ... upper level cert, etc. |
| 1634 | */ |
| 1635 | i = 7; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1636 | crt = mbedtls_ssl_own_cert(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1637 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1638 | while (crt != NULL) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1639 | n = crt->raw.len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1640 | if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) { |
| 1641 | MBEDTLS_SSL_DEBUG_MSG( |
| 1642 | 1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 1643 | " > %" MBEDTLS_PRINTF_SIZET, |
| 1644 | i + 3 + n, (size_t)MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 1645 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1648 | ssl->out_msg[i] = (unsigned char)(n >> 16); |
| 1649 | ssl->out_msg[i + 1] = (unsigned char)(n >> 8); |
| 1650 | ssl->out_msg[i + 2] = (unsigned char)(n); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1651 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1652 | i += 3; |
| 1653 | memcpy(ssl->out_msg + i, crt->raw.p, n); |
| 1654 | i += n; |
| 1655 | crt = crt->next; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1658 | ssl->out_msg[4] = (unsigned char)((i - 7) >> 16); |
| 1659 | ssl->out_msg[5] = (unsigned char)((i - 7) >> 8); |
| 1660 | ssl->out_msg[6] = (unsigned char)((i - 7)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1661 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1662 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1663 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1664 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1665 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1666 | ssl->state++; |
| 1667 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1668 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 1669 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 1670 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1673 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1674 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1675 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1678 | # if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1679 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1680 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1681 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 1682 | unsigned char *crt_buf, |
| 1683 | size_t crt_buf_len) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1684 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1685 | mbedtls_x509_crt const *const peer_crt = ssl->session->peer_cert; |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1686 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1687 | if (peer_crt == NULL) |
| 1688 | return -1; |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1689 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1690 | if (peer_crt->raw.len != crt_buf_len) |
| 1691 | return -1; |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1692 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1693 | return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1694 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1695 | # else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1696 | static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl, |
| 1697 | unsigned char *crt_buf, |
| 1698 | size_t crt_buf_len) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1699 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1700 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1701 | unsigned char const *const peer_cert_digest = |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1702 | ssl->session->peer_cert_digest; |
| 1703 | mbedtls_md_type_t const peer_cert_digest_type = |
| 1704 | ssl->session->peer_cert_digest_type; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1705 | mbedtls_md_info_t const *const digest_info = |
| 1706 | mbedtls_md_info_from_type(peer_cert_digest_type); |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1707 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 1708 | size_t digest_len; |
| 1709 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1710 | if (peer_cert_digest == NULL || digest_info == NULL) |
| 1711 | return -1; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1713 | digest_len = mbedtls_md_get_size(digest_info); |
| 1714 | if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) |
| 1715 | return -1; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1716 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1717 | ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest); |
| 1718 | if (ret != 0) |
| 1719 | return -1; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1721 | return memcmp(tmp_digest, peer_cert_digest, digest_len); |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1722 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1723 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1724 | # endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1725 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1726 | /* |
| 1727 | * Once the certificate message is read, parse it into a cert chain and |
| 1728 | * perform basic checks, but leave actual verification to the caller |
| 1729 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1730 | static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl, |
| 1731 | mbedtls_x509_crt *chain) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1732 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1733 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1734 | # if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1735 | int crt_cnt = 0; |
| 1736 | # endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1737 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1738 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1739 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1740 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 1741 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1742 | mbedtls_ssl_send_alert_message( |
| 1743 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1744 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 1745 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1748 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) { |
| 1749 | mbedtls_ssl_send_alert_message( |
| 1750 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1751 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 1752 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1753 | } |
| 1754 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1755 | if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) { |
| 1756 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1757 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1758 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 1759 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1762 | i = mbedtls_ssl_hs_hdr_len(ssl); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1763 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1764 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1766 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1767 | n = (ssl->in_msg[i + 1] << 8) | ssl->in_msg[i + 2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1768 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1769 | if (ssl->in_msg[i] != 0 || |
| 1770 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) { |
| 1771 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1772 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1773 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 1774 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1777 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 1778 | i += 3; |
| 1779 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1780 | /* Iterate through and parse the CRTs in the provided chain. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1781 | while (i < ssl->in_hslen) { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1782 | /* Check that there's room for the next CRT's length fields. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1783 | if (i + 3 > ssl->in_hslen) { |
| 1784 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1785 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1786 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 1787 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1788 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1789 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 1790 | * anything beyond 2**16 ~ 64K. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1791 | if (ssl->in_msg[i] != 0) { |
| 1792 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1793 | mbedtls_ssl_send_alert_message( |
| 1794 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1795 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT); |
| 1796 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1799 | /* Read length of the next CRT in the chain. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1800 | n = ((unsigned int)ssl->in_msg[i + 1] << 8) | |
| 1801 | (unsigned int)ssl->in_msg[i + 2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1802 | i += 3; |
| 1803 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1804 | if (n < 128 || i + n > ssl->in_hslen) { |
| 1805 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message")); |
| 1806 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1807 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 1808 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1811 | /* Check if we're handling the first CRT in the chain. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1812 | # if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1813 | if (crt_cnt++ == 0 && ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1814 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 1815 | /* During client-side renegotiation, check that the server's |
| 1816 | * end-CRTs hasn't changed compared to the initial handshake, |
| 1817 | * mitigating the triple handshake attack. On success, reuse |
| 1818 | * the original end-CRT instead of parsing it again. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1819 | MBEDTLS_SSL_DEBUG_MSG( |
| 1820 | 3, ("Check that peer CRT hasn't changed during renegotiation")); |
| 1821 | if (ssl_check_peer_crt_unchanged(ssl, &ssl->in_msg[i], n) != 0) { |
| 1822 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1823 | ("new server cert during renegotiation")); |
| 1824 | mbedtls_ssl_send_alert_message( |
| 1825 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1826 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED); |
| 1827 | return MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1828 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1829 | |
| 1830 | /* Now we can safely free the original chain. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1831 | ssl_clear_peer_cert(ssl->session); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1832 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1833 | # endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1834 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1835 | /* Parse the next certificate in the chain. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1836 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1837 | ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n); |
| 1838 | # else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 1839 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1840 | * it in-place from the input buffer instead of making a copy. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1841 | ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n); |
| 1842 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1843 | switch (ret) { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1844 | case 0: /*ok*/ |
| 1845 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 1846 | /* Ignore certificate with an unknown algorithm: maybe a |
| 1847 | prior certificate was already trusted. */ |
| 1848 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1849 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1850 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 1851 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 1852 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1853 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1854 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 1855 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 1856 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1857 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1858 | default: |
| 1859 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1860 | crt_parse_der_failed: |
| 1861 | mbedtls_ssl_send_alert_message( |
| 1862 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert); |
| 1863 | MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret); |
| 1864 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | i += n; |
| 1868 | } |
| 1869 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1870 | MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain); |
| 1871 | return 0; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1872 | } |
| 1873 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1874 | # if defined(MBEDTLS_SSL_SRV_C) |
| 1875 | static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl) |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1876 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1877 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) |
| 1878 | return -1; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1879 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1880 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1881 | if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) && |
| 1882 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 1883 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 1884 | memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) { |
| 1885 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate")); |
| 1886 | return 0; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1889 | return -1; |
| 1890 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1891 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1892 | # endif /* MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1893 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1894 | /* Check if a certificate message is expected. |
| 1895 | * Return either |
| 1896 | * - SSL_CERTIFICATE_EXPECTED, or |
| 1897 | * - SSL_CERTIFICATE_SKIP |
| 1898 | * indicating whether a Certificate message is expected or not. |
| 1899 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1900 | # define SSL_CERTIFICATE_EXPECTED 0 |
| 1901 | # define SSL_CERTIFICATE_SKIP 1 |
| 1902 | static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl, |
| 1903 | int authmode) |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1904 | { |
| 1905 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1906 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1907 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1908 | if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) |
| 1909 | return SSL_CERTIFICATE_SKIP; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1910 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1911 | # if defined(MBEDTLS_SSL_SRV_C) |
| 1912 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1913 | if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) |
| 1914 | return SSL_CERTIFICATE_SKIP; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1915 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1916 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1917 | ssl->session_negotiate->verify_result = |
| 1918 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1919 | return SSL_CERTIFICATE_SKIP; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1922 | # else |
| 1923 | ((void)authmode); |
| 1924 | # endif /* MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1925 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1926 | return SSL_CERTIFICATE_EXPECTED; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1929 | static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl, |
| 1930 | int authmode, |
| 1931 | mbedtls_x509_crt *chain, |
| 1932 | void *rs_ctx) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1933 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 1934 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1935 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1936 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1937 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1938 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1939 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 1940 | void *p_vrfy; |
| 1941 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1942 | if (authmode == MBEDTLS_SSL_VERIFY_NONE) |
| 1943 | return 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1944 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1945 | if (ssl->f_vrfy != NULL) { |
| 1946 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 1947 | ("Use context-specific verification callback")); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1948 | f_vrfy = ssl->f_vrfy; |
| 1949 | p_vrfy = ssl->p_vrfy; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1950 | } else { |
| 1951 | MBEDTLS_SSL_DEBUG_MSG( |
| 1952 | 3, ("Use configuration-specific verification callback")); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1953 | f_vrfy = ssl->conf->f_vrfy; |
| 1954 | p_vrfy = ssl->conf->p_vrfy; |
| 1955 | } |
| 1956 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1957 | /* |
| 1958 | * Main check: verify certificate |
| 1959 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1960 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1961 | if (ssl->conf->f_ca_cb != NULL) { |
| 1962 | ((void)rs_ctx); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1963 | have_ca_chain = 1; |
| 1964 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1965 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 1966 | ("use CA callback for X.509 CRT verification")); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 1967 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1968 | chain, ssl->conf->f_ca_cb, ssl->conf->p_ca_cb, |
| 1969 | ssl->conf->cert_profile, ssl->hostname, |
| 1970 | &ssl->session_negotiate->verify_result, f_vrfy, p_vrfy); |
| 1971 | } else |
| 1972 | # endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1973 | { |
| 1974 | mbedtls_x509_crt *ca_chain; |
| 1975 | mbedtls_x509_crl *ca_crl; |
| 1976 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1977 | # if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1978 | if (ssl->handshake->sni_ca_chain != NULL) { |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1979 | ca_chain = ssl->handshake->sni_ca_chain; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1980 | ca_crl = ssl->handshake->sni_ca_crl; |
| 1981 | } else |
| 1982 | # endif |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1983 | { |
| 1984 | ca_chain = ssl->conf->ca_chain; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1985 | ca_crl = ssl->conf->ca_crl; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1988 | if (ca_chain != NULL) |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1989 | have_ca_chain = 1; |
| 1990 | |
| 1991 | ret = mbedtls_x509_crt_verify_restartable( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1992 | chain, ca_chain, ca_crl, ssl->conf->cert_profile, ssl->hostname, |
| 1993 | &ssl->session_negotiate->verify_result, f_vrfy, p_vrfy, rs_ctx); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1994 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1995 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1996 | if (ret != 0) { |
| 1997 | MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2000 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2001 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) |
| 2002 | return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS; |
| 2003 | # endif |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2004 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2005 | /* |
| 2006 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2007 | */ |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2008 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2009 | # if defined(MBEDTLS_ECP_C) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2010 | { |
| 2011 | const mbedtls_pk_context *pk = &chain->pk; |
| 2012 | |
| 2013 | /* If certificate uses an EC key, make sure the curve is OK */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2014 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) && |
| 2015 | mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) { |
| 2016 | ssl->session_negotiate->verify_result |= |
| 2017 | MBEDTLS_X509_BADCERT_BAD_KEY; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2018 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2019 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)")); |
| 2020 | if (ret == 0) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2021 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2022 | } |
| 2023 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2024 | # endif /* MBEDTLS_ECP_C */ |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2025 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2026 | if (mbedtls_ssl_check_cert_usage( |
| 2027 | chain, ciphersuite_info, !ssl->conf->endpoint, |
| 2028 | &ssl->session_negotiate->verify_result) != 0) { |
| 2029 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)")); |
| 2030 | if (ret == 0) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2031 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2035 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2036 | * with details encoded in the verification flags. All other kinds |
| 2037 | * of error codes, including those from the user provided f_vrfy |
| 2038 | * functions, are treated as fatal and lead to a failure of |
| 2039 | * ssl_parse_certificate even if verification was optional. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2040 | if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2041 | (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 2042 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) { |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2043 | ret = 0; |
| 2044 | } |
| 2045 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2046 | if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) { |
| 2047 | MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain")); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2048 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2049 | } |
| 2050 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2051 | if (ret != 0) { |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2052 | uint8_t alert; |
| 2053 | |
| 2054 | /* The certificate may have been rejected for several reasons. |
| 2055 | Pick one and send the corresponding alert. Which alert to send |
| 2056 | may be a subject of debate in some cases. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2057 | if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2058 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2059 | else if (ssl->session_negotiate->verify_result & |
| 2060 | MBEDTLS_X509_BADCERT_CN_MISMATCH) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2061 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2062 | else if (ssl->session_negotiate->verify_result & |
| 2063 | MBEDTLS_X509_BADCERT_KEY_USAGE) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2064 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2065 | else if (ssl->session_negotiate->verify_result & |
| 2066 | MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2067 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2068 | else if (ssl->session_negotiate->verify_result & |
| 2069 | MBEDTLS_X509_BADCERT_NS_CERT_TYPE) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2070 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2071 | else if (ssl->session_negotiate->verify_result & |
| 2072 | MBEDTLS_X509_BADCERT_BAD_PK) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2073 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2074 | else if (ssl->session_negotiate->verify_result & |
| 2075 | MBEDTLS_X509_BADCERT_BAD_KEY) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2076 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2077 | else if (ssl->session_negotiate->verify_result & |
| 2078 | MBEDTLS_X509_BADCERT_EXPIRED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2079 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2080 | else if (ssl->session_negotiate->verify_result & |
| 2081 | MBEDTLS_X509_BADCERT_REVOKED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2082 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2083 | else if (ssl->session_negotiate->verify_result & |
| 2084 | MBEDTLS_X509_BADCERT_NOT_TRUSTED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2085 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2086 | else |
| 2087 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2088 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2089 | alert); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2092 | # if defined(MBEDTLS_DEBUG_C) |
| 2093 | if (ssl->session_negotiate->verify_result != 0) { |
| 2094 | MBEDTLS_SSL_DEBUG_MSG( |
| 2095 | 3, ("! Certificate verification flags %08x", |
| 2096 | (unsigned int)ssl->session_negotiate->verify_result)); |
| 2097 | } else { |
| 2098 | MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear")); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2099 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2100 | # endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2101 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2102 | return ret; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2105 | # if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2106 | static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl, |
| 2107 | unsigned char *start, |
| 2108 | size_t len) |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2109 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2110 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2111 | /* Remember digest of the peer's end-CRT. */ |
| 2112 | ssl->session_negotiate->peer_cert_digest = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2113 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 2114 | if (ssl->session_negotiate->peer_cert_digest == NULL) { |
| 2115 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed", |
| 2116 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN)); |
| 2117 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2118 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2119 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2120 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2123 | ret = mbedtls_md( |
| 2124 | mbedtls_md_info_from_type(MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), start, |
| 2125 | len, ssl->session_negotiate->peer_cert_digest); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2126 | |
| 2127 | ssl->session_negotiate->peer_cert_digest_type = |
| 2128 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2129 | ssl->session_negotiate->peer_cert_digest_len = |
| 2130 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2131 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2132 | return ret; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2135 | static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl, |
| 2136 | unsigned char *start, |
| 2137 | size_t len) |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2138 | { |
| 2139 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2140 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2141 | |
| 2142 | /* Make a copy of the peer's raw public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2143 | mbedtls_pk_init(&ssl->handshake->peer_pubkey); |
| 2144 | ret = mbedtls_pk_parse_subpubkey(&start, end, &ssl->handshake->peer_pubkey); |
| 2145 | if (ret != 0) { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2146 | /* We should have parsed the public key before. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2147 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2150 | return 0; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2151 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2152 | # endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2153 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2154 | int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2155 | { |
| 2156 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2157 | int crt_expected; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2158 | # if defined(MBEDTLS_SSL_SRV_C) && \ |
| 2159 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2160 | const int authmode = ssl->handshake->sni_authmode != |
| 2161 | MBEDTLS_SSL_VERIFY_UNSET ? |
| 2162 | ssl->handshake->sni_authmode : |
| 2163 | ssl->conf->authmode; |
| 2164 | # else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2165 | const int authmode = ssl->conf->authmode; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2166 | # endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2167 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2168 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2169 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2170 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate")); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2172 | crt_expected = ssl_parse_certificate_coordinate(ssl, authmode); |
| 2173 | if (crt_expected == SSL_CERTIFICATE_SKIP) { |
| 2174 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate")); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2175 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2178 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2179 | if (ssl->handshake->ecrs_enabled && |
| 2180 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2181 | chain = ssl->handshake->ecrs_peer_cert; |
| 2182 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2183 | goto crt_verify; |
| 2184 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2185 | # endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2186 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2187 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2188 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2189 | let it decide whether to alert. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2190 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2191 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2192 | } |
| 2193 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2194 | # if defined(MBEDTLS_SSL_SRV_C) |
| 2195 | if (ssl_srv_check_client_no_crt_notification(ssl) == 0) { |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2196 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2197 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2198 | if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2199 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2200 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2201 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2202 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2203 | # endif /* MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2204 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2205 | /* Clear existing peer CRT structure in case we tried to |
| 2206 | * reuse a session but it failed, and allocate a new one. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2207 | ssl_clear_peer_cert(ssl->session_negotiate); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2208 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2209 | chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 2210 | if (chain == NULL) { |
| 2211 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2212 | ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 2213 | sizeof(mbedtls_x509_crt))); |
| 2214 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2215 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2216 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2217 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2218 | goto exit; |
| 2219 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2220 | mbedtls_x509_crt_init(chain); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2221 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2222 | ret = ssl_parse_certificate_chain(ssl, chain); |
| 2223 | if (ret != 0) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2224 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2225 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2226 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2227 | if (ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2228 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2229 | |
| 2230 | crt_verify: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2231 | if (ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2232 | rs_ctx = &ssl->handshake->ecrs_ctx; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2233 | # endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2234 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2235 | ret = ssl_parse_certificate_verify(ssl, authmode, chain, rs_ctx); |
| 2236 | if (ret != 0) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2237 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2238 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2239 | # if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2240 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2241 | unsigned char *crt_start, *pk_start; |
| 2242 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2243 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2244 | /* We parse the CRT chain without copying, so |
| 2245 | * these pointers point into the input buffer, |
| 2246 | * and are hence still valid after freeing the |
| 2247 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2248 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2249 | crt_start = chain->raw.p; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2250 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2251 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2252 | pk_start = chain->pk_raw.p; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2253 | pk_len = chain->pk_raw.len; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2254 | |
| 2255 | /* Free the CRT structures before computing |
| 2256 | * digest and copying the peer's public key. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2257 | mbedtls_x509_crt_free(chain); |
| 2258 | mbedtls_free(chain); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2259 | chain = NULL; |
| 2260 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2261 | ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len); |
| 2262 | if (ret != 0) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2263 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2264 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2265 | ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len); |
| 2266 | if (ret != 0) |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2267 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2268 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2269 | # else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2270 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2271 | ssl->session_negotiate->peer_cert = chain; |
| 2272 | chain = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2273 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2274 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2275 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2276 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2277 | exit: |
| 2278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2279 | if (ret == 0) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2280 | ssl->state++; |
| 2281 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2282 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2283 | if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2284 | ssl->handshake->ecrs_peer_cert = chain; |
| 2285 | chain = NULL; |
| 2286 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2287 | # endif |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2288 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2289 | if (chain != NULL) { |
| 2290 | mbedtls_x509_crt_free(chain); |
| 2291 | mbedtls_free(chain); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2294 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2295 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2296 | # endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2297 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2298 | void mbedtls_ssl_optimize_checksum( |
| 2299 | mbedtls_ssl_context *ssl, |
| 2300 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2301 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2302 | ((void)ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 2303 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2304 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2305 | # if defined(MBEDTLS_SHA384_C) |
| 2306 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2307 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 2308 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2309 | # endif |
| 2310 | # if defined(MBEDTLS_SHA256_C) |
| 2311 | if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2312 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2313 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2314 | # endif |
| 2315 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2316 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2317 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2318 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 2319 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2320 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2321 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2322 | void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2323 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2324 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2325 | # if defined(MBEDTLS_SHA256_C) |
| 2326 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2327 | psa_hash_abort(&ssl->handshake->fin_sha256_psa); |
| 2328 | psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 2329 | # else |
| 2330 | mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0); |
| 2331 | # endif |
| 2332 | # endif |
| 2333 | # if defined(MBEDTLS_SHA384_C) |
| 2334 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2335 | psa_hash_abort(&ssl->handshake->fin_sha384_psa); |
| 2336 | psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 2337 | # else |
| 2338 | mbedtls_sha512_starts(&ssl->handshake->fin_sha512, 1); |
| 2339 | # endif |
| 2340 | # endif |
| 2341 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2344 | static void ssl_update_checksum_start(mbedtls_ssl_context *ssl, |
| 2345 | const unsigned char *buf, |
| 2346 | size_t len) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2347 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2348 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2349 | # if defined(MBEDTLS_SHA256_C) |
| 2350 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2351 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 2352 | # else |
| 2353 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
| 2354 | # endif |
| 2355 | # endif |
| 2356 | # if defined(MBEDTLS_SHA384_C) |
| 2357 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2358 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 2359 | # else |
| 2360 | mbedtls_sha512_update(&ssl->handshake->fin_sha512, buf, len); |
| 2361 | # endif |
| 2362 | # endif |
| 2363 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2366 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2367 | # if defined(MBEDTLS_SHA256_C) |
| 2368 | static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl, |
| 2369 | const unsigned char *buf, |
| 2370 | size_t len) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2371 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2372 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2373 | psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len); |
| 2374 | # else |
| 2375 | mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len); |
| 2376 | # endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2377 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2378 | # endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2379 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2380 | # if defined(MBEDTLS_SHA384_C) |
| 2381 | static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl, |
| 2382 | const unsigned char *buf, |
| 2383 | size_t len) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2384 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2385 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2386 | psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len); |
| 2387 | # else |
| 2388 | mbedtls_sha512_update(&ssl->handshake->fin_sha512, buf, len); |
| 2389 | # endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2390 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2391 | # endif |
| 2392 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 2393 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2394 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2395 | # if defined(MBEDTLS_SHA256_C) |
| 2396 | static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *ssl, |
| 2397 | unsigned char *buf, |
| 2398 | int from) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2399 | { |
| 2400 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2401 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2402 | unsigned char padbuf[32]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2403 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2404 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2405 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2406 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2407 | # else |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2408 | mbedtls_sha256_context sha256; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2409 | # endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2411 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2412 | if (!session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2413 | session = ssl->session; |
| 2414 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2415 | sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "client finished" : |
| 2416 | "server finished"; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2417 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2418 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2419 | sha256_psa = psa_hash_operation_init(); |
| 2420 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2421 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2422 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2423 | status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa); |
| 2424 | if (status != PSA_SUCCESS) { |
| 2425 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2426 | return; |
| 2427 | } |
| 2428 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2429 | status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size); |
| 2430 | if (status != PSA_SUCCESS) { |
| 2431 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2432 | return; |
| 2433 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2434 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32); |
| 2435 | # else |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2436 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2437 | mbedtls_sha256_init(&sha256); |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2438 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2439 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256")); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2440 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2441 | mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* |
| 2444 | * TLSv1.2: |
| 2445 | * hash = PRF( master, finished_label, |
| 2446 | * Hash( handshake ) )[0.11] |
| 2447 | */ |
| 2448 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2449 | # if !defined(MBEDTLS_SHA256_ALT) |
| 2450 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", |
| 2451 | (unsigned char *)sha256.state, sizeof(sha256.state)); |
| 2452 | # endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2453 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2454 | mbedtls_sha256_finish(&sha256, padbuf); |
| 2455 | mbedtls_sha256_free(&sha256); |
| 2456 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2457 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2458 | ssl->handshake->tls_prf(session->master, 48, sender, padbuf, 32, buf, len); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2459 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2460 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2461 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2462 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2463 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2464 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2465 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2466 | # endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2468 | # if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2470 | static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *ssl, |
| 2471 | unsigned char *buf, |
| 2472 | int from) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2473 | { |
| 2474 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2475 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2476 | unsigned char padbuf[48]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2477 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2478 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2479 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2480 | psa_status_t status; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2481 | # else |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2482 | mbedtls_sha512_context sha512; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2483 | # endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2486 | if (!session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2487 | session = ssl->session; |
| 2488 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2489 | sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "client finished" : |
| 2490 | "server finished"; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2491 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2492 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2493 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2494 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2495 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2496 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2497 | status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa); |
| 2498 | if (status != PSA_SUCCESS) { |
| 2499 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2500 | return; |
| 2501 | } |
| 2502 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2503 | status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size); |
| 2504 | if (status != PSA_SUCCESS) { |
| 2505 | MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed")); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2506 | return; |
| 2507 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2508 | MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48); |
| 2509 | # else |
| 2510 | mbedtls_sha512_init(&sha512); |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2511 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2512 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384")); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2513 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2514 | mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2515 | |
| 2516 | /* |
| 2517 | * TLSv1.2: |
| 2518 | * hash = PRF( master, finished_label, |
| 2519 | * Hash( handshake ) )[0.11] |
| 2520 | */ |
| 2521 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2522 | # if !defined(MBEDTLS_SHA512_ALT) |
| 2523 | MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", |
| 2524 | (unsigned char *)sha512.state, sizeof(sha512.state)); |
| 2525 | # endif |
| 2526 | mbedtls_sha512_finish(&sha512, padbuf); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2527 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2528 | mbedtls_sha512_free(&sha512); |
| 2529 | # endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2530 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2531 | ssl->handshake->tls_prf(session->master, 48, sender, padbuf, 48, buf, len); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2532 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2533 | MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2534 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2535 | mbedtls_platform_zeroize(padbuf, sizeof(padbuf)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2536 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2537 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished")); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2538 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2539 | # endif /* MBEDTLS_SHA384_C */ |
| 2540 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2541 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2542 | void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2543 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2544 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2545 | |
| 2546 | /* |
| 2547 | * Free our handshake params |
| 2548 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2549 | mbedtls_ssl_handshake_free(ssl); |
| 2550 | mbedtls_free(ssl->handshake); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2551 | ssl->handshake = NULL; |
| 2552 | |
| 2553 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2554 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2555 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2556 | if (ssl->transform) { |
| 2557 | mbedtls_ssl_transform_free(ssl->transform); |
| 2558 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2559 | } |
| 2560 | ssl->transform = ssl->transform_negotiate; |
| 2561 | ssl->transform_negotiate = NULL; |
| 2562 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2563 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free")); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2564 | } |
| 2565 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2566 | void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2567 | { |
| 2568 | int resume = ssl->handshake->resume; |
| 2569 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2570 | MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup")); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2571 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2572 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2573 | if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 2574 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2575 | ssl->renego_records_seen = 0; |
| 2576 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2577 | # endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2578 | |
| 2579 | /* |
| 2580 | * Free the previous session and switch in the current one |
| 2581 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2582 | if (ssl->session) { |
| 2583 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2584 | /* RFC 7366 3.1: keep the EtM state */ |
| 2585 | ssl->session_negotiate->encrypt_then_mac = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2586 | ssl->session->encrypt_then_mac; |
| 2587 | # endif |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2588 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2589 | mbedtls_ssl_session_free(ssl->session); |
| 2590 | mbedtls_free(ssl->session); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2591 | } |
| 2592 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2593 | ssl->session_negotiate = NULL; |
| 2594 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2595 | /* |
| 2596 | * Add cache entry |
| 2597 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2598 | if (ssl->conf->f_set_cache != NULL && ssl->session->id_len != 0 && |
| 2599 | resume == 0) { |
| 2600 | if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session->id, |
| 2601 | ssl->session->id_len, ssl->session) != 0) |
| 2602 | MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session")); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2603 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2605 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2606 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2607 | ssl->handshake->flight != NULL) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2608 | /* Cancel handshake timer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2609 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2610 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2611 | /* Keep last flight around in case we need to resend it: |
| 2612 | * we need the handshake and transform structures for that */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2613 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform")); |
| 2614 | } else |
| 2615 | # endif |
| 2616 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2617 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2618 | ssl->state++; |
| 2619 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2620 | MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2623 | int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2624 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2625 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2626 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2627 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished")); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2628 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2629 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2630 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2631 | ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2632 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 2633 | /* |
| 2634 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 2635 | * may define some other value. Currently (early 2016), no defined |
| 2636 | * ciphersuite does this (and this is unlikely to change as activity has |
| 2637 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 2638 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2639 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2640 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2641 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2642 | ssl->verify_data_len = hash_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2643 | memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len); |
| 2644 | # endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2645 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2646 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2647 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2648 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2649 | |
| 2650 | /* |
| 2651 | * In case of session resuming, invert the client and server |
| 2652 | * ChangeCipherSpec messages order. |
| 2653 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2654 | if (ssl->handshake->resume != 0) { |
| 2655 | # if defined(MBEDTLS_SSL_CLI_C) |
| 2656 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2658 | # endif |
| 2659 | # if defined(MBEDTLS_SSL_SRV_C) |
| 2660 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2661 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2662 | # endif |
| 2663 | } else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2664 | ssl->state++; |
| 2665 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2666 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2667 | * Switch to our negotiated transform and session parameters for outbound |
| 2668 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2669 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2670 | MBEDTLS_SSL_DEBUG_MSG( |
| 2671 | 3, ("switching to new transform spec for outbound data")); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2672 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2673 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2674 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2675 | unsigned char i; |
| 2676 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2677 | /* Remember current epoch settings for resending */ |
| 2678 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2679 | memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2680 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2681 | /* Set sequence_number to zero */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2682 | memset(ssl->cur_out_ctr + 2, 0, 6); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2683 | |
| 2684 | /* Increment epoch */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2685 | for (i = 2; i > 0; i--) |
| 2686 | if (++ssl->cur_out_ctr[i - 1] != 0) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2687 | break; |
| 2688 | |
| 2689 | /* The loop goes to its end iff the counter is wrapping */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2690 | if (i == 0) { |
| 2691 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
| 2692 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2693 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2694 | } else |
| 2695 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2696 | memset(ssl->cur_out_ctr, 0, 8); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2697 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2698 | ssl->transform_out = ssl->transform_negotiate; |
| 2699 | ssl->session_out = ssl->session_negotiate; |
| 2700 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2701 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2702 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
| 2703 | mbedtls_ssl_send_flight_completed(ssl); |
| 2704 | # endif |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2705 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2706 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 2707 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 2708 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2711 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2712 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2713 | (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 2714 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret); |
| 2715 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2716 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2717 | # endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2718 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2719 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2721 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2724 | # define SSL_MAX_HASH_LEN 12 |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2725 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2726 | int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2727 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2728 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2729 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2730 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2731 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2732 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2733 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2734 | ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2735 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2736 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 2737 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 2738 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2741 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 2742 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 2743 | mbedtls_ssl_send_alert_message( |
| 2744 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2745 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 2746 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2749 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2750 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2751 | if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) { |
| 2752 | mbedtls_ssl_send_alert_message( |
| 2753 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2754 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 2755 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2756 | } |
| 2757 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2758 | if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) { |
| 2759 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 2760 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2761 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR); |
| 2762 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2765 | if (mbedtls_ssl_safer_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), buf, |
| 2766 | hash_len) != 0) { |
| 2767 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message")); |
| 2768 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2769 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR); |
| 2770 | return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2773 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2774 | ssl->verify_data_len = hash_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2775 | memcpy(ssl->peer_verify_data, buf, hash_len); |
| 2776 | # endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2777 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2778 | if (ssl->handshake->resume != 0) { |
| 2779 | # if defined(MBEDTLS_SSL_CLI_C) |
| 2780 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2781 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2782 | # endif |
| 2783 | # if defined(MBEDTLS_SSL_SRV_C) |
| 2784 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2786 | # endif |
| 2787 | } else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2788 | ssl->state++; |
| 2789 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2790 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2791 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
| 2792 | mbedtls_ssl_recv_flight_completed(ssl); |
| 2793 | # endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2794 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2795 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2796 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2797 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2800 | static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2801 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2802 | memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2803 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2804 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2805 | # if defined(MBEDTLS_SHA256_C) |
| 2806 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2807 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2808 | psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256); |
| 2809 | # else |
| 2810 | mbedtls_sha256_init(&handshake->fin_sha256); |
| 2811 | mbedtls_sha256_starts(&handshake->fin_sha256, 0); |
| 2812 | # endif |
| 2813 | # endif |
| 2814 | # if defined(MBEDTLS_SHA384_C) |
| 2815 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2816 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2817 | psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384); |
| 2818 | # else |
| 2819 | mbedtls_sha512_init(&handshake->fin_sha512); |
| 2820 | mbedtls_sha512_starts(&handshake->fin_sha512, 1); |
| 2821 | # endif |
| 2822 | # endif |
| 2823 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2824 | |
| 2825 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2826 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2827 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 2828 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 2829 | mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs); |
| 2830 | # endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2831 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2832 | # if defined(MBEDTLS_DHM_C) |
| 2833 | mbedtls_dhm_init(&handshake->dhm_ctx); |
| 2834 | # endif |
| 2835 | # if defined(MBEDTLS_ECDH_C) |
| 2836 | mbedtls_ecdh_init(&handshake->ecdh_ctx); |
| 2837 | # endif |
| 2838 | # if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 2839 | mbedtls_ecjpake_init(&handshake->ecjpake_ctx); |
| 2840 | # if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 2841 | handshake->ecjpake_cache = NULL; |
| 2842 | handshake->ecjpake_cache_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2843 | # endif |
| 2844 | # endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2845 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2846 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 2847 | mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx); |
| 2848 | # endif |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 2849 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2850 | # if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2851 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2852 | # endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 2853 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2854 | # if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2855 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2856 | mbedtls_pk_init(&handshake->peer_pubkey); |
| 2857 | # endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2860 | void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2861 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2862 | memset(transform, 0, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2863 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2864 | mbedtls_cipher_init(&transform->cipher_ctx_enc); |
| 2865 | mbedtls_cipher_init(&transform->cipher_ctx_dec); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2866 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2867 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 2868 | mbedtls_md_init(&transform->md_ctx_enc); |
| 2869 | mbedtls_md_init(&transform->md_ctx_dec); |
| 2870 | # endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2871 | } |
| 2872 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2873 | void mbedtls_ssl_session_init(mbedtls_ssl_session *session) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2874 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2875 | memset(session, 0, sizeof(mbedtls_ssl_session)); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2878 | static int ssl_handshake_init(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2879 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2880 | /* Clear old handshake information if present */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2881 | if (ssl->transform_negotiate) |
| 2882 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 2883 | if (ssl->session_negotiate) |
| 2884 | mbedtls_ssl_session_free(ssl->session_negotiate); |
| 2885 | if (ssl->handshake) |
| 2886 | mbedtls_ssl_handshake_free(ssl); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2887 | |
| 2888 | /* |
| 2889 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 2890 | * Now allocate missing structures. |
| 2891 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2892 | if (ssl->transform_negotiate == NULL) { |
| 2893 | ssl->transform_negotiate = |
| 2894 | mbedtls_calloc(1, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2895 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2896 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2897 | if (ssl->session_negotiate == NULL) { |
| 2898 | ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2899 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2900 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2901 | if (ssl->handshake == NULL) { |
| 2902 | ssl->handshake = |
| 2903 | mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params)); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2904 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2905 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2906 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 2907 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2908 | handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 2909 | MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2910 | # endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2911 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2912 | /* All pointers should exist and can be directly freed without issue */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2913 | if (ssl->handshake == NULL || ssl->transform_negotiate == NULL || |
| 2914 | ssl->session_negotiate == NULL) { |
| 2915 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed")); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2916 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2917 | mbedtls_free(ssl->handshake); |
| 2918 | mbedtls_free(ssl->transform_negotiate); |
| 2919 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2920 | |
| 2921 | ssl->handshake = NULL; |
| 2922 | ssl->transform_negotiate = NULL; |
| 2923 | ssl->session_negotiate = NULL; |
| 2924 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2925 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2928 | /* Initialize structures */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2929 | mbedtls_ssl_session_init(ssl->session_negotiate); |
| 2930 | mbedtls_ssl_transform_init(ssl->transform_negotiate); |
| 2931 | ssl_handshake_params_init(ssl->handshake); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 2932 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2933 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2934 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2935 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2936 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2937 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2938 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 2939 | else |
| 2940 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2941 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2942 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2943 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2944 | # endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2945 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2946 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2949 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2950 | /* Dummy cookie callbacks for defaults */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2951 | static int ssl_cookie_write_dummy(void *ctx, |
| 2952 | unsigned char **p, |
| 2953 | unsigned char *end, |
| 2954 | const unsigned char *cli_id, |
| 2955 | size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2956 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2957 | ((void)ctx); |
| 2958 | ((void)p); |
| 2959 | ((void)end); |
| 2960 | ((void)cli_id); |
| 2961 | ((void)cli_id_len); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2962 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2963 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2964 | } |
| 2965 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2966 | static int ssl_cookie_check_dummy(void *ctx, |
| 2967 | const unsigned char *cookie, |
| 2968 | size_t cookie_len, |
| 2969 | const unsigned char *cli_id, |
| 2970 | size_t cli_id_len) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2971 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2972 | ((void)ctx); |
| 2973 | ((void)cookie); |
| 2974 | ((void)cookie_len); |
| 2975 | ((void)cli_id); |
| 2976 | ((void)cli_id_len); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2977 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2978 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2979 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2980 | # endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 2981 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2982 | /* |
| 2983 | * Initialize an SSL context |
| 2984 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2985 | void mbedtls_ssl_init(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2986 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2987 | memset(ssl, 0, sizeof(mbedtls_ssl_context)); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | /* |
| 2991 | * Setup an SSL context |
| 2992 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 2993 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2994 | int mbedtls_ssl_setup(mbedtls_ssl_context *ssl, const mbedtls_ssl_config *conf) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2995 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2996 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2997 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 2998 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2999 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3000 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3001 | |
| 3002 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3003 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3004 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3005 | |
| 3006 | /* Set to NULL in case of an error condition */ |
| 3007 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3008 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3009 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3010 | ssl->in_buf_len = in_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3011 | # endif |
| 3012 | ssl->in_buf = mbedtls_calloc(1, in_buf_len); |
| 3013 | if (ssl->in_buf == NULL) { |
| 3014 | MBEDTLS_SSL_DEBUG_MSG( |
| 3015 | 1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len)); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3016 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3017 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3018 | } |
| 3019 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3020 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3021 | ssl->out_buf_len = out_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3022 | # endif |
| 3023 | ssl->out_buf = mbedtls_calloc(1, out_buf_len); |
| 3024 | if (ssl->out_buf == NULL) { |
| 3025 | MBEDTLS_SSL_DEBUG_MSG( |
| 3026 | 1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len)); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3027 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3028 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3031 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3032 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3033 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 3034 | memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info)); |
| 3035 | # endif |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3036 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3037 | if ((ret = ssl_handshake_init(ssl)) != 0) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3038 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3039 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3040 | return 0; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3041 | |
| 3042 | error: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3043 | mbedtls_free(ssl->in_buf); |
| 3044 | mbedtls_free(ssl->out_buf); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3045 | |
| 3046 | ssl->conf = NULL; |
| 3047 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3048 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3049 | ssl->in_buf_len = 0; |
| 3050 | ssl->out_buf_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3051 | # endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3052 | ssl->in_buf = NULL; |
| 3053 | ssl->out_buf = NULL; |
| 3054 | |
| 3055 | ssl->in_hdr = NULL; |
| 3056 | ssl->in_ctr = NULL; |
| 3057 | ssl->in_len = NULL; |
| 3058 | ssl->in_iv = NULL; |
| 3059 | ssl->in_msg = NULL; |
| 3060 | |
| 3061 | ssl->out_hdr = NULL; |
| 3062 | ssl->out_ctr = NULL; |
| 3063 | ssl->out_len = NULL; |
| 3064 | ssl->out_iv = NULL; |
| 3065 | ssl->out_msg = NULL; |
| 3066 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3067 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3071 | * Reset an initialized and used SSL context for re-use while retaining |
| 3072 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3073 | * |
| 3074 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3075 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3076 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3077 | int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3078 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3079 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3080 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3081 | size_t in_buf_len = ssl->in_buf_len; |
| 3082 | size_t out_buf_len = ssl->out_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3083 | # else |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3084 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3085 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3086 | # endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3087 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3088 | # if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 3089 | !defined(MBEDTLS_SSL_SRV_C) |
| 3090 | ((void)partial); |
| 3091 | # endif |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3092 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3093 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3094 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3095 | /* Cancel any possibly running timer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3096 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3097 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3098 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3099 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3100 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3101 | |
| 3102 | ssl->verify_data_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3103 | memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 3104 | memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN); |
| 3105 | # endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3106 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3107 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3108 | ssl->in_offt = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3109 | mbedtls_ssl_reset_in_out_pointers(ssl); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3110 | |
| 3111 | ssl->in_msgtype = 0; |
| 3112 | ssl->in_msglen = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3113 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3114 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3115 | ssl->in_epoch = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3116 | # endif |
| 3117 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3118 | mbedtls_ssl_dtls_replay_reset(ssl); |
| 3119 | # endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3120 | |
| 3121 | ssl->in_hslen = 0; |
| 3122 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3123 | |
| 3124 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3125 | |
| 3126 | ssl->out_msgtype = 0; |
| 3127 | ssl->out_msglen = 0; |
| 3128 | ssl->out_left = 0; |
| 3129 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3130 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3131 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3132 | ssl->transform_in = NULL; |
| 3133 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3134 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3135 | ssl->session_in = NULL; |
| 3136 | ssl->session_out = NULL; |
| 3137 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3138 | memset(ssl->out_buf, 0, out_buf_len); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3139 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3140 | # if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \ |
| 3141 | defined(MBEDTLS_SSL_SRV_C) |
| 3142 | if (partial == 0) |
| 3143 | # endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3144 | { |
| 3145 | ssl->in_left = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3146 | memset(ssl->in_buf, 0, in_buf_len); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3147 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3148 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3149 | if (ssl->transform) { |
| 3150 | mbedtls_ssl_transform_free(ssl->transform); |
| 3151 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3152 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3153 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3155 | if (ssl->session) { |
| 3156 | mbedtls_ssl_session_free(ssl->session); |
| 3157 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3158 | ssl->session = NULL; |
| 3159 | } |
| 3160 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3161 | # if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3162 | ssl->alpn_chosen = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3163 | # endif |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3164 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3165 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 3166 | # if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
| 3167 | if (partial == 0) |
| 3168 | # endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3169 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3170 | mbedtls_free(ssl->cli_id); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3171 | ssl->cli_id = NULL; |
| 3172 | ssl->cli_id_len = 0; |
| 3173 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3174 | # endif |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3176 | if ((ret = ssl_handshake_init(ssl)) != 0) |
| 3177 | return ret; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3178 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3179 | return 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3182 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3183 | * Reset an initialized and used SSL context for re-use while retaining |
| 3184 | * all application-set variables, function pointers and data. |
| 3185 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3186 | int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3187 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3188 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3192 | * SSL set accessors |
| 3193 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3194 | void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3195 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3196 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3199 | void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport) |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3200 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3201 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3204 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3205 | void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3206 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3207 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3208 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3209 | # endif |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3211 | void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, |
| 3212 | unsigned limit) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3213 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3214 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3215 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3216 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3217 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3218 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3219 | void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl, |
| 3220 | unsigned allow_packing) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3221 | { |
| 3222 | ssl->disable_datagram_packing = !allow_packing; |
| 3223 | } |
| 3224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3225 | void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf, |
| 3226 | uint32_t min, |
| 3227 | uint32_t max) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3228 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3229 | conf->hs_timeout_min = min; |
| 3230 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3231 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3232 | # endif |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3233 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3234 | void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3235 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3236 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3239 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3240 | void mbedtls_ssl_conf_verify( |
| 3241 | mbedtls_ssl_config *conf, |
| 3242 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3243 | void *p_vrfy) |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3244 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3245 | conf->f_vrfy = f_vrfy; |
| 3246 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3247 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3248 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3249 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3250 | void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf, |
| 3251 | int (*f_rng)(void *, unsigned char *, size_t), |
| 3252 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3253 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3254 | conf->f_rng = f_rng; |
| 3255 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3258 | void mbedtls_ssl_conf_dbg( |
| 3259 | mbedtls_ssl_config *conf, |
| 3260 | void (*f_dbg)(void *, int, const char *, int, const char *), |
| 3261 | void *p_dbg) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3262 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3263 | conf->f_dbg = f_dbg; |
| 3264 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3267 | void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl, |
| 3268 | void *p_bio, |
| 3269 | mbedtls_ssl_send_t *f_send, |
| 3270 | mbedtls_ssl_recv_t *f_recv, |
| 3271 | mbedtls_ssl_recv_timeout_t *f_recv_timeout) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3272 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3273 | ssl->p_bio = p_bio; |
| 3274 | ssl->f_send = f_send; |
| 3275 | ssl->f_recv = f_recv; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3276 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3277 | } |
| 3278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3279 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3280 | void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu) |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3281 | { |
| 3282 | ssl->mtu = mtu; |
| 3283 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3284 | # endif |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3285 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3286 | void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout) |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3287 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3288 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3291 | void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl, |
| 3292 | void *p_timer, |
| 3293 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 3294 | mbedtls_ssl_get_timer_t *f_get_timer) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3295 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3296 | ssl->p_timer = p_timer; |
| 3297 | ssl->f_set_timer = f_set_timer; |
| 3298 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3299 | |
| 3300 | /* Make sure we start with no timer running */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3301 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3304 | # if defined(MBEDTLS_SSL_SRV_C) |
| 3305 | void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf, |
| 3306 | void *p_cache, |
| 3307 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 3308 | mbedtls_ssl_cache_set_t *f_set_cache) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3309 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3310 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3311 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3312 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3313 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3314 | # endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3315 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3316 | # if defined(MBEDTLS_SSL_CLI_C) |
| 3317 | int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, |
| 3318 | const mbedtls_ssl_session *session) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3319 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3320 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3321 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3322 | if (ssl == NULL || session == NULL || ssl->session_negotiate == NULL || |
| 3323 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 3324 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3325 | } |
| 3326 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3327 | if (ssl->handshake->resume == 1) |
| 3328 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3330 | if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate, session)) != 0) |
| 3331 | return ret; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3332 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3333 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3334 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3335 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3336 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3337 | # endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3338 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3339 | void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf, |
| 3340 | const int *ciphersuites) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 3341 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 3342 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3345 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3346 | void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf, |
| 3347 | const mbedtls_x509_crt_profile *profile) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3348 | { |
| 3349 | conf->cert_profile = profile; |
| 3350 | } |
| 3351 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3352 | /* Append a new keycert entry to a (possibly empty) list */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3353 | static int ssl_append_key_cert(mbedtls_ssl_key_cert **head, |
| 3354 | mbedtls_x509_crt *cert, |
| 3355 | mbedtls_pk_context *key) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3356 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3357 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3358 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3359 | new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert)); |
| 3360 | if (new_cert == NULL) |
| 3361 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3362 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3363 | new_cert->cert = cert; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3364 | new_cert->key = key; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3365 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3366 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3367 | /* Update head is the list was null, else add to the end */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3368 | if (*head == NULL) { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3369 | *head = new_cert; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3370 | } else { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3371 | mbedtls_ssl_key_cert *cur = *head; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3372 | while (cur->next != NULL) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3373 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3374 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3375 | } |
| 3376 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3377 | return 0; |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3380 | int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3381 | mbedtls_x509_crt *own_cert, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3382 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3383 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3384 | return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3385 | } |
| 3386 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3387 | void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3388 | mbedtls_x509_crt *ca_chain, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3389 | mbedtls_x509_crl *ca_crl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3390 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3391 | conf->ca_chain = ca_chain; |
| 3392 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3393 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3394 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3395 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3396 | * cannot be used together. */ |
| 3397 | conf->f_ca_cb = NULL; |
| 3398 | conf->p_ca_cb = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3399 | # endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3400 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3401 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3402 | # if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3403 | void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf, |
| 3404 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 3405 | void *p_ca_cb) |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3406 | { |
| 3407 | conf->f_ca_cb = f_ca_cb; |
| 3408 | conf->p_ca_cb = p_ca_cb; |
| 3409 | |
| 3410 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3411 | * cannot be used together. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3412 | conf->ca_chain = NULL; |
| 3413 | conf->ca_crl = NULL; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3414 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3415 | # endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3416 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 3417 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3418 | # if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3419 | int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl, |
| 3420 | mbedtls_x509_crt *own_cert, |
| 3421 | mbedtls_pk_context *pk_key) |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3422 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3423 | return ( |
| 3424 | ssl_append_key_cert(&ssl->handshake->sni_key_cert, own_cert, pk_key)); |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3425 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3426 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3427 | void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl, |
| 3428 | mbedtls_x509_crt *ca_chain, |
| 3429 | mbedtls_x509_crl *ca_crl) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3430 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3431 | ssl->handshake->sni_ca_chain = ca_chain; |
| 3432 | ssl->handshake->sni_ca_crl = ca_crl; |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3433 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3434 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3435 | void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl, int authmode) |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3436 | { |
| 3437 | ssl->handshake->sni_authmode = authmode; |
| 3438 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3439 | # endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3440 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3441 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3442 | void mbedtls_ssl_set_verify( |
| 3443 | mbedtls_ssl_context *ssl, |
| 3444 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3445 | void *p_vrfy) |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3446 | { |
| 3447 | ssl->f_vrfy = f_vrfy; |
| 3448 | ssl->p_vrfy = p_vrfy; |
| 3449 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3450 | # endif |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3451 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3452 | # if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3453 | /* |
| 3454 | * Set EC J-PAKE password for current handshake |
| 3455 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3456 | int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl, |
| 3457 | const unsigned char *pw, |
| 3458 | size_t pw_len) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3459 | { |
| 3460 | mbedtls_ecjpake_role role; |
| 3461 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3462 | if (ssl->handshake == NULL || ssl->conf == NULL) |
| 3463 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3464 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3465 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3466 | role = MBEDTLS_ECJPAKE_SERVER; |
| 3467 | else |
| 3468 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 3469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3470 | return (mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx, role, |
| 3471 | MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, |
| 3472 | pw, pw_len)); |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3473 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3474 | # endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3475 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3476 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3477 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3478 | static int ssl_conf_psk_is_configured(mbedtls_ssl_config const *conf) |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3479 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3480 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3481 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) |
| 3482 | return 1; |
| 3483 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3484 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3485 | if (conf->psk != NULL) |
| 3486 | return 1; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3487 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3488 | return 0; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3489 | } |
| 3490 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3491 | static void ssl_conf_remove_psk(mbedtls_ssl_config *conf) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3492 | { |
| 3493 | /* Remove reference to existing PSK, if any. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3494 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3495 | if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3496 | /* The maintenance of the PSK key slot is the |
| 3497 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3498 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3499 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 3500 | /* This and the following branch should never |
| 3501 | * be taken simultaenously as we maintain the |
| 3502 | * invariant that raw and opaque PSKs are never |
| 3503 | * configured simultaneously. As a safeguard, |
| 3504 | * though, `else` is omitted here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3505 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3506 | if (conf->psk != NULL) { |
| 3507 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3508 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3509 | mbedtls_free(conf->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3510 | conf->psk = NULL; |
| 3511 | conf->psk_len = 0; |
| 3512 | } |
| 3513 | |
| 3514 | /* Remove reference to PSK identity, if any. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3515 | if (conf->psk_identity != NULL) { |
| 3516 | mbedtls_free(conf->psk_identity); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3517 | conf->psk_identity = NULL; |
| 3518 | conf->psk_identity_len = 0; |
| 3519 | } |
| 3520 | } |
| 3521 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3522 | /* This function assumes that PSK identity in the SSL config is unset. |
| 3523 | * It checks that the provided identity is well-formed and attempts |
| 3524 | * to make a copy of it in the SSL config. |
| 3525 | * On failure, the PSK identity in the config remains unset. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3526 | static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf, |
| 3527 | unsigned char const *psk_identity, |
| 3528 | size_t psk_identity_len) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3529 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3530 | /* Identity len will be encoded on two bytes */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3531 | if (psk_identity == NULL || (psk_identity_len >> 16) != 0 || |
| 3532 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 3533 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3536 | conf->psk_identity = mbedtls_calloc(1, psk_identity_len); |
| 3537 | if (conf->psk_identity == NULL) |
| 3538 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3539 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3540 | conf->psk_identity_len = psk_identity_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3541 | memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 3542 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3543 | return 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3544 | } |
| 3545 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3546 | int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf, |
| 3547 | const unsigned char *psk, |
| 3548 | size_t psk_len, |
| 3549 | const unsigned char *psk_identity, |
| 3550 | size_t psk_identity_len) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3551 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3552 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3553 | |
| 3554 | /* We currently only support one PSK, raw or opaque. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3555 | if (ssl_conf_psk_is_configured(conf)) |
| 3556 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3557 | |
| 3558 | /* Check and set raw PSK */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3559 | if (psk == NULL) |
| 3560 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3561 | if (psk_len == 0) |
| 3562 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 3563 | if (psk_len > MBEDTLS_PSK_MAX_LEN) |
| 3564 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3565 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3566 | if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) |
| 3567 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3568 | conf->psk_len = psk_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3569 | memcpy(conf->psk, psk, conf->psk_len); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3570 | |
| 3571 | /* Check and set PSK Identity */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3572 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 3573 | if (ret != 0) |
| 3574 | ssl_conf_remove_psk(conf); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3575 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3576 | return ret; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3579 | static void ssl_remove_psk(mbedtls_ssl_context *ssl) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3580 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3581 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3582 | if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3583 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3584 | } else |
| 3585 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3586 | if (ssl->handshake->psk != NULL) { |
| 3587 | mbedtls_platform_zeroize(ssl->handshake->psk, ssl->handshake->psk_len); |
| 3588 | mbedtls_free(ssl->handshake->psk); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3589 | ssl->handshake->psk_len = 0; |
| 3590 | } |
| 3591 | } |
| 3592 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3593 | int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl, |
| 3594 | const unsigned char *psk, |
| 3595 | size_t psk_len) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3596 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3597 | if (psk == NULL || ssl->handshake == NULL) |
| 3598 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3599 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3600 | if (psk_len > MBEDTLS_PSK_MAX_LEN) |
| 3601 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3602 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3603 | ssl_remove_psk(ssl); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3605 | if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) |
| 3606 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3607 | |
| 3608 | ssl->handshake->psk_len = psk_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3609 | memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3610 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3611 | return 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3612 | } |
| 3613 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3614 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3615 | int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf, |
| 3616 | psa_key_id_t psk, |
| 3617 | const unsigned char *psk_identity, |
| 3618 | size_t psk_identity_len) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3619 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3620 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3621 | |
| 3622 | /* We currently only support one PSK, raw or opaque. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3623 | if (ssl_conf_psk_is_configured(conf)) |
| 3624 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3625 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3626 | /* Check and set opaque PSK */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3627 | if (mbedtls_svc_key_id_is_null(psk)) |
| 3628 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3629 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3630 | |
| 3631 | /* Check and set PSK Identity */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3632 | ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len); |
| 3633 | if (ret != 0) |
| 3634 | ssl_conf_remove_psk(conf); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3635 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3636 | return ret; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3637 | } |
| 3638 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3639 | int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl, psa_key_id_t psk) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3640 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3641 | if ((mbedtls_svc_key_id_is_null(psk)) || (ssl->handshake == NULL)) |
| 3642 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3643 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3644 | ssl_remove_psk(ssl); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3645 | ssl->handshake->psk_opaque = psk; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3646 | return 0; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3647 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3648 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3649 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3650 | void mbedtls_ssl_conf_psk_cb( |
| 3651 | mbedtls_ssl_config *conf, |
| 3652 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t), |
| 3653 | void *p_psk) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3654 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3655 | conf->f_psk = f_psk; |
| 3656 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3657 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3658 | # endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 3659 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3660 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 3661 | int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, |
| 3662 | const unsigned char *dhm_P, |
| 3663 | size_t P_len, |
| 3664 | const unsigned char *dhm_G, |
| 3665 | size_t G_len) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3666 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3667 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3668 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3669 | if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || |
| 3670 | (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { |
| 3671 | mbedtls_mpi_free(&conf->dhm_P); |
| 3672 | mbedtls_mpi_free(&conf->dhm_G); |
| 3673 | return ret; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3676 | return 0; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3677 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3678 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3679 | int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, |
| 3680 | mbedtls_dhm_context *dhm_ctx) |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3681 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3682 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3683 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3684 | if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 3685 | &conf->dhm_P)) != 0 || |
| 3686 | (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 3687 | &conf->dhm_G)) != 0) { |
| 3688 | mbedtls_mpi_free(&conf->dhm_P); |
| 3689 | mbedtls_mpi_free(&conf->dhm_G); |
| 3690 | return ret; |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 3691 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3692 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3693 | return 0; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3694 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3695 | # endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 3696 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3697 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 3698 | /* |
| 3699 | * Set the minimum length for Diffie-Hellman parameters |
| 3700 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3701 | void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, |
| 3702 | unsigned int bitlen) |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 3703 | { |
| 3704 | conf->dhm_min_bitlen = bitlen; |
| 3705 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3706 | # endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 3707 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3708 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 3709 | /* |
| 3710 | * Set allowed/preferred hashes for handshake signatures |
| 3711 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3712 | void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf, const int *hashes) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 3713 | { |
| 3714 | conf->sig_hashes = hashes; |
| 3715 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3716 | # endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 3717 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3718 | # if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3719 | /* |
| 3720 | * Set the allowed elliptic curves |
| 3721 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3722 | void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf, |
| 3723 | const mbedtls_ecp_group_id *curve_list) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3724 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3725 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3726 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3727 | # endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 3728 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3729 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3730 | int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3731 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3732 | /* Initialize to suppress unnecessary compiler warning */ |
| 3733 | size_t hostname_len = 0; |
| 3734 | |
| 3735 | /* Check if new hostname is valid before |
| 3736 | * making any change to current one */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3737 | if (hostname != NULL) { |
| 3738 | hostname_len = strlen(hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3739 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3740 | if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) |
| 3741 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | /* Now it's clear that we will overwrite the old hostname, |
| 3745 | * so we can free it safely */ |
| 3746 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3747 | if (ssl->hostname != NULL) { |
| 3748 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 3749 | mbedtls_free(ssl->hostname); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 3753 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3754 | if (hostname == NULL) { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3755 | ssl->hostname = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3756 | } else { |
| 3757 | ssl->hostname = mbedtls_calloc(1, hostname_len + 1); |
| 3758 | if (ssl->hostname == NULL) |
| 3759 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 3760 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3761 | memcpy(ssl->hostname, hostname, hostname_len); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 3762 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 3763 | ssl->hostname[hostname_len] = '\0'; |
| 3764 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3765 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3766 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3767 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3768 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3769 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3770 | # if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3771 | void mbedtls_ssl_conf_sni( |
| 3772 | mbedtls_ssl_config *conf, |
| 3773 | int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t), |
| 3774 | void *p_sni) |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3775 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3776 | conf->f_sni = f_sni; |
| 3777 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3778 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3779 | # endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 3780 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3781 | # if defined(MBEDTLS_SSL_ALPN) |
| 3782 | int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, |
| 3783 | const char **protos) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3784 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3785 | size_t cur_len, tot_len; |
| 3786 | const char **p; |
| 3787 | |
| 3788 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3789 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 3790 | * MUST NOT be truncated." |
| 3791 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3792 | */ |
| 3793 | tot_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3794 | for (p = protos; *p != NULL; p++) { |
| 3795 | cur_len = strlen(*p); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3796 | tot_len += cur_len; |
| 3797 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3798 | if ((cur_len == 0) || (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) || |
| 3799 | (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) |
| 3800 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3801 | } |
| 3802 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3803 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 3804 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3805 | return 0; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3808 | const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3809 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3810 | return ssl->alpn_chosen; |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3811 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3812 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3813 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3814 | # if defined(MBEDTLS_SSL_DTLS_SRTP) |
| 3815 | void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf, |
| 3816 | int support_mki_value) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3817 | { |
| 3818 | conf->dtls_srtp_mki_support = support_mki_value; |
| 3819 | } |
| 3820 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3821 | int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl, |
| 3822 | unsigned char *mki_value, |
| 3823 | uint16_t mki_len) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3824 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3825 | if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) { |
| 3826 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3827 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3828 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3829 | if (ssl->conf->dtls_srtp_mki_support == |
| 3830 | MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) { |
| 3831 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 3832 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3833 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3834 | memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3835 | ssl->dtls_srtp_info.mki_len = mki_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3836 | return 0; |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3839 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( |
| 3840 | mbedtls_ssl_config *conf, |
| 3841 | const mbedtls_ssl_srtp_profile *profiles) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3842 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3843 | const mbedtls_ssl_srtp_profile *p; |
| 3844 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3845 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3846 | /* check the profiles list: all entry must be valid, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3847 | * its size cannot be more than the total number of supported profiles, |
| 3848 | * currently 4 */ |
| 3849 | for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 3850 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3851 | p++) { |
| 3852 | if (mbedtls_ssl_check_srtp_profile_value(*p) != |
| 3853 | MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3854 | list_size++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3855 | } else { |
| 3856 | /* unsupported value, stop parsing and set the size to an error |
| 3857 | * value */ |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 3858 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3862 | if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) { |
| 3863 | conf->dtls_srtp_profile_list = NULL; |
| 3864 | conf->dtls_srtp_profile_list_len = 0; |
| 3865 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3866 | } |
| 3867 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 3868 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 3869 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3870 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3871 | return 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3872 | } |
| 3873 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3874 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( |
| 3875 | const mbedtls_ssl_context *ssl, |
| 3876 | mbedtls_dtls_srtp_info *dtls_srtp_info) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3877 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3878 | dtls_srtp_info->chosen_dtls_srtp_profile = |
| 3879 | ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3880 | /* do not copy the mki value if there is no chosen profile */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3881 | if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3882 | dtls_srtp_info->mki_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3883 | } else { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3884 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3885 | memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 3886 | ssl->dtls_srtp_info.mki_len); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 3887 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3888 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3889 | # endif /* MBEDTLS_SSL_DTLS_SRTP */ |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3890 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3891 | void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, |
| 3892 | int major, |
| 3893 | int minor) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3894 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3895 | conf->max_major_ver = major; |
| 3896 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3899 | void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, |
| 3900 | int major, |
| 3901 | int minor) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3902 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3903 | conf->min_major_ver = major; |
| 3904 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3907 | # if defined(MBEDTLS_SSL_SRV_C) |
| 3908 | void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf, |
| 3909 | char cert_req_ca_list) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3910 | { |
| 3911 | conf->cert_req_ca_list = cert_req_ca_list; |
| 3912 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3913 | # endif |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 3914 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3915 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 3916 | void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3917 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3918 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3919 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3920 | # endif |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 3921 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3922 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 3923 | void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3924 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3925 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3926 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3927 | # endif |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 3928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3929 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 3930 | int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, |
| 3931 | unsigned char mfl_code) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3932 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3933 | if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 3934 | ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) { |
| 3935 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3936 | } |
| 3937 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 3938 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3939 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3940 | return 0; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3941 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3942 | # endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 3943 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3944 | void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, |
| 3945 | int allow_legacy) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3946 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3947 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3950 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3951 | void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3952 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3953 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3956 | void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, |
| 3957 | int max_records) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3958 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3959 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3962 | void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf, |
| 3963 | const unsigned char period[8]) |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3964 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3965 | memcpy(conf->renego_period, period, 8); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 3966 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3967 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3968 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3969 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 3970 | # if defined(MBEDTLS_SSL_CLI_C) |
| 3971 | void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3972 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 3973 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3974 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3975 | # endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3976 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3977 | # if defined(MBEDTLS_SSL_SRV_C) |
| 3978 | void mbedtls_ssl_conf_session_tickets_cb( |
| 3979 | mbedtls_ssl_config *conf, |
| 3980 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 3981 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 3982 | void *p_ticket) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3983 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 3984 | conf->f_ticket_write = f_ticket_write; |
| 3985 | conf->f_ticket_parse = f_ticket_parse; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3986 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 3987 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3988 | # endif |
| 3989 | # endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 3990 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3991 | # if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 3992 | void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl, |
| 3993 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 3994 | void *p_export_keys) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 3995 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 3996 | ssl->f_export_keys = f_export_keys; |
| 3997 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 3998 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3999 | # endif |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4000 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4001 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4002 | void mbedtls_ssl_conf_async_private_cb( |
| 4003 | mbedtls_ssl_config *conf, |
| 4004 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4005 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4006 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4007 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4008 | void *async_config_data) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4009 | { |
| 4010 | conf->f_async_sign_start = f_async_sign; |
| 4011 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4012 | conf->f_async_resume = f_async_resume; |
| 4013 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4014 | conf->p_async_config_data = async_config_data; |
| 4015 | } |
| 4016 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4017 | void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf) |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4018 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4019 | return conf->p_async_config_data; |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4020 | } |
| 4021 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4022 | void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4023 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4024 | if (ssl->handshake == NULL) |
| 4025 | return NULL; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4026 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4027 | return ssl->handshake->user_async_ctx; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4030 | void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl, void *ctx) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4031 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4032 | if (ssl->handshake != NULL) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4033 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4034 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4035 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4036 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4037 | /* |
| 4038 | * SSL get accessors |
| 4039 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4040 | uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4041 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4042 | if (ssl->session != NULL) |
| 4043 | return ssl->session->verify_result; |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4044 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4045 | if (ssl->session_negotiate != NULL) |
| 4046 | return ssl->session_negotiate->verify_result; |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4047 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4048 | return 0xFFFFFFFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4051 | const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4052 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4053 | if (ssl == NULL || ssl->session == NULL) |
| 4054 | return NULL; |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4055 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4056 | return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4059 | const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4060 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4061 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4062 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4063 | switch (ssl->minor_ver) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4065 | return "DTLSv1.2"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4066 | |
| 4067 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4068 | return "unknown (DTLS)"; |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4069 | } |
| 4070 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4071 | # endif |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4072 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4073 | switch (ssl->minor_ver) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4074 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4075 | return "TLSv1.2"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4076 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4077 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4078 | return "unknown"; |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4079 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4082 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4083 | size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4084 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 4085 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4086 | size_t read_mfl; |
| 4087 | |
| 4088 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4089 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4090 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) { |
| 4091 | return ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* Check if a smaller max length was negotiated */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4095 | if (ssl->session_out != NULL) { |
| 4096 | read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
| 4097 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4098 | max_len = read_mfl; |
| 4099 | } |
| 4100 | } |
| 4101 | |
| 4102 | // During a handshake, use the value being negotiated |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4103 | if (ssl->session_negotiate != NULL) { |
| 4104 | read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
| 4105 | if (read_mfl < max_len) { |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4106 | max_len = read_mfl; |
| 4107 | } |
| 4108 | } |
| 4109 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4110 | return max_len; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4111 | } |
| 4112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4113 | size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4114 | { |
| 4115 | size_t max_len; |
| 4116 | |
| 4117 | /* |
| 4118 | * Assume mfl_code is correct since it was checked when set |
| 4119 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4120 | max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4121 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4122 | /* Check if a smaller max length was negotiated */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4123 | if (ssl->session_out != NULL && |
| 4124 | ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) { |
| 4125 | max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4128 | /* During a handshake, use the value being negotiated */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4129 | if (ssl->session_negotiate != NULL && |
| 4130 | ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) { |
| 4131 | max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code); |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4134 | return max_len; |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4135 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4136 | # endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4137 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4138 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4139 | size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4140 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4141 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4142 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4143 | (ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4144 | ssl->state == MBEDTLS_SSL_SERVER_HELLO)) |
| 4145 | return 0; |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4146 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4147 | if (ssl->handshake == NULL || ssl->handshake->mtu == 0) |
| 4148 | return ssl->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4149 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4150 | if (ssl->mtu == 0) |
| 4151 | return ssl->handshake->mtu; |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4152 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4153 | return (ssl->mtu < ssl->handshake->mtu ? ssl->mtu : ssl->handshake->mtu); |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4154 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4155 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4156 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4157 | int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4158 | { |
| 4159 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4160 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4161 | # if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4162 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4163 | (void)ssl; |
| 4164 | # endif |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4165 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4166 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4167 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4168 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4169 | if (max_len > mfl) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4170 | max_len = mfl; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4171 | # endif |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4173 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4174 | if (mbedtls_ssl_get_current_mtu(ssl) != 0) { |
| 4175 | const size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 4176 | const int ret = mbedtls_ssl_get_record_expansion(ssl); |
| 4177 | const size_t overhead = (size_t)ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4178 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4179 | if (ret < 0) |
| 4180 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4181 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4182 | if (mtu <= overhead) { |
| 4183 | MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion")); |
| 4184 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4187 | if (max_len > mtu - overhead) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4188 | max_len = mtu - overhead; |
| 4189 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4190 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4191 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4192 | # if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4193 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4194 | ((void)ssl); |
| 4195 | # endif |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4196 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4197 | return (int)max_len; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4198 | } |
| 4199 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4200 | int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl) |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4201 | { |
| 4202 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 4203 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4204 | # if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4205 | (void)ssl; |
| 4206 | # endif |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4207 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4208 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4209 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl); |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4210 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4211 | if (max_len > mfl) |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4212 | max_len = mfl; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4213 | # endif |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4214 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4215 | return (int)max_len; |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4218 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4219 | const mbedtls_x509_crt * |
| 4220 | mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl) |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4221 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4222 | if (ssl == NULL || ssl->session == NULL) |
| 4223 | return NULL; |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4225 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4226 | return ssl->session->peer_cert; |
| 4227 | # else |
| 4228 | return NULL; |
| 4229 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4230 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4231 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4232 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4233 | # if defined(MBEDTLS_SSL_CLI_C) |
| 4234 | int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl, |
| 4235 | mbedtls_ssl_session *dst) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4236 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4237 | int ret; |
| 4238 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4239 | if (ssl == NULL || dst == NULL || ssl->session == NULL || |
| 4240 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 4241 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4242 | } |
| 4243 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4244 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 4245 | * idempotent: Each session can only be exported once. |
| 4246 | * |
| 4247 | * (This is in preparation for TLS 1.3 support where we will |
| 4248 | * need the ability to export multiple sessions (aka tickets), |
| 4249 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 4250 | * multiple times until it fails.) |
| 4251 | * |
| 4252 | * Check whether we have already exported the current session, |
| 4253 | * and fail if so. |
| 4254 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4255 | if (ssl->session->exported == 1) |
| 4256 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4257 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4258 | ret = mbedtls_ssl_session_copy(dst, ssl->session); |
| 4259 | if (ret != 0) |
| 4260 | return ret; |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4261 | |
| 4262 | /* Remember that we've exported the session. */ |
| 4263 | ssl->session->exported = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4264 | return 0; |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4265 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4266 | # endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4267 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4268 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4269 | * Define ticket header determining Mbed TLS version |
| 4270 | * and structure of the ticket. |
| 4271 | */ |
| 4272 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4273 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4274 | * Define bitflag determining compile-time settings influencing |
| 4275 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4276 | */ |
| 4277 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4278 | # if defined(MBEDTLS_HAVE_TIME) |
| 4279 | # define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
| 4280 | # else |
| 4281 | # define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
| 4282 | # endif /* MBEDTLS_HAVE_TIME */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4283 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4284 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4285 | # define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
| 4286 | # else |
| 4287 | # define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
| 4288 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4289 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4290 | # if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4291 | # define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
| 4292 | # else |
| 4293 | # define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
| 4294 | # endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4295 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4296 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4297 | # define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
| 4298 | # else |
| 4299 | # define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
| 4300 | # endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4301 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4302 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4303 | # define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
| 4304 | # else |
| 4305 | # define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
| 4306 | # endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4307 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4308 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4309 | # define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4310 | # else |
| 4311 | # define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4312 | # endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4314 | # define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4315 | # define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4316 | # define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4317 | # define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 4318 | # define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4319 | # define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4321 | # define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
| 4322 | ((uint16_t)((SSL_SERIALIZED_SESSION_CONFIG_TIME \ |
| 4323 | << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \ |
| 4324 | (SSL_SERIALIZED_SESSION_CONFIG_CRT \ |
| 4325 | << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \ |
| 4326 | (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET \ |
| 4327 | << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \ |
| 4328 | (SSL_SERIALIZED_SESSION_CONFIG_MFL \ |
| 4329 | << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \ |
| 4330 | (SSL_SERIALIZED_SESSION_CONFIG_ETM \ |
| 4331 | << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \ |
| 4332 | (SSL_SERIALIZED_SESSION_CONFIG_TICKET \ |
| 4333 | << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT))) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4334 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4335 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4336 | MBEDTLS_VERSION_MAJOR, |
| 4337 | MBEDTLS_VERSION_MINOR, |
| 4338 | MBEDTLS_VERSION_PATCH, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4339 | (SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8) & 0xFF, |
| 4340 | (SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0) & 0xFF, |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4341 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4342 | |
| 4343 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4344 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4345 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4346 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4347 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4348 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4349 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4350 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4351 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4352 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4353 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4354 | * Note: When updating the format, remember to keep |
| 4355 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4356 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4357 | * // In this version, `session_format` determines |
| 4358 | * // the setting of those compile-time |
| 4359 | * // configuration options which influence |
| 4360 | * // the structure of mbedtls_ssl_session. |
| 4361 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 4362 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4363 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 4364 | * |
| 4365 | * select (serialized_session.minor_ver) { |
| 4366 | * |
| 4367 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 4368 | * serialized_session_tls12 data; |
| 4369 | * |
| 4370 | * }; |
| 4371 | * |
| 4372 | * } serialized_session; |
| 4373 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4374 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4376 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4377 | /* Serialization of TLS 1.2 sessions: |
| 4378 | * |
| 4379 | * struct { |
| 4380 | * uint64 start_time; |
| 4381 | * uint8 ciphersuite[2]; // defined by the standard |
| 4382 | * uint8 compression; // 0 or 1 |
| 4383 | * uint8 session_id_len; // at most 32 |
| 4384 | * opaque session_id[32]; |
| 4385 | * opaque master[48]; // fixed length in the standard |
| 4386 | * uint32 verify_result; |
| 4387 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4388 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4389 | * uint32 ticket_lifetime; |
| 4390 | * uint8 mfl_code; // up to 255 according to standard |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4391 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4392 | * } serialized_session_tls12; |
| 4393 | * |
| 4394 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4395 | static size_t ssl_session_save_tls12(const mbedtls_ssl_session *session, |
| 4396 | unsigned char *buf, |
| 4397 | size_t buf_len) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4398 | { |
| 4399 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4400 | size_t used = 0; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4401 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4402 | # if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4403 | uint64_t start; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4404 | # endif |
| 4405 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4406 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4407 | size_t cert_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4408 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4409 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4410 | |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4411 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4412 | * Time |
| 4413 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4414 | # if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4415 | used += 8; |
| 4416 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4417 | if (used <= buf_len) { |
| 4418 | start = (uint64_t)session->start; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4419 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4420 | *p++ = (unsigned char)((start >> 56) & 0xFF); |
| 4421 | *p++ = (unsigned char)((start >> 48) & 0xFF); |
| 4422 | *p++ = (unsigned char)((start >> 40) & 0xFF); |
| 4423 | *p++ = (unsigned char)((start >> 32) & 0xFF); |
| 4424 | *p++ = (unsigned char)((start >> 24) & 0xFF); |
| 4425 | *p++ = (unsigned char)((start >> 16) & 0xFF); |
| 4426 | *p++ = (unsigned char)((start >> 8) & 0xFF); |
| 4427 | *p++ = (unsigned char)((start)&0xFF); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4428 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4429 | # endif /* MBEDTLS_HAVE_TIME */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4430 | |
| 4431 | /* |
| 4432 | * Basic mandatory fields |
| 4433 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4434 | used += 2 /* ciphersuite */ |
| 4435 | + 1 /* compression */ |
| 4436 | + 1 /* id_len */ |
| 4437 | + sizeof(session->id) + sizeof(session->master) + |
| 4438 | 4; /* verify_result |
| 4439 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4440 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4441 | if (used <= buf_len) { |
| 4442 | *p++ = (unsigned char)((session->ciphersuite >> 8) & 0xFF); |
| 4443 | *p++ = (unsigned char)((session->ciphersuite) & 0xFF); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4444 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4445 | *p++ = (unsigned char)(session->compression & 0xFF); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4446 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4447 | *p++ = (unsigned char)(session->id_len & 0xFF); |
| 4448 | memcpy(p, session->id, 32); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4449 | p += 32; |
| 4450 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4451 | memcpy(p, session->master, 48); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4452 | p += 48; |
| 4453 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4454 | *p++ = (unsigned char)((session->verify_result >> 24) & 0xFF); |
| 4455 | *p++ = (unsigned char)((session->verify_result >> 16) & 0xFF); |
| 4456 | *p++ = (unsigned char)((session->verify_result >> 8) & 0xFF); |
| 4457 | *p++ = (unsigned char)((session->verify_result) & 0xFF); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4458 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4459 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4460 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4461 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4462 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4463 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4464 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4465 | if (session->peer_cert == NULL) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4466 | cert_len = 0; |
| 4467 | else |
| 4468 | cert_len = session->peer_cert->raw.len; |
| 4469 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4470 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4471 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4472 | if (used <= buf_len) { |
| 4473 | *p++ = (unsigned char)((cert_len >> 16) & 0xFF); |
| 4474 | *p++ = (unsigned char)((cert_len >> 8) & 0xFF); |
| 4475 | *p++ = (unsigned char)((cert_len)&0xFF); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4476 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4477 | if (session->peer_cert != NULL) { |
| 4478 | memcpy(p, session->peer_cert->raw.p, cert_len); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4479 | p += cert_len; |
| 4480 | } |
| 4481 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4482 | # else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4483 | if (session->peer_cert_digest != NULL) { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4484 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4485 | if (used <= buf_len) { |
| 4486 | *p++ = (unsigned char)session->peer_cert_digest_type; |
| 4487 | *p++ = (unsigned char)session->peer_cert_digest_len; |
| 4488 | memcpy(p, session->peer_cert_digest, session->peer_cert_digest_len); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4489 | p += session->peer_cert_digest_len; |
| 4490 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4491 | } else { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4492 | used += 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4493 | if (used <= buf_len) { |
| 4494 | *p++ = (unsigned char)MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4495 | *p++ = 0; |
| 4496 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4497 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4498 | # endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4499 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4500 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4501 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4502 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4503 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4504 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4505 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4506 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4507 | if (used <= buf_len) { |
| 4508 | *p++ = (unsigned char)((session->ticket_len >> 16) & 0xFF); |
| 4509 | *p++ = (unsigned char)((session->ticket_len >> 8) & 0xFF); |
| 4510 | *p++ = (unsigned char)((session->ticket_len) & 0xFF); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4511 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4512 | if (session->ticket != NULL) { |
| 4513 | memcpy(p, session->ticket, session->ticket_len); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4514 | p += session->ticket_len; |
| 4515 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4516 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4517 | *p++ = (unsigned char)((session->ticket_lifetime >> 24) & 0xFF); |
| 4518 | *p++ = (unsigned char)((session->ticket_lifetime >> 16) & 0xFF); |
| 4519 | *p++ = (unsigned char)((session->ticket_lifetime >> 8) & 0xFF); |
| 4520 | *p++ = (unsigned char)((session->ticket_lifetime) & 0xFF); |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4521 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4522 | # endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4523 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4524 | /* |
| 4525 | * Misc extension-related info |
| 4526 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4527 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4528 | used += 1; |
| 4529 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4530 | if (used <= buf_len) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4531 | *p++ = session->mfl_code; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4532 | # endif |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4533 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4534 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4535 | used += 1; |
| 4536 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4537 | if (used <= buf_len) |
| 4538 | *p++ = (unsigned char)((session->encrypt_then_mac) & 0xFF); |
| 4539 | # endif |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4540 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4541 | return used; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4542 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4543 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4544 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4545 | static int ssl_session_save(const mbedtls_ssl_session *session, |
| 4546 | unsigned char omit_header, |
| 4547 | unsigned char *buf, |
| 4548 | size_t buf_len, |
| 4549 | size_t *olen) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4550 | { |
| 4551 | unsigned char *p = buf; |
| 4552 | size_t used = 0; |
| 4553 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4554 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4555 | /* |
| 4556 | * Add Mbed TLS version identifier |
| 4557 | */ |
| 4558 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4559 | used += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4560 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4561 | if (used <= buf_len) { |
| 4562 | memcpy(p, ssl_serialized_session_header, |
| 4563 | sizeof(ssl_serialized_session_header)); |
| 4564 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | /* |
| 4569 | * TLS version identifier |
| 4570 | */ |
| 4571 | used += 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4572 | if (used <= buf_len) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4573 | *p++ = session->minor_ver; |
| 4574 | } |
| 4575 | |
| 4576 | /* Forward to version-specific serialization routine. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4577 | switch (session->minor_ver) { |
| 4578 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4579 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 4580 | { |
| 4581 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 4582 | used += ssl_session_save_tls12(session, p, remaining_len); |
| 4583 | break; |
| 4584 | } |
| 4585 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4586 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4587 | default: |
| 4588 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | *olen = used; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4592 | if (used > buf_len) |
| 4593 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4594 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4595 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4599 | * Public wrapper for ssl_session_save() |
| 4600 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4601 | int mbedtls_ssl_session_save(const mbedtls_ssl_session *session, |
| 4602 | unsigned char *buf, |
| 4603 | size_t buf_len, |
| 4604 | size_t *olen) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4605 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4606 | return ssl_session_save(session, 0, buf, buf_len, olen); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4610 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4611 | * |
| 4612 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4613 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4614 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4615 | static int ssl_session_load_tls12(mbedtls_ssl_session *session, |
| 4616 | const unsigned char *buf, |
| 4617 | size_t len) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4618 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4619 | # if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4620 | uint64_t start; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4621 | # endif |
| 4622 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4623 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4624 | size_t cert_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4625 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4626 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4627 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4628 | const unsigned char *p = buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4629 | const unsigned char *const end = buf + len; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4630 | |
| 4631 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4632 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4633 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4634 | # if defined(MBEDTLS_HAVE_TIME) |
| 4635 | if (8 > (size_t)(end - p)) |
| 4636 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4637 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4638 | start = ((uint64_t)p[0] << 56) | ((uint64_t)p[1] << 48) | |
| 4639 | ((uint64_t)p[2] << 40) | ((uint64_t)p[3] << 32) | |
| 4640 | ((uint64_t)p[4] << 24) | ((uint64_t)p[5] << 16) | |
| 4641 | ((uint64_t)p[6] << 8) | ((uint64_t)p[7]); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4642 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4643 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4644 | session->start = (time_t)start; |
| 4645 | # endif /* MBEDTLS_HAVE_TIME */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4646 | |
| 4647 | /* |
| 4648 | * Basic mandatory fields |
| 4649 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4650 | if (2 + 1 + 1 + 32 + 48 + 4 > (size_t)(end - p)) |
| 4651 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4652 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4653 | session->ciphersuite = (p[0] << 8) | p[1]; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4654 | p += 2; |
| 4655 | |
| 4656 | session->compression = *p++; |
| 4657 | |
| 4658 | session->id_len = *p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4659 | memcpy(session->id, p, 32); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4660 | p += 32; |
| 4661 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4662 | memcpy(session->master, p, 48); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4663 | p += 48; |
| 4664 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4665 | session->verify_result = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | |
| 4666 | ((uint32_t)p[2] << 8) | ((uint32_t)p[3]); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4667 | p += 4; |
| 4668 | |
| 4669 | /* Immediately clear invalid pointer values that have been read, in case |
| 4670 | * we exit early before we replaced them with valid ones. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4671 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4672 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4673 | session->peer_cert = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4674 | # else |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4675 | session->peer_cert_digest = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4676 | # endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4677 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4678 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4679 | session->ticket = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4680 | # endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4681 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4682 | /* |
| 4683 | * Peer certificate |
| 4684 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4685 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4686 | # if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4687 | /* Deserialize CRT from the end of the ticket. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4688 | if (3 > (size_t)(end - p)) |
| 4689 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4690 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4691 | cert_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4692 | p += 3; |
| 4693 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4694 | if (cert_len != 0) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4696 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4697 | if (cert_len > (size_t)(end - p)) |
| 4698 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4699 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4700 | session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4701 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4702 | if (session->peer_cert == NULL) |
| 4703 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4704 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4705 | mbedtls_x509_crt_init(session->peer_cert); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4706 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4707 | if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert, p, |
| 4708 | cert_len)) != 0) { |
| 4709 | mbedtls_x509_crt_free(session->peer_cert); |
| 4710 | mbedtls_free(session->peer_cert); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4711 | session->peer_cert = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4712 | return ret; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | p += cert_len; |
| 4716 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4717 | # else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4718 | /* Deserialize CRT digest from the end of the ticket. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4719 | if (2 > (size_t)(end - p)) |
| 4720 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4721 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4722 | session->peer_cert_digest_type = (mbedtls_md_type_t)*p++; |
| 4723 | session->peer_cert_digest_len = (size_t)*p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4724 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4725 | if (session->peer_cert_digest_len != 0) { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4726 | const mbedtls_md_info_t *md_info = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4727 | mbedtls_md_info_from_type(session->peer_cert_digest_type); |
| 4728 | if (md_info == NULL) |
| 4729 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4730 | if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) |
| 4731 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4732 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4733 | if (session->peer_cert_digest_len > (size_t)(end - p)) |
| 4734 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4735 | |
| 4736 | session->peer_cert_digest = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4737 | mbedtls_calloc(1, session->peer_cert_digest_len); |
| 4738 | if (session->peer_cert_digest == NULL) |
| 4739 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4740 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4741 | memcpy(session->peer_cert_digest, p, session->peer_cert_digest_len); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4742 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4743 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4744 | # endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4745 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4746 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4747 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4748 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4749 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4750 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4751 | if (3 > (size_t)(end - p)) |
| 4752 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4753 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4754 | session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2]; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4755 | p += 3; |
| 4756 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4757 | if (session->ticket_len != 0) { |
| 4758 | if (session->ticket_len > (size_t)(end - p)) |
| 4759 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4760 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4761 | session->ticket = mbedtls_calloc(1, session->ticket_len); |
| 4762 | if (session->ticket == NULL) |
| 4763 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4764 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4765 | memcpy(session->ticket, p, session->ticket_len); |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4766 | p += session->ticket_len; |
| 4767 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4768 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4769 | if (4 > (size_t)(end - p)) |
| 4770 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4771 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4772 | session->ticket_lifetime = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | |
| 4773 | ((uint32_t)p[2] << 8) | ((uint32_t)p[3]); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4774 | p += 4; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4775 | # endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4776 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4777 | /* |
| 4778 | * Misc extension-related info |
| 4779 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4780 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4781 | if (1 > (size_t)(end - p)) |
| 4782 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4783 | |
| 4784 | session->mfl_code = *p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4785 | # endif |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4787 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4788 | if (1 > (size_t)(end - p)) |
| 4789 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4790 | |
| 4791 | session->encrypt_then_mac = *p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4792 | # endif |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4793 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4794 | /* Done, should have consumed entire buffer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4795 | if (p != end) |
| 4796 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4797 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4798 | return 0; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4801 | static int ssl_session_load(mbedtls_ssl_session *session, |
| 4802 | unsigned char omit_header, |
| 4803 | const unsigned char *buf, |
| 4804 | size_t len) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4805 | { |
| 4806 | const unsigned char *p = buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4807 | const unsigned char *const end = buf + len; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4808 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4809 | if (!omit_header) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4810 | /* |
| 4811 | * Check Mbed TLS version identifier |
| 4812 | */ |
| 4813 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4814 | if ((size_t)(end - p) < sizeof(ssl_serialized_session_header)) |
| 4815 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4816 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4817 | if (memcmp(p, ssl_serialized_session_header, |
| 4818 | sizeof(ssl_serialized_session_header)) != 0) { |
| 4819 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4820 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4821 | p += sizeof(ssl_serialized_session_header); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * TLS version identifier |
| 4826 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4827 | if (1 > (size_t)(end - p)) |
| 4828 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4829 | session->minor_ver = *p++; |
| 4830 | |
| 4831 | /* Dispatch according to TLS version. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4832 | switch (session->minor_ver) { |
| 4833 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4834 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 4835 | { |
| 4836 | size_t remaining_len = (end - p); |
| 4837 | return ssl_session_load_tls12(session, p, remaining_len); |
| 4838 | } |
| 4839 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4840 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4841 | default: |
| 4842 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4843 | } |
| 4844 | } |
| 4845 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4846 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 4847 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4848 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4849 | int mbedtls_ssl_session_load(mbedtls_ssl_session *session, |
| 4850 | const unsigned char *buf, |
| 4851 | size_t len) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4852 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4853 | int ret = ssl_session_load(session, 0, buf, len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4854 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4855 | if (ret != 0) |
| 4856 | mbedtls_ssl_session_free(session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4857 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4858 | return ret; |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4862 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4863 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4864 | int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4866 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4867 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4868 | if (ssl == NULL || ssl->conf == NULL) |
| 4869 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4870 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4871 | # if defined(MBEDTLS_SSL_CLI_C) |
| 4872 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) |
| 4873 | ret = mbedtls_ssl_handshake_client_step(ssl); |
| 4874 | # endif |
| 4875 | # if defined(MBEDTLS_SSL_SRV_C) |
| 4876 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) |
| 4877 | ret = mbedtls_ssl_handshake_server_step(ssl); |
| 4878 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4879 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4880 | return ret; |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | /* |
| 4884 | * Perform the SSL handshake |
| 4885 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4886 | int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4887 | { |
| 4888 | int ret = 0; |
| 4889 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4890 | /* Sanity checks */ |
| 4891 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4892 | if (ssl == NULL || ssl->conf == NULL) |
| 4893 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4894 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4895 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4896 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4897 | (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) { |
| 4898 | MBEDTLS_SSL_DEBUG_MSG(1, ("You must use " |
| 4899 | "mbedtls_ssl_set_timer_cb() for DTLS")); |
| 4900 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4901 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4902 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4903 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4904 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake")); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4905 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 4906 | /* Main handshake loop */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4907 | while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4908 | ret = mbedtls_ssl_handshake_step(ssl); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4909 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4910 | if (ret != 0) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 4911 | break; |
| 4912 | } |
| 4913 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4914 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4915 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4916 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4919 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4920 | # if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4921 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4922 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4923 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4924 | static int ssl_write_hello_request(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4925 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4926 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4927 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4928 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4929 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4930 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4931 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4932 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4933 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4934 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4935 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4936 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4937 | } |
| 4938 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4939 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request")); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4940 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4941 | return 0; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4942 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4943 | # endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4944 | |
| 4945 | /* |
| 4946 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4947 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 4948 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4949 | * - server: receiving any handshake message on server during mbedtls_ssl_read() |
| 4950 | * after the initial handshake is completed. If the handshake doesn't complete |
| 4951 | * due to waiting for I/O, it will continue during the next calls to |
| 4952 | * mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4953 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4954 | int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4955 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4956 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4957 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4958 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4959 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4960 | if ((ret = ssl_handshake_init(ssl)) != 0) |
| 4961 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4962 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4963 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 4964 | * the ServerHello will have message_seq = 1" */ |
| 4965 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4966 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4967 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4968 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4969 | ssl->handshake->out_msg_seq = 1; |
| 4970 | else |
| 4971 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4972 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4973 | # endif |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 4974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4975 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 4976 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4977 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4978 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 4979 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4980 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4983 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4984 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4985 | return 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4989 | * Renegotiate current connection on client, |
| 4990 | * or request renegotiation on server |
| 4991 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4992 | int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4993 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4994 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 4995 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4996 | if (ssl == NULL || ssl->conf == NULL) |
| 4997 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4998 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4999 | # if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5000 | /* On server, just send the request */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5001 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 5002 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) |
| 5003 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5005 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5006 | |
| 5007 | /* Did we already try/start sending HelloRequest? */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5008 | if (ssl->out_left != 0) |
| 5009 | return mbedtls_ssl_flush_output(ssl); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5010 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5011 | return ssl_write_hello_request(ssl); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5012 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5013 | # endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5014 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5015 | # if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5016 | /* |
| 5017 | * On client, either start the renegotiation process or, |
| 5018 | * if already in progress, continue the handshake |
| 5019 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5020 | if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) { |
| 5021 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) |
| 5022 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5023 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5024 | if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) { |
| 5025 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 5026 | return ret; |
| 5027 | } |
| 5028 | } else { |
| 5029 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 5030 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5031 | return ret; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5032 | } |
| 5033 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5034 | # endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5035 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5036 | return ret; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5037 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5038 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5039 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5040 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5041 | static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5042 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5043 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5044 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5045 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5046 | next = cur->next; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5047 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5048 | cur = next; |
| 5049 | } |
| 5050 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5051 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5052 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5053 | void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5054 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5055 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5056 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5057 | if (handshake == NULL) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5058 | return; |
| 5059 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5060 | # if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5061 | if (ssl->conf->f_async_cancel != NULL && |
| 5062 | handshake->async_in_progress != 0) { |
| 5063 | ssl->conf->f_async_cancel(ssl); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5064 | handshake->async_in_progress = 0; |
| 5065 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5066 | # endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5067 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5068 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5069 | # if defined(MBEDTLS_SHA256_C) |
| 5070 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5071 | psa_hash_abort(&handshake->fin_sha256_psa); |
| 5072 | # else |
| 5073 | mbedtls_sha256_free(&handshake->fin_sha256); |
| 5074 | # endif |
| 5075 | # endif |
| 5076 | # if defined(MBEDTLS_SHA384_C) |
| 5077 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5078 | psa_hash_abort(&handshake->fin_sha384_psa); |
| 5079 | # else |
| 5080 | mbedtls_sha512_free(&handshake->fin_sha512); |
| 5081 | # endif |
| 5082 | # endif |
| 5083 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5084 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5085 | # if defined(MBEDTLS_DHM_C) |
| 5086 | mbedtls_dhm_free(&handshake->dhm_ctx); |
| 5087 | # endif |
| 5088 | # if defined(MBEDTLS_ECDH_C) |
| 5089 | mbedtls_ecdh_free(&handshake->ecdh_ctx); |
| 5090 | # endif |
| 5091 | # if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 5092 | mbedtls_ecjpake_free(&handshake->ecjpake_ctx); |
| 5093 | # if defined(MBEDTLS_SSL_CLI_C) |
| 5094 | mbedtls_free(handshake->ecjpake_cache); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5095 | handshake->ecjpake_cache = NULL; |
| 5096 | handshake->ecjpake_cache_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5097 | # endif |
| 5098 | # endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5099 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5100 | # if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5101 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5102 | /* explicit void pointer cast for buggy MS compiler */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5103 | mbedtls_free((void *)handshake->curves); |
| 5104 | # endif |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5106 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 5107 | if (handshake->psk != NULL) { |
| 5108 | mbedtls_platform_zeroize(handshake->psk, handshake->psk_len); |
| 5109 | mbedtls_free(handshake->psk); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5110 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5111 | # endif |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5113 | # if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5114 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5115 | /* |
| 5116 | * Free only the linked list wrapper, not the keys themselves |
| 5117 | * since the belong to the SNI callback |
| 5118 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5119 | if (handshake->sni_key_cert != NULL) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5120 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5121 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5122 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5123 | next = cur->next; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5124 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5125 | cur = next; |
| 5126 | } |
| 5127 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5128 | # endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION \ |
| 5129 | */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5130 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5131 | # if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 5132 | mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx); |
| 5133 | if (handshake->ecrs_peer_cert != NULL) { |
| 5134 | mbedtls_x509_crt_free(handshake->ecrs_peer_cert); |
| 5135 | mbedtls_free(handshake->ecrs_peer_cert); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5136 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5137 | # endif |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5139 | # if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5140 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5141 | mbedtls_pk_free(&handshake->peer_pubkey); |
| 5142 | # endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE \ |
| 5143 | */ |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5145 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5146 | mbedtls_free(handshake->verify_cookie); |
| 5147 | mbedtls_ssl_flight_free(handshake->flight); |
| 5148 | mbedtls_ssl_buffering_free(ssl); |
| 5149 | # endif |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5150 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5151 | # if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5152 | psa_destroy_key(handshake->ecdh_psa_privkey); |
| 5153 | # endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5155 | mbedtls_platform_zeroize(handshake, sizeof(mbedtls_ssl_handshake_params)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5156 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5157 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5158 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5159 | * processes datagrams and the fact that a datagram is allowed to have |
| 5160 | * several records in it, it is possible that the I/O buffers are not |
| 5161 | * empty at this stage */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5162 | handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl), |
| 5163 | mbedtls_ssl_get_output_buflen(ssl)); |
| 5164 | # endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5167 | void mbedtls_ssl_session_free(mbedtls_ssl_session *session) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5168 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5169 | if (session == NULL) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5170 | return; |
| 5171 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5172 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5173 | ssl_clear_peer_cert(session); |
| 5174 | # endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5176 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5177 | mbedtls_free(session->ticket); |
| 5178 | # endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5179 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5180 | mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5183 | # if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5184 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5185 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5186 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5187 | # else |
| 5188 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5189 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5190 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5191 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5192 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5193 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5194 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5195 | # else |
| 5196 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5197 | # endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5198 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5199 | # if defined(MBEDTLS_SSL_ALPN) |
| 5200 | # define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5201 | # else |
| 5202 | # define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5203 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5204 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5205 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5206 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5207 | # define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5208 | # define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5209 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5210 | # define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5211 | ((uint32_t)((SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID \ |
| 5212 | << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \ |
| 5213 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT \ |
| 5214 | << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \ |
| 5215 | (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY \ |
| 5216 | << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \ |
| 5217 | (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN \ |
| 5218 | << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \ |
| 5219 | 0u)) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5220 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5221 | static unsigned char ssl_serialized_context_header[] = { |
| 5222 | MBEDTLS_VERSION_MAJOR, |
| 5223 | MBEDTLS_VERSION_MINOR, |
| 5224 | MBEDTLS_VERSION_PATCH, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5225 | (SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8) & 0xFF, |
| 5226 | (SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0) & 0xFF, |
| 5227 | (SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16) & 0xFF, |
| 5228 | (SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8) & 0xFF, |
| 5229 | (SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0) & 0xFF, |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5230 | }; |
| 5231 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5232 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5233 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5234 | * |
| 5235 | * The format of the serialized data is: |
| 5236 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5237 | * |
| 5238 | * // header |
| 5239 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5240 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5241 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5242 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5243 | * Note: When updating the format, remember to keep these |
| 5244 | * version+format bytes. (We may make their size part of the API.) |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5245 | * |
| 5246 | * // session sub-structure |
| 5247 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5248 | * // transform sub-structure |
| 5249 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5250 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5251 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5252 | * // fields from ssl_context |
| 5253 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5254 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5255 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5256 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5257 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5258 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5259 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5260 | * |
| 5261 | * Note that many fields of the ssl_context or sub-structures are not |
| 5262 | * serialized, as they fall in one of the following categories: |
| 5263 | * |
| 5264 | * 1. forced value (eg in_left must be 0) |
| 5265 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5266 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5267 | * 4. value was temporary (eg content of input buffer) |
| 5268 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5269 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5270 | int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl, |
| 5271 | unsigned char *buf, |
| 5272 | size_t buf_len, |
| 5273 | size_t *olen) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5274 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5275 | unsigned char *p = buf; |
| 5276 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5277 | size_t session_len; |
| 5278 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5279 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5280 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5281 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5282 | * this function's documentation. |
| 5283 | * |
| 5284 | * These are due to assumptions/limitations in the implementation. Some of |
| 5285 | * them are likely to stay (no handshake in progress) some might go away |
| 5286 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5287 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5288 | /* The initial handshake must be over */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5289 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5290 | MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over")); |
| 5291 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5292 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5293 | if (ssl->handshake != NULL) { |
| 5294 | MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed")); |
| 5295 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5296 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5297 | /* Double-check that sub-structures are indeed ready */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5298 | if (ssl->transform == NULL || ssl->session == NULL) { |
| 5299 | MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready")); |
| 5300 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5301 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5302 | /* There must be no pending incoming or outgoing data */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5303 | if (mbedtls_ssl_check_pending(ssl) != 0) { |
| 5304 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data")); |
| 5305 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5306 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5307 | if (ssl->out_left != 0) { |
| 5308 | MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data")); |
| 5309 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5310 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5311 | /* Protocol must be DLTS, not TLS */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5312 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5313 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported")); |
| 5314 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5315 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5316 | /* Version must be 1.2 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5317 | if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) { |
| 5318 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5319 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5320 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5321 | if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) { |
| 5322 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported")); |
| 5323 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5324 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5325 | /* We must be using an AEAD ciphersuite */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5326 | if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) { |
| 5327 | MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported")); |
| 5328 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5329 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5330 | /* Renegotiation must not be enabled */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5331 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5332 | if (ssl->conf->disable_renegotiation != |
| 5333 | MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5334 | MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled")); |
| 5335 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5336 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5337 | # endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5338 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5339 | /* |
| 5340 | * Version and format identifier |
| 5341 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5342 | used += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5343 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5344 | if (used <= buf_len) { |
| 5345 | memcpy(p, ssl_serialized_context_header, |
| 5346 | sizeof(ssl_serialized_context_header)); |
| 5347 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5351 | * Session (length + data) |
| 5352 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5353 | ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len); |
| 5354 | if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) |
| 5355 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5356 | |
| 5357 | used += 4 + session_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5358 | if (used <= buf_len) { |
| 5359 | *p++ = (unsigned char)((session_len >> 24) & 0xFF); |
| 5360 | *p++ = (unsigned char)((session_len >> 16) & 0xFF); |
| 5361 | *p++ = (unsigned char)((session_len >> 8) & 0xFF); |
| 5362 | *p++ = (unsigned char)((session_len)&0xFF); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5363 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5364 | ret = ssl_session_save(ssl->session, 1, p, session_len, &session_len); |
| 5365 | if (ret != 0) |
| 5366 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5367 | |
| 5368 | p += session_len; |
| 5369 | } |
| 5370 | |
| 5371 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5372 | * Transform |
| 5373 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5374 | used += sizeof(ssl->transform->randbytes); |
| 5375 | if (used <= buf_len) { |
| 5376 | memcpy(p, ssl->transform->randbytes, sizeof(ssl->transform->randbytes)); |
| 5377 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5378 | } |
| 5379 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5380 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5381 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5382 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5383 | *p++ = ssl->transform->in_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5384 | memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5385 | p += ssl->transform->in_cid_len; |
| 5386 | |
| 5387 | *p++ = ssl->transform->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5388 | memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5389 | p += ssl->transform->out_cid_len; |
| 5390 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5391 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5392 | |
| 5393 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5394 | * Saved fields from top-level ssl_context structure |
| 5395 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5396 | used += 4; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5397 | if (used <= buf_len) { |
| 5398 | *p++ = (unsigned char)((ssl->badmac_seen >> 24) & 0xFF); |
| 5399 | *p++ = (unsigned char)((ssl->badmac_seen >> 16) & 0xFF); |
| 5400 | *p++ = (unsigned char)((ssl->badmac_seen >> 8) & 0xFF); |
| 5401 | *p++ = (unsigned char)((ssl->badmac_seen) & 0xFF); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5402 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5403 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5404 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5405 | used += 16; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5406 | if (used <= buf_len) { |
| 5407 | *p++ = (unsigned char)((ssl->in_window_top >> 56) & 0xFF); |
| 5408 | *p++ = (unsigned char)((ssl->in_window_top >> 48) & 0xFF); |
| 5409 | *p++ = (unsigned char)((ssl->in_window_top >> 40) & 0xFF); |
| 5410 | *p++ = (unsigned char)((ssl->in_window_top >> 32) & 0xFF); |
| 5411 | *p++ = (unsigned char)((ssl->in_window_top >> 24) & 0xFF); |
| 5412 | *p++ = (unsigned char)((ssl->in_window_top >> 16) & 0xFF); |
| 5413 | *p++ = (unsigned char)((ssl->in_window_top >> 8) & 0xFF); |
| 5414 | *p++ = (unsigned char)((ssl->in_window_top) & 0xFF); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5415 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5416 | *p++ = (unsigned char)((ssl->in_window >> 56) & 0xFF); |
| 5417 | *p++ = (unsigned char)((ssl->in_window >> 48) & 0xFF); |
| 5418 | *p++ = (unsigned char)((ssl->in_window >> 40) & 0xFF); |
| 5419 | *p++ = (unsigned char)((ssl->in_window >> 32) & 0xFF); |
| 5420 | *p++ = (unsigned char)((ssl->in_window >> 24) & 0xFF); |
| 5421 | *p++ = (unsigned char)((ssl->in_window >> 16) & 0xFF); |
| 5422 | *p++ = (unsigned char)((ssl->in_window >> 8) & 0xFF); |
| 5423 | *p++ = (unsigned char)((ssl->in_window) & 0xFF); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5424 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5425 | # endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5426 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5427 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5428 | used += 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5429 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5430 | *p++ = ssl->disable_datagram_packing; |
| 5431 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5432 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5433 | |
| 5434 | used += 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5435 | if (used <= buf_len) { |
| 5436 | memcpy(p, ssl->cur_out_ctr, 8); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5437 | p += 8; |
| 5438 | } |
| 5439 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5440 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5441 | used += 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5442 | if (used <= buf_len) { |
| 5443 | *p++ = (unsigned char)((ssl->mtu >> 8) & 0xFF); |
| 5444 | *p++ = (unsigned char)((ssl->mtu) & 0xFF); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5445 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5446 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5447 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5448 | # if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5449 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5450 | const uint8_t alpn_len = |
| 5451 | ssl->alpn_chosen ? (uint8_t)strlen(ssl->alpn_chosen) : 0; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5452 | |
| 5453 | used += 1 + alpn_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5454 | if (used <= buf_len) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5455 | *p++ = alpn_len; |
| 5456 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5457 | if (ssl->alpn_chosen != NULL) { |
| 5458 | memcpy(p, ssl->alpn_chosen, alpn_len); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5459 | p += alpn_len; |
| 5460 | } |
| 5461 | } |
| 5462 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5463 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5464 | |
| 5465 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5466 | * Done |
| 5467 | */ |
| 5468 | *olen = used; |
| 5469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5470 | if (used > buf_len) |
| 5471 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5472 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5473 | MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5474 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5475 | return mbedtls_ssl_session_reset_int(ssl, 0); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5479 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 5480 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 5481 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5482 | typedef int (*tls_prf_fn)(const unsigned char *secret, |
| 5483 | size_t slen, |
| 5484 | const char *label, |
| 5485 | const unsigned char *random, |
| 5486 | size_t rlen, |
| 5487 | unsigned char *dstbuf, |
| 5488 | size_t dlen); |
| 5489 | static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5490 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5491 | # if defined(MBEDTLS_SHA384_C) |
| 5492 | const mbedtls_ssl_ciphersuite_t *const ciphersuite_info = |
| 5493 | mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5494 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5495 | if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) |
| 5496 | return tls_prf_sha384; |
| 5497 | # else |
| 5498 | (void)ciphersuite_id; |
| 5499 | # endif |
| 5500 | return tls_prf_sha256; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5504 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5505 | * |
| 5506 | * This internal version is wrapped by a public function that cleans up in |
| 5507 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5508 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5509 | static int |
| 5510 | ssl_context_load(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5511 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5512 | const unsigned char *p = buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5513 | const unsigned char *const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5514 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5515 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5516 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5517 | /* |
| 5518 | * The context should have been freshly setup or reset. |
| 5519 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 5520 | * (Checking session is useful because it won't be NULL if we're |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5521 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 5522 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5523 | if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST || ssl->session != NULL) { |
| 5524 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5525 | } |
| 5526 | |
| 5527 | /* |
| 5528 | * We can't check that the config matches the initial one, but we can at |
| 5529 | * least check it matches the requirements for serializing. |
| 5530 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5531 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5532 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 5533 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 5534 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 5535 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5536 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5537 | ssl->conf->disable_renegotiation != |
| 5538 | MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5539 | # endif |
| 5540 | 0) { |
| 5541 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 5542 | } |
| 5543 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5544 | MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5545 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5546 | /* |
| 5547 | * Check version identifier |
| 5548 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5549 | if ((size_t)(end - p) < sizeof(ssl_serialized_context_header)) |
| 5550 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5551 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5552 | if (memcmp(p, ssl_serialized_context_header, |
| 5553 | sizeof(ssl_serialized_context_header)) != 0) { |
| 5554 | return MBEDTLS_ERR_SSL_VERSION_MISMATCH; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5555 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5556 | p += sizeof(ssl_serialized_context_header); |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5557 | |
| 5558 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5559 | * Session |
| 5560 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5561 | if ((size_t)(end - p) < 4) |
| 5562 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5563 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5564 | session_len = ((size_t)p[0] << 24) | ((size_t)p[1] << 16) | |
| 5565 | ((size_t)p[2] << 8) | ((size_t)p[3]); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5566 | p += 4; |
| 5567 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5568 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5569 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5570 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5571 | ssl->session_in = ssl->session; |
| 5572 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5573 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5574 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5575 | if ((size_t)(end - p) < session_len) |
| 5576 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5577 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5578 | ret = ssl_session_load(ssl->session, 1, p, session_len); |
| 5579 | if (ret != 0) { |
| 5580 | mbedtls_ssl_session_free(ssl->session); |
| 5581 | return ret; |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5582 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5583 | |
| 5584 | p += session_len; |
| 5585 | |
| 5586 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5587 | * Transform |
| 5588 | */ |
| 5589 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5590 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 5591 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5592 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5593 | ssl->transform_in = ssl->transform; |
| 5594 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 5595 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5596 | |
| 5597 | /* Read random bytes and populate structure */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5598 | if ((size_t)(end - p) < sizeof(ssl->transform->randbytes)) |
| 5599 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5600 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5601 | ret = ssl_populate_transform( |
| 5602 | ssl->transform, ssl->session->ciphersuite, ssl->session->master, |
| 5603 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 5604 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5605 | ssl->session->encrypt_then_mac, |
| 5606 | # endif |
| 5607 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 5608 | ssl_tls12prf_from_cs(ssl->session->ciphersuite), p, /* currently |
| 5609 | pointing to |
| 5610 | randbytes */ |
| 5611 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 5612 | ssl->conf->endpoint, ssl); |
| 5613 | if (ret != 0) |
| 5614 | return ret; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5615 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5616 | p += sizeof(ssl->transform->randbytes); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5617 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5618 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5619 | /* Read connection IDs and store them */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5620 | if ((size_t)(end - p) < 1) |
| 5621 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5622 | |
| 5623 | ssl->transform->in_cid_len = *p++; |
| 5624 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5625 | if ((size_t)(end - p) < ssl->transform->in_cid_len + 1u) |
| 5626 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5627 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5628 | memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5629 | p += ssl->transform->in_cid_len; |
| 5630 | |
| 5631 | ssl->transform->out_cid_len = *p++; |
| 5632 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5633 | if ((size_t)(end - p) < ssl->transform->out_cid_len) |
| 5634 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5635 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5636 | memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len); |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5637 | p += ssl->transform->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5638 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5639 | |
| 5640 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5641 | * Saved fields from top-level ssl_context structure |
| 5642 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5643 | if ((size_t)(end - p) < 4) |
| 5644 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5645 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5646 | ssl->badmac_seen = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | |
| 5647 | ((uint32_t)p[2] << 8) | ((uint32_t)p[3]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5648 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5649 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5650 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5651 | if ((size_t)(end - p) < 16) |
| 5652 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5653 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5654 | ssl->in_window_top = ((uint64_t)p[0] << 56) | ((uint64_t)p[1] << 48) | |
| 5655 | ((uint64_t)p[2] << 40) | ((uint64_t)p[3] << 32) | |
| 5656 | ((uint64_t)p[4] << 24) | ((uint64_t)p[5] << 16) | |
| 5657 | ((uint64_t)p[6] << 8) | ((uint64_t)p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5658 | p += 8; |
| 5659 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5660 | ssl->in_window = ((uint64_t)p[0] << 56) | ((uint64_t)p[1] << 48) | |
| 5661 | ((uint64_t)p[2] << 40) | ((uint64_t)p[3] << 32) | |
| 5662 | ((uint64_t)p[4] << 24) | ((uint64_t)p[5] << 16) | |
| 5663 | ((uint64_t)p[6] << 8) | ((uint64_t)p[7]); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5664 | p += 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5665 | # endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5666 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5667 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5668 | if ((size_t)(end - p) < 1) |
| 5669 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5670 | |
| 5671 | ssl->disable_datagram_packing = *p++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5672 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5673 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5674 | if ((size_t)(end - p) < 8) |
| 5675 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5676 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5677 | memcpy(ssl->cur_out_ctr, p, 8); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5678 | p += 8; |
| 5679 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5680 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5681 | if ((size_t)(end - p) < 2) |
| 5682 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5683 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5684 | ssl->mtu = (p[0] << 8) | p[1]; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5685 | p += 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5686 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5687 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5688 | # if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5689 | { |
| 5690 | uint8_t alpn_len; |
| 5691 | const char **cur; |
| 5692 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5693 | if ((size_t)(end - p) < 1) |
| 5694 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5695 | |
| 5696 | alpn_len = *p++; |
| 5697 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5698 | if (alpn_len != 0 && ssl->conf->alpn_list != NULL) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5699 | /* alpn_chosen should point to an item in the configured list */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5700 | for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) { |
| 5701 | if (strlen(*cur) == alpn_len && memcmp(p, cur, alpn_len) == 0) { |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5702 | ssl->alpn_chosen = *cur; |
| 5703 | break; |
| 5704 | } |
| 5705 | } |
| 5706 | } |
| 5707 | |
| 5708 | /* can only happen on conf mismatch */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5709 | if (alpn_len != 0 && ssl->alpn_chosen == NULL) |
| 5710 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5711 | |
| 5712 | p += alpn_len; |
| 5713 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5714 | # endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5715 | |
| 5716 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5717 | * Forced fields from top-level ssl_context structure |
| 5718 | * |
| 5719 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 5720 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 5721 | */ |
| 5722 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 5723 | |
| 5724 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 5725 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 5726 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5727 | /* Adjust pointers for header fields of outgoing records to |
| 5728 | * the given transform, accounting for explicit IV and CID. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5729 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 5730 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5731 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5732 | ssl->in_epoch = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5733 | # endif |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5734 | |
| 5735 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 5736 | * which we don't want - otherwise we'd end up freeing the wrong transform |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 5737 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 5738 | * inappropriately. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5739 | if (ssl->handshake != NULL) { |
| 5740 | mbedtls_ssl_handshake_free(ssl); |
| 5741 | mbedtls_free(ssl->handshake); |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 5742 | ssl->handshake = NULL; |
| 5743 | } |
| 5744 | |
| 5745 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5746 | * Done - should have consumed entire buffer |
| 5747 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5748 | if (p != end) |
| 5749 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5750 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5751 | return 0; |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5755 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5756 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5757 | int mbedtls_ssl_context_load(mbedtls_ssl_context *context, |
| 5758 | const unsigned char *buf, |
| 5759 | size_t len) |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5760 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5761 | int ret = ssl_context_load(context, buf, len); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5762 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5763 | if (ret != 0) |
| 5764 | mbedtls_ssl_free(context); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5765 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5766 | return ret; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5767 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5768 | # endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5769 | |
| 5770 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5771 | * Free an SSL context |
| 5772 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5773 | void mbedtls_ssl_free(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5774 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5775 | if (ssl == NULL) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5776 | return; |
| 5777 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5778 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> free")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5779 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5780 | if (ssl->out_buf != NULL) { |
| 5781 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5782 | size_t out_buf_len = ssl->out_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5783 | # else |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5784 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5785 | # endif |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5787 | mbedtls_platform_zeroize(ssl->out_buf, out_buf_len); |
| 5788 | mbedtls_free(ssl->out_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5789 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5792 | if (ssl->in_buf != NULL) { |
| 5793 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5794 | size_t in_buf_len = ssl->in_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5795 | # else |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5796 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5797 | # endif |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 5798 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5799 | mbedtls_platform_zeroize(ssl->in_buf, in_buf_len); |
| 5800 | mbedtls_free(ssl->in_buf); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5801 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5804 | if (ssl->transform) { |
| 5805 | mbedtls_ssl_transform_free(ssl->transform); |
| 5806 | mbedtls_free(ssl->transform); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5809 | if (ssl->handshake) { |
| 5810 | mbedtls_ssl_handshake_free(ssl); |
| 5811 | mbedtls_ssl_transform_free(ssl->transform_negotiate); |
| 5812 | mbedtls_ssl_session_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5813 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5814 | mbedtls_free(ssl->handshake); |
| 5815 | mbedtls_free(ssl->transform_negotiate); |
| 5816 | mbedtls_free(ssl->session_negotiate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5819 | if (ssl->session) { |
| 5820 | mbedtls_ssl_session_free(ssl->session); |
| 5821 | mbedtls_free(ssl->session); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5822 | } |
| 5823 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5824 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5825 | if (ssl->hostname != NULL) { |
| 5826 | mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname)); |
| 5827 | mbedtls_free(ssl->hostname); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5828 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5829 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5830 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5831 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
| 5832 | mbedtls_free(ssl->cli_id); |
| 5833 | # endif |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 5834 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5835 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= free")); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 5836 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 5837 | /* Actually clear after last debug message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5838 | mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5839 | } |
| 5840 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5841 | /* |
| 5842 | * Initialze mbedtls_ssl_config |
| 5843 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5844 | void mbedtls_ssl_config_init(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5845 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5846 | memset(conf, 0, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5847 | } |
| 5848 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5849 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5850 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
Gilles Peskine | a28f0f5 | 2021-06-02 15:29:38 +0200 | [diff] [blame] | 5851 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 5852 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5853 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 5854 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5855 | static int ssl_preset_default_hashes[] = { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5856 | # if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5857 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5858 | # endif |
| 5859 | # if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5860 | MBEDTLS_MD_SHA384, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5861 | # endif |
| 5862 | # if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5863 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5864 | # endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5865 | MBEDTLS_MD_NONE |
| 5866 | }; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5867 | # endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 5868 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5869 | # if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5870 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 5871 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5872 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5873 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5874 | * about this list. |
| 5875 | */ |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5876 | static mbedtls_ecp_group_id ssl_preset_default_curves[] = { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5877 | # if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5878 | MBEDTLS_ECP_DP_CURVE25519, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5879 | # endif |
| 5880 | # if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5881 | MBEDTLS_ECP_DP_SECP256R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5882 | # endif |
| 5883 | # if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5884 | MBEDTLS_ECP_DP_SECP384R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5885 | # endif |
| 5886 | # if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5887 | MBEDTLS_ECP_DP_CURVE448, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5888 | # endif |
| 5889 | # if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5890 | MBEDTLS_ECP_DP_SECP521R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5891 | # endif |
| 5892 | # if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5893 | MBEDTLS_ECP_DP_BP256R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5894 | # endif |
| 5895 | # if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5896 | MBEDTLS_ECP_DP_BP384R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5897 | # endif |
| 5898 | # if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 5899 | MBEDTLS_ECP_DP_BP512R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5900 | # endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5901 | MBEDTLS_ECP_DP_NONE |
| 5902 | }; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5903 | # endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 5904 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5905 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 5906 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5907 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 0 |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5908 | }; |
| 5909 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5910 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5911 | static int ssl_preset_suiteb_hashes[] = { MBEDTLS_MD_SHA256, MBEDTLS_MD_SHA384, |
| 5912 | MBEDTLS_MD_NONE }; |
| 5913 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5914 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5915 | # if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5916 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5917 | # if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5918 | MBEDTLS_ECP_DP_SECP256R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5919 | # endif |
| 5920 | # if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5921 | MBEDTLS_ECP_DP_SECP384R1, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5922 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5923 | MBEDTLS_ECP_DP_NONE |
| 5924 | }; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5925 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5926 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5927 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 5928 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5929 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5930 | int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, |
| 5931 | int endpoint, |
| 5932 | int transport, |
| 5933 | int preset) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5934 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5935 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5936 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5937 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5938 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 5939 | /* Use the functions here so that they are covered in tests, |
| 5940 | * but otherwise access member directly for efficiency */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5941 | mbedtls_ssl_conf_endpoint(conf, endpoint); |
| 5942 | mbedtls_ssl_conf_transport(conf, transport); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5943 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 5944 | /* |
| 5945 | * Things that are common to all presets |
| 5946 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5947 | # if defined(MBEDTLS_SSL_CLI_C) |
| 5948 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5949 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5950 | # if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5951 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5952 | # endif |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5953 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5954 | # endif |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5955 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5956 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5957 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5958 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5959 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5960 | # if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5961 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5962 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5963 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5964 | # if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5965 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 5966 | conf->f_cookie_check = ssl_cookie_check_dummy; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5967 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5968 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5969 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5970 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5971 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5972 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5973 | # if defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5974 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 5975 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5976 | # endif |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 5977 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5978 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5979 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 5980 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5981 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5982 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5983 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5984 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5985 | memset(conf->renego_period, 0x00, 2); |
| 5986 | memset(conf->renego_period + 2, 0xFF, 6); |
| 5987 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5988 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5989 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 5990 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 5991 | const unsigned char dhm_p[] = MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 5992 | const unsigned char dhm_g[] = MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 5993 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5994 | if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, dhm_p, sizeof(dhm_p), |
| 5995 | dhm_g, sizeof(dhm_g))) != 0) { |
| 5996 | return ret; |
| 5997 | } |
| 5998 | } |
| 5999 | # endif |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6000 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6001 | /* |
| 6002 | * Preset-specific defaults |
| 6003 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6004 | switch (preset) { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6005 | /* |
| 6006 | * NSA Suite B |
| 6007 | */ |
| 6008 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6009 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6010 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6011 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6012 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6013 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6014 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6016 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6017 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6018 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6019 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6020 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6021 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6022 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6023 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6024 | # if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6025 | conf->curve_list = ssl_preset_suiteb_curves; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6026 | # endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6027 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6028 | |
| 6029 | /* |
| 6030 | * Default |
| 6031 | */ |
| 6032 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6033 | conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6034 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ? |
| 6035 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6036 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6037 | conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6038 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ? |
| 6039 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6040 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6041 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6042 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6043 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6044 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6045 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 6046 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6047 | # endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6048 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6049 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6050 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6051 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6052 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6053 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6054 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6055 | conf->sig_hashes = ssl_preset_default_hashes; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6056 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6057 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6058 | # if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6059 | conf->curve_list = ssl_preset_default_curves; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6060 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6061 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6062 | # if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6063 | conf->dhm_min_bitlen = 1024; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6064 | # endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6065 | } |
| 6066 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6067 | return 0; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | /* |
| 6071 | * Free mbedtls_ssl_config |
| 6072 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6073 | void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6074 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6075 | # if defined(MBEDTLS_DHM_C) |
| 6076 | mbedtls_mpi_free(&conf->dhm_P); |
| 6077 | mbedtls_mpi_free(&conf->dhm_G); |
| 6078 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6079 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6080 | # if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 6081 | if (conf->psk != NULL) { |
| 6082 | mbedtls_platform_zeroize(conf->psk, conf->psk_len); |
| 6083 | mbedtls_free(conf->psk); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6084 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6085 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6086 | } |
| 6087 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6088 | if (conf->psk_identity != NULL) { |
| 6089 | mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len); |
| 6090 | mbedtls_free(conf->psk_identity); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6091 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6092 | conf->psk_identity_len = 0; |
| 6093 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6094 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6095 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6096 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6097 | ssl_key_cert_free(conf->key_cert); |
| 6098 | # endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6099 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6100 | mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config)); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6101 | } |
| 6102 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6103 | # if defined(MBEDTLS_PK_C) && \ |
| 6104 | (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6105 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6106 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6107 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6108 | unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6109 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6110 | # if defined(MBEDTLS_RSA_C) |
| 6111 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) |
| 6112 | return MBEDTLS_SSL_SIG_RSA; |
| 6113 | # endif |
| 6114 | # if defined(MBEDTLS_ECDSA_C) |
| 6115 | if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) |
| 6116 | return MBEDTLS_SSL_SIG_ECDSA; |
| 6117 | # endif |
| 6118 | return MBEDTLS_SSL_SIG_ANON; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6121 | unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6122 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6123 | switch (type) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6124 | case MBEDTLS_PK_RSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6125 | return MBEDTLS_SSL_SIG_RSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6126 | case MBEDTLS_PK_ECDSA: |
| 6127 | case MBEDTLS_PK_ECKEY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6128 | return MBEDTLS_SSL_SIG_ECDSA; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6129 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6130 | return MBEDTLS_SSL_SIG_ANON; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6131 | } |
| 6132 | } |
| 6133 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6134 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6135 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6136 | switch (sig) { |
| 6137 | # if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6138 | case MBEDTLS_SSL_SIG_RSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6139 | return MBEDTLS_PK_RSA; |
| 6140 | # endif |
| 6141 | # if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6142 | case MBEDTLS_SSL_SIG_ECDSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6143 | return MBEDTLS_PK_ECDSA; |
| 6144 | # endif |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6145 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6146 | return MBEDTLS_PK_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6147 | } |
| 6148 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6149 | # endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6150 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6151 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 6152 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6153 | |
| 6154 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6155 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set, |
| 6156 | mbedtls_pk_type_t sig_alg) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6157 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6158 | switch (sig_alg) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6159 | case MBEDTLS_PK_RSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6160 | return set->rsa; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6161 | case MBEDTLS_PK_ECDSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6162 | return set->ecdsa; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6163 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6164 | return MBEDTLS_MD_NONE; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6165 | } |
| 6166 | } |
| 6167 | |
| 6168 | /* Add a signature-hash-pair to a signature-hash set */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6169 | void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set, |
| 6170 | mbedtls_pk_type_t sig_alg, |
| 6171 | mbedtls_md_type_t md_alg) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6172 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6173 | switch (sig_alg) { |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6174 | case MBEDTLS_PK_RSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6175 | if (set->rsa == MBEDTLS_MD_NONE) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6176 | set->rsa = md_alg; |
| 6177 | break; |
| 6178 | |
| 6179 | case MBEDTLS_PK_ECDSA: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6180 | if (set->ecdsa == MBEDTLS_MD_NONE) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6181 | set->ecdsa = md_alg; |
| 6182 | break; |
| 6183 | |
| 6184 | default: |
| 6185 | break; |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | /* Allow exactly one hash algorithm for each signature. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6190 | void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set, |
| 6191 | mbedtls_md_type_t md_alg) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6192 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6193 | set->rsa = md_alg; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6194 | set->ecdsa = md_alg; |
| 6195 | } |
| 6196 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6197 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 6198 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6199 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6200 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6201 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6202 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6203 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6204 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6205 | switch (hash) { |
| 6206 | # if defined(MBEDTLS_MD5_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6207 | case MBEDTLS_SSL_HASH_MD5: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6208 | return MBEDTLS_MD_MD5; |
| 6209 | # endif |
| 6210 | # if defined(MBEDTLS_SHA1_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6211 | case MBEDTLS_SSL_HASH_SHA1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6212 | return MBEDTLS_MD_SHA1; |
| 6213 | # endif |
| 6214 | # if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6215 | case MBEDTLS_SSL_HASH_SHA224: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6216 | return MBEDTLS_MD_SHA224; |
| 6217 | # endif |
| 6218 | # if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6219 | case MBEDTLS_SSL_HASH_SHA256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6220 | return MBEDTLS_MD_SHA256; |
| 6221 | # endif |
| 6222 | # if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6223 | case MBEDTLS_SSL_HASH_SHA384: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6224 | return MBEDTLS_MD_SHA384; |
| 6225 | # endif |
| 6226 | # if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6227 | case MBEDTLS_SSL_HASH_SHA512: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6228 | return MBEDTLS_MD_SHA512; |
| 6229 | # endif |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6230 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6231 | return MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6232 | } |
| 6233 | } |
| 6234 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6235 | /* |
| 6236 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6237 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6238 | unsigned char mbedtls_ssl_hash_from_md_alg(int md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6239 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6240 | switch (md) { |
| 6241 | # if defined(MBEDTLS_MD5_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6242 | case MBEDTLS_MD_MD5: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6243 | return MBEDTLS_SSL_HASH_MD5; |
| 6244 | # endif |
| 6245 | # if defined(MBEDTLS_SHA1_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6246 | case MBEDTLS_MD_SHA1: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6247 | return MBEDTLS_SSL_HASH_SHA1; |
| 6248 | # endif |
| 6249 | # if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6250 | case MBEDTLS_MD_SHA224: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6251 | return MBEDTLS_SSL_HASH_SHA224; |
| 6252 | # endif |
| 6253 | # if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6254 | case MBEDTLS_MD_SHA256: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6255 | return MBEDTLS_SSL_HASH_SHA256; |
| 6256 | # endif |
| 6257 | # if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6258 | case MBEDTLS_MD_SHA384: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6259 | return MBEDTLS_SSL_HASH_SHA384; |
| 6260 | # endif |
| 6261 | # if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6262 | case MBEDTLS_MD_SHA512: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6263 | return MBEDTLS_SSL_HASH_SHA512; |
| 6264 | # endif |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6265 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6266 | return MBEDTLS_SSL_HASH_NONE; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6267 | } |
| 6268 | } |
| 6269 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6270 | # if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6271 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6272 | * Check if a curve proposed by the peer is in our list. |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6273 | * Return 0 if we're willing to use it, -1 otherwise. |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6274 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6275 | int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, |
| 6276 | mbedtls_ecp_group_id grp_id) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6278 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6279 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6280 | if (ssl->conf->curve_list == NULL) |
| 6281 | return -1; |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6282 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6283 | for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) |
| 6284 | if (*gid == grp_id) |
| 6285 | return 0; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6286 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6287 | return -1; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6288 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6289 | # endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6290 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6291 | # if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6292 | /* |
| 6293 | * Check if a hash proposed by the peer is in our list. |
| 6294 | * Return 0 if we're willing to use it, -1 otherwise. |
| 6295 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6296 | int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl, |
| 6297 | mbedtls_md_type_t md) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6298 | { |
| 6299 | const int *cur; |
| 6300 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6301 | if (ssl->conf->sig_hashes == NULL) |
| 6302 | return -1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6303 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6304 | for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) |
| 6305 | if (*cur == (int)md) |
| 6306 | return 0; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6307 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6308 | return -1; |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6309 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6310 | # endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6311 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6312 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6313 | int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert, |
| 6314 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 6315 | int cert_endpoint, |
| 6316 | uint32_t *flags) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6317 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6318 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6319 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6320 | const char *ext_oid; |
| 6321 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6322 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6323 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6324 | /* Server part of the key exchange */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6325 | switch (ciphersuite->key_exchange) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6326 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 6327 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6328 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6329 | break; |
| 6330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6331 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 6332 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 6333 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 6334 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6335 | break; |
| 6336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6337 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 6338 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6339 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6340 | break; |
| 6341 | |
| 6342 | /* Don't use default: we want warnings when adding new values */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6343 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 6344 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 6345 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 6346 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 6347 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6348 | usage = 0; |
| 6349 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6350 | } else { |
| 6351 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for |
| 6352 | * now */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6353 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6354 | } |
| 6355 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6356 | if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6357 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6358 | ret = -1; |
| 6359 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6360 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6361 | if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6362 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6363 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH); |
| 6364 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6365 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6366 | ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6367 | } |
| 6368 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6369 | if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != |
| 6370 | 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6371 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6372 | ret = -1; |
| 6373 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 6374 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6375 | return ret; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6376 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6377 | # endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 6378 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6379 | int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6380 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6381 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6382 | if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) |
| 6383 | return -1; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6384 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6385 | switch (md) { |
| 6386 | # if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6387 | case MBEDTLS_SSL_HASH_SHA384: |
| 6388 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 6389 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6390 | # endif |
| 6391 | # if defined(MBEDTLS_SHA256_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6392 | case MBEDTLS_SSL_HASH_SHA256: |
| 6393 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 6394 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6395 | # endif |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6396 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6397 | return -1; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6398 | } |
| 6399 | |
| 6400 | return 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6401 | # else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6402 | (void)ssl; |
| 6403 | (void)md; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6404 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6405 | return -1; |
| 6406 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6409 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6410 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6411 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6412 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 6413 | unsigned char *hash, |
| 6414 | size_t *hashlen, |
| 6415 | unsigned char *data, |
| 6416 | size_t data_len, |
| 6417 | mbedtls_md_type_t md_alg) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6418 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6419 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 6420 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6421 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6422 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6423 | MBEDTLS_SSL_DEBUG_MSG( |
| 6424 | 3, ("Perform PSA-based computation of digest of ServerKeyExchange")); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6425 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6426 | if ((status = psa_hash_setup(&hash_operation, hash_alg)) != PSA_SUCCESS) { |
| 6427 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6428 | goto exit; |
| 6429 | } |
| 6430 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6431 | if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes, |
| 6432 | 64)) != PSA_SUCCESS) { |
| 6433 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6434 | goto exit; |
| 6435 | } |
| 6436 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6437 | if ((status = psa_hash_update(&hash_operation, data, data_len)) != |
| 6438 | PSA_SUCCESS) { |
| 6439 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6440 | goto exit; |
| 6441 | } |
| 6442 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6443 | if ((status = psa_hash_finish(&hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 6444 | hashlen)) != PSA_SUCCESS) { |
| 6445 | MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status); |
| 6446 | goto exit; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6447 | } |
| 6448 | |
| 6449 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6450 | if (status != PSA_SUCCESS) { |
| 6451 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6452 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 6453 | switch (status) { |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6454 | case PSA_ERROR_NOT_SUPPORTED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6455 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6456 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6457 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6458 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6459 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6460 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6461 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6462 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6463 | } |
| 6464 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6465 | return 0; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6466 | } |
| 6467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6468 | # else |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6469 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6470 | int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl, |
| 6471 | unsigned char *hash, |
| 6472 | size_t *hashlen, |
| 6473 | unsigned char *data, |
| 6474 | size_t data_len, |
| 6475 | mbedtls_md_type_t md_alg) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6476 | { |
| 6477 | int ret = 0; |
| 6478 | mbedtls_md_context_t ctx; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6479 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 6480 | *hashlen = mbedtls_md_get_size(md_info); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6481 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6482 | MBEDTLS_SSL_DEBUG_MSG( |
| 6483 | 3, |
| 6484 | ("Perform mbedtls-based computation of digest of ServerKeyExchange")); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 6485 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6486 | mbedtls_md_init(&ctx); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6487 | |
| 6488 | /* |
| 6489 | * digitally-signed struct { |
| 6490 | * opaque client_random[32]; |
| 6491 | * opaque server_random[32]; |
| 6492 | * ServerDHParams params; |
| 6493 | * }; |
| 6494 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6495 | if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) { |
| 6496 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6497 | goto exit; |
| 6498 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6499 | if ((ret = mbedtls_md_starts(&ctx)) != 0) { |
| 6500 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6501 | goto exit; |
| 6502 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6503 | if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) { |
| 6504 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6505 | goto exit; |
| 6506 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6507 | if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) { |
| 6508 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6509 | goto exit; |
| 6510 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6511 | if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) { |
| 6512 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6513 | goto exit; |
| 6514 | } |
| 6515 | |
| 6516 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6517 | mbedtls_md_free(&ctx); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6518 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6519 | if (ret != 0) |
| 6520 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6521 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6522 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6523 | return ret; |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6524 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6525 | # endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 6526 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 6527 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 6528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6529 | #endif /* MBEDTLS_SSL_TLS_C */ |