2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the OpenSSL license (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
13 #include <openssl/evp.h>
14 #include <openssl/md5.h>
15 #include "internal/cryptlib.h"
17 static int ssl3_generate_key_block(SSL
*s
, unsigned char *km
, int num
)
21 unsigned char buf
[16], smd
[SHA_DIGEST_LENGTH
];
22 unsigned char c
= 'A';
27 c
= os_toascii
[c
]; /* 'A' in ASCII */
30 m5
= EVP_MD_CTX_new();
31 s1
= EVP_MD_CTX_new();
32 if (m5
== NULL
|| s1
== NULL
) {
33 SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK
, ERR_R_MALLOC_FAILURE
);
36 EVP_MD_CTX_set_flags(m5
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
37 for (i
= 0; (int)i
< num
; i
+= MD5_DIGEST_LENGTH
) {
39 if (k
> sizeof(buf
)) {
40 /* bug: 'buf' is too small for this ciphersuite */
41 SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK
, ERR_R_INTERNAL_ERROR
);
45 for (j
= 0; j
< k
; j
++)
48 if (!EVP_DigestInit_ex(s1
, EVP_sha1(), NULL
)
49 || !EVP_DigestUpdate(s1
, buf
, k
)
50 || !EVP_DigestUpdate(s1
, s
->session
->master_key
,
51 s
->session
->master_key_length
)
52 || !EVP_DigestUpdate(s1
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
)
53 || !EVP_DigestUpdate(s1
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
)
54 || !EVP_DigestFinal_ex(s1
, smd
, NULL
)
55 || !EVP_DigestInit_ex(m5
, EVP_md5(), NULL
)
56 || !EVP_DigestUpdate(m5
, s
->session
->master_key
,
57 s
->session
->master_key_length
)
58 || !EVP_DigestUpdate(m5
, smd
, SHA_DIGEST_LENGTH
))
60 if ((int)(i
+ MD5_DIGEST_LENGTH
) > num
) {
61 if (!EVP_DigestFinal_ex(m5
, smd
, NULL
))
63 memcpy(km
, smd
, (num
- i
));
65 if (!EVP_DigestFinal_ex(m5
, km
, NULL
))
69 km
+= MD5_DIGEST_LENGTH
;
71 OPENSSL_cleanse(smd
, sizeof(smd
));
79 int ssl3_change_cipher_state(SSL
*s
, int which
)
81 unsigned char *p
, *mac_secret
;
82 unsigned char exp_key
[EVP_MAX_KEY_LENGTH
];
83 unsigned char exp_iv
[EVP_MAX_IV_LENGTH
];
84 unsigned char *ms
, *key
, *iv
;
87 #ifndef OPENSSL_NO_COMP
92 size_t n
, i
, j
, k
, cl
;
95 c
= s
->s3
->tmp
.new_sym_enc
;
96 m
= s
->s3
->tmp
.new_hash
;
97 /* m == NULL will lead to a crash later */
98 if (!ossl_assert(m
!= NULL
)) {
99 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
102 #ifndef OPENSSL_NO_COMP
103 if (s
->s3
->tmp
.new_compression
== NULL
)
106 comp
= s
->s3
->tmp
.new_compression
->method
;
109 if (which
& SSL3_CC_READ
) {
110 if (s
->enc_read_ctx
!= NULL
)
112 else if ((s
->enc_read_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
116 * make sure it's initialised in case we exit later with an error
118 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
119 dd
= s
->enc_read_ctx
;
121 if (ssl_replace_hash(&s
->read_hash
, m
) == NULL
) {
122 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
125 #ifndef OPENSSL_NO_COMP
127 COMP_CTX_free(s
->expand
);
130 s
->expand
= COMP_CTX_new(comp
);
131 if (s
->expand
== NULL
) {
132 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
,
133 SSL_R_COMPRESSION_LIBRARY_ERROR
);
138 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
139 mac_secret
= &(s
->s3
->read_mac_secret
[0]);
141 if (s
->enc_write_ctx
!= NULL
)
143 else if ((s
->enc_write_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
147 * make sure it's initialised in case we exit later with an error
149 EVP_CIPHER_CTX_reset(s
->enc_write_ctx
);
150 dd
= s
->enc_write_ctx
;
151 if (ssl_replace_hash(&s
->write_hash
, m
) == NULL
) {
152 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
155 #ifndef OPENSSL_NO_COMP
157 COMP_CTX_free(s
->compress
);
160 s
->compress
= COMP_CTX_new(comp
);
161 if (s
->compress
== NULL
) {
162 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
,
163 SSL_R_COMPRESSION_LIBRARY_ERROR
);
168 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
169 mac_secret
= &(s
->s3
->write_mac_secret
[0]);
173 EVP_CIPHER_CTX_reset(dd
);
175 p
= s
->s3
->tmp
.key_block
;
176 mdi
= EVP_MD_size(m
);
180 cl
= EVP_CIPHER_key_length(c
);
182 k
= EVP_CIPHER_iv_length(c
);
183 if ((which
== SSL3_CHANGE_CIPHER_CLIENT_WRITE
) ||
184 (which
== SSL3_CHANGE_CIPHER_SERVER_READ
)) {
201 if (n
> s
->s3
->tmp
.key_block_length
) {
202 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
206 memcpy(mac_secret
, ms
, i
);
208 if (!EVP_CipherInit_ex(dd
, c
, NULL
, key
, iv
, (which
& SSL3_CC_WRITE
)))
211 OPENSSL_cleanse(exp_key
, sizeof(exp_key
));
212 OPENSSL_cleanse(exp_iv
, sizeof(exp_iv
));
215 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
217 OPENSSL_cleanse(exp_key
, sizeof(exp_key
));
218 OPENSSL_cleanse(exp_iv
, sizeof(exp_iv
));
222 int ssl3_setup_key_block(SSL
*s
)
231 if (s
->s3
->tmp
.key_block_length
!= 0)
234 if (!ssl_cipher_get_evp(s
->session
, &c
, &hash
, NULL
, NULL
, &comp
, 0)) {
235 SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK
, SSL_R_CIPHER_OR_HASH_UNAVAILABLE
);
239 s
->s3
->tmp
.new_sym_enc
= c
;
240 s
->s3
->tmp
.new_hash
= hash
;
241 #ifdef OPENSSL_NO_COMP
242 s
->s3
->tmp
.new_compression
= NULL
;
244 s
->s3
->tmp
.new_compression
= comp
;
247 num
= EVP_MD_size(hash
);
251 num
= EVP_CIPHER_key_length(c
) + num
+ EVP_CIPHER_iv_length(c
);
254 ssl3_cleanup_key_block(s
);
256 if ((p
= OPENSSL_malloc(num
)) == NULL
)
259 s
->s3
->tmp
.key_block_length
= num
;
260 s
->s3
->tmp
.key_block
= p
;
262 ret
= ssl3_generate_key_block(s
, p
, num
);
264 if (!(s
->options
& SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
)) {
266 * enable vulnerability countermeasure for CBC ciphers with known-IV
267 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
269 s
->s3
->need_empty_fragments
= 1;
271 if (s
->session
->cipher
!= NULL
) {
272 if (s
->session
->cipher
->algorithm_enc
== SSL_eNULL
)
273 s
->s3
->need_empty_fragments
= 0;
275 #ifndef OPENSSL_NO_RC4
276 if (s
->session
->cipher
->algorithm_enc
== SSL_RC4
)
277 s
->s3
->need_empty_fragments
= 0;
285 SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK
, ERR_R_MALLOC_FAILURE
);
289 void ssl3_cleanup_key_block(SSL
*s
)
291 OPENSSL_clear_free(s
->s3
->tmp
.key_block
, s
->s3
->tmp
.key_block_length
);
292 s
->s3
->tmp
.key_block
= NULL
;
293 s
->s3
->tmp
.key_block_length
= 0;
296 int ssl3_init_finished_mac(SSL
*s
)
298 BIO
*buf
= BIO_new(BIO_s_mem());
301 SSLerr(SSL_F_SSL3_INIT_FINISHED_MAC
, ERR_R_MALLOC_FAILURE
);
304 ssl3_free_digest_list(s
);
305 s
->s3
->handshake_buffer
= buf
;
306 (void)BIO_set_close(s
->s3
->handshake_buffer
, BIO_CLOSE
);
311 * Free digest list. Also frees handshake buffer since they are always freed
315 void ssl3_free_digest_list(SSL
*s
)
317 BIO_free(s
->s3
->handshake_buffer
);
318 s
->s3
->handshake_buffer
= NULL
;
319 EVP_MD_CTX_free(s
->s3
->handshake_dgst
);
320 s
->s3
->handshake_dgst
= NULL
;
323 int ssl3_finish_mac(SSL
*s
, const unsigned char *buf
, size_t len
)
325 if (s
->s3
->handshake_dgst
== NULL
) {
327 /* Note: this writes to a memory BIO so a failure is a fatal error */
330 ret
= BIO_write(s
->s3
->handshake_buffer
, (void *)buf
, (int)len
);
331 return ret
> 0 && ret
== (int)len
;
333 return EVP_DigestUpdate(s
->s3
->handshake_dgst
, buf
, len
);
337 int ssl3_digest_cached_records(SSL
*s
, int keep
)
343 if (s
->s3
->handshake_dgst
== NULL
) {
344 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
346 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
,
347 SSL_R_BAD_HANDSHAKE_LENGTH
);
351 s
->s3
->handshake_dgst
= EVP_MD_CTX_new();
352 if (s
->s3
->handshake_dgst
== NULL
) {
353 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
, ERR_R_MALLOC_FAILURE
);
357 md
= ssl_handshake_md(s
);
358 if (md
== NULL
|| !EVP_DigestInit_ex(s
->s3
->handshake_dgst
, md
, NULL
)
359 || !EVP_DigestUpdate(s
->s3
->handshake_dgst
, hdata
, hdatalen
)) {
360 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
, ERR_R_INTERNAL_ERROR
);
365 BIO_free(s
->s3
->handshake_buffer
);
366 s
->s3
->handshake_buffer
= NULL
;
372 size_t ssl3_final_finish_mac(SSL
*s
, const char *sender
, size_t len
,
376 EVP_MD_CTX
*ctx
= NULL
;
378 if (!ssl3_digest_cached_records(s
, 0))
381 if (EVP_MD_CTX_type(s
->s3
->handshake_dgst
) != NID_md5_sha1
) {
382 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, SSL_R_NO_REQUIRED_DIGEST
);
386 ctx
= EVP_MD_CTX_new();
388 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_MALLOC_FAILURE
);
391 if (!EVP_MD_CTX_copy_ex(ctx
, s
->s3
->handshake_dgst
)) {
392 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_INTERNAL_ERROR
);
396 ret
= EVP_MD_CTX_size(ctx
);
398 EVP_MD_CTX_reset(ctx
);
402 if ((sender
!= NULL
&& EVP_DigestUpdate(ctx
, sender
, len
) <= 0)
403 || EVP_MD_CTX_ctrl(ctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
404 (int)s
->session
->master_key_length
,
405 s
->session
->master_key
) <= 0
406 || EVP_DigestFinal_ex(ctx
, p
, NULL
) <= 0) {
407 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_INTERNAL_ERROR
);
411 EVP_MD_CTX_free(ctx
);
416 int ssl3_generate_master_secret(SSL
*s
, unsigned char *out
, unsigned char *p
,
417 size_t len
, size_t *secret_size
)
419 static const unsigned char *salt
[3] = {
420 #ifndef CHARSET_EBCDIC
421 (const unsigned char *)"A",
422 (const unsigned char *)"BB",
423 (const unsigned char *)"CCC",
425 (const unsigned char *)"\x41",
426 (const unsigned char *)"\x42\x42",
427 (const unsigned char *)"\x43\x43\x43",
430 unsigned char buf
[EVP_MAX_MD_SIZE
];
431 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
434 size_t ret_secret_size
= 0;
437 SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET
, ERR_R_MALLOC_FAILURE
);
440 for (i
= 0; i
< 3; i
++) {
441 if (EVP_DigestInit_ex(ctx
, s
->ctx
->sha1
, NULL
) <= 0
442 || EVP_DigestUpdate(ctx
, salt
[i
],
443 strlen((const char *)salt
[i
])) <= 0
444 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
445 || EVP_DigestUpdate(ctx
, &(s
->s3
->client_random
[0]),
446 SSL3_RANDOM_SIZE
) <= 0
447 || EVP_DigestUpdate(ctx
, &(s
->s3
->server_random
[0]),
448 SSL3_RANDOM_SIZE
) <= 0
449 /* TODO(size_t) : convert me */
450 || EVP_DigestFinal_ex(ctx
, buf
, &n
) <= 0
451 || EVP_DigestInit_ex(ctx
, s
->ctx
->md5
, NULL
) <= 0
452 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
453 || EVP_DigestUpdate(ctx
, buf
, n
) <= 0
454 || EVP_DigestFinal_ex(ctx
, out
, &n
) <= 0) {
455 SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET
, ERR_R_INTERNAL_ERROR
);
460 ret_secret_size
+= n
;
462 EVP_MD_CTX_free(ctx
);
464 OPENSSL_cleanse(buf
, sizeof(buf
));
466 *secret_size
= ret_secret_size
;
470 int ssl3_alert_code(int code
)
473 case SSL_AD_CLOSE_NOTIFY
:
474 return (SSL3_AD_CLOSE_NOTIFY
);
475 case SSL_AD_UNEXPECTED_MESSAGE
:
476 return (SSL3_AD_UNEXPECTED_MESSAGE
);
477 case SSL_AD_BAD_RECORD_MAC
:
478 return (SSL3_AD_BAD_RECORD_MAC
);
479 case SSL_AD_DECRYPTION_FAILED
:
480 return (SSL3_AD_BAD_RECORD_MAC
);
481 case SSL_AD_RECORD_OVERFLOW
:
482 return (SSL3_AD_BAD_RECORD_MAC
);
483 case SSL_AD_DECOMPRESSION_FAILURE
:
484 return (SSL3_AD_DECOMPRESSION_FAILURE
);
485 case SSL_AD_HANDSHAKE_FAILURE
:
486 return (SSL3_AD_HANDSHAKE_FAILURE
);
487 case SSL_AD_NO_CERTIFICATE
:
488 return (SSL3_AD_NO_CERTIFICATE
);
489 case SSL_AD_BAD_CERTIFICATE
:
490 return (SSL3_AD_BAD_CERTIFICATE
);
491 case SSL_AD_UNSUPPORTED_CERTIFICATE
:
492 return (SSL3_AD_UNSUPPORTED_CERTIFICATE
);
493 case SSL_AD_CERTIFICATE_REVOKED
:
494 return (SSL3_AD_CERTIFICATE_REVOKED
);
495 case SSL_AD_CERTIFICATE_EXPIRED
:
496 return (SSL3_AD_CERTIFICATE_EXPIRED
);
497 case SSL_AD_CERTIFICATE_UNKNOWN
:
498 return (SSL3_AD_CERTIFICATE_UNKNOWN
);
499 case SSL_AD_ILLEGAL_PARAMETER
:
500 return (SSL3_AD_ILLEGAL_PARAMETER
);
501 case SSL_AD_UNKNOWN_CA
:
502 return (SSL3_AD_BAD_CERTIFICATE
);
503 case SSL_AD_ACCESS_DENIED
:
504 return (SSL3_AD_HANDSHAKE_FAILURE
);
505 case SSL_AD_DECODE_ERROR
:
506 return (SSL3_AD_HANDSHAKE_FAILURE
);
507 case SSL_AD_DECRYPT_ERROR
:
508 return (SSL3_AD_HANDSHAKE_FAILURE
);
509 case SSL_AD_EXPORT_RESTRICTION
:
510 return (SSL3_AD_HANDSHAKE_FAILURE
);
511 case SSL_AD_PROTOCOL_VERSION
:
512 return (SSL3_AD_HANDSHAKE_FAILURE
);
513 case SSL_AD_INSUFFICIENT_SECURITY
:
514 return (SSL3_AD_HANDSHAKE_FAILURE
);
515 case SSL_AD_INTERNAL_ERROR
:
516 return (SSL3_AD_HANDSHAKE_FAILURE
);
517 case SSL_AD_USER_CANCELLED
:
518 return (SSL3_AD_HANDSHAKE_FAILURE
);
519 case SSL_AD_NO_RENEGOTIATION
:
520 return (-1); /* Don't send it :-) */
521 case SSL_AD_UNSUPPORTED_EXTENSION
:
522 return (SSL3_AD_HANDSHAKE_FAILURE
);
523 case SSL_AD_CERTIFICATE_UNOBTAINABLE
:
524 return (SSL3_AD_HANDSHAKE_FAILURE
);
525 case SSL_AD_UNRECOGNIZED_NAME
:
526 return (SSL3_AD_HANDSHAKE_FAILURE
);
527 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
:
528 return (SSL3_AD_HANDSHAKE_FAILURE
);
529 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE
:
530 return (SSL3_AD_HANDSHAKE_FAILURE
);
531 case SSL_AD_UNKNOWN_PSK_IDENTITY
:
532 return (TLS1_AD_UNKNOWN_PSK_IDENTITY
);
533 case SSL_AD_INAPPROPRIATE_FALLBACK
:
534 return (TLS1_AD_INAPPROPRIATE_FALLBACK
);
535 case SSL_AD_NO_APPLICATION_PROTOCOL
:
536 return (TLS1_AD_NO_APPLICATION_PROTOCOL
);
537 case SSL_AD_CERTIFICATE_REQUIRED
:
538 return SSL_AD_HANDSHAKE_FAILURE
;