1e71b7053SJung-uk Kim /* 2*da327cd2SJung-uk Kim * Copyright 2005-2019 The OpenSSL Project Authors. All Rights Reserved. 3e71b7053SJung-uk Kim * 4e71b7053SJung-uk Kim * Licensed under the OpenSSL license (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 10e71b7053SJung-uk Kim #include "ssl_locl.h" 11e71b7053SJung-uk Kim 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) { 22e71b7053SJung-uk Kim SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, 23e71b7053SJung-uk Kim SSL_R_SSL_HANDSHAKE_FAILURE); 24e71b7053SJung-uk Kim return -1; 25e71b7053SJung-uk Kim } 26e71b7053SJung-uk Kim } 27e71b7053SJung-uk Kim 28e71b7053SJung-uk Kim if (len > SSL3_RT_MAX_PLAIN_LENGTH) { 29e71b7053SJung-uk Kim SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG); 30e71b7053SJung-uk Kim return -1; 31e71b7053SJung-uk Kim } 32e71b7053SJung-uk Kim 33e71b7053SJung-uk Kim return dtls1_write_bytes(s, type, buf_, len, written); 34e71b7053SJung-uk Kim } 35e71b7053SJung-uk Kim 36e71b7053SJung-uk Kim int dtls1_dispatch_alert(SSL *s) 37e71b7053SJung-uk Kim { 38e71b7053SJung-uk Kim int i, j; 39e71b7053SJung-uk Kim void (*cb) (const SSL *ssl, int type, int val) = NULL; 40e71b7053SJung-uk Kim unsigned char buf[DTLS1_AL_HEADER_LENGTH]; 41e71b7053SJung-uk Kim unsigned char *ptr = &buf[0]; 42e71b7053SJung-uk Kim size_t written; 43e71b7053SJung-uk Kim 44e71b7053SJung-uk Kim s->s3->alert_dispatch = 0; 45e71b7053SJung-uk Kim 46e71b7053SJung-uk Kim memset(buf, 0, sizeof(buf)); 47e71b7053SJung-uk Kim *ptr++ = s->s3->send_alert[0]; 48e71b7053SJung-uk Kim *ptr++ = s->s3->send_alert[1]; 49e71b7053SJung-uk Kim 50e71b7053SJung-uk Kim i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0, &written); 51e71b7053SJung-uk Kim if (i <= 0) { 52e71b7053SJung-uk Kim s->s3->alert_dispatch = 1; 53e71b7053SJung-uk Kim /* fprintf( stderr, "not done with alert\n" ); */ 54e71b7053SJung-uk Kim } else { 55e71b7053SJung-uk Kim (void)BIO_flush(s->wbio); 56e71b7053SJung-uk Kim 57e71b7053SJung-uk Kim if (s->msg_callback) 58e71b7053SJung-uk Kim s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 59e71b7053SJung-uk Kim 2, s, s->msg_callback_arg); 60e71b7053SJung-uk Kim 61e71b7053SJung-uk Kim if (s->info_callback != NULL) 62e71b7053SJung-uk Kim cb = s->info_callback; 63e71b7053SJung-uk Kim else if (s->ctx->info_callback != NULL) 64e71b7053SJung-uk Kim cb = s->ctx->info_callback; 65e71b7053SJung-uk Kim 66e71b7053SJung-uk Kim if (cb != NULL) { 67e71b7053SJung-uk Kim j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; 68e71b7053SJung-uk Kim cb(s, SSL_CB_WRITE_ALERT, j); 69e71b7053SJung-uk Kim } 70e71b7053SJung-uk Kim } 71e71b7053SJung-uk Kim return i; 72e71b7053SJung-uk Kim } 73