xref: /freebsd/crypto/openssl/ssl/d1_msg.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2005-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 
dtls1_write_app_data_bytes(SSL * s,uint8_t type,const void * buf_,size_t len,size_t * written)13 int dtls1_write_app_data_bytes(SSL *s, uint8_t type, const void *buf_,
14                                size_t len, size_t *written)
15 {
16     int i;
17     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
18 
19     if (sc == NULL)
20         return -1;
21 
22     if (SSL_in_init(s) && !ossl_statem_get_in_handshake(sc)) {
23         i = sc->handshake_func(s);
24         if (i < 0)
25             return i;
26         if (i == 0) {
27             ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
28             return -1;
29         }
30     }
31 
32     if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
33         ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
34         return -1;
35     }
36 
37     return dtls1_write_bytes(sc, type, buf_, len, written);
38 }
39 
dtls1_dispatch_alert(SSL * ssl)40 int dtls1_dispatch_alert(SSL *ssl)
41 {
42     int i, j;
43     void (*cb) (const SSL *ssl, int type, int val) = NULL;
44     unsigned char buf[DTLS1_AL_HEADER_LENGTH];
45     unsigned char *ptr = &buf[0];
46     size_t written;
47     SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
48 
49     if (s == NULL)
50         return 0;
51 
52     s->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
53 
54     memset(buf, 0, sizeof(buf));
55     *ptr++ = s->s3.send_alert[0];
56     *ptr++ = s->s3.send_alert[1];
57 
58     i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), &written);
59     if (i <= 0) {
60         s->s3.alert_dispatch = 1;
61         /* fprintf(stderr, "not done with alert\n"); */
62     } else {
63         (void)BIO_flush(s->wbio);
64 
65         if (s->msg_callback)
66             s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
67                             2, ssl, s->msg_callback_arg);
68 
69         if (s->info_callback != NULL)
70             cb = s->info_callback;
71         else if (ssl->ctx->info_callback != NULL)
72             cb = ssl->ctx->info_callback;
73 
74         if (cb != NULL) {
75             j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
76             cb(ssl, SSL_CB_WRITE_ALERT, j);
77         }
78     }
79     return i;
80 }
81