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