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 | |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 10 | #include "ssl_locl.h" |
FdaSilvaYY | 0485d54 | 2016-06-28 22:51:27 +0200 | [diff] [blame] | 11 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 12 | int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, size_t len, |
| 13 | size_t *written) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 14 | { |
| 15 | int i; |
| 16 | |
Matt Caswell | bd79bcb | 2017-04-20 15:13:28 +0100 | [diff] [blame] | 17 | if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) { |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 18 | i = s->handshake_func(s); |
| 19 | if (i < 0) |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 20 | return i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 21 | 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 Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 33 | return dtls1_write_bytes(s, type, buf_, len, written); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | int 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 Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 42 | size_t written; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 43 | |
| 44 | s->s3->alert_dispatch = 0; |
| 45 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 46 | memset(buf, 0, sizeof(buf)); |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 47 | *ptr++ = s->s3->send_alert[0]; |
| 48 | *ptr++ = s->s3->send_alert[1]; |
| 49 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 50 | 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] | 51 | if (i <= 0) { |
| 52 | s->s3->alert_dispatch = 1; |
| 53 | /* fprintf( stderr, "not done with alert\n" ); */ |
| 54 | } else { |
Emilia Kasper | 2f0ca54 | 2017-02-28 14:13:40 +0100 | [diff] [blame] | 55 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 56 | (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 Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 72 | return i; |
Matt Caswell | c103c7e | 2015-02-02 10:05:09 +0000 | [diff] [blame] | 73 | } |