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