blob: 5906e88ca68f69b04c62f764cc27a58bd4536266 [file] [log] [blame]
Matt Caswellc103c7e2015-02-02 10:05:09 +00001/*
Rich Salz846e33c2016-05-17 14:18:30 -04002 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
Matt Caswellc103c7e2015-02-02 10:05:09 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (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
Matt Caswellc103c7e2015-02-02 10:05:09 +00008 */
9
Matt Caswellc103c7e2015-02-02 10:05:09 +000010#include "ssl_locl.h"
FdaSilvaYY0485d542016-06-28 22:51:27 +020011
Matt Caswell7ee86272016-09-07 11:34:39 +010012int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, size_t len,
13 size_t *written)
Matt Caswellc103c7e2015-02-02 10:05:09 +000014{
15 int i;
16
Matt Caswellbd79bcb2017-04-20 15:13:28 +010017 if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
Matt Caswellc103c7e2015-02-02 10:05:09 +000018 i = s->handshake_func(s);
19 if (i < 0)
KaoruToda26a7d932017-10-17 23:04:09 +090020 return i;
Matt Caswellc103c7e2015-02-02 10:05:09 +000021 if (i == 0) {
22 SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,
23 SSL_R_SSL_HANDSHAKE_FAILURE);
24 return -1;
25 }
26 }
27
28 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
29 SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG);
30 return -1;
31 }
32
Matt Caswell7ee86272016-09-07 11:34:39 +010033 return dtls1_write_bytes(s, type, buf_, len, written);
Matt Caswellc103c7e2015-02-02 10:05:09 +000034}
35
36int dtls1_dispatch_alert(SSL *s)
37{
38 int i, j;
39 void (*cb) (const SSL *ssl, int type, int val) = NULL;
40 unsigned char buf[DTLS1_AL_HEADER_LENGTH];
41 unsigned char *ptr = &buf[0];
Matt Caswell7ee86272016-09-07 11:34:39 +010042 size_t written;
Matt Caswellc103c7e2015-02-02 10:05:09 +000043
44 s->s3->alert_dispatch = 0;
45
Rich Salz16f8d4e2015-05-04 18:00:15 -040046 memset(buf, 0, sizeof(buf));
Matt Caswellc103c7e2015-02-02 10:05:09 +000047 *ptr++ = s->s3->send_alert[0];
48 *ptr++ = s->s3->send_alert[1];
49
Matt Caswell7ee86272016-09-07 11:34:39 +010050 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0, &written);
Matt Caswellc103c7e2015-02-02 10:05:09 +000051 if (i <= 0) {
52 s->s3->alert_dispatch = 1;
53 /* fprintf( stderr, "not done with alert\n" ); */
54 } else {
Emilia Kasper2f0ca542017-02-28 14:13:40 +010055 if (s->s3->send_alert[0] == SSL3_AL_FATAL)
Matt Caswellc103c7e2015-02-02 10:05:09 +000056 (void)BIO_flush(s->wbio);
57
58 if (s->msg_callback)
59 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
60 2, s, s->msg_callback_arg);
61
62 if (s->info_callback != NULL)
63 cb = s->info_callback;
64 else if (s->ctx->info_callback != NULL)
65 cb = s->ctx->info_callback;
66
67 if (cb != NULL) {
68 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
69 cb(s, SSL_CB_WRITE_ALERT, j);
70 }
71 }
Matt Caswell7ee86272016-09-07 11:34:39 +010072 return i;
Matt Caswellc103c7e2015-02-02 10:05:09 +000073}