xref: /freebsd/crypto/openssl/ssl/s3_msg.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
29a3ae0cdSJung-uk Kim  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
8e71b7053SJung-uk Kim  */
9e71b7053SJung-uk Kim 
1017f01e99SJung-uk Kim #include "ssl_local.h"
11e71b7053SJung-uk Kim 
ssl3_do_change_cipher_spec(SSL * s)12e71b7053SJung-uk Kim int ssl3_do_change_cipher_spec(SSL *s)
13e71b7053SJung-uk Kim {
14e71b7053SJung-uk Kim     int i;
15e71b7053SJung-uk Kim 
16e71b7053SJung-uk Kim     if (s->server)
17e71b7053SJung-uk Kim         i = SSL3_CHANGE_CIPHER_SERVER_READ;
18e71b7053SJung-uk Kim     else
19e71b7053SJung-uk Kim         i = SSL3_CHANGE_CIPHER_CLIENT_READ;
20e71b7053SJung-uk Kim 
21*b077aed3SPierre Pronchery     if (s->s3.tmp.key_block == NULL) {
22e71b7053SJung-uk Kim         if (s->session == NULL || s->session->master_key_length == 0) {
23e71b7053SJung-uk Kim             /* might happen if dtls1_read_bytes() calls this */
24*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_SSL, SSL_R_CCS_RECEIVED_EARLY);
25e71b7053SJung-uk Kim             return 0;
26e71b7053SJung-uk Kim         }
27e71b7053SJung-uk Kim 
28*b077aed3SPierre Pronchery         s->session->cipher = s->s3.tmp.new_cipher;
29*b077aed3SPierre Pronchery         if (!s->method->ssl3_enc->setup_key_block(s)) {
30*b077aed3SPierre Pronchery             /* SSLfatal() already called */
31e71b7053SJung-uk Kim             return 0;
32e71b7053SJung-uk Kim         }
33*b077aed3SPierre Pronchery     }
34e71b7053SJung-uk Kim 
35*b077aed3SPierre Pronchery     if (!s->method->ssl3_enc->change_cipher_state(s, i)) {
36*b077aed3SPierre Pronchery         /* SSLfatal() already called */
37e71b7053SJung-uk Kim         return 0;
38*b077aed3SPierre Pronchery     }
39e71b7053SJung-uk Kim 
40e71b7053SJung-uk Kim     return 1;
41e71b7053SJung-uk Kim }
42e71b7053SJung-uk Kim 
ssl3_send_alert(SSL * s,int level,int desc)43e71b7053SJung-uk Kim int ssl3_send_alert(SSL *s, int level, int desc)
44e71b7053SJung-uk Kim {
45e71b7053SJung-uk Kim     /* Map tls/ssl alert value to correct one */
46e71b7053SJung-uk Kim     if (SSL_TREAT_AS_TLS13(s))
47e71b7053SJung-uk Kim         desc = tls13_alert_code(desc);
48e71b7053SJung-uk Kim     else
49e71b7053SJung-uk Kim         desc = s->method->ssl3_enc->alert_value(desc);
50e71b7053SJung-uk Kim     if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
51e71b7053SJung-uk Kim         desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
52e71b7053SJung-uk Kim                                           * protocol_version alerts */
53e71b7053SJung-uk Kim     if (desc < 0)
54e71b7053SJung-uk Kim         return -1;
559a3ae0cdSJung-uk Kim     if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY)
569a3ae0cdSJung-uk Kim         return -1;
57e71b7053SJung-uk Kim     /* If a fatal one, remove from cache */
58e71b7053SJung-uk Kim     if ((level == SSL3_AL_FATAL) && (s->session != NULL))
59e71b7053SJung-uk Kim         SSL_CTX_remove_session(s->session_ctx, s->session);
60e71b7053SJung-uk Kim 
61*b077aed3SPierre Pronchery     s->s3.alert_dispatch = 1;
62*b077aed3SPierre Pronchery     s->s3.send_alert[0] = level;
63*b077aed3SPierre Pronchery     s->s3.send_alert[1] = desc;
64e71b7053SJung-uk Kim     if (!RECORD_LAYER_write_pending(&s->rlayer)) {
65e71b7053SJung-uk Kim         /* data still being written out? */
66e71b7053SJung-uk Kim         return s->method->ssl_dispatch_alert(s);
67e71b7053SJung-uk Kim     }
68e71b7053SJung-uk Kim     /*
69e71b7053SJung-uk Kim      * else data is still being written out, we will get written some time in
70e71b7053SJung-uk Kim      * the future
71e71b7053SJung-uk Kim      */
72e71b7053SJung-uk Kim     return -1;
73e71b7053SJung-uk Kim }
74e71b7053SJung-uk Kim 
ssl3_dispatch_alert(SSL * s)75e71b7053SJung-uk Kim int ssl3_dispatch_alert(SSL *s)
76e71b7053SJung-uk Kim {
77e71b7053SJung-uk Kim     int i, j;
78e71b7053SJung-uk Kim     size_t alertlen;
79e71b7053SJung-uk Kim     void (*cb) (const SSL *ssl, int type, int val) = NULL;
80e71b7053SJung-uk Kim     size_t written;
81e71b7053SJung-uk Kim 
82*b077aed3SPierre Pronchery     s->s3.alert_dispatch = 0;
83e71b7053SJung-uk Kim     alertlen = 2;
84*b077aed3SPierre Pronchery     i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3.send_alert[0], &alertlen, 1, 0,
85e71b7053SJung-uk Kim                       &written);
86e71b7053SJung-uk Kim     if (i <= 0) {
87*b077aed3SPierre Pronchery         s->s3.alert_dispatch = 1;
88e71b7053SJung-uk Kim     } else {
89e71b7053SJung-uk Kim         /*
90e71b7053SJung-uk Kim          * Alert sent to BIO - now flush. If the message does not get sent due
91e71b7053SJung-uk Kim          * to non-blocking IO, we will not worry too much.
92e71b7053SJung-uk Kim          */
93e71b7053SJung-uk Kim         (void)BIO_flush(s->wbio);
94e71b7053SJung-uk Kim 
95e71b7053SJung-uk Kim         if (s->msg_callback)
96*b077aed3SPierre Pronchery             s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
97e71b7053SJung-uk Kim                             2, s, s->msg_callback_arg);
98e71b7053SJung-uk Kim 
99e71b7053SJung-uk Kim         if (s->info_callback != NULL)
100e71b7053SJung-uk Kim             cb = s->info_callback;
101e71b7053SJung-uk Kim         else if (s->ctx->info_callback != NULL)
102e71b7053SJung-uk Kim             cb = s->ctx->info_callback;
103e71b7053SJung-uk Kim 
104e71b7053SJung-uk Kim         if (cb != NULL) {
105*b077aed3SPierre Pronchery             j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
106e71b7053SJung-uk Kim             cb(s, SSL_CB_WRITE_ALERT, j);
107e71b7053SJung-uk Kim         }
108e71b7053SJung-uk Kim     }
109e71b7053SJung-uk Kim     return i;
110e71b7053SJung-uk Kim }
111