2015-02-02 18:05:09 +08:00
|
|
|
/*
|
2023-09-07 16:59:15 +08:00
|
|
|
* Copyright 2005-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2015-02-02 18:05:09 +08:00
|
|
|
*
|
2018-12-06 20:08:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:18:30 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-02-02 18:05:09 +08:00
|
|
|
*/
|
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ssl_local.h"
|
2016-06-29 04:51:27 +08:00
|
|
|
|
2023-08-24 16:34:53 +08:00
|
|
|
int dtls1_write_app_data_bytes(SSL *s, uint8_t type, const void *buf_,
|
|
|
|
size_t len, size_t *written)
|
2015-02-02 18:05:09 +08:00
|
|
|
{
|
|
|
|
int i;
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
|
2015-02-02 18:05:09 +08:00
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
if (sc == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (SSL_in_init(s) && !ossl_statem_get_in_handshake(sc)) {
|
|
|
|
i = sc->handshake_func(s);
|
2015-02-02 18:05:09 +08:00
|
|
|
if (i < 0)
|
2017-10-17 22:04:09 +08:00
|
|
|
return i;
|
2015-02-02 18:05:09 +08:00
|
|
|
if (i == 0) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
|
2015-02-02 18:05:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
|
2020-11-04 19:18:33 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
|
2015-02-02 18:05:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
return dtls1_write_bytes(sc, type, buf_, len, written);
|
2015-02-02 18:05:09 +08:00
|
|
|
}
|
|
|
|
|
2022-06-20 23:11:28 +08:00
|
|
|
int dtls1_dispatch_alert(SSL *ssl)
|
2015-02-02 18:05:09 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
void (*cb) (const SSL *ssl, int type, int val) = NULL;
|
|
|
|
unsigned char buf[DTLS1_AL_HEADER_LENGTH];
|
|
|
|
unsigned char *ptr = &buf[0];
|
2016-09-07 18:34:39 +08:00
|
|
|
size_t written;
|
2022-06-20 23:11:28 +08:00
|
|
|
SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 0;
|
2015-02-02 18:05:09 +08:00
|
|
|
|
2022-10-27 22:38:32 +08:00
|
|
|
s->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
|
2015-02-02 18:05:09 +08:00
|
|
|
|
2015-05-05 06:00:15 +08:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2018-12-13 02:09:50 +08:00
|
|
|
*ptr++ = s->s3.send_alert[0];
|
|
|
|
*ptr++ = s->s3.send_alert[1];
|
2015-02-02 18:05:09 +08:00
|
|
|
|
2022-10-06 20:18:43 +08:00
|
|
|
i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), &written);
|
2015-02-02 18:05:09 +08:00
|
|
|
if (i <= 0) {
|
2018-12-13 02:09:50 +08:00
|
|
|
s->s3.alert_dispatch = 1;
|
2021-10-26 15:16:18 +08:00
|
|
|
/* fprintf(stderr, "not done with alert\n"); */
|
2015-02-02 18:05:09 +08:00
|
|
|
} else {
|
2019-05-29 23:27:26 +08:00
|
|
|
(void)BIO_flush(s->wbio);
|
2015-02-02 18:05:09 +08:00
|
|
|
|
|
|
|
if (s->msg_callback)
|
2018-12-13 02:09:50 +08:00
|
|
|
s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
|
2022-06-20 23:11:28 +08:00
|
|
|
2, ssl, s->msg_callback_arg);
|
2015-02-02 18:05:09 +08:00
|
|
|
|
|
|
|
if (s->info_callback != NULL)
|
|
|
|
cb = s->info_callback;
|
2022-06-20 23:11:28 +08:00
|
|
|
else if (ssl->ctx->info_callback != NULL)
|
|
|
|
cb = ssl->ctx->info_callback;
|
2015-02-02 18:05:09 +08:00
|
|
|
|
|
|
|
if (cb != NULL) {
|
2018-12-13 02:09:50 +08:00
|
|
|
j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
|
2022-06-20 23:11:28 +08:00
|
|
|
cb(ssl, SSL_CB_WRITE_ALERT, j);
|
2015-02-02 18:05:09 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:34:39 +08:00
|
|
|
return i;
|
2015-02-02 18:05:09 +08:00
|
|
|
}
|