1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
111 #include "../ssl_locl.h"
112 #include "internal/constant_time_locl.h"
113 #include <openssl/rand.h>
114 #include "record_locl.h"
116 static const unsigned char ssl3_pad_1[48] = {
117 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
118 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
119 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
120 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
121 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
122 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
125 static const unsigned char ssl3_pad_2[48] = {
126 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
127 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
128 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
129 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
130 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
131 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
135 * Clear the contents of an SSL3_RECORD but retain any memory allocated
137 void SSL3_RECORD_clear(SSL3_RECORD *r)
139 unsigned char *comp = r->comp;
141 memset(r, 0, sizeof(*r));
145 void SSL3_RECORD_release(SSL3_RECORD *r)
147 OPENSSL_free(r->comp);
151 int SSL3_RECORD_setup(SSL3_RECORD *r)
154 r->comp = (unsigned char *)
155 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
161 void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
163 memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
167 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
168 * will be processed per call to ssl3_get_record. Without this limit an
169 * attacker could send empty records at a faster rate than we can process and
170 * cause ssl3_get_record to loop forever.
172 #define MAX_EMPTY_RECORDS 32
174 #define SSL2_RT_HEADER_LENGTH 2
176 * Call this to get a new input record.
177 * It will return <= 0 if more data is needed, normally due to an error
178 * or non-blocking IO.
179 * When it finishes, one packet has been decoded and can be found in
180 * ssl->s3->rrec.type - is the type of record
181 * ssl->s3->rrec.data, - data
182 * ssl->s3->rrec.length, - number of bytes
184 /* used only by ssl3_read_bytes */
185 int ssl3_get_record(SSL *s)
187 int ssl_major, ssl_minor, al;
188 int enc_err, n, i, ret = -1;
192 unsigned char md[EVP_MAX_MD_SIZE];
195 unsigned empty_record_count = 0;
197 rr = RECORD_LAYER_get_rrec(&s->rlayer);
201 /* check if we have the header */
202 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
203 (RECORD_LAYER_get_packet_length(&s->rlayer) < SSL3_RT_HEADER_LENGTH)) {
204 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
205 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0);
207 return (n); /* error or non-blocking */
208 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
210 p = RECORD_LAYER_get_packet(&s->rlayer);
213 * Check whether this is a regular record or an SSLv2 style record. The
214 * latter is only used in an initial ClientHello for old clients. We
215 * check s->read_hash and s->enc_read_ctx to ensure this does not apply
216 * during renegotiation
218 if (s->first_packet && s->server && !s->read_hash && !s->enc_read_ctx
219 && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
220 /* SSLv2 style record */
221 rr->type = SSL3_RT_HANDSHAKE;
222 rr->rec_version = SSL2_VERSION;
224 rr->length = ((p[0] & 0x7f) << 8) | p[1];
226 if (rr->length > SSL3_BUFFER_get_len(&s->rlayer.rbuf)
227 - SSL2_RT_HEADER_LENGTH) {
228 al = SSL_AD_RECORD_OVERFLOW;
229 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
233 if (rr->length < MIN_SSL2_RECORD_LEN) {
234 al = SSL_AD_HANDSHAKE_FAILURE;
235 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
239 /* SSLv3+ style record */
241 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
242 s->msg_callback_arg);
244 /* Pull apart the header into the SSL3_RECORD */
248 version = (ssl_major << 8) | ssl_minor;
249 rr->rec_version = version;
252 /* Lets check version */
253 if (!s->first_packet && version != s->version) {
254 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
255 if ((s->version & 0xFF00) == (version & 0xFF00)
256 && !s->enc_write_ctx && !s->write_hash) {
257 if (rr->type == SSL3_RT_ALERT) {
259 * The record is using an incorrect version number, but
260 * what we've got appears to be an alert. We haven't
261 * read the body yet to check whether its a fatal or
262 * not - but chances are it is. We probably shouldn't
263 * send a fatal alert back. We'll just end.
268 * Send back error using their minor version number :-)
270 s->version = (unsigned short)version;
272 al = SSL_AD_PROTOCOL_VERSION;
276 if ((version >> 8) != SSL3_VERSION_MAJOR) {
277 if (s->first_packet) {
278 /* Go back to start of packet, look at the five bytes
280 p = RECORD_LAYER_get_packet(&s->rlayer);
281 if (strncmp((char *)p, "GET ", 4) == 0 ||
282 strncmp((char *)p, "POST ", 5) == 0 ||
283 strncmp((char *)p, "HEAD ", 5) == 0 ||
284 strncmp((char *)p, "PUT ", 4) == 0) {
285 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
287 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
288 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTPS_PROXY_REQUEST);
292 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
297 SSL3_BUFFER_get_len(&s->rlayer.rbuf)
298 - SSL3_RT_HEADER_LENGTH) {
299 al = SSL_AD_RECORD_OVERFLOW;
300 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
305 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
309 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
310 * Calculate how much more data we need to read for the rest of the record
312 if (rr->rec_version == SSL2_VERSION) {
313 i = rr->length + SSL2_RT_HEADER_LENGTH - SSL3_RT_HEADER_LENGTH;
318 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
320 n = ssl3_read_n(s, i, i, 1);
322 return (n); /* error or non-blocking io */
325 /* set state for later operations */
326 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
329 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
330 * or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
331 * and we have that many bytes in s->packet
333 if(rr->rec_version == SSL2_VERSION) {
334 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
336 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
340 * ok, we can now read from 's->packet' data into 'rr' rr->input points
341 * at rr->length bytes, which need to be copied into rr->data by either
342 * the decryption or by the decompression When the data is 'copied' into
343 * the rr->data buffer, rr->input will be pointed at the new buffer
347 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
348 * bytes of encrypted compressed stuff.
351 /* check is not needed I believe */
352 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
353 al = SSL_AD_RECORD_OVERFLOW;
354 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
358 /* decrypt in place in 'rr->input' */
359 rr->data = rr->input;
360 rr->orig_len = rr->length;
362 * If in encrypt-then-mac mode calculate mac from encrypted record. All
363 * the details below are public so no timing details can leak.
365 if (SSL_USE_ETM(s) && s->read_hash) {
367 mac_size = EVP_MD_CTX_size(s->read_hash);
368 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
369 if (rr->length < mac_size) {
370 al = SSL_AD_DECODE_ERROR;
371 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
374 rr->length -= mac_size;
375 mac = rr->data + rr->length;
376 i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
377 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
378 al = SSL_AD_BAD_RECORD_MAC;
379 SSLerr(SSL_F_SSL3_GET_RECORD,
380 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
385 enc_err = s->method->ssl3_enc->enc(s, 0);
388 * 0: (in non-constant time) if the record is publically invalid.
389 * 1: if the padding is valid
390 * -1: if the padding is invalid
393 al = SSL_AD_DECRYPTION_FAILED;
394 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
398 printf("dec %d\n", rr->length);
401 for (z = 0; z < rr->length; z++)
402 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
407 /* r->length is now the compressed data plus mac */
408 if ((sess != NULL) &&
409 (s->enc_read_ctx != NULL) &&
410 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
411 /* s->read_hash != NULL => mac_size != -1 */
412 unsigned char *mac = NULL;
413 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
414 mac_size = EVP_MD_CTX_size(s->read_hash);
415 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
418 * orig_len is the length of the record before any padding was
419 * removed. This is public information, as is the MAC in use,
420 * therefore we can safely process the record in a different amount
421 * of time if it's too short to possibly contain a MAC.
423 if (rr->orig_len < mac_size ||
424 /* CBC records must have a padding length byte too. */
425 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
426 rr->orig_len < mac_size + 1)) {
427 al = SSL_AD_DECODE_ERROR;
428 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
432 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
434 * We update the length so that the TLS header bytes can be
435 * constructed correctly but we need to extract the MAC in
436 * constant time from within the record, without leaking the
437 * contents of the padding bytes.
440 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
441 rr->length -= mac_size;
444 * In this case there's no padding, so |rec->orig_len| equals
445 * |rec->length| and we checked that there's enough bytes for
448 rr->length -= mac_size;
449 mac = &rr->data[rr->length];
452 i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
453 if (i < 0 || mac == NULL
454 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
456 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
462 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
463 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
464 * failure is directly visible from the ciphertext anyway, we should
465 * not reveal which kind of error occurred -- this might become
466 * visible to an attacker (e.g. via a logfile)
468 al = SSL_AD_BAD_RECORD_MAC;
469 SSLerr(SSL_F_SSL3_GET_RECORD,
470 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
474 /* r->length is now just compressed */
475 if (s->expand != NULL) {
476 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
477 al = SSL_AD_RECORD_OVERFLOW;
478 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
481 if (!ssl3_do_uncompress(s)) {
482 al = SSL_AD_DECOMPRESSION_FAILURE;
483 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
488 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
489 al = SSL_AD_RECORD_OVERFLOW;
490 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
496 * So at this point the following is true
497 * ssl->s3->rrec.type is the type of record
498 * ssl->s3->rrec.length == number of bytes in record
499 * ssl->s3->rrec.off == offset to first valid byte
500 * ssl->s3->rrec.data == where to take bytes from, increment
504 /* we have pulled in a full packet so zero things */
505 RECORD_LAYER_reset_packet_length(&s->rlayer);
507 /* just read a 0 length packet */
508 if (rr->length == 0) {
509 empty_record_count++;
510 if (empty_record_count > MAX_EMPTY_RECORDS) {
511 al = SSL_AD_UNEXPECTED_MESSAGE;
512 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
521 ssl3_send_alert(s, SSL3_AL_FATAL, al);
526 int ssl3_do_uncompress(SSL *ssl)
528 #ifndef OPENSSL_NO_COMP
532 rr = RECORD_LAYER_get_rrec(&ssl->rlayer);
533 i = COMP_expand_block(ssl->expand, rr->comp,
534 SSL3_RT_MAX_PLAIN_LENGTH, rr->data,
545 int ssl3_do_compress(SSL *ssl)
547 #ifndef OPENSSL_NO_COMP
551 wr = RECORD_LAYER_get_wrec(&ssl->rlayer);
552 i = COMP_compress_block(ssl->compress, wr->data,
553 SSL3_RT_MAX_COMPRESSED_LENGTH,
554 wr->input, (int)wr->length);
560 wr->input = wr->data;
566 * ssl3_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
569 * 0: (in non-constant time) if the record is publically invalid (i.e. too
571 * 1: if the record's padding is valid / the encryption was successful.
572 * -1: if the record's padding is invalid or, if sending, an internal error
575 int ssl3_enc(SSL *s, int send)
580 int bs, i, mac_size = 0;
581 const EVP_CIPHER *enc;
584 ds = s->enc_write_ctx;
585 rec = RECORD_LAYER_get_wrec(&s->rlayer);
586 if (s->enc_write_ctx == NULL)
589 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
591 ds = s->enc_read_ctx;
592 rec = RECORD_LAYER_get_rrec(&s->rlayer);
593 if (s->enc_read_ctx == NULL)
596 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
599 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
600 memmove(rec->data, rec->input, rec->length);
601 rec->input = rec->data;
604 bs = EVP_CIPHER_CTX_block_size(ds);
608 if ((bs != 1) && send) {
609 i = bs - ((int)l % bs);
611 /* we need to add 'i-1' padding bytes */
614 * the last of these zero bytes will be overwritten with the
617 memset(&rec->input[rec->length], 0, i);
619 rec->input[l - 1] = (i - 1);
623 if (l == 0 || l % bs != 0)
625 /* otherwise, rec->length >= bs */
628 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
631 if (EVP_MD_CTX_md(s->read_hash) != NULL)
632 mac_size = EVP_MD_CTX_size(s->read_hash);
633 if ((bs != 1) && !send)
634 return ssl3_cbc_remove_padding(s, rec, bs, mac_size);
640 * tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
643 * 0: (in non-constant time) if the record is publically invalid (i.e. too
645 * 1: if the record's padding is valid / the encryption was successful.
646 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
647 * an internal error occurred.
649 int tls1_enc(SSL *s, int send)
654 int bs, i, j, k, pad = 0, ret, mac_size = 0;
655 const EVP_CIPHER *enc;
658 if (EVP_MD_CTX_md(s->write_hash)) {
659 int n = EVP_MD_CTX_size(s->write_hash);
660 OPENSSL_assert(n >= 0);
662 ds = s->enc_write_ctx;
663 rec = RECORD_LAYER_get_wrec(&s->rlayer);
664 if (s->enc_write_ctx == NULL)
668 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
669 /* For TLSv1.1 and later explicit IV */
670 if (SSL_USE_EXPLICIT_IV(s)
671 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
672 ivlen = EVP_CIPHER_iv_length(enc);
676 if (rec->data != rec->input)
678 * we can't write into the input stream: Can this ever
681 fprintf(stderr, "tls1_enc(): rec->data != rec->input\n");
682 else if (RAND_bytes(rec->input, ivlen) <= 0)
687 if (EVP_MD_CTX_md(s->read_hash)) {
688 int n = EVP_MD_CTX_size(s->read_hash);
689 OPENSSL_assert(n >= 0);
691 ds = s->enc_read_ctx;
692 rec = RECORD_LAYER_get_rrec(&s->rlayer);
693 if (s->enc_read_ctx == NULL)
696 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
699 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
700 memmove(rec->data, rec->input, rec->length);
701 rec->input = rec->data;
705 bs = EVP_CIPHER_CTX_block_size(ds);
707 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds)) & EVP_CIPH_FLAG_AEAD_CIPHER) {
708 unsigned char buf[EVP_AEAD_TLS1_AAD_LEN], *seq;
710 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
711 : RECORD_LAYER_get_read_sequence(&s->rlayer);
713 if (SSL_IS_DTLS(s)) {
714 unsigned char dtlsseq[9], *p = dtlsseq;
716 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
717 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
718 memcpy(p, &seq[2], 6);
719 memcpy(buf, dtlsseq, 8);
722 for (i = 7; i >= 0; i--) { /* increment */
730 buf[9] = (unsigned char)(s->version >> 8);
731 buf[10] = (unsigned char)(s->version);
732 buf[11] = rec->length >> 8;
733 buf[12] = rec->length & 0xff;
734 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
735 EVP_AEAD_TLS1_AAD_LEN, buf);
742 } else if ((bs != 1) && send) {
743 i = bs - ((int)l % bs);
745 /* Add weird padding of upto 256 bytes */
747 /* we need to add 'i' padding bytes of value j */
749 for (k = (int)l; k < (int)(l + i); k++)
756 if (l == 0 || l % bs != 0)
760 i = EVP_Cipher(ds, rec->data, rec->input, l);
761 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds)) & EVP_CIPH_FLAG_CUSTOM_CIPHER)
764 return -1; /* AEAD can fail to verify MAC */
766 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
767 rec->data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
768 rec->input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
769 rec->length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
770 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
771 rec->data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
772 rec->input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
773 rec->length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
778 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)
779 mac_size = EVP_MD_CTX_size(s->read_hash);
780 if ((bs != 1) && !send)
781 ret = tls1_cbc_remove_padding(s, rec, bs, mac_size);
788 int n_ssl3_mac(SSL *ssl, unsigned char *md, int send)
791 unsigned char *mac_sec, *seq;
792 const EVP_MD_CTX *hash;
793 unsigned char *p, rec_char;
799 rec = RECORD_LAYER_get_wrec(&ssl->rlayer);
800 mac_sec = &(ssl->s3->write_mac_secret[0]);
801 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
802 hash = ssl->write_hash;
804 rec = RECORD_LAYER_get_rrec(&ssl->rlayer);
805 mac_sec = &(ssl->s3->read_mac_secret[0]);
806 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
807 hash = ssl->read_hash;
810 t = EVP_MD_CTX_size(hash);
814 npad = (48 / md_size) * md_size;
817 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
818 ssl3_cbc_record_digest_supported(hash)) {
820 * This is a CBC-encrypted record. We must avoid leaking any
821 * timing-side channel information about how many blocks of data we
822 * are hashing because that gives an attacker a timing-oracle.
826 * npad is, at most, 48 bytes and that's with MD5:
827 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
829 * With SHA-1 (the largest hash speced for SSLv3) the hash size
830 * goes up 4, but npad goes down by 8, resulting in a smaller
833 unsigned char header[75];
835 memcpy(header + j, mac_sec, md_size);
837 memcpy(header + j, ssl3_pad_1, npad);
839 memcpy(header + j, seq, 8);
841 header[j++] = rec->type;
842 header[j++] = rec->length >> 8;
843 header[j++] = rec->length & 0xff;
845 /* Final param == is SSLv3 */
846 if (ssl3_cbc_digest_record(hash,
849 rec->length + md_size, rec->orig_len,
850 mac_sec, md_size, 1) <= 0)
853 unsigned int md_size_u;
854 /* Chop the digest off the end :-) */
855 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
860 rec_char = rec->type;
863 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
864 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
865 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
866 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
867 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
868 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
869 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
870 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
871 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
872 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
873 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
874 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
875 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
876 EVP_MD_CTX_reset(md_ctx);
881 EVP_MD_CTX_free(md_ctx);
884 ssl3_record_sequence_update(seq);
888 int tls1_mac(SSL *ssl, unsigned char *md, int send)
895 EVP_MD_CTX *hmac = NULL, *mac_ctx;
896 unsigned char header[13];
897 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
898 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
902 rec = RECORD_LAYER_get_wrec(&ssl->rlayer);
903 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
904 hash = ssl->write_hash;
906 rec = RECORD_LAYER_get_rrec(&ssl->rlayer);
907 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
908 hash = ssl->read_hash;
911 t = EVP_MD_CTX_size(hash);
912 OPENSSL_assert(t >= 0);
915 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
919 hmac = EVP_MD_CTX_new();
921 || !EVP_MD_CTX_copy(hmac, hash))
926 if (SSL_IS_DTLS(ssl)) {
927 unsigned char dtlsseq[8], *p = dtlsseq;
929 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
930 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
931 memcpy(p, &seq[2], 6);
933 memcpy(header, dtlsseq, 8);
935 memcpy(header, seq, 8);
937 header[8] = rec->type;
938 header[9] = (unsigned char)(ssl->version >> 8);
939 header[10] = (unsigned char)(ssl->version);
940 header[11] = (rec->length) >> 8;
941 header[12] = (rec->length) & 0xff;
943 if (!send && !SSL_USE_ETM(ssl) &&
944 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
945 ssl3_cbc_record_digest_supported(mac_ctx)) {
947 * This is a CBC-encrypted record. We must avoid leaking any
948 * timing-side channel information about how many blocks of data we
949 * are hashing because that gives an attacker a timing-oracle.
951 /* Final param == not SSLv3 */
952 if (ssl3_cbc_digest_record(mac_ctx,
955 rec->length + md_size, rec->orig_len,
956 ssl->s3->read_mac_secret,
957 ssl->s3->read_mac_secret_size, 0) <= 0) {
958 EVP_MD_CTX_free(hmac);
962 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
963 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
964 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
965 EVP_MD_CTX_free(hmac);
968 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
969 tls_fips_digest_extra(ssl->enc_read_ctx,
971 rec->length, rec->orig_len);
974 EVP_MD_CTX_free(hmac);
977 fprintf(stderr, "seq=");
980 for (z = 0; z < 8; z++)
981 fprintf(stderr, "%02X ", seq[z]);
982 fprintf(stderr, "\n");
984 fprintf(stderr, "rec=");
987 for (z = 0; z < rec->length; z++)
988 fprintf(stderr, "%02X ", rec->data[z]);
989 fprintf(stderr, "\n");
993 if (!SSL_IS_DTLS(ssl)) {
994 for (i = 7; i >= 0; i--) {
1003 for (z = 0; z < md_size; z++)
1004 fprintf(stderr, "%02X ", md[z]);
1005 fprintf(stderr, "\n");
1012 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1013 * record in |rec| by updating |rec->length| in constant time.
1015 * block_size: the block size of the cipher used to encrypt the record.
1017 * 0: (in non-constant time) if the record is publicly invalid.
1018 * 1: if the padding was valid
1021 int ssl3_cbc_remove_padding(const SSL *s,
1023 unsigned block_size, unsigned mac_size)
1025 unsigned padding_length, good;
1026 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1029 * These lengths are all public so we can test them in non-constant time.
1031 if (overhead > rec->length)
1034 padding_length = rec->data[rec->length - 1];
1035 good = constant_time_ge(rec->length, padding_length + overhead);
1036 /* SSLv3 requires that the padding is minimal. */
1037 good &= constant_time_ge(block_size, padding_length + 1);
1038 rec->length -= good & (padding_length + 1);
1039 return constant_time_select_int(good, 1, -1);
1043 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1044 * record in |rec| in constant time and returns 1 if the padding is valid and
1045 * -1 otherwise. It also removes any explicit IV from the start of the record
1046 * without leaking any timing about whether there was enough space after the
1047 * padding was removed.
1049 * block_size: the block size of the cipher used to encrypt the record.
1051 * 0: (in non-constant time) if the record is publicly invalid.
1052 * 1: if the padding was valid
1055 int tls1_cbc_remove_padding(const SSL *s,
1057 unsigned block_size, unsigned mac_size)
1059 unsigned padding_length, good, to_check, i;
1060 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1061 /* Check if version requires explicit IV */
1062 if (SSL_USE_EXPLICIT_IV(s)) {
1064 * These lengths are all public so we can test them in non-constant
1067 if (overhead + block_size > rec->length)
1069 /* We can now safely skip explicit IV */
1070 rec->data += block_size;
1071 rec->input += block_size;
1072 rec->length -= block_size;
1073 rec->orig_len -= block_size;
1074 } else if (overhead > rec->length)
1077 padding_length = rec->data[rec->length - 1];
1079 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) {
1080 /* padding is already verified */
1081 rec->length -= padding_length + 1;
1085 good = constant_time_ge(rec->length, overhead + padding_length);
1087 * The padding consists of a length byte at the end of the record and
1088 * then that many bytes of padding, all with the same value as the length
1089 * byte. Thus, with the length byte included, there are i+1 bytes of
1090 * padding. We can't check just |padding_length+1| bytes because that
1091 * leaks decrypted information. Therefore we always have to check the
1092 * maximum amount of padding possible. (Again, the length of the record
1093 * is public information so we can use it.)
1095 to_check = 255; /* maximum amount of padding. */
1096 if (to_check > rec->length - 1)
1097 to_check = rec->length - 1;
1099 for (i = 0; i < to_check; i++) {
1100 unsigned char mask = constant_time_ge_8(padding_length, i);
1101 unsigned char b = rec->data[rec->length - 1 - i];
1103 * The final |padding_length+1| bytes should all have the value
1104 * |padding_length|. Therefore the XOR should be zero.
1106 good &= ~(mask & (padding_length ^ b));
1110 * If any of the final |padding_length+1| bytes had the wrong value, one
1111 * or more of the lower eight bits of |good| will be cleared.
1113 good = constant_time_eq(0xff, good & 0xff);
1114 rec->length -= good & (padding_length + 1);
1116 return constant_time_select_int(good, 1, -1);
1120 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1121 * constant time (independent of the concrete value of rec->length, which may
1122 * vary within a 256-byte window).
1124 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1128 * rec->orig_len >= md_size
1129 * md_size <= EVP_MAX_MD_SIZE
1131 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1132 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1133 * a single or pair of cache-lines, then the variable memory accesses don't
1134 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1135 * not multi-core and are not considered vulnerable to cache-timing attacks.
1137 #define CBC_MAC_ROTATE_IN_PLACE
1139 void ssl3_cbc_copy_mac(unsigned char *out,
1140 const SSL3_RECORD *rec, unsigned md_size)
1142 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1143 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1144 unsigned char *rotated_mac;
1146 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1150 * mac_end is the index of |rec->data| just after the end of the MAC.
1152 unsigned mac_end = rec->length;
1153 unsigned mac_start = mac_end - md_size;
1155 * scan_start contains the number of bytes that we can ignore because the
1156 * MAC's position can only vary by 255 bytes.
1158 unsigned scan_start = 0;
1160 unsigned div_spoiler;
1161 unsigned rotate_offset;
1163 OPENSSL_assert(rec->orig_len >= md_size);
1164 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1166 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1167 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1170 /* This information is public so it's safe to branch based on it. */
1171 if (rec->orig_len > md_size + 255 + 1)
1172 scan_start = rec->orig_len - (md_size + 255 + 1);
1174 * div_spoiler contains a multiple of md_size that is used to cause the
1175 * modulo operation to be constant time. Without this, the time varies
1176 * based on the amount of padding when running on Intel chips at least.
1177 * The aim of right-shifting md_size is so that the compiler doesn't
1178 * figure out that it can remove div_spoiler as that would require it to
1179 * prove that md_size is always even, which I hope is beyond it.
1181 div_spoiler = md_size >> 1;
1182 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1183 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1185 memset(rotated_mac, 0, md_size);
1186 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1187 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1188 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1189 unsigned char b = rec->data[i];
1190 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1191 j &= constant_time_lt(j, md_size);
1194 /* Now rotate the MAC */
1195 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1197 for (i = 0; i < md_size; i++) {
1198 /* in case cache-line is 32 bytes, touch second line */
1199 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1200 out[j++] = rotated_mac[rotate_offset++];
1201 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1204 memset(out, 0, md_size);
1205 rotate_offset = md_size - rotate_offset;
1206 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1207 for (i = 0; i < md_size; i++) {
1208 for (j = 0; j < md_size; j++)
1209 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1211 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1216 int dtls1_process_record(SSL *s)
1222 unsigned int mac_size;
1223 unsigned char md[EVP_MAX_MD_SIZE];
1225 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1229 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1230 * and we have that many bytes in s->packet
1232 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1235 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1236 * at rr->length bytes, which need to be copied into rr->data by either
1237 * the decryption or by the decompression When the data is 'copied' into
1238 * the rr->data buffer, rr->input will be pointed at the new buffer
1242 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1243 * bytes of encrypted compressed stuff.
1246 /* check is not needed I believe */
1247 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1248 al = SSL_AD_RECORD_OVERFLOW;
1249 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1253 /* decrypt in place in 'rr->input' */
1254 rr->data = rr->input;
1255 rr->orig_len = rr->length;
1257 enc_err = s->method->ssl3_enc->enc(s, 0);
1260 * 0: (in non-constant time) if the record is publically invalid.
1261 * 1: if the padding is valid
1262 * -1: if the padding is invalid
1265 /* For DTLS we simply ignore bad packets. */
1267 RECORD_LAYER_reset_packet_length(&s->rlayer);
1271 printf("dec %d\n", rr->length);
1274 for (z = 0; z < rr->length; z++)
1275 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1280 /* r->length is now the compressed data plus mac */
1281 if ((sess != NULL) &&
1282 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1283 /* s->read_hash != NULL => mac_size != -1 */
1284 unsigned char *mac = NULL;
1285 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1286 mac_size = EVP_MD_CTX_size(s->read_hash);
1287 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1290 * orig_len is the length of the record before any padding was
1291 * removed. This is public information, as is the MAC in use,
1292 * therefore we can safely process the record in a different amount
1293 * of time if it's too short to possibly contain a MAC.
1295 if (rr->orig_len < mac_size ||
1296 /* CBC records must have a padding length byte too. */
1297 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1298 rr->orig_len < mac_size + 1)) {
1299 al = SSL_AD_DECODE_ERROR;
1300 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1304 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1306 * We update the length so that the TLS header bytes can be
1307 * constructed correctly but we need to extract the MAC in
1308 * constant time from within the record, without leaking the
1309 * contents of the padding bytes.
1312 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1313 rr->length -= mac_size;
1316 * In this case there's no padding, so |rec->orig_len| equals
1317 * |rec->length| and we checked that there's enough bytes for
1320 rr->length -= mac_size;
1321 mac = &rr->data[rr->length];
1324 i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
1325 if (i < 0 || mac == NULL
1326 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
1328 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1333 /* decryption failed, silently discard message */
1335 RECORD_LAYER_reset_packet_length(&s->rlayer);
1339 /* r->length is now just compressed */
1340 if (s->expand != NULL) {
1341 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1342 al = SSL_AD_RECORD_OVERFLOW;
1343 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1344 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1347 if (!ssl3_do_uncompress(s)) {
1348 al = SSL_AD_DECOMPRESSION_FAILURE;
1349 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1354 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1355 al = SSL_AD_RECORD_OVERFLOW;
1356 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1362 * So at this point the following is true
1363 * ssl->s3->rrec.type is the type of record
1364 * ssl->s3->rrec.length == number of bytes in record
1365 * ssl->s3->rrec.off == offset to first valid byte
1366 * ssl->s3->rrec.data == where to take bytes from, increment
1370 /* we have pulled in a full packet so zero things */
1371 RECORD_LAYER_reset_packet_length(&s->rlayer);
1375 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1382 * retrieve a buffered record that belongs to the current epoch, ie,
1385 #define dtls1_get_processed_record(s) \
1386 dtls1_retrieve_buffered_record((s), \
1387 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1390 * Call this to get a new input record.
1391 * It will return <= 0 if more data is needed, normally due to an error
1392 * or non-blocking IO.
1393 * When it finishes, one packet has been decoded and can be found in
1394 * ssl->s3->rrec.type - is the type of record
1395 * ssl->s3->rrec.data, - data
1396 * ssl->s3->rrec.length, - number of bytes
1398 /* used only by dtls1_read_bytes */
1399 int dtls1_get_record(SSL *s)
1401 int ssl_major, ssl_minor;
1404 unsigned char *p = NULL;
1405 unsigned short version;
1406 DTLS1_BITMAP *bitmap;
1407 unsigned int is_next_epoch;
1409 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1412 * The epoch may have changed. If so, process all the pending records.
1413 * This is a non-blocking operation.
1415 if (dtls1_process_buffered_records(s) < 0)
1418 /* if we're renegotiating, then there may be buffered records */
1419 if (dtls1_get_processed_record(s))
1422 /* get something from the wire */
1424 /* check if we have the header */
1425 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1426 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1427 n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1428 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0);
1429 /* read timeout is handled by dtls1_read_bytes */
1431 return (n); /* error or non-blocking */
1433 /* this packet contained a partial record, dump it */
1434 if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {
1435 RECORD_LAYER_reset_packet_length(&s->rlayer);
1439 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1441 p = RECORD_LAYER_get_packet(&s->rlayer);
1443 if (s->msg_callback)
1444 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1445 s, s->msg_callback_arg);
1447 /* Pull apart the header into the DTLS1_RECORD */
1451 version = (ssl_major << 8) | ssl_minor;
1453 /* sequence number is 64 bits, with top 2 bytes = epoch */
1456 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1461 /* Lets check version */
1462 if (!s->first_packet) {
1463 if (version != s->version) {
1464 /* unexpected version, silently discard */
1466 RECORD_LAYER_reset_packet_length(&s->rlayer);
1471 if ((version & 0xff00) != (s->version & 0xff00)) {
1472 /* wrong version, silently discard record */
1474 RECORD_LAYER_reset_packet_length(&s->rlayer);
1478 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1479 /* record too long, silently discard it */
1481 RECORD_LAYER_reset_packet_length(&s->rlayer);
1485 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1488 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1491 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1492 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1494 n = ssl3_read_n(s, i, i, 1);
1495 /* this packet contained a partial record, dump it */
1498 RECORD_LAYER_reset_packet_length(&s->rlayer);
1503 * now n == rr->length, and s->packet_length ==
1504 * DTLS1_RT_HEADER_LENGTH + rr->length
1507 /* set state for later operations */
1508 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1510 /* match epochs. NULL means the packet is dropped on the floor */
1511 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1512 if (bitmap == NULL) {
1514 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1515 goto again; /* get another record */
1517 #ifndef OPENSSL_NO_SCTP
1518 /* Only do replay check if no SCTP bio */
1519 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1521 /* Check whether this is a repeat, or aged record. */
1522 if (!dtls1_record_replay_check(s, bitmap)) {
1524 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1525 goto again; /* get another record */
1527 #ifndef OPENSSL_NO_SCTP
1531 /* just read a 0 length packet */
1532 if (rr->length == 0)
1536 * If this record is from the next epoch (either HM or ALERT), and a
1537 * handshake is currently in progress, buffer it since it cannot be
1538 * processed at this time.
1540 if (is_next_epoch) {
1541 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1542 if (dtls1_buffer_record
1543 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1546 /* Mark receipt of record. */
1547 dtls1_record_bitmap_update(s, bitmap);
1550 RECORD_LAYER_reset_packet_length(&s->rlayer);
1554 if (!dtls1_process_record(s)) {
1556 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1557 goto again; /* get another record */
1559 dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */