2 * Copyright 1995-2021 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_CONNECTION
*s
, unsigned char *km
, int num
)
20 const EVP_MD
*md5
= NULL
, *sha1
= NULL
;
23 unsigned char buf
[16], smd
[SHA_DIGEST_LENGTH
];
24 unsigned char c
= 'A';
27 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
30 c
= os_toascii
[c
]; /* 'A' in ASCII */
33 md5
= ssl_evp_md_fetch(sctx
->libctx
, NID_md5
, sctx
->propq
);
34 sha1
= ssl_evp_md_fetch(sctx
->libctx
, NID_sha1
, sctx
->propq
);
35 m5
= EVP_MD_CTX_new();
36 s1
= EVP_MD_CTX_new();
37 if (md5
== NULL
|| sha1
== NULL
|| m5
== NULL
|| s1
== NULL
) {
38 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
41 for (i
= 0; (int)i
< num
; i
+= MD5_DIGEST_LENGTH
) {
43 if (k
> sizeof(buf
)) {
44 /* bug: 'buf' is too small for this ciphersuite */
45 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
51 if (!EVP_DigestInit_ex(s1
, sha1
, NULL
)
52 || !EVP_DigestUpdate(s1
, buf
, k
)
53 || !EVP_DigestUpdate(s1
, s
->session
->master_key
,
54 s
->session
->master_key_length
)
55 || !EVP_DigestUpdate(s1
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
)
56 || !EVP_DigestUpdate(s1
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
)
57 || !EVP_DigestFinal_ex(s1
, smd
, NULL
)
58 || !EVP_DigestInit_ex(m5
, md5
, NULL
)
59 || !EVP_DigestUpdate(m5
, s
->session
->master_key
,
60 s
->session
->master_key_length
)
61 || !EVP_DigestUpdate(m5
, smd
, SHA_DIGEST_LENGTH
)) {
62 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
65 if ((int)(i
+ MD5_DIGEST_LENGTH
) > num
) {
66 if (!EVP_DigestFinal_ex(m5
, smd
, NULL
)) {
67 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
70 memcpy(km
, smd
, (num
- i
));
72 if (!EVP_DigestFinal_ex(m5
, km
, NULL
)) {
73 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
78 km
+= MD5_DIGEST_LENGTH
;
80 OPENSSL_cleanse(smd
, sizeof(smd
));
86 ssl_evp_md_free(sha1
);
90 int ssl3_change_cipher_state(SSL_CONNECTION
*s
, int which
)
92 unsigned char *p
, *mac_secret
;
94 unsigned char *key
, *iv
;
96 const EVP_CIPHER
*ciph
;
97 const SSL_COMP
*comp
= NULL
;
100 size_t n
, iv_len
, key_len
;
102 int direction
= (which
& SSL3_CC_READ
) != 0 ? OSSL_RECORD_DIRECTION_READ
103 : OSSL_RECORD_DIRECTION_WRITE
;
105 ciph
= s
->s3
.tmp
.new_sym_enc
;
106 md
= s
->s3
.tmp
.new_hash
;
107 /* m == NULL will lead to a crash later */
108 if (!ossl_assert(md
!= NULL
)) {
109 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
112 #ifndef OPENSSL_NO_COMP
113 comp
= s
->s3
.tmp
.new_compression
;
116 p
= s
->s3
.tmp
.key_block
;
117 mdi
= EVP_MD_get_size(md
);
119 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
122 md_len
= (size_t)mdi
;
123 key_len
= EVP_CIPHER_get_key_length(ciph
);
124 iv_len
= EVP_CIPHER_get_iv_length(ciph
);
125 if ((which
== SSL3_CHANGE_CIPHER_CLIENT_WRITE
) ||
126 (which
== SSL3_CHANGE_CIPHER_SERVER_READ
)) {
127 mac_secret
= &(p
[0]);
130 n
+= key_len
+ key_len
;
132 n
+= iv_len
+ iv_len
;
135 mac_secret
= &(p
[n
]);
136 n
+= md_len
+ key_len
;
138 n
+= key_len
+ iv_len
;
143 if (n
> s
->s3
.tmp
.key_block_length
) {
144 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
148 if (!ssl_set_new_record_layer(s
, SSL3_VERSION
,
150 OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
,
151 key
, key_len
, iv
, iv_len
, mac_secret
,
152 md_len
, ciph
, 0, NID_undef
, md
, comp
)) {
153 /* SSLfatal already called */
157 if (which
& SSL3_CC_READ
) {
158 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
162 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
163 if (s
->enc_write_ctx
!= NULL
) {
165 } else if ((s
->enc_write_ctx
= EVP_CIPHER_CTX_new()) == NULL
) {
166 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
169 /* 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
, md
) == NULL
) {
174 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
177 #ifndef OPENSSL_NO_COMP
179 COMP_CTX_free(s
->compress
);
182 s
->compress
= COMP_CTX_new(comp
->method
);
183 if (s
->compress
== NULL
) {
184 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
185 SSL_R_COMPRESSION_LIBRARY_ERROR
);
190 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
191 memcpy(&(s
->s3
.write_mac_secret
[0]), mac_secret
, md_len
);
194 EVP_CIPHER_CTX_reset(dd
);
196 if (!EVP_CipherInit_ex(dd
, ciph
, NULL
, key
, iv
, (which
& SSL3_CC_WRITE
))) {
197 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
201 if (EVP_CIPHER_get0_provider(ciph
) != NULL
202 && !tls_provider_set_tls_params(s
, dd
, ciph
, md
)) {
203 /* SSLfatal already called */
207 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
213 int ssl3_setup_key_block(SSL_CONNECTION
*s
)
222 if (s
->s3
.tmp
.key_block_length
!= 0)
225 if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s
), s
->session
, &c
, &hash
,
226 NULL
, NULL
, &comp
, 0)) {
227 /* Error is already recorded */
228 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
232 ssl_evp_cipher_free(s
->s3
.tmp
.new_sym_enc
);
233 s
->s3
.tmp
.new_sym_enc
= c
;
234 ssl_evp_md_free(s
->s3
.tmp
.new_hash
);
235 s
->s3
.tmp
.new_hash
= hash
;
236 #ifdef OPENSSL_NO_COMP
237 s
->s3
.tmp
.new_compression
= NULL
;
239 s
->s3
.tmp
.new_compression
= comp
;
242 num
= EVP_MD_get_size(hash
);
246 num
= EVP_CIPHER_get_key_length(c
) + num
+ EVP_CIPHER_get_iv_length(c
);
249 ssl3_cleanup_key_block(s
);
251 if ((p
= OPENSSL_malloc(num
)) == NULL
) {
252 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
256 s
->s3
.tmp
.key_block_length
= num
;
257 s
->s3
.tmp
.key_block
= p
;
259 /* Calls SSLfatal() as required */
260 ret
= ssl3_generate_key_block(s
, p
, num
);
265 void ssl3_cleanup_key_block(SSL_CONNECTION
*s
)
267 OPENSSL_clear_free(s
->s3
.tmp
.key_block
, s
->s3
.tmp
.key_block_length
);
268 s
->s3
.tmp
.key_block
= NULL
;
269 s
->s3
.tmp
.key_block_length
= 0;
272 int ssl3_init_finished_mac(SSL_CONNECTION
*s
)
274 BIO
*buf
= BIO_new(BIO_s_mem());
277 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
280 ssl3_free_digest_list(s
);
281 s
->s3
.handshake_buffer
= buf
;
282 (void)BIO_set_close(s
->s3
.handshake_buffer
, BIO_CLOSE
);
287 * Free digest list. Also frees handshake buffer since they are always freed
291 void ssl3_free_digest_list(SSL_CONNECTION
*s
)
293 BIO_free(s
->s3
.handshake_buffer
);
294 s
->s3
.handshake_buffer
= NULL
;
295 EVP_MD_CTX_free(s
->s3
.handshake_dgst
);
296 s
->s3
.handshake_dgst
= NULL
;
299 int ssl3_finish_mac(SSL_CONNECTION
*s
, const unsigned char *buf
, size_t len
)
303 if (s
->s3
.handshake_dgst
== NULL
) {
304 /* Note: this writes to a memory BIO so a failure is a fatal error */
306 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_OVERFLOW_ERROR
);
309 ret
= BIO_write(s
->s3
.handshake_buffer
, (void *)buf
, (int)len
);
310 if (ret
<= 0 || ret
!= (int)len
) {
311 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
315 ret
= EVP_DigestUpdate(s
->s3
.handshake_dgst
, buf
, len
);
317 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
324 int ssl3_digest_cached_records(SSL_CONNECTION
*s
, int keep
)
330 if (s
->s3
.handshake_dgst
== NULL
) {
331 hdatalen
= BIO_get_mem_data(s
->s3
.handshake_buffer
, &hdata
);
333 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_LENGTH
);
337 s
->s3
.handshake_dgst
= EVP_MD_CTX_new();
338 if (s
->s3
.handshake_dgst
== NULL
) {
339 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
343 md
= ssl_handshake_md(s
);
345 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
346 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM
);
349 if (!EVP_DigestInit_ex(s
->s3
.handshake_dgst
, md
, NULL
)
350 || !EVP_DigestUpdate(s
->s3
.handshake_dgst
, hdata
, hdatalen
)) {
351 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
356 BIO_free(s
->s3
.handshake_buffer
);
357 s
->s3
.handshake_buffer
= NULL
;
363 void ssl3_digest_master_key_set_params(const SSL_SESSION
*session
,
367 params
[n
++] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
368 (void *)session
->master_key
,
369 session
->master_key_length
);
370 params
[n
++] = OSSL_PARAM_construct_end();
373 size_t ssl3_final_finish_mac(SSL_CONNECTION
*s
, const char *sender
, size_t len
,
377 EVP_MD_CTX
*ctx
= NULL
;
379 if (!ssl3_digest_cached_records(s
, 0)) {
380 /* SSLfatal() already called */
384 if (EVP_MD_CTX_get_type(s
->s3
.handshake_dgst
) != NID_md5_sha1
) {
385 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_REQUIRED_DIGEST
);
389 ctx
= EVP_MD_CTX_new();
391 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
394 if (!EVP_MD_CTX_copy_ex(ctx
, s
->s3
.handshake_dgst
)) {
395 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
400 ret
= EVP_MD_CTX_get_size(ctx
);
402 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
407 if (sender
!= NULL
) {
408 OSSL_PARAM digest_cmd_params
[3];
410 ssl3_digest_master_key_set_params(s
->session
, digest_cmd_params
);
412 if (EVP_DigestUpdate(ctx
, sender
, len
) <= 0
413 || EVP_MD_CTX_set_params(ctx
, digest_cmd_params
) <= 0
414 || EVP_DigestFinal_ex(ctx
, p
, NULL
) <= 0) {
415 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
421 EVP_MD_CTX_free(ctx
);
426 int ssl3_generate_master_secret(SSL_CONNECTION
*s
, unsigned char *out
,
428 size_t len
, size_t *secret_size
)
430 static const unsigned char *salt
[3] = {
431 #ifndef CHARSET_EBCDIC
432 (const unsigned char *)"A",
433 (const unsigned char *)"BB",
434 (const unsigned char *)"CCC",
436 (const unsigned char *)"\x41",
437 (const unsigned char *)"\x42\x42",
438 (const unsigned char *)"\x43\x43\x43",
441 unsigned char buf
[EVP_MAX_MD_SIZE
];
442 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
445 size_t ret_secret_size
= 0;
448 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
451 for (i
= 0; i
< 3; i
++) {
452 if (EVP_DigestInit_ex(ctx
, SSL_CONNECTION_GET_CTX(s
)->sha1
, NULL
) <= 0
453 || EVP_DigestUpdate(ctx
, salt
[i
],
454 strlen((const char *)salt
[i
])) <= 0
455 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
456 || EVP_DigestUpdate(ctx
, &(s
->s3
.client_random
[0]),
457 SSL3_RANDOM_SIZE
) <= 0
458 || EVP_DigestUpdate(ctx
, &(s
->s3
.server_random
[0]),
459 SSL3_RANDOM_SIZE
) <= 0
460 || EVP_DigestFinal_ex(ctx
, buf
, &n
) <= 0
461 || EVP_DigestInit_ex(ctx
, SSL_CONNECTION_GET_CTX(s
)->md5
, NULL
) <= 0
462 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
463 || EVP_DigestUpdate(ctx
, buf
, n
) <= 0
464 || EVP_DigestFinal_ex(ctx
, out
, &n
) <= 0) {
465 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
470 ret_secret_size
+= n
;
472 EVP_MD_CTX_free(ctx
);
474 OPENSSL_cleanse(buf
, sizeof(buf
));
476 *secret_size
= ret_secret_size
;
480 int ssl3_alert_code(int code
)
483 case SSL_AD_CLOSE_NOTIFY
:
484 return SSL3_AD_CLOSE_NOTIFY
;
485 case SSL_AD_UNEXPECTED_MESSAGE
:
486 return SSL3_AD_UNEXPECTED_MESSAGE
;
487 case SSL_AD_BAD_RECORD_MAC
:
488 return SSL3_AD_BAD_RECORD_MAC
;
489 case SSL_AD_DECRYPTION_FAILED
:
490 return SSL3_AD_BAD_RECORD_MAC
;
491 case SSL_AD_RECORD_OVERFLOW
:
492 return SSL3_AD_BAD_RECORD_MAC
;
493 case SSL_AD_DECOMPRESSION_FAILURE
:
494 return SSL3_AD_DECOMPRESSION_FAILURE
;
495 case SSL_AD_HANDSHAKE_FAILURE
:
496 return SSL3_AD_HANDSHAKE_FAILURE
;
497 case SSL_AD_NO_CERTIFICATE
:
498 return SSL3_AD_NO_CERTIFICATE
;
499 case SSL_AD_BAD_CERTIFICATE
:
500 return SSL3_AD_BAD_CERTIFICATE
;
501 case SSL_AD_UNSUPPORTED_CERTIFICATE
:
502 return SSL3_AD_UNSUPPORTED_CERTIFICATE
;
503 case SSL_AD_CERTIFICATE_REVOKED
:
504 return SSL3_AD_CERTIFICATE_REVOKED
;
505 case SSL_AD_CERTIFICATE_EXPIRED
:
506 return SSL3_AD_CERTIFICATE_EXPIRED
;
507 case SSL_AD_CERTIFICATE_UNKNOWN
:
508 return SSL3_AD_CERTIFICATE_UNKNOWN
;
509 case SSL_AD_ILLEGAL_PARAMETER
:
510 return SSL3_AD_ILLEGAL_PARAMETER
;
511 case SSL_AD_UNKNOWN_CA
:
512 return SSL3_AD_BAD_CERTIFICATE
;
513 case SSL_AD_ACCESS_DENIED
:
514 return SSL3_AD_HANDSHAKE_FAILURE
;
515 case SSL_AD_DECODE_ERROR
:
516 return SSL3_AD_HANDSHAKE_FAILURE
;
517 case SSL_AD_DECRYPT_ERROR
:
518 return SSL3_AD_HANDSHAKE_FAILURE
;
519 case SSL_AD_EXPORT_RESTRICTION
:
520 return SSL3_AD_HANDSHAKE_FAILURE
;
521 case SSL_AD_PROTOCOL_VERSION
:
522 return SSL3_AD_HANDSHAKE_FAILURE
;
523 case SSL_AD_INSUFFICIENT_SECURITY
:
524 return SSL3_AD_HANDSHAKE_FAILURE
;
525 case SSL_AD_INTERNAL_ERROR
:
526 return SSL3_AD_HANDSHAKE_FAILURE
;
527 case SSL_AD_USER_CANCELLED
:
528 return SSL3_AD_HANDSHAKE_FAILURE
;
529 case SSL_AD_NO_RENEGOTIATION
:
530 return -1; /* Don't send it :-) */
531 case SSL_AD_UNSUPPORTED_EXTENSION
:
532 return SSL3_AD_HANDSHAKE_FAILURE
;
533 case SSL_AD_CERTIFICATE_UNOBTAINABLE
:
534 return SSL3_AD_HANDSHAKE_FAILURE
;
535 case SSL_AD_UNRECOGNIZED_NAME
:
536 return SSL3_AD_HANDSHAKE_FAILURE
;
537 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
:
538 return SSL3_AD_HANDSHAKE_FAILURE
;
539 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE
:
540 return SSL3_AD_HANDSHAKE_FAILURE
;
541 case SSL_AD_UNKNOWN_PSK_IDENTITY
:
542 return TLS1_AD_UNKNOWN_PSK_IDENTITY
;
543 case SSL_AD_INAPPROPRIATE_FALLBACK
:
544 return TLS1_AD_INAPPROPRIATE_FALLBACK
;
545 case SSL_AD_NO_APPLICATION_PROTOCOL
:
546 return TLS1_AD_NO_APPLICATION_PROTOCOL
;
547 case SSL_AD_CERTIFICATE_REQUIRED
:
548 return SSL_AD_HANDSHAKE_FAILURE
;
549 case TLS13_AD_MISSING_EXTENSION
:
550 return SSL_AD_HANDSHAKE_FAILURE
;