Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
Matt Caswell | 6738bf1 | 2018-02-13 12:51:29 +0000 | [diff] [blame] | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 10 | #include "ssl_locl.h" |
| 11 | |
| 12 | int ssl3_do_change_cipher_spec(SSL *s) |
| 13 | { |
| 14 | int i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 15 | |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 16 | if (s->server) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 17 | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
| 18 | else |
| 19 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 20 | |
| 21 | if (s->s3->tmp.key_block == NULL) { |
| 22 | if (s->session == NULL || s->session->master_key_length == 0) { |
| 23 | /* might happen if dtls1_read_bytes() calls this */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 24 | SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 25 | return 0; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | s->session->cipher = s->s3->tmp.new_cipher; |
| 29 | if (!s->method->ssl3_enc->setup_key_block(s)) |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 30 | return 0; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | if (!s->method->ssl3_enc->change_cipher_state(s, i)) |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 34 | return 0; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 35 | |
KaoruToda | 208fb89 | 2017-10-09 20:05:58 +0900 | [diff] [blame] | 36 | return 1; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int ssl3_send_alert(SSL *s, int level, int desc) |
| 40 | { |
| 41 | /* Map tls/ssl alert value to correct one */ |
Matt Caswell | 49e7fe1 | 2017-02-21 09:22:22 +0000 | [diff] [blame] | 42 | if (SSL_TREAT_AS_TLS13(s)) |
| 43 | desc = tls13_alert_code(desc); |
| 44 | else |
| 45 | desc = s->method->ssl3_enc->alert_value(desc); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 46 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) |
| 47 | desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have |
| 48 | * protocol_version alerts */ |
| 49 | if (desc < 0) |
| 50 | return -1; |
| 51 | /* If a fatal one, remove from cache */ |
| 52 | if ((level == SSL3_AL_FATAL) && (s->session != NULL)) |
Todd Short | e2bb9b9 | 2016-05-26 13:49:36 -0400 | [diff] [blame] | 53 | SSL_CTX_remove_session(s->session_ctx, s->session); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 54 | |
| 55 | s->s3->alert_dispatch = 1; |
| 56 | s->s3->send_alert[0] = level; |
| 57 | s->s3->send_alert[1] = desc; |
Matt Caswell | f161995 | 2015-02-02 12:18:03 +0000 | [diff] [blame] | 58 | if (!RECORD_LAYER_write_pending(&s->rlayer)) { |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 59 | /* data still being written out? */ |
| 60 | return s->method->ssl_dispatch_alert(s); |
| 61 | } |
| 62 | /* |
| 63 | * else data is still being written out, we will get written some time in |
| 64 | * the future |
| 65 | */ |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | int ssl3_dispatch_alert(SSL *s) |
| 70 | { |
| 71 | int i, j; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 72 | size_t alertlen; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 73 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 74 | size_t written; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 75 | |
| 76 | s->s3->alert_dispatch = 0; |
Matt Caswell | d102d9d | 2015-09-22 11:12:50 +0100 | [diff] [blame] | 77 | alertlen = 2; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 78 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0, |
| 79 | &written); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 80 | if (i <= 0) { |
| 81 | s->s3->alert_dispatch = 1; |
| 82 | } else { |
| 83 | /* |
Todd Short | 270d65f | 2017-05-12 09:05:11 -0400 | [diff] [blame] | 84 | * Alert sent to BIO - now flush. If the message does not get sent due |
| 85 | * to non-blocking IO, we will not worry too much. |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 86 | */ |
Todd Short | 270d65f | 2017-05-12 09:05:11 -0400 | [diff] [blame] | 87 | (void)BIO_flush(s->wbio); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 88 | |
| 89 | if (s->msg_callback) |
| 90 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, |
| 91 | 2, s, s->msg_callback_arg); |
| 92 | |
| 93 | if (s->info_callback != NULL) |
| 94 | cb = s->info_callback; |
| 95 | else if (s->ctx->info_callback != NULL) |
| 96 | cb = s->ctx->info_callback; |
| 97 | |
| 98 | if (cb != NULL) { |
| 99 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 100 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 101 | } |
| 102 | } |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 103 | return i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 104 | } |