]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/s3_msg.c
Fix error in BIO_get_ktls_send() and BIO_get_ktls_recv()
[thirdparty/openssl.git] / ssl / s3_msg.c
CommitLineData
846e33c7 1/*
6738bf14 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
c103c7e2 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
c103c7e2
MC
8 */
9
c103c7e2
MC
10#include "ssl_locl.h"
11
12int ssl3_do_change_cipher_spec(SSL *s)
13{
14 int i;
c103c7e2 15
49ae7423 16 if (s->server)
c103c7e2
MC
17 i = SSL3_CHANGE_CIPHER_SERVER_READ;
18 else
19 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
20
21 if (s->s3->tmp.key_block == NULL) {
22 if (s->session == NULL || s->session->master_key_length == 0) {
23 /* might happen if dtls1_read_bytes() calls this */
a230b26e 24 SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
26a7d938 25 return 0;
c103c7e2
MC
26 }
27
28 s->session->cipher = s->s3->tmp.new_cipher;
157af9be
MC
29 if (!s->method->ssl3_enc->setup_key_block(s)) {
30 /* SSLfatal() already called */
26a7d938 31 return 0;
157af9be 32 }
c103c7e2
MC
33 }
34
157af9be
MC
35 if (!s->method->ssl3_enc->change_cipher_state(s, i)) {
36 /* SSLfatal() already called */
26a7d938 37 return 0;
157af9be 38 }
c103c7e2 39
208fb891 40 return 1;
c103c7e2
MC
41}
42
43int ssl3_send_alert(SSL *s, int level, int desc)
44{
45 /* Map tls/ssl alert value to correct one */
49e7fe12
MC
46 if (SSL_TREAT_AS_TLS13(s))
47 desc = tls13_alert_code(desc);
48 else
49 desc = s->method->ssl3_enc->alert_value(desc);
c103c7e2
MC
50 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
51 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
52 * protocol_version alerts */
53 if (desc < 0)
54 return -1;
55 /* If a fatal one, remove from cache */
56 if ((level == SSL3_AL_FATAL) && (s->session != NULL))
e2bb9b9b 57 SSL_CTX_remove_session(s->session_ctx, s->session);
c103c7e2
MC
58
59 s->s3->alert_dispatch = 1;
60 s->s3->send_alert[0] = level;
61 s->s3->send_alert[1] = desc;
f161995e 62 if (!RECORD_LAYER_write_pending(&s->rlayer)) {
c103c7e2
MC
63 /* data still being written out? */
64 return s->method->ssl_dispatch_alert(s);
65 }
66 /*
67 * else data is still being written out, we will get written some time in
68 * the future
69 */
70 return -1;
71}
72
73int ssl3_dispatch_alert(SSL *s)
74{
75 int i, j;
7ee8627f 76 size_t alertlen;
c103c7e2 77 void (*cb) (const SSL *ssl, int type, int val) = NULL;
7ee8627f 78 size_t written;
c103c7e2
MC
79
80 s->s3->alert_dispatch = 0;
d102d9df 81 alertlen = 2;
7ee8627f
MC
82 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0,
83 &written);
c103c7e2
MC
84 if (i <= 0) {
85 s->s3->alert_dispatch = 1;
86 } else {
87 /*
270d65fa
TS
88 * Alert sent to BIO - now flush. If the message does not get sent due
89 * to non-blocking IO, we will not worry too much.
c103c7e2 90 */
270d65fa 91 (void)BIO_flush(s->wbio);
c103c7e2
MC
92
93 if (s->msg_callback)
94 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
95 2, s, s->msg_callback_arg);
96
97 if (s->info_callback != NULL)
98 cb = s->info_callback;
99 else if (s->ctx->info_callback != NULL)
100 cb = s->ctx->info_callback;
101
102 if (cb != NULL) {
103 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
104 cb(s, SSL_CB_WRITE_ALERT, j);
105 }
106 }
7ee8627f 107 return i;
c103c7e2 108}