Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 1 | /* |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 2 | * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #define USE_SOCKETS |
| 11 | #include "ssl_locl.h" |
FdaSilvaYY | 0485d54 | 2016-06-28 22:51:27 +0200 | [diff] [blame] | 12 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 13 | int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, size_t len, |
| 14 | size_t *written) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 15 | { |
| 16 | int i; |
| 17 | |
| 18 | #ifndef OPENSSL_NO_SCTP |
| 19 | /* |
| 20 | * Check if we have to continue an interrupted handshake for reading |
| 21 | * belated app data with SCTP. |
| 22 | */ |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 23 | if ((SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) || |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 24 | (BIO_dgram_is_sctp(SSL_get_wbio(s)) && |
Matt Caswell | fe3a329 | 2015-10-05 10:39:54 +0100 | [diff] [blame] | 25 | ossl_statem_in_sctp_read_sock(s))) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 26 | #else |
Matt Caswell | 024f543 | 2015-10-22 13:57:18 +0100 | [diff] [blame] | 27 | if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 28 | #endif |
| 29 | { |
| 30 | i = s->handshake_func(s); |
| 31 | if (i < 0) |
| 32 | return (i); |
| 33 | if (i == 0) { |
| 34 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, |
| 35 | SSL_R_SSL_HANDSHAKE_FAILURE); |
| 36 | return -1; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 41 | SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG); |
| 42 | return -1; |
| 43 | } |
| 44 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 45 | return dtls1_write_bytes(s, type, buf_, len, written); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 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]; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 54 | size_t written; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 55 | |
| 56 | s->s3->alert_dispatch = 0; |
| 57 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 58 | memset(buf, 0, sizeof(buf)); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 59 | *ptr++ = s->s3->send_alert[0]; |
| 60 | *ptr++ = s->s3->send_alert[1]; |
| 61 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 62 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0, &written); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 63 | if (i <= 0) { |
| 64 | s->s3->alert_dispatch = 1; |
| 65 | /* fprintf( stderr, "not done with alert\n" ); */ |
| 66 | } else { |
Emilia Kasper | 2f0ca54 | 2017-02-28 14:13:40 +0100 | [diff] [blame] | 67 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 68 | (void)BIO_flush(s->wbio); |
| 69 | |
| 70 | if (s->msg_callback) |
| 71 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, |
| 72 | 2, s, s->msg_callback_arg); |
| 73 | |
| 74 | if (s->info_callback != NULL) |
| 75 | cb = s->info_callback; |
| 76 | else if (s->ctx->info_callback != NULL) |
| 77 | cb = s->ctx->info_callback; |
| 78 | |
| 79 | if (cb != NULL) { |
| 80 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 81 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 82 | } |
| 83 | } |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 84 | return i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 85 | } |