/src/openssl/ssl/s3_msg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 |
8 | | */ |
9 | | |
10 | | #include "ssl_local.h" |
11 | | #include "internal/ssl_unwrap.h" |
12 | | |
13 | | int ssl3_do_change_cipher_spec(SSL_CONNECTION *s) |
14 | 1.60k | { |
15 | 1.60k | int i; |
16 | 1.60k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
17 | | |
18 | 1.60k | if (s->server) |
19 | 1.60k | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
20 | 0 | else |
21 | 0 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
22 | | |
23 | 1.60k | if (s->s3.tmp.key_block == NULL) { |
24 | 1.60k | if (s->session == NULL || s->session->master_key_length == 0) { |
25 | | /* might happen if dtls1_read_bytes() calls this */ |
26 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_CCS_RECEIVED_EARLY); |
27 | 0 | return 0; |
28 | 0 | } |
29 | | |
30 | 1.60k | s->session->cipher = s->s3.tmp.new_cipher; |
31 | 1.60k | if (!ssl->method->ssl3_enc->setup_key_block(s)) { |
32 | | /* SSLfatal() already called */ |
33 | 0 | return 0; |
34 | 0 | } |
35 | 1.60k | } |
36 | | |
37 | 1.60k | if (!ssl->method->ssl3_enc->change_cipher_state(s, i)) { |
38 | | /* SSLfatal() already called */ |
39 | 0 | return 0; |
40 | 0 | } |
41 | | |
42 | 1.60k | return 1; |
43 | 1.60k | } |
44 | | |
45 | | int ssl3_send_alert(SSL_CONNECTION *s, int level, int desc) |
46 | 1.65k | { |
47 | 1.65k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
48 | | |
49 | | /* Map tls/ssl alert value to correct one */ |
50 | 1.65k | if (SSL_CONNECTION_TREAT_AS_TLS13(s)) |
51 | 0 | desc = tls13_alert_code(desc); |
52 | 1.65k | else |
53 | 1.65k | desc = ssl->method->ssl3_enc->alert_value(desc); |
54 | 1.65k | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) |
55 | 0 | desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have |
56 | | * protocol_version alerts */ |
57 | 1.65k | if (desc < 0) |
58 | 0 | return -1; |
59 | 1.65k | if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY) |
60 | 0 | return -1; |
61 | | /* If a fatal one, remove from cache */ |
62 | 1.65k | if ((level == SSL3_AL_FATAL) && (s->session != NULL)) |
63 | 1.18k | SSL_CTX_remove_session(s->session_ctx, s->session); |
64 | | |
65 | 1.65k | s->s3.alert_dispatch = SSL_ALERT_DISPATCH_PENDING; |
66 | 1.65k | s->s3.send_alert[0] = level; |
67 | 1.65k | s->s3.send_alert[1] = desc; |
68 | 1.65k | if (!RECORD_LAYER_write_pending(&s->rlayer)) { |
69 | | /* data still being written out? */ |
70 | 1.65k | return ssl->method->ssl_dispatch_alert(ssl); |
71 | 1.65k | } |
72 | | /* |
73 | | * else data is still being written out, we will get written some time in |
74 | | * the future |
75 | | */ |
76 | 0 | return -1; |
77 | 1.65k | } |
78 | | |
79 | | int ssl3_dispatch_alert(SSL *s) |
80 | 0 | { |
81 | 0 | int i, j; |
82 | 0 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
83 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
84 | 0 | OSSL_RECORD_TEMPLATE templ; |
85 | |
|
86 | 0 | if (sc == NULL) |
87 | 0 | return -1; |
88 | | |
89 | 0 | if (sc->rlayer.wrlmethod == NULL) { |
90 | | /* No write record layer so we can't sent and alert. We just ignore it */ |
91 | 0 | sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
92 | 0 | return 1; |
93 | 0 | } |
94 | | |
95 | 0 | templ.type = SSL3_RT_ALERT; |
96 | 0 | templ.version = (sc->version == TLS1_3_VERSION) ? TLS1_2_VERSION |
97 | 0 | : sc->version; |
98 | 0 | if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO |
99 | 0 | && !sc->renegotiate |
100 | 0 | && TLS1_get_version(s) > TLS1_VERSION |
101 | 0 | && sc->hello_retry_request == SSL_HRR_NONE) { |
102 | 0 | templ.version = TLS1_VERSION; |
103 | 0 | } |
104 | 0 | templ.buf = &sc->s3.send_alert[0]; |
105 | 0 | templ.buflen = 2; |
106 | |
|
107 | 0 | if (RECORD_LAYER_write_pending(&sc->rlayer)) { |
108 | 0 | if (sc->s3.alert_dispatch != SSL_ALERT_DISPATCH_RETRY) { |
109 | | /* |
110 | | * We have a write pending but it wasn't from a previous call to |
111 | | * this function! Can we ever get here? Maybe via API misuse?? |
112 | | * Give up. |
113 | | */ |
114 | 0 | sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
115 | 0 | return -1; |
116 | 0 | } |
117 | | /* Retry what we've already got pending */ |
118 | 0 | i = HANDLE_RLAYER_WRITE_RETURN(sc, |
119 | 0 | sc->rlayer.wrlmethod->retry_write_records(sc->rlayer.wrl)); |
120 | 0 | if (i <= 0) { |
121 | | /* Could be NBIO. Keep alert_dispatch as SSL_ALERT_DISPATCH_RETRY */ |
122 | 0 | return -1; |
123 | 0 | } |
124 | 0 | sc->rlayer.wpend_tot = 0; |
125 | 0 | sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
126 | 0 | return 1; |
127 | 0 | } |
128 | | |
129 | 0 | i = HANDLE_RLAYER_WRITE_RETURN(sc, |
130 | 0 | sc->rlayer.wrlmethod->write_records(sc->rlayer.wrl, &templ, 1)); |
131 | |
|
132 | 0 | if (i <= 0) { |
133 | 0 | sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_RETRY; |
134 | 0 | sc->rlayer.wpend_tot = templ.buflen; |
135 | 0 | sc->rlayer.wpend_type = templ.type; |
136 | 0 | sc->rlayer.wpend_buf = templ.buf; |
137 | 0 | } else { |
138 | | /* |
139 | | * Alert sent to BIO - now flush. If the message does not get sent due |
140 | | * to non-blocking IO, we will not worry too much. |
141 | | */ |
142 | 0 | (void)BIO_flush(sc->wbio); |
143 | 0 | sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
144 | |
|
145 | 0 | if (sc->msg_callback) |
146 | 0 | sc->msg_callback(1, sc->version, SSL3_RT_ALERT, sc->s3.send_alert, |
147 | 0 | 2, s, sc->msg_callback_arg); |
148 | |
|
149 | 0 | if (sc->info_callback != NULL) |
150 | 0 | cb = sc->info_callback; |
151 | 0 | else if (s->ctx->info_callback != NULL) |
152 | 0 | cb = s->ctx->info_callback; |
153 | |
|
154 | 0 | if (cb != NULL) { |
155 | 0 | j = (sc->s3.send_alert[0] << 8) | sc->s3.send_alert[1]; |
156 | 0 | cb(s, SSL_CB_WRITE_ALERT, j); |
157 | 0 | } |
158 | 0 | } |
159 | 0 | return i; |
160 | 0 | } |