xref: /freebsd-src/crypto/openssl/ssl/record/ssl3_record.c (revision c085ca5245797ae17fc69353bbdf7584acb2feaa)
1e71b7053SJung-uk Kim /*
2b077aed3SPierre Pronchery  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
8e71b7053SJung-uk Kim  */
9e71b7053SJung-uk Kim 
1017f01e99SJung-uk Kim #include "../ssl_local.h"
11b077aed3SPierre Pronchery #include <openssl/trace.h>
12e71b7053SJung-uk Kim #include <openssl/rand.h>
13b077aed3SPierre Pronchery #include <openssl/core_names.h>
1417f01e99SJung-uk Kim #include "record_local.h"
15e71b7053SJung-uk Kim #include "internal/cryptlib.h"
16e71b7053SJung-uk Kim 
17e71b7053SJung-uk Kim static const unsigned char ssl3_pad_1[48] = {
18e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
19e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
20e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
21e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
22e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
23e71b7053SJung-uk Kim     0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
24e71b7053SJung-uk Kim };
25e71b7053SJung-uk Kim 
26e71b7053SJung-uk Kim static const unsigned char ssl3_pad_2[48] = {
27e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
28e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
29e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
30e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
31e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
32e71b7053SJung-uk Kim     0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
33e71b7053SJung-uk Kim };
34e71b7053SJung-uk Kim 
35e71b7053SJung-uk Kim /*
36e71b7053SJung-uk Kim  * Clear the contents of an SSL3_RECORD but retain any memory allocated
37e71b7053SJung-uk Kim  */
SSL3_RECORD_clear(SSL3_RECORD * r,size_t num_recs)38e71b7053SJung-uk Kim void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
39e71b7053SJung-uk Kim {
40e71b7053SJung-uk Kim     unsigned char *comp;
41e71b7053SJung-uk Kim     size_t i;
42e71b7053SJung-uk Kim 
43e71b7053SJung-uk Kim     for (i = 0; i < num_recs; i++) {
44e71b7053SJung-uk Kim         comp = r[i].comp;
45e71b7053SJung-uk Kim 
46e71b7053SJung-uk Kim         memset(&r[i], 0, sizeof(*r));
47e71b7053SJung-uk Kim         r[i].comp = comp;
48e71b7053SJung-uk Kim     }
49e71b7053SJung-uk Kim }
50e71b7053SJung-uk Kim 
SSL3_RECORD_release(SSL3_RECORD * r,size_t num_recs)51e71b7053SJung-uk Kim void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
52e71b7053SJung-uk Kim {
53e71b7053SJung-uk Kim     size_t i;
54e71b7053SJung-uk Kim 
55e71b7053SJung-uk Kim     for (i = 0; i < num_recs; i++) {
56e71b7053SJung-uk Kim         OPENSSL_free(r[i].comp);
57e71b7053SJung-uk Kim         r[i].comp = NULL;
58e71b7053SJung-uk Kim     }
59e71b7053SJung-uk Kim }
60e71b7053SJung-uk Kim 
SSL3_RECORD_set_seq_num(SSL3_RECORD * r,const unsigned char * seq_num)61e71b7053SJung-uk Kim void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
62e71b7053SJung-uk Kim {
63e71b7053SJung-uk Kim     memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
64e71b7053SJung-uk Kim }
65e71b7053SJung-uk Kim 
66e71b7053SJung-uk Kim /*
67e71b7053SJung-uk Kim  * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
68e71b7053SJung-uk Kim  * for us in the buffer.
69e71b7053SJung-uk Kim  */
ssl3_record_app_data_waiting(SSL * s)70e71b7053SJung-uk Kim static int ssl3_record_app_data_waiting(SSL *s)
71e71b7053SJung-uk Kim {
72e71b7053SJung-uk Kim     SSL3_BUFFER *rbuf;
73e71b7053SJung-uk Kim     size_t left, len;
74e71b7053SJung-uk Kim     unsigned char *p;
75e71b7053SJung-uk Kim 
76e71b7053SJung-uk Kim     rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
77e71b7053SJung-uk Kim 
78e71b7053SJung-uk Kim     p = SSL3_BUFFER_get_buf(rbuf);
79e71b7053SJung-uk Kim     if (p == NULL)
80e71b7053SJung-uk Kim         return 0;
81e71b7053SJung-uk Kim 
82e71b7053SJung-uk Kim     left = SSL3_BUFFER_get_left(rbuf);
83e71b7053SJung-uk Kim 
84e71b7053SJung-uk Kim     if (left < SSL3_RT_HEADER_LENGTH)
85e71b7053SJung-uk Kim         return 0;
86e71b7053SJung-uk Kim 
87e71b7053SJung-uk Kim     p += SSL3_BUFFER_get_offset(rbuf);
88e71b7053SJung-uk Kim 
89e71b7053SJung-uk Kim     /*
90e71b7053SJung-uk Kim      * We only check the type and record length, we will sanity check version
91e71b7053SJung-uk Kim      * etc later
92e71b7053SJung-uk Kim      */
93e71b7053SJung-uk Kim     if (*p != SSL3_RT_APPLICATION_DATA)
94e71b7053SJung-uk Kim         return 0;
95e71b7053SJung-uk Kim 
96e71b7053SJung-uk Kim     p += 3;
97e71b7053SJung-uk Kim     n2s(p, len);
98e71b7053SJung-uk Kim 
99e71b7053SJung-uk Kim     if (left < SSL3_RT_HEADER_LENGTH + len)
100e71b7053SJung-uk Kim         return 0;
101e71b7053SJung-uk Kim 
102e71b7053SJung-uk Kim     return 1;
103e71b7053SJung-uk Kim }
104e71b7053SJung-uk Kim 
early_data_count_ok(SSL * s,size_t length,size_t overhead,int send)105e71b7053SJung-uk Kim int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send)
106e71b7053SJung-uk Kim {
107e71b7053SJung-uk Kim     uint32_t max_early_data;
108e71b7053SJung-uk Kim     SSL_SESSION *sess = s->session;
109e71b7053SJung-uk Kim 
110e71b7053SJung-uk Kim     /*
111e71b7053SJung-uk Kim      * If we are a client then we always use the max_early_data from the
112e71b7053SJung-uk Kim      * session/psksession. Otherwise we go with the lowest out of the max early
113e71b7053SJung-uk Kim      * data set in the session and the configured max_early_data.
114e71b7053SJung-uk Kim      */
115e71b7053SJung-uk Kim     if (!s->server && sess->ext.max_early_data == 0) {
116e71b7053SJung-uk Kim         if (!ossl_assert(s->psksession != NULL
117e71b7053SJung-uk Kim                          && s->psksession->ext.max_early_data > 0)) {
118b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
119e71b7053SJung-uk Kim             return 0;
120e71b7053SJung-uk Kim         }
121e71b7053SJung-uk Kim         sess = s->psksession;
122e71b7053SJung-uk Kim     }
123e71b7053SJung-uk Kim 
124e71b7053SJung-uk Kim     if (!s->server)
125e71b7053SJung-uk Kim         max_early_data = sess->ext.max_early_data;
126e71b7053SJung-uk Kim     else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
127e71b7053SJung-uk Kim         max_early_data = s->recv_max_early_data;
128e71b7053SJung-uk Kim     else
129e71b7053SJung-uk Kim         max_early_data = s->recv_max_early_data < sess->ext.max_early_data
130e71b7053SJung-uk Kim                          ? s->recv_max_early_data : sess->ext.max_early_data;
131e71b7053SJung-uk Kim 
132e71b7053SJung-uk Kim     if (max_early_data == 0) {
133e71b7053SJung-uk Kim         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
134b077aed3SPierre Pronchery                  SSL_R_TOO_MUCH_EARLY_DATA);
135e71b7053SJung-uk Kim         return 0;
136e71b7053SJung-uk Kim     }
137e71b7053SJung-uk Kim 
138e71b7053SJung-uk Kim     /* If we are dealing with ciphertext we need to allow for the overhead */
139e71b7053SJung-uk Kim     max_early_data += overhead;
140e71b7053SJung-uk Kim 
141e71b7053SJung-uk Kim     if (s->early_data_count + length > max_early_data) {
142e71b7053SJung-uk Kim         SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
143b077aed3SPierre Pronchery                  SSL_R_TOO_MUCH_EARLY_DATA);
144e71b7053SJung-uk Kim         return 0;
145e71b7053SJung-uk Kim     }
146e71b7053SJung-uk Kim     s->early_data_count += length;
147e71b7053SJung-uk Kim 
148e71b7053SJung-uk Kim     return 1;
149e71b7053SJung-uk Kim }
150e71b7053SJung-uk Kim 
151e71b7053SJung-uk Kim /*
152e71b7053SJung-uk Kim  * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
153e71b7053SJung-uk Kim  * will be processed per call to ssl3_get_record. Without this limit an
154e71b7053SJung-uk Kim  * attacker could send empty records at a faster rate than we can process and
155e71b7053SJung-uk Kim  * cause ssl3_get_record to loop forever.
156e71b7053SJung-uk Kim  */
157e71b7053SJung-uk Kim #define MAX_EMPTY_RECORDS 32
158e71b7053SJung-uk Kim 
159e71b7053SJung-uk Kim #define SSL2_RT_HEADER_LENGTH   2
160e71b7053SJung-uk Kim /*-
161e71b7053SJung-uk Kim  * Call this to get new input records.
162e71b7053SJung-uk Kim  * It will return <= 0 if more data is needed, normally due to an error
163e71b7053SJung-uk Kim  * or non-blocking IO.
164e71b7053SJung-uk Kim  * When it finishes, |numrpipes| records have been decoded. For each record 'i':
165e71b7053SJung-uk Kim  * rr[i].type    - is the type of record
166e71b7053SJung-uk Kim  * rr[i].data,   - data
167e71b7053SJung-uk Kim  * rr[i].length, - number of bytes
168e71b7053SJung-uk Kim  * Multiple records will only be returned if the record types are all
169e71b7053SJung-uk Kim  * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
170e71b7053SJung-uk Kim  * |max_pipelines|
171e71b7053SJung-uk Kim  */
172e71b7053SJung-uk Kim /* used only by ssl3_read_bytes */
ssl3_get_record(SSL * s)173e71b7053SJung-uk Kim int ssl3_get_record(SSL *s)
174e71b7053SJung-uk Kim {
175e71b7053SJung-uk Kim     int enc_err, rret;
176e71b7053SJung-uk Kim     int i;
177e71b7053SJung-uk Kim     size_t more, n;
178e71b7053SJung-uk Kim     SSL3_RECORD *rr, *thisrr;
179e71b7053SJung-uk Kim     SSL3_BUFFER *rbuf;
180e71b7053SJung-uk Kim     SSL_SESSION *sess;
181e71b7053SJung-uk Kim     unsigned char *p;
182e71b7053SJung-uk Kim     unsigned char md[EVP_MAX_MD_SIZE];
183e71b7053SJung-uk Kim     unsigned int version;
184b077aed3SPierre Pronchery     size_t mac_size = 0;
185e71b7053SJung-uk Kim     int imac_size;
186e71b7053SJung-uk Kim     size_t num_recs = 0, max_recs, j;
187e71b7053SJung-uk Kim     PACKET pkt, sslv2pkt;
1880fc28f22SJohn Baldwin     int using_ktls;
189b077aed3SPierre Pronchery     SSL_MAC_BUF *macbufs = NULL;
190b077aed3SPierre Pronchery     int ret = -1;
191e71b7053SJung-uk Kim 
192e71b7053SJung-uk Kim     rr = RECORD_LAYER_get_rrec(&s->rlayer);
193e71b7053SJung-uk Kim     rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
194e71b7053SJung-uk Kim     max_recs = s->max_pipelines;
195e71b7053SJung-uk Kim     if (max_recs == 0)
196e71b7053SJung-uk Kim         max_recs = 1;
197e71b7053SJung-uk Kim     sess = s->session;
198e71b7053SJung-uk Kim 
1990fc28f22SJohn Baldwin     /*
2000fc28f22SJohn Baldwin      * KTLS reads full records. If there is any data left,
2010fc28f22SJohn Baldwin      * then it is from before enabling ktls.
2020fc28f22SJohn Baldwin      */
2030fc28f22SJohn Baldwin     using_ktls = BIO_get_ktls_recv(s->rbio) && SSL3_BUFFER_get_left(rbuf) == 0;
2040fc28f22SJohn Baldwin 
205e71b7053SJung-uk Kim     do {
206e71b7053SJung-uk Kim         thisrr = &rr[num_recs];
207e71b7053SJung-uk Kim 
208e71b7053SJung-uk Kim         /* check if we have the header */
209e71b7053SJung-uk Kim         if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
210e71b7053SJung-uk Kim             (RECORD_LAYER_get_packet_length(&s->rlayer)
211e71b7053SJung-uk Kim              < SSL3_RT_HEADER_LENGTH)) {
212e71b7053SJung-uk Kim             size_t sslv2len;
213e71b7053SJung-uk Kim             unsigned int type;
214e71b7053SJung-uk Kim 
215e71b7053SJung-uk Kim             rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
216e71b7053SJung-uk Kim                                SSL3_BUFFER_get_len(rbuf), 0,
217e71b7053SJung-uk Kim                                num_recs == 0 ? 1 : 0, &n);
218aa906e2aSJohn Baldwin             if (rret <= 0) {
219aa906e2aSJohn Baldwin #ifndef OPENSSL_NO_KTLS
220aa906e2aSJohn Baldwin                 if (!BIO_get_ktls_recv(s->rbio) || rret == 0)
221e71b7053SJung-uk Kim                     return rret;     /* error or non-blocking */
222aa906e2aSJohn Baldwin                 switch (errno) {
223aa906e2aSJohn Baldwin                 case EBADMSG:
224aa906e2aSJohn Baldwin                     SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
225aa906e2aSJohn Baldwin                              SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
226aa906e2aSJohn Baldwin                     break;
227aa906e2aSJohn Baldwin                 case EMSGSIZE:
228aa906e2aSJohn Baldwin                     SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
229aa906e2aSJohn Baldwin                              SSL_R_PACKET_LENGTH_TOO_LONG);
230aa906e2aSJohn Baldwin                     break;
231aa906e2aSJohn Baldwin                 case EINVAL:
232aa906e2aSJohn Baldwin                     SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
233aa906e2aSJohn Baldwin                              SSL_R_WRONG_VERSION_NUMBER);
234aa906e2aSJohn Baldwin                     break;
235aa906e2aSJohn Baldwin                 default:
236aa906e2aSJohn Baldwin                     break;
237aa906e2aSJohn Baldwin                 }
238aa906e2aSJohn Baldwin #endif
239aa906e2aSJohn Baldwin                 return rret;
240aa906e2aSJohn Baldwin             }
241e71b7053SJung-uk Kim             RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
242e71b7053SJung-uk Kim 
243e71b7053SJung-uk Kim             p = RECORD_LAYER_get_packet(&s->rlayer);
244e71b7053SJung-uk Kim             if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
245e71b7053SJung-uk Kim                                  RECORD_LAYER_get_packet_length(&s->rlayer))) {
246b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
247e71b7053SJung-uk Kim                 return -1;
248e71b7053SJung-uk Kim             }
249e71b7053SJung-uk Kim             sslv2pkt = pkt;
250e71b7053SJung-uk Kim             if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
251e71b7053SJung-uk Kim                     || !PACKET_get_1(&sslv2pkt, &type)) {
252b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
253e71b7053SJung-uk Kim                 return -1;
254e71b7053SJung-uk Kim             }
255e71b7053SJung-uk Kim             /*
256e71b7053SJung-uk Kim              * The first record received by the server may be a V2ClientHello.
257e71b7053SJung-uk Kim              */
258e71b7053SJung-uk Kim             if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
259e71b7053SJung-uk Kim                     && (sslv2len & 0x8000) != 0
260e71b7053SJung-uk Kim                     && (type == SSL2_MT_CLIENT_HELLO)) {
261e71b7053SJung-uk Kim                 /*
262e71b7053SJung-uk Kim                  *  SSLv2 style record
263e71b7053SJung-uk Kim                  *
264e71b7053SJung-uk Kim                  * |num_recs| here will actually always be 0 because
265e71b7053SJung-uk Kim                  * |num_recs > 0| only ever occurs when we are processing
266e71b7053SJung-uk Kim                  * multiple app data records - which we know isn't the case here
267e71b7053SJung-uk Kim                  * because it is an SSLv2ClientHello. We keep it using
268e71b7053SJung-uk Kim                  * |num_recs| for the sake of consistency
269e71b7053SJung-uk Kim                  */
270e71b7053SJung-uk Kim                 thisrr->type = SSL3_RT_HANDSHAKE;
271e71b7053SJung-uk Kim                 thisrr->rec_version = SSL2_VERSION;
272e71b7053SJung-uk Kim 
273e71b7053SJung-uk Kim                 thisrr->length = sslv2len & 0x7fff;
274e71b7053SJung-uk Kim 
275e71b7053SJung-uk Kim                 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
276e71b7053SJung-uk Kim                     - SSL2_RT_HEADER_LENGTH) {
277b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
278e71b7053SJung-uk Kim                              SSL_R_PACKET_LENGTH_TOO_LONG);
279e71b7053SJung-uk Kim                     return -1;
280e71b7053SJung-uk Kim                 }
281e71b7053SJung-uk Kim 
282e71b7053SJung-uk Kim                 if (thisrr->length < MIN_SSL2_RECORD_LEN) {
283b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
284e71b7053SJung-uk Kim                     return -1;
285e71b7053SJung-uk Kim                 }
286e71b7053SJung-uk Kim             } else {
287e71b7053SJung-uk Kim                 /* SSLv3+ style record */
288e71b7053SJung-uk Kim 
289e71b7053SJung-uk Kim                 /* Pull apart the header into the SSL3_RECORD */
290e71b7053SJung-uk Kim                 if (!PACKET_get_1(&pkt, &type)
291e71b7053SJung-uk Kim                         || !PACKET_get_net_2(&pkt, &version)
292e71b7053SJung-uk Kim                         || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
293b077aed3SPierre Pronchery                     if (s->msg_callback)
294b077aed3SPierre Pronchery                         s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
295b077aed3SPierre Pronchery                                         s->msg_callback_arg);
296b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
297e71b7053SJung-uk Kim                     return -1;
298e71b7053SJung-uk Kim                 }
299e71b7053SJung-uk Kim                 thisrr->type = type;
300e71b7053SJung-uk Kim                 thisrr->rec_version = version;
301e71b7053SJung-uk Kim 
302b077aed3SPierre Pronchery                 if (s->msg_callback)
303b077aed3SPierre Pronchery                     s->msg_callback(0, version, SSL3_RT_HEADER, p, 5, s,
304b077aed3SPierre Pronchery                                     s->msg_callback_arg);
305b077aed3SPierre Pronchery 
306e71b7053SJung-uk Kim                 /*
307e71b7053SJung-uk Kim                  * Lets check version. In TLSv1.3 we only check this field
308e71b7053SJung-uk Kim                  * when encryption is occurring (see later check). For the
309e71b7053SJung-uk Kim                  * ServerHello after an HRR we haven't actually selected TLSv1.3
310e71b7053SJung-uk Kim                  * yet, but we still treat it as TLSv1.3, so we must check for
311e71b7053SJung-uk Kim                  * that explicitly
312e71b7053SJung-uk Kim                  */
313e71b7053SJung-uk Kim                 if (!s->first_packet && !SSL_IS_TLS13(s)
314e71b7053SJung-uk Kim                         && s->hello_retry_request != SSL_HRR_PENDING
315e71b7053SJung-uk Kim                         && version != (unsigned int)s->version) {
316e71b7053SJung-uk Kim                     if ((s->version & 0xFF00) == (version & 0xFF00)
317e71b7053SJung-uk Kim                         && !s->enc_write_ctx && !s->write_hash) {
318e71b7053SJung-uk Kim                         if (thisrr->type == SSL3_RT_ALERT) {
319e71b7053SJung-uk Kim                             /*
320e71b7053SJung-uk Kim                              * The record is using an incorrect version number,
321e71b7053SJung-uk Kim                              * but what we've got appears to be an alert. We
322e71b7053SJung-uk Kim                              * haven't read the body yet to check whether its a
323e71b7053SJung-uk Kim                              * fatal or not - but chances are it is. We probably
324e71b7053SJung-uk Kim                              * shouldn't send a fatal alert back. We'll just
325e71b7053SJung-uk Kim                              * end.
326e71b7053SJung-uk Kim                              */
327b077aed3SPierre Pronchery                             SSLfatal(s, SSL_AD_NO_ALERT,
328e71b7053SJung-uk Kim                                      SSL_R_WRONG_VERSION_NUMBER);
329e71b7053SJung-uk Kim                             return -1;
330e71b7053SJung-uk Kim                         }
331e71b7053SJung-uk Kim                         /*
332e71b7053SJung-uk Kim                          * Send back error using their minor version number :-)
333e71b7053SJung-uk Kim                          */
334e71b7053SJung-uk Kim                         s->version = (unsigned short)version;
335e71b7053SJung-uk Kim                     }
336b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
337e71b7053SJung-uk Kim                              SSL_R_WRONG_VERSION_NUMBER);
338e71b7053SJung-uk Kim                     return -1;
339e71b7053SJung-uk Kim                 }
340e71b7053SJung-uk Kim 
341e71b7053SJung-uk Kim                 if ((version >> 8) != SSL3_VERSION_MAJOR) {
342e71b7053SJung-uk Kim                     if (RECORD_LAYER_is_first_record(&s->rlayer)) {
343e71b7053SJung-uk Kim                         /* Go back to start of packet, look at the five bytes
344e71b7053SJung-uk Kim                          * that we have. */
345e71b7053SJung-uk Kim                         p = RECORD_LAYER_get_packet(&s->rlayer);
346e71b7053SJung-uk Kim                         if (strncmp((char *)p, "GET ", 4) == 0 ||
347e71b7053SJung-uk Kim                             strncmp((char *)p, "POST ", 5) == 0 ||
348e71b7053SJung-uk Kim                             strncmp((char *)p, "HEAD ", 5) == 0 ||
349e71b7053SJung-uk Kim                             strncmp((char *)p, "PUT ", 4) == 0) {
350b077aed3SPierre Pronchery                             SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_HTTP_REQUEST);
351e71b7053SJung-uk Kim                             return -1;
352e71b7053SJung-uk Kim                         } else if (strncmp((char *)p, "CONNE", 5) == 0) {
353b077aed3SPierre Pronchery                             SSLfatal(s, SSL_AD_NO_ALERT,
354e71b7053SJung-uk Kim                                      SSL_R_HTTPS_PROXY_REQUEST);
355e71b7053SJung-uk Kim                             return -1;
356e71b7053SJung-uk Kim                         }
357e71b7053SJung-uk Kim 
358e71b7053SJung-uk Kim                         /* Doesn't look like TLS - don't send an alert */
359b077aed3SPierre Pronchery                         SSLfatal(s, SSL_AD_NO_ALERT,
360e71b7053SJung-uk Kim                                  SSL_R_WRONG_VERSION_NUMBER);
361e71b7053SJung-uk Kim                         return -1;
362e71b7053SJung-uk Kim                     } else {
363e71b7053SJung-uk Kim                         SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
364e71b7053SJung-uk Kim                                  SSL_R_WRONG_VERSION_NUMBER);
365e71b7053SJung-uk Kim                         return -1;
366e71b7053SJung-uk Kim                     }
367e71b7053SJung-uk Kim                 }
368e71b7053SJung-uk Kim 
369*c085ca52SDaiki Ueno                 if (SSL_IS_TLS13(s)
370*c085ca52SDaiki Ueno                         && s->enc_read_ctx != NULL
371*c085ca52SDaiki Ueno                         && !using_ktls) {
372e71b7053SJung-uk Kim                     if (thisrr->type != SSL3_RT_APPLICATION_DATA
373e71b7053SJung-uk Kim                             && (thisrr->type != SSL3_RT_CHANGE_CIPHER_SPEC
374e71b7053SJung-uk Kim                                 || !SSL_IS_FIRST_HANDSHAKE(s))
375e71b7053SJung-uk Kim                             && (thisrr->type != SSL3_RT_ALERT
376e71b7053SJung-uk Kim                                 || s->statem.enc_read_state
377e71b7053SJung-uk Kim                                    != ENC_READ_STATE_ALLOW_PLAIN_ALERTS)) {
378e71b7053SJung-uk Kim                         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
379b077aed3SPierre Pronchery                                  SSL_R_BAD_RECORD_TYPE);
380e71b7053SJung-uk Kim                         return -1;
381e71b7053SJung-uk Kim                     }
382e71b7053SJung-uk Kim                     if (thisrr->rec_version != TLS1_2_VERSION) {
383b077aed3SPierre Pronchery                         SSLfatal(s, SSL_AD_DECODE_ERROR,
384e71b7053SJung-uk Kim                                  SSL_R_WRONG_VERSION_NUMBER);
385e71b7053SJung-uk Kim                         return -1;
386e71b7053SJung-uk Kim                     }
387e71b7053SJung-uk Kim                 }
388e71b7053SJung-uk Kim 
389e71b7053SJung-uk Kim                 if (thisrr->length >
390e71b7053SJung-uk Kim                     SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
391b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
392e71b7053SJung-uk Kim                              SSL_R_PACKET_LENGTH_TOO_LONG);
393e71b7053SJung-uk Kim                     return -1;
394e71b7053SJung-uk Kim                 }
395e71b7053SJung-uk Kim             }
396e71b7053SJung-uk Kim 
397e71b7053SJung-uk Kim             /* now s->rlayer.rstate == SSL_ST_READ_BODY */
398e71b7053SJung-uk Kim         }
399e71b7053SJung-uk Kim 
400e71b7053SJung-uk Kim         if (SSL_IS_TLS13(s)) {
401*c085ca52SDaiki Ueno             size_t len = SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH;
402*c085ca52SDaiki Ueno 
403*c085ca52SDaiki Ueno             /* KTLS strips the inner record type. */
404*c085ca52SDaiki Ueno             if (using_ktls)
405*c085ca52SDaiki Ueno                 len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
406*c085ca52SDaiki Ueno 
407*c085ca52SDaiki Ueno             if (thisrr->length > len) {
408b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
409e71b7053SJung-uk Kim                          SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
410e71b7053SJung-uk Kim                 return -1;
411e71b7053SJung-uk Kim             }
412e71b7053SJung-uk Kim         } else {
413e71b7053SJung-uk Kim             size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
414e71b7053SJung-uk Kim 
415e71b7053SJung-uk Kim #ifndef OPENSSL_NO_COMP
416e71b7053SJung-uk Kim             /*
417e71b7053SJung-uk Kim              * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
418e71b7053SJung-uk Kim              * does not include the compression overhead anyway.
419e71b7053SJung-uk Kim              */
420e71b7053SJung-uk Kim             if (s->expand == NULL)
421e71b7053SJung-uk Kim                 len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
422e71b7053SJung-uk Kim #endif
423e71b7053SJung-uk Kim 
424f6e5fcdcSJohn Baldwin             /* KTLS may use all of the buffer */
4250fc28f22SJohn Baldwin             if (using_ktls)
426f6e5fcdcSJohn Baldwin                 len = SSL3_BUFFER_get_left(rbuf);
427f6e5fcdcSJohn Baldwin 
428f6e5fcdcSJohn Baldwin             if (thisrr->length > len) {
429b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
430e71b7053SJung-uk Kim                          SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
431e71b7053SJung-uk Kim                 return -1;
432e71b7053SJung-uk Kim             }
433e71b7053SJung-uk Kim         }
434e71b7053SJung-uk Kim 
435e71b7053SJung-uk Kim         /*
436e71b7053SJung-uk Kim          * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
437e71b7053SJung-uk Kim          * Calculate how much more data we need to read for the rest of the
438e71b7053SJung-uk Kim          * record
439e71b7053SJung-uk Kim          */
440e71b7053SJung-uk Kim         if (thisrr->rec_version == SSL2_VERSION) {
441e71b7053SJung-uk Kim             more = thisrr->length + SSL2_RT_HEADER_LENGTH
442e71b7053SJung-uk Kim                 - SSL3_RT_HEADER_LENGTH;
443e71b7053SJung-uk Kim         } else {
444e71b7053SJung-uk Kim             more = thisrr->length;
445e71b7053SJung-uk Kim         }
446aa906e2aSJohn Baldwin 
447e71b7053SJung-uk Kim         if (more > 0) {
4489a3ae0cdSJung-uk Kim             /* now s->rlayer.packet_length == SSL3_RT_HEADER_LENGTH */
449e71b7053SJung-uk Kim 
450e71b7053SJung-uk Kim             rret = ssl3_read_n(s, more, more, 1, 0, &n);
451e71b7053SJung-uk Kim             if (rret <= 0)
452e71b7053SJung-uk Kim                 return rret;     /* error or non-blocking io */
453e71b7053SJung-uk Kim         }
454e71b7053SJung-uk Kim 
455e71b7053SJung-uk Kim         /* set state for later operations */
456e71b7053SJung-uk Kim         RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
457e71b7053SJung-uk Kim 
458e71b7053SJung-uk Kim         /*
4599a3ae0cdSJung-uk Kim          * At this point, s->rlayer.packet_length == SSL3_RT_HEADER_LENGTH
4609a3ae0cdSJung-uk Kim          * + thisrr->length, or s->rlayer.packet_length == SSL2_RT_HEADER_LENGTH
4619a3ae0cdSJung-uk Kim          * + thisrr->length and we have that many bytes in s->rlayer.packet
462e71b7053SJung-uk Kim          */
463e71b7053SJung-uk Kim         if (thisrr->rec_version == SSL2_VERSION) {
464e71b7053SJung-uk Kim             thisrr->input =
465e71b7053SJung-uk Kim                 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
466e71b7053SJung-uk Kim         } else {
467e71b7053SJung-uk Kim             thisrr->input =
468e71b7053SJung-uk Kim                 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
469e71b7053SJung-uk Kim         }
470e71b7053SJung-uk Kim 
471e71b7053SJung-uk Kim         /*
4729a3ae0cdSJung-uk Kim          * ok, we can now read from 's->rlayer.packet' data into 'thisrr'.
4739a3ae0cdSJung-uk Kim          * thisrr->input points at thisrr->length bytes, which need to be copied
4749a3ae0cdSJung-uk Kim          * into thisrr->data by either the decryption or by the decompression.
4759a3ae0cdSJung-uk Kim          * When the data is 'copied' into the thisrr->data buffer,
4769a3ae0cdSJung-uk Kim          * thisrr->input will be updated to point at the new buffer
477e71b7053SJung-uk Kim          */
478e71b7053SJung-uk Kim 
479e71b7053SJung-uk Kim         /*
480e71b7053SJung-uk Kim          * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
481e71b7053SJung-uk Kim          * thisrr->length bytes of encrypted compressed stuff.
482e71b7053SJung-uk Kim          */
483e71b7053SJung-uk Kim 
484e71b7053SJung-uk Kim         /* decrypt in place in 'thisrr->input' */
485e71b7053SJung-uk Kim         thisrr->data = thisrr->input;
486e71b7053SJung-uk Kim         thisrr->orig_len = thisrr->length;
487e71b7053SJung-uk Kim 
488e71b7053SJung-uk Kim         /* Mark this record as not read by upper layers yet */
489e71b7053SJung-uk Kim         thisrr->read = 0;
490e71b7053SJung-uk Kim 
491e71b7053SJung-uk Kim         num_recs++;
492e71b7053SJung-uk Kim 
493e71b7053SJung-uk Kim         /* we have pulled in a full packet so zero things */
494e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer);
495e71b7053SJung-uk Kim         RECORD_LAYER_clear_first_record(&s->rlayer);
496e71b7053SJung-uk Kim     } while (num_recs < max_recs
497e71b7053SJung-uk Kim              && thisrr->type == SSL3_RT_APPLICATION_DATA
498e71b7053SJung-uk Kim              && SSL_USE_EXPLICIT_IV(s)
499e71b7053SJung-uk Kim              && s->enc_read_ctx != NULL
500b077aed3SPierre Pronchery              && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx))
501b077aed3SPierre Pronchery                  & EVP_CIPH_FLAG_PIPELINE) != 0
502e71b7053SJung-uk Kim              && ssl3_record_app_data_waiting(s));
503e71b7053SJung-uk Kim 
504e71b7053SJung-uk Kim     if (num_recs == 1
505e71b7053SJung-uk Kim             && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
506e71b7053SJung-uk Kim             && (SSL_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
507e71b7053SJung-uk Kim             && SSL_IS_FIRST_HANDSHAKE(s)) {
508e71b7053SJung-uk Kim         /*
509e71b7053SJung-uk Kim          * CCS messages must be exactly 1 byte long, containing the value 0x01
510e71b7053SJung-uk Kim          */
511e71b7053SJung-uk Kim         if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
512b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
513e71b7053SJung-uk Kim                      SSL_R_INVALID_CCS_MESSAGE);
514e71b7053SJung-uk Kim             return -1;
515e71b7053SJung-uk Kim         }
516e71b7053SJung-uk Kim         /*
517e71b7053SJung-uk Kim          * CCS messages are ignored in TLSv1.3. We treat it like an empty
518e71b7053SJung-uk Kim          * handshake record
519e71b7053SJung-uk Kim          */
520e71b7053SJung-uk Kim         thisrr->type = SSL3_RT_HANDSHAKE;
521e71b7053SJung-uk Kim         RECORD_LAYER_inc_empty_record_count(&s->rlayer);
522e71b7053SJung-uk Kim         if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
523e71b7053SJung-uk Kim             > MAX_EMPTY_RECORDS) {
524b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
525e71b7053SJung-uk Kim                      SSL_R_UNEXPECTED_CCS_MESSAGE);
526e71b7053SJung-uk Kim             return -1;
527e71b7053SJung-uk Kim         }
528e71b7053SJung-uk Kim         thisrr->read = 1;
529e71b7053SJung-uk Kim         RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
530e71b7053SJung-uk Kim 
531e71b7053SJung-uk Kim         return 1;
532e71b7053SJung-uk Kim     }
533e71b7053SJung-uk Kim 
5340fc28f22SJohn Baldwin     if (using_ktls)
535aa906e2aSJohn Baldwin         goto skip_decryption;
536aa906e2aSJohn Baldwin 
537b077aed3SPierre Pronchery     if (s->read_hash != NULL) {
538b077aed3SPierre Pronchery         const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(s->read_hash);
539b077aed3SPierre Pronchery 
540b077aed3SPierre Pronchery         if (tmpmd != NULL) {
541b077aed3SPierre Pronchery             imac_size = EVP_MD_get_size(tmpmd);
542b077aed3SPierre Pronchery             if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
543b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
544b077aed3SPierre Pronchery                     return -1;
545b077aed3SPierre Pronchery             }
546b077aed3SPierre Pronchery             mac_size = (size_t)imac_size;
547b077aed3SPierre Pronchery         }
548b077aed3SPierre Pronchery     }
549b077aed3SPierre Pronchery 
550aa906e2aSJohn Baldwin     /*
551e71b7053SJung-uk Kim      * If in encrypt-then-mac mode calculate mac from encrypted record. All
552e71b7053SJung-uk Kim      * the details below are public so no timing details can leak.
553e71b7053SJung-uk Kim      */
554e71b7053SJung-uk Kim     if (SSL_READ_ETM(s) && s->read_hash) {
555e71b7053SJung-uk Kim         unsigned char *mac;
556b077aed3SPierre Pronchery 
557e71b7053SJung-uk Kim         for (j = 0; j < num_recs; j++) {
558e71b7053SJung-uk Kim             thisrr = &rr[j];
559e71b7053SJung-uk Kim 
560e71b7053SJung-uk Kim             if (thisrr->length < mac_size) {
561b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
562e71b7053SJung-uk Kim                 return -1;
563e71b7053SJung-uk Kim             }
564e71b7053SJung-uk Kim             thisrr->length -= mac_size;
565e71b7053SJung-uk Kim             mac = thisrr->data + thisrr->length;
566e71b7053SJung-uk Kim             i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
567e71b7053SJung-uk Kim             if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
568b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
569e71b7053SJung-uk Kim                          SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
570e71b7053SJung-uk Kim                 return -1;
571e71b7053SJung-uk Kim             }
572e71b7053SJung-uk Kim         }
573b077aed3SPierre Pronchery         /*
574b077aed3SPierre Pronchery          * We've handled the mac now - there is no MAC inside the encrypted
575b077aed3SPierre Pronchery          * record
576b077aed3SPierre Pronchery          */
577b077aed3SPierre Pronchery         mac_size = 0;
578e71b7053SJung-uk Kim     }
579e71b7053SJung-uk Kim 
580b077aed3SPierre Pronchery     if (mac_size > 0) {
581b077aed3SPierre Pronchery         macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
582b077aed3SPierre Pronchery         if (macbufs == NULL) {
583b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
584b077aed3SPierre Pronchery             return -1;
585b077aed3SPierre Pronchery         }
586b077aed3SPierre Pronchery     }
587e71b7053SJung-uk Kim 
588b077aed3SPierre Pronchery     ERR_set_mark();
589b077aed3SPierre Pronchery     enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0, macbufs, mac_size);
590e71b7053SJung-uk Kim 
591e71b7053SJung-uk Kim     /*-
592e71b7053SJung-uk Kim      * enc_err is:
593b077aed3SPierre Pronchery      *    0: if the record is publicly invalid, or an internal error, or AEAD
594b077aed3SPierre Pronchery      *       decryption failed, or ETM decryption failed.
595b077aed3SPierre Pronchery      *    1: Success or MTE decryption failed (MAC will be randomised)
596e71b7053SJung-uk Kim      */
597e71b7053SJung-uk Kim     if (enc_err == 0) {
598e71b7053SJung-uk Kim         if (ossl_statem_in_error(s)) {
599e71b7053SJung-uk Kim             /* SSLfatal() already got called */
600b077aed3SPierre Pronchery             ERR_clear_last_mark();
601b077aed3SPierre Pronchery             goto end;
602e71b7053SJung-uk Kim         }
603e71b7053SJung-uk Kim         if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
604e71b7053SJung-uk Kim             /*
605b077aed3SPierre Pronchery              * Valid early_data that we cannot decrypt will fail here. We treat
606b077aed3SPierre Pronchery              * it like an empty record.
607e71b7053SJung-uk Kim              */
608e71b7053SJung-uk Kim 
609b077aed3SPierre Pronchery             /*
610b077aed3SPierre Pronchery              * Remove any errors from the stack. Decryption failures are normal
611b077aed3SPierre Pronchery              * behaviour.
612b077aed3SPierre Pronchery              */
613b077aed3SPierre Pronchery             ERR_pop_to_mark();
614b077aed3SPierre Pronchery 
615e71b7053SJung-uk Kim             thisrr = &rr[0];
616e71b7053SJung-uk Kim 
617e71b7053SJung-uk Kim             if (!early_data_count_ok(s, thisrr->length,
618e71b7053SJung-uk Kim                                      EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
619e71b7053SJung-uk Kim                 /* SSLfatal() already called */
620b077aed3SPierre Pronchery                 goto end;
621e71b7053SJung-uk Kim             }
622e71b7053SJung-uk Kim 
623e71b7053SJung-uk Kim             thisrr->length = 0;
624e71b7053SJung-uk Kim             thisrr->read = 1;
625e71b7053SJung-uk Kim             RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
626e71b7053SJung-uk Kim             RECORD_LAYER_reset_read_sequence(&s->rlayer);
627b077aed3SPierre Pronchery             ret = 1;
628b077aed3SPierre Pronchery             goto end;
629e71b7053SJung-uk Kim         }
630b077aed3SPierre Pronchery         ERR_clear_last_mark();
631b077aed3SPierre Pronchery         SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
632b077aed3SPierre Pronchery                  SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
633b077aed3SPierre Pronchery         goto end;
634b077aed3SPierre Pronchery     } else {
635b077aed3SPierre Pronchery         ERR_clear_last_mark();
636e71b7053SJung-uk Kim     }
637b077aed3SPierre Pronchery     OSSL_TRACE_BEGIN(TLS) {
638b077aed3SPierre Pronchery         BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
639b077aed3SPierre Pronchery         BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
640b077aed3SPierre Pronchery     } OSSL_TRACE_END(TLS);
641e71b7053SJung-uk Kim 
642e71b7053SJung-uk Kim     /* r->length is now the compressed data plus mac */
643b077aed3SPierre Pronchery     if ((sess != NULL)
644b077aed3SPierre Pronchery             && (s->enc_read_ctx != NULL)
645b077aed3SPierre Pronchery             && (!SSL_READ_ETM(s) && EVP_MD_CTX_get0_md(s->read_hash) != NULL)) {
646e71b7053SJung-uk Kim         /* s->read_hash != NULL => mac_size != -1 */
647e71b7053SJung-uk Kim 
648e71b7053SJung-uk Kim         for (j = 0; j < num_recs; j++) {
649b077aed3SPierre Pronchery             SSL_MAC_BUF *thismb = &macbufs[j];
650e71b7053SJung-uk Kim             thisrr = &rr[j];
651e71b7053SJung-uk Kim 
652e71b7053SJung-uk Kim             i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
653b077aed3SPierre Pronchery             if (i == 0 || thismb == NULL || thismb->mac == NULL
654b077aed3SPierre Pronchery                 || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
655b077aed3SPierre Pronchery                 enc_err = 0;
656e71b7053SJung-uk Kim             if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
657b077aed3SPierre Pronchery                 enc_err = 0;
658e71b7053SJung-uk Kim         }
659e71b7053SJung-uk Kim     }
660e71b7053SJung-uk Kim 
661b077aed3SPierre Pronchery     if (enc_err == 0) {
662e71b7053SJung-uk Kim         if (ossl_statem_in_error(s)) {
663e71b7053SJung-uk Kim             /* We already called SSLfatal() */
664b077aed3SPierre Pronchery             goto end;
665e71b7053SJung-uk Kim         }
666e71b7053SJung-uk Kim         /*
667e71b7053SJung-uk Kim          * A separate 'decryption_failed' alert was introduced with TLS 1.0,
668e71b7053SJung-uk Kim          * SSL 3.0 only has 'bad_record_mac'.  But unless a decryption
669e71b7053SJung-uk Kim          * failure is directly visible from the ciphertext anyway, we should
670e71b7053SJung-uk Kim          * not reveal which kind of error occurred -- this might become
671e71b7053SJung-uk Kim          * visible to an attacker (e.g. via a logfile)
672e71b7053SJung-uk Kim          */
673b077aed3SPierre Pronchery         SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
674e71b7053SJung-uk Kim                  SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
675b077aed3SPierre Pronchery         goto end;
676e71b7053SJung-uk Kim     }
677e71b7053SJung-uk Kim 
678aa906e2aSJohn Baldwin  skip_decryption:
679aa906e2aSJohn Baldwin 
680e71b7053SJung-uk Kim     for (j = 0; j < num_recs; j++) {
681e71b7053SJung-uk Kim         thisrr = &rr[j];
682e71b7053SJung-uk Kim 
683e71b7053SJung-uk Kim         /* thisrr->length is now just compressed */
684e71b7053SJung-uk Kim         if (s->expand != NULL) {
685e71b7053SJung-uk Kim             if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
686b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
687e71b7053SJung-uk Kim                          SSL_R_COMPRESSED_LENGTH_TOO_LONG);
688b077aed3SPierre Pronchery                 goto end;
689e71b7053SJung-uk Kim             }
690e71b7053SJung-uk Kim             if (!ssl3_do_uncompress(s, thisrr)) {
691b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
692e71b7053SJung-uk Kim                          SSL_R_BAD_DECOMPRESSION);
693b077aed3SPierre Pronchery                 goto end;
694e71b7053SJung-uk Kim             }
695e71b7053SJung-uk Kim         }
696e71b7053SJung-uk Kim 
697e71b7053SJung-uk Kim         if (SSL_IS_TLS13(s)
698e71b7053SJung-uk Kim                 && s->enc_read_ctx != NULL
699e71b7053SJung-uk Kim                 && thisrr->type != SSL3_RT_ALERT) {
700*c085ca52SDaiki Ueno             /*
701*c085ca52SDaiki Ueno              * The following logic are irrelevant in KTLS: the kernel provides
702*c085ca52SDaiki Ueno              * unprotected record and thus record type represent the actual
703*c085ca52SDaiki Ueno              * content type, and padding is already removed and thisrr->type and
704*c085ca52SDaiki Ueno              * thisrr->length should have the correct values.
705*c085ca52SDaiki Ueno              */
706*c085ca52SDaiki Ueno             if (!using_ktls) {
707e71b7053SJung-uk Kim                 size_t end;
708e71b7053SJung-uk Kim 
709e71b7053SJung-uk Kim                 if (thisrr->length == 0
710e71b7053SJung-uk Kim                         || thisrr->type != SSL3_RT_APPLICATION_DATA) {
711b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
712b077aed3SPierre Pronchery                     goto end;
713e71b7053SJung-uk Kim                 }
714e71b7053SJung-uk Kim 
715e71b7053SJung-uk Kim                 /* Strip trailing padding */
716e71b7053SJung-uk Kim                 for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
717e71b7053SJung-uk Kim                      end--)
718e71b7053SJung-uk Kim                     continue;
719e71b7053SJung-uk Kim 
720e71b7053SJung-uk Kim                 thisrr->length = end;
721e71b7053SJung-uk Kim                 thisrr->type = thisrr->data[end];
722*c085ca52SDaiki Ueno             }
723e71b7053SJung-uk Kim             if (thisrr->type != SSL3_RT_APPLICATION_DATA
724e71b7053SJung-uk Kim                     && thisrr->type != SSL3_RT_ALERT
725e71b7053SJung-uk Kim                     && thisrr->type != SSL3_RT_HANDSHAKE) {
726b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
727b077aed3SPierre Pronchery                 goto end;
728e71b7053SJung-uk Kim             }
729e71b7053SJung-uk Kim             if (s->msg_callback)
730e71b7053SJung-uk Kim                 s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
731*c085ca52SDaiki Ueno                                 &thisrr->type, 1, s, s->msg_callback_arg);
732e71b7053SJung-uk Kim         }
733e71b7053SJung-uk Kim 
734e71b7053SJung-uk Kim         /*
735e71b7053SJung-uk Kim          * TLSv1.3 alert and handshake records are required to be non-zero in
736e71b7053SJung-uk Kim          * length.
737e71b7053SJung-uk Kim          */
738e71b7053SJung-uk Kim         if (SSL_IS_TLS13(s)
739e71b7053SJung-uk Kim                 && (thisrr->type == SSL3_RT_HANDSHAKE
740e71b7053SJung-uk Kim                     || thisrr->type == SSL3_RT_ALERT)
741e71b7053SJung-uk Kim                 && thisrr->length == 0) {
742b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_LENGTH);
743b077aed3SPierre Pronchery             goto end;
744e71b7053SJung-uk Kim         }
745e71b7053SJung-uk Kim 
746f6e5fcdcSJohn Baldwin         /*
747f6e5fcdcSJohn Baldwin          * Usually thisrr->length is the length of a single record, but when
748f6e5fcdcSJohn Baldwin          * KTLS handles the decryption, thisrr->length may be larger than
749f6e5fcdcSJohn Baldwin          * SSL3_RT_MAX_PLAIN_LENGTH because the kernel may have coalesced
750f6e5fcdcSJohn Baldwin          * multiple records.
751f6e5fcdcSJohn Baldwin          * Therefore we have to rely on KTLS to check the plaintext length
752f6e5fcdcSJohn Baldwin          * limit in the kernel.
753f6e5fcdcSJohn Baldwin          */
7540fc28f22SJohn Baldwin         if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH && !using_ktls) {
755b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
756b077aed3SPierre Pronchery             goto end;
757e71b7053SJung-uk Kim         }
758e71b7053SJung-uk Kim 
759f6e5fcdcSJohn Baldwin         /*
760f6e5fcdcSJohn Baldwin          * Check if the received packet overflows the current
761f6e5fcdcSJohn Baldwin          * Max Fragment Length setting.
762f6e5fcdcSJohn Baldwin          * Note: USE_MAX_FRAGMENT_LENGTH_EXT and KTLS are mutually exclusive.
763f6e5fcdcSJohn Baldwin          */
764e71b7053SJung-uk Kim         if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
765f6e5fcdcSJohn Baldwin                 && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
766b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
767b077aed3SPierre Pronchery             goto end;
768e71b7053SJung-uk Kim         }
769e71b7053SJung-uk Kim 
770e71b7053SJung-uk Kim         thisrr->off = 0;
771e71b7053SJung-uk Kim         /*-
772e71b7053SJung-uk Kim          * So at this point the following is true
773e71b7053SJung-uk Kim          * thisrr->type   is the type of record
774e71b7053SJung-uk Kim          * thisrr->length == number of bytes in record
775e71b7053SJung-uk Kim          * thisrr->off    == offset to first valid byte
776e71b7053SJung-uk Kim          * thisrr->data   == where to take bytes from, increment after use :-).
777e71b7053SJung-uk Kim          */
778e71b7053SJung-uk Kim 
779e71b7053SJung-uk Kim         /* just read a 0 length packet */
780e71b7053SJung-uk Kim         if (thisrr->length == 0) {
781e71b7053SJung-uk Kim             RECORD_LAYER_inc_empty_record_count(&s->rlayer);
782e71b7053SJung-uk Kim             if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
783e71b7053SJung-uk Kim                 > MAX_EMPTY_RECORDS) {
784b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_RECORD_TOO_SMALL);
785b077aed3SPierre Pronchery                 goto end;
786e71b7053SJung-uk Kim             }
787e71b7053SJung-uk Kim         } else {
788e71b7053SJung-uk Kim             RECORD_LAYER_reset_empty_record_count(&s->rlayer);
789e71b7053SJung-uk Kim         }
790e71b7053SJung-uk Kim     }
791e71b7053SJung-uk Kim 
792e71b7053SJung-uk Kim     if (s->early_data_state == SSL_EARLY_DATA_READING) {
793e71b7053SJung-uk Kim         thisrr = &rr[0];
794e71b7053SJung-uk Kim         if (thisrr->type == SSL3_RT_APPLICATION_DATA
795e71b7053SJung-uk Kim                 && !early_data_count_ok(s, thisrr->length, 0, 0)) {
796e71b7053SJung-uk Kim             /* SSLfatal already called */
797b077aed3SPierre Pronchery             goto end;
798e71b7053SJung-uk Kim         }
799e71b7053SJung-uk Kim     }
800e71b7053SJung-uk Kim 
801e71b7053SJung-uk Kim     RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
802b077aed3SPierre Pronchery     ret = 1;
803b077aed3SPierre Pronchery  end:
804b077aed3SPierre Pronchery     if (macbufs != NULL) {
805b077aed3SPierre Pronchery         for (j = 0; j < num_recs; j++) {
806b077aed3SPierre Pronchery             if (macbufs[j].alloced)
807b077aed3SPierre Pronchery                 OPENSSL_free(macbufs[j].mac);
808b077aed3SPierre Pronchery         }
809b077aed3SPierre Pronchery         OPENSSL_free(macbufs);
810b077aed3SPierre Pronchery     }
811b077aed3SPierre Pronchery     return ret;
812e71b7053SJung-uk Kim }
813e71b7053SJung-uk Kim 
ssl3_do_uncompress(SSL * ssl,SSL3_RECORD * rr)814e71b7053SJung-uk Kim int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
815e71b7053SJung-uk Kim {
816e71b7053SJung-uk Kim #ifndef OPENSSL_NO_COMP
817e71b7053SJung-uk Kim     int i;
818e71b7053SJung-uk Kim 
819e71b7053SJung-uk Kim     if (rr->comp == NULL) {
820e71b7053SJung-uk Kim         rr->comp = (unsigned char *)
821e71b7053SJung-uk Kim             OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
822e71b7053SJung-uk Kim     }
823e71b7053SJung-uk Kim     if (rr->comp == NULL)
824e71b7053SJung-uk Kim         return 0;
825e71b7053SJung-uk Kim 
826e71b7053SJung-uk Kim     i = COMP_expand_block(ssl->expand, rr->comp,
827e71b7053SJung-uk Kim                           SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
828e71b7053SJung-uk Kim     if (i < 0)
829e71b7053SJung-uk Kim         return 0;
830e71b7053SJung-uk Kim     else
831e71b7053SJung-uk Kim         rr->length = i;
832e71b7053SJung-uk Kim     rr->data = rr->comp;
833e71b7053SJung-uk Kim #endif
834e71b7053SJung-uk Kim     return 1;
835e71b7053SJung-uk Kim }
836e71b7053SJung-uk Kim 
ssl3_do_compress(SSL * ssl,SSL3_RECORD * wr)837e71b7053SJung-uk Kim int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
838e71b7053SJung-uk Kim {
839e71b7053SJung-uk Kim #ifndef OPENSSL_NO_COMP
840e71b7053SJung-uk Kim     int i;
841e71b7053SJung-uk Kim 
842e71b7053SJung-uk Kim     i = COMP_compress_block(ssl->compress, wr->data,
843e71b7053SJung-uk Kim                             (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
844e71b7053SJung-uk Kim                             wr->input, (int)wr->length);
845e71b7053SJung-uk Kim     if (i < 0)
846e71b7053SJung-uk Kim         return 0;
847e71b7053SJung-uk Kim     else
848e71b7053SJung-uk Kim         wr->length = i;
849e71b7053SJung-uk Kim 
850e71b7053SJung-uk Kim     wr->input = wr->data;
851e71b7053SJung-uk Kim #endif
852e71b7053SJung-uk Kim     return 1;
853e71b7053SJung-uk Kim }
854e71b7053SJung-uk Kim 
855e71b7053SJung-uk Kim /*-
856b077aed3SPierre Pronchery  * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
857b077aed3SPierre Pronchery  * internal error, but not otherwise. It is the responsibility of the caller to
858b077aed3SPierre Pronchery  * report a bad_record_mac
859e71b7053SJung-uk Kim  *
860e71b7053SJung-uk Kim  * Returns:
861b077aed3SPierre Pronchery  *    0: if the record is publicly invalid, or an internal error
862b077aed3SPierre Pronchery  *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
863e71b7053SJung-uk Kim  */
ssl3_enc(SSL * s,SSL3_RECORD * inrecs,size_t n_recs,int sending,SSL_MAC_BUF * mac,size_t macsize)864b077aed3SPierre Pronchery int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
865b077aed3SPierre Pronchery              SSL_MAC_BUF *mac, size_t macsize)
866e71b7053SJung-uk Kim {
867e71b7053SJung-uk Kim     SSL3_RECORD *rec;
868e71b7053SJung-uk Kim     EVP_CIPHER_CTX *ds;
869e71b7053SJung-uk Kim     size_t l, i;
870b077aed3SPierre Pronchery     size_t bs;
871e71b7053SJung-uk Kim     const EVP_CIPHER *enc;
872e71b7053SJung-uk Kim 
873e71b7053SJung-uk Kim     rec = inrecs;
874e71b7053SJung-uk Kim     /*
875e71b7053SJung-uk Kim      * We shouldn't ever be called with more than one record in the SSLv3 case
876e71b7053SJung-uk Kim      */
877e71b7053SJung-uk Kim     if (n_recs != 1)
878e71b7053SJung-uk Kim         return 0;
879e71b7053SJung-uk Kim     if (sending) {
880e71b7053SJung-uk Kim         ds = s->enc_write_ctx;
881e71b7053SJung-uk Kim         if (s->enc_write_ctx == NULL)
882e71b7053SJung-uk Kim             enc = NULL;
883e71b7053SJung-uk Kim         else
884b077aed3SPierre Pronchery             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
885e71b7053SJung-uk Kim     } else {
886e71b7053SJung-uk Kim         ds = s->enc_read_ctx;
887e71b7053SJung-uk Kim         if (s->enc_read_ctx == NULL)
888e71b7053SJung-uk Kim             enc = NULL;
889e71b7053SJung-uk Kim         else
890b077aed3SPierre Pronchery             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
891e71b7053SJung-uk Kim     }
892e71b7053SJung-uk Kim 
893e71b7053SJung-uk Kim     if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
894e71b7053SJung-uk Kim         memmove(rec->data, rec->input, rec->length);
895e71b7053SJung-uk Kim         rec->input = rec->data;
896e71b7053SJung-uk Kim     } else {
897b077aed3SPierre Pronchery         int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
898b077aed3SPierre Pronchery 
899e71b7053SJung-uk Kim         l = rec->length;
900b077aed3SPierre Pronchery         bs = EVP_CIPHER_CTX_get_block_size(ds);
901e71b7053SJung-uk Kim 
902e71b7053SJung-uk Kim         /* COMPRESS */
903e71b7053SJung-uk Kim 
904b077aed3SPierre Pronchery         if ((bs != 1) && sending && !provided) {
905b077aed3SPierre Pronchery             /*
906b077aed3SPierre Pronchery              * We only do this for legacy ciphers. Provided ciphers add the
907b077aed3SPierre Pronchery              * padding on the provider side.
908b077aed3SPierre Pronchery              */
909e71b7053SJung-uk Kim             i = bs - (l % bs);
910e71b7053SJung-uk Kim 
911e71b7053SJung-uk Kim             /* we need to add 'i-1' padding bytes */
912e71b7053SJung-uk Kim             l += i;
913e71b7053SJung-uk Kim             /*
914e71b7053SJung-uk Kim              * the last of these zero bytes will be overwritten with the
915e71b7053SJung-uk Kim              * padding length.
916e71b7053SJung-uk Kim              */
917e71b7053SJung-uk Kim             memset(&rec->input[rec->length], 0, i);
918e71b7053SJung-uk Kim             rec->length += i;
919e71b7053SJung-uk Kim             rec->input[l - 1] = (unsigned char)(i - 1);
920e71b7053SJung-uk Kim         }
921e71b7053SJung-uk Kim 
922e71b7053SJung-uk Kim         if (!sending) {
923b077aed3SPierre Pronchery             if (l == 0 || l % bs != 0) {
924b077aed3SPierre Pronchery                 /* Publicly invalid */
925e71b7053SJung-uk Kim                 return 0;
926b077aed3SPierre Pronchery             }
927e71b7053SJung-uk Kim             /* otherwise, rec->length >= bs */
928e71b7053SJung-uk Kim         }
929e71b7053SJung-uk Kim 
930b077aed3SPierre Pronchery         if (EVP_CIPHER_get0_provider(enc) != NULL) {
931b077aed3SPierre Pronchery             int outlen;
932e71b7053SJung-uk Kim 
933b077aed3SPierre Pronchery             if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
934b077aed3SPierre Pronchery                                   (unsigned int)l))
935b077aed3SPierre Pronchery                 return 0;
936b077aed3SPierre Pronchery             rec->length = outlen;
937b077aed3SPierre Pronchery 
938b077aed3SPierre Pronchery             if (!sending && mac != NULL) {
939b077aed3SPierre Pronchery                 /* Now get a pointer to the MAC */
940b077aed3SPierre Pronchery                 OSSL_PARAM params[2], *p = params;
941b077aed3SPierre Pronchery 
942b077aed3SPierre Pronchery                 /* Get the MAC */
943b077aed3SPierre Pronchery                 mac->alloced = 0;
944b077aed3SPierre Pronchery 
945b077aed3SPierre Pronchery                 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
946b077aed3SPierre Pronchery                                                       (void **)&mac->mac,
947b077aed3SPierre Pronchery                                                       macsize);
948b077aed3SPierre Pronchery                 *p = OSSL_PARAM_construct_end();
949b077aed3SPierre Pronchery 
950b077aed3SPierre Pronchery                 if (!EVP_CIPHER_CTX_get_params(ds, params)) {
951b077aed3SPierre Pronchery                     /* Shouldn't normally happen */
952b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
953b077aed3SPierre Pronchery                     return 0;
954e71b7053SJung-uk Kim                 }
955e71b7053SJung-uk Kim             }
956b077aed3SPierre Pronchery         } else {
957b077aed3SPierre Pronchery             if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
958b077aed3SPierre Pronchery                 /* Shouldn't happen */
959b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, ERR_R_INTERNAL_ERROR);
960b077aed3SPierre Pronchery                 return 0;
961b077aed3SPierre Pronchery             }
962b077aed3SPierre Pronchery 
963b077aed3SPierre Pronchery             if (!sending)
964b077aed3SPierre Pronchery                 return ssl3_cbc_remove_padding_and_mac(&rec->length,
965b077aed3SPierre Pronchery                                            rec->orig_len,
966b077aed3SPierre Pronchery                                            rec->data,
967b077aed3SPierre Pronchery                                            (mac != NULL) ? &mac->mac : NULL,
968b077aed3SPierre Pronchery                                            (mac != NULL) ? &mac->alloced : NULL,
969b077aed3SPierre Pronchery                                            bs,
970b077aed3SPierre Pronchery                                            macsize,
971b077aed3SPierre Pronchery                                            s->ctx->libctx);
972b077aed3SPierre Pronchery         }
973e71b7053SJung-uk Kim     }
974e71b7053SJung-uk Kim     return 1;
975e71b7053SJung-uk Kim }
976e71b7053SJung-uk Kim 
977e71b7053SJung-uk Kim #define MAX_PADDING 256
978e71b7053SJung-uk Kim /*-
979b077aed3SPierre Pronchery  * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
980b077aed3SPierre Pronchery  * error, but not otherwise. It is the responsibility of the caller to report
981b077aed3SPierre Pronchery  * a bad_record_mac - if appropriate (DTLS just drops the record).
982e71b7053SJung-uk Kim  *
983e71b7053SJung-uk Kim  * Returns:
984b077aed3SPierre Pronchery  *    0: if the record is publicly invalid, or an internal error, or AEAD
985b077aed3SPierre Pronchery  *       decryption failed, or Encrypt-then-mac decryption failed.
986b077aed3SPierre Pronchery  *    1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
987e71b7053SJung-uk Kim  */
tls1_enc(SSL * s,SSL3_RECORD * recs,size_t n_recs,int sending,SSL_MAC_BUF * macs,size_t macsize)988b077aed3SPierre Pronchery int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
989b077aed3SPierre Pronchery              SSL_MAC_BUF *macs, size_t macsize)
990e71b7053SJung-uk Kim {
991e71b7053SJung-uk Kim     EVP_CIPHER_CTX *ds;
992e71b7053SJung-uk Kim     size_t reclen[SSL_MAX_PIPELINES];
993e71b7053SJung-uk Kim     unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
994b077aed3SPierre Pronchery     unsigned char *data[SSL_MAX_PIPELINES];
995b077aed3SPierre Pronchery     int i, pad = 0, tmpr;
996b077aed3SPierre Pronchery     size_t bs, ctr, padnum, loop;
997e71b7053SJung-uk Kim     unsigned char padval;
998e71b7053SJung-uk Kim     const EVP_CIPHER *enc;
999b077aed3SPierre Pronchery     int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
1000b077aed3SPierre Pronchery                               : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
1001e71b7053SJung-uk Kim 
1002e71b7053SJung-uk Kim     if (n_recs == 0) {
1003b077aed3SPierre Pronchery         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1004e71b7053SJung-uk Kim         return 0;
1005e71b7053SJung-uk Kim     }
1006e71b7053SJung-uk Kim 
1007e71b7053SJung-uk Kim     if (sending) {
1008b077aed3SPierre Pronchery         if (EVP_MD_CTX_get0_md(s->write_hash)) {
1009b077aed3SPierre Pronchery             int n = EVP_MD_CTX_get_size(s->write_hash);
1010e71b7053SJung-uk Kim             if (!ossl_assert(n >= 0)) {
1011b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1012b077aed3SPierre Pronchery                 return 0;
1013e71b7053SJung-uk Kim             }
1014e71b7053SJung-uk Kim         }
1015e71b7053SJung-uk Kim         ds = s->enc_write_ctx;
1016e71b7053SJung-uk Kim         if (s->enc_write_ctx == NULL)
1017e71b7053SJung-uk Kim             enc = NULL;
1018e71b7053SJung-uk Kim         else {
1019e71b7053SJung-uk Kim             int ivlen;
1020b077aed3SPierre Pronchery 
1021b077aed3SPierre Pronchery             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx);
1022e71b7053SJung-uk Kim             /* For TLSv1.1 and later explicit IV */
1023e71b7053SJung-uk Kim             if (SSL_USE_EXPLICIT_IV(s)
1024b077aed3SPierre Pronchery                 && EVP_CIPHER_get_mode(enc) == EVP_CIPH_CBC_MODE)
1025b077aed3SPierre Pronchery                 ivlen = EVP_CIPHER_get_iv_length(enc);
1026e71b7053SJung-uk Kim             else
1027e71b7053SJung-uk Kim                 ivlen = 0;
1028e71b7053SJung-uk Kim             if (ivlen > 1) {
1029e71b7053SJung-uk Kim                 for (ctr = 0; ctr < n_recs; ctr++) {
1030e71b7053SJung-uk Kim                     if (recs[ctr].data != recs[ctr].input) {
1031e71b7053SJung-uk Kim                         /*
1032e71b7053SJung-uk Kim                          * we can't write into the input stream: Can this ever
1033e71b7053SJung-uk Kim                          * happen?? (steve)
1034e71b7053SJung-uk Kim                          */
1035b077aed3SPierre Pronchery                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1036b077aed3SPierre Pronchery                         return 0;
1037b077aed3SPierre Pronchery                     } else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
1038b077aed3SPierre Pronchery                                              ivlen, 0) <= 0) {
1039b077aed3SPierre Pronchery                         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1040b077aed3SPierre Pronchery                         return 0;
1041e71b7053SJung-uk Kim                     }
1042e71b7053SJung-uk Kim                 }
1043e71b7053SJung-uk Kim             }
1044e71b7053SJung-uk Kim         }
1045e71b7053SJung-uk Kim     } else {
1046b077aed3SPierre Pronchery         if (EVP_MD_CTX_get0_md(s->read_hash)) {
1047b077aed3SPierre Pronchery             int n = EVP_MD_CTX_get_size(s->read_hash);
1048e71b7053SJung-uk Kim             if (!ossl_assert(n >= 0)) {
1049b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1050b077aed3SPierre Pronchery                 return 0;
1051e71b7053SJung-uk Kim             }
1052e71b7053SJung-uk Kim         }
1053e71b7053SJung-uk Kim         ds = s->enc_read_ctx;
1054e71b7053SJung-uk Kim         if (s->enc_read_ctx == NULL)
1055e71b7053SJung-uk Kim             enc = NULL;
1056e71b7053SJung-uk Kim         else
1057b077aed3SPierre Pronchery             enc = EVP_CIPHER_CTX_get0_cipher(s->enc_read_ctx);
1058e71b7053SJung-uk Kim     }
1059e71b7053SJung-uk Kim 
1060e71b7053SJung-uk Kim     if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
1061e71b7053SJung-uk Kim         for (ctr = 0; ctr < n_recs; ctr++) {
1062e71b7053SJung-uk Kim             memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
1063e71b7053SJung-uk Kim             recs[ctr].input = recs[ctr].data;
1064e71b7053SJung-uk Kim         }
1065e71b7053SJung-uk Kim     } else {
1066b077aed3SPierre Pronchery         int provided = (EVP_CIPHER_get0_provider(enc) != NULL);
1067b077aed3SPierre Pronchery 
1068b077aed3SPierre Pronchery         bs = EVP_CIPHER_get_block_size(EVP_CIPHER_CTX_get0_cipher(ds));
1069e71b7053SJung-uk Kim 
1070e71b7053SJung-uk Kim         if (n_recs > 1) {
1071b077aed3SPierre Pronchery             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
1072b077aed3SPierre Pronchery                   & EVP_CIPH_FLAG_PIPELINE) == 0) {
1073e71b7053SJung-uk Kim                 /*
1074e71b7053SJung-uk Kim                  * We shouldn't have been called with pipeline data if the
1075e71b7053SJung-uk Kim                  * cipher doesn't support pipelining
1076e71b7053SJung-uk Kim                  */
1077b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
1078b077aed3SPierre Pronchery                 return 0;
1079e71b7053SJung-uk Kim             }
1080e71b7053SJung-uk Kim         }
1081e71b7053SJung-uk Kim         for (ctr = 0; ctr < n_recs; ctr++) {
1082e71b7053SJung-uk Kim             reclen[ctr] = recs[ctr].length;
1083e71b7053SJung-uk Kim 
1084b077aed3SPierre Pronchery             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
1085b077aed3SPierre Pronchery                         & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
1086e71b7053SJung-uk Kim                 unsigned char *seq;
1087e71b7053SJung-uk Kim 
1088e71b7053SJung-uk Kim                 seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
1089e71b7053SJung-uk Kim                     : RECORD_LAYER_get_read_sequence(&s->rlayer);
1090e71b7053SJung-uk Kim 
1091e71b7053SJung-uk Kim                 if (SSL_IS_DTLS(s)) {
1092e71b7053SJung-uk Kim                     /* DTLS does not support pipelining */
1093b2bf0c7eSJung-uk Kim                     unsigned char dtlsseq[8], *p = dtlsseq;
1094e71b7053SJung-uk Kim 
1095e71b7053SJung-uk Kim                     s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
1096e71b7053SJung-uk Kim                         DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
1097e71b7053SJung-uk Kim                     memcpy(p, &seq[2], 6);
1098e71b7053SJung-uk Kim                     memcpy(buf[ctr], dtlsseq, 8);
1099e71b7053SJung-uk Kim                 } else {
1100e71b7053SJung-uk Kim                     memcpy(buf[ctr], seq, 8);
1101e71b7053SJung-uk Kim                     for (i = 7; i >= 0; i--) { /* increment */
1102e71b7053SJung-uk Kim                         ++seq[i];
1103e71b7053SJung-uk Kim                         if (seq[i] != 0)
1104e71b7053SJung-uk Kim                             break;
1105e71b7053SJung-uk Kim                     }
1106e71b7053SJung-uk Kim                 }
1107e71b7053SJung-uk Kim 
1108e71b7053SJung-uk Kim                 buf[ctr][8] = recs[ctr].type;
1109e71b7053SJung-uk Kim                 buf[ctr][9] = (unsigned char)(s->version >> 8);
1110e71b7053SJung-uk Kim                 buf[ctr][10] = (unsigned char)(s->version);
1111e71b7053SJung-uk Kim                 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
1112e71b7053SJung-uk Kim                 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
1113e71b7053SJung-uk Kim                 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
1114e71b7053SJung-uk Kim                                           EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
1115e71b7053SJung-uk Kim                 if (pad <= 0) {
1116b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1117b077aed3SPierre Pronchery                     return 0;
1118e71b7053SJung-uk Kim                 }
1119e71b7053SJung-uk Kim 
1120e71b7053SJung-uk Kim                 if (sending) {
1121e71b7053SJung-uk Kim                     reclen[ctr] += pad;
1122e71b7053SJung-uk Kim                     recs[ctr].length += pad;
1123e71b7053SJung-uk Kim                 }
1124e71b7053SJung-uk Kim 
1125b077aed3SPierre Pronchery             } else if ((bs != 1) && sending && !provided) {
1126b077aed3SPierre Pronchery                 /*
1127b077aed3SPierre Pronchery                  * We only do this for legacy ciphers. Provided ciphers add the
1128b077aed3SPierre Pronchery                  * padding on the provider side.
1129b077aed3SPierre Pronchery                  */
1130e71b7053SJung-uk Kim                 padnum = bs - (reclen[ctr] % bs);
1131e71b7053SJung-uk Kim 
1132e71b7053SJung-uk Kim                 /* Add weird padding of up to 256 bytes */
1133e71b7053SJung-uk Kim 
1134e71b7053SJung-uk Kim                 if (padnum > MAX_PADDING) {
1135b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1136b077aed3SPierre Pronchery                     return 0;
1137e71b7053SJung-uk Kim                 }
1138e71b7053SJung-uk Kim                 /* we need to add 'padnum' padding bytes of value padval */
1139e71b7053SJung-uk Kim                 padval = (unsigned char)(padnum - 1);
1140e71b7053SJung-uk Kim                 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
1141e71b7053SJung-uk Kim                     recs[ctr].input[loop] = padval;
1142e71b7053SJung-uk Kim                 reclen[ctr] += padnum;
1143e71b7053SJung-uk Kim                 recs[ctr].length += padnum;
1144e71b7053SJung-uk Kim             }
1145e71b7053SJung-uk Kim 
1146e71b7053SJung-uk Kim             if (!sending) {
1147b077aed3SPierre Pronchery                 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
1148b077aed3SPierre Pronchery                     /* Publicly invalid */
1149e71b7053SJung-uk Kim                     return 0;
1150e71b7053SJung-uk Kim                 }
1151e71b7053SJung-uk Kim             }
1152b077aed3SPierre Pronchery         }
1153e71b7053SJung-uk Kim         if (n_recs > 1) {
1154e71b7053SJung-uk Kim             /* Set the output buffers */
1155e71b7053SJung-uk Kim             for (ctr = 0; ctr < n_recs; ctr++) {
1156e71b7053SJung-uk Kim                 data[ctr] = recs[ctr].data;
1157e71b7053SJung-uk Kim             }
1158e71b7053SJung-uk Kim             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
1159e71b7053SJung-uk Kim                                     (int)n_recs, data) <= 0) {
1160b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
1161b077aed3SPierre Pronchery                 return 0;
1162e71b7053SJung-uk Kim             }
1163e71b7053SJung-uk Kim             /* Set the input buffers */
1164e71b7053SJung-uk Kim             for (ctr = 0; ctr < n_recs; ctr++) {
1165e71b7053SJung-uk Kim                 data[ctr] = recs[ctr].input;
1166e71b7053SJung-uk Kim             }
1167e71b7053SJung-uk Kim             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
1168e71b7053SJung-uk Kim                                     (int)n_recs, data) <= 0
1169e71b7053SJung-uk Kim                 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
1170e71b7053SJung-uk Kim                                        (int)n_recs, reclen) <= 0) {
1171b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
1172b077aed3SPierre Pronchery                 return 0;
1173e71b7053SJung-uk Kim             }
1174e71b7053SJung-uk Kim         }
1175e71b7053SJung-uk Kim 
1176b077aed3SPierre Pronchery         if (!SSL_IS_DTLS(s) && tlstree_enc) {
1177b077aed3SPierre Pronchery             unsigned char *seq;
1178b077aed3SPierre Pronchery             int decrement_seq = 0;
1179b077aed3SPierre Pronchery 
1180b077aed3SPierre Pronchery             /*
1181b077aed3SPierre Pronchery              * When sending, seq is incremented after MAC calculation.
1182b077aed3SPierre Pronchery              * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
1183b077aed3SPierre Pronchery              * Otherwise we have to decrease it in the implementation
1184b077aed3SPierre Pronchery              */
1185b077aed3SPierre Pronchery             if (sending && !SSL_WRITE_ETM(s))
1186b077aed3SPierre Pronchery                 decrement_seq = 1;
1187b077aed3SPierre Pronchery 
1188b077aed3SPierre Pronchery             seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
1189b077aed3SPierre Pronchery                           : RECORD_LAYER_get_read_sequence(&s->rlayer);
1190b077aed3SPierre Pronchery             if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
1191b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1192b077aed3SPierre Pronchery                 return 0;
1193b077aed3SPierre Pronchery             }
1194b077aed3SPierre Pronchery         }
1195b077aed3SPierre Pronchery 
1196b077aed3SPierre Pronchery         if (provided) {
1197b077aed3SPierre Pronchery             int outlen;
1198b077aed3SPierre Pronchery 
1199b077aed3SPierre Pronchery             /* Provided cipher - we do not support pipelining on this path */
1200b077aed3SPierre Pronchery             if (n_recs > 1)  {
1201b077aed3SPierre Pronchery                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1202b077aed3SPierre Pronchery                 return 0;
1203b077aed3SPierre Pronchery             }
1204b077aed3SPierre Pronchery 
1205b077aed3SPierre Pronchery             if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
1206b077aed3SPierre Pronchery                                   (unsigned int)reclen[0]))
1207b077aed3SPierre Pronchery                 return 0;
1208b077aed3SPierre Pronchery             recs[0].length = outlen;
1209b077aed3SPierre Pronchery 
1210b077aed3SPierre Pronchery             /*
1211b077aed3SPierre Pronchery              * The length returned from EVP_CipherUpdate above is the actual
1212b077aed3SPierre Pronchery              * payload length. We need to adjust the data/input ptr to skip over
1213b077aed3SPierre Pronchery              * any explicit IV
1214b077aed3SPierre Pronchery              */
1215b077aed3SPierre Pronchery             if (!sending) {
1216b077aed3SPierre Pronchery                 if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
1217b077aed3SPierre Pronchery                         recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1218b077aed3SPierre Pronchery                         recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1219b077aed3SPierre Pronchery                 } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
1220b077aed3SPierre Pronchery                         recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1221b077aed3SPierre Pronchery                         recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1222b077aed3SPierre Pronchery                 } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
1223b077aed3SPierre Pronchery                     recs[0].data += bs;
1224b077aed3SPierre Pronchery                     recs[0].input += bs;
1225b077aed3SPierre Pronchery                     recs[0].orig_len -= bs;
1226b077aed3SPierre Pronchery                 }
1227b077aed3SPierre Pronchery 
1228b077aed3SPierre Pronchery                 /* Now get a pointer to the MAC (if applicable) */
1229b077aed3SPierre Pronchery                 if (macs != NULL) {
1230b077aed3SPierre Pronchery                     OSSL_PARAM params[2], *p = params;
1231b077aed3SPierre Pronchery 
1232b077aed3SPierre Pronchery                     /* Get the MAC */
1233b077aed3SPierre Pronchery                     macs[0].alloced = 0;
1234b077aed3SPierre Pronchery 
1235b077aed3SPierre Pronchery                     *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
1236b077aed3SPierre Pronchery                                                           (void **)&macs[0].mac,
1237b077aed3SPierre Pronchery                                                           macsize);
1238b077aed3SPierre Pronchery                     *p = OSSL_PARAM_construct_end();
1239b077aed3SPierre Pronchery 
1240b077aed3SPierre Pronchery                     if (!EVP_CIPHER_CTX_get_params(ds, params)) {
1241b077aed3SPierre Pronchery                         /* Shouldn't normally happen */
1242b077aed3SPierre Pronchery                         SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1243b077aed3SPierre Pronchery                                  ERR_R_INTERNAL_ERROR);
1244b077aed3SPierre Pronchery                         return 0;
1245b077aed3SPierre Pronchery                     }
1246b077aed3SPierre Pronchery                 }
1247b077aed3SPierre Pronchery             }
1248b077aed3SPierre Pronchery         } else {
1249b077aed3SPierre Pronchery             /* Legacy cipher */
1250b077aed3SPierre Pronchery 
1251e71b7053SJung-uk Kim             tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
1252e71b7053SJung-uk Kim                               (unsigned int)reclen[0]);
1253b077aed3SPierre Pronchery             if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
1254b077aed3SPierre Pronchery                  & EVP_CIPH_FLAG_CUSTOM_CIPHER) != 0
1255e71b7053SJung-uk Kim                 ? (tmpr < 0)
1256b077aed3SPierre Pronchery                 : (tmpr == 0)) {
1257b077aed3SPierre Pronchery                 /* AEAD can fail to verify MAC */
1258b077aed3SPierre Pronchery                 return 0;
1259b077aed3SPierre Pronchery             }
1260e71b7053SJung-uk Kim 
1261b077aed3SPierre Pronchery             if (!sending) {
1262e71b7053SJung-uk Kim                 for (ctr = 0; ctr < n_recs; ctr++) {
1263b077aed3SPierre Pronchery                     /* Adjust the record to remove the explicit IV/MAC/Tag */
1264b077aed3SPierre Pronchery                     if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
1265e71b7053SJung-uk Kim                         recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1266e71b7053SJung-uk Kim                         recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1267e71b7053SJung-uk Kim                         recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1268b077aed3SPierre Pronchery                     } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
1269e71b7053SJung-uk Kim                         recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1270e71b7053SJung-uk Kim                         recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1271e71b7053SJung-uk Kim                         recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
1272b077aed3SPierre Pronchery                     } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
1273b077aed3SPierre Pronchery                         if (recs[ctr].length < bs)
1274b077aed3SPierre Pronchery                             return 0;
1275b077aed3SPierre Pronchery                         recs[ctr].data += bs;
1276b077aed3SPierre Pronchery                         recs[ctr].input += bs;
1277b077aed3SPierre Pronchery                         recs[ctr].length -= bs;
1278b077aed3SPierre Pronchery                         recs[ctr].orig_len -= bs;
1279e71b7053SJung-uk Kim                     }
1280e71b7053SJung-uk Kim 
1281e71b7053SJung-uk Kim                     /*
1282b077aed3SPierre Pronchery                      * If using Mac-then-encrypt, then this will succeed but
1283b077aed3SPierre Pronchery                      * with a random MAC if padding is invalid
1284e71b7053SJung-uk Kim                      */
1285b077aed3SPierre Pronchery                     if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
1286b077aed3SPierre Pronchery                                          recs[ctr].orig_len,
1287b077aed3SPierre Pronchery                                          recs[ctr].data,
1288b077aed3SPierre Pronchery                                          (macs != NULL) ? &macs[ctr].mac : NULL,
1289b077aed3SPierre Pronchery                                          (macs != NULL) ? &macs[ctr].alloced
1290b077aed3SPierre Pronchery                                                         : NULL,
1291b077aed3SPierre Pronchery                                          bs,
1292b077aed3SPierre Pronchery                                          pad ? (size_t)pad : macsize,
1293b077aed3SPierre Pronchery                                          (EVP_CIPHER_get_flags(enc)
1294b077aed3SPierre Pronchery                                          & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
1295b077aed3SPierre Pronchery                                          s->ctx->libctx))
1296e71b7053SJung-uk Kim                         return 0;
1297e71b7053SJung-uk Kim                 }
1298e71b7053SJung-uk Kim             }
1299e71b7053SJung-uk Kim         }
1300b077aed3SPierre Pronchery     }
1301b077aed3SPierre Pronchery     return 1;
1302b077aed3SPierre Pronchery }
1303b077aed3SPierre Pronchery 
1304b077aed3SPierre Pronchery /*
1305b077aed3SPierre Pronchery  * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
1306b077aed3SPierre Pronchery  * which ssl3_cbc_digest_record supports.
1307b077aed3SPierre Pronchery  */
ssl3_cbc_record_digest_supported(const EVP_MD_CTX * ctx)1308b077aed3SPierre Pronchery char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
1309b077aed3SPierre Pronchery {
1310b077aed3SPierre Pronchery     switch (EVP_MD_CTX_get_type(ctx)) {
1311b077aed3SPierre Pronchery     case NID_md5:
1312b077aed3SPierre Pronchery     case NID_sha1:
1313b077aed3SPierre Pronchery     case NID_sha224:
1314b077aed3SPierre Pronchery     case NID_sha256:
1315b077aed3SPierre Pronchery     case NID_sha384:
1316b077aed3SPierre Pronchery     case NID_sha512:
1317b077aed3SPierre Pronchery         return 1;
1318b077aed3SPierre Pronchery     default:
1319b077aed3SPierre Pronchery         return 0;
1320b077aed3SPierre Pronchery     }
1321e71b7053SJung-uk Kim }
1322e71b7053SJung-uk Kim 
n_ssl3_mac(SSL * ssl,SSL3_RECORD * rec,unsigned char * md,int sending)1323e71b7053SJung-uk Kim int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
1324e71b7053SJung-uk Kim {
1325e71b7053SJung-uk Kim     unsigned char *mac_sec, *seq;
1326e71b7053SJung-uk Kim     const EVP_MD_CTX *hash;
1327e71b7053SJung-uk Kim     unsigned char *p, rec_char;
1328e71b7053SJung-uk Kim     size_t md_size;
1329e71b7053SJung-uk Kim     size_t npad;
1330e71b7053SJung-uk Kim     int t;
1331e71b7053SJung-uk Kim 
1332e71b7053SJung-uk Kim     if (sending) {
1333b077aed3SPierre Pronchery         mac_sec = &(ssl->s3.write_mac_secret[0]);
1334e71b7053SJung-uk Kim         seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1335e71b7053SJung-uk Kim         hash = ssl->write_hash;
1336e71b7053SJung-uk Kim     } else {
1337b077aed3SPierre Pronchery         mac_sec = &(ssl->s3.read_mac_secret[0]);
1338e71b7053SJung-uk Kim         seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1339e71b7053SJung-uk Kim         hash = ssl->read_hash;
1340e71b7053SJung-uk Kim     }
1341e71b7053SJung-uk Kim 
1342b077aed3SPierre Pronchery     t = EVP_MD_CTX_get_size(hash);
1343b077aed3SPierre Pronchery     if (t <= 0)
1344e71b7053SJung-uk Kim         return 0;
1345e71b7053SJung-uk Kim     md_size = t;
1346e71b7053SJung-uk Kim     npad = (48 / md_size) * md_size;
1347e71b7053SJung-uk Kim 
1348b077aed3SPierre Pronchery     if (!sending
1349b077aed3SPierre Pronchery         && EVP_CIPHER_CTX_get_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE
1350b077aed3SPierre Pronchery         && ssl3_cbc_record_digest_supported(hash)) {
1351b077aed3SPierre Pronchery #ifdef OPENSSL_NO_DEPRECATED_3_0
1352b077aed3SPierre Pronchery         return 0;
1353b077aed3SPierre Pronchery #else
1354e71b7053SJung-uk Kim         /*
1355e71b7053SJung-uk Kim          * This is a CBC-encrypted record. We must avoid leaking any
1356e71b7053SJung-uk Kim          * timing-side channel information about how many blocks of data we
1357e71b7053SJung-uk Kim          * are hashing because that gives an attacker a timing-oracle.
1358e71b7053SJung-uk Kim          */
1359e71b7053SJung-uk Kim 
1360e71b7053SJung-uk Kim         /*-
1361e71b7053SJung-uk Kim          * npad is, at most, 48 bytes and that's with MD5:
1362e71b7053SJung-uk Kim          *   16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
1363e71b7053SJung-uk Kim          *
1364e71b7053SJung-uk Kim          * With SHA-1 (the largest hash speced for SSLv3) the hash size
1365e71b7053SJung-uk Kim          * goes up 4, but npad goes down by 8, resulting in a smaller
1366e71b7053SJung-uk Kim          * total size.
1367e71b7053SJung-uk Kim          */
1368e71b7053SJung-uk Kim         unsigned char header[75];
1369e71b7053SJung-uk Kim         size_t j = 0;
1370e71b7053SJung-uk Kim         memcpy(header + j, mac_sec, md_size);
1371e71b7053SJung-uk Kim         j += md_size;
1372e71b7053SJung-uk Kim         memcpy(header + j, ssl3_pad_1, npad);
1373e71b7053SJung-uk Kim         j += npad;
1374e71b7053SJung-uk Kim         memcpy(header + j, seq, 8);
1375e71b7053SJung-uk Kim         j += 8;
1376e71b7053SJung-uk Kim         header[j++] = rec->type;
1377e71b7053SJung-uk Kim         header[j++] = (unsigned char)(rec->length >> 8);
1378e71b7053SJung-uk Kim         header[j++] = (unsigned char)(rec->length & 0xff);
1379e71b7053SJung-uk Kim 
1380e71b7053SJung-uk Kim         /* Final param == is SSLv3 */
1381b077aed3SPierre Pronchery         if (ssl3_cbc_digest_record(EVP_MD_CTX_get0_md(hash),
1382e71b7053SJung-uk Kim                                    md, &md_size,
1383e71b7053SJung-uk Kim                                    header, rec->input,
1384b077aed3SPierre Pronchery                                    rec->length, rec->orig_len,
1385e71b7053SJung-uk Kim                                    mac_sec, md_size, 1) <= 0)
1386e71b7053SJung-uk Kim             return 0;
1387b077aed3SPierre Pronchery #endif
1388e71b7053SJung-uk Kim     } else {
1389e71b7053SJung-uk Kim         unsigned int md_size_u;
1390e71b7053SJung-uk Kim         /* Chop the digest off the end :-) */
1391e71b7053SJung-uk Kim         EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
1392e71b7053SJung-uk Kim 
1393e71b7053SJung-uk Kim         if (md_ctx == NULL)
1394e71b7053SJung-uk Kim             return 0;
1395e71b7053SJung-uk Kim 
1396e71b7053SJung-uk Kim         rec_char = rec->type;
1397e71b7053SJung-uk Kim         p = md;
1398e71b7053SJung-uk Kim         s2n(rec->length, p);
1399e71b7053SJung-uk Kim         if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1400e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1401e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
1402e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
1403e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
1404e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, md, 2) <= 0
1405e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
1406e71b7053SJung-uk Kim             || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
1407e71b7053SJung-uk Kim             || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1408e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1409e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
1410e71b7053SJung-uk Kim             || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
1411e71b7053SJung-uk Kim             || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
1412e71b7053SJung-uk Kim             EVP_MD_CTX_free(md_ctx);
1413e71b7053SJung-uk Kim             return 0;
1414e71b7053SJung-uk Kim         }
1415e71b7053SJung-uk Kim 
1416e71b7053SJung-uk Kim         EVP_MD_CTX_free(md_ctx);
1417e71b7053SJung-uk Kim     }
1418e71b7053SJung-uk Kim 
1419e71b7053SJung-uk Kim     ssl3_record_sequence_update(seq);
1420e71b7053SJung-uk Kim     return 1;
1421e71b7053SJung-uk Kim }
1422e71b7053SJung-uk Kim 
tls1_mac(SSL * ssl,SSL3_RECORD * rec,unsigned char * md,int sending)1423e71b7053SJung-uk Kim int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
1424e71b7053SJung-uk Kim {
1425e71b7053SJung-uk Kim     unsigned char *seq;
1426e71b7053SJung-uk Kim     EVP_MD_CTX *hash;
1427e71b7053SJung-uk Kim     size_t md_size;
1428e71b7053SJung-uk Kim     int i;
1429e71b7053SJung-uk Kim     EVP_MD_CTX *hmac = NULL, *mac_ctx;
1430e71b7053SJung-uk Kim     unsigned char header[13];
1431b077aed3SPierre Pronchery     int stream_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
1432b077aed3SPierre Pronchery                              : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
1433b077aed3SPierre Pronchery     int tlstree_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
1434b077aed3SPierre Pronchery                               : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
1435e71b7053SJung-uk Kim     int t;
1436b077aed3SPierre Pronchery     int ret = 0;
1437e71b7053SJung-uk Kim 
1438e71b7053SJung-uk Kim     if (sending) {
1439e71b7053SJung-uk Kim         seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1440e71b7053SJung-uk Kim         hash = ssl->write_hash;
1441e71b7053SJung-uk Kim     } else {
1442e71b7053SJung-uk Kim         seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1443e71b7053SJung-uk Kim         hash = ssl->read_hash;
1444e71b7053SJung-uk Kim     }
1445e71b7053SJung-uk Kim 
1446b077aed3SPierre Pronchery     t = EVP_MD_CTX_get_size(hash);
1447e71b7053SJung-uk Kim     if (!ossl_assert(t >= 0))
1448e71b7053SJung-uk Kim         return 0;
1449e71b7053SJung-uk Kim     md_size = t;
1450e71b7053SJung-uk Kim 
1451e71b7053SJung-uk Kim     /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1452e71b7053SJung-uk Kim     if (stream_mac) {
1453e71b7053SJung-uk Kim         mac_ctx = hash;
1454e71b7053SJung-uk Kim     } else {
1455e71b7053SJung-uk Kim         hmac = EVP_MD_CTX_new();
1456e71b7053SJung-uk Kim         if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
1457b077aed3SPierre Pronchery             goto end;
1458e71b7053SJung-uk Kim         }
1459e71b7053SJung-uk Kim         mac_ctx = hmac;
1460e71b7053SJung-uk Kim     }
1461e71b7053SJung-uk Kim 
1462b077aed3SPierre Pronchery     if (!SSL_IS_DTLS(ssl) && tlstree_mac && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
1463b077aed3SPierre Pronchery         goto end;
1464b077aed3SPierre Pronchery     }
1465b077aed3SPierre Pronchery 
1466e71b7053SJung-uk Kim     if (SSL_IS_DTLS(ssl)) {
1467e71b7053SJung-uk Kim         unsigned char dtlsseq[8], *p = dtlsseq;
1468e71b7053SJung-uk Kim 
1469e71b7053SJung-uk Kim         s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1470e71b7053SJung-uk Kim             DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1471e71b7053SJung-uk Kim         memcpy(p, &seq[2], 6);
1472e71b7053SJung-uk Kim 
1473e71b7053SJung-uk Kim         memcpy(header, dtlsseq, 8);
1474e71b7053SJung-uk Kim     } else
1475e71b7053SJung-uk Kim         memcpy(header, seq, 8);
1476e71b7053SJung-uk Kim 
1477e71b7053SJung-uk Kim     header[8] = rec->type;
1478e71b7053SJung-uk Kim     header[9] = (unsigned char)(ssl->version >> 8);
1479e71b7053SJung-uk Kim     header[10] = (unsigned char)(ssl->version);
1480e71b7053SJung-uk Kim     header[11] = (unsigned char)(rec->length >> 8);
1481e71b7053SJung-uk Kim     header[12] = (unsigned char)(rec->length & 0xff);
1482e71b7053SJung-uk Kim 
1483b077aed3SPierre Pronchery     if (!sending && !SSL_READ_ETM(ssl)
1484b077aed3SPierre Pronchery         && EVP_CIPHER_CTX_get_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE
1485b077aed3SPierre Pronchery         && ssl3_cbc_record_digest_supported(mac_ctx)) {
1486b077aed3SPierre Pronchery         OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
1487b077aed3SPierre Pronchery 
1488b077aed3SPierre Pronchery         *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
1489b077aed3SPierre Pronchery                                            &rec->orig_len);
1490b077aed3SPierre Pronchery         *p++ = OSSL_PARAM_construct_end();
1491b077aed3SPierre Pronchery 
1492b077aed3SPierre Pronchery         if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_get_pkey_ctx(mac_ctx),
1493b077aed3SPierre Pronchery                                      tls_hmac_params)) {
1494b077aed3SPierre Pronchery             goto end;
1495e71b7053SJung-uk Kim         }
1496b077aed3SPierre Pronchery     }
1497b077aed3SPierre Pronchery 
1498e71b7053SJung-uk Kim     if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1499e71b7053SJung-uk Kim         || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1500e71b7053SJung-uk Kim         || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1501b077aed3SPierre Pronchery         goto end;
1502e71b7053SJung-uk Kim     }
1503e71b7053SJung-uk Kim 
1504b077aed3SPierre Pronchery     OSSL_TRACE_BEGIN(TLS) {
1505b077aed3SPierre Pronchery         BIO_printf(trc_out, "seq:\n");
1506b077aed3SPierre Pronchery         BIO_dump_indent(trc_out, seq, 8, 4);
1507b077aed3SPierre Pronchery         BIO_printf(trc_out, "rec:\n");
1508b077aed3SPierre Pronchery         BIO_dump_indent(trc_out, rec->data, rec->length, 4);
1509b077aed3SPierre Pronchery     } OSSL_TRACE_END(TLS);
1510e71b7053SJung-uk Kim 
1511e71b7053SJung-uk Kim     if (!SSL_IS_DTLS(ssl)) {
1512e71b7053SJung-uk Kim         for (i = 7; i >= 0; i--) {
1513e71b7053SJung-uk Kim             ++seq[i];
1514e71b7053SJung-uk Kim             if (seq[i] != 0)
1515e71b7053SJung-uk Kim                 break;
1516e71b7053SJung-uk Kim         }
1517e71b7053SJung-uk Kim     }
1518b077aed3SPierre Pronchery     OSSL_TRACE_BEGIN(TLS) {
1519b077aed3SPierre Pronchery         BIO_printf(trc_out, "md:\n");
1520b077aed3SPierre Pronchery         BIO_dump_indent(trc_out, md, md_size, 4);
1521b077aed3SPierre Pronchery     } OSSL_TRACE_END(TLS);
1522b077aed3SPierre Pronchery     ret = 1;
1523b077aed3SPierre Pronchery  end:
1524b077aed3SPierre Pronchery     EVP_MD_CTX_free(hmac);
1525b077aed3SPierre Pronchery     return ret;
1526e71b7053SJung-uk Kim }
1527e71b7053SJung-uk Kim 
dtls1_process_record(SSL * s,DTLS1_BITMAP * bitmap)1528e71b7053SJung-uk Kim int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1529e71b7053SJung-uk Kim {
1530e71b7053SJung-uk Kim     int i;
1531e71b7053SJung-uk Kim     int enc_err;
1532e71b7053SJung-uk Kim     SSL_SESSION *sess;
1533e71b7053SJung-uk Kim     SSL3_RECORD *rr;
1534e71b7053SJung-uk Kim     int imac_size;
1535b077aed3SPierre Pronchery     size_t mac_size = 0;
1536e71b7053SJung-uk Kim     unsigned char md[EVP_MAX_MD_SIZE];
153717f01e99SJung-uk Kim     size_t max_plain_length = SSL3_RT_MAX_PLAIN_LENGTH;
1538b077aed3SPierre Pronchery     SSL_MAC_BUF macbuf = { NULL, 0 };
1539b077aed3SPierre Pronchery     int ret = 0;
1540e71b7053SJung-uk Kim 
1541e71b7053SJung-uk Kim     rr = RECORD_LAYER_get_rrec(&s->rlayer);
1542e71b7053SJung-uk Kim     sess = s->session;
1543e71b7053SJung-uk Kim 
1544e71b7053SJung-uk Kim     /*
15459a3ae0cdSJung-uk Kim      * At this point, s->rlayer.packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
15469a3ae0cdSJung-uk Kim      * and we have that many bytes in s->rlayer.packet
1547e71b7053SJung-uk Kim      */
1548e71b7053SJung-uk Kim     rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1549e71b7053SJung-uk Kim 
1550e71b7053SJung-uk Kim     /*
15519a3ae0cdSJung-uk Kim      * ok, we can now read from 's->rlayer.packet' data into 'rr'. rr->input
15529a3ae0cdSJung-uk Kim      * points at rr->length bytes, which need to be copied into rr->data by
15539a3ae0cdSJung-uk Kim      * either the decryption or by the decompression. When the data is 'copied'
15549a3ae0cdSJung-uk Kim      * into the rr->data buffer, rr->input will be pointed at the new buffer
1555e71b7053SJung-uk Kim      */
1556e71b7053SJung-uk Kim 
1557e71b7053SJung-uk Kim     /*
1558e71b7053SJung-uk Kim      * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1559e71b7053SJung-uk Kim      * bytes of encrypted compressed stuff.
1560e71b7053SJung-uk Kim      */
1561e71b7053SJung-uk Kim 
1562e71b7053SJung-uk Kim     /* check is not needed I believe */
1563e71b7053SJung-uk Kim     if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1564b077aed3SPierre Pronchery         SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1565e71b7053SJung-uk Kim         return 0;
1566e71b7053SJung-uk Kim     }
1567e71b7053SJung-uk Kim 
1568e71b7053SJung-uk Kim     /* decrypt in place in 'rr->input' */
1569e71b7053SJung-uk Kim     rr->data = rr->input;
1570e71b7053SJung-uk Kim     rr->orig_len = rr->length;
1571e71b7053SJung-uk Kim 
1572b077aed3SPierre Pronchery     if (s->read_hash != NULL) {
1573b077aed3SPierre Pronchery         const EVP_MD *tmpmd = EVP_MD_CTX_get0_md(s->read_hash);
1574b077aed3SPierre Pronchery 
1575b077aed3SPierre Pronchery         if (tmpmd != NULL) {
1576b077aed3SPierre Pronchery             imac_size = EVP_MD_get_size(tmpmd);
1577b077aed3SPierre Pronchery             if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
1578b077aed3SPierre Pronchery                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
1579e71b7053SJung-uk Kim                     return 0;
1580e71b7053SJung-uk Kim             }
1581b077aed3SPierre Pronchery             mac_size = (size_t)imac_size;
1582b077aed3SPierre Pronchery         }
1583b077aed3SPierre Pronchery     }
1584b077aed3SPierre Pronchery 
1585b077aed3SPierre Pronchery     if (SSL_READ_ETM(s) && s->read_hash) {
1586b077aed3SPierre Pronchery         unsigned char *mac;
1587b077aed3SPierre Pronchery 
1588e71b7053SJung-uk Kim         if (rr->orig_len < mac_size) {
1589b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
1590e71b7053SJung-uk Kim             return 0;
1591e71b7053SJung-uk Kim         }
1592e71b7053SJung-uk Kim         rr->length -= mac_size;
1593e71b7053SJung-uk Kim         mac = rr->data + rr->length;
1594e71b7053SJung-uk Kim         i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1595e71b7053SJung-uk Kim         if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1596b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
1597e71b7053SJung-uk Kim                      SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1598e71b7053SJung-uk Kim             return 0;
1599e71b7053SJung-uk Kim         }
1600b077aed3SPierre Pronchery         /*
1601b077aed3SPierre Pronchery          * We've handled the mac now - there is no MAC inside the encrypted
1602b077aed3SPierre Pronchery          * record
1603b077aed3SPierre Pronchery          */
1604b077aed3SPierre Pronchery         mac_size = 0;
1605e71b7053SJung-uk Kim     }
1606e71b7053SJung-uk Kim 
1607b077aed3SPierre Pronchery     /*
1608b077aed3SPierre Pronchery      * Set a mark around the packet decryption attempt.  This is DTLS, so
1609b077aed3SPierre Pronchery      * bad packets are just ignored, and we don't want to leave stray
1610b077aed3SPierre Pronchery      * errors in the queue from processing bogus junk that we ignored.
1611b077aed3SPierre Pronchery      */
1612b077aed3SPierre Pronchery     ERR_set_mark();
1613b077aed3SPierre Pronchery     enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0, &macbuf, mac_size);
1614b077aed3SPierre Pronchery 
1615e71b7053SJung-uk Kim     /*-
1616e71b7053SJung-uk Kim      * enc_err is:
1617b077aed3SPierre Pronchery      *    0: if the record is publicly invalid, or an internal error, or AEAD
1618b077aed3SPierre Pronchery      *       decryption failed, or ETM decryption failed.
1619b077aed3SPierre Pronchery      *    1: Success or MTE decryption failed (MAC will be randomised)
1620e71b7053SJung-uk Kim      */
1621e71b7053SJung-uk Kim     if (enc_err == 0) {
1622b077aed3SPierre Pronchery         ERR_pop_to_mark();
1623e71b7053SJung-uk Kim         if (ossl_statem_in_error(s)) {
1624e71b7053SJung-uk Kim             /* SSLfatal() got called */
1625b077aed3SPierre Pronchery             goto end;
1626e71b7053SJung-uk Kim         }
1627e71b7053SJung-uk Kim         /* For DTLS we simply ignore bad packets. */
1628e71b7053SJung-uk Kim         rr->length = 0;
1629e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer);
1630b077aed3SPierre Pronchery         goto end;
1631e71b7053SJung-uk Kim     }
1632b077aed3SPierre Pronchery     ERR_clear_last_mark();
1633b077aed3SPierre Pronchery     OSSL_TRACE_BEGIN(TLS) {
1634b077aed3SPierre Pronchery         BIO_printf(trc_out, "dec %zd\n", rr->length);
1635b077aed3SPierre Pronchery         BIO_dump_indent(trc_out, rr->data, rr->length, 4);
1636b077aed3SPierre Pronchery     } OSSL_TRACE_END(TLS);
1637e71b7053SJung-uk Kim 
1638e71b7053SJung-uk Kim     /* r->length is now the compressed data plus mac */
1639b077aed3SPierre Pronchery     if ((sess != NULL)
1640b077aed3SPierre Pronchery             && !SSL_READ_ETM(s)
1641b077aed3SPierre Pronchery             && (s->enc_read_ctx != NULL)
1642b077aed3SPierre Pronchery             && (EVP_MD_CTX_get0_md(s->read_hash) != NULL)) {
1643e71b7053SJung-uk Kim         /* s->read_hash != NULL => mac_size != -1 */
1644e71b7053SJung-uk Kim 
1645e71b7053SJung-uk Kim         i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1646b077aed3SPierre Pronchery         if (i == 0 || macbuf.mac == NULL
1647b077aed3SPierre Pronchery             || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
1648b077aed3SPierre Pronchery             enc_err = 0;
1649e71b7053SJung-uk Kim         if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1650b077aed3SPierre Pronchery             enc_err = 0;
1651e71b7053SJung-uk Kim     }
1652e71b7053SJung-uk Kim 
1653b077aed3SPierre Pronchery     if (enc_err == 0) {
1654e71b7053SJung-uk Kim         /* decryption failed, silently discard message */
1655e71b7053SJung-uk Kim         rr->length = 0;
1656e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer);
1657b077aed3SPierre Pronchery         goto end;
1658e71b7053SJung-uk Kim     }
1659e71b7053SJung-uk Kim 
1660e71b7053SJung-uk Kim     /* r->length is now just compressed */
1661e71b7053SJung-uk Kim     if (s->expand != NULL) {
1662e71b7053SJung-uk Kim         if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1663b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
1664e71b7053SJung-uk Kim                      SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1665b077aed3SPierre Pronchery             goto end;
1666e71b7053SJung-uk Kim         }
1667e71b7053SJung-uk Kim         if (!ssl3_do_uncompress(s, rr)) {
1668b077aed3SPierre Pronchery             SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_R_BAD_DECOMPRESSION);
1669b077aed3SPierre Pronchery             goto end;
1670e71b7053SJung-uk Kim         }
1671e71b7053SJung-uk Kim     }
1672e71b7053SJung-uk Kim 
167317f01e99SJung-uk Kim     /* use current Max Fragment Length setting if applicable */
167417f01e99SJung-uk Kim     if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
167517f01e99SJung-uk Kim         max_plain_length = GET_MAX_FRAGMENT_LENGTH(s->session);
167617f01e99SJung-uk Kim 
167717f01e99SJung-uk Kim     /* send overflow if the plaintext is too long now it has passed MAC */
167817f01e99SJung-uk Kim     if (rr->length > max_plain_length) {
1679b077aed3SPierre Pronchery         SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
1680b077aed3SPierre Pronchery         goto end;
1681e71b7053SJung-uk Kim     }
1682e71b7053SJung-uk Kim 
1683e71b7053SJung-uk Kim     rr->off = 0;
1684e71b7053SJung-uk Kim     /*-
1685e71b7053SJung-uk Kim      * So at this point the following is true
1686b077aed3SPierre Pronchery      * ssl->s3.rrec.type   is the type of record
1687b077aed3SPierre Pronchery      * ssl->s3.rrec.length == number of bytes in record
1688b077aed3SPierre Pronchery      * ssl->s3.rrec.off    == offset to first valid byte
1689b077aed3SPierre Pronchery      * ssl->s3.rrec.data   == where to take bytes from, increment
1690e71b7053SJung-uk Kim      *                        after use :-).
1691e71b7053SJung-uk Kim      */
1692e71b7053SJung-uk Kim 
1693e71b7053SJung-uk Kim     /* we have pulled in a full packet so zero things */
1694e71b7053SJung-uk Kim     RECORD_LAYER_reset_packet_length(&s->rlayer);
1695e71b7053SJung-uk Kim 
1696e71b7053SJung-uk Kim     /* Mark receipt of record. */
1697e71b7053SJung-uk Kim     dtls1_record_bitmap_update(s, bitmap);
1698e71b7053SJung-uk Kim 
1699b077aed3SPierre Pronchery     ret = 1;
1700b077aed3SPierre Pronchery  end:
1701b077aed3SPierre Pronchery     if (macbuf.alloced)
1702b077aed3SPierre Pronchery         OPENSSL_free(macbuf.mac);
1703b077aed3SPierre Pronchery     return ret;
1704e71b7053SJung-uk Kim }
1705e71b7053SJung-uk Kim 
1706e71b7053SJung-uk Kim /*
1707e71b7053SJung-uk Kim  * Retrieve a buffered record that belongs to the current epoch, i.e. processed
1708e71b7053SJung-uk Kim  */
1709e71b7053SJung-uk Kim #define dtls1_get_processed_record(s) \
1710e71b7053SJung-uk Kim                    dtls1_retrieve_buffered_record((s), \
1711e71b7053SJung-uk Kim                    &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1712e71b7053SJung-uk Kim 
1713e71b7053SJung-uk Kim /*-
1714e71b7053SJung-uk Kim  * Call this to get a new input record.
1715e71b7053SJung-uk Kim  * It will return <= 0 if more data is needed, normally due to an error
1716e71b7053SJung-uk Kim  * or non-blocking IO.
1717e71b7053SJung-uk Kim  * When it finishes, one packet has been decoded and can be found in
1718b077aed3SPierre Pronchery  * ssl->s3.rrec.type    - is the type of record
1719b077aed3SPierre Pronchery  * ssl->s3.rrec.data    - data
1720b077aed3SPierre Pronchery  * ssl->s3.rrec.length  - number of bytes
1721e71b7053SJung-uk Kim  */
1722e71b7053SJung-uk Kim /* used only by dtls1_read_bytes */
dtls1_get_record(SSL * s)1723e71b7053SJung-uk Kim int dtls1_get_record(SSL *s)
1724e71b7053SJung-uk Kim {
1725e71b7053SJung-uk Kim     int ssl_major, ssl_minor;
1726e71b7053SJung-uk Kim     int rret;
1727e71b7053SJung-uk Kim     size_t more, n;
1728e71b7053SJung-uk Kim     SSL3_RECORD *rr;
1729e71b7053SJung-uk Kim     unsigned char *p = NULL;
1730e71b7053SJung-uk Kim     unsigned short version;
1731e71b7053SJung-uk Kim     DTLS1_BITMAP *bitmap;
1732e71b7053SJung-uk Kim     unsigned int is_next_epoch;
1733e71b7053SJung-uk Kim 
1734e71b7053SJung-uk Kim     rr = RECORD_LAYER_get_rrec(&s->rlayer);
1735e71b7053SJung-uk Kim 
1736e71b7053SJung-uk Kim  again:
1737e71b7053SJung-uk Kim     /*
1738e71b7053SJung-uk Kim      * The epoch may have changed.  If so, process all the pending records.
1739e71b7053SJung-uk Kim      * This is a non-blocking operation.
1740e71b7053SJung-uk Kim      */
1741e71b7053SJung-uk Kim     if (!dtls1_process_buffered_records(s)) {
1742e71b7053SJung-uk Kim         /* SSLfatal() already called */
1743e71b7053SJung-uk Kim         return -1;
1744e71b7053SJung-uk Kim     }
1745e71b7053SJung-uk Kim 
1746e71b7053SJung-uk Kim     /* if we're renegotiating, then there may be buffered records */
1747e71b7053SJung-uk Kim     if (dtls1_get_processed_record(s))
1748e71b7053SJung-uk Kim         return 1;
1749e71b7053SJung-uk Kim 
1750e71b7053SJung-uk Kim     /* get something from the wire */
1751e71b7053SJung-uk Kim 
1752e71b7053SJung-uk Kim     /* check if we have the header */
1753e71b7053SJung-uk Kim     if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1754e71b7053SJung-uk Kim         (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1755e71b7053SJung-uk Kim         rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1756e71b7053SJung-uk Kim                            SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1757e71b7053SJung-uk Kim         /* read timeout is handled by dtls1_read_bytes */
1758e71b7053SJung-uk Kim         if (rret <= 0) {
1759e71b7053SJung-uk Kim             /* SSLfatal() already called if appropriate */
1760e71b7053SJung-uk Kim             return rret;         /* error or non-blocking */
1761e71b7053SJung-uk Kim         }
1762e71b7053SJung-uk Kim 
1763e71b7053SJung-uk Kim         /* this packet contained a partial record, dump it */
1764e71b7053SJung-uk Kim         if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1765e71b7053SJung-uk Kim             DTLS1_RT_HEADER_LENGTH) {
1766e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer);
1767e71b7053SJung-uk Kim             goto again;
1768e71b7053SJung-uk Kim         }
1769e71b7053SJung-uk Kim 
1770e71b7053SJung-uk Kim         RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1771e71b7053SJung-uk Kim 
1772e71b7053SJung-uk Kim         p = RECORD_LAYER_get_packet(&s->rlayer);
1773e71b7053SJung-uk Kim 
1774e71b7053SJung-uk Kim         if (s->msg_callback)
1775e71b7053SJung-uk Kim             s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1776e71b7053SJung-uk Kim                             s, s->msg_callback_arg);
1777e71b7053SJung-uk Kim 
1778e71b7053SJung-uk Kim         /* Pull apart the header into the DTLS1_RECORD */
1779e71b7053SJung-uk Kim         rr->type = *(p++);
1780e71b7053SJung-uk Kim         ssl_major = *(p++);
1781e71b7053SJung-uk Kim         ssl_minor = *(p++);
1782e71b7053SJung-uk Kim         version = (ssl_major << 8) | ssl_minor;
1783e71b7053SJung-uk Kim 
1784e71b7053SJung-uk Kim         /* sequence number is 64 bits, with top 2 bytes = epoch */
1785e71b7053SJung-uk Kim         n2s(p, rr->epoch);
1786e71b7053SJung-uk Kim 
1787e71b7053SJung-uk Kim         memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1788e71b7053SJung-uk Kim         p += 6;
1789e71b7053SJung-uk Kim 
1790e71b7053SJung-uk Kim         n2s(p, rr->length);
1791e71b7053SJung-uk Kim         rr->read = 0;
1792e71b7053SJung-uk Kim 
1793e71b7053SJung-uk Kim         /*
1794e71b7053SJung-uk Kim          * Lets check the version. We tolerate alerts that don't have the exact
1795e71b7053SJung-uk Kim          * version number (e.g. because of protocol version errors)
1796e71b7053SJung-uk Kim          */
1797e71b7053SJung-uk Kim         if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
1798e71b7053SJung-uk Kim             if (version != s->version) {
1799e71b7053SJung-uk Kim                 /* unexpected version, silently discard */
1800e71b7053SJung-uk Kim                 rr->length = 0;
1801e71b7053SJung-uk Kim                 rr->read = 1;
1802e71b7053SJung-uk Kim                 RECORD_LAYER_reset_packet_length(&s->rlayer);
1803e71b7053SJung-uk Kim                 goto again;
1804e71b7053SJung-uk Kim             }
1805e71b7053SJung-uk Kim         }
1806e71b7053SJung-uk Kim 
1807e71b7053SJung-uk Kim         if ((version & 0xff00) != (s->version & 0xff00)) {
1808e71b7053SJung-uk Kim             /* wrong version, silently discard record */
1809e71b7053SJung-uk Kim             rr->length = 0;
1810e71b7053SJung-uk Kim             rr->read = 1;
1811e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer);
1812e71b7053SJung-uk Kim             goto again;
1813e71b7053SJung-uk Kim         }
1814e71b7053SJung-uk Kim 
1815e71b7053SJung-uk Kim         if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1816e71b7053SJung-uk Kim             /* record too long, silently discard it */
1817e71b7053SJung-uk Kim             rr->length = 0;
1818e71b7053SJung-uk Kim             rr->read = 1;
1819e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer);
1820e71b7053SJung-uk Kim             goto again;
1821e71b7053SJung-uk Kim         }
1822e71b7053SJung-uk Kim 
1823e71b7053SJung-uk Kim         /* If received packet overflows own-client Max Fragment Length setting */
1824e71b7053SJung-uk Kim         if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
182517f01e99SJung-uk Kim                 && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session) + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
1826e71b7053SJung-uk Kim             /* record too long, silently discard it */
1827e71b7053SJung-uk Kim             rr->length = 0;
1828e71b7053SJung-uk Kim             rr->read = 1;
1829e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer);
1830e71b7053SJung-uk Kim             goto again;
1831e71b7053SJung-uk Kim         }
1832e71b7053SJung-uk Kim 
1833e71b7053SJung-uk Kim         /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1834e71b7053SJung-uk Kim     }
1835e71b7053SJung-uk Kim 
1836e71b7053SJung-uk Kim     /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1837e71b7053SJung-uk Kim 
1838e71b7053SJung-uk Kim     if (rr->length >
1839e71b7053SJung-uk Kim         RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
18409a3ae0cdSJung-uk Kim         /* now s->rlayer.packet_length == DTLS1_RT_HEADER_LENGTH */
1841e71b7053SJung-uk Kim         more = rr->length;
1842e71b7053SJung-uk Kim         rret = ssl3_read_n(s, more, more, 1, 1, &n);
1843e71b7053SJung-uk Kim         /* this packet contained a partial record, dump it */
1844e71b7053SJung-uk Kim         if (rret <= 0 || n != more) {
1845e71b7053SJung-uk Kim             if (ossl_statem_in_error(s)) {
1846e71b7053SJung-uk Kim                 /* ssl3_read_n() called SSLfatal() */
1847e71b7053SJung-uk Kim                 return -1;
1848e71b7053SJung-uk Kim             }
1849e71b7053SJung-uk Kim             rr->length = 0;
1850e71b7053SJung-uk Kim             rr->read = 1;
1851e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer);
1852e71b7053SJung-uk Kim             goto again;
1853e71b7053SJung-uk Kim         }
1854e71b7053SJung-uk Kim 
1855e71b7053SJung-uk Kim         /*
18569a3ae0cdSJung-uk Kim          * now n == rr->length, and s->rlayer.packet_length ==
1857e71b7053SJung-uk Kim          * DTLS1_RT_HEADER_LENGTH + rr->length
1858e71b7053SJung-uk Kim          */
1859e71b7053SJung-uk Kim     }
1860e71b7053SJung-uk Kim     /* set state for later operations */
1861e71b7053SJung-uk Kim     RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1862e71b7053SJung-uk Kim 
1863e71b7053SJung-uk Kim     /* match epochs.  NULL means the packet is dropped on the floor */
1864e71b7053SJung-uk Kim     bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1865e71b7053SJung-uk Kim     if (bitmap == NULL) {
1866e71b7053SJung-uk Kim         rr->length = 0;
1867e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1868e71b7053SJung-uk Kim         goto again;             /* get another record */
1869e71b7053SJung-uk Kim     }
1870e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1871e71b7053SJung-uk Kim     /* Only do replay check if no SCTP bio */
1872e71b7053SJung-uk Kim     if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1873e71b7053SJung-uk Kim #endif
1874e71b7053SJung-uk Kim         /* Check whether this is a repeat, or aged record. */
1875e71b7053SJung-uk Kim         if (!dtls1_record_replay_check(s, bitmap)) {
1876e71b7053SJung-uk Kim             rr->length = 0;
1877e71b7053SJung-uk Kim             rr->read = 1;
1878e71b7053SJung-uk Kim             RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1879e71b7053SJung-uk Kim             goto again;         /* get another record */
1880e71b7053SJung-uk Kim         }
1881e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SCTP
1882e71b7053SJung-uk Kim     }
1883e71b7053SJung-uk Kim #endif
1884e71b7053SJung-uk Kim 
1885e71b7053SJung-uk Kim     /* just read a 0 length packet */
1886e71b7053SJung-uk Kim     if (rr->length == 0) {
1887e71b7053SJung-uk Kim         rr->read = 1;
1888e71b7053SJung-uk Kim         goto again;
1889e71b7053SJung-uk Kim     }
1890e71b7053SJung-uk Kim 
1891e71b7053SJung-uk Kim     /*
1892e71b7053SJung-uk Kim      * If this record is from the next epoch (either HM or ALERT), and a
1893e71b7053SJung-uk Kim      * handshake is currently in progress, buffer it since it cannot be
1894e71b7053SJung-uk Kim      * processed at this time.
1895e71b7053SJung-uk Kim      */
1896e71b7053SJung-uk Kim     if (is_next_epoch) {
1897e71b7053SJung-uk Kim         if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1898e71b7053SJung-uk Kim             if (dtls1_buffer_record (s,
1899e71b7053SJung-uk Kim                     &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1900e71b7053SJung-uk Kim                     rr->seq_num) < 0) {
1901e71b7053SJung-uk Kim                 /* SSLfatal() already called */
1902e71b7053SJung-uk Kim                 return -1;
1903e71b7053SJung-uk Kim             }
1904e71b7053SJung-uk Kim         }
1905e71b7053SJung-uk Kim         rr->length = 0;
1906e71b7053SJung-uk Kim         rr->read = 1;
1907e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer);
1908e71b7053SJung-uk Kim         goto again;
1909e71b7053SJung-uk Kim     }
1910e71b7053SJung-uk Kim 
1911e71b7053SJung-uk Kim     if (!dtls1_process_record(s, bitmap)) {
1912e71b7053SJung-uk Kim         if (ossl_statem_in_error(s)) {
1913e71b7053SJung-uk Kim             /* dtls1_process_record() called SSLfatal */
1914e71b7053SJung-uk Kim             return -1;
1915e71b7053SJung-uk Kim         }
1916e71b7053SJung-uk Kim         rr->length = 0;
1917e71b7053SJung-uk Kim         rr->read = 1;
1918e71b7053SJung-uk Kim         RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1919e71b7053SJung-uk Kim         goto again;             /* get another record */
1920e71b7053SJung-uk Kim     }
1921e71b7053SJung-uk Kim 
1922e71b7053SJung-uk Kim     return 1;
1923e71b7053SJung-uk Kim 
1924e71b7053SJung-uk Kim }
1925c9cf7b5cSJung-uk Kim 
dtls_buffer_listen_record(SSL * s,size_t len,unsigned char * seq,size_t off)1926c9cf7b5cSJung-uk Kim int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
1927c9cf7b5cSJung-uk Kim {
1928c9cf7b5cSJung-uk Kim     SSL3_RECORD *rr;
1929c9cf7b5cSJung-uk Kim 
1930c9cf7b5cSJung-uk Kim     rr = RECORD_LAYER_get_rrec(&s->rlayer);
1931c9cf7b5cSJung-uk Kim     memset(rr, 0, sizeof(SSL3_RECORD));
1932c9cf7b5cSJung-uk Kim 
1933c9cf7b5cSJung-uk Kim     rr->length = len;
1934c9cf7b5cSJung-uk Kim     rr->type = SSL3_RT_HANDSHAKE;
1935c9cf7b5cSJung-uk Kim     memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
1936c9cf7b5cSJung-uk Kim     rr->off = off;
1937c9cf7b5cSJung-uk Kim 
1938c9cf7b5cSJung-uk Kim     s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
1939c9cf7b5cSJung-uk Kim     s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
1940c9cf7b5cSJung-uk Kim     rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
1941c9cf7b5cSJung-uk Kim 
1942c9cf7b5cSJung-uk Kim     if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
1943c9cf7b5cSJung-uk Kim                             SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
1944c9cf7b5cSJung-uk Kim         /* SSLfatal() already called */
1945c9cf7b5cSJung-uk Kim         return 0;
1946c9cf7b5cSJung-uk Kim     }
1947c9cf7b5cSJung-uk Kim 
1948c9cf7b5cSJung-uk Kim     return 1;
1949c9cf7b5cSJung-uk Kim }
1950