blob: 5bddf9809c75a25e00754fe184cc10d42601490c [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <assert.h>
Adam Langley87750b42014-06-20 12:00:00 -0700110#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400112#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113
114#include <openssl/buf.h>
115#include <openssl/err.h>
116#include <openssl/evp.h>
117#include <openssl/mem.h>
118#include <openssl/rand.h>
119
David Benjamin2ee94aa2015-04-07 22:38:30 -0400120#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121
Adam Langleyfcf25832014-12-18 17:42:32 -0800122
David Benjaminb8d28cf2015-07-28 21:34:45 -0400123static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len);
Adam Langley95c29f32014-06-20 12:00:00 -0700124
David Benjamina8ebe222015-06-06 03:04:39 -0400125/* kMaxEmptyRecords is the number of consecutive, empty records that will be
126 * processed. Without this limit an attacker could send empty records at a
David Benjaminb8d28cf2015-07-28 21:34:45 -0400127 * faster rate than we can process and cause record processing to loop
David Benjamina8ebe222015-06-06 03:04:39 -0400128 * forever. */
129static const uint8_t kMaxEmptyRecords = 32;
Adam Langley48105fa2014-06-20 12:00:00 -0700130
David Benjamin24f346d2015-06-06 03:28:08 -0400131/* kMaxWarningAlerts is the number of consecutive warning alerts that will be
132 * processed. */
133static const uint8_t kMaxWarningAlerts = 4;
134
David Benjaminb8d28cf2015-07-28 21:34:45 -0400135/* ssl3_get_record reads a new input record. On success, it places it in
136 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
137 * more data is needed. */
138static int ssl3_get_record(SSL *ssl) {
139 int ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700140again:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400141 /* Ensure the buffer is large enough to decrypt in-place. */
142 ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
143 if (ret <= 0) {
144 return ret;
145 }
146 assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjaminb8d28cf2015-07-28 21:34:45 -0400148 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
149 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
150 uint8_t type, alert;
151 size_t len, consumed;
152 switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
153 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
154 case ssl_open_record_success:
155 ssl_read_buffer_consume(ssl, consumed);
David Benjamin6a08da22015-05-08 22:58:12 -0400156
David Benjaminb8d28cf2015-07-28 21:34:45 -0400157 /* Discard empty records.
158 * TODO(davidben): This logic should be moved to a higher level. See
159 * https://crbug.com/521840. */
160 if (len == 0) {
161 ssl->s3->empty_record_count++;
162 if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
163 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
164 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
165 return -1;
166 }
167 goto again;
168 }
169 ssl->s3->empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjaminb8d28cf2015-07-28 21:34:45 -0400171 if (len > 0xffff) {
172 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
173 return -1;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjaminb8d28cf2015-07-28 21:34:45 -0400176 SSL3_RECORD *rr = &ssl->s3->rrec;
177 rr->type = type;
178 rr->length = (uint16_t)len;
179 rr->off = 0;
180 rr->data = out;
181 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjaminb8d28cf2015-07-28 21:34:45 -0400183 case ssl_open_record_partial:
184 ret = ssl_read_buffer_extend_to(ssl, consumed);
185 if (ret <= 0) {
186 return ret;
187 }
188 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjaminb8d28cf2015-07-28 21:34:45 -0400190 case ssl_open_record_discard:
191 ssl_read_buffer_consume(ssl, consumed);
192 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjaminb8d28cf2015-07-28 21:34:45 -0400194 case ssl_open_record_error:
195 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
196 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjaminb8d28cf2015-07-28 21:34:45 -0400199 assert(0);
200 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
201 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800202}
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjaminc933a472015-05-30 16:14:58 -0400204int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
205 return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
206}
207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208/* Call this to write data in records of type |type|. It will return <= 0 if
209 * not all data has been sent or non-blocking IO. */
210int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
211 const uint8_t *buf = buf_;
212 unsigned int tot, n, nw;
213 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 s->rwstate = SSL_NOTHING;
216 assert(s->s3->wnum <= INT_MAX);
217 tot = s->s3->wnum;
218 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamined7c4752015-02-16 19:16:46 -0500220 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 i = s->handshake_func(s);
222 if (i < 0) {
223 return i;
224 }
225 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400226 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 return -1;
228 }
229 }
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 /* Ensure that if we end up with a smaller value of data to write out than
232 * the the original len from a write which didn't complete for non-blocking
233 * I/O and also somehow ended up avoiding the check for this in
234 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
235 * end up with (len-tot) as a large number that will then promptly send
236 * beyond the end of the users buffer ... so we trap and report the error in
237 * a way the user will notice. */
238 if (len < 0 || (size_t)len < tot) {
David Benjamin3570d732015-06-29 00:28:17 -0400239 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return -1;
241 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 n = (len - tot);
244 for (;;) {
245 /* max contains the maximum number of bytes that we can put into a
246 * record. */
247 unsigned max = s->max_send_fragment;
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (n > max) {
249 nw = max;
250 } else {
251 nw = n;
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjaminb8d28cf2015-07-28 21:34:45 -0400254 i = do_ssl3_write(s, type, &buf[tot], nw);
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 if (i <= 0) {
256 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 return i;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
261 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 return tot + i;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 n -= i;
266 tot += i;
267 }
268}
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjaminb8d28cf2015-07-28 21:34:45 -0400270/* do_ssl3_write writes an SSL record of the given type. */
271static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len) {
272 /* If there is still data from the previous record, flush it. */
273 if (ssl_write_buffer_is_pending(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 return ssl3_write_pending(s, type, buf, len);
275 }
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 /* If we have an alert to send, lets send it */
278 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400279 int ret = s->method->ssl_dispatch_alert(s);
280 if (ret <= 0) {
281 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 }
283 /* if it went, fall through and send more stuff */
284 }
Adam Langley95c29f32014-06-20 12:00:00 -0700285
David Benjaminb8d28cf2015-07-28 21:34:45 -0400286 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
287 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 return -1;
289 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 if (len == 0) {
292 return 0;
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294
David Benjaminb8d28cf2015-07-28 21:34:45 -0400295 size_t max_out = len + ssl_max_seal_overhead(s);
296 if (max_out < len) {
297 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin9faafda2015-04-04 19:23:10 -0400298 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400300 uint8_t *out;
301 size_t ciphertext_len;
302 if (!ssl_write_buffer_init(s, &out, max_out) ||
303 !tls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
304 return -1;
305 }
306 ssl_write_buffer_set_len(s, ciphertext_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 /* memorize arguments so that ssl3_write_pending can detect bad write retries
309 * later */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400310 s->s3->wpend_tot = len;
311 s->s3->wpend_buf = buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 s->s3->wpend_type = type;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400313 s->s3->wpend_ret = len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800314
315 /* we now just need to write the buffer */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400316 return ssl3_write_pending(s, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800317}
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 if (s->s3->wpend_tot > (int)len ||
321 (s->s3->wpend_buf != buf &&
322 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
323 s->s3->wpend_type != type) {
David Benjamin3570d732015-06-29 00:28:17 -0400324 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 return -1;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
David Benjaminb8d28cf2015-07-28 21:34:45 -0400328 int ret = ssl_write_buffer_flush(s);
329 if (ret <= 0) {
330 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400332 return s->s3->wpend_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800333}
Adam Langley95c29f32014-06-20 12:00:00 -0700334
David Benjamin86271ee2014-07-21 16:14:03 -0400335/* ssl3_expect_change_cipher_spec informs the record layer that a
336 * ChangeCipherSpec record is required at this point. If a Handshake record is
337 * received before ChangeCipherSpec, the connection will fail. Moreover, if
338 * there are unprocessed handshake bytes, the handshake will also fail and the
339 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800340int ssl3_expect_change_cipher_spec(SSL *s) {
341 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
David Benjamin3570d732015-06-29 00:28:17 -0400342 OPENSSL_PUT_ERROR(SSL, SSL_R_UNPROCESSED_HANDSHAKE_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 return 0;
344 }
345
346 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
347 return 1;
348}
David Benjamin86271ee2014-07-21 16:14:03 -0400349
David Benjamina6022772015-05-30 16:22:10 -0400350int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
351 return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
352}
353
354void ssl3_read_close_notify(SSL *ssl) {
355 ssl3_read_bytes(ssl, 0, NULL, 0, 0);
356}
357
Adam Langley95c29f32014-06-20 12:00:00 -0700358/* Return up to 'len' payload bytes received in 'type' records.
359 * 'type' is one of the following:
360 *
361 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
362 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
363 * - 0 (during a shutdown, no data has to be returned)
364 *
365 * If we don't have stored data to work from, read a SSL/TLS record first
366 * (possibly multiple records if we still don't have anything to return).
367 *
368 * This function must handle any surprises the peer may have for us, such as
369 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
370 * a surprise, but handled as if it were), or renegotiation requests.
371 * Also if record payloads contain fragments too small to process, we store
372 * them until there is enough for the respective protocol (the record protocol
373 * may use arbitrary fragmentation and even interleaving):
374 * Change cipher spec protocol
375 * just 1 byte needed, no need for keeping anything stored
376 * Alert protocol
377 * 2 bytes needed (AlertLevel, AlertDescription)
378 * Handshake protocol
379 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
380 * to detect unexpected Client Hello and Hello Request messages
381 * here, anything else is handled by higher layers
382 * Application data protocol
383 * none of our business
384 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800385int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500386 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 unsigned int n;
388 SSL3_RECORD *rr;
389 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
392 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400393 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return -1;
395 }
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
398 /* (partially) satisfy request from storage */
399 uint8_t *src = s->s3->handshake_fragment;
400 uint8_t *dst = buf;
401 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 /* peek == 0 */
404 n = 0;
405 while (len > 0 && s->s3->handshake_fragment_len > 0) {
406 *dst++ = *src++;
407 len--;
408 s->s3->handshake_fragment_len--;
409 n++;
410 }
411 /* move any remaining fragment bytes: */
412 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
413 s->s3->handshake_fragment[k] = *src++;
414 }
415 return n;
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700419
David Benjamin931ab342015-02-08 19:46:57 -0500420 /* This may require multiple iterations. False Start will cause
421 * |s->handshake_func| to signal success one step early, but the handshake
422 * must be completely finished before other modes are accepted.
423 *
424 * TODO(davidben): Move this check up to a higher level. */
425 while (!s->in_handshake && SSL_in_init(s)) {
426 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 i = s->handshake_func(s);
428 if (i < 0) {
429 return i;
430 }
431 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400432 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 return -1;
434 }
435 }
436
Adam Langley95c29f32014-06-20 12:00:00 -0700437start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 /* s->s3->rrec.type - is the type of record
441 * s->s3->rrec.data - data
442 * s->s3->rrec.off - offset into 'data' for next read
443 * s->s3->rrec.length - number of bytes. */
444 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400447 if (rr->length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 ret = ssl3_get_record(s);
449 if (ret <= 0) {
450 return ret;
451 }
452 }
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700455
David Benjamindc3da932015-03-12 15:09:02 -0400456 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
457 * ssl3_get_finished. */
458 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
459 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400461 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 goto f_err;
463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
466 * Handshake record. */
467 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
468 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400469 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 goto f_err;
471 }
David Benjamin86271ee2014-07-21 16:14:03 -0400472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 /* If the other end has shut down, throw anything we read away (even in
474 * 'peek' mode) */
475 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
476 rr->length = 0;
477 s->rwstate = SSL_NOTHING;
478 return 0;
479 }
Adam Langley95c29f32014-06-20 12:00:00 -0700480
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 if (type == rr->type) {
David Benjamin24f346d2015-06-06 03:28:08 -0400482 s->s3->warning_alert_count = 0;
483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
485 /* make sure that we are not getting application data when we are doing a
486 * handshake for the first time */
487 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500488 s->aead_read_ctx == NULL) {
489 /* TODO(davidben): Is this check redundant with the handshake_func
490 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400492 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 goto f_err;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 if (len <= 0) {
497 return len;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 if ((unsigned int)len > rr->length) {
501 n = rr->length;
502 } else {
503 n = (unsigned int)len;
504 }
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 memcpy(buf, &(rr->data[rr->off]), n);
507 if (!peek) {
508 rr->length -= n;
509 rr->off += n;
510 if (rr->length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 rr->off = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400512 /* The record has been consumed, so we may now clear the buffer. */
513 ssl_read_buffer_discard(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 }
515 }
516
517 return n;
518 }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin44d3eed2015-05-21 01:29:55 -0400520 /* Process unexpected records. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400523 /* If peer renegotiations are disabled, all out-of-order handshake records
David Benjamin44d3eed2015-05-21 01:29:55 -0400524 * are fatal. Renegotiations as a server are never supported. */
525 if (!s->accept_peer_renegotiations || s->server) {
David Benjaminb16346b2015-04-08 19:16:58 -0400526 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjaminb16346b2015-04-08 19:16:58 -0400528 goto f_err;
529 }
530
David Benjamin44d3eed2015-05-21 01:29:55 -0400531 /* HelloRequests may be fragmented across multiple records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 const size_t size = sizeof(s->s3->handshake_fragment);
533 const size_t avail = size - s->s3->handshake_fragment_len;
534 const size_t todo = (rr->length < avail) ? rr->length : avail;
535 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
536 &rr->data[rr->off], todo);
537 rr->off += todo;
538 rr->length -= todo;
539 s->s3->handshake_fragment_len += todo;
540 if (s->s3->handshake_fragment_len < size) {
541 goto start; /* fragment was too small */
542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin44d3eed2015-05-21 01:29:55 -0400544 /* Parse out and consume a HelloRequest. */
545 if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST ||
546 s->s3->handshake_fragment[1] != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 s->s3->handshake_fragment[2] != 0 ||
548 s->s3->handshake_fragment[3] != 0) {
549 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 goto f_err;
552 }
David Benjamin44d3eed2015-05-21 01:29:55 -0400553 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700554
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 if (s->msg_callback) {
556 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
557 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamincd90f3a2015-05-21 01:37:55 -0400560 if (!SSL_is_init_finished(s) || !s->s3->initial_handshake_complete) {
561 /* This cannot happen. If a handshake is in progress, |type| must be
562 * |SSL3_RT_HANDSHAKE|. */
563 assert(0);
David Benjamin3570d732015-06-29 00:28:17 -0400564 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincd90f3a2015-05-21 01:37:55 -0400565 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 }
David Benjamincd90f3a2015-05-21 01:37:55 -0400567
568 /* Renegotiation is only supported at quiescent points in the application
569 * protocol, namely in HTTPS, just before reading the HTTP response. Require
570 * the record-layer be idle and avoid complexities of sending a handshake
571 * record while an application_data record is being written. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400572 if (ssl_write_buffer_is_pending(s)) {
David Benjamincd90f3a2015-05-21 01:37:55 -0400573 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400574 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjamincd90f3a2015-05-21 01:37:55 -0400575 goto f_err;
576 }
577
578 /* Begin a new handshake. */
579 s->state = SSL_ST_CONNECT;
580 i = s->handshake_func(s);
581 if (i < 0) {
582 return i;
583 }
584 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400585 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
David Benjamincd90f3a2015-05-21 01:37:55 -0400586 return -1;
587 }
588
589 /* The handshake completed synchronously. Continue reading records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 goto start;
591 }
David Benjaminb4188f02014-11-01 03:43:48 -0400592
David Benjamin86058a22015-02-22 13:07:21 -0500593 /* If an alert record, process one alert out of the record. Note that we allow
594 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500596 /* Alerts may not be fragmented. */
597 if (rr->length < 2) {
598 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400599 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin86058a22015-02-22 13:07:21 -0500600 goto f_err;
601 }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500604 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 s->msg_callback_arg);
606 }
David Benjamin86058a22015-02-22 13:07:21 -0500607 const uint8_t alert_level = rr->data[rr->off++];
608 const uint8_t alert_descr = rr->data[rr->off++];
609 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700610
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 if (s->info_callback != NULL) {
612 cb = s->info_callback;
613 } else if (s->ctx->info_callback != NULL) {
614 cb = s->ctx->info_callback;
615 }
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500618 uint16_t alert = (alert_level << 8) | alert_descr;
619 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 }
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjamin86058a22015-02-22 13:07:21 -0500622 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 s->s3->warn_alert = alert_descr;
624 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
625 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
626 return 0;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 /* This is a warning but we receive it if we requested renegotiation and
630 * the peer denied it. Terminate with a fatal alert because if
631 * application tried to renegotiatie it presumably had a good reason and
632 * expects it to succeed.
633 *
634 * In future we might have a renegotiation where we don't care if the
635 * peer refused it where we carry on. */
636 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
637 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400638 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 goto f_err;
640 }
David Benjamin24f346d2015-06-06 03:28:08 -0400641
642 s->s3->warning_alert_count++;
643 if (s->s3->warning_alert_count > kMaxWarningAlerts) {
644 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400645 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
David Benjamin24f346d2015-06-06 03:28:08 -0400646 goto f_err;
647 }
David Benjamin86058a22015-02-22 13:07:21 -0500648 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 s->rwstate = SSL_NOTHING;
652 s->s3->fatal_alert = alert_descr;
David Benjamin3570d732015-06-29 00:28:17 -0400653 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
655 ERR_add_error_data(2, "SSL alert number ", tmp);
656 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
657 SSL_CTX_remove_session(s->ctx, s->session);
658 return 0;
659 } else {
660 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400661 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 goto f_err;
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 goto start;
666 }
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 if (s->shutdown & SSL_SENT_SHUTDOWN) {
669 /* but we have not received a shutdown */
670 s->rwstate = SSL_NOTHING;
671 rr->length = 0;
672 return 0;
673 }
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
676 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
677 * record payload has to look like */
678 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
679 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400680 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
Adam Langleyfcf25832014-12-18 17:42:32 -0800681 goto f_err;
682 }
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 /* Check we have a cipher to change to */
685 if (s->s3->tmp.new_cipher == NULL) {
686 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400687 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 goto f_err;
689 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700690
Adam Langleyfcf25832014-12-18 17:42:32 -0800691 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
692 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400693 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 goto f_err;
695 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700696
Adam Langleyfcf25832014-12-18 17:42:32 -0800697 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langleyfcf25832014-12-18 17:42:32 -0800701 if (s->msg_callback) {
702 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
703 s->msg_callback_arg);
704 }
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 s->s3->change_cipher_spec = 1;
707 if (!ssl3_do_change_cipher_spec(s)) {
708 goto err;
709 } else {
710 goto start;
711 }
712 }
Adam Langley95c29f32014-06-20 12:00:00 -0700713
David Benjamine820df92015-02-08 16:06:54 -0500714 /* We already handled these. */
715 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
716 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -0500717
David Benjamine820df92015-02-08 16:06:54 -0500718 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400719 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -0700720
721f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700723err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 return -1;
725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langleyfcf25832014-12-18 17:42:32 -0800727int ssl3_do_change_cipher_spec(SSL *s) {
728 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700729
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 if (s->state & SSL_ST_ACCEPT) {
731 i = SSL3_CHANGE_CIPHER_SERVER_READ;
732 } else {
733 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
734 }
Adam Langley95c29f32014-06-20 12:00:00 -0700735
Adam Langleyfcf25832014-12-18 17:42:32 -0800736 if (s->s3->tmp.key_block == NULL) {
737 if (s->session == NULL || s->session->master_key_length == 0) {
738 /* might happen if dtls1_read_bytes() calls this */
David Benjamin3570d732015-06-29 00:28:17 -0400739 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 return 0;
741 }
Adam Langley95c29f32014-06-20 12:00:00 -0700742
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 s->session->cipher = s->s3->tmp.new_cipher;
744 if (!s->enc_method->setup_key_block(s)) {
745 return 0;
746 }
747 }
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 if (!s->enc_method->change_cipher_state(s, i)) {
750 return 0;
751 }
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 return 1;
754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756int ssl3_send_alert(SSL *s, int level, int desc) {
757 /* Map tls/ssl alert value to correct one */
758 desc = s->enc_method->alert_value(desc);
759 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
760 /* SSL 3.0 does not have protocol_version alerts */
761 desc = SSL_AD_HANDSHAKE_FAILURE;
762 }
763 if (desc < 0) {
764 return -1;
765 }
Adam Langley95c29f32014-06-20 12:00:00 -0700766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 /* If a fatal one, remove from cache */
768 if (level == 2 && s->session != NULL) {
769 SSL_CTX_remove_session(s->ctx, s->session);
770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 s->s3->alert_dispatch = 1;
773 s->s3->send_alert[0] = level;
774 s->s3->send_alert[1] = desc;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400775 if (!ssl_write_buffer_is_pending(s)) {
776 /* Nothing is being written out, so the alert may be dispatched
777 * immediately. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800778 return s->method->ssl_dispatch_alert(s);
779 }
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 /* else data is still being written out, we will get written some time in the
782 * future */
783 return -1;
784}
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786int ssl3_dispatch_alert(SSL *s) {
787 int i, j;
788 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 s->s3->alert_dispatch = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400791 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2);
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 if (i <= 0) {
793 s->s3->alert_dispatch = 1;
794 } else {
795 /* Alert sent to BIO. If it is important, flush it now. If the message
796 * does not get sent due to non-blocking IO, we will not worry too much. */
797 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
798 BIO_flush(s->wbio);
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 if (s->msg_callback) {
802 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
803 s->msg_callback_arg);
804 }
805
806 if (s->info_callback != NULL) {
807 cb = s->info_callback;
808 } else if (s->ctx->info_callback != NULL) {
809 cb = s->ctx->info_callback;
810 }
811
812 if (cb != NULL) {
813 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
814 cb(s, SSL_CB_WRITE_ALERT, j);
815 }
816 }
817
818 return i;
819}