/src/openssl/ssl/d1_msg.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2005-2026 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 dtls1_write_app_data_bytes(SSL *s, uint8_t type, const void *buf_, |
14 | | size_t len, size_t *written) |
15 | 0 | { |
16 | 0 | int i; |
17 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s); |
18 | |
|
19 | 0 | if (sc == NULL) |
20 | 0 | return -1; |
21 | | |
22 | | /* |
23 | | * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go |
24 | | * into init - in which case we should finish doing that first. |
25 | | */ |
26 | 0 | if (sc->key_update != SSL_KEY_UPDATE_NONE || sc->ext.extra_tickets_expected > 0) |
27 | 0 | ossl_statem_set_in_init(sc, 1); |
28 | |
|
29 | 0 | if (SSL_in_init(s) && !ossl_statem_get_in_handshake(sc)) { |
30 | 0 | i = sc->handshake_func(s); |
31 | | |
32 | | /* |
33 | | * DTLS 1.3: finishing a pending ACK ends a call at TLS_ST_OK, so the |
34 | | * KeyUpdate or ticket still owed is only built by the next call. A |
35 | | * call can also finish with something new owed, such as a response to |
36 | | * a KeyUpdate that arrived while it waited for an ACK. Keep calling |
37 | | * until nothing is owed, before any application data is sent. A call |
38 | | * that needs I/O or fails ends the loop. |
39 | | */ |
40 | 0 | while (i > 0 && SSL_CONNECTION_IS_DTLS13(sc) |
41 | 0 | && (sc->key_update != SSL_KEY_UPDATE_NONE |
42 | 0 | || sc->ext.extra_tickets_expected > 0)) { |
43 | 0 | ossl_statem_set_in_init(sc, 1); |
44 | 0 | i = sc->handshake_func(s); |
45 | 0 | } |
46 | 0 | if (i < 0) |
47 | 0 | return i; |
48 | 0 | if (i == 0) { |
49 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
50 | 0 | return -1; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
55 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
56 | 0 | return -1; |
57 | 0 | } |
58 | | |
59 | 0 | return dtls1_write_bytes(sc, type, buf_, len, written); |
60 | 0 | } |
61 | | |
62 | | int dtls1_dispatch_alert(SSL *ssl) |
63 | 0 | { |
64 | 0 | int i, j; |
65 | 0 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
66 | 0 | unsigned char buf[DTLS1_AL_HEADER_LENGTH]; |
67 | 0 | unsigned char *ptr = &buf[0]; |
68 | 0 | size_t written; |
69 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
70 | |
|
71 | 0 | if (s == NULL) |
72 | 0 | return 0; |
73 | | |
74 | 0 | s->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
75 | |
|
76 | 0 | memset(buf, 0, sizeof(buf)); |
77 | 0 | *ptr++ = s->s3.send_alert[0]; |
78 | 0 | *ptr++ = s->s3.send_alert[1]; |
79 | |
|
80 | 0 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), &written); |
81 | 0 | if (i <= 0) { |
82 | 0 | s->s3.alert_dispatch = 1; |
83 | | /* fprintf(stderr, "not done with alert\n"); */ |
84 | 0 | } else { |
85 | 0 | (void)BIO_flush(s->wbio); |
86 | |
|
87 | 0 | if (s->msg_callback) |
88 | 0 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert, |
89 | 0 | 2, ssl, s->msg_callback_arg); |
90 | |
|
91 | 0 | if (s->info_callback != NULL) |
92 | 0 | cb = s->info_callback; |
93 | 0 | else if (ssl->ctx->info_callback != NULL) |
94 | 0 | cb = ssl->ctx->info_callback; |
95 | |
|
96 | 0 | if (cb != NULL) { |
97 | 0 | j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1]; |
98 | 0 | cb(ssl, SSL_CB_WRITE_ALERT, j); |
99 | 0 | } |
100 | 0 | } |
101 | 0 | return i; |
102 | 0 | } |