1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "ssl_local.h"
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ciint dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, size_t len,
13e1051a39Sopenharmony_ci                               size_t *written)
14e1051a39Sopenharmony_ci{
15e1051a39Sopenharmony_ci    int i;
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci    if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
18e1051a39Sopenharmony_ci        i = s->handshake_func(s);
19e1051a39Sopenharmony_ci        if (i < 0)
20e1051a39Sopenharmony_ci            return i;
21e1051a39Sopenharmony_ci        if (i == 0) {
22e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
23e1051a39Sopenharmony_ci            return -1;
24e1051a39Sopenharmony_ci        }
25e1051a39Sopenharmony_ci    }
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci    if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
28e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
29e1051a39Sopenharmony_ci        return -1;
30e1051a39Sopenharmony_ci    }
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    return dtls1_write_bytes(s, type, buf_, len, written);
33e1051a39Sopenharmony_ci}
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ciint dtls1_dispatch_alert(SSL *s)
36e1051a39Sopenharmony_ci{
37e1051a39Sopenharmony_ci    int i, j;
38e1051a39Sopenharmony_ci    void (*cb) (const SSL *ssl, int type, int val) = NULL;
39e1051a39Sopenharmony_ci    unsigned char buf[DTLS1_AL_HEADER_LENGTH];
40e1051a39Sopenharmony_ci    unsigned char *ptr = &buf[0];
41e1051a39Sopenharmony_ci    size_t written;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    s->s3.alert_dispatch = 0;
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci    memset(buf, 0, sizeof(buf));
46e1051a39Sopenharmony_ci    *ptr++ = s->s3.send_alert[0];
47e1051a39Sopenharmony_ci    *ptr++ = s->s3.send_alert[1];
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci    i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0, &written);
50e1051a39Sopenharmony_ci    if (i <= 0) {
51e1051a39Sopenharmony_ci        s->s3.alert_dispatch = 1;
52e1051a39Sopenharmony_ci        /* fprintf( stderr, "not done with alert\n" ); */
53e1051a39Sopenharmony_ci    } else {
54e1051a39Sopenharmony_ci        (void)BIO_flush(s->wbio);
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci        if (s->msg_callback)
57e1051a39Sopenharmony_ci            s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
58e1051a39Sopenharmony_ci                            2, s, s->msg_callback_arg);
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci        if (s->info_callback != NULL)
61e1051a39Sopenharmony_ci            cb = s->info_callback;
62e1051a39Sopenharmony_ci        else if (s->ctx->info_callback != NULL)
63e1051a39Sopenharmony_ci            cb = s->ctx->info_callback;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci        if (cb != NULL) {
66e1051a39Sopenharmony_ci            j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
67e1051a39Sopenharmony_ci            cb(s, SSL_CB_WRITE_ALERT, j);
68e1051a39Sopenharmony_ci        }
69e1051a39Sopenharmony_ci    }
70e1051a39Sopenharmony_ci    return i;
71e1051a39Sopenharmony_ci}
72