2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include "ssl_local.h"
13 #include <openssl/evp.h>
14 #include <openssl/md5.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
18 static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num)
23 unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
24 unsigned char c = 'A';
29 c = os_toascii[c]; /* 'A' in ASCII */
32 md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips");
33 m5 = EVP_MD_CTX_new();
34 s1 = EVP_MD_CTX_new();
35 if (md5 == NULL || m5 == NULL || s1 == NULL) {
36 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK,
37 ERR_R_MALLOC_FAILURE);
40 for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) {
42 if (k > sizeof(buf)) {
43 /* bug: 'buf' is too small for this ciphersuite */
44 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK,
45 ERR_R_INTERNAL_ERROR);
49 for (j = 0; j < k; j++)
52 if (!EVP_DigestInit_ex(s1, EVP_sha1(), NULL)
53 || !EVP_DigestUpdate(s1, buf, k)
54 || !EVP_DigestUpdate(s1, s->session->master_key,
55 s->session->master_key_length)
56 || !EVP_DigestUpdate(s1, s->s3.server_random, SSL3_RANDOM_SIZE)
57 || !EVP_DigestUpdate(s1, s->s3.client_random, SSL3_RANDOM_SIZE)
58 || !EVP_DigestFinal_ex(s1, smd, NULL)
59 || !EVP_DigestInit_ex(m5, md5, NULL)
60 || !EVP_DigestUpdate(m5, s->session->master_key,
61 s->session->master_key_length)
62 || !EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH)) {
63 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK,
64 ERR_R_INTERNAL_ERROR);
67 if ((int)(i + MD5_DIGEST_LENGTH) > num) {
68 if (!EVP_DigestFinal_ex(m5, smd, NULL)) {
69 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
70 SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR);
73 memcpy(km, smd, (num - i));
75 if (!EVP_DigestFinal_ex(m5, km, NULL)) {
76 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
77 SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR);
82 km += MD5_DIGEST_LENGTH;
84 OPENSSL_cleanse(smd, sizeof(smd));
93 int ssl3_change_cipher_state(SSL *s, int which)
95 unsigned char *p, *mac_secret;
96 unsigned char *ms, *key, *iv;
99 #ifndef OPENSSL_NO_COMP
104 size_t n, i, j, k, cl;
107 c = s->s3.tmp.new_sym_enc;
108 m = s->s3.tmp.new_hash;
109 /* m == NULL will lead to a crash later */
110 if (!ossl_assert(m != NULL)) {
111 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
112 ERR_R_INTERNAL_ERROR);
115 #ifndef OPENSSL_NO_COMP
116 if (s->s3.tmp.new_compression == NULL)
119 comp = s->s3.tmp.new_compression->method;
122 if (which & SSL3_CC_READ) {
123 if (s->enc_read_ctx != NULL) {
125 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
126 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
127 ERR_R_MALLOC_FAILURE);
131 * make sure it's initialised in case we exit later with an error
133 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
135 dd = s->enc_read_ctx;
137 if (ssl_replace_hash(&s->read_hash, m) == NULL) {
138 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
139 ERR_R_INTERNAL_ERROR);
142 #ifndef OPENSSL_NO_COMP
144 COMP_CTX_free(s->expand);
147 s->expand = COMP_CTX_new(comp);
148 if (s->expand == NULL) {
149 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
150 SSL_F_SSL3_CHANGE_CIPHER_STATE,
151 SSL_R_COMPRESSION_LIBRARY_ERROR);
156 RECORD_LAYER_reset_read_sequence(&s->rlayer);
157 mac_secret = &(s->s3.read_mac_secret[0]);
159 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
160 if (s->enc_write_ctx != NULL) {
162 } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
163 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
164 ERR_R_MALLOC_FAILURE);
168 * make sure it's initialised in case we exit later with an error
170 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
172 dd = s->enc_write_ctx;
173 if (ssl_replace_hash(&s->write_hash, m) == NULL) {
174 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
175 ERR_R_MALLOC_FAILURE);
178 #ifndef OPENSSL_NO_COMP
180 COMP_CTX_free(s->compress);
183 s->compress = COMP_CTX_new(comp);
184 if (s->compress == NULL) {
185 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
186 SSL_F_SSL3_CHANGE_CIPHER_STATE,
187 SSL_R_COMPRESSION_LIBRARY_ERROR);
192 RECORD_LAYER_reset_write_sequence(&s->rlayer);
193 mac_secret = &(s->s3.write_mac_secret[0]);
197 EVP_CIPHER_CTX_reset(dd);
199 p = s->s3.tmp.key_block;
200 mdi = EVP_MD_size(m);
202 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
203 ERR_R_INTERNAL_ERROR);
207 cl = EVP_CIPHER_key_length(c);
209 k = EVP_CIPHER_iv_length(c);
210 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
211 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
228 if (n > s->s3.tmp.key_block_length) {
229 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
230 ERR_R_INTERNAL_ERROR);
234 memcpy(mac_secret, ms, i);
236 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
237 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE,
238 ERR_R_INTERNAL_ERROR);
242 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
248 int ssl3_setup_key_block(SSL *s)
257 if (s->s3.tmp.key_block_length != 0)
260 if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, &comp,
262 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_SETUP_KEY_BLOCK,
263 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
267 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
268 s->s3.tmp.new_sym_enc = c;
269 ssl_evp_md_free(s->s3.tmp.new_hash);
270 s->s3.tmp.new_hash = hash;
271 #ifdef OPENSSL_NO_COMP
272 s->s3.tmp.new_compression = NULL;
274 s->s3.tmp.new_compression = comp;
277 num = EVP_MD_size(hash);
281 num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c);
284 ssl3_cleanup_key_block(s);
286 if ((p = OPENSSL_malloc(num)) == NULL) {
287 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_SETUP_KEY_BLOCK,
288 ERR_R_MALLOC_FAILURE);
292 s->s3.tmp.key_block_length = num;
293 s->s3.tmp.key_block = p;
295 /* Calls SSLfatal() as required */
296 ret = ssl3_generate_key_block(s, p, num);
298 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) {
300 * enable vulnerability countermeasure for CBC ciphers with known-IV
301 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
303 s->s3.need_empty_fragments = 1;
305 if (s->session->cipher != NULL) {
306 if (s->session->cipher->algorithm_enc == SSL_eNULL)
307 s->s3.need_empty_fragments = 0;
309 #ifndef OPENSSL_NO_RC4
310 if (s->session->cipher->algorithm_enc == SSL_RC4)
311 s->s3.need_empty_fragments = 0;
319 void ssl3_cleanup_key_block(SSL *s)
321 OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length);
322 s->s3.tmp.key_block = NULL;
323 s->s3.tmp.key_block_length = 0;
326 int ssl3_init_finished_mac(SSL *s)
328 BIO *buf = BIO_new(BIO_s_mem());
331 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_INIT_FINISHED_MAC,
332 ERR_R_MALLOC_FAILURE);
335 ssl3_free_digest_list(s);
336 s->s3.handshake_buffer = buf;
337 (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE);
342 * Free digest list. Also frees handshake buffer since they are always freed
346 void ssl3_free_digest_list(SSL *s)
348 BIO_free(s->s3.handshake_buffer);
349 s->s3.handshake_buffer = NULL;
350 EVP_MD_CTX_free(s->s3.handshake_dgst);
351 s->s3.handshake_dgst = NULL;
354 int ssl3_finish_mac(SSL *s, const unsigned char *buf, size_t len)
358 if (s->s3.handshake_dgst == NULL) {
359 /* Note: this writes to a memory BIO so a failure is a fatal error */
361 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC,
362 SSL_R_OVERFLOW_ERROR);
365 ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len);
366 if (ret <= 0 || ret != (int)len) {
367 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC,
368 ERR_R_INTERNAL_ERROR);
372 ret = EVP_DigestUpdate(s->s3.handshake_dgst, buf, len);
374 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC,
375 ERR_R_INTERNAL_ERROR);
382 int ssl3_digest_cached_records(SSL *s, int keep)
388 if (s->s3.handshake_dgst == NULL) {
389 hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
391 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS,
392 SSL_R_BAD_HANDSHAKE_LENGTH);
396 s->s3.handshake_dgst = EVP_MD_CTX_new();
397 if (s->s3.handshake_dgst == NULL) {
398 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS,
399 ERR_R_MALLOC_FAILURE);
403 md = ssl_handshake_md(s);
404 if (md == NULL || !EVP_DigestInit_ex(s->s3.handshake_dgst, md, NULL)
405 || !EVP_DigestUpdate(s->s3.handshake_dgst, hdata, hdatalen)) {
406 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS,
407 ERR_R_INTERNAL_ERROR);
412 BIO_free(s->s3.handshake_buffer);
413 s->s3.handshake_buffer = NULL;
419 void ssl3_digest_master_key_set_params(const SSL_SESSION *session,
423 params[n++] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
424 (void *)session->master_key,
425 session->master_key_length);
426 params[n++] = OSSL_PARAM_construct_end();
429 size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len,
433 EVP_MD_CTX *ctx = NULL;
435 if (!ssl3_digest_cached_records(s, 0)) {
436 /* SSLfatal() already called */
440 if (EVP_MD_CTX_type(s->s3.handshake_dgst) != NID_md5_sha1) {
441 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC,
442 SSL_R_NO_REQUIRED_DIGEST);
446 ctx = EVP_MD_CTX_new();
448 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC,
449 ERR_R_MALLOC_FAILURE);
452 if (!EVP_MD_CTX_copy_ex(ctx, s->s3.handshake_dgst)) {
453 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC,
454 ERR_R_INTERNAL_ERROR);
459 ret = EVP_MD_CTX_size(ctx);
461 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC,
462 ERR_R_INTERNAL_ERROR);
467 if (sender != NULL) {
468 OSSL_PARAM digest_cmd_params[3];
470 ssl3_digest_master_key_set_params(s->session, digest_cmd_params);
472 if (EVP_DigestUpdate(ctx, sender, len) <= 0
473 || EVP_MD_CTX_set_params(ctx, digest_cmd_params) <= 0
474 || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
475 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC,
476 ERR_R_INTERNAL_ERROR);
482 EVP_MD_CTX_free(ctx);
487 int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
488 size_t len, size_t *secret_size)
490 static const unsigned char *salt[3] = {
491 #ifndef CHARSET_EBCDIC
492 (const unsigned char *)"A",
493 (const unsigned char *)"BB",
494 (const unsigned char *)"CCC",
496 (const unsigned char *)"\x41",
497 (const unsigned char *)"\x42\x42",
498 (const unsigned char *)"\x43\x43\x43",
501 unsigned char buf[EVP_MAX_MD_SIZE];
502 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
505 size_t ret_secret_size = 0;
508 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_MASTER_SECRET,
509 ERR_R_MALLOC_FAILURE);
512 for (i = 0; i < 3; i++) {
513 if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
514 || EVP_DigestUpdate(ctx, salt[i],
515 strlen((const char *)salt[i])) <= 0
516 || EVP_DigestUpdate(ctx, p, len) <= 0
517 || EVP_DigestUpdate(ctx, &(s->s3.client_random[0]),
518 SSL3_RANDOM_SIZE) <= 0
519 || EVP_DigestUpdate(ctx, &(s->s3.server_random[0]),
520 SSL3_RANDOM_SIZE) <= 0
521 /* TODO(size_t) : convert me */
522 || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
523 || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
524 || EVP_DigestUpdate(ctx, p, len) <= 0
525 || EVP_DigestUpdate(ctx, buf, n) <= 0
526 || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
527 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
528 SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);
533 ret_secret_size += n;
535 EVP_MD_CTX_free(ctx);
537 OPENSSL_cleanse(buf, sizeof(buf));
539 *secret_size = ret_secret_size;
543 int ssl3_alert_code(int code)
546 case SSL_AD_CLOSE_NOTIFY:
547 return SSL3_AD_CLOSE_NOTIFY;
548 case SSL_AD_UNEXPECTED_MESSAGE:
549 return SSL3_AD_UNEXPECTED_MESSAGE;
550 case SSL_AD_BAD_RECORD_MAC:
551 return SSL3_AD_BAD_RECORD_MAC;
552 case SSL_AD_DECRYPTION_FAILED:
553 return SSL3_AD_BAD_RECORD_MAC;
554 case SSL_AD_RECORD_OVERFLOW:
555 return SSL3_AD_BAD_RECORD_MAC;
556 case SSL_AD_DECOMPRESSION_FAILURE:
557 return SSL3_AD_DECOMPRESSION_FAILURE;
558 case SSL_AD_HANDSHAKE_FAILURE:
559 return SSL3_AD_HANDSHAKE_FAILURE;
560 case SSL_AD_NO_CERTIFICATE:
561 return SSL3_AD_NO_CERTIFICATE;
562 case SSL_AD_BAD_CERTIFICATE:
563 return SSL3_AD_BAD_CERTIFICATE;
564 case SSL_AD_UNSUPPORTED_CERTIFICATE:
565 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
566 case SSL_AD_CERTIFICATE_REVOKED:
567 return SSL3_AD_CERTIFICATE_REVOKED;
568 case SSL_AD_CERTIFICATE_EXPIRED:
569 return SSL3_AD_CERTIFICATE_EXPIRED;
570 case SSL_AD_CERTIFICATE_UNKNOWN:
571 return SSL3_AD_CERTIFICATE_UNKNOWN;
572 case SSL_AD_ILLEGAL_PARAMETER:
573 return SSL3_AD_ILLEGAL_PARAMETER;
574 case SSL_AD_UNKNOWN_CA:
575 return SSL3_AD_BAD_CERTIFICATE;
576 case SSL_AD_ACCESS_DENIED:
577 return SSL3_AD_HANDSHAKE_FAILURE;
578 case SSL_AD_DECODE_ERROR:
579 return SSL3_AD_HANDSHAKE_FAILURE;
580 case SSL_AD_DECRYPT_ERROR:
581 return SSL3_AD_HANDSHAKE_FAILURE;
582 case SSL_AD_EXPORT_RESTRICTION:
583 return SSL3_AD_HANDSHAKE_FAILURE;
584 case SSL_AD_PROTOCOL_VERSION:
585 return SSL3_AD_HANDSHAKE_FAILURE;
586 case SSL_AD_INSUFFICIENT_SECURITY:
587 return SSL3_AD_HANDSHAKE_FAILURE;
588 case SSL_AD_INTERNAL_ERROR:
589 return SSL3_AD_HANDSHAKE_FAILURE;
590 case SSL_AD_USER_CANCELLED:
591 return SSL3_AD_HANDSHAKE_FAILURE;
592 case SSL_AD_NO_RENEGOTIATION:
593 return -1; /* Don't send it :-) */
594 case SSL_AD_UNSUPPORTED_EXTENSION:
595 return SSL3_AD_HANDSHAKE_FAILURE;
596 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
597 return SSL3_AD_HANDSHAKE_FAILURE;
598 case SSL_AD_UNRECOGNIZED_NAME:
599 return SSL3_AD_HANDSHAKE_FAILURE;
600 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
601 return SSL3_AD_HANDSHAKE_FAILURE;
602 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
603 return SSL3_AD_HANDSHAKE_FAILURE;
604 case SSL_AD_UNKNOWN_PSK_IDENTITY:
605 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
606 case SSL_AD_INAPPROPRIATE_FALLBACK:
607 return TLS1_AD_INAPPROPRIATE_FALLBACK;
608 case SSL_AD_NO_APPLICATION_PROTOCOL:
609 return TLS1_AD_NO_APPLICATION_PROTOCOL;
610 case SSL_AD_CERTIFICATE_REQUIRED:
611 return SSL_AD_HANDSHAKE_FAILURE;