[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include <limits.h>
|
---|
| 12 | #include <errno.h>
|
---|
| 13 | #define USE_SOCKETS
|
---|
| 14 | #include "../ssl_locl.h"
|
---|
| 15 | #include <openssl/evp.h>
|
---|
| 16 | #include <openssl/buffer.h>
|
---|
| 17 | #include <openssl/rand.h>
|
---|
| 18 | #include "record_locl.h"
|
---|
| 19 |
|
---|
| 20 | #if defined(OPENSSL_SMALL_FOOTPRINT) || \
|
---|
| 21 | !( defined(AES_ASM) && ( \
|
---|
| 22 | defined(__x86_64) || defined(__x86_64__) || \
|
---|
| 23 | defined(_M_AMD64) || defined(_M_X64) ) \
|
---|
| 24 | )
|
---|
| 25 | # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
---|
| 26 | # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s)
|
---|
| 30 | {
|
---|
| 31 | rl->s = s;
|
---|
| 32 | RECORD_LAYER_set_first_record(&s->rlayer);
|
---|
| 33 | SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | void RECORD_LAYER_clear(RECORD_LAYER *rl)
|
---|
| 37 | {
|
---|
| 38 | rl->rstate = SSL_ST_READ_HEADER;
|
---|
| 39 |
|
---|
| 40 | /*
|
---|
| 41 | * Do I need to clear read_ahead? As far as I can tell read_ahead did not
|
---|
| 42 | * previously get reset by SSL_clear...so I'll keep it that way..but is
|
---|
| 43 | * that right?
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | rl->packet = NULL;
|
---|
| 47 | rl->packet_length = 0;
|
---|
| 48 | rl->wnum = 0;
|
---|
| 49 | memset(rl->alert_fragment, 0, sizeof(rl->alert_fragment));
|
---|
| 50 | rl->alert_fragment_len = 0;
|
---|
| 51 | memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
|
---|
| 52 | rl->handshake_fragment_len = 0;
|
---|
| 53 | rl->wpend_tot = 0;
|
---|
| 54 | rl->wpend_type = 0;
|
---|
| 55 | rl->wpend_ret = 0;
|
---|
| 56 | rl->wpend_buf = NULL;
|
---|
| 57 |
|
---|
| 58 | SSL3_BUFFER_clear(&rl->rbuf);
|
---|
| 59 | ssl3_release_write_buffer(rl->s);
|
---|
| 60 | rl->numrpipes = 0;
|
---|
| 61 | SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
|
---|
| 62 |
|
---|
| 63 | RECORD_LAYER_reset_read_sequence(rl);
|
---|
| 64 | RECORD_LAYER_reset_write_sequence(rl);
|
---|
| 65 |
|
---|
| 66 | if (rl->d)
|
---|
| 67 | DTLS_RECORD_LAYER_clear(rl);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void RECORD_LAYER_release(RECORD_LAYER *rl)
|
---|
| 71 | {
|
---|
| 72 | if (SSL3_BUFFER_is_initialised(&rl->rbuf))
|
---|
| 73 | ssl3_release_read_buffer(rl->s);
|
---|
| 74 | if (rl->numwpipes > 0)
|
---|
| 75 | ssl3_release_write_buffer(rl->s);
|
---|
| 76 | SSL3_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
|
---|
| 80 | {
|
---|
| 81 | return SSL3_BUFFER_get_left(&rl->rbuf) != 0;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
|
---|
| 85 | {
|
---|
| 86 | return (rl->numwpipes > 0)
|
---|
| 87 | && SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len)
|
---|
| 91 | {
|
---|
| 92 | rl->packet_length = len;
|
---|
| 93 | if (len != 0) {
|
---|
| 94 | rl->rstate = SSL_ST_READ_HEADER;
|
---|
| 95 | if (!SSL3_BUFFER_is_initialised(&rl->rbuf))
|
---|
| 96 | if (!ssl3_setup_read_buffer(rl->s))
|
---|
| 97 | return 0;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | rl->packet = SSL3_BUFFER_get_buf(&rl->rbuf);
|
---|
| 101 | SSL3_BUFFER_set_data(&rl->rbuf, buf, len);
|
---|
| 102 |
|
---|
| 103 | return 1;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl)
|
---|
| 107 | {
|
---|
| 108 | memset(rl->read_sequence, 0, sizeof(rl->read_sequence));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
|
---|
| 112 | {
|
---|
| 113 | memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | int ssl3_pending(const SSL *s)
|
---|
| 117 | {
|
---|
| 118 | unsigned int i;
|
---|
| 119 | int num = 0;
|
---|
| 120 |
|
---|
| 121 | if (s->rlayer.rstate == SSL_ST_READ_BODY)
|
---|
| 122 | return 0;
|
---|
| 123 |
|
---|
| 124 | for (i = 0; i < RECORD_LAYER_get_numrpipes(&s->rlayer); i++) {
|
---|
| 125 | if (SSL3_RECORD_get_type(&s->rlayer.rrec[i])
|
---|
| 126 | != SSL3_RT_APPLICATION_DATA)
|
---|
| 127 | return 0;
|
---|
| 128 | num += SSL3_RECORD_get_length(&s->rlayer.rrec[i]);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | return num;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
|
---|
| 135 | {
|
---|
| 136 | ctx->default_read_buf_len = len;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void SSL_set_default_read_buffer_len(SSL *s, size_t len)
|
---|
| 140 | {
|
---|
| 141 | SSL3_BUFFER_set_default_len(RECORD_LAYER_get_rbuf(&s->rlayer), len);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | const char *SSL_rstate_string_long(const SSL *s)
|
---|
| 145 | {
|
---|
| 146 | switch (s->rlayer.rstate) {
|
---|
| 147 | case SSL_ST_READ_HEADER:
|
---|
| 148 | return "read header";
|
---|
| 149 | case SSL_ST_READ_BODY:
|
---|
| 150 | return "read body";
|
---|
| 151 | case SSL_ST_READ_DONE:
|
---|
| 152 | return "read done";
|
---|
| 153 | default:
|
---|
| 154 | return "unknown";
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | const char *SSL_rstate_string(const SSL *s)
|
---|
| 159 | {
|
---|
| 160 | switch (s->rlayer.rstate) {
|
---|
| 161 | case SSL_ST_READ_HEADER:
|
---|
| 162 | return "RH";
|
---|
| 163 | case SSL_ST_READ_BODY:
|
---|
| 164 | return "RB";
|
---|
| 165 | case SSL_ST_READ_DONE:
|
---|
| 166 | return "RD";
|
---|
| 167 | default:
|
---|
| 168 | return "unknown";
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * Return values are as per SSL_read()
|
---|
| 174 | */
|
---|
| 175 | int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
---|
| 176 | {
|
---|
| 177 | /*
|
---|
| 178 | * If extend == 0, obtain new n-byte packet; if extend == 1, increase
|
---|
| 179 | * packet by another n bytes. The packet will be in the sub-array of
|
---|
| 180 | * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
|
---|
| 181 | * s->rlayer.read_ahead is set, 'max' bytes may be stored in rbuf [plus
|
---|
| 182 | * s->packet_length bytes if extend == 1].)
|
---|
| 183 | * if clearold == 1, move the packet to the start of the buffer; if
|
---|
| 184 | * clearold == 0 then leave any old packets where they were
|
---|
| 185 | */
|
---|
| 186 | int i, len, left;
|
---|
| 187 | size_t align = 0;
|
---|
| 188 | unsigned char *pkt;
|
---|
| 189 | SSL3_BUFFER *rb;
|
---|
| 190 |
|
---|
| 191 | if (n <= 0)
|
---|
| 192 | return n;
|
---|
| 193 |
|
---|
| 194 | rb = &s->rlayer.rbuf;
|
---|
| 195 | if (rb->buf == NULL)
|
---|
| 196 | if (!ssl3_setup_read_buffer(s))
|
---|
| 197 | return -1;
|
---|
| 198 |
|
---|
| 199 | left = rb->left;
|
---|
| 200 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
---|
| 201 | align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
|
---|
| 202 | align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
---|
| 203 | #endif
|
---|
| 204 |
|
---|
| 205 | if (!extend) {
|
---|
| 206 | /* start with empty packet ... */
|
---|
| 207 | if (left == 0)
|
---|
| 208 | rb->offset = align;
|
---|
| 209 | else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
|
---|
| 210 | /*
|
---|
| 211 | * check if next packet length is large enough to justify payload
|
---|
| 212 | * alignment...
|
---|
| 213 | */
|
---|
| 214 | pkt = rb->buf + rb->offset;
|
---|
| 215 | if (pkt[0] == SSL3_RT_APPLICATION_DATA
|
---|
| 216 | && (pkt[3] << 8 | pkt[4]) >= 128) {
|
---|
| 217 | /*
|
---|
| 218 | * Note that even if packet is corrupted and its length field
|
---|
| 219 | * is insane, we can only be led to wrong decision about
|
---|
| 220 | * whether memmove will occur or not. Header values has no
|
---|
| 221 | * effect on memmove arguments and therefore no buffer
|
---|
| 222 | * overrun can be triggered.
|
---|
| 223 | */
|
---|
| 224 | memmove(rb->buf + align, pkt, left);
|
---|
| 225 | rb->offset = align;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | s->rlayer.packet = rb->buf + rb->offset;
|
---|
| 229 | s->rlayer.packet_length = 0;
|
---|
| 230 | /* ... now we can act as if 'extend' was set */
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | len = s->rlayer.packet_length;
|
---|
| 234 | pkt = rb->buf + align;
|
---|
| 235 | /*
|
---|
| 236 | * Move any available bytes to front of buffer: 'len' bytes already
|
---|
| 237 | * pointed to by 'packet', 'left' extra ones at the end
|
---|
| 238 | */
|
---|
| 239 | if (s->rlayer.packet != pkt && clearold == 1) {
|
---|
| 240 | memmove(pkt, s->rlayer.packet, len + left);
|
---|
| 241 | s->rlayer.packet = pkt;
|
---|
| 242 | rb->offset = len + align;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /*
|
---|
| 246 | * For DTLS/UDP reads should not span multiple packets because the read
|
---|
| 247 | * operation returns the whole packet at once (as long as it fits into
|
---|
| 248 | * the buffer).
|
---|
| 249 | */
|
---|
| 250 | if (SSL_IS_DTLS(s)) {
|
---|
| 251 | if (left == 0 && extend)
|
---|
| 252 | return 0;
|
---|
| 253 | if (left > 0 && n > left)
|
---|
| 254 | n = left;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /* if there is enough in the buffer from a previous read, take some */
|
---|
| 258 | if (left >= n) {
|
---|
| 259 | s->rlayer.packet_length += n;
|
---|
| 260 | rb->left = left - n;
|
---|
| 261 | rb->offset += n;
|
---|
| 262 | return (n);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /* else we need to read more data */
|
---|
| 266 |
|
---|
| 267 | if (n > (int)(rb->len - rb->offset)) { /* does not happen */
|
---|
| 268 | SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
|
---|
| 269 | return -1;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /* We always act like read_ahead is set for DTLS */
|
---|
| 273 | if (!s->rlayer.read_ahead && !SSL_IS_DTLS(s))
|
---|
| 274 | /* ignore max parameter */
|
---|
| 275 | max = n;
|
---|
| 276 | else {
|
---|
| 277 | if (max < n)
|
---|
| 278 | max = n;
|
---|
| 279 | if (max > (int)(rb->len - rb->offset))
|
---|
| 280 | max = rb->len - rb->offset;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | while (left < n) {
|
---|
| 284 | /*
|
---|
| 285 | * Now we have len+left bytes at the front of s->s3->rbuf.buf and
|
---|
| 286 | * need to read in more until we have len+n (up to len+max if
|
---|
| 287 | * possible)
|
---|
| 288 | */
|
---|
| 289 |
|
---|
| 290 | clear_sys_error();
|
---|
| 291 | if (s->rbio != NULL) {
|
---|
| 292 | s->rwstate = SSL_READING;
|
---|
| 293 | i = BIO_read(s->rbio, pkt + len + left, max - left);
|
---|
| 294 | } else {
|
---|
| 295 | SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
|
---|
| 296 | i = -1;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | if (i <= 0) {
|
---|
| 300 | rb->left = left;
|
---|
| 301 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
|
---|
| 302 | if (len + left == 0)
|
---|
| 303 | ssl3_release_read_buffer(s);
|
---|
| 304 | return i;
|
---|
| 305 | }
|
---|
| 306 | left += i;
|
---|
| 307 | /*
|
---|
| 308 | * reads should *never* span multiple packets for DTLS because the
|
---|
| 309 | * underlying transport protocol is message oriented as opposed to
|
---|
| 310 | * byte oriented as in the TLS case.
|
---|
| 311 | */
|
---|
| 312 | if (SSL_IS_DTLS(s)) {
|
---|
| 313 | if (n > left)
|
---|
| 314 | n = left; /* makes the while condition false */
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /* done reading, now the book-keeping */
|
---|
| 319 | rb->offset += n;
|
---|
| 320 | rb->left = left - n;
|
---|
| 321 | s->rlayer.packet_length += n;
|
---|
| 322 | s->rwstate = SSL_NOTHING;
|
---|
| 323 | return (n);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /*
|
---|
| 327 | * Call this to write data in records of type 'type' It will return <= 0 if
|
---|
| 328 | * not all data has been sent or non-blocking IO.
|
---|
| 329 | */
|
---|
| 330 | int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
---|
| 331 | {
|
---|
| 332 | const unsigned char *buf = buf_;
|
---|
| 333 | int tot;
|
---|
| 334 | unsigned int n, split_send_fragment, maxpipes;
|
---|
| 335 | #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
---|
| 336 | unsigned int max_send_fragment, nw;
|
---|
| 337 | unsigned int u_len = (unsigned int)len;
|
---|
| 338 | #endif
|
---|
| 339 | SSL3_BUFFER *wb = &s->rlayer.wbuf[0];
|
---|
| 340 | int i;
|
---|
| 341 |
|
---|
| 342 | if (len < 0) {
|
---|
| 343 | SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_NEGATIVE_LENGTH);
|
---|
| 344 | return -1;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | s->rwstate = SSL_NOTHING;
|
---|
| 348 | tot = s->rlayer.wnum;
|
---|
| 349 | /*
|
---|
| 350 | * ensure that if we end up with a smaller value of data to write out
|
---|
| 351 | * than the the original len from a write which didn't complete for
|
---|
| 352 | * non-blocking I/O and also somehow ended up avoiding the check for
|
---|
| 353 | * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
|
---|
| 354 | * possible to end up with (len-tot) as a large number that will then
|
---|
| 355 | * promptly send beyond the end of the users buffer ... so we trap and
|
---|
| 356 | * report the error in a way the user will notice
|
---|
| 357 | */
|
---|
| 358 | if ((unsigned int)len < s->rlayer.wnum) {
|
---|
| 359 | SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
|
---|
| 360 | return -1;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | s->rlayer.wnum = 0;
|
---|
| 364 |
|
---|
| 365 | if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
|
---|
| 366 | i = s->handshake_func(s);
|
---|
| 367 | if (i < 0)
|
---|
| 368 | return (i);
|
---|
| 369 | if (i == 0) {
|
---|
| 370 | SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
---|
| 371 | return -1;
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /*
|
---|
| 376 | * first check if there is a SSL3_BUFFER still being written out. This
|
---|
| 377 | * will happen with non blocking IO
|
---|
| 378 | */
|
---|
| 379 | if (wb->left != 0) {
|
---|
| 380 | i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot);
|
---|
| 381 | if (i <= 0) {
|
---|
| 382 | /* XXX should we ssl3_release_write_buffer if i<0? */
|
---|
| 383 | s->rlayer.wnum = tot;
|
---|
| 384 | return i;
|
---|
| 385 | }
|
---|
| 386 | tot += i; /* this might be last fragment */
|
---|
| 387 | }
|
---|
| 388 | #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
---|
| 389 | /*
|
---|
| 390 | * Depending on platform multi-block can deliver several *times*
|
---|
| 391 | * better performance. Downside is that it has to allocate
|
---|
| 392 | * jumbo buffer to accommodate up to 8 records, but the
|
---|
| 393 | * compromise is considered worthy.
|
---|
| 394 | */
|
---|
| 395 | if (type == SSL3_RT_APPLICATION_DATA &&
|
---|
| 396 | u_len >= 4 * (max_send_fragment = s->max_send_fragment) &&
|
---|
| 397 | s->compress == NULL && s->msg_callback == NULL &&
|
---|
| 398 | !SSL_WRITE_ETM(s) && SSL_USE_EXPLICIT_IV(s) &&
|
---|
| 399 | EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
|
---|
| 400 | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
|
---|
| 401 | unsigned char aad[13];
|
---|
| 402 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
|
---|
| 403 | int packlen;
|
---|
| 404 |
|
---|
| 405 | /* minimize address aliasing conflicts */
|
---|
| 406 | if ((max_send_fragment & 0xfff) == 0)
|
---|
| 407 | max_send_fragment -= 512;
|
---|
| 408 |
|
---|
| 409 | if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
|
---|
| 410 | ssl3_release_write_buffer(s);
|
---|
| 411 |
|
---|
| 412 | packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
---|
| 413 | EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
|
---|
| 414 | max_send_fragment, NULL);
|
---|
| 415 |
|
---|
| 416 | if (u_len >= 8 * max_send_fragment)
|
---|
| 417 | packlen *= 8;
|
---|
| 418 | else
|
---|
| 419 | packlen *= 4;
|
---|
| 420 |
|
---|
| 421 | if (!ssl3_setup_write_buffer(s, 1, packlen)) {
|
---|
| 422 | SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_MALLOC_FAILURE);
|
---|
| 423 | return -1;
|
---|
| 424 | }
|
---|
| 425 | } else if (tot == len) { /* done? */
|
---|
| 426 | /* free jumbo buffer */
|
---|
| 427 | ssl3_release_write_buffer(s);
|
---|
| 428 | return tot;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | n = (len - tot);
|
---|
| 432 | for (;;) {
|
---|
| 433 | if (n < 4 * max_send_fragment) {
|
---|
| 434 | /* free jumbo buffer */
|
---|
| 435 | ssl3_release_write_buffer(s);
|
---|
| 436 | break;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | if (s->s3->alert_dispatch) {
|
---|
| 440 | i = s->method->ssl_dispatch_alert(s);
|
---|
| 441 | if (i <= 0) {
|
---|
| 442 | s->rlayer.wnum = tot;
|
---|
| 443 | return i;
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | if (n >= 8 * max_send_fragment)
|
---|
| 448 | nw = max_send_fragment * (mb_param.interleave = 8);
|
---|
| 449 | else
|
---|
| 450 | nw = max_send_fragment * (mb_param.interleave = 4);
|
---|
| 451 |
|
---|
| 452 | memcpy(aad, s->rlayer.write_sequence, 8);
|
---|
| 453 | aad[8] = type;
|
---|
| 454 | aad[9] = (unsigned char)(s->version >> 8);
|
---|
| 455 | aad[10] = (unsigned char)(s->version);
|
---|
| 456 | aad[11] = 0;
|
---|
| 457 | aad[12] = 0;
|
---|
| 458 | mb_param.out = NULL;
|
---|
| 459 | mb_param.inp = aad;
|
---|
| 460 | mb_param.len = nw;
|
---|
| 461 |
|
---|
| 462 | packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
---|
| 463 | EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
|
---|
| 464 | sizeof(mb_param), &mb_param);
|
---|
| 465 |
|
---|
| 466 | if (packlen <= 0 || packlen > (int)wb->len) { /* never happens */
|
---|
| 467 | /* free jumbo buffer */
|
---|
| 468 | ssl3_release_write_buffer(s);
|
---|
| 469 | break;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | mb_param.out = wb->buf;
|
---|
| 473 | mb_param.inp = &buf[tot];
|
---|
| 474 | mb_param.len = nw;
|
---|
| 475 |
|
---|
| 476 | if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
---|
| 477 | EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
|
---|
| 478 | sizeof(mb_param), &mb_param) <= 0)
|
---|
| 479 | return -1;
|
---|
| 480 |
|
---|
| 481 | s->rlayer.write_sequence[7] += mb_param.interleave;
|
---|
| 482 | if (s->rlayer.write_sequence[7] < mb_param.interleave) {
|
---|
| 483 | int j = 6;
|
---|
| 484 | while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | wb->offset = 0;
|
---|
| 488 | wb->left = packlen;
|
---|
| 489 |
|
---|
| 490 | s->rlayer.wpend_tot = nw;
|
---|
| 491 | s->rlayer.wpend_buf = &buf[tot];
|
---|
| 492 | s->rlayer.wpend_type = type;
|
---|
| 493 | s->rlayer.wpend_ret = nw;
|
---|
| 494 |
|
---|
| 495 | i = ssl3_write_pending(s, type, &buf[tot], nw);
|
---|
| 496 | if (i <= 0) {
|
---|
| 497 | if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
|
---|
| 498 | /* free jumbo buffer */
|
---|
| 499 | ssl3_release_write_buffer(s);
|
---|
| 500 | }
|
---|
| 501 | s->rlayer.wnum = tot;
|
---|
| 502 | return i;
|
---|
| 503 | }
|
---|
| 504 | if (i == (int)n) {
|
---|
| 505 | /* free jumbo buffer */
|
---|
| 506 | ssl3_release_write_buffer(s);
|
---|
| 507 | return tot + i;
|
---|
| 508 | }
|
---|
| 509 | n -= i;
|
---|
| 510 | tot += i;
|
---|
| 511 | }
|
---|
| 512 | } else
|
---|
| 513 | #endif
|
---|
| 514 | if (tot == len) { /* done? */
|
---|
| 515 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
|
---|
| 516 | ssl3_release_write_buffer(s);
|
---|
| 517 |
|
---|
| 518 | return tot;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | n = (len - tot);
|
---|
| 522 |
|
---|
| 523 | split_send_fragment = s->split_send_fragment;
|
---|
| 524 | /*
|
---|
| 525 | * If max_pipelines is 0 then this means "undefined" and we default to
|
---|
| 526 | * 1 pipeline. Similarly if the cipher does not support pipelined
|
---|
| 527 | * processing then we also only use 1 pipeline, or if we're not using
|
---|
| 528 | * explicit IVs
|
---|
| 529 | */
|
---|
| 530 | maxpipes = s->max_pipelines;
|
---|
| 531 | if (maxpipes > SSL_MAX_PIPELINES) {
|
---|
| 532 | /*
|
---|
| 533 | * We should have prevented this when we set max_pipelines so we
|
---|
| 534 | * shouldn't get here
|
---|
| 535 | */
|
---|
| 536 | SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 537 | return -1;
|
---|
| 538 | }
|
---|
| 539 | if (maxpipes == 0
|
---|
| 540 | || s->enc_write_ctx == NULL
|
---|
| 541 | || !(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx))
|
---|
| 542 | & EVP_CIPH_FLAG_PIPELINE)
|
---|
| 543 | || !SSL_USE_EXPLICIT_IV(s))
|
---|
| 544 | maxpipes = 1;
|
---|
| 545 | if (s->max_send_fragment == 0 || split_send_fragment > s->max_send_fragment
|
---|
| 546 | || split_send_fragment == 0) {
|
---|
| 547 | /*
|
---|
| 548 | * We should have prevented this when we set the split and max send
|
---|
| 549 | * fragments so we shouldn't get here
|
---|
| 550 | */
|
---|
| 551 | SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 552 | return -1;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | for (;;) {
|
---|
| 556 | unsigned int pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;
|
---|
| 557 | unsigned int numpipes, j;
|
---|
| 558 |
|
---|
| 559 | if (n == 0)
|
---|
| 560 | numpipes = 1;
|
---|
| 561 | else
|
---|
| 562 | numpipes = ((n - 1) / split_send_fragment) + 1;
|
---|
| 563 | if (numpipes > maxpipes)
|
---|
| 564 | numpipes = maxpipes;
|
---|
| 565 |
|
---|
| 566 | if (n / numpipes >= s->max_send_fragment) {
|
---|
| 567 | /*
|
---|
| 568 | * We have enough data to completely fill all available
|
---|
| 569 | * pipelines
|
---|
| 570 | */
|
---|
| 571 | for (j = 0; j < numpipes; j++) {
|
---|
| 572 | pipelens[j] = s->max_send_fragment;
|
---|
| 573 | }
|
---|
| 574 | } else {
|
---|
| 575 | /* We can partially fill all available pipelines */
|
---|
| 576 | tmppipelen = n / numpipes;
|
---|
| 577 | remain = n % numpipes;
|
---|
| 578 | for (j = 0; j < numpipes; j++) {
|
---|
| 579 | pipelens[j] = tmppipelen;
|
---|
| 580 | if (j < remain)
|
---|
| 581 | pipelens[j]++;
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0);
|
---|
| 586 | if (i <= 0) {
|
---|
| 587 | /* XXX should we ssl3_release_write_buffer if i<0? */
|
---|
| 588 | s->rlayer.wnum = tot;
|
---|
| 589 | return i;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | if ((i == (int)n) ||
|
---|
| 593 | (type == SSL3_RT_APPLICATION_DATA &&
|
---|
| 594 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
|
---|
| 595 | /*
|
---|
| 596 | * next chunk of data should get another prepended empty fragment
|
---|
| 597 | * in ciphersuites with known-IV weakness:
|
---|
| 598 | */
|
---|
| 599 | s->s3->empty_fragment_done = 0;
|
---|
| 600 |
|
---|
| 601 | if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&
|
---|
| 602 | !SSL_IS_DTLS(s))
|
---|
| 603 | ssl3_release_write_buffer(s);
|
---|
| 604 |
|
---|
| 605 | return tot + i;
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | n -= i;
|
---|
| 609 | tot += i;
|
---|
| 610 | }
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
---|
| 614 | unsigned int *pipelens, unsigned int numpipes,
|
---|
| 615 | int create_empty_fragment)
|
---|
| 616 | {
|
---|
| 617 | unsigned char *outbuf[SSL_MAX_PIPELINES], *plen[SSL_MAX_PIPELINES];
|
---|
| 618 | SSL3_RECORD wr[SSL_MAX_PIPELINES];
|
---|
| 619 | int i, mac_size, clear = 0;
|
---|
| 620 | int prefix_len = 0;
|
---|
| 621 | int eivlen;
|
---|
| 622 | size_t align = 0;
|
---|
| 623 | SSL3_BUFFER *wb;
|
---|
| 624 | SSL_SESSION *sess;
|
---|
| 625 | unsigned int totlen = 0;
|
---|
| 626 | unsigned int j;
|
---|
| 627 |
|
---|
| 628 | for (j = 0; j < numpipes; j++)
|
---|
| 629 | totlen += pipelens[j];
|
---|
| 630 | /*
|
---|
| 631 | * first check if there is a SSL3_BUFFER still being written out. This
|
---|
| 632 | * will happen with non blocking IO
|
---|
| 633 | */
|
---|
| 634 | if (RECORD_LAYER_write_pending(&s->rlayer))
|
---|
| 635 | return (ssl3_write_pending(s, type, buf, totlen));
|
---|
| 636 |
|
---|
| 637 | /* If we have an alert to send, lets send it */
|
---|
| 638 | if (s->s3->alert_dispatch) {
|
---|
| 639 | i = s->method->ssl_dispatch_alert(s);
|
---|
| 640 | if (i <= 0)
|
---|
| 641 | return (i);
|
---|
| 642 | /* if it went, fall through and send more stuff */
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | if (s->rlayer.numwpipes < numpipes)
|
---|
| 646 | if (!ssl3_setup_write_buffer(s, numpipes, 0))
|
---|
| 647 | return -1;
|
---|
| 648 |
|
---|
| 649 | if (totlen == 0 && !create_empty_fragment)
|
---|
| 650 | return 0;
|
---|
| 651 |
|
---|
| 652 | sess = s->session;
|
---|
| 653 |
|
---|
| 654 | if ((sess == NULL) ||
|
---|
| 655 | (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {
|
---|
| 656 | clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
|
---|
| 657 | mac_size = 0;
|
---|
| 658 | } else {
|
---|
| 659 | mac_size = EVP_MD_CTX_size(s->write_hash);
|
---|
| 660 | if (mac_size < 0)
|
---|
| 661 | goto err;
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | /*
|
---|
| 665 | * 'create_empty_fragment' is true only when this function calls itself
|
---|
| 666 | */
|
---|
| 667 | if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
|
---|
| 668 | /*
|
---|
| 669 | * countermeasure against known-IV weakness in CBC ciphersuites (see
|
---|
| 670 | * http://www.openssl.org/~bodo/tls-cbc.txt)
|
---|
| 671 | */
|
---|
| 672 |
|
---|
| 673 | if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
|
---|
| 674 | /*
|
---|
| 675 | * recursive function call with 'create_empty_fragment' set; this
|
---|
| 676 | * prepares and buffers the data for an empty fragment (these
|
---|
| 677 | * 'prefix_len' bytes are sent out later together with the actual
|
---|
| 678 | * payload)
|
---|
| 679 | */
|
---|
| 680 | unsigned int tmppipelen = 0;
|
---|
| 681 |
|
---|
| 682 | prefix_len = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1);
|
---|
| 683 | if (prefix_len <= 0)
|
---|
| 684 | goto err;
|
---|
| 685 |
|
---|
| 686 | if (prefix_len >
|
---|
| 687 | (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
|
---|
| 688 | /* insufficient space */
|
---|
| 689 | SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
|
---|
| 690 | goto err;
|
---|
| 691 | }
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | s->s3->empty_fragment_done = 1;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | if (create_empty_fragment) {
|
---|
| 698 | wb = &s->rlayer.wbuf[0];
|
---|
| 699 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
---|
| 700 | /*
|
---|
| 701 | * extra fragment would be couple of cipher blocks, which would be
|
---|
| 702 | * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
|
---|
| 703 | * payload, then we can just pretend we simply have two headers.
|
---|
| 704 | */
|
---|
| 705 | align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;
|
---|
| 706 | align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
---|
| 707 | #endif
|
---|
| 708 | outbuf[0] = SSL3_BUFFER_get_buf(wb) + align;
|
---|
| 709 | SSL3_BUFFER_set_offset(wb, align);
|
---|
| 710 | } else if (prefix_len) {
|
---|
| 711 | wb = &s->rlayer.wbuf[0];
|
---|
| 712 | outbuf[0] = SSL3_BUFFER_get_buf(wb) + SSL3_BUFFER_get_offset(wb)
|
---|
| 713 | + prefix_len;
|
---|
| 714 | } else {
|
---|
| 715 | for (j = 0; j < numpipes; j++) {
|
---|
| 716 | wb = &s->rlayer.wbuf[j];
|
---|
| 717 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
---|
| 718 | align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
|
---|
| 719 | align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
---|
| 720 | #endif
|
---|
| 721 | outbuf[j] = SSL3_BUFFER_get_buf(wb) + align;
|
---|
| 722 | SSL3_BUFFER_set_offset(wb, align);
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | /* Explicit IV length, block ciphers appropriate version flag */
|
---|
| 727 | if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
|
---|
| 728 | int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
|
---|
| 729 | if (mode == EVP_CIPH_CBC_MODE) {
|
---|
| 730 | eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
|
---|
| 731 | if (eivlen <= 1)
|
---|
| 732 | eivlen = 0;
|
---|
| 733 | }
|
---|
| 734 | /* Need explicit part of IV for GCM mode */
|
---|
| 735 | else if (mode == EVP_CIPH_GCM_MODE)
|
---|
| 736 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
---|
| 737 | else if (mode == EVP_CIPH_CCM_MODE)
|
---|
| 738 | eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
---|
| 739 | else
|
---|
| 740 | eivlen = 0;
|
---|
| 741 | } else
|
---|
| 742 | eivlen = 0;
|
---|
| 743 |
|
---|
| 744 | totlen = 0;
|
---|
| 745 | /* Clear our SSL3_RECORD structures */
|
---|
| 746 | memset(wr, 0, sizeof wr);
|
---|
| 747 | for (j = 0; j < numpipes; j++) {
|
---|
| 748 | /* write the header */
|
---|
| 749 | *(outbuf[j]++) = type & 0xff;
|
---|
| 750 | SSL3_RECORD_set_type(&wr[j], type);
|
---|
| 751 |
|
---|
| 752 | *(outbuf[j]++) = (s->version >> 8);
|
---|
| 753 | /*
|
---|
| 754 | * Some servers hang if initial client hello is larger than 256 bytes
|
---|
| 755 | * and record version number > TLS 1.0
|
---|
| 756 | */
|
---|
| 757 | if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
|
---|
| 758 | && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
|
---|
| 759 | *(outbuf[j]++) = 0x1;
|
---|
| 760 | else
|
---|
| 761 | *(outbuf[j]++) = s->version & 0xff;
|
---|
| 762 |
|
---|
| 763 | /* field where we are to write out packet length */
|
---|
| 764 | plen[j] = outbuf[j];
|
---|
| 765 | outbuf[j] += 2;
|
---|
| 766 |
|
---|
| 767 | /* lets setup the record stuff. */
|
---|
| 768 | SSL3_RECORD_set_data(&wr[j], outbuf[j] + eivlen);
|
---|
| 769 | SSL3_RECORD_set_length(&wr[j], (int)pipelens[j]);
|
---|
| 770 | SSL3_RECORD_set_input(&wr[j], (unsigned char *)&buf[totlen]);
|
---|
| 771 | totlen += pipelens[j];
|
---|
| 772 |
|
---|
| 773 | /*
|
---|
| 774 | * we now 'read' from wr->input, wr->length bytes into wr->data
|
---|
| 775 | */
|
---|
| 776 |
|
---|
| 777 | /* first we compress */
|
---|
| 778 | if (s->compress != NULL) {
|
---|
| 779 | if (!ssl3_do_compress(s, &wr[j])) {
|
---|
| 780 | SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
|
---|
| 781 | goto err;
|
---|
| 782 | }
|
---|
| 783 | } else {
|
---|
| 784 | memcpy(wr[j].data, wr[j].input, wr[j].length);
|
---|
| 785 | SSL3_RECORD_reset_input(&wr[j]);
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | /*
|
---|
| 789 | * we should still have the output to wr->data and the input from
|
---|
| 790 | * wr->input. Length should be wr->length. wr->data still points in the
|
---|
| 791 | * wb->buf
|
---|
| 792 | */
|
---|
| 793 |
|
---|
| 794 | if (!SSL_WRITE_ETM(s) && mac_size != 0) {
|
---|
| 795 | if (s->method->ssl3_enc->mac(s, &wr[j],
|
---|
| 796 | &(outbuf[j][wr[j].length + eivlen]),
|
---|
| 797 | 1) < 0)
|
---|
| 798 | goto err;
|
---|
| 799 | SSL3_RECORD_add_length(&wr[j], mac_size);
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | SSL3_RECORD_set_data(&wr[j], outbuf[j]);
|
---|
| 803 | SSL3_RECORD_reset_input(&wr[j]);
|
---|
| 804 |
|
---|
| 805 | if (eivlen) {
|
---|
| 806 | /*
|
---|
| 807 | * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
|
---|
| 808 | */
|
---|
| 809 | SSL3_RECORD_add_length(&wr[j], eivlen);
|
---|
| 810 | }
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1)
|
---|
| 814 | goto err;
|
---|
| 815 |
|
---|
| 816 | for (j = 0; j < numpipes; j++) {
|
---|
| 817 | if (SSL_WRITE_ETM(s) && mac_size != 0) {
|
---|
| 818 | if (s->method->ssl3_enc->mac(s, &wr[j],
|
---|
| 819 | outbuf[j] + wr[j].length, 1) < 0)
|
---|
| 820 | goto err;
|
---|
| 821 | SSL3_RECORD_add_length(&wr[j], mac_size);
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | /* record length after mac and block padding */
|
---|
| 825 | s2n(SSL3_RECORD_get_length(&wr[j]), plen[j]);
|
---|
| 826 |
|
---|
| 827 | if (s->msg_callback)
|
---|
| 828 | s->msg_callback(1, 0, SSL3_RT_HEADER, plen[j] - 5, 5, s,
|
---|
| 829 | s->msg_callback_arg);
|
---|
| 830 |
|
---|
| 831 | /*
|
---|
| 832 | * we should now have wr->data pointing to the encrypted data, which is
|
---|
| 833 | * wr->length long
|
---|
| 834 | */
|
---|
| 835 | SSL3_RECORD_set_type(&wr[j], type); /* not needed but helps for
|
---|
| 836 | * debugging */
|
---|
| 837 | SSL3_RECORD_add_length(&wr[j], SSL3_RT_HEADER_LENGTH);
|
---|
| 838 |
|
---|
| 839 | if (create_empty_fragment) {
|
---|
| 840 | /*
|
---|
| 841 | * we are in a recursive call; just return the length, don't write
|
---|
| 842 | * out anything here
|
---|
| 843 | */
|
---|
| 844 | if (j > 0) {
|
---|
| 845 | /* We should never be pipelining an empty fragment!! */
|
---|
| 846 | SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
|
---|
| 847 | goto err;
|
---|
| 848 | }
|
---|
| 849 | return SSL3_RECORD_get_length(wr);
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | /* now let's set up wb */
|
---|
| 853 | SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],
|
---|
| 854 | prefix_len + SSL3_RECORD_get_length(&wr[j]));
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 | /*
|
---|
| 858 | * memorize arguments so that ssl3_write_pending can detect bad write
|
---|
| 859 | * retries later
|
---|
| 860 | */
|
---|
| 861 | s->rlayer.wpend_tot = totlen;
|
---|
| 862 | s->rlayer.wpend_buf = buf;
|
---|
| 863 | s->rlayer.wpend_type = type;
|
---|
| 864 | s->rlayer.wpend_ret = totlen;
|
---|
| 865 |
|
---|
| 866 | /* we now just need to write the buffer */
|
---|
| 867 | return ssl3_write_pending(s, type, buf, totlen);
|
---|
| 868 | err:
|
---|
| 869 | return -1;
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 | /* if s->s3->wbuf.left != 0, we need to call this
|
---|
| 873 | *
|
---|
| 874 | * Return values are as per SSL_write()
|
---|
| 875 | */
|
---|
| 876 | int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
---|
| 877 | unsigned int len)
|
---|
| 878 | {
|
---|
| 879 | int i;
|
---|
| 880 | SSL3_BUFFER *wb = s->rlayer.wbuf;
|
---|
| 881 | unsigned int currbuf = 0;
|
---|
| 882 |
|
---|
| 883 | /* XXXX */
|
---|
| 884 | if ((s->rlayer.wpend_tot > (int)len)
|
---|
| 885 | || ((s->rlayer.wpend_buf != buf) &&
|
---|
| 886 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
|
---|
| 887 | || (s->rlayer.wpend_type != type)) {
|
---|
| 888 | SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
|
---|
| 889 | return (-1);
|
---|
| 890 | }
|
---|
| 891 |
|
---|
| 892 | for (;;) {
|
---|
| 893 | /* Loop until we find a buffer we haven't written out yet */
|
---|
| 894 | if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
|
---|
| 895 | && currbuf < s->rlayer.numwpipes - 1) {
|
---|
| 896 | currbuf++;
|
---|
| 897 | continue;
|
---|
| 898 | }
|
---|
| 899 | clear_sys_error();
|
---|
| 900 | if (s->wbio != NULL) {
|
---|
| 901 | s->rwstate = SSL_WRITING;
|
---|
| 902 | i = BIO_write(s->wbio, (char *)
|
---|
| 903 | &(SSL3_BUFFER_get_buf(&wb[currbuf])
|
---|
| 904 | [SSL3_BUFFER_get_offset(&wb[currbuf])]),
|
---|
| 905 | (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
|
---|
| 906 | } else {
|
---|
| 907 | SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
|
---|
| 908 | i = -1;
|
---|
| 909 | }
|
---|
| 910 | if (i == SSL3_BUFFER_get_left(&wb[currbuf])) {
|
---|
| 911 | SSL3_BUFFER_set_left(&wb[currbuf], 0);
|
---|
| 912 | SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
---|
| 913 | if (currbuf + 1 < s->rlayer.numwpipes)
|
---|
| 914 | continue;
|
---|
| 915 | s->rwstate = SSL_NOTHING;
|
---|
| 916 | return (s->rlayer.wpend_ret);
|
---|
| 917 | } else if (i <= 0) {
|
---|
| 918 | if (SSL_IS_DTLS(s)) {
|
---|
| 919 | /*
|
---|
| 920 | * For DTLS, just drop it. That's kind of the whole point in
|
---|
| 921 | * using a datagram service
|
---|
| 922 | */
|
---|
| 923 | SSL3_BUFFER_set_left(&wb[currbuf], 0);
|
---|
| 924 | }
|
---|
| 925 | return i;
|
---|
| 926 | }
|
---|
| 927 | SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
---|
| 928 | SSL3_BUFFER_add_left(&wb[currbuf], -i);
|
---|
| 929 | }
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | /*-
|
---|
| 933 | * Return up to 'len' payload bytes received in 'type' records.
|
---|
| 934 | * 'type' is one of the following:
|
---|
| 935 | *
|
---|
| 936 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
|
---|
| 937 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
|
---|
| 938 | * - 0 (during a shutdown, no data has to be returned)
|
---|
| 939 | *
|
---|
| 940 | * If we don't have stored data to work from, read a SSL/TLS record first
|
---|
| 941 | * (possibly multiple records if we still don't have anything to return).
|
---|
| 942 | *
|
---|
| 943 | * This function must handle any surprises the peer may have for us, such as
|
---|
| 944 | * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
|
---|
| 945 | * messages are treated as if they were handshake messages *if* the |recd_type|
|
---|
| 946 | * argument is non NULL.
|
---|
| 947 | * Also if record payloads contain fragments too small to process, we store
|
---|
| 948 | * them until there is enough for the respective protocol (the record protocol
|
---|
| 949 | * may use arbitrary fragmentation and even interleaving):
|
---|
| 950 | * Change cipher spec protocol
|
---|
| 951 | * just 1 byte needed, no need for keeping anything stored
|
---|
| 952 | * Alert protocol
|
---|
| 953 | * 2 bytes needed (AlertLevel, AlertDescription)
|
---|
| 954 | * Handshake protocol
|
---|
| 955 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have
|
---|
| 956 | * to detect unexpected Client Hello and Hello Request messages
|
---|
| 957 | * here, anything else is handled by higher layers
|
---|
| 958 | * Application data protocol
|
---|
| 959 | * none of our business
|
---|
| 960 | */
|
---|
| 961 | int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
---|
| 962 | int len, int peek)
|
---|
| 963 | {
|
---|
| 964 | int al, i, j, ret;
|
---|
| 965 | unsigned int n, curr_rec, num_recs, read_bytes;
|
---|
| 966 | SSL3_RECORD *rr;
|
---|
| 967 | SSL3_BUFFER *rbuf;
|
---|
| 968 | void (*cb) (const SSL *ssl, int type2, int val) = NULL;
|
---|
| 969 |
|
---|
| 970 | rbuf = &s->rlayer.rbuf;
|
---|
| 971 |
|
---|
| 972 | if (!SSL3_BUFFER_is_initialised(rbuf)) {
|
---|
| 973 | /* Not initialized yet */
|
---|
| 974 | if (!ssl3_setup_read_buffer(s))
|
---|
| 975 | return (-1);
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | if ((type && (type != SSL3_RT_APPLICATION_DATA)
|
---|
| 979 | && (type != SSL3_RT_HANDSHAKE)) || (peek
|
---|
| 980 | && (type !=
|
---|
| 981 | SSL3_RT_APPLICATION_DATA))) {
|
---|
| 982 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 983 | return -1;
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
|
---|
| 987 | /* (partially) satisfy request from storage */
|
---|
| 988 | {
|
---|
| 989 | unsigned char *src = s->rlayer.handshake_fragment;
|
---|
| 990 | unsigned char *dst = buf;
|
---|
| 991 | unsigned int k;
|
---|
| 992 |
|
---|
| 993 | /* peek == 0 */
|
---|
| 994 | n = 0;
|
---|
| 995 | while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
|
---|
| 996 | *dst++ = *src++;
|
---|
| 997 | len--;
|
---|
| 998 | s->rlayer.handshake_fragment_len--;
|
---|
| 999 | n++;
|
---|
| 1000 | }
|
---|
| 1001 | /* move any remaining fragment bytes: */
|
---|
| 1002 | for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
|
---|
| 1003 | s->rlayer.handshake_fragment[k] = *src++;
|
---|
| 1004 |
|
---|
| 1005 | if (recvd_type != NULL)
|
---|
| 1006 | *recvd_type = SSL3_RT_HANDSHAKE;
|
---|
| 1007 |
|
---|
| 1008 | return n;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | /*
|
---|
| 1012 | * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
|
---|
| 1013 | */
|
---|
| 1014 |
|
---|
| 1015 | if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
|
---|
| 1016 | /* type == SSL3_RT_APPLICATION_DATA */
|
---|
| 1017 | i = s->handshake_func(s);
|
---|
| 1018 | if (i < 0)
|
---|
| 1019 | return (i);
|
---|
| 1020 | if (i == 0) {
|
---|
| 1021 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
---|
| 1022 | return (-1);
|
---|
| 1023 | }
|
---|
| 1024 | }
|
---|
| 1025 | start:
|
---|
| 1026 | s->rwstate = SSL_NOTHING;
|
---|
| 1027 |
|
---|
| 1028 | /*-
|
---|
| 1029 | * For each record 'i' up to |num_recs]
|
---|
| 1030 | * rr[i].type - is the type of record
|
---|
| 1031 | * rr[i].data, - data
|
---|
| 1032 | * rr[i].off, - offset into 'data' for next read
|
---|
| 1033 | * rr[i].length, - number of bytes.
|
---|
| 1034 | */
|
---|
| 1035 | rr = s->rlayer.rrec;
|
---|
| 1036 | num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
|
---|
| 1037 |
|
---|
| 1038 | do {
|
---|
| 1039 | /* get new records if necessary */
|
---|
| 1040 | if (num_recs == 0) {
|
---|
| 1041 | ret = ssl3_get_record(s);
|
---|
| 1042 | if (ret <= 0)
|
---|
| 1043 | return (ret);
|
---|
| 1044 | num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
|
---|
| 1045 | if (num_recs == 0) {
|
---|
| 1046 | /* Shouldn't happen */
|
---|
| 1047 | al = SSL_AD_INTERNAL_ERROR;
|
---|
| 1048 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 1049 | goto f_err;
|
---|
| 1050 | }
|
---|
| 1051 | }
|
---|
| 1052 | /* Skip over any records we have already read */
|
---|
| 1053 | for (curr_rec = 0;
|
---|
| 1054 | curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]);
|
---|
| 1055 | curr_rec++) ;
|
---|
| 1056 | if (curr_rec == num_recs) {
|
---|
| 1057 | RECORD_LAYER_set_numrpipes(&s->rlayer, 0);
|
---|
| 1058 | num_recs = 0;
|
---|
| 1059 | curr_rec = 0;
|
---|
| 1060 | }
|
---|
| 1061 | } while (num_recs == 0);
|
---|
| 1062 | rr = &rr[curr_rec];
|
---|
| 1063 |
|
---|
| 1064 | /*
|
---|
| 1065 | * Reset the count of consecutive warning alerts if we've got a non-empty
|
---|
| 1066 | * record that isn't an alert.
|
---|
| 1067 | */
|
---|
| 1068 | if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
|
---|
| 1069 | && SSL3_RECORD_get_length(rr) != 0)
|
---|
| 1070 | s->rlayer.alert_count = 0;
|
---|
| 1071 |
|
---|
| 1072 | /* we now have a packet which can be read and processed */
|
---|
| 1073 |
|
---|
| 1074 | if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
|
---|
| 1075 | * reset by ssl3_get_finished */
|
---|
| 1076 | && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
|
---|
| 1077 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1078 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
|
---|
| 1079 | goto f_err;
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 | /*
|
---|
| 1083 | * If the other end has shut down, throw anything we read away (even in
|
---|
| 1084 | * 'peek' mode)
|
---|
| 1085 | */
|
---|
| 1086 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
---|
| 1087 | SSL3_RECORD_set_length(rr, 0);
|
---|
| 1088 | s->rwstate = SSL_NOTHING;
|
---|
| 1089 | return (0);
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | if (type == SSL3_RECORD_get_type(rr)
|
---|
| 1093 | || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
|
---|
| 1094 | && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
|
---|
| 1095 | /*
|
---|
| 1096 | * SSL3_RT_APPLICATION_DATA or
|
---|
| 1097 | * SSL3_RT_HANDSHAKE or
|
---|
| 1098 | * SSL3_RT_CHANGE_CIPHER_SPEC
|
---|
| 1099 | */
|
---|
| 1100 | /*
|
---|
| 1101 | * make sure that we are not getting application data when we are
|
---|
| 1102 | * doing a handshake for the first time
|
---|
| 1103 | */
|
---|
| 1104 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
|
---|
| 1105 | (s->enc_read_ctx == NULL)) {
|
---|
| 1106 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1107 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
|
---|
| 1108 | goto f_err;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 | if (type == SSL3_RT_HANDSHAKE
|
---|
| 1112 | && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
|
---|
| 1113 | && s->rlayer.handshake_fragment_len > 0) {
|
---|
| 1114 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1115 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
|
---|
| 1116 | goto f_err;
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 | if (recvd_type != NULL)
|
---|
| 1120 | *recvd_type = SSL3_RECORD_get_type(rr);
|
---|
| 1121 |
|
---|
| 1122 | if (len <= 0)
|
---|
| 1123 | return (len);
|
---|
| 1124 |
|
---|
| 1125 | read_bytes = 0;
|
---|
| 1126 | do {
|
---|
| 1127 | if ((unsigned int)len - read_bytes > SSL3_RECORD_get_length(rr))
|
---|
| 1128 | n = SSL3_RECORD_get_length(rr);
|
---|
| 1129 | else
|
---|
| 1130 | n = (unsigned int)len - read_bytes;
|
---|
| 1131 |
|
---|
| 1132 | memcpy(buf, &(rr->data[rr->off]), n);
|
---|
| 1133 | buf += n;
|
---|
| 1134 | if (peek) {
|
---|
| 1135 | /* Mark any zero length record as consumed CVE-2016-6305 */
|
---|
| 1136 | if (SSL3_RECORD_get_length(rr) == 0)
|
---|
| 1137 | SSL3_RECORD_set_read(rr);
|
---|
| 1138 | } else {
|
---|
| 1139 | SSL3_RECORD_sub_length(rr, n);
|
---|
| 1140 | SSL3_RECORD_add_off(rr, n);
|
---|
| 1141 | if (SSL3_RECORD_get_length(rr) == 0) {
|
---|
| 1142 | s->rlayer.rstate = SSL_ST_READ_HEADER;
|
---|
| 1143 | SSL3_RECORD_set_off(rr, 0);
|
---|
| 1144 | SSL3_RECORD_set_read(rr);
|
---|
| 1145 | }
|
---|
| 1146 | }
|
---|
| 1147 | if (SSL3_RECORD_get_length(rr) == 0
|
---|
| 1148 | || (peek && n == SSL3_RECORD_get_length(rr))) {
|
---|
| 1149 | curr_rec++;
|
---|
| 1150 | rr++;
|
---|
| 1151 | }
|
---|
| 1152 | read_bytes += n;
|
---|
| 1153 | } while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs
|
---|
| 1154 | && read_bytes < (unsigned int)len);
|
---|
| 1155 | if (read_bytes == 0) {
|
---|
| 1156 | /* We must have read empty records. Get more data */
|
---|
| 1157 | goto start;
|
---|
| 1158 | }
|
---|
| 1159 | if (!peek && curr_rec == num_recs
|
---|
| 1160 | && (s->mode & SSL_MODE_RELEASE_BUFFERS)
|
---|
| 1161 | && SSL3_BUFFER_get_left(rbuf) == 0)
|
---|
| 1162 | ssl3_release_read_buffer(s);
|
---|
| 1163 | return read_bytes;
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | /*
|
---|
| 1167 | * If we get here, then type != rr->type; if we have a handshake message,
|
---|
| 1168 | * then it was unexpected (Hello Request or Client Hello) or invalid (we
|
---|
| 1169 | * were actually expecting a CCS).
|
---|
| 1170 | */
|
---|
| 1171 |
|
---|
| 1172 | /*
|
---|
| 1173 | * Lets just double check that we've not got an SSLv2 record
|
---|
| 1174 | */
|
---|
| 1175 | if (rr->rec_version == SSL2_VERSION) {
|
---|
| 1176 | /*
|
---|
| 1177 | * Should never happen. ssl3_get_record() should only give us an SSLv2
|
---|
| 1178 | * record back if this is the first packet and we are looking for an
|
---|
| 1179 | * initial ClientHello. Therefore |type| should always be equal to
|
---|
| 1180 | * |rr->type|. If not then something has gone horribly wrong
|
---|
| 1181 | */
|
---|
| 1182 | al = SSL_AD_INTERNAL_ERROR;
|
---|
| 1183 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 1184 | goto f_err;
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | if (s->method->version == TLS_ANY_VERSION
|
---|
| 1188 | && (s->server || rr->type != SSL3_RT_ALERT)) {
|
---|
| 1189 | /*
|
---|
| 1190 | * If we've got this far and still haven't decided on what version
|
---|
| 1191 | * we're using then this must be a client side alert we're dealing with
|
---|
| 1192 | * (we don't allow heartbeats yet). We shouldn't be receiving anything
|
---|
| 1193 | * other than a ClientHello if we are a server.
|
---|
| 1194 | */
|
---|
| 1195 | s->version = rr->rec_version;
|
---|
| 1196 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1197 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_MESSAGE);
|
---|
| 1198 | goto f_err;
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | /*
|
---|
| 1202 | * In case of record types for which we have 'fragment' storage, fill
|
---|
| 1203 | * that so that we can process the data at a fixed place.
|
---|
| 1204 | */
|
---|
| 1205 | {
|
---|
| 1206 | unsigned int dest_maxlen = 0;
|
---|
| 1207 | unsigned char *dest = NULL;
|
---|
| 1208 | unsigned int *dest_len = NULL;
|
---|
| 1209 |
|
---|
| 1210 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
|
---|
| 1211 | dest_maxlen = sizeof s->rlayer.handshake_fragment;
|
---|
| 1212 | dest = s->rlayer.handshake_fragment;
|
---|
| 1213 | dest_len = &s->rlayer.handshake_fragment_len;
|
---|
| 1214 | } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
|
---|
| 1215 | dest_maxlen = sizeof s->rlayer.alert_fragment;
|
---|
| 1216 | dest = s->rlayer.alert_fragment;
|
---|
| 1217 | dest_len = &s->rlayer.alert_fragment_len;
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 | if (dest_maxlen > 0) {
|
---|
| 1221 | n = dest_maxlen - *dest_len; /* available space in 'dest' */
|
---|
| 1222 | if (SSL3_RECORD_get_length(rr) < n)
|
---|
| 1223 | n = SSL3_RECORD_get_length(rr); /* available bytes */
|
---|
| 1224 |
|
---|
| 1225 | /* now move 'n' bytes: */
|
---|
| 1226 | while (n-- > 0) {
|
---|
| 1227 | dest[(*dest_len)++] =
|
---|
| 1228 | SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
|
---|
| 1229 | SSL3_RECORD_add_off(rr, 1);
|
---|
| 1230 | SSL3_RECORD_add_length(rr, -1);
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | if (*dest_len < dest_maxlen) {
|
---|
| 1234 | SSL3_RECORD_set_read(rr);
|
---|
| 1235 | goto start; /* fragment was too small */
|
---|
| 1236 | }
|
---|
| 1237 | }
|
---|
| 1238 | }
|
---|
| 1239 |
|
---|
| 1240 | /*-
|
---|
| 1241 | * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
|
---|
| 1242 | * s->rlayer.alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
|
---|
| 1243 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
|
---|
| 1244 | */
|
---|
| 1245 |
|
---|
| 1246 | /* If we are a client, check for an incoming 'Hello Request': */
|
---|
| 1247 | if ((!s->server) &&
|
---|
| 1248 | (s->rlayer.handshake_fragment_len >= 4) &&
|
---|
| 1249 | (s->rlayer.handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
|
---|
| 1250 | (s->session != NULL) && (s->session->cipher != NULL)) {
|
---|
| 1251 | s->rlayer.handshake_fragment_len = 0;
|
---|
| 1252 |
|
---|
| 1253 | if ((s->rlayer.handshake_fragment[1] != 0) ||
|
---|
| 1254 | (s->rlayer.handshake_fragment[2] != 0) ||
|
---|
| 1255 | (s->rlayer.handshake_fragment[3] != 0)) {
|
---|
| 1256 | al = SSL_AD_DECODE_ERROR;
|
---|
| 1257 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
|
---|
| 1258 | goto f_err;
|
---|
| 1259 | }
|
---|
| 1260 |
|
---|
| 1261 | if (s->msg_callback)
|
---|
| 1262 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
|
---|
| 1263 | s->rlayer.handshake_fragment, 4, s,
|
---|
| 1264 | s->msg_callback_arg);
|
---|
| 1265 |
|
---|
| 1266 | if (SSL_is_init_finished(s) &&
|
---|
| 1267 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
|
---|
| 1268 | !s->s3->renegotiate) {
|
---|
| 1269 | ssl3_renegotiate(s);
|
---|
| 1270 | if (ssl3_renegotiate_check(s)) {
|
---|
| 1271 | i = s->handshake_func(s);
|
---|
| 1272 | if (i < 0)
|
---|
| 1273 | return (i);
|
---|
| 1274 | if (i == 0) {
|
---|
| 1275 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
---|
| 1276 | return (-1);
|
---|
| 1277 | }
|
---|
| 1278 |
|
---|
| 1279 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
---|
| 1280 | if (SSL3_BUFFER_get_left(rbuf) == 0) {
|
---|
| 1281 | /* no read-ahead left? */
|
---|
| 1282 | BIO *bio;
|
---|
| 1283 | /*
|
---|
| 1284 | * In the case where we try to read application data,
|
---|
| 1285 | * but we trigger an SSL handshake, we return -1 with
|
---|
| 1286 | * the retry option set. Otherwise renegotiation may
|
---|
| 1287 | * cause nasty problems in the blocking world
|
---|
| 1288 | */
|
---|
| 1289 | s->rwstate = SSL_READING;
|
---|
| 1290 | bio = SSL_get_rbio(s);
|
---|
| 1291 | BIO_clear_retry_flags(bio);
|
---|
| 1292 | BIO_set_retry_read(bio);
|
---|
| 1293 | return (-1);
|
---|
| 1294 | }
|
---|
| 1295 | }
|
---|
| 1296 | } else {
|
---|
| 1297 | SSL3_RECORD_set_read(rr);
|
---|
| 1298 | }
|
---|
| 1299 | } else {
|
---|
| 1300 | /* Does this ever happen? */
|
---|
| 1301 | SSL3_RECORD_set_read(rr);
|
---|
| 1302 | }
|
---|
| 1303 | /*
|
---|
| 1304 | * we either finished a handshake or ignored the request, now try
|
---|
| 1305 | * again to obtain the (application) data we were asked for
|
---|
| 1306 | */
|
---|
| 1307 | goto start;
|
---|
| 1308 | }
|
---|
| 1309 | /*
|
---|
| 1310 | * If we are a server and get a client hello when renegotiation isn't
|
---|
| 1311 | * allowed send back a no renegotiation alert and carry on. WARNING:
|
---|
| 1312 | * experimental code, needs reviewing (steve)
|
---|
| 1313 | */
|
---|
| 1314 | if (s->server &&
|
---|
| 1315 | SSL_is_init_finished(s) &&
|
---|
| 1316 | !s->s3->send_connection_binding &&
|
---|
| 1317 | (s->version > SSL3_VERSION) &&
|
---|
| 1318 | (s->rlayer.handshake_fragment_len >= 4) &&
|
---|
| 1319 | (s->rlayer.handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&
|
---|
| 1320 | (s->session != NULL) && (s->session->cipher != NULL) &&
|
---|
| 1321 | !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
|
---|
| 1322 | SSL3_RECORD_set_length(rr, 0);
|
---|
| 1323 | SSL3_RECORD_set_read(rr);
|
---|
| 1324 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
---|
| 1325 | goto start;
|
---|
| 1326 | }
|
---|
| 1327 | if (s->rlayer.alert_fragment_len >= 2) {
|
---|
| 1328 | int alert_level = s->rlayer.alert_fragment[0];
|
---|
| 1329 | int alert_descr = s->rlayer.alert_fragment[1];
|
---|
| 1330 |
|
---|
| 1331 | s->rlayer.alert_fragment_len = 0;
|
---|
| 1332 |
|
---|
| 1333 | if (s->msg_callback)
|
---|
| 1334 | s->msg_callback(0, s->version, SSL3_RT_ALERT,
|
---|
| 1335 | s->rlayer.alert_fragment, 2, s,
|
---|
| 1336 | s->msg_callback_arg);
|
---|
| 1337 |
|
---|
| 1338 | if (s->info_callback != NULL)
|
---|
| 1339 | cb = s->info_callback;
|
---|
| 1340 | else if (s->ctx->info_callback != NULL)
|
---|
| 1341 | cb = s->ctx->info_callback;
|
---|
| 1342 |
|
---|
| 1343 | if (cb != NULL) {
|
---|
| 1344 | j = (alert_level << 8) | alert_descr;
|
---|
| 1345 | cb(s, SSL_CB_READ_ALERT, j);
|
---|
| 1346 | }
|
---|
| 1347 |
|
---|
| 1348 | if (alert_level == SSL3_AL_WARNING) {
|
---|
| 1349 | s->s3->warn_alert = alert_descr;
|
---|
| 1350 | SSL3_RECORD_set_read(rr);
|
---|
| 1351 |
|
---|
| 1352 | s->rlayer.alert_count++;
|
---|
| 1353 | if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
|
---|
| 1354 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1355 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);
|
---|
| 1356 | goto f_err;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
---|
| 1360 | s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
---|
| 1361 | return (0);
|
---|
| 1362 | }
|
---|
| 1363 | /*
|
---|
| 1364 | * This is a warning but we receive it if we requested
|
---|
| 1365 | * renegotiation and the peer denied it. Terminate with a fatal
|
---|
| 1366 | * alert because if application tried to renegotiate it
|
---|
| 1367 | * presumably had a good reason and expects it to succeed. In
|
---|
| 1368 | * future we might have a renegotiation where we don't care if
|
---|
| 1369 | * the peer refused it where we carry on.
|
---|
| 1370 | */
|
---|
| 1371 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
|
---|
| 1372 | al = SSL_AD_HANDSHAKE_FAILURE;
|
---|
| 1373 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
|
---|
| 1374 | goto f_err;
|
---|
| 1375 | }
|
---|
| 1376 | #ifdef SSL_AD_MISSING_SRP_USERNAME
|
---|
| 1377 | else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
|
---|
| 1378 | return (0);
|
---|
| 1379 | #endif
|
---|
| 1380 | } else if (alert_level == SSL3_AL_FATAL) {
|
---|
| 1381 | char tmp[16];
|
---|
| 1382 |
|
---|
| 1383 | s->rwstate = SSL_NOTHING;
|
---|
| 1384 | s->s3->fatal_alert = alert_descr;
|
---|
| 1385 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
|
---|
| 1386 | BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
|
---|
| 1387 | ERR_add_error_data(2, "SSL alert number ", tmp);
|
---|
| 1388 | s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
---|
| 1389 | SSL3_RECORD_set_read(rr);
|
---|
| 1390 | SSL_CTX_remove_session(s->session_ctx, s->session);
|
---|
| 1391 | return (0);
|
---|
| 1392 | } else {
|
---|
| 1393 | al = SSL_AD_ILLEGAL_PARAMETER;
|
---|
| 1394 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
|
---|
| 1395 | goto f_err;
|
---|
| 1396 | }
|
---|
| 1397 |
|
---|
| 1398 | goto start;
|
---|
| 1399 | }
|
---|
| 1400 |
|
---|
| 1401 | if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
|
---|
| 1402 | * shutdown */
|
---|
| 1403 | s->rwstate = SSL_NOTHING;
|
---|
| 1404 | SSL3_RECORD_set_length(rr, 0);
|
---|
| 1405 | SSL3_RECORD_set_read(rr);
|
---|
| 1406 | return (0);
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
|
---|
| 1410 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1411 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
|
---|
| 1412 | goto f_err;
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 | /*
|
---|
| 1416 | * Unexpected handshake message (Client Hello, or protocol violation)
|
---|
| 1417 | */
|
---|
| 1418 | if ((s->rlayer.handshake_fragment_len >= 4)
|
---|
| 1419 | && !ossl_statem_get_in_handshake(s)) {
|
---|
| 1420 | if (SSL_is_init_finished(s) &&
|
---|
| 1421 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
|
---|
| 1422 | ossl_statem_set_in_init(s, 1);
|
---|
| 1423 | s->renegotiate = 1;
|
---|
| 1424 | s->new_session = 1;
|
---|
| 1425 | }
|
---|
| 1426 | i = s->handshake_func(s);
|
---|
| 1427 | if (i < 0)
|
---|
| 1428 | return (i);
|
---|
| 1429 | if (i == 0) {
|
---|
| 1430 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
---|
| 1431 | return (-1);
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
---|
| 1435 | if (SSL3_BUFFER_get_left(rbuf) == 0) {
|
---|
| 1436 | /* no read-ahead left? */
|
---|
| 1437 | BIO *bio;
|
---|
| 1438 | /*
|
---|
| 1439 | * In the case where we try to read application data, but we
|
---|
| 1440 | * trigger an SSL handshake, we return -1 with the retry
|
---|
| 1441 | * option set. Otherwise renegotiation may cause nasty
|
---|
| 1442 | * problems in the blocking world
|
---|
| 1443 | */
|
---|
| 1444 | s->rwstate = SSL_READING;
|
---|
| 1445 | bio = SSL_get_rbio(s);
|
---|
| 1446 | BIO_clear_retry_flags(bio);
|
---|
| 1447 | BIO_set_retry_read(bio);
|
---|
| 1448 | return (-1);
|
---|
| 1449 | }
|
---|
| 1450 | }
|
---|
| 1451 | goto start;
|
---|
| 1452 | }
|
---|
| 1453 |
|
---|
| 1454 | switch (SSL3_RECORD_get_type(rr)) {
|
---|
| 1455 | default:
|
---|
| 1456 | /*
|
---|
| 1457 | * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
|
---|
| 1458 | * TLS 1.2 says you MUST send an unexpected message alert. We use the
|
---|
| 1459 | * TLS 1.2 behaviour for all protocol versions to prevent issues where
|
---|
| 1460 | * no progress is being made and the peer continually sends unrecognised
|
---|
| 1461 | * record types, using up resources processing them.
|
---|
| 1462 | */
|
---|
| 1463 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1464 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
---|
| 1465 | goto f_err;
|
---|
| 1466 | case SSL3_RT_CHANGE_CIPHER_SPEC:
|
---|
| 1467 | case SSL3_RT_ALERT:
|
---|
| 1468 | case SSL3_RT_HANDSHAKE:
|
---|
| 1469 | /*
|
---|
| 1470 | * we already handled all of these, with the possible exception of
|
---|
| 1471 | * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
|
---|
| 1472 | * that should not happen when type != rr->type
|
---|
| 1473 | */
|
---|
| 1474 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1475 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
---|
| 1476 | goto f_err;
|
---|
| 1477 | case SSL3_RT_APPLICATION_DATA:
|
---|
| 1478 | /*
|
---|
| 1479 | * At this point, we were expecting handshake data, but have
|
---|
| 1480 | * application data. If the library was running inside ssl3_read()
|
---|
| 1481 | * (i.e. in_read_app_data is set) and it makes sense to read
|
---|
| 1482 | * application data at this point (session renegotiation not yet
|
---|
| 1483 | * started), we will indulge it.
|
---|
| 1484 | */
|
---|
| 1485 | if (ossl_statem_app_data_allowed(s)) {
|
---|
| 1486 | s->s3->in_read_app_data = 2;
|
---|
| 1487 | return (-1);
|
---|
| 1488 | } else {
|
---|
| 1489 | al = SSL_AD_UNEXPECTED_MESSAGE;
|
---|
| 1490 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
---|
| 1491 | goto f_err;
|
---|
| 1492 | }
|
---|
| 1493 | }
|
---|
| 1494 | /* not reached */
|
---|
| 1495 |
|
---|
| 1496 | f_err:
|
---|
| 1497 | ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
---|
| 1498 | return (-1);
|
---|
| 1499 | }
|
---|
| 1500 |
|
---|
| 1501 | void ssl3_record_sequence_update(unsigned char *seq)
|
---|
| 1502 | {
|
---|
| 1503 | int i;
|
---|
| 1504 |
|
---|
| 1505 | for (i = 7; i >= 0; i--) {
|
---|
| 1506 | ++seq[i];
|
---|
| 1507 | if (seq[i] != 0)
|
---|
| 1508 | break;
|
---|
| 1509 | }
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
| 1512 | /*
|
---|
| 1513 | * Returns true if the current rrec was sent in SSLv2 backwards compatible
|
---|
| 1514 | * format and false otherwise.
|
---|
| 1515 | */
|
---|
| 1516 | int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
|
---|
| 1517 | {
|
---|
| 1518 | return SSL3_RECORD_is_sslv2_record(&rl->rrec[0]);
|
---|
| 1519 | }
|
---|
| 1520 |
|
---|
| 1521 | /*
|
---|
| 1522 | * Returns the length in bytes of the current rrec
|
---|
| 1523 | */
|
---|
| 1524 | unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
|
---|
| 1525 | {
|
---|
| 1526 | return SSL3_RECORD_get_length(&rl->rrec[0]);
|
---|
| 1527 | }
|
---|