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