Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | /* ==================================================================== |
| 109 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 110 | * ECC cipher suite support in OpenSSL originally developed by |
| 111 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
| 112 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 113 | #include <openssl/ssl.h> |
| 114 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 115 | #include <assert.h> |
| 116 | #include <limits.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 117 | #include <string.h> |
| 118 | |
| 119 | #include <openssl/buf.h> |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 120 | #include <openssl/bytestring.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 121 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 122 | #include <openssl/evp.h> |
| 123 | #include <openssl/mem.h> |
| 124 | #include <openssl/md5.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 125 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 126 | #include <openssl/rand.h> |
| 127 | #include <openssl/sha.h> |
| 128 | #include <openssl/x509.h> |
| 129 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 130 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 131 | |
| 132 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame^] | 133 | SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 134 | SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE)); |
| 135 | if (hs == NULL) { |
| 136 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 137 | return NULL; |
| 138 | } |
| 139 | memset(hs, 0, sizeof(SSL_HANDSHAKE)); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame^] | 140 | hs->ssl = ssl; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 141 | hs->wait = ssl_hs_ok; |
| 142 | return hs; |
| 143 | } |
| 144 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 145 | void ssl_handshake_free(SSL_HANDSHAKE *hs) { |
| 146 | if (hs == NULL) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | OPENSSL_cleanse(hs->secret, sizeof(hs->secret)); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 151 | OPENSSL_cleanse(hs->client_traffic_secret_0, |
| 152 | sizeof(hs->client_traffic_secret_0)); |
| 153 | OPENSSL_cleanse(hs->server_traffic_secret_0, |
| 154 | sizeof(hs->server_traffic_secret_0)); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 155 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 156 | OPENSSL_free(hs->cookie); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 157 | OPENSSL_free(hs->key_share_bytes); |
| 158 | OPENSSL_free(hs->public_key); |
| 159 | OPENSSL_free(hs->peer_sigalgs); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 160 | OPENSSL_free(hs->peer_supported_group_list); |
| 161 | OPENSSL_free(hs->peer_key); |
| 162 | OPENSSL_free(hs->server_params); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 163 | OPENSSL_free(hs->peer_psk_identity_hint); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 164 | sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free); |
| 165 | OPENSSL_free(hs->certificate_types); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 166 | |
| 167 | if (hs->key_block != NULL) { |
| 168 | OPENSSL_cleanse(hs->key_block, hs->key_block_len); |
| 169 | OPENSSL_free(hs->key_block); |
| 170 | } |
| 171 | |
| 172 | OPENSSL_free(hs->hostname); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 173 | OPENSSL_free(hs); |
| 174 | } |
| 175 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 176 | /* ssl3_do_write sends |ssl->init_buf| in records of type 'type' |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 177 | * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns 1 on success |
| 178 | * and <= 0 on error. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 179 | static int ssl3_do_write(SSL *ssl, int type, const uint8_t *data, size_t len) { |
| 180 | int ret = ssl3_write_bytes(ssl, type, data, len); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 181 | if (ret <= 0) { |
| 182 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 183 | } |
| 184 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 185 | /* ssl3_write_bytes writes the data in its entirety. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 186 | assert((size_t)ret == len); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 187 | ssl_do_msg_callback(ssl, 1 /* write */, type, data, len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
| 192 | CBB_zero(cbb); |
| 193 | if (ssl->s3->pending_message != NULL) { |
| 194 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* Pick a modest size hint to save most of the |realloc| calls. */ |
| 199 | if (!CBB_init(cbb, 64) || |
| 200 | !CBB_add_u8(cbb, type) || |
| 201 | !CBB_add_u24_length_prefixed(cbb, body)) { |
| 202 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 209 | int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg, |
| 210 | size_t *out_len) { |
| 211 | if (!CBB_finish(cbb, out_msg, out_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 212 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 213 | return 0; |
| 214 | } |
| 215 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | int ssl3_queue_message(SSL *ssl, uint8_t *msg, size_t len) { |
| 220 | if (ssl->s3->pending_message != NULL || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 221 | len > 0xffffffffu) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 222 | OPENSSL_free(msg); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 223 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | ssl3_update_handshake_hash(ssl, msg, len); |
| 228 | |
| 229 | ssl->s3->pending_message = msg; |
| 230 | ssl->s3->pending_message_len = (uint32_t)len; |
| 231 | return 1; |
| 232 | } |
| 233 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 234 | int ssl_complete_message(SSL *ssl, CBB *cbb) { |
| 235 | uint8_t *msg; |
| 236 | size_t len; |
| 237 | if (!ssl->method->finish_message(ssl, cbb, &msg, &len) || |
| 238 | !ssl->method->queue_message(ssl, msg, len)) { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 245 | int ssl3_write_message(SSL *ssl) { |
| 246 | if (ssl->s3->pending_message == NULL) { |
| 247 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int ret = ssl3_do_write(ssl, SSL3_RT_HANDSHAKE, ssl->s3->pending_message, |
| 252 | ssl->s3->pending_message_len); |
| 253 | if (ret <= 0) { |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | OPENSSL_free(ssl->s3->pending_message); |
| 258 | ssl->s3->pending_message = NULL; |
| 259 | ssl->s3->pending_message_len = 0; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 260 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 261 | } |
| 262 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame^] | 263 | int ssl3_send_finished(SSL_HANDSHAKE *hs, int a, int b) { |
| 264 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 265 | if (ssl->state == b) { |
| 266 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | } |
| 268 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 269 | uint8_t finished[EVP_MAX_MD_SIZE]; |
| 270 | size_t finished_len = |
| 271 | ssl->s3->enc_method->final_finish_mac(ssl, ssl->server, finished); |
| 272 | if (finished_len == 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 273 | return 0; |
| 274 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 275 | |
| 276 | /* Log the master secret, if logging is enabled. */ |
| 277 | if (!ssl_log_secret(ssl, "CLIENT_RANDOM", |
| 278 | SSL_get_session(ssl)->master_key, |
| 279 | SSL_get_session(ssl)->master_key_length)) { |
| 280 | return 0; |
| 281 | } |
| 282 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 283 | /* Copy the Finished so we can use it for renegotiation checks. */ |
| 284 | if (ssl->version != SSL3_VERSION) { |
| 285 | if (finished_len > sizeof(ssl->s3->previous_client_finished) || |
| 286 | finished_len > sizeof(ssl->s3->previous_server_finished)) { |
| 287 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | if (ssl->server) { |
| 292 | memcpy(ssl->s3->previous_server_finished, finished, finished_len); |
| 293 | ssl->s3->previous_server_finished_len = finished_len; |
| 294 | } else { |
| 295 | memcpy(ssl->s3->previous_client_finished, finished, finished_len); |
| 296 | ssl->s3->previous_client_finished_len = finished_len; |
| 297 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | CBB cbb, body; |
| 301 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 302 | !CBB_add_bytes(&body, finished, finished_len) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 303 | !ssl_complete_message(ssl, &cbb)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 304 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 305 | CBB_cleanup(&cbb); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | ssl->state = b; |
| 310 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 311 | } |
| 312 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame^] | 313 | int ssl3_get_finished(SSL_HANDSHAKE *hs) { |
| 314 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 315 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_FINISHED, |
| 316 | ssl_dont_hash_message); |
| 317 | if (ret <= 0) { |
| 318 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* Snapshot the finished hash before incorporating the new message. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 322 | uint8_t finished[EVP_MAX_MD_SIZE]; |
| 323 | size_t finished_len = |
| 324 | ssl->s3->enc_method->final_finish_mac(ssl, !ssl->server, finished); |
| 325 | if (finished_len == 0 || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 326 | !ssl_hash_current_message(ssl)) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 327 | return -1; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 328 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 329 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 330 | int finished_ok = ssl->init_num == finished_len && |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 331 | CRYPTO_memcmp(ssl->init_msg, finished, finished_len) == 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 332 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 333 | finished_ok = 1; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 334 | #endif |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 335 | if (!finished_ok) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 336 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 337 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 338 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | } |
| 340 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 341 | /* Copy the Finished so we can use it for renegotiation checks. */ |
| 342 | if (ssl->version != SSL3_VERSION) { |
| 343 | if (finished_len > sizeof(ssl->s3->previous_client_finished) || |
| 344 | finished_len > sizeof(ssl->s3->previous_server_finished)) { |
| 345 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | if (ssl->server) { |
| 350 | memcpy(ssl->s3->previous_client_finished, finished, finished_len); |
| 351 | ssl->s3->previous_client_finished_len = finished_len; |
| 352 | } else { |
| 353 | memcpy(ssl->s3->previous_server_finished, finished, finished_len); |
| 354 | ssl->s3->previous_server_finished_len = finished_len; |
| 355 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 359 | } |
| 360 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 361 | int ssl3_send_change_cipher_spec(SSL *ssl) { |
| 362 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 363 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 364 | return ssl3_do_write(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 365 | sizeof(kChangeCipherSpec)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 368 | int ssl3_output_cert_chain(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 369 | CBB cbb, body; |
| 370 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) || |
| 371 | !ssl_add_cert_chain(ssl, &body) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 372 | !ssl_complete_message(ssl, &cbb)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 373 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 374 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 378 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 379 | } |
| 380 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 381 | size_t ssl_max_handshake_message_len(const SSL *ssl) { |
| 382 | /* kMaxMessageLen is the default maximum message size for handshakes which do |
| 383 | * not accept peer certificate chains. */ |
| 384 | static const size_t kMaxMessageLen = 16384; |
| 385 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 386 | if (SSL_in_init(ssl)) { |
| 387 | if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) && |
| 388 | kMaxMessageLen < ssl->max_cert_list) { |
| 389 | return ssl->max_cert_list; |
| 390 | } |
| 391 | return kMaxMessageLen; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 392 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 393 | |
| 394 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 395 | /* In TLS 1.2 and below, the largest acceptable post-handshake message is |
| 396 | * a HelloRequest. */ |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | if (ssl->server) { |
| 401 | /* The largest acceptable post-handshake message for a server is a |
| 402 | * KeyUpdate. We will never initiate post-handshake auth. */ |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /* Clients must accept NewSessionTicket and CertificateRequest, so allow the |
| 407 | * default size. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 408 | return kMaxMessageLen; |
| 409 | } |
| 410 | |
| 411 | static int extend_handshake_buffer(SSL *ssl, size_t length) { |
| 412 | if (!BUF_MEM_reserve(ssl->init_buf, length)) { |
| 413 | return -1; |
| 414 | } |
| 415 | while (ssl->init_buf->length < length) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 416 | int ret = ssl3_read_handshake_bytes( |
| 417 | ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length, |
| 418 | length - ssl->init_buf->length); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 419 | if (ret <= 0) { |
| 420 | return ret; |
| 421 | } |
| 422 | ssl->init_buf->length += (size_t)ret; |
| 423 | } |
| 424 | return 1; |
| 425 | } |
| 426 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 427 | static int read_v2_client_hello(SSL *ssl, int *out_is_v2_client_hello) { |
| 428 | /* Read the first 5 bytes, the size of the TLS record header. This is |
| 429 | * sufficient to detect a V2ClientHello and ensures that we never read beyond |
| 430 | * the first record. */ |
| 431 | int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH); |
| 432 | if (ret <= 0) { |
| 433 | return ret; |
| 434 | } |
| 435 | const uint8_t *p = ssl_read_buffer(ssl); |
| 436 | |
| 437 | /* Some dedicated error codes for protocol mixups should the application wish |
| 438 | * to interpret them differently. (These do not overlap with ClientHello or |
| 439 | * V2ClientHello.) */ |
| 440 | if (strncmp("GET ", (const char *)p, 4) == 0 || |
| 441 | strncmp("POST ", (const char *)p, 5) == 0 || |
| 442 | strncmp("HEAD ", (const char *)p, 5) == 0 || |
| 443 | strncmp("PUT ", (const char *)p, 4) == 0) { |
| 444 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); |
| 445 | return -1; |
| 446 | } |
| 447 | if (strncmp("CONNE", (const char *)p, 5) == 0) { |
| 448 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO || |
| 453 | p[3] != SSL3_VERSION_MAJOR) { |
| 454 | /* Not a V2ClientHello. */ |
| 455 | *out_is_v2_client_hello = 0; |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | /* Determine the length of the V2ClientHello. */ |
| 460 | size_t msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 461 | if (msg_length > (1024 * 4)) { |
| 462 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
| 463 | return -1; |
| 464 | } |
| 465 | if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { |
| 466 | /* Reject lengths that are too short early. We have already read |
| 467 | * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an |
| 468 | * (invalid) V2ClientHello which would be shorter than that. */ |
| 469 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); |
| 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | /* Read the remainder of the V2ClientHello. */ |
| 474 | ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length); |
| 475 | if (ret <= 0) { |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | CBS v2_client_hello; |
| 480 | CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length); |
| 481 | |
| 482 | /* The V2ClientHello without the length is incorporated into the handshake |
| 483 | * hash. */ |
| 484 | if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello), |
| 485 | CBS_len(&v2_client_hello))) { |
| 486 | return -1; |
| 487 | } |
| 488 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 489 | ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 490 | CBS_data(&v2_client_hello), CBS_len(&v2_client_hello)); |
| 491 | |
| 492 | uint8_t msg_type; |
| 493 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 494 | CBS cipher_specs, session_id, challenge; |
| 495 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 496 | !CBS_get_u16(&v2_client_hello, &version) || |
| 497 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 498 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 499 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 500 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 501 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 502 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 503 | CBS_len(&v2_client_hello) != 0) { |
| 504 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | /* msg_type has already been checked. */ |
| 509 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 510 | |
| 511 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 512 | * left-pad with zeros as needed. */ |
| 513 | size_t rand_len = CBS_len(&challenge); |
| 514 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 515 | rand_len = SSL3_RANDOM_SIZE; |
| 516 | } |
| 517 | uint8_t random[SSL3_RANDOM_SIZE]; |
| 518 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 519 | memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 520 | rand_len); |
| 521 | |
| 522 | /* Write out an equivalent SSLv3 ClientHello. */ |
| 523 | size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ + |
| 524 | SSL3_RANDOM_SIZE + 1 /* session ID length */ + |
| 525 | 2 /* cipher list length */ + |
| 526 | CBS_len(&cipher_specs) / 3 * 2 + |
| 527 | 1 /* compression length */ + 1 /* compression */; |
| 528 | CBB client_hello, hello_body, cipher_suites; |
| 529 | CBB_zero(&client_hello); |
| 530 | if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) || |
| 531 | !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data, |
| 532 | ssl->init_buf->max) || |
| 533 | !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
| 534 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 535 | !CBB_add_u16(&hello_body, version) || |
| 536 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 537 | /* No session id. */ |
| 538 | !CBB_add_u8(&hello_body, 0) || |
| 539 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 540 | CBB_cleanup(&client_hello); |
| 541 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | /* Copy the cipher suites. */ |
| 546 | while (CBS_len(&cipher_specs) > 0) { |
| 547 | uint32_t cipher_spec; |
| 548 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 549 | CBB_cleanup(&client_hello); |
| 550 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | /* Skip SSLv2 ciphers. */ |
| 555 | if ((cipher_spec & 0xff0000) != 0) { |
| 556 | continue; |
| 557 | } |
| 558 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 559 | CBB_cleanup(&client_hello); |
| 560 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 561 | return -1; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* Add the null compression scheme and finish. */ |
| 566 | if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || |
| 567 | !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) { |
| 568 | CBB_cleanup(&client_hello); |
| 569 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | /* Consume and discard the V2ClientHello. */ |
| 574 | ssl_read_buffer_consume(ssl, 2 + msg_length); |
| 575 | ssl_read_buffer_discard(ssl); |
| 576 | |
| 577 | *out_is_v2_client_hello = 1; |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | int ssl3_get_message(SSL *ssl, int msg_type, |
| 582 | enum ssl_hash_message_t hash_message) { |
| 583 | again: |
| 584 | /* Re-create the handshake buffer if needed. */ |
| 585 | if (ssl->init_buf == NULL) { |
| 586 | ssl->init_buf = BUF_MEM_new(); |
| 587 | if (ssl->init_buf == NULL) { |
| 588 | return -1; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (ssl->server && !ssl->s3->v2_hello_done) { |
| 593 | /* Bypass the record layer for the first message to handle V2ClientHello. */ |
| 594 | assert(hash_message == ssl_hash_message); |
| 595 | int is_v2_client_hello = 0; |
| 596 | int ret = read_v2_client_hello(ssl, &is_v2_client_hello); |
| 597 | if (ret <= 0) { |
| 598 | return ret; |
| 599 | } |
| 600 | if (is_v2_client_hello) { |
| 601 | /* V2ClientHello is hashed separately. */ |
| 602 | hash_message = ssl_dont_hash_message; |
| 603 | } |
| 604 | ssl->s3->v2_hello_done = 1; |
| 605 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 606 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 607 | if (ssl->s3->tmp.reuse_message) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 608 | /* A ssl_dont_hash_message call cannot be combined with reuse_message; the |
| 609 | * ssl_dont_hash_message would have to have been applied to the previous |
| 610 | * call. */ |
| 611 | assert(hash_message == ssl_hash_message); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 612 | assert(ssl->init_msg != NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 613 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 614 | ssl->s3->tmp.reuse_message = 0; |
| 615 | hash_message = ssl_dont_hash_message; |
| 616 | } else { |
| 617 | ssl3_release_current_message(ssl, 0 /* don't free buffer */); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 618 | } |
| 619 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 620 | /* Read the message header, if we haven't yet. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 621 | int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 622 | if (ret <= 0) { |
| 623 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 624 | } |
| 625 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 626 | /* Parse out the length. Cap it so the peer cannot force us to buffer up to |
| 627 | * 2^24 bytes. */ |
| 628 | const uint8_t *p = (uint8_t *)ssl->init_buf->data; |
| 629 | size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3]; |
| 630 | if (msg_len > ssl_max_handshake_message_len(ssl)) { |
| 631 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 632 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | /* Read the message body, if we haven't yet. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 637 | ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 638 | if (ret <= 0) { |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | /* We have now received a complete message. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 643 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, ssl->init_buf->data, |
| 644 | ssl->init_buf->length); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 645 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 646 | ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0]; |
| 647 | ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH; |
| 648 | ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH; |
| 649 | |
| 650 | /* Ignore stray HelloRequest messages in the handshake before TLS 1.3. Per RFC |
| 651 | * 5246, section 7.4.1.1, the server may send HelloRequest at any time. */ |
| 652 | if (!ssl->server && SSL_in_init(ssl) && |
| 653 | (!ssl->s3->have_version || ssl3_protocol_version(ssl) < TLS1_3_VERSION) && |
| 654 | ssl->s3->tmp.message_type == SSL3_MT_HELLO_REQUEST && |
| 655 | ssl->init_num == 0) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 656 | goto again; |
| 657 | } |
| 658 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 659 | if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 660 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 661 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 662 | return -1; |
| 663 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 664 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 665 | /* Feed this message into MAC computation. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 666 | if (hash_message == ssl_hash_message && !ssl_hash_current_message(ssl)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 667 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 668 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 669 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 670 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 673 | void ssl3_get_current_message(const SSL *ssl, CBS *out) { |
| 674 | CBS_init(out, (uint8_t *)ssl->init_buf->data, ssl->init_buf->length); |
| 675 | } |
| 676 | |
| 677 | int ssl_hash_current_message(SSL *ssl) { |
| 678 | CBS cbs; |
| 679 | ssl->method->get_current_message(ssl, &cbs); |
| 680 | return ssl3_update_handshake_hash(ssl, CBS_data(&cbs), CBS_len(&cbs)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 681 | } |
| 682 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 683 | void ssl3_release_current_message(SSL *ssl, int free_buffer) { |
| 684 | if (ssl->init_msg != NULL) { |
| 685 | /* |init_buf| never contains data beyond the current message. */ |
| 686 | assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 687 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 688 | /* Clear the current message. */ |
| 689 | ssl->init_msg = NULL; |
| 690 | ssl->init_num = 0; |
| 691 | ssl->init_buf->length = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 692 | } |
| 693 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 694 | if (free_buffer) { |
| 695 | BUF_MEM_free(ssl->init_buf); |
| 696 | ssl->init_buf = NULL; |
| 697 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 700 | int ssl_verify_alarm_type(long type) { |
| 701 | int al; |
| 702 | |
| 703 | switch (type) { |
| 704 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 705 | case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 706 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 707 | al = SSL_AD_UNKNOWN_CA; |
| 708 | break; |
| 709 | |
| 710 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 711 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 712 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 713 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 714 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 715 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 716 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 717 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 718 | case X509_V_ERR_CRL_NOT_YET_VALID: |
| 719 | case X509_V_ERR_CERT_UNTRUSTED: |
| 720 | case X509_V_ERR_CERT_REJECTED: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 721 | case X509_V_ERR_HOSTNAME_MISMATCH: |
| 722 | case X509_V_ERR_EMAIL_MISMATCH: |
| 723 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 724 | al = SSL_AD_BAD_CERTIFICATE; |
| 725 | break; |
| 726 | |
| 727 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: |
| 728 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: |
| 729 | al = SSL_AD_DECRYPT_ERROR; |
| 730 | break; |
| 731 | |
| 732 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 733 | case X509_V_ERR_CRL_HAS_EXPIRED: |
| 734 | al = SSL_AD_CERTIFICATE_EXPIRED; |
| 735 | break; |
| 736 | |
| 737 | case X509_V_ERR_CERT_REVOKED: |
| 738 | al = SSL_AD_CERTIFICATE_REVOKED; |
| 739 | break; |
| 740 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 741 | case X509_V_ERR_UNSPECIFIED: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 742 | case X509_V_ERR_OUT_OF_MEM: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 743 | case X509_V_ERR_INVALID_CALL: |
| 744 | case X509_V_ERR_STORE_LOOKUP: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 745 | al = SSL_AD_INTERNAL_ERROR; |
| 746 | break; |
| 747 | |
| 748 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 749 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 750 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 751 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 752 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 753 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 754 | case X509_V_ERR_INVALID_CA: |
| 755 | al = SSL_AD_UNKNOWN_CA; |
| 756 | break; |
| 757 | |
| 758 | case X509_V_ERR_APPLICATION_VERIFICATION: |
| 759 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 760 | break; |
| 761 | |
| 762 | case X509_V_ERR_INVALID_PURPOSE: |
| 763 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 764 | break; |
| 765 | |
| 766 | default: |
| 767 | al = SSL_AD_CERTIFICATE_UNKNOWN; |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | return al; |
| 772 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 773 | |
| 774 | int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
| 775 | const SSL_EXTENSION_TYPE *ext_types, |
| 776 | size_t num_ext_types) { |
| 777 | /* Reset everything. */ |
| 778 | for (size_t i = 0; i < num_ext_types; i++) { |
| 779 | *ext_types[i].out_present = 0; |
| 780 | CBS_init(ext_types[i].out_data, NULL, 0); |
| 781 | } |
| 782 | |
| 783 | CBS copy = *cbs; |
| 784 | while (CBS_len(©) != 0) { |
| 785 | uint16_t type; |
| 786 | CBS data; |
| 787 | if (!CBS_get_u16(©, &type) || |
| 788 | !CBS_get_u16_length_prefixed(©, &data)) { |
| 789 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 790 | *out_alert = SSL_AD_DECODE_ERROR; |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | const SSL_EXTENSION_TYPE *ext_type = NULL; |
| 795 | for (size_t i = 0; i < num_ext_types; i++) { |
| 796 | if (type == ext_types[i].type) { |
| 797 | ext_type = &ext_types[i]; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if (ext_type == NULL) { |
| 803 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 804 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | /* Duplicate ext_types are forbidden. */ |
| 809 | if (*ext_type->out_present) { |
| 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
| 811 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | *ext_type->out_present = 1; |
| 816 | *ext_type->out_data = data; |
| 817 | } |
| 818 | |
| 819 | return 1; |
| 820 | } |