blob: f2a26a9bc2e7dd0821b2b6fd46f5249aca010b14 [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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
Adam Langley87750b42014-06-20 12:00:00 -0700112#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
116#include <openssl/buf.h>
117#include <openssl/err.h>
118#include <openssl/evp.h>
119#include <openssl/mem.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700123
Adam Langleyfcf25832014-12-18 17:42:32 -0800124
David Benjaminb8d28cf2015-07-28 21:34:45 -0400125static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjamin24f346d2015-06-06 03:28:08 -0400127/* kMaxWarningAlerts is the number of consecutive warning alerts that will be
128 * processed. */
129static const uint8_t kMaxWarningAlerts = 4;
130
David Benjaminb8d28cf2015-07-28 21:34:45 -0400131/* ssl3_get_record reads a new input record. On success, it places it in
132 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
133 * more data is needed. */
134static int ssl3_get_record(SSL *ssl) {
135 int ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700136again:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400137 /* Ensure the buffer is large enough to decrypt in-place. */
138 ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
139 if (ret <= 0) {
140 return ret;
141 }
142 assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjaminb8d28cf2015-07-28 21:34:45 -0400144 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
145 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
146 uint8_t type, alert;
147 size_t len, consumed;
148 switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
149 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
150 case ssl_open_record_success:
151 ssl_read_buffer_consume(ssl, consumed);
David Benjamin6a08da22015-05-08 22:58:12 -0400152
David Benjaminb8d28cf2015-07-28 21:34:45 -0400153 if (len > 0xffff) {
154 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
155 return -1;
156 }
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjaminb8d28cf2015-07-28 21:34:45 -0400158 SSL3_RECORD *rr = &ssl->s3->rrec;
159 rr->type = type;
160 rr->length = (uint16_t)len;
161 rr->off = 0;
162 rr->data = out;
163 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjaminb8d28cf2015-07-28 21:34:45 -0400165 case ssl_open_record_partial:
166 ret = ssl_read_buffer_extend_to(ssl, consumed);
167 if (ret <= 0) {
168 return ret;
169 }
170 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjaminb8d28cf2015-07-28 21:34:45 -0400172 case ssl_open_record_discard:
173 ssl_read_buffer_consume(ssl, consumed);
174 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjaminb8d28cf2015-07-28 21:34:45 -0400176 case ssl_open_record_error:
177 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
178 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjaminb8d28cf2015-07-28 21:34:45 -0400181 assert(0);
182 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
183 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800184}
Adam Langley95c29f32014-06-20 12:00:00 -0700185
David Benjaminc933a472015-05-30 16:14:58 -0400186int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
187 return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
188}
189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190/* Call this to write data in records of type |type|. It will return <= 0 if
191 * not all data has been sent or non-blocking IO. */
192int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
193 const uint8_t *buf = buf_;
194 unsigned int tot, n, nw;
195 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 s->rwstate = SSL_NOTHING;
198 assert(s->s3->wnum <= INT_MAX);
199 tot = s->s3->wnum;
200 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamined7c4752015-02-16 19:16:46 -0500202 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 i = s->handshake_func(s);
204 if (i < 0) {
205 return i;
206 }
207 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400208 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 return -1;
210 }
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 /* Ensure that if we end up with a smaller value of data to write out than
214 * the the original len from a write which didn't complete for non-blocking
215 * I/O and also somehow ended up avoiding the check for this in
216 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
217 * end up with (len-tot) as a large number that will then promptly send
218 * beyond the end of the users buffer ... so we trap and report the error in
219 * a way the user will notice. */
220 if (len < 0 || (size_t)len < tot) {
David Benjamin3570d732015-06-29 00:28:17 -0400221 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 return -1;
223 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 n = (len - tot);
226 for (;;) {
227 /* max contains the maximum number of bytes that we can put into a
228 * record. */
229 unsigned max = s->max_send_fragment;
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 if (n > max) {
231 nw = max;
232 } else {
233 nw = n;
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjaminb8d28cf2015-07-28 21:34:45 -0400236 i = do_ssl3_write(s, type, &buf[tot], nw);
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (i <= 0) {
238 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 return i;
240 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
243 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 return tot + i;
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 n -= i;
248 tot += i;
249 }
250}
Adam Langley95c29f32014-06-20 12:00:00 -0700251
David Benjaminb8d28cf2015-07-28 21:34:45 -0400252/* do_ssl3_write writes an SSL record of the given type. */
253static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len) {
254 /* If there is still data from the previous record, flush it. */
255 if (ssl_write_buffer_is_pending(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 return ssl3_write_pending(s, type, buf, len);
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 /* If we have an alert to send, lets send it */
260 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400261 int ret = s->method->ssl_dispatch_alert(s);
262 if (ret <= 0) {
263 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 }
265 /* if it went, fall through and send more stuff */
266 }
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjaminb8d28cf2015-07-28 21:34:45 -0400268 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
269 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 return -1;
271 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 if (len == 0) {
274 return 0;
275 }
Adam Langley95c29f32014-06-20 12:00:00 -0700276
David Benjaminb8d28cf2015-07-28 21:34:45 -0400277 size_t max_out = len + ssl_max_seal_overhead(s);
278 if (max_out < len) {
279 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin9faafda2015-04-04 19:23:10 -0400280 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400282 uint8_t *out;
283 size_t ciphertext_len;
284 if (!ssl_write_buffer_init(s, &out, max_out) ||
285 !tls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
286 return -1;
287 }
288 ssl_write_buffer_set_len(s, ciphertext_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 /* memorize arguments so that ssl3_write_pending can detect bad write retries
291 * later */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400292 s->s3->wpend_tot = len;
293 s->s3->wpend_buf = buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 s->s3->wpend_type = type;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400295 s->s3->wpend_ret = len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296
297 /* we now just need to write the buffer */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400298 return ssl3_write_pending(s, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800299}
Adam Langley95c29f32014-06-20 12:00:00 -0700300
Adam Langleyfcf25832014-12-18 17:42:32 -0800301int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (s->s3->wpend_tot > (int)len ||
303 (s->s3->wpend_buf != buf &&
304 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
305 s->s3->wpend_type != type) {
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 return -1;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
David Benjaminb8d28cf2015-07-28 21:34:45 -0400310 int ret = ssl_write_buffer_flush(s);
311 if (ret <= 0) {
312 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400314 return s->s3->wpend_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800315}
Adam Langley95c29f32014-06-20 12:00:00 -0700316
David Benjamin86271ee2014-07-21 16:14:03 -0400317/* ssl3_expect_change_cipher_spec informs the record layer that a
318 * ChangeCipherSpec record is required at this point. If a Handshake record is
319 * received before ChangeCipherSpec, the connection will fail. Moreover, if
320 * there are unprocessed handshake bytes, the handshake will also fail and the
321 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800322int ssl3_expect_change_cipher_spec(SSL *s) {
323 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
David Benjamin3570d732015-06-29 00:28:17 -0400324 OPENSSL_PUT_ERROR(SSL, SSL_R_UNPROCESSED_HANDSHAKE_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 return 0;
326 }
327
328 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
329 return 1;
330}
David Benjamin86271ee2014-07-21 16:14:03 -0400331
David Benjamina6022772015-05-30 16:22:10 -0400332int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
333 return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
334}
335
336void ssl3_read_close_notify(SSL *ssl) {
337 ssl3_read_bytes(ssl, 0, NULL, 0, 0);
338}
339
David Benjamin1d5ef3b2015-10-12 19:54:18 -0400340static int ssl3_can_renegotiate(SSL *ssl) {
341 switch (ssl->renegotiate_mode) {
342 case ssl_renegotiate_never:
343 return 0;
344 case ssl_renegotiate_once:
345 return ssl->s3->total_renegotiations == 0;
346 case ssl_renegotiate_freely:
347 return 1;
348 }
349
350 assert(0);
351 return 0;
352}
353
Adam Langley95c29f32014-06-20 12:00:00 -0700354/* Return up to 'len' payload bytes received in 'type' records.
355 * 'type' is one of the following:
356 *
357 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
358 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
359 * - 0 (during a shutdown, no data has to be returned)
360 *
361 * If we don't have stored data to work from, read a SSL/TLS record first
362 * (possibly multiple records if we still don't have anything to return).
363 *
364 * This function must handle any surprises the peer may have for us, such as
365 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
366 * a surprise, but handled as if it were), or renegotiation requests.
367 * Also if record payloads contain fragments too small to process, we store
368 * them until there is enough for the respective protocol (the record protocol
369 * may use arbitrary fragmentation and even interleaving):
370 * Change cipher spec protocol
371 * just 1 byte needed, no need for keeping anything stored
372 * Alert protocol
373 * 2 bytes needed (AlertLevel, AlertDescription)
374 * Handshake protocol
375 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
376 * to detect unexpected Client Hello and Hello Request messages
377 * here, anything else is handled by higher layers
378 * Application data protocol
379 * none of our business
380 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800381int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500382 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 unsigned int n;
384 SSL3_RECORD *rr;
385 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
388 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400389 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 return -1;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
394 /* (partially) satisfy request from storage */
395 uint8_t *src = s->s3->handshake_fragment;
396 uint8_t *dst = buf;
397 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 /* peek == 0 */
400 n = 0;
401 while (len > 0 && s->s3->handshake_fragment_len > 0) {
402 *dst++ = *src++;
403 len--;
404 s->s3->handshake_fragment_len--;
405 n++;
406 }
407 /* move any remaining fragment bytes: */
408 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
409 s->s3->handshake_fragment[k] = *src++;
410 }
411 return n;
412 }
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700415
David Benjamin931ab342015-02-08 19:46:57 -0500416 /* This may require multiple iterations. False Start will cause
417 * |s->handshake_func| to signal success one step early, but the handshake
418 * must be completely finished before other modes are accepted.
419 *
420 * TODO(davidben): Move this check up to a higher level. */
421 while (!s->in_handshake && SSL_in_init(s)) {
422 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 i = s->handshake_func(s);
424 if (i < 0) {
425 return i;
426 }
427 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400428 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 return -1;
430 }
431 }
432
Adam Langley95c29f32014-06-20 12:00:00 -0700433start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 /* s->s3->rrec.type - is the type of record
437 * s->s3->rrec.data - data
438 * s->s3->rrec.off - offset into 'data' for next read
439 * s->s3->rrec.length - number of bytes. */
440 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700441
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400443 if (rr->length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 ret = ssl3_get_record(s);
445 if (ret <= 0) {
446 return ret;
447 }
448 }
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700451
David Benjamindc3da932015-03-12 15:09:02 -0400452 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
453 * ssl3_get_finished. */
454 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
455 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400457 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 goto f_err;
459 }
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
462 * Handshake record. */
463 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
464 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400465 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 goto f_err;
467 }
David Benjamin86271ee2014-07-21 16:14:03 -0400468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 /* If the other end has shut down, throw anything we read away (even in
470 * 'peek' mode) */
471 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
472 rr->length = 0;
473 s->rwstate = SSL_NOTHING;
474 return 0;
475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjaminee0b02a2015-08-22 11:35:40 -0400477 if (type != 0 && type == rr->type) {
David Benjamin24f346d2015-06-06 03:28:08 -0400478 s->s3->warning_alert_count = 0;
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
481 /* make sure that we are not getting application data when we are doing a
482 * handshake for the first time */
483 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500484 s->aead_read_ctx == NULL) {
485 /* TODO(davidben): Is this check redundant with the handshake_func
486 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400488 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 goto f_err;
490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491
David Benjamin4cf369b2015-08-22 01:35:43 -0400492 /* Discard empty records. */
493 if (rr->length == 0) {
494 goto start;
495 }
496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 if (len <= 0) {
498 return len;
499 }
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 if ((unsigned int)len > rr->length) {
502 n = rr->length;
503 } else {
504 n = (unsigned int)len;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 memcpy(buf, &(rr->data[rr->off]), n);
508 if (!peek) {
509 rr->length -= n;
510 rr->off += n;
511 if (rr->length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 rr->off = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400513 /* The record has been consumed, so we may now clear the buffer. */
514 ssl_read_buffer_discard(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 }
516 }
517
518 return n;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin44d3eed2015-05-21 01:29:55 -0400521 /* Process unexpected records. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400524 /* If peer renegotiations are disabled, all out-of-order handshake records
David Benjamin44d3eed2015-05-21 01:29:55 -0400525 * are fatal. Renegotiations as a server are never supported. */
David Benjamin1d5ef3b2015-10-12 19:54:18 -0400526 if (s->server || !ssl3_can_renegotiate(s)) {
David Benjaminb16346b2015-04-08 19:16:58 -0400527 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400528 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjaminb16346b2015-04-08 19:16:58 -0400529 goto f_err;
530 }
531
David Benjamin44d3eed2015-05-21 01:29:55 -0400532 /* HelloRequests may be fragmented across multiple records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 const size_t size = sizeof(s->s3->handshake_fragment);
534 const size_t avail = size - s->s3->handshake_fragment_len;
535 const size_t todo = (rr->length < avail) ? rr->length : avail;
536 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
537 &rr->data[rr->off], todo);
538 rr->off += todo;
539 rr->length -= todo;
540 s->s3->handshake_fragment_len += todo;
541 if (s->s3->handshake_fragment_len < size) {
542 goto start; /* fragment was too small */
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin44d3eed2015-05-21 01:29:55 -0400545 /* Parse out and consume a HelloRequest. */
546 if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST ||
547 s->s3->handshake_fragment[1] != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 s->s3->handshake_fragment[2] != 0 ||
549 s->s3->handshake_fragment[3] != 0) {
550 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400551 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800552 goto f_err;
553 }
David Benjamin44d3eed2015-05-21 01:29:55 -0400554 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langleyfcf25832014-12-18 17:42:32 -0800556 if (s->msg_callback) {
557 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
558 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
David Benjamincd90f3a2015-05-21 01:37:55 -0400561 if (!SSL_is_init_finished(s) || !s->s3->initial_handshake_complete) {
562 /* This cannot happen. If a handshake is in progress, |type| must be
563 * |SSL3_RT_HANDSHAKE|. */
564 assert(0);
David Benjamin3570d732015-06-29 00:28:17 -0400565 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincd90f3a2015-05-21 01:37:55 -0400566 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 }
David Benjamincd90f3a2015-05-21 01:37:55 -0400568
569 /* Renegotiation is only supported at quiescent points in the application
570 * protocol, namely in HTTPS, just before reading the HTTP response. Require
571 * the record-layer be idle and avoid complexities of sending a handshake
572 * record while an application_data record is being written. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400573 if (ssl_write_buffer_is_pending(s)) {
David Benjamincd90f3a2015-05-21 01:37:55 -0400574 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400575 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjamincd90f3a2015-05-21 01:37:55 -0400576 goto f_err;
577 }
578
579 /* Begin a new handshake. */
David Benjamin324dce42015-10-12 19:49:00 -0400580 s->s3->total_renegotiations++;
David Benjamincd90f3a2015-05-21 01:37:55 -0400581 s->state = SSL_ST_CONNECT;
582 i = s->handshake_func(s);
583 if (i < 0) {
584 return i;
585 }
586 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400587 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
David Benjamincd90f3a2015-05-21 01:37:55 -0400588 return -1;
589 }
590
591 /* The handshake completed synchronously. Continue reading records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 goto start;
593 }
David Benjaminb4188f02014-11-01 03:43:48 -0400594
David Benjamin86058a22015-02-22 13:07:21 -0500595 /* If an alert record, process one alert out of the record. Note that we allow
596 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500598 /* Alerts may not be fragmented. */
599 if (rr->length < 2) {
600 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400601 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin86058a22015-02-22 13:07:21 -0500602 goto f_err;
603 }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
Adam Langleyfcf25832014-12-18 17:42:32 -0800605 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500606 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 s->msg_callback_arg);
608 }
David Benjamin86058a22015-02-22 13:07:21 -0500609 const uint8_t alert_level = rr->data[rr->off++];
610 const uint8_t alert_descr = rr->data[rr->off++];
611 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 if (s->info_callback != NULL) {
614 cb = s->info_callback;
615 } else if (s->ctx->info_callback != NULL) {
616 cb = s->ctx->info_callback;
617 }
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500620 uint16_t alert = (alert_level << 8) | alert_descr;
621 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
David Benjamin86058a22015-02-22 13:07:21 -0500624 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 s->s3->warn_alert = alert_descr;
626 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
627 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
628 return 0;
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 /* This is a warning but we receive it if we requested renegotiation and
632 * the peer denied it. Terminate with a fatal alert because if
633 * application tried to renegotiatie it presumably had a good reason and
634 * expects it to succeed.
635 *
636 * In future we might have a renegotiation where we don't care if the
637 * peer refused it where we carry on. */
638 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
639 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400640 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 goto f_err;
642 }
David Benjamin24f346d2015-06-06 03:28:08 -0400643
644 s->s3->warning_alert_count++;
645 if (s->s3->warning_alert_count > kMaxWarningAlerts) {
646 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400647 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
David Benjamin24f346d2015-06-06 03:28:08 -0400648 goto f_err;
649 }
David Benjamin86058a22015-02-22 13:07:21 -0500650 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700652
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 s->rwstate = SSL_NOTHING;
654 s->s3->fatal_alert = alert_descr;
David Benjamin3570d732015-06-29 00:28:17 -0400655 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
657 ERR_add_error_data(2, "SSL alert number ", tmp);
658 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
659 SSL_CTX_remove_session(s->ctx, s->session);
660 return 0;
661 } else {
662 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 goto f_err;
665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 goto start;
668 }
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 if (s->shutdown & SSL_SENT_SHUTDOWN) {
David Benjaminaa9361b2015-08-29 22:33:16 -0400671 /* close_notify has been sent, so discard all records other than alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 rr->length = 0;
David Benjaminaa9361b2015-08-29 22:33:16 -0400673 goto start;
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
677 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
678 * record payload has to look like */
679 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
680 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400681 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 goto f_err;
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 /* Check we have a cipher to change to */
686 if (s->s3->tmp.new_cipher == NULL) {
687 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400688 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 goto f_err;
690 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700691
Adam Langleyfcf25832014-12-18 17:42:32 -0800692 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
693 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400694 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 goto f_err;
696 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700697
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 if (s->msg_callback) {
703 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
704 s->msg_callback_arg);
705 }
Adam Langley95c29f32014-06-20 12:00:00 -0700706
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 s->s3->change_cipher_spec = 1;
708 if (!ssl3_do_change_cipher_spec(s)) {
709 goto err;
710 } else {
711 goto start;
712 }
713 }
Adam Langley95c29f32014-06-20 12:00:00 -0700714
David Benjamine820df92015-02-08 16:06:54 -0500715 /* We already handled these. */
716 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
717 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -0500718
David Benjamine820df92015-02-08 16:06:54 -0500719 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400720 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -0700721
722f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800723 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700724err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 return -1;
726}
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728int ssl3_do_change_cipher_spec(SSL *s) {
729 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 if (s->state & SSL_ST_ACCEPT) {
732 i = SSL3_CHANGE_CIPHER_SERVER_READ;
733 } else {
734 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
735 }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Adam Langleyfcf25832014-12-18 17:42:32 -0800737 if (s->s3->tmp.key_block == NULL) {
738 if (s->session == NULL || s->session->master_key_length == 0) {
739 /* might happen if dtls1_read_bytes() calls this */
David Benjamin3570d732015-06-29 00:28:17 -0400740 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyfcf25832014-12-18 17:42:32 -0800741 return 0;
742 }
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 s->session->cipher = s->s3->tmp.new_cipher;
745 if (!s->enc_method->setup_key_block(s)) {
746 return 0;
747 }
748 }
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 if (!s->enc_method->change_cipher_state(s, i)) {
751 return 0;
752 }
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 return 1;
755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757int ssl3_send_alert(SSL *s, int level, int desc) {
758 /* Map tls/ssl alert value to correct one */
759 desc = s->enc_method->alert_value(desc);
760 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
761 /* SSL 3.0 does not have protocol_version alerts */
762 desc = SSL_AD_HANDSHAKE_FAILURE;
763 }
764 if (desc < 0) {
765 return -1;
766 }
Adam Langley95c29f32014-06-20 12:00:00 -0700767
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 /* If a fatal one, remove from cache */
769 if (level == 2 && s->session != NULL) {
770 SSL_CTX_remove_session(s->ctx, s->session);
771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 s->s3->alert_dispatch = 1;
774 s->s3->send_alert[0] = level;
775 s->s3->send_alert[1] = desc;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400776 if (!ssl_write_buffer_is_pending(s)) {
777 /* Nothing is being written out, so the alert may be dispatched
778 * immediately. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 return s->method->ssl_dispatch_alert(s);
780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 /* else data is still being written out, we will get written some time in the
783 * future */
784 return -1;
785}
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787int ssl3_dispatch_alert(SSL *s) {
788 int i, j;
789 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langleyfcf25832014-12-18 17:42:32 -0800791 s->s3->alert_dispatch = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400792 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2);
Adam Langleyfcf25832014-12-18 17:42:32 -0800793 if (i <= 0) {
794 s->s3->alert_dispatch = 1;
795 } else {
796 /* Alert sent to BIO. If it is important, flush it now. If the message
797 * does not get sent due to non-blocking IO, we will not worry too much. */
798 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
799 BIO_flush(s->wbio);
800 }
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 if (s->msg_callback) {
803 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
804 s->msg_callback_arg);
805 }
806
807 if (s->info_callback != NULL) {
808 cb = s->info_callback;
809 } else if (s->ctx->info_callback != NULL) {
810 cb = s->ctx->info_callback;
811 }
812
813 if (cb != NULL) {
814 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
815 cb(s, SSL_CB_WRITE_ALERT, j);
816 }
817 }
818
819 return i;
820}