xref: /openssl/ssl/d1_msg.c (revision 38b051a1)
1 /*
2  * Copyright 2005-2020 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 
dtls1_write_app_data_bytes(SSL * s,int type,const void * buf_,size_t len,size_t * written)12 int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, size_t len,
13                                size_t *written)
14 {
15     int i;
16     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
17 
18     if (sc == NULL)
19         return -1;
20 
21     if (SSL_in_init(s) && !ossl_statem_get_in_handshake(sc)) {
22         i = sc->handshake_func(s);
23         if (i < 0)
24             return i;
25         if (i == 0) {
26             ERR_raise(ERR_LIB_SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
27             return -1;
28         }
29     }
30 
31     if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
32         ERR_raise(ERR_LIB_SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
33         return -1;
34     }
35 
36     return dtls1_write_bytes(sc, type, buf_, len, written);
37 }
38 
dtls1_dispatch_alert(SSL * ssl)39 int dtls1_dispatch_alert(SSL *ssl)
40 {
41     int i, j;
42     void (*cb) (const SSL *ssl, int type, int val) = NULL;
43     unsigned char buf[DTLS1_AL_HEADER_LENGTH];
44     unsigned char *ptr = &buf[0];
45     size_t written;
46     SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
47 
48     if (s == NULL)
49         return 0;
50 
51     s->s3.alert_dispatch = 0;
52 
53     memset(buf, 0, sizeof(buf));
54     *ptr++ = s->s3.send_alert[0];
55     *ptr++ = s->s3.send_alert[1];
56 
57     i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0, &written);
58     if (i <= 0) {
59         s->s3.alert_dispatch = 1;
60         /* fprintf(stderr, "not done with alert\n"); */
61     } else {
62         (void)BIO_flush(s->wbio);
63 
64         if (s->msg_callback)
65             s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
66                             2, ssl, s->msg_callback_arg);
67 
68         if (s->info_callback != NULL)
69             cb = s->info_callback;
70         else if (ssl->ctx->info_callback != NULL)
71             cb = ssl->ctx->info_callback;
72 
73         if (cb != NULL) {
74             j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
75             cb(ssl, SSL_CB_WRITE_ALERT, j);
76         }
77     }
78     return i;
79 }
80