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