/src/openssl/ssl/d1_msg.c
Line | Count | Source |
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 | | |
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 | 0 | if (i < 0) |
32 | 0 | return i; |
33 | 0 | if (i == 0) { |
34 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
35 | 0 | return -1; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
40 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
41 | 0 | return -1; |
42 | 0 | } |
43 | | |
44 | 0 | return dtls1_write_bytes(sc, type, buf_, len, written); |
45 | 0 | } |
46 | | |
47 | | int dtls1_dispatch_alert(SSL *ssl) |
48 | 0 | { |
49 | 0 | int i, j; |
50 | 0 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
51 | 0 | unsigned char buf[DTLS1_AL_HEADER_LENGTH]; |
52 | 0 | unsigned char *ptr = &buf[0]; |
53 | 0 | size_t written; |
54 | 0 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
55 | |
|
56 | 0 | if (s == NULL) |
57 | 0 | return 0; |
58 | | |
59 | 0 | s->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE; |
60 | |
|
61 | 0 | memset(buf, 0, sizeof(buf)); |
62 | 0 | *ptr++ = s->s3.send_alert[0]; |
63 | 0 | *ptr++ = s->s3.send_alert[1]; |
64 | |
|
65 | 0 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), &written); |
66 | 0 | if (i <= 0) { |
67 | 0 | s->s3.alert_dispatch = 1; |
68 | | /* fprintf(stderr, "not done with alert\n"); */ |
69 | 0 | } else { |
70 | 0 | (void)BIO_flush(s->wbio); |
71 | |
|
72 | 0 | if (s->msg_callback) |
73 | 0 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert, |
74 | 0 | 2, ssl, s->msg_callback_arg); |
75 | |
|
76 | 0 | if (s->info_callback != NULL) |
77 | 0 | cb = s->info_callback; |
78 | 0 | else if (ssl->ctx->info_callback != NULL) |
79 | 0 | cb = ssl->ctx->info_callback; |
80 | |
|
81 | 0 | if (cb != NULL) { |
82 | 0 | j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1]; |
83 | 0 | cb(ssl, SSL_CB_WRITE_ALERT, j); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | return i; |
87 | 0 | } |