1 | /*
|
---|
2 | * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * 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
|
---|
8 | */
|
---|
9 |
|
---|
10 | #define USE_SOCKETS
|
---|
11 | #include "ssl_locl.h"
|
---|
12 |
|
---|
13 | int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len)
|
---|
14 | {
|
---|
15 | int i;
|
---|
16 |
|
---|
17 | #ifndef OPENSSL_NO_SCTP
|
---|
18 | /*
|
---|
19 | * Check if we have to continue an interrupted handshake for reading
|
---|
20 | * belated app data with SCTP.
|
---|
21 | */
|
---|
22 | if ((SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) ||
|
---|
23 | (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
|
---|
24 | ossl_statem_in_sctp_read_sock(s)))
|
---|
25 | #else
|
---|
26 | if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s))
|
---|
27 | #endif
|
---|
28 | {
|
---|
29 | i = s->handshake_func(s);
|
---|
30 | if (i < 0)
|
---|
31 | return (i);
|
---|
32 | if (i == 0) {
|
---|
33 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,
|
---|
34 | SSL_R_SSL_HANDSHAKE_FAILURE);
|
---|
35 | return -1;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
|
---|
40 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG);
|
---|
41 | return -1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | i = dtls1_write_bytes(s, type, buf_, len);
|
---|
45 | return i;
|
---|
46 | }
|
---|
47 |
|
---|
48 | int dtls1_dispatch_alert(SSL *s)
|
---|
49 | {
|
---|
50 | int i, j;
|
---|
51 | void (*cb) (const SSL *ssl, int type, int val) = NULL;
|
---|
52 | unsigned char buf[DTLS1_AL_HEADER_LENGTH];
|
---|
53 | unsigned char *ptr = &buf[0];
|
---|
54 |
|
---|
55 | s->s3->alert_dispatch = 0;
|
---|
56 |
|
---|
57 | memset(buf, 0, sizeof(buf));
|
---|
58 | *ptr++ = s->s3->send_alert[0];
|
---|
59 | *ptr++ = s->s3->send_alert[1];
|
---|
60 |
|
---|
61 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
|
---|
62 | if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
|
---|
63 | s2n(s->d1->handshake_read_seq, ptr);
|
---|
64 | l2n3(s->d1->r_msg_hdr.frag_off, ptr);
|
---|
65 | }
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
|
---|
69 | if (i <= 0) {
|
---|
70 | s->s3->alert_dispatch = 1;
|
---|
71 | /* fprintf( stderr, "not done with alert\n" ); */
|
---|
72 | } else {
|
---|
73 | if (s->s3->send_alert[0] == SSL3_AL_FATAL
|
---|
74 | #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
|
---|
75 | || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
|
---|
76 | #endif
|
---|
77 | )
|
---|
78 | (void)BIO_flush(s->wbio);
|
---|
79 |
|
---|
80 | if (s->msg_callback)
|
---|
81 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
|
---|
82 | 2, s, s->msg_callback_arg);
|
---|
83 |
|
---|
84 | if (s->info_callback != NULL)
|
---|
85 | cb = s->info_callback;
|
---|
86 | else if (s->ctx->info_callback != NULL)
|
---|
87 | cb = s->ctx->info_callback;
|
---|
88 |
|
---|
89 | if (cb != NULL) {
|
---|
90 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
|
---|
91 | cb(s, SSL_CB_WRITE_ALERT, j);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return (i);
|
---|
95 | }
|
---|