Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [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 | |
| 113 | #include <assert.h> |
| 114 | #include <limits.h> |
| 115 | #include <stdio.h> |
| 116 | #include <string.h> |
| 117 | |
| 118 | #include <openssl/buf.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame^] | 119 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | #include <openssl/evp.h> |
| 121 | #include <openssl/mem.h> |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 122 | #include <openssl/md5.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | #include <openssl/obj.h> |
| 124 | #include <openssl/rand.h> |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 125 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | #include <openssl/x509.h> |
| 127 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 128 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 131 | /* ssl3_do_write sends |s->init_buf| in records of type 'type' |
| 132 | * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns -1 on error, 1 |
| 133 | * on success or zero if the transmission is still incomplete. */ |
| 134 | int ssl3_do_write(SSL *s, int type) { |
| 135 | int n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 137 | n = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off], s->init_num); |
| 138 | if (n < 0) { |
| 139 | return -1; |
| 140 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 142 | if (n == s->init_num) { |
| 143 | if (s->msg_callback) { |
| 144 | s->msg_callback(1, s->version, type, s->init_buf->data, |
| 145 | (size_t)(s->init_off + s->init_num), s, |
| 146 | s->msg_callback_arg); |
| 147 | } |
| 148 | return 1; |
| 149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 151 | s->init_off += n; |
| 152 | s->init_num -= n; |
| 153 | return 0; |
| 154 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 156 | int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen) { |
| 157 | uint8_t *p; |
| 158 | int n; |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 159 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 160 | if (s->state == a) { |
| 161 | p = ssl_handshake_start(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 163 | n = s->enc_method->final_finish_mac(s, sender, slen, s->s3->tmp.finish_md); |
| 164 | if (n == 0) { |
| 165 | return 0; |
| 166 | } |
| 167 | s->s3->tmp.finish_md_len = n; |
| 168 | memcpy(p, s->s3->tmp.finish_md, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 170 | /* Log the master secret, if logging is enabled. */ |
| 171 | if (!ssl_ctx_log_master_secret(s->ctx, s->s3->client_random, |
| 172 | SSL3_RANDOM_SIZE, s->session->master_key, |
| 173 | s->session->master_key_length)) { |
| 174 | return 0; |
| 175 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
David Benjamin | 8b36841 | 2015-03-14 01:54:17 -0400 | [diff] [blame] | 177 | /* Copy the finished so we can use it for renegotiation checks */ |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 178 | if (s->server) { |
| 179 | assert(n <= EVP_MAX_MD_SIZE); |
| 180 | memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, n); |
| 181 | s->s3->previous_server_finished_len = n; |
| 182 | } else { |
| 183 | assert(n <= EVP_MAX_MD_SIZE); |
| 184 | memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, n); |
| 185 | s->s3->previous_client_finished_len = n; |
| 186 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 188 | if (!ssl_set_handshake_header(s, SSL3_MT_FINISHED, n)) { |
| 189 | return 0; |
| 190 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 191 | s->state = b; |
| 192 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 194 | /* SSL3_ST_SEND_xxxxxx_HELLO_B */ |
| 195 | return ssl_do_write(s); |
| 196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 198 | /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen to |
| 199 | * far. */ |
| 200 | static void ssl3_take_mac(SSL *s) { |
| 201 | const char *sender; |
| 202 | int slen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 204 | /* If no new cipher setup then return immediately: other functions will set |
| 205 | * the appropriate error. */ |
| 206 | if (s->s3->tmp.new_cipher == NULL) { |
| 207 | return; |
| 208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 210 | if (s->state & SSL_ST_CONNECT) { |
| 211 | sender = s->enc_method->server_finished_label; |
| 212 | slen = s->enc_method->server_finished_label_len; |
| 213 | } else { |
| 214 | sender = s->enc_method->client_finished_label; |
| 215 | slen = s->enc_method->client_finished_label_len; |
| 216 | } |
David Benjamin | 5b8f104 | 2014-08-25 23:28:44 -0400 | [diff] [blame] | 217 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 218 | s->s3->tmp.peer_finish_md_len = s->enc_method->final_finish_mac( |
| 219 | s, sender, slen, s->s3->tmp.peer_finish_md); |
| 220 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 222 | int ssl3_get_finished(SSL *s, int a, int b) { |
| 223 | int al, finished_len, ok; |
| 224 | long message_len; |
| 225 | uint8_t *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 227 | message_len = |
| 228 | s->method->ssl_get_message(s, a, b, SSL3_MT_FINISHED, EVP_MAX_MD_SIZE, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 229 | ssl_dont_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 231 | if (!ok) { |
| 232 | return message_len; |
| 233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 235 | /* Snapshot the finished hash before incorporating the new message. */ |
| 236 | ssl3_take_mac(s); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 237 | if (!ssl3_hash_current_message(s)) { |
| 238 | goto err; |
| 239 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 241 | /* If this occurs, we have missed a message. |
| 242 | * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */ |
| 243 | if (!s->s3->change_cipher_spec) { |
| 244 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 245 | OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_GOT_A_FIN_BEFORE_A_CCS); |
| 246 | goto f_err; |
| 247 | } |
| 248 | s->s3->change_cipher_spec = 0; |
| 249 | |
| 250 | p = s->init_msg; |
| 251 | finished_len = s->s3->tmp.peer_finish_md_len; |
| 252 | |
| 253 | if (finished_len != message_len) { |
| 254 | al = SSL_AD_DECODE_ERROR; |
| 255 | OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_BAD_DIGEST_LENGTH); |
| 256 | goto f_err; |
| 257 | } |
| 258 | |
| 259 | if (CRYPTO_memcmp(p, s->s3->tmp.peer_finish_md, finished_len) != 0) { |
| 260 | al = SSL_AD_DECRYPT_ERROR; |
| 261 | OPENSSL_PUT_ERROR(SSL, ssl3_get_finished, SSL_R_DIGEST_CHECK_FAILED); |
| 262 | goto f_err; |
| 263 | } |
| 264 | |
| 265 | /* Copy the finished so we can use it for renegotiation checks */ |
| 266 | if (s->server) { |
| 267 | assert(finished_len <= EVP_MAX_MD_SIZE); |
| 268 | memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, finished_len); |
| 269 | s->s3->previous_client_finished_len = finished_len; |
| 270 | } else { |
| 271 | assert(finished_len <= EVP_MAX_MD_SIZE); |
| 272 | memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, finished_len); |
| 273 | s->s3->previous_server_finished_len = finished_len; |
| 274 | } |
| 275 | |
| 276 | return 1; |
| 277 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | f_err: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 279 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 280 | err: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 281 | return 0; |
| 282 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
| 284 | /* for these 2 messages, we need to |
| 285 | * ssl->enc_read_ctx re-init |
| 286 | * ssl->s3->read_sequence zero |
| 287 | * ssl->s3->read_mac_secret re-init |
| 288 | * ssl->session->read_sym_enc assign |
| 289 | * ssl->session->read_compression assign |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 290 | * ssl->session->read_hash assign */ |
| 291 | int ssl3_send_change_cipher_spec(SSL *s, int a, int b) { |
| 292 | if (s->state == a) { |
| 293 | *((uint8_t *)s->init_buf->data) = SSL3_MT_CCS; |
| 294 | s->init_num = 1; |
| 295 | s->init_off = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 297 | s->state = b; |
| 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 300 | /* SSL3_ST_CW_CHANGE_B */ |
| 301 | return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); |
| 302 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 304 | int ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 305 | uint8_t *p; |
| 306 | unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 308 | if (!ssl_add_cert_chain(s, cpk, &l)) { |
| 309 | return 0; |
| 310 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 312 | l -= 3 + SSL_HM_HEADER_LENGTH(s); |
| 313 | p = ssl_handshake_start(s); |
| 314 | l2n3(l, p); |
| 315 | l += 3; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 316 | return ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 317 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 319 | /* Obtain handshake message of message type |msg_type| (any if |msg_type| == -1), |
| 320 | * maximum acceptable body length |max|. The first four bytes (msg_type and |
| 321 | * length) are read in state |header_state|, the body is read in state |body_state|. */ |
| 322 | long ssl3_get_message(SSL *s, int header_state, int body_state, int msg_type, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 323 | long max, enum ssl_hash_message_t hash_message, int *ok) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 324 | uint8_t *p; |
| 325 | unsigned long l; |
| 326 | long n; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 327 | int al; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 329 | if (s->s3->tmp.reuse_message) { |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 330 | /* A ssl_dont_hash_message call cannot be combined with reuse_message; the |
| 331 | * ssl_dont_hash_message would have to have been applied to the previous |
| 332 | * call. */ |
| 333 | assert(hash_message == ssl_hash_message); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 334 | s->s3->tmp.reuse_message = 0; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 335 | if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 336 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 337 | OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_UNEXPECTED_MESSAGE); |
| 338 | goto f_err; |
| 339 | } |
| 340 | *ok = 1; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 341 | s->state = body_state; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 342 | s->init_msg = (uint8_t *)s->init_buf->data + 4; |
| 343 | s->init_num = (int)s->s3->tmp.message_size; |
| 344 | return s->init_num; |
| 345 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 347 | p = (uint8_t *)s->init_buf->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 349 | if (s->state == header_state) { |
| 350 | assert(s->init_num < 4); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 352 | for (;;) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 353 | while (s->init_num < 4) { |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 354 | int bytes_read = s->method->ssl_read_bytes( |
| 355 | s, SSL3_RT_HANDSHAKE, &p[s->init_num], 4 - s->init_num, 0); |
| 356 | if (bytes_read <= 0) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 357 | *ok = 0; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 358 | return bytes_read; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 359 | } |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 360 | s->init_num += bytes_read; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 361 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 363 | static const uint8_t kHelloRequest[4] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0}; |
| 364 | if (s->server || memcmp(p, kHelloRequest, sizeof(kHelloRequest)) != 0) { |
| 365 | break; |
| 366 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 368 | /* The server may always send 'Hello Request' messages -- we are doing |
| 369 | * a handshake anyway now, so ignore them if their format is correct. |
| 370 | * Does not count for 'Finished' MAC. */ |
| 371 | s->init_num = 0; |
| 372 | |
| 373 | if (s->msg_callback) { |
| 374 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, |
| 375 | s->msg_callback_arg); |
| 376 | } |
| 377 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 379 | /* s->init_num == 4 */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 381 | if (msg_type >= 0 && *p != msg_type) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 382 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 383 | OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_UNEXPECTED_MESSAGE); |
| 384 | goto f_err; |
| 385 | } |
| 386 | s->s3->tmp.message_type = *(p++); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 388 | n2l3(p, l); |
| 389 | if (l > (unsigned long)max) { |
| 390 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 391 | OPENSSL_PUT_ERROR(SSL, ssl3_get_message, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 392 | goto f_err; |
| 393 | } |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 394 | |
| 395 | if (l && !BUF_MEM_grow_clean(s->init_buf, l + 4)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 396 | OPENSSL_PUT_ERROR(SSL, ssl3_get_message, ERR_R_BUF_LIB); |
| 397 | goto err; |
| 398 | } |
| 399 | s->s3->tmp.message_size = l; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 400 | s->state = body_state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 402 | s->init_msg = (uint8_t *)s->init_buf->data + 4; |
| 403 | s->init_num = 0; |
| 404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 406 | /* next state (body_state) */ |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 407 | p = s->init_msg; |
| 408 | n = s->s3->tmp.message_size - s->init_num; |
| 409 | while (n > 0) { |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 410 | int bytes_read = |
| 411 | s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[s->init_num], n, 0); |
| 412 | if (bytes_read <= 0) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 413 | s->rwstate = SSL_READING; |
| 414 | *ok = 0; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 415 | return bytes_read; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 416 | } |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 417 | s->init_num += bytes_read; |
| 418 | n -= bytes_read; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Feed this message into MAC computation. */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 422 | if (hash_message == ssl_hash_message && !ssl3_hash_current_message(s)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 423 | goto err; |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 424 | } |
| 425 | if (s->msg_callback) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 426 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, |
| 427 | (size_t)s->init_num + 4, s, s->msg_callback_arg); |
Adam Langley | 6e73d62 | 2014-12-15 18:46:16 -0800 | [diff] [blame] | 428 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 429 | *ok = 1; |
| 430 | return s->init_num; |
| 431 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | f_err: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 433 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 435 | err: |
| 436 | *ok = 0; |
| 437 | return -1; |
| 438 | } |
| 439 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 440 | int ssl3_hash_current_message(SSL *s) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 441 | /* The handshake header (different size between DTLS and TLS) is included in |
| 442 | * the hash. */ |
| 443 | size_t header_len = s->init_msg - (uint8_t *)s->init_buf->data; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 444 | return ssl3_finish_mac(s, (uint8_t *)s->init_buf->data, |
| 445 | s->init_num + header_len); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 446 | } |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 447 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 448 | /* ssl3_cert_verify_hash is documented as needing EVP_MAX_MD_SIZE because that |
| 449 | * is sufficient pre-TLS1.2 as well. */ |
| 450 | OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE > MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 451 | combined_tls_hash_fits_in_max); |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 452 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 453 | int ssl3_cert_verify_hash(SSL *s, uint8_t *out, size_t *out_len, |
| 454 | const EVP_MD **out_md, EVP_PKEY *pkey) { |
| 455 | /* For TLS v1.2 send signature algorithm and signature using |
| 456 | * agreed digest and cached handshake records. Otherwise, use |
| 457 | * SHA1 or MD5 + SHA1 depending on key type. */ |
| 458 | if (SSL_USE_SIGALGS(s)) { |
| 459 | const uint8_t *hdata; |
| 460 | size_t hdatalen; |
| 461 | EVP_MD_CTX mctx; |
| 462 | unsigned len; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 463 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 464 | if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen)) { |
| 465 | OPENSSL_PUT_ERROR(SSL, ssl3_cert_verify_hash, ERR_R_INTERNAL_ERROR); |
| 466 | return 0; |
| 467 | } |
| 468 | EVP_MD_CTX_init(&mctx); |
| 469 | if (!EVP_DigestInit_ex(&mctx, *out_md, NULL) || |
| 470 | !EVP_DigestUpdate(&mctx, hdata, hdatalen) || |
| 471 | !EVP_DigestFinal(&mctx, out, &len)) { |
| 472 | OPENSSL_PUT_ERROR(SSL, ssl3_cert_verify_hash, ERR_R_EVP_LIB); |
| 473 | EVP_MD_CTX_cleanup(&mctx); |
| 474 | return 0; |
| 475 | } |
| 476 | *out_len = len; |
| 477 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 478 | if (s->enc_method->cert_verify_mac(s, NID_md5, out) == 0 || |
| 479 | s->enc_method->cert_verify_mac(s, NID_sha1, out + MD5_DIGEST_LENGTH) == |
| 480 | 0) { |
| 481 | return 0; |
| 482 | } |
| 483 | *out_len = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH; |
| 484 | *out_md = EVP_md5_sha1(); |
| 485 | } else if (pkey->type == EVP_PKEY_EC) { |
| 486 | if (s->enc_method->cert_verify_mac(s, NID_sha1, out) == 0) { |
| 487 | return 0; |
| 488 | } |
| 489 | *out_len = SHA_DIGEST_LENGTH; |
| 490 | *out_md = EVP_sha1(); |
| 491 | } else { |
| 492 | OPENSSL_PUT_ERROR(SSL, ssl3_cert_verify_hash, ERR_R_INTERNAL_ERROR); |
| 493 | return 0; |
| 494 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 495 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 496 | return 1; |
| 497 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | |
David Benjamin | 263eac0 | 2014-12-16 06:20:56 -0500 | [diff] [blame] | 499 | int ssl_cert_type(EVP_PKEY *pkey) { |
| 500 | switch (pkey->type) { |
| 501 | case EVP_PKEY_RSA: |
| 502 | return SSL_PKEY_RSA_ENC; |
| 503 | case EVP_PKEY_EC: |
| 504 | return SSL_PKEY_ECC; |
| 505 | default: |
| 506 | return -1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 507 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 508 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 510 | int ssl_verify_alarm_type(long type) { |
| 511 | int al; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 513 | switch (type) { |
| 514 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
| 515 | case X509_V_ERR_UNABLE_TO_GET_CRL: |
| 516 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: |
| 517 | al = SSL_AD_UNKNOWN_CA; |
| 518 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 520 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: |
| 521 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: |
| 522 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: |
| 523 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
| 524 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
| 525 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: |
| 526 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: |
| 527 | case X509_V_ERR_CERT_NOT_YET_VALID: |
| 528 | case X509_V_ERR_CRL_NOT_YET_VALID: |
| 529 | case X509_V_ERR_CERT_UNTRUSTED: |
| 530 | case X509_V_ERR_CERT_REJECTED: |
| 531 | al = SSL_AD_BAD_CERTIFICATE; |
| 532 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 533 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 534 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: |
| 535 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: |
| 536 | al = SSL_AD_DECRYPT_ERROR; |
| 537 | break; |
| 538 | |
| 539 | case X509_V_ERR_CERT_HAS_EXPIRED: |
| 540 | case X509_V_ERR_CRL_HAS_EXPIRED: |
| 541 | al = SSL_AD_CERTIFICATE_EXPIRED; |
| 542 | break; |
| 543 | |
| 544 | case X509_V_ERR_CERT_REVOKED: |
| 545 | al = SSL_AD_CERTIFICATE_REVOKED; |
| 546 | break; |
| 547 | |
| 548 | case X509_V_ERR_OUT_OF_MEM: |
| 549 | al = SSL_AD_INTERNAL_ERROR; |
| 550 | break; |
| 551 | |
| 552 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 553 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 554 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: |
| 555 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: |
| 556 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: |
| 557 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 558 | case X509_V_ERR_INVALID_CA: |
| 559 | al = SSL_AD_UNKNOWN_CA; |
| 560 | break; |
| 561 | |
| 562 | case X509_V_ERR_APPLICATION_VERIFICATION: |
| 563 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 564 | break; |
| 565 | |
| 566 | case X509_V_ERR_INVALID_PURPOSE: |
| 567 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 568 | break; |
| 569 | |
| 570 | default: |
| 571 | al = SSL_AD_CERTIFICATE_UNKNOWN; |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | return al; |
| 576 | } |
| 577 | |
| 578 | int ssl3_setup_read_buffer(SSL *s) { |
| 579 | uint8_t *p; |
| 580 | size_t len, align = 0, headerlen; |
| 581 | |
| 582 | if (SSL_IS_DTLS(s)) { |
| 583 | headerlen = DTLS1_RT_HEADER_LENGTH; |
| 584 | } else { |
| 585 | headerlen = SSL3_RT_HEADER_LENGTH; |
| 586 | } |
| 587 | |
| 588 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0 |
| 589 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | #endif |
| 591 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 592 | if (s->s3->rbuf.buf == NULL) { |
David Benjamin | 4a3f073 | 2015-04-05 02:21:33 -0400 | [diff] [blame] | 593 | len = SSL3_RT_MAX_ENCRYPTED_LENGTH + headerlen + align; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 594 | if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) { |
| 595 | s->s3->init_extra = 1; |
| 596 | len += SSL3_RT_MAX_EXTRA; |
| 597 | } |
| 598 | p = OPENSSL_malloc(len); |
| 599 | if (p == NULL) { |
| 600 | goto err; |
| 601 | } |
| 602 | s->s3->rbuf.buf = p; |
| 603 | s->s3->rbuf.len = len; |
| 604 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 606 | s->packet = &s->s3->rbuf.buf[0]; |
| 607 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 608 | |
| 609 | err: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 610 | OPENSSL_PUT_ERROR(SSL, ssl3_setup_read_buffer, ERR_R_MALLOC_FAILURE); |
| 611 | return 0; |
| 612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 614 | int ssl3_setup_write_buffer(SSL *s) { |
| 615 | uint8_t *p; |
| 616 | size_t len, align = 0, headerlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 618 | if (SSL_IS_DTLS(s)) { |
| 619 | headerlen = DTLS1_RT_HEADER_LENGTH + 1; |
| 620 | } else { |
| 621 | headerlen = SSL3_RT_HEADER_LENGTH; |
| 622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 624 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0 |
| 625 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 626 | #endif |
| 627 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 628 | if (s->s3->wbuf.buf == NULL) { |
| 629 | len = s->max_send_fragment + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + |
| 630 | headerlen + align; |
| 631 | /* Account for 1/n-1 record splitting. */ |
| 632 | if (s->mode & SSL_MODE_CBC_RECORD_SPLITTING) { |
| 633 | len += headerlen + align + 1 + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD; |
| 634 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 636 | p = OPENSSL_malloc(len); |
| 637 | if (p == NULL) { |
| 638 | goto err; |
| 639 | } |
| 640 | s->s3->wbuf.buf = p; |
| 641 | s->s3->wbuf.len = len; |
| 642 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 643 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 644 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
| 646 | err: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 647 | OPENSSL_PUT_ERROR(SSL, ssl3_setup_write_buffer, ERR_R_MALLOC_FAILURE); |
| 648 | return 0; |
| 649 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 650 | |
| 651 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 652 | int ssl3_setup_buffers(SSL *s) { |
| 653 | if (!ssl3_setup_read_buffer(s) || |
| 654 | !ssl3_setup_write_buffer(s)) { |
| 655 | return 0; |
| 656 | } |
| 657 | return 1; |
| 658 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 659 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 660 | int ssl3_release_write_buffer(SSL *s) { |
| 661 | if (s->s3->wbuf.buf != NULL) { |
| 662 | OPENSSL_free(s->s3->wbuf.buf); |
| 663 | s->s3->wbuf.buf = NULL; |
| 664 | } |
| 665 | return 1; |
| 666 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 668 | int ssl3_release_read_buffer(SSL *s) { |
| 669 | if (s->s3->rbuf.buf != NULL) { |
| 670 | OPENSSL_free(s->s3->rbuf.buf); |
| 671 | s->s3->rbuf.buf = NULL; |
| 672 | } |
| 673 | return 1; |
| 674 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 675 | |
Adam Langley | 139ed19 | 2014-12-13 15:35:50 -0800 | [diff] [blame] | 676 | /* ssl_fill_hello_random fills a client_random or server_random field of length |
| 677 | * |len|. Returns 0 on failure or 1 on success. */ |
| 678 | int ssl_fill_hello_random(SSL *s, int server, uint8_t *result, size_t len) { |
| 679 | int send_time = 0; |
| 680 | |
| 681 | if (server) { |
| 682 | send_time = (s->mode & SSL_MODE_SEND_SERVERHELLO_TIME) != 0; |
| 683 | } else { |
| 684 | send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0; |
| 685 | } |
| 686 | |
| 687 | if (send_time) { |
| 688 | const uint32_t current_time = time(NULL); |
| 689 | uint8_t *p = result; |
| 690 | |
| 691 | if (len < 4) { |
| 692 | return 0; |
| 693 | } |
| 694 | p[0] = current_time >> 24; |
| 695 | p[1] = current_time >> 16; |
| 696 | p[2] = current_time >> 8; |
| 697 | p[3] = current_time; |
| 698 | return RAND_bytes(p + 4, len - 4); |
| 699 | } else { |
| 700 | return RAND_bytes(result, len); |
| 701 | } |
| 702 | } |