1 /* 2 * Copyright 1995-2018 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 ssl3_do_change_cipher_spec(SSL *s) 14 { 15 int i; 16 17 if (s->server) 18 i = SSL3_CHANGE_CIPHER_SERVER_READ; 19 else 20 i = SSL3_CHANGE_CIPHER_CLIENT_READ; 21 22 if (s->s3->tmp.key_block == NULL) { 23 if (s->session == NULL || s->session->master_key_length == 0) { 24 /* might happen if dtls1_read_bytes() calls this */ 25 SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); 26 return (0); 27 } 28 29 s->session->cipher = s->s3->tmp.new_cipher; 30 if (!s->method->ssl3_enc->setup_key_block(s)) 31 return (0); 32 } 33 34 if (!s->method->ssl3_enc->change_cipher_state(s, i)) 35 return (0); 36 37 return 1; 38 } 39 40 int ssl3_send_alert(SSL *s, int level, int desc) 41 { 42 /* Map tls/ssl alert value to correct one */ 43 desc = s->method->ssl3_enc->alert_value(desc); 44 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) 45 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have 46 * protocol_version alerts */ 47 if (desc < 0) 48 return -1; 49 /* If a fatal one, remove from cache */ 50 if ((level == SSL3_AL_FATAL) && (s->session != NULL)) 51 SSL_CTX_remove_session(s->session_ctx, s->session); 52 53 s->s3->alert_dispatch = 1; 54 s->s3->send_alert[0] = level; 55 s->s3->send_alert[1] = desc; 56 if (!RECORD_LAYER_write_pending(&s->rlayer)) { 57 /* data still being written out? */ 58 return s->method->ssl_dispatch_alert(s); 59 } 60 /* 61 * else data is still being written out, we will get written some time in 62 * the future 63 */ 64 return -1; 65 } 66 67 int ssl3_dispatch_alert(SSL *s) 68 { 69 int i, j; 70 unsigned int alertlen; 71 void (*cb) (const SSL *ssl, int type, int val) = NULL; 72 73 s->s3->alert_dispatch = 0; 74 alertlen = 2; 75 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0); 76 if (i <= 0) { 77 s->s3->alert_dispatch = 1; 78 } else { 79 /* 80 * Alert sent to BIO. If it is important, flush it now. If the 81 * message does not get sent due to non-blocking IO, we will not 82 * worry too much. 83 */ 84 if (s->s3->send_alert[0] == SSL3_AL_FATAL) 85 (void)BIO_flush(s->wbio); 86 87 if (s->msg_callback) 88 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 89 2, s, s->msg_callback_arg); 90 91 if (s->info_callback != NULL) 92 cb = s->info_callback; 93 else if (s->ctx->info_callback != NULL) 94 cb = s->ctx->info_callback; 95 96 if (cb != NULL) { 97 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; 98 cb(s, SSL_CB_WRITE_ALERT, j); 99 } 100 } 101 return (i); 102 } 103