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