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 | #include <assert.h> |
| 110 | #include <errno.h> |
| 111 | #include <stdio.h> |
| 112 | |
| 113 | #include <openssl/buf.h> |
| 114 | #include <openssl/err.h> |
| 115 | #include <openssl/evp.h> |
| 116 | #include <openssl/mem.h> |
| 117 | #include <openssl/rand.h> |
| 118 | |
| 119 | #include "ssl_locl.h" |
| 120 | |
| 121 | static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | unsigned int len, char fragment, char is_fragment); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | static int ssl3_get_record(SSL *s); |
| 124 | |
| 125 | int ssl3_read_n(SSL *s, int n, int max, int extend) |
| 126 | { |
| 127 | /* If extend == 0, obtain new n-byte packet; if extend == 1, increase |
| 128 | * packet by another n bytes. |
| 129 | * The packet will be in the sub-array of s->s3->rbuf.buf specified |
| 130 | * by s->packet and s->packet_length. |
| 131 | * (If s->read_ahead is set, 'max' bytes may be stored in rbuf |
| 132 | * [plus s->packet_length bytes if extend == 1].) |
| 133 | */ |
| 134 | int i,len,left; |
| 135 | long align=0; |
| 136 | unsigned char *pkt; |
| 137 | SSL3_BUFFER *rb; |
| 138 | |
| 139 | if (n <= 0) return n; |
| 140 | |
| 141 | rb = &(s->s3->rbuf); |
| 142 | if (rb->buf == NULL) |
| 143 | if (!ssl3_setup_read_buffer(s)) |
| 144 | return -1; |
| 145 | |
| 146 | left = rb->left; |
| 147 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 |
| 148 | align = (long)rb->buf + SSL3_RT_HEADER_LENGTH; |
| 149 | align = (-align)&(SSL3_ALIGN_PAYLOAD-1); |
| 150 | #endif |
| 151 | |
| 152 | if (!extend) |
| 153 | { |
| 154 | /* start with empty packet ... */ |
| 155 | if (left == 0) |
| 156 | rb->offset = align; |
| 157 | else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) |
| 158 | { |
| 159 | /* check if next packet length is large |
| 160 | * enough to justify payload alignment... */ |
| 161 | pkt = rb->buf + rb->offset; |
| 162 | if (pkt[0] == SSL3_RT_APPLICATION_DATA |
| 163 | && (pkt[3]<<8|pkt[4]) >= 128) |
| 164 | { |
| 165 | /* Note that even if packet is corrupted |
| 166 | * and its length field is insane, we can |
| 167 | * only be led to wrong decision about |
| 168 | * whether memmove will occur or not. |
| 169 | * Header values has no effect on memmove |
| 170 | * arguments and therefore no buffer |
| 171 | * overrun can be triggered. */ |
| 172 | memmove (rb->buf+align,pkt,left); |
| 173 | rb->offset = align; |
| 174 | } |
| 175 | } |
| 176 | s->packet = rb->buf + rb->offset; |
| 177 | s->packet_length = 0; |
| 178 | /* ... now we can act as if 'extend' was set */ |
| 179 | } |
| 180 | |
| 181 | /* For DTLS/UDP reads should not span multiple packets |
| 182 | * because the read operation returns the whole packet |
| 183 | * at once (as long as it fits into the buffer). */ |
| 184 | if (SSL_IS_DTLS(s)) |
| 185 | { |
| 186 | if (left > 0 && n > left) |
| 187 | n = left; |
| 188 | } |
| 189 | |
| 190 | /* if there is enough in the buffer from a previous read, take some */ |
| 191 | if (left >= n) |
| 192 | { |
| 193 | s->packet_length+=n; |
| 194 | rb->left=left-n; |
| 195 | rb->offset+=n; |
| 196 | return(n); |
| 197 | } |
| 198 | |
| 199 | /* else we need to read more data */ |
| 200 | |
| 201 | len = s->packet_length; |
| 202 | pkt = rb->buf+align; |
| 203 | /* Move any available bytes to front of buffer: |
| 204 | * 'len' bytes already pointed to by 'packet', |
| 205 | * 'left' extra ones at the end */ |
| 206 | if (s->packet != pkt) /* len > 0 */ |
| 207 | { |
| 208 | memmove(pkt, s->packet, len+left); |
| 209 | s->packet = pkt; |
| 210 | rb->offset = len + align; |
| 211 | } |
| 212 | |
| 213 | if (n > (int)(rb->len - rb->offset)) /* does not happen */ |
| 214 | { |
| 215 | OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | if (!s->read_ahead) |
| 220 | /* ignore max parameter */ |
| 221 | max = n; |
| 222 | else |
| 223 | { |
| 224 | if (max < n) |
| 225 | max = n; |
| 226 | if (max > (int)(rb->len - rb->offset)) |
| 227 | max = rb->len - rb->offset; |
| 228 | } |
| 229 | |
| 230 | while (left < n) |
| 231 | { |
| 232 | /* Now we have len+left bytes at the front of s->s3->rbuf.buf |
| 233 | * and need to read in more until we have len+n (up to |
| 234 | * len+max if possible) */ |
| 235 | |
| 236 | ERR_clear_system_error(); |
| 237 | if (s->rbio != NULL) |
| 238 | { |
| 239 | s->rwstate=SSL_READING; |
| 240 | i=BIO_read(s->rbio,pkt+len+left, max-left); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET); |
| 245 | i = -1; |
| 246 | } |
| 247 | |
| 248 | if (i <= 0) |
| 249 | { |
| 250 | rb->left = left; |
| 251 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && |
| 252 | !SSL_IS_DTLS(s)) |
| 253 | if (len+left == 0) |
| 254 | ssl3_release_read_buffer(s); |
| 255 | return(i); |
| 256 | } |
| 257 | left+=i; |
| 258 | /* reads should *never* span multiple packets for DTLS because |
| 259 | * the underlying transport protocol is message oriented as opposed |
| 260 | * to byte oriented as in the TLS case. */ |
| 261 | if (SSL_IS_DTLS(s)) |
| 262 | { |
| 263 | if (n > left) |
| 264 | n = left; /* makes the while condition false */ |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* done reading, now the book-keeping */ |
| 269 | rb->offset += n; |
| 270 | rb->left = left - n; |
| 271 | s->packet_length += n; |
| 272 | s->rwstate=SSL_NOTHING; |
| 273 | return(n); |
| 274 | } |
| 275 | |
Adam Langley | 48105fa | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | /* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will |
| 277 | * be processed per call to ssl3_get_record. Without this limit an attacker |
| 278 | * could send empty records at a faster rate than we can process and cause |
| 279 | * ssl3_get_record to loop forever. */ |
| 280 | #define MAX_EMPTY_RECORDS 32 |
| 281 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | /* Call this to get a new input record. |
| 283 | * It will return <= 0 if more data is needed, normally due to an error |
| 284 | * or non-blocking IO. |
| 285 | * When it finishes, one packet has been decoded and can be found in |
| 286 | * ssl->s3->rrec.type - is the type of record |
| 287 | * ssl->s3->rrec.data, - data |
| 288 | * ssl->s3->rrec.length, - number of bytes |
| 289 | */ |
| 290 | /* used only by ssl3_read_bytes */ |
| 291 | static int ssl3_get_record(SSL *s) |
| 292 | { |
| 293 | int ssl_major,ssl_minor,al; |
| 294 | int enc_err,n,i,ret= -1; |
| 295 | SSL3_RECORD *rr; |
| 296 | SSL_SESSION *sess; |
| 297 | unsigned char *p; |
| 298 | unsigned char md[EVP_MAX_MD_SIZE]; |
| 299 | short version; |
| 300 | unsigned mac_size, orig_len; |
| 301 | size_t extra; |
Adam Langley | 48105fa | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | unsigned empty_record_count = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
| 304 | rr= &(s->s3->rrec); |
| 305 | sess=s->session; |
| 306 | |
| 307 | if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) |
| 308 | extra=SSL3_RT_MAX_EXTRA; |
| 309 | else |
| 310 | extra=0; |
| 311 | if (extra && !s->s3->init_extra) |
| 312 | { |
| 313 | /* An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER |
| 314 | * set after ssl3_setup_buffers() was done */ |
| 315 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, ERR_R_INTERNAL_ERROR); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | again: |
| 320 | /* check if we have the header */ |
| 321 | if ( (s->rstate != SSL_ST_READ_BODY) || |
| 322 | (s->packet_length < SSL3_RT_HEADER_LENGTH)) |
| 323 | { |
| 324 | n=ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); |
| 325 | if (n <= 0) return(n); /* error or non-blocking */ |
| 326 | s->rstate=SSL_ST_READ_BODY; |
| 327 | |
| 328 | p=s->packet; |
| 329 | if (s->msg_callback) |
| 330 | s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg); |
| 331 | |
| 332 | /* Pull apart the header into the SSL3_RECORD */ |
| 333 | rr->type= *(p++); |
| 334 | ssl_major= *(p++); |
| 335 | ssl_minor= *(p++); |
| 336 | version=(ssl_major<<8)|ssl_minor; |
| 337 | n2s(p,rr->length); |
| 338 | #if 0 |
| 339 | fprintf(stderr, "Record type=%d, Length=%d\n", rr->type, rr->length); |
| 340 | #endif |
| 341 | |
| 342 | /* Lets check version */ |
| 343 | if (!s->first_packet) |
| 344 | { |
| 345 | if (version != s->version) |
| 346 | { |
| 347 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
| 348 | if ((s->version & 0xFF00) == (version & 0xFF00) && !s->enc_write_ctx && !s->write_hash) |
| 349 | /* Send back error using their minor version number :-) */ |
| 350 | s->version = (unsigned short)version; |
| 351 | al=SSL_AD_PROTOCOL_VERSION; |
| 352 | goto f_err; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if ((version>>8) != SSL3_VERSION_MAJOR) |
| 357 | { |
| 358 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
| 359 | goto err; |
| 360 | } |
| 361 | |
| 362 | if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) |
| 363 | { |
| 364 | al=SSL_AD_RECORD_OVERFLOW; |
| 365 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_PACKET_LENGTH_TOO_LONG); |
| 366 | goto f_err; |
| 367 | } |
| 368 | |
| 369 | /* now s->rstate == SSL_ST_READ_BODY */ |
| 370 | } |
| 371 | |
| 372 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ |
| 373 | |
| 374 | if (rr->length > s->packet_length-SSL3_RT_HEADER_LENGTH) |
| 375 | { |
| 376 | /* now s->packet_length == SSL3_RT_HEADER_LENGTH */ |
| 377 | i=rr->length; |
| 378 | n=ssl3_read_n(s,i,i,1); |
| 379 | if (n <= 0) return(n); /* error or non-blocking io */ |
| 380 | /* now n == rr->length, |
| 381 | * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */ |
| 382 | } |
| 383 | |
| 384 | s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */ |
| 385 | |
| 386 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, |
| 387 | * and we have that many bytes in s->packet |
| 388 | */ |
| 389 | rr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]); |
| 390 | |
| 391 | /* ok, we can now read from 's->packet' data into 'rr' |
| 392 | * rr->input points at rr->length bytes, which |
| 393 | * need to be copied into rr->data by either |
| 394 | * the decryption or by the decompression |
| 395 | * When the data is 'copied' into the rr->data buffer, |
| 396 | * rr->input will be pointed at the new buffer */ |
| 397 | |
| 398 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] |
| 399 | * rr->length bytes of encrypted compressed stuff. */ |
| 400 | |
| 401 | /* check is not needed I believe */ |
| 402 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra) |
| 403 | { |
| 404 | al=SSL_AD_RECORD_OVERFLOW; |
| 405 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); |
| 406 | goto f_err; |
| 407 | } |
| 408 | |
| 409 | /* decrypt in place in 'rr->input' */ |
| 410 | rr->data=rr->input; |
| 411 | |
| 412 | enc_err = s->method->ssl3_enc->enc(s,0); |
| 413 | /* enc_err is: |
| 414 | * 0: (in non-constant time) if the record is publically invalid. |
| 415 | * 1: if the padding is valid |
| 416 | * -1: if the padding is invalid */ |
| 417 | if (enc_err == 0) |
| 418 | { |
| 419 | al=SSL_AD_DECRYPTION_FAILED; |
| 420 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG); |
| 421 | goto f_err; |
| 422 | } |
| 423 | |
| 424 | #ifdef TLS_DEBUG |
| 425 | printf("dec %d\n",rr->length); |
| 426 | { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); } |
| 427 | printf("\n"); |
| 428 | #endif |
| 429 | |
| 430 | /* r->length is now the compressed data plus mac */ |
| 431 | if ((sess != NULL) && |
| 432 | (s->enc_read_ctx != NULL) && |
| 433 | (EVP_MD_CTX_md(s->read_hash) != NULL)) |
| 434 | { |
| 435 | /* s->read_hash != NULL => mac_size != -1 */ |
| 436 | unsigned char *mac = NULL; |
| 437 | unsigned char mac_tmp[EVP_MAX_MD_SIZE]; |
| 438 | mac_size=EVP_MD_CTX_size(s->read_hash); |
| 439 | assert(mac_size <= EVP_MAX_MD_SIZE); |
| 440 | |
| 441 | /* kludge: *_cbc_remove_padding passes padding length in rr->type */ |
| 442 | orig_len = rr->length+((unsigned int)rr->type>>8); |
| 443 | |
| 444 | /* orig_len is the length of the record before any padding was |
| 445 | * removed. This is public information, as is the MAC in use, |
| 446 | * therefore we can safely process the record in a different |
| 447 | * amount of time if it's too short to possibly contain a MAC. |
| 448 | */ |
| 449 | if (orig_len < mac_size || |
| 450 | /* CBC records must have a padding length byte too. */ |
| 451 | (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE && |
| 452 | orig_len < mac_size+1)) |
| 453 | { |
| 454 | al=SSL_AD_DECODE_ERROR; |
| 455 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_LENGTH_TOO_SHORT); |
| 456 | goto f_err; |
| 457 | } |
| 458 | |
| 459 | if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) |
| 460 | { |
| 461 | /* We update the length so that the TLS header bytes |
| 462 | * can be constructed correctly but we need to extract |
| 463 | * the MAC in constant time from within the record, |
| 464 | * without leaking the contents of the padding bytes. |
| 465 | * */ |
| 466 | mac = mac_tmp; |
| 467 | ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len); |
| 468 | rr->length -= mac_size; |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | /* In this case there's no padding, so |orig_len| |
| 473 | * equals |rec->length| and we checked that there's |
| 474 | * enough bytes for |mac_size| above. */ |
| 475 | rr->length -= mac_size; |
| 476 | mac = &rr->data[rr->length]; |
| 477 | } |
| 478 | |
| 479 | i=s->method->ssl3_enc->mac(s,md,0 /* not send */); |
| 480 | if (i < 0 || mac == NULL || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) |
| 481 | enc_err = -1; |
| 482 | if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size) |
| 483 | enc_err = -1; |
| 484 | } |
| 485 | |
| 486 | if (enc_err < 0) |
| 487 | { |
| 488 | /* A separate 'decryption_failed' alert was introduced with TLS 1.0, |
| 489 | * SSL 3.0 only has 'bad_record_mac'. But unless a decryption |
| 490 | * failure is directly visible from the ciphertext anyway, |
| 491 | * we should not reveal which kind of error occured -- this |
| 492 | * might become visible to an attacker (e.g. via a logfile) */ |
| 493 | al=SSL_AD_BAD_RECORD_MAC; |
| 494 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 495 | goto f_err; |
| 496 | } |
| 497 | |
| 498 | /* r->length is now just compressed */ |
| 499 | if (s->expand != NULL) |
| 500 | { |
| 501 | if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra) |
| 502 | { |
| 503 | al=SSL_AD_RECORD_OVERFLOW; |
| 504 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_COMPRESSED_LENGTH_TOO_LONG); |
| 505 | goto f_err; |
| 506 | } |
| 507 | if (!ssl3_do_uncompress(s)) |
| 508 | { |
| 509 | al=SSL_AD_DECOMPRESSION_FAILURE; |
| 510 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_BAD_DECOMPRESSION); |
| 511 | goto f_err; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH+extra) |
| 516 | { |
| 517 | al=SSL_AD_RECORD_OVERFLOW; |
| 518 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG); |
| 519 | goto f_err; |
| 520 | } |
| 521 | |
| 522 | rr->off=0; |
| 523 | /* So at this point the following is true |
| 524 | * ssl->s3->rrec.type is the type of record |
| 525 | * ssl->s3->rrec.length == number of bytes in record |
| 526 | * ssl->s3->rrec.off == offset to first valid byte |
| 527 | * ssl->s3->rrec.data == where to take bytes from, increment |
| 528 | * after use :-). |
| 529 | */ |
| 530 | |
| 531 | /* we have pulled in a full packet so zero things */ |
| 532 | s->packet_length=0; |
| 533 | |
| 534 | /* just read a 0 length packet */ |
Adam Langley | 48105fa | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | if (rr->length == 0) |
| 536 | { |
| 537 | empty_record_count++; |
| 538 | if (empty_record_count > MAX_EMPTY_RECORDS) |
| 539 | { |
| 540 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 541 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 542 | goto f_err; |
| 543 | } |
| 544 | goto again; |
| 545 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 546 | |
| 547 | #if 0 |
| 548 | fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type, rr->length); |
| 549 | #endif |
| 550 | |
| 551 | return(1); |
| 552 | |
| 553 | f_err: |
| 554 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 555 | err: |
| 556 | return(ret); |
| 557 | } |
| 558 | |
| 559 | int ssl3_do_uncompress(SSL *ssl) |
| 560 | { |
| 561 | return(1); |
| 562 | } |
| 563 | |
| 564 | int ssl3_do_compress(SSL *ssl) |
| 565 | { |
| 566 | return(1); |
| 567 | } |
| 568 | |
| 569 | /* Call this to write data in records of type 'type' |
| 570 | * It will return <= 0 if not all data has been sent or non-blocking IO. |
| 571 | */ |
| 572 | int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) |
| 573 | { |
| 574 | const unsigned char *buf=buf_; |
| 575 | unsigned int tot,n,nw; |
| 576 | int i; |
| 577 | |
| 578 | s->rwstate=SSL_NOTHING; |
| 579 | tot=s->s3->wnum; |
| 580 | s->s3->wnum=0; |
| 581 | |
| 582 | if (SSL_in_init(s) && !s->in_handshake) |
| 583 | { |
| 584 | i=s->handshake_func(s); |
| 585 | if (i < 0) return(i); |
| 586 | if (i == 0) |
| 587 | { |
| 588 | OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 589 | return -1; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | n=(len-tot); |
| 594 | for (;;) |
| 595 | { |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 596 | /* max contains the maximum number of bytes that we can put |
| 597 | * into a record. */ |
| 598 | unsigned max = s->max_send_fragment; |
| 599 | /* fragment is true if do_ssl3_write should send the first byte |
| 600 | * in its own record in order to randomise a CBC IV. */ |
| 601 | int fragment = 0; |
| 602 | |
| 603 | if (n > 1 && |
| 604 | s->s3->need_record_splitting && |
| 605 | type == SSL3_RT_APPLICATION_DATA && |
| 606 | !s->s3->record_split_done) |
| 607 | { |
| 608 | fragment = 1; |
| 609 | /* The first byte will be in its own record, so we |
| 610 | * can write an extra byte. */ |
| 611 | max++; |
| 612 | /* record_split_done records that the splitting has |
| 613 | * been done in case we hit an SSL_WANT_WRITE condition. |
| 614 | * In that case, we don't need to do the split again. */ |
| 615 | s->s3->record_split_done = 1; |
| 616 | } |
| 617 | |
| 618 | if (n > max) |
| 619 | nw=max; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | else |
| 621 | nw=n; |
| 622 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | i=do_ssl3_write(s, type, &(buf[tot]), nw, fragment, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | if (i <= 0) |
| 625 | { |
| 626 | s->s3->wnum=tot; |
| 627 | return i; |
| 628 | } |
| 629 | |
| 630 | if ((i == (int)n) || |
| 631 | (type == SSL3_RT_APPLICATION_DATA && |
| 632 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) |
| 633 | { |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 634 | /* next chunk of data should get another prepended, |
| 635 | * one-byte fragment in ciphersuites with known-IV |
| 636 | * weakness. */ |
| 637 | s->s3->record_split_done = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | return tot+i; |
| 639 | } |
| 640 | |
| 641 | n-=i; |
| 642 | tot+=i; |
| 643 | } |
| 644 | } |
| 645 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 646 | /* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1 |
| 647 | * then it splits the record into a one byte record and a record with the rest |
| 648 | * of the data in order to randomise a CBC IV. If |is_fragment| is true then |
| 649 | * this call resulted from do_ssl3_write calling itself in order to create that |
| 650 | * one byte fragment. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 652 | unsigned int len, char fragment, char is_fragment) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | { |
| 654 | unsigned char *p,*plen; |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 655 | int i,mac_size; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 656 | int prefix_len=0; |
| 657 | int eivlen; |
| 658 | long align=0; |
| 659 | SSL3_RECORD *wr; |
| 660 | SSL3_BUFFER *wb=&(s->s3->wbuf); |
| 661 | SSL_SESSION *sess; |
| 662 | |
| 663 | if (wb->buf == NULL) |
| 664 | if (!ssl3_setup_write_buffer(s)) |
| 665 | return -1; |
| 666 | |
| 667 | /* first check if there is a SSL3_BUFFER still being written |
| 668 | * out. This will happen with non blocking IO */ |
| 669 | if (wb->left != 0) |
| 670 | return(ssl3_write_pending(s,type,buf,len)); |
| 671 | |
| 672 | /* If we have an alert to send, lets send it */ |
| 673 | if (s->s3->alert_dispatch) |
| 674 | { |
| 675 | i=s->method->ssl_dispatch_alert(s); |
| 676 | if (i <= 0) |
| 677 | return(i); |
| 678 | /* if it went, fall through and send more stuff */ |
| 679 | } |
| 680 | |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 681 | /* Dispatching the alert may have released the write buffer. */ |
| 682 | if (wb->buf == NULL) |
| 683 | if (!ssl3_setup_write_buffer(s)) |
| 684 | return -1; |
| 685 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | if (len == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 687 | return 0; |
| 688 | |
| 689 | wr= &(s->s3->wrec); |
| 690 | sess=s->session; |
| 691 | |
| 692 | if ( (sess == NULL) || |
| 693 | (s->enc_write_ctx == NULL) || |
| 694 | (EVP_MD_CTX_md(s->write_hash) == NULL)) |
| 695 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 696 | mac_size=0; |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | mac_size=EVP_MD_CTX_size(s->write_hash); |
| 701 | if (mac_size < 0) |
| 702 | goto err; |
| 703 | } |
| 704 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 705 | if (fragment) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 706 | { |
| 707 | /* countermeasure against known-IV weakness in CBC ciphersuites |
| 708 | * (see http://www.openssl.org/~bodo/tls-cbc.txt) */ |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 709 | prefix_len = do_ssl3_write(s, type, buf, 1 /* length */, |
| 710 | 0 /* fragment */, |
| 711 | 1 /* is_fragment */); |
| 712 | if (prefix_len <= 0) |
| 713 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 714 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 715 | if (prefix_len > (SSL3_RT_HEADER_LENGTH + |
| 716 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | { |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 718 | /* insufficient space */ |
| 719 | OPENSSL_PUT_ERROR(SSL, do_ssl3_write, ERR_R_INTERNAL_ERROR); |
| 720 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 724 | if (is_fragment) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 725 | { |
| 726 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 727 | /* The extra fragment would be couple of cipher blocks, and |
| 728 | * that will be a multiple of SSL3_ALIGN_PAYLOAD. So, if we |
| 729 | * want to align the real payload, we can just pretend that we |
| 730 | * have two headers and a byte. */ |
| 731 | align = (long)wb->buf + 2*SSL3_RT_HEADER_LENGTH + 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | align = (-align)&(SSL3_ALIGN_PAYLOAD-1); |
| 733 | #endif |
| 734 | p = wb->buf + align; |
| 735 | wb->offset = align; |
| 736 | } |
| 737 | else if (prefix_len) |
| 738 | { |
| 739 | p = wb->buf + wb->offset + prefix_len; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 |
| 744 | align = (long)wb->buf + SSL3_RT_HEADER_LENGTH; |
| 745 | align = (-align)&(SSL3_ALIGN_PAYLOAD-1); |
| 746 | #endif |
| 747 | p = wb->buf + align; |
| 748 | wb->offset = align; |
| 749 | } |
| 750 | |
| 751 | /* write the header */ |
| 752 | |
| 753 | *(p++)=type&0xff; |
| 754 | wr->type=type; |
| 755 | |
| 756 | *(p++)=(s->version>>8); |
| 757 | /* Some servers hang if iniatial client hello is larger than 256 |
| 758 | * bytes and record version number > TLS 1.0 |
| 759 | */ |
| 760 | if (s->state == SSL3_ST_CW_CLNT_HELLO_B |
| 761 | && !s->renegotiate |
| 762 | && TLS1_get_version(s) > TLS1_VERSION) |
| 763 | *(p++) = 0x1; |
| 764 | else |
| 765 | *(p++)=s->version&0xff; |
| 766 | |
| 767 | /* field where we are to write out packet length */ |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 768 | plen=p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 769 | p+=2; |
| 770 | /* Explicit IV length, block ciphers appropriate version flag */ |
| 771 | if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) |
| 772 | { |
| 773 | int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx); |
| 774 | if (mode == EVP_CIPH_CBC_MODE) |
| 775 | { |
| 776 | eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx); |
| 777 | if (eivlen <= 1) |
| 778 | eivlen = 0; |
| 779 | } |
| 780 | /* Need explicit part of IV for GCM mode */ |
| 781 | else if (mode == EVP_CIPH_GCM_MODE) |
| 782 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
| 783 | else |
| 784 | eivlen = 0; |
| 785 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 786 | else if (s->aead_write_ctx != NULL && |
| 787 | s->aead_write_ctx->variable_nonce_included_in_record) |
| 788 | { |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | eivlen = s->aead_write_ctx->variable_nonce_len; |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 790 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 791 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 792 | eivlen = 0; |
| 793 | |
| 794 | /* lets setup the record stuff. */ |
| 795 | wr->data=p + eivlen; |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | wr->length=(int)(len - (fragment != 0)); |
| 797 | wr->input=(unsigned char *)buf + (fragment != 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 798 | |
| 799 | /* we now 'read' from wr->input, wr->length bytes into |
| 800 | * wr->data */ |
| 801 | |
| 802 | /* first we compress */ |
| 803 | if (s->compress != NULL) |
| 804 | { |
| 805 | if (!ssl3_do_compress(s)) |
| 806 | { |
| 807 | OPENSSL_PUT_ERROR(SSL, do_ssl3_write, SSL_R_COMPRESSION_FAILURE); |
| 808 | goto err; |
| 809 | } |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | memcpy(wr->data,wr->input,wr->length); |
| 814 | wr->input=wr->data; |
| 815 | } |
| 816 | |
| 817 | /* we should still have the output to wr->data and the input |
| 818 | * from wr->input. Length should be wr->length. |
| 819 | * wr->data still points in the wb->buf */ |
| 820 | |
| 821 | if (mac_size != 0) |
| 822 | { |
| 823 | if (s->method->ssl3_enc->mac(s,&(p[wr->length + eivlen]),1) < 0) |
| 824 | goto err; |
| 825 | wr->length+=mac_size; |
| 826 | } |
| 827 | |
| 828 | wr->input=p; |
| 829 | wr->data=p; |
| 830 | |
| 831 | if (eivlen) |
| 832 | { |
| 833 | /* if (RAND_pseudo_bytes(p, eivlen) <= 0) |
| 834 | goto err; */ |
| 835 | wr->length += eivlen; |
| 836 | } |
| 837 | |
| 838 | /* ssl3_enc can only have an error on read */ |
| 839 | s->method->ssl3_enc->enc(s,1); |
| 840 | |
| 841 | /* record length after mac and block padding */ |
| 842 | s2n(wr->length,plen); |
| 843 | |
| 844 | if (s->msg_callback) |
| 845 | s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s, s->msg_callback_arg); |
| 846 | |
| 847 | /* we should now have |
| 848 | * wr->data pointing to the encrypted data, which is |
| 849 | * wr->length long */ |
| 850 | wr->type=type; /* not needed but helps for debugging */ |
| 851 | wr->length+=SSL3_RT_HEADER_LENGTH; |
| 852 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 853 | if (is_fragment) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 854 | { |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 855 | /* we are in a recursive call; just return the length, don't |
| 856 | * write out anything. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 857 | return wr->length; |
| 858 | } |
| 859 | |
| 860 | /* now let's set up wb */ |
| 861 | wb->left = prefix_len + wr->length; |
| 862 | |
| 863 | /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ |
| 864 | s->s3->wpend_tot=len; |
| 865 | s->s3->wpend_buf=buf; |
| 866 | s->s3->wpend_type=type; |
| 867 | s->s3->wpend_ret=len; |
| 868 | |
| 869 | /* we now just need to write the buffer */ |
| 870 | return ssl3_write_pending(s,type,buf,len); |
| 871 | err: |
| 872 | return -1; |
| 873 | } |
| 874 | |
| 875 | /* if s->s3->wbuf.left != 0, we need to call this */ |
| 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->s3->wbuf); |
| 881 | |
| 882 | /* XXXX */ |
| 883 | if ((s->s3->wpend_tot > (int)len) |
| 884 | || ((s->s3->wpend_buf != buf) && |
| 885 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) |
| 886 | || (s->s3->wpend_type != type)) |
| 887 | { |
| 888 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY); |
| 889 | return(-1); |
| 890 | } |
| 891 | |
| 892 | for (;;) |
| 893 | { |
| 894 | ERR_clear_system_error(); |
| 895 | if (s->wbio != NULL) |
| 896 | { |
| 897 | s->rwstate=SSL_WRITING; |
| 898 | i=BIO_write(s->wbio, |
| 899 | (char *)&(wb->buf[wb->offset]), |
| 900 | (unsigned int)wb->left); |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET); |
| 905 | i= -1; |
| 906 | } |
| 907 | if (i == wb->left) |
| 908 | { |
| 909 | wb->left=0; |
| 910 | wb->offset+=i; |
| 911 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && |
| 912 | !SSL_IS_DTLS(s)) |
| 913 | ssl3_release_write_buffer(s); |
| 914 | s->rwstate=SSL_NOTHING; |
| 915 | return(s->s3->wpend_ret); |
| 916 | } |
| 917 | else if (i <= 0) { |
| 918 | if (s->version == DTLS1_VERSION || |
| 919 | s->version == DTLS1_BAD_VER) { |
| 920 | /* For DTLS, just drop it. That's kind of the whole |
| 921 | point in using a datagram service */ |
| 922 | wb->left = 0; |
| 923 | } |
| 924 | return(i); |
| 925 | } |
| 926 | wb->offset+=i; |
| 927 | wb->left-=i; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | /* Return up to 'len' payload bytes received in 'type' records. |
| 932 | * 'type' is one of the following: |
| 933 | * |
| 934 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 935 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 936 | * - 0 (during a shutdown, no data has to be returned) |
| 937 | * |
| 938 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 939 | * (possibly multiple records if we still don't have anything to return). |
| 940 | * |
| 941 | * This function must handle any surprises the peer may have for us, such as |
| 942 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 943 | * a surprise, but handled as if it were), or renegotiation requests. |
| 944 | * Also if record payloads contain fragments too small to process, we store |
| 945 | * them until there is enough for the respective protocol (the record protocol |
| 946 | * may use arbitrary fragmentation and even interleaving): |
| 947 | * Change cipher spec protocol |
| 948 | * just 1 byte needed, no need for keeping anything stored |
| 949 | * Alert protocol |
| 950 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 951 | * Handshake protocol |
| 952 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 953 | * to detect unexpected Client Hello and Hello Request messages |
| 954 | * here, anything else is handled by higher layers |
| 955 | * Application data protocol |
| 956 | * none of our business |
| 957 | */ |
| 958 | int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) |
| 959 | { |
| 960 | int al,i,j,ret; |
| 961 | unsigned int n; |
| 962 | SSL3_RECORD *rr; |
| 963 | void (*cb)(const SSL *ssl,int type2,int val)=NULL; |
| 964 | |
| 965 | if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ |
| 966 | if (!ssl3_setup_read_buffer(s)) |
| 967 | return(-1); |
| 968 | |
| 969 | if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) || |
| 970 | (peek && (type != SSL3_RT_APPLICATION_DATA))) |
| 971 | { |
| 972 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR); |
| 973 | return -1; |
| 974 | } |
| 975 | |
| 976 | if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0)) |
| 977 | /* (partially) satisfy request from storage */ |
| 978 | { |
| 979 | unsigned char *src = s->s3->handshake_fragment; |
| 980 | unsigned char *dst = buf; |
| 981 | unsigned int k; |
| 982 | |
| 983 | /* peek == 0 */ |
| 984 | n = 0; |
| 985 | while ((len > 0) && (s->s3->handshake_fragment_len > 0)) |
| 986 | { |
| 987 | *dst++ = *src++; |
| 988 | len--; s->s3->handshake_fragment_len--; |
| 989 | n++; |
| 990 | } |
| 991 | /* move any remaining fragment bytes: */ |
| 992 | for (k = 0; k < s->s3->handshake_fragment_len; k++) |
| 993 | s->s3->handshake_fragment[k] = *src++; |
| 994 | return n; |
| 995 | } |
| 996 | |
| 997 | /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ |
| 998 | |
| 999 | if (!s->in_handshake && SSL_in_init(s)) |
| 1000 | { |
| 1001 | /* type == SSL3_RT_APPLICATION_DATA */ |
| 1002 | i=s->handshake_func(s); |
| 1003 | if (i < 0) return(i); |
| 1004 | if (i == 0) |
| 1005 | { |
| 1006 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1007 | return(-1); |
| 1008 | } |
| 1009 | } |
| 1010 | start: |
| 1011 | s->rwstate=SSL_NOTHING; |
| 1012 | |
| 1013 | /* s->s3->rrec.type - is the type of record |
| 1014 | * s->s3->rrec.data, - data |
| 1015 | * s->s3->rrec.off, - offset into 'data' for next read |
| 1016 | * s->s3->rrec.length, - number of bytes. */ |
| 1017 | rr = &(s->s3->rrec); |
| 1018 | |
| 1019 | /* get new packet if necessary */ |
| 1020 | if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) |
| 1021 | { |
| 1022 | ret=ssl3_get_record(s); |
| 1023 | if (ret <= 0) return(ret); |
| 1024 | } |
| 1025 | |
| 1026 | /* we now have a packet which can be read and processed */ |
| 1027 | |
| 1028 | if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, |
| 1029 | * reset by ssl3_get_finished */ |
| 1030 | && (rr->type != SSL3_RT_HANDSHAKE)) |
| 1031 | { |
| 1032 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1033 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); |
| 1034 | goto f_err; |
| 1035 | } |
| 1036 | |
| 1037 | /* If the other end has shut down, throw anything we read away |
| 1038 | * (even in 'peek' mode) */ |
| 1039 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) |
| 1040 | { |
| 1041 | rr->length=0; |
| 1042 | s->rwstate=SSL_NOTHING; |
| 1043 | return(0); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 1048 | { |
| 1049 | /* make sure that we are not getting application data when we |
| 1050 | * are doing a handshake for the first time */ |
| 1051 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && |
| 1052 | (s->enc_read_ctx == NULL)) |
| 1053 | { |
| 1054 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1055 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE); |
| 1056 | goto f_err; |
| 1057 | } |
| 1058 | |
| 1059 | if (len <= 0) return(len); |
| 1060 | |
| 1061 | if ((unsigned int)len > rr->length) |
| 1062 | n = rr->length; |
| 1063 | else |
| 1064 | n = (unsigned int)len; |
| 1065 | |
| 1066 | memcpy(buf,&(rr->data[rr->off]),n); |
| 1067 | if (!peek) |
| 1068 | { |
| 1069 | rr->length-=n; |
| 1070 | rr->off+=n; |
| 1071 | if (rr->length == 0) |
| 1072 | { |
| 1073 | s->rstate=SSL_ST_READ_HEADER; |
| 1074 | rr->off=0; |
| 1075 | if (s->mode & SSL_MODE_RELEASE_BUFFERS) |
| 1076 | ssl3_release_read_buffer(s); |
| 1077 | } |
| 1078 | } |
| 1079 | return(n); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | /* If we get here, then type != rr->type; if we have a handshake |
| 1084 | * message, then it was unexpected (Hello Request or Client Hello). */ |
| 1085 | |
| 1086 | /* In case of record types for which we have 'fragment' storage, |
| 1087 | * fill that so that we can process the data at a fixed place. |
| 1088 | */ |
| 1089 | { |
| 1090 | unsigned int dest_maxlen = 0; |
| 1091 | unsigned char *dest = NULL; |
| 1092 | unsigned int *dest_len = NULL; |
| 1093 | |
| 1094 | if (rr->type == SSL3_RT_HANDSHAKE) |
| 1095 | { |
| 1096 | dest_maxlen = sizeof s->s3->handshake_fragment; |
| 1097 | dest = s->s3->handshake_fragment; |
| 1098 | dest_len = &s->s3->handshake_fragment_len; |
| 1099 | } |
| 1100 | else if (rr->type == SSL3_RT_ALERT) |
| 1101 | { |
| 1102 | dest_maxlen = sizeof s->s3->alert_fragment; |
| 1103 | dest = s->s3->alert_fragment; |
| 1104 | dest_len = &s->s3->alert_fragment_len; |
| 1105 | } |
| 1106 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1107 | else if (rr->type == TLS1_RT_HEARTBEAT) |
| 1108 | { |
| 1109 | tls1_process_heartbeat(s); |
| 1110 | |
| 1111 | /* Exit and notify application to read again */ |
| 1112 | rr->length = 0; |
| 1113 | s->rwstate=SSL_READING; |
| 1114 | BIO_clear_retry_flags(SSL_get_rbio(s)); |
| 1115 | BIO_set_retry_read(SSL_get_rbio(s)); |
| 1116 | return(-1); |
| 1117 | } |
| 1118 | #endif |
| 1119 | |
| 1120 | if (dest_maxlen > 0) |
| 1121 | { |
| 1122 | n = dest_maxlen - *dest_len; /* available space in 'dest' */ |
| 1123 | if (rr->length < n) |
| 1124 | n = rr->length; /* available bytes */ |
| 1125 | |
| 1126 | /* now move 'n' bytes: */ |
| 1127 | while (n-- > 0) |
| 1128 | { |
| 1129 | dest[(*dest_len)++] = rr->data[rr->off++]; |
| 1130 | rr->length--; |
| 1131 | } |
| 1132 | |
| 1133 | if (*dest_len < dest_maxlen) |
| 1134 | goto start; /* fragment was too small */ |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; |
| 1139 | * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT. |
| 1140 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ |
| 1141 | |
| 1142 | /* If we are a client, check for an incoming 'Hello Request': */ |
| 1143 | if ((!s->server) && |
| 1144 | (s->s3->handshake_fragment_len >= 4) && |
| 1145 | (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && |
| 1146 | (s->session != NULL) && (s->session->cipher != NULL)) |
| 1147 | { |
| 1148 | s->s3->handshake_fragment_len = 0; |
| 1149 | |
| 1150 | if ((s->s3->handshake_fragment[1] != 0) || |
| 1151 | (s->s3->handshake_fragment[2] != 0) || |
| 1152 | (s->s3->handshake_fragment[3] != 0)) |
| 1153 | { |
| 1154 | al=SSL_AD_DECODE_ERROR; |
| 1155 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST); |
| 1156 | goto f_err; |
| 1157 | } |
| 1158 | |
| 1159 | if (s->msg_callback) |
| 1160 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg); |
| 1161 | |
| 1162 | if (SSL_is_init_finished(s) && |
| 1163 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && |
| 1164 | !s->s3->renegotiate) |
| 1165 | { |
| 1166 | ssl3_renegotiate(s); |
| 1167 | if (ssl3_renegotiate_check(s)) |
| 1168 | { |
| 1169 | i=s->handshake_func(s); |
| 1170 | if (i < 0) return(i); |
| 1171 | if (i == 0) |
| 1172 | { |
| 1173 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1174 | return(-1); |
| 1175 | } |
| 1176 | |
| 1177 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) |
| 1178 | { |
| 1179 | if (s->s3->rbuf.left == 0) /* no read-ahead left? */ |
| 1180 | { |
| 1181 | BIO *bio; |
| 1182 | /* In the case where we try to read application data, |
| 1183 | * but we trigger an SSL handshake, we return -1 with |
| 1184 | * the retry option set. Otherwise renegotiation may |
| 1185 | * cause nasty problems in the blocking world */ |
| 1186 | s->rwstate=SSL_READING; |
| 1187 | bio=SSL_get_rbio(s); |
| 1188 | BIO_clear_retry_flags(bio); |
| 1189 | BIO_set_retry_read(bio); |
| 1190 | return(-1); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | /* we either finished a handshake or ignored the request, |
| 1196 | * now try again to obtain the (application) data we were asked for */ |
| 1197 | goto start; |
| 1198 | } |
| 1199 | /* If we are a server and get a client hello when renegotiation isn't |
| 1200 | * allowed send back a no renegotiation alert and carry on. |
| 1201 | * WARNING: experimental code, needs reviewing (steve) |
| 1202 | */ |
| 1203 | if (s->server && |
| 1204 | SSL_is_init_finished(s) && |
| 1205 | !s->s3->send_connection_binding && |
| 1206 | (s->version > SSL3_VERSION) && |
| 1207 | (s->s3->handshake_fragment_len >= 4) && |
| 1208 | (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) && |
| 1209 | (s->session != NULL) && (s->session->cipher != NULL) && |
| 1210 | !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1211 | |
| 1212 | { |
| 1213 | /*s->s3->handshake_fragment_len = 0;*/ |
| 1214 | rr->length = 0; |
| 1215 | ssl3_send_alert(s,SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); |
| 1216 | goto start; |
| 1217 | } |
| 1218 | if (s->s3->alert_fragment_len >= 2) |
| 1219 | { |
| 1220 | int alert_level = s->s3->alert_fragment[0]; |
| 1221 | int alert_descr = s->s3->alert_fragment[1]; |
| 1222 | |
| 1223 | s->s3->alert_fragment_len = 0; |
| 1224 | |
| 1225 | if (s->msg_callback) |
| 1226 | s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg); |
| 1227 | |
| 1228 | if (s->info_callback != NULL) |
| 1229 | cb=s->info_callback; |
| 1230 | else if (s->ctx->info_callback != NULL) |
| 1231 | cb=s->ctx->info_callback; |
| 1232 | |
| 1233 | if (cb != NULL) |
| 1234 | { |
| 1235 | j = (alert_level << 8) | alert_descr; |
| 1236 | cb(s, SSL_CB_READ_ALERT, j); |
| 1237 | } |
| 1238 | |
| 1239 | if (alert_level == 1) /* warning */ |
| 1240 | { |
| 1241 | s->s3->warn_alert = alert_descr; |
| 1242 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) |
| 1243 | { |
| 1244 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 1245 | return(0); |
| 1246 | } |
| 1247 | /* This is a warning but we receive it if we requested |
| 1248 | * renegotiation and the peer denied it. Terminate with |
| 1249 | * a fatal alert because if application tried to |
| 1250 | * renegotiatie it presumably had a good reason and |
| 1251 | * expects it to succeed. |
| 1252 | * |
| 1253 | * In future we might have a renegotiation where we |
| 1254 | * don't care if the peer refused it where we carry on. |
| 1255 | */ |
| 1256 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) |
| 1257 | { |
| 1258 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1259 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION); |
| 1260 | goto f_err; |
| 1261 | } |
| 1262 | #ifdef SSL_AD_MISSING_SRP_USERNAME |
| 1263 | else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME) |
| 1264 | return(0); |
| 1265 | #endif |
| 1266 | } |
| 1267 | else if (alert_level == 2) /* fatal */ |
| 1268 | { |
| 1269 | char tmp[16]; |
| 1270 | |
| 1271 | s->rwstate=SSL_NOTHING; |
| 1272 | s->s3->fatal_alert = alert_descr; |
| 1273 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_AD_REASON_OFFSET + alert_descr); |
| 1274 | BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr); |
| 1275 | ERR_add_error_data(2,"SSL alert number ",tmp); |
| 1276 | s->shutdown|=SSL_RECEIVED_SHUTDOWN; |
| 1277 | SSL_CTX_remove_session(s->ctx,s->session); |
| 1278 | return(0); |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1283 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE); |
| 1284 | goto f_err; |
| 1285 | } |
| 1286 | |
| 1287 | goto start; |
| 1288 | } |
| 1289 | |
| 1290 | if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */ |
| 1291 | { |
| 1292 | s->rwstate=SSL_NOTHING; |
| 1293 | rr->length=0; |
| 1294 | return(0); |
| 1295 | } |
| 1296 | |
| 1297 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) |
| 1298 | { |
| 1299 | /* 'Change Cipher Spec' is just a single byte, so we know |
| 1300 | * exactly what the record payload has to look like */ |
| 1301 | if ( (rr->length != 1) || (rr->off != 0) || |
| 1302 | (rr->data[0] != SSL3_MT_CCS)) |
| 1303 | { |
| 1304 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1305 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 1306 | goto f_err; |
| 1307 | } |
| 1308 | |
| 1309 | /* Check we have a cipher to change to */ |
| 1310 | if (s->s3->tmp.new_cipher == NULL) |
| 1311 | { |
| 1312 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1313 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY); |
| 1314 | goto f_err; |
| 1315 | } |
| 1316 | |
| 1317 | rr->length=0; |
| 1318 | |
| 1319 | if (s->msg_callback) |
| 1320 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg); |
| 1321 | |
| 1322 | s->s3->change_cipher_spec=1; |
| 1323 | if (!ssl3_do_change_cipher_spec(s)) |
| 1324 | goto err; |
| 1325 | else |
| 1326 | goto start; |
| 1327 | } |
| 1328 | |
| 1329 | /* Unexpected handshake message (Client Hello, or protocol violation) */ |
| 1330 | if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) |
| 1331 | { |
| 1332 | if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && |
| 1333 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) |
| 1334 | { |
| 1335 | #if 0 /* worked only because C operator preferences are not as expected (and |
| 1336 | * because this is not really needed for clients except for detecting |
| 1337 | * protocol violations): */ |
| 1338 | s->state=SSL_ST_BEFORE|(s->server) |
| 1339 | ?SSL_ST_ACCEPT |
| 1340 | :SSL_ST_CONNECT; |
| 1341 | #else |
| 1342 | s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; |
| 1343 | #endif |
| 1344 | s->renegotiate=1; |
| 1345 | s->new_session=1; |
| 1346 | } |
| 1347 | i=s->handshake_func(s); |
| 1348 | if (i < 0) return(i); |
| 1349 | if (i == 0) |
| 1350 | { |
| 1351 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1352 | return(-1); |
| 1353 | } |
| 1354 | |
| 1355 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) |
| 1356 | { |
| 1357 | if (s->s3->rbuf.left == 0) /* no read-ahead left? */ |
| 1358 | { |
| 1359 | BIO *bio; |
| 1360 | /* In the case where we try to read application data, |
| 1361 | * but we trigger an SSL handshake, we return -1 with |
| 1362 | * the retry option set. Otherwise renegotiation may |
| 1363 | * cause nasty problems in the blocking world */ |
| 1364 | s->rwstate=SSL_READING; |
| 1365 | bio=SSL_get_rbio(s); |
| 1366 | BIO_clear_retry_flags(bio); |
| 1367 | BIO_set_retry_read(bio); |
| 1368 | return(-1); |
| 1369 | } |
| 1370 | } |
| 1371 | goto start; |
| 1372 | } |
| 1373 | |
| 1374 | switch (rr->type) |
| 1375 | { |
| 1376 | default: |
| 1377 | #ifndef OPENSSL_NO_TLS |
| 1378 | /* TLS up to v1.1 just ignores unknown message types: |
| 1379 | * TLS v1.2 give an unexpected message alert. |
| 1380 | */ |
| 1381 | if (s->version >= TLS1_VERSION && s->version <= TLS1_1_VERSION) |
| 1382 | { |
| 1383 | rr->length = 0; |
| 1384 | goto start; |
| 1385 | } |
| 1386 | #endif |
| 1387 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1388 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD); |
| 1389 | goto f_err; |
| 1390 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
| 1391 | case SSL3_RT_ALERT: |
| 1392 | case SSL3_RT_HANDSHAKE: |
| 1393 | /* we already handled all of these, with the possible exception |
| 1394 | * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that |
| 1395 | * should not happen when type != rr->type */ |
| 1396 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1397 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR); |
| 1398 | goto f_err; |
| 1399 | case SSL3_RT_APPLICATION_DATA: |
| 1400 | /* At this point, we were expecting handshake data, |
| 1401 | * but have application data. If the library was |
| 1402 | * running inside ssl3_read() (i.e. in_read_app_data |
| 1403 | * is set) and it makes sense to read application data |
| 1404 | * at this point (session renegotiation not yet started), |
| 1405 | * we will indulge it. |
| 1406 | */ |
| 1407 | if (s->s3->in_read_app_data && |
| 1408 | (s->s3->total_renegotiations != 0) && |
| 1409 | (( |
| 1410 | (s->state & SSL_ST_CONNECT) && |
| 1411 | (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && |
| 1412 | (s->state <= SSL3_ST_CR_SRVR_HELLO_A) |
| 1413 | ) || ( |
| 1414 | (s->state & SSL_ST_ACCEPT) && |
| 1415 | (s->state <= SSL3_ST_SW_HELLO_REQ_A) && |
| 1416 | (s->state >= SSL3_ST_SR_CLNT_HELLO_A) |
| 1417 | ) |
| 1418 | )) |
| 1419 | { |
| 1420 | s->s3->in_read_app_data=2; |
| 1421 | return(-1); |
| 1422 | } |
| 1423 | else |
| 1424 | { |
| 1425 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 1426 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD); |
| 1427 | goto f_err; |
| 1428 | } |
| 1429 | } |
| 1430 | /* not reached */ |
| 1431 | |
| 1432 | f_err: |
| 1433 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1434 | err: |
| 1435 | return(-1); |
| 1436 | } |
| 1437 | |
| 1438 | int ssl3_do_change_cipher_spec(SSL *s) |
| 1439 | { |
| 1440 | int i; |
| 1441 | const char *sender; |
| 1442 | int slen; |
| 1443 | |
| 1444 | if (s->state & SSL_ST_ACCEPT) |
| 1445 | i=SSL3_CHANGE_CIPHER_SERVER_READ; |
| 1446 | else |
| 1447 | i=SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 1448 | |
| 1449 | if (s->s3->tmp.key_block == NULL) |
| 1450 | { |
| 1451 | if (s->session == NULL) |
| 1452 | { |
| 1453 | /* might happen if dtls1_read_bytes() calls this */ |
| 1454 | OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec, SSL_R_CCS_RECEIVED_EARLY); |
| 1455 | return (0); |
| 1456 | } |
| 1457 | |
| 1458 | s->session->cipher=s->s3->tmp.new_cipher; |
| 1459 | if (!s->method->ssl3_enc->setup_key_block(s)) return(0); |
| 1460 | } |
| 1461 | |
| 1462 | if (!s->method->ssl3_enc->change_cipher_state(s,i)) |
| 1463 | return(0); |
| 1464 | |
| 1465 | /* we have to record the message digest at |
| 1466 | * this point so we can get it before we read |
| 1467 | * the finished message */ |
| 1468 | if (s->state & SSL_ST_CONNECT) |
| 1469 | { |
| 1470 | sender=s->method->ssl3_enc->server_finished_label; |
| 1471 | slen=s->method->ssl3_enc->server_finished_label_len; |
| 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | sender=s->method->ssl3_enc->client_finished_label; |
| 1476 | slen=s->method->ssl3_enc->client_finished_label_len; |
| 1477 | } |
| 1478 | |
| 1479 | i = s->method->ssl3_enc->final_finish_mac(s, |
| 1480 | sender,slen,s->s3->tmp.peer_finish_md); |
| 1481 | if (i == 0) |
| 1482 | { |
| 1483 | OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec, ERR_R_INTERNAL_ERROR); |
| 1484 | return 0; |
| 1485 | } |
| 1486 | s->s3->tmp.peer_finish_md_len = i; |
| 1487 | |
| 1488 | return(1); |
| 1489 | } |
| 1490 | |
| 1491 | int ssl3_send_alert(SSL *s, int level, int desc) |
| 1492 | { |
| 1493 | /* Map tls/ssl alert value to correct one */ |
| 1494 | desc=s->method->ssl3_enc->alert_value(desc); |
| 1495 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) |
| 1496 | desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */ |
| 1497 | if (desc < 0) return -1; |
| 1498 | /* If a fatal one, remove from cache */ |
| 1499 | if ((level == 2) && (s->session != NULL)) |
| 1500 | SSL_CTX_remove_session(s->ctx,s->session); |
| 1501 | |
| 1502 | s->s3->alert_dispatch=1; |
| 1503 | s->s3->send_alert[0]=level; |
| 1504 | s->s3->send_alert[1]=desc; |
| 1505 | if (s->s3->wbuf.left == 0) /* data still being written out? */ |
| 1506 | return s->method->ssl_dispatch_alert(s); |
| 1507 | /* else data is still being written out, we will get written |
| 1508 | * some time in the future */ |
| 1509 | return -1; |
| 1510 | } |
| 1511 | |
| 1512 | int ssl3_dispatch_alert(SSL *s) |
| 1513 | { |
| 1514 | int i,j; |
| 1515 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 1516 | |
| 1517 | s->s3->alert_dispatch=0; |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1518 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1519 | if (i <= 0) |
| 1520 | { |
| 1521 | s->s3->alert_dispatch=1; |
| 1522 | } |
| 1523 | else |
| 1524 | { |
| 1525 | /* Alert sent to BIO. If it is important, flush it now. |
| 1526 | * If the message does not get sent due to non-blocking IO, |
| 1527 | * we will not worry too much. */ |
| 1528 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) |
| 1529 | (void)BIO_flush(s->wbio); |
| 1530 | |
| 1531 | if (s->msg_callback) |
| 1532 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg); |
| 1533 | |
| 1534 | if (s->info_callback != NULL) |
| 1535 | cb=s->info_callback; |
| 1536 | else if (s->ctx->info_callback != NULL) |
| 1537 | cb=s->ctx->info_callback; |
| 1538 | |
| 1539 | if (cb != NULL) |
| 1540 | { |
| 1541 | j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; |
| 1542 | cb(s,SSL_CB_WRITE_ALERT,j); |
| 1543 | } |
| 1544 | } |
| 1545 | return(i); |
| 1546 | } |