2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
13 int ssl3_do_change_cipher_spec(SSL *s)
20 i = SSL3_CHANGE_CIPHER_SERVER_READ;
22 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
24 if (s->s3->tmp.key_block == NULL) {
25 if (s->session == NULL || s->session->master_key_length == 0) {
26 /* might happen if dtls1_read_bytes() calls this */
27 SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC,
28 SSL_R_CCS_RECEIVED_EARLY);
32 s->session->cipher = s->s3->tmp.new_cipher;
33 if (!s->method->ssl3_enc->setup_key_block(s))
37 if (!s->method->ssl3_enc->change_cipher_state(s, i))
41 * we have to record the message digest at this point so we can get it
42 * before we read the finished message
45 sender = s->method->ssl3_enc->server_finished_label;
46 slen = s->method->ssl3_enc->server_finished_label_len;
48 sender = s->method->ssl3_enc->client_finished_label;
49 slen = s->method->ssl3_enc->client_finished_label_len;
52 i = s->method->ssl3_enc->final_finish_mac(s,
54 s->s3->tmp.peer_finish_md);
56 SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
59 s->s3->tmp.peer_finish_md_len = i;
64 int ssl3_send_alert(SSL *s, int level, int desc)
66 /* Map tls/ssl alert value to correct one */
67 desc = s->method->ssl3_enc->alert_value(desc);
68 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
69 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
70 * protocol_version alerts */
73 /* If a fatal one, remove from cache */
74 if ((level == SSL3_AL_FATAL) && (s->session != NULL))
75 SSL_CTX_remove_session(s->session_ctx, s->session);
77 s->s3->alert_dispatch = 1;
78 s->s3->send_alert[0] = level;
79 s->s3->send_alert[1] = desc;
80 if (!RECORD_LAYER_write_pending(&s->rlayer)) {
81 /* data still being written out? */
82 return s->method->ssl_dispatch_alert(s);
85 * else data is still being written out, we will get written some time in
91 int ssl3_dispatch_alert(SSL *s)
94 unsigned int alertlen;
95 void (*cb) (const SSL *ssl, int type, int val) = NULL;
97 s->s3->alert_dispatch = 0;
99 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0);
101 s->s3->alert_dispatch = 1;
104 * Alert sent to BIO. If it is important, flush it now. If the
105 * message does not get sent due to non-blocking IO, we will not
108 if (s->s3->send_alert[0] == SSL3_AL_FATAL)
109 (void)BIO_flush(s->wbio);
112 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
113 2, s, s->msg_callback_arg);
115 if (s->info_callback != NULL)
116 cb = s->info_callback;
117 else if (s->ctx->info_callback != NULL)
118 cb = s->ctx->info_callback;
121 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
122 cb(s, SSL_CB_WRITE_ALERT, j);