Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 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 | |
| 10 | #define USE_SOCKETS |
| 11 | #include "ssl_locl.h" |
| 12 | |
| 13 | int ssl3_do_change_cipher_spec(SSL *s) |
| 14 | { |
| 15 | int i; |
Matt Caswell | 12472b4 | 2016-10-04 20:56:11 +0100 | [diff] [blame] | 16 | size_t finish_md_len; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 17 | const char *sender; |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 18 | size_t slen; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 19 | |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 20 | if (s->server) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 21 | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
| 22 | else |
| 23 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 24 | |
| 25 | if (s->s3->tmp.key_block == NULL) { |
| 26 | if (s->session == NULL || s->session->master_key_length == 0) { |
| 27 | /* might happen if dtls1_read_bytes() calls this */ |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 28 | SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 29 | return (0); |
| 30 | } |
| 31 | |
| 32 | s->session->cipher = s->s3->tmp.new_cipher; |
| 33 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 34 | return (0); |
| 35 | } |
| 36 | |
| 37 | if (!s->method->ssl3_enc->change_cipher_state(s, i)) |
| 38 | return (0); |
| 39 | |
| 40 | /* |
| 41 | * we have to record the message digest at this point so we can get it |
| 42 | * before we read the finished message |
| 43 | */ |
Matt Caswell | 49ae742 | 2015-09-08 09:13:50 +0100 | [diff] [blame] | 44 | if (!s->server) { |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 45 | sender = s->method->ssl3_enc->server_finished_label; |
| 46 | slen = s->method->ssl3_enc->server_finished_label_len; |
| 47 | } else { |
| 48 | sender = s->method->ssl3_enc->client_finished_label; |
| 49 | slen = s->method->ssl3_enc->client_finished_label_len; |
| 50 | } |
| 51 | |
Matt Caswell | 12472b4 | 2016-10-04 20:56:11 +0100 | [diff] [blame] | 52 | finish_md_len = s->method->ssl3_enc->final_finish_mac(s, sender, slen, |
| 53 | s->s3->tmp.peer_finish_md); |
| 54 | if (finish_md_len == 0) { |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 55 | SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
| 56 | return 0; |
| 57 | } |
Matt Caswell | 12472b4 | 2016-10-04 20:56:11 +0100 | [diff] [blame] | 58 | s->s3->tmp.peer_finish_md_len = finish_md_len; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 59 | |
| 60 | return (1); |
| 61 | } |
| 62 | |
| 63 | int ssl3_send_alert(SSL *s, int level, int desc) |
| 64 | { |
| 65 | /* Map tls/ssl alert value to correct one */ |
Matt Caswell | 49e7fe1 | 2017-02-21 09:22:22 +0000 | [diff] [blame] | 66 | if (SSL_TREAT_AS_TLS13(s)) |
| 67 | desc = tls13_alert_code(desc); |
| 68 | else |
| 69 | desc = s->method->ssl3_enc->alert_value(desc); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 70 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) |
| 71 | desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have |
| 72 | * protocol_version alerts */ |
| 73 | if (desc < 0) |
| 74 | return -1; |
| 75 | /* If a fatal one, remove from cache */ |
| 76 | if ((level == SSL3_AL_FATAL) && (s->session != NULL)) |
Todd Short | e2bb9b9 | 2016-05-26 13:49:36 -0400 | [diff] [blame] | 77 | SSL_CTX_remove_session(s->session_ctx, s->session); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 78 | |
| 79 | s->s3->alert_dispatch = 1; |
| 80 | s->s3->send_alert[0] = level; |
| 81 | s->s3->send_alert[1] = desc; |
Matt Caswell | f161995 | 2015-02-02 12:18:03 +0000 | [diff] [blame] | 82 | if (!RECORD_LAYER_write_pending(&s->rlayer)) { |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 83 | /* data still being written out? */ |
| 84 | return s->method->ssl_dispatch_alert(s); |
| 85 | } |
| 86 | /* |
| 87 | * else data is still being written out, we will get written some time in |
| 88 | * the future |
| 89 | */ |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | int ssl3_dispatch_alert(SSL *s) |
| 94 | { |
| 95 | int i, j; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 96 | size_t alertlen; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 97 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 98 | size_t written; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 99 | |
| 100 | s->s3->alert_dispatch = 0; |
Matt Caswell | d102d9d | 2015-09-22 11:12:50 +0100 | [diff] [blame] | 101 | alertlen = 2; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 102 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0, |
| 103 | &written); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 104 | if (i <= 0) { |
| 105 | s->s3->alert_dispatch = 1; |
| 106 | } else { |
| 107 | /* |
| 108 | * Alert sent to BIO. If it is important, flush it now. If the |
| 109 | * message does not get sent due to non-blocking IO, we will not |
| 110 | * worry too much. |
| 111 | */ |
| 112 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) |
| 113 | (void)BIO_flush(s->wbio); |
| 114 | |
| 115 | if (s->msg_callback) |
| 116 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, |
| 117 | 2, s, s->msg_callback_arg); |
| 118 | |
| 119 | if (s->info_callback != NULL) |
| 120 | cb = s->info_callback; |
| 121 | else if (s->ctx->info_callback != NULL) |
| 122 | cb = s->ctx->info_callback; |
| 123 | |
| 124 | if (cb != NULL) { |
| 125 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 126 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 127 | } |
| 128 | } |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 129 | return i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 130 | } |