2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* ====================================================================
11 * Copyright 2005 Nokia. All rights reserved.
13 * The portions of the attached software ("Contribution") is developed by
14 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
17 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
18 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
19 * support (see RFC 4279) to OpenSSL.
21 * No patent licenses or other rights except those expressly stated in
22 * the OpenSSL open source license shall be deemed granted or received
23 * expressly, by implication, estoppel, or otherwise.
25 * No assurances are provided by Nokia that the Contribution does not
26 * infringe the patent or other intellectual property rights of any third
27 * party or that the license provides you with all the necessary rights
28 * to make use of the Contribution.
30 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
31 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
32 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
33 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
39 #include <openssl/evp.h>
40 #include <openssl/md5.h>
42 static int ssl3_generate_key_block(SSL
*s
, unsigned char *km
, int num
)
46 unsigned char buf
[16], smd
[SHA_DIGEST_LENGTH
];
47 unsigned char c
= 'A';
52 c
= os_toascii
[c
]; /* 'A' in ASCII */
55 m5
= EVP_MD_CTX_new();
56 s1
= EVP_MD_CTX_new();
57 if (m5
== NULL
|| s1
== NULL
) {
58 SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK
, ERR_R_MALLOC_FAILURE
);
61 EVP_MD_CTX_set_flags(m5
, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
62 for (i
= 0; (int)i
< num
; i
+= MD5_DIGEST_LENGTH
) {
65 /* bug: 'buf' is too small for this ciphersuite */
66 SSLerr(SSL_F_SSL3_GENERATE_KEY_BLOCK
, ERR_R_INTERNAL_ERROR
);
70 for (j
= 0; j
< k
; j
++)
73 if (!EVP_DigestInit_ex(s1
, EVP_sha1(), NULL
)
74 || !EVP_DigestUpdate(s1
, buf
, k
)
75 || !EVP_DigestUpdate(s1
, s
->session
->master_key
,
76 s
->session
->master_key_length
)
77 || !EVP_DigestUpdate(s1
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
)
78 || !EVP_DigestUpdate(s1
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
)
79 || !EVP_DigestFinal_ex(s1
, smd
, NULL
)
80 || !EVP_DigestInit_ex(m5
, EVP_md5(), NULL
)
81 || !EVP_DigestUpdate(m5
, s
->session
->master_key
,
82 s
->session
->master_key_length
)
83 || !EVP_DigestUpdate(m5
, smd
, SHA_DIGEST_LENGTH
))
85 if ((int)(i
+ MD5_DIGEST_LENGTH
) > num
) {
86 if (!EVP_DigestFinal_ex(m5
, smd
, NULL
))
88 memcpy(km
, smd
, (num
- i
));
90 if (!EVP_DigestFinal_ex(m5
, km
, NULL
))
94 km
+= MD5_DIGEST_LENGTH
;
96 OPENSSL_cleanse(smd
, sizeof(smd
));
104 int ssl3_change_cipher_state(SSL
*s
, int which
)
106 unsigned char *p
, *mac_secret
;
107 unsigned char exp_key
[EVP_MAX_KEY_LENGTH
];
108 unsigned char exp_iv
[EVP_MAX_IV_LENGTH
];
109 unsigned char *ms
, *key
, *iv
;
112 #ifndef OPENSSL_NO_COMP
117 size_t n
, i
, j
, k
, cl
;
120 c
= s
->s3
->tmp
.new_sym_enc
;
121 m
= s
->s3
->tmp
.new_hash
;
122 /* m == NULL will lead to a crash later */
124 #ifndef OPENSSL_NO_COMP
125 if (s
->s3
->tmp
.new_compression
== NULL
)
128 comp
= s
->s3
->tmp
.new_compression
->method
;
131 if (which
& SSL3_CC_READ
) {
132 if (s
->enc_read_ctx
!= NULL
)
134 else if ((s
->enc_read_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
138 * make sure it's initialised in case we exit later with an error
140 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
141 dd
= s
->enc_read_ctx
;
143 if (ssl_replace_hash(&s
->read_hash
, m
) == NULL
) {
144 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
147 #ifndef OPENSSL_NO_COMP
149 COMP_CTX_free(s
->expand
);
152 s
->expand
= COMP_CTX_new(comp
);
153 if (s
->expand
== NULL
) {
154 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
,
155 SSL_R_COMPRESSION_LIBRARY_ERROR
);
160 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
161 mac_secret
= &(s
->s3
->read_mac_secret
[0]);
163 if (s
->enc_write_ctx
!= NULL
)
165 else if ((s
->enc_write_ctx
= EVP_CIPHER_CTX_new()) == NULL
)
169 * make sure it's initialised in case we exit later with an error
171 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 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
177 #ifndef OPENSSL_NO_COMP
179 COMP_CTX_free(s
->compress
);
182 s
->compress
= COMP_CTX_new(comp
);
183 if (s
->compress
== NULL
) {
184 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
,
185 SSL_R_COMPRESSION_LIBRARY_ERROR
);
190 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
191 mac_secret
= &(s
->s3
->write_mac_secret
[0]);
195 EVP_CIPHER_CTX_reset(dd
);
197 p
= s
->s3
->tmp
.key_block
;
198 mdi
= EVP_MD_size(m
);
202 cl
= EVP_CIPHER_key_length(c
);
204 k
= EVP_CIPHER_iv_length(c
);
205 if ((which
== SSL3_CHANGE_CIPHER_CLIENT_WRITE
) ||
206 (which
== SSL3_CHANGE_CIPHER_SERVER_READ
)) {
223 if (n
> s
->s3
->tmp
.key_block_length
) {
224 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
228 memcpy(mac_secret
, ms
, i
);
230 EVP_CipherInit_ex(dd
, c
, NULL
, key
, iv
, (which
& SSL3_CC_WRITE
));
232 #ifdef OPENSSL_SSL_TRACE_CRYPTO
233 if (s
->msg_callback
) {
235 int wh
= which
& SSL3_CC_WRITE
?
236 TLS1_RT_CRYPTO_WRITE
: TLS1_RT_CRYPTO_READ
;
237 s
->msg_callback(2, s
->version
, wh
| TLS1_RT_CRYPTO_MAC
,
238 mac_secret
, EVP_MD_size(m
), s
, s
->msg_callback_arg
);
240 s
->msg_callback(2, s
->version
, wh
| TLS1_RT_CRYPTO_KEY
,
241 key
, c
->key_len
, s
, s
->msg_callback_arg
);
243 s
->msg_callback(2, s
->version
, wh
| TLS1_RT_CRYPTO_IV
,
244 iv
, k
, s
, s
->msg_callback_arg
);
249 OPENSSL_cleanse(exp_key
, sizeof(exp_key
));
250 OPENSSL_cleanse(exp_iv
, sizeof(exp_iv
));
253 SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
255 OPENSSL_cleanse(exp_key
, sizeof(exp_key
));
256 OPENSSL_cleanse(exp_iv
, sizeof(exp_iv
));
260 int ssl3_setup_key_block(SSL
*s
)
269 if (s
->s3
->tmp
.key_block_length
!= 0)
272 if (!ssl_cipher_get_evp(s
->session
, &c
, &hash
, NULL
, NULL
, &comp
, 0)) {
273 SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK
, SSL_R_CIPHER_OR_HASH_UNAVAILABLE
);
277 s
->s3
->tmp
.new_sym_enc
= c
;
278 s
->s3
->tmp
.new_hash
= hash
;
279 #ifdef OPENSSL_NO_COMP
280 s
->s3
->tmp
.new_compression
= NULL
;
282 s
->s3
->tmp
.new_compression
= comp
;
285 num
= EVP_MD_size(hash
);
289 num
= EVP_CIPHER_key_length(c
) + num
+ EVP_CIPHER_iv_length(c
);
292 ssl3_cleanup_key_block(s
);
294 if ((p
= OPENSSL_malloc(num
)) == NULL
)
297 s
->s3
->tmp
.key_block_length
= num
;
298 s
->s3
->tmp
.key_block
= p
;
300 ret
= ssl3_generate_key_block(s
, p
, num
);
302 if (!(s
->options
& SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
)) {
304 * enable vulnerability countermeasure for CBC ciphers with known-IV
305 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
307 s
->s3
->need_empty_fragments
= 1;
309 if (s
->session
->cipher
!= NULL
) {
310 if (s
->session
->cipher
->algorithm_enc
== SSL_eNULL
)
311 s
->s3
->need_empty_fragments
= 0;
313 #ifndef OPENSSL_NO_RC4
314 if (s
->session
->cipher
->algorithm_enc
== SSL_RC4
)
315 s
->s3
->need_empty_fragments
= 0;
323 SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK
, ERR_R_MALLOC_FAILURE
);
327 void ssl3_cleanup_key_block(SSL
*s
)
329 OPENSSL_clear_free(s
->s3
->tmp
.key_block
, s
->s3
->tmp
.key_block_length
);
330 s
->s3
->tmp
.key_block
= NULL
;
331 s
->s3
->tmp
.key_block_length
= 0;
334 int ssl3_init_finished_mac(SSL
*s
)
336 BIO
*buf
= BIO_new(BIO_s_mem());
339 SSLerr(SSL_F_SSL3_INIT_FINISHED_MAC
, ERR_R_MALLOC_FAILURE
);
342 ssl3_free_digest_list(s
);
343 s
->s3
->handshake_buffer
= buf
;
344 (void)BIO_set_close(s
->s3
->handshake_buffer
, BIO_CLOSE
);
349 * Free digest list. Also frees handshake buffer since they are always freed
353 void ssl3_free_digest_list(SSL
*s
)
355 BIO_free(s
->s3
->handshake_buffer
);
356 s
->s3
->handshake_buffer
= NULL
;
357 EVP_MD_CTX_free(s
->s3
->handshake_dgst
);
358 s
->s3
->handshake_dgst
= NULL
;
361 int ssl3_finish_mac(SSL
*s
, const unsigned char *buf
, size_t len
)
363 if (s
->s3
->handshake_dgst
== NULL
) {
365 /* Note: this writes to a memory BIO so a failure is a fatal error */
368 ret
= BIO_write(s
->s3
->handshake_buffer
, (void *)buf
, (int)len
);
369 return ret
> 0 && ret
== (int)len
;
371 return EVP_DigestUpdate(s
->s3
->handshake_dgst
, buf
, len
);
375 int ssl3_digest_cached_records(SSL
*s
, int keep
)
381 if (s
->s3
->handshake_dgst
== NULL
) {
382 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
384 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
,
385 SSL_R_BAD_HANDSHAKE_LENGTH
);
389 s
->s3
->handshake_dgst
= EVP_MD_CTX_new();
390 if (s
->s3
->handshake_dgst
== NULL
) {
391 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
, ERR_R_MALLOC_FAILURE
);
395 md
= ssl_handshake_md(s
);
396 if (md
== NULL
|| !EVP_DigestInit_ex(s
->s3
->handshake_dgst
, md
, NULL
)
397 || !EVP_DigestUpdate(s
->s3
->handshake_dgst
, hdata
, hdatalen
)) {
398 SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS
, ERR_R_INTERNAL_ERROR
);
403 BIO_free(s
->s3
->handshake_buffer
);
404 s
->s3
->handshake_buffer
= NULL
;
410 size_t ssl3_final_finish_mac(SSL
*s
, const char *sender
, size_t len
,
414 EVP_MD_CTX
*ctx
= NULL
;
416 if (!ssl3_digest_cached_records(s
, 0))
419 if (EVP_MD_CTX_type(s
->s3
->handshake_dgst
) != NID_md5_sha1
) {
420 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, SSL_R_NO_REQUIRED_DIGEST
);
424 ctx
= EVP_MD_CTX_new();
426 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_MALLOC_FAILURE
);
429 if (!EVP_MD_CTX_copy_ex(ctx
, s
->s3
->handshake_dgst
)) {
430 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_INTERNAL_ERROR
);
434 ret
= EVP_MD_CTX_size(ctx
);
436 EVP_MD_CTX_reset(ctx
);
440 if ((sender
!= NULL
&& EVP_DigestUpdate(ctx
, sender
, len
) <= 0)
441 || EVP_MD_CTX_ctrl(ctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
442 (int)s
->session
->master_key_length
,
443 s
->session
->master_key
) <= 0
444 || EVP_DigestFinal_ex(ctx
, p
, NULL
) <= 0) {
445 SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC
, ERR_R_INTERNAL_ERROR
);
449 EVP_MD_CTX_free(ctx
);
454 int ssl3_generate_master_secret(SSL
*s
, unsigned char *out
, unsigned char *p
,
455 size_t len
, size_t *secret_size
)
457 static const unsigned char *salt
[3] = {
458 #ifndef CHARSET_EBCDIC
459 (const unsigned char *)"A",
460 (const unsigned char *)"BB",
461 (const unsigned char *)"CCC",
463 (const unsigned char *)"\x41",
464 (const unsigned char *)"\x42\x42",
465 (const unsigned char *)"\x43\x43\x43",
468 unsigned char buf
[EVP_MAX_MD_SIZE
];
469 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
472 #ifdef OPENSSL_SSL_TRACE_CRYPTO
473 unsigned char *tmpout
= out
;
475 size_t ret_secret_size
= 0;
478 SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET
, ERR_R_MALLOC_FAILURE
);
481 for (i
= 0; i
< 3; i
++) {
482 if (EVP_DigestInit_ex(ctx
, s
->ctx
->sha1
, NULL
) <= 0
483 || EVP_DigestUpdate(ctx
, salt
[i
],
484 strlen((const char *)salt
[i
])) <= 0
485 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
486 || EVP_DigestUpdate(ctx
, &(s
->s3
->client_random
[0]),
487 SSL3_RANDOM_SIZE
) <= 0
488 || EVP_DigestUpdate(ctx
, &(s
->s3
->server_random
[0]),
489 SSL3_RANDOM_SIZE
) <= 0
490 /* TODO(size_t) : convert me */
491 || EVP_DigestFinal_ex(ctx
, buf
, &n
) <= 0
492 || EVP_DigestInit_ex(ctx
, s
->ctx
->md5
, NULL
) <= 0
493 || EVP_DigestUpdate(ctx
, p
, len
) <= 0
494 || EVP_DigestUpdate(ctx
, buf
, n
) <= 0
495 || EVP_DigestFinal_ex(ctx
, out
, &n
) <= 0) {
496 SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET
, ERR_R_INTERNAL_ERROR
);
501 ret_secret_size
+= n
;
503 EVP_MD_CTX_free(ctx
);
505 #ifdef OPENSSL_SSL_TRACE_CRYPTO
506 if (ret
&& s
->msg_callback
) {
507 s
->msg_callback(2, s
->version
, TLS1_RT_CRYPTO_PREMASTER
,
508 p
, len
, s
, s
->msg_callback_arg
);
509 s
->msg_callback(2, s
->version
, TLS1_RT_CRYPTO_CLIENT_RANDOM
,
510 s
->s3
->client_random
, SSL3_RANDOM_SIZE
,
511 s
, s
->msg_callback_arg
);
512 s
->msg_callback(2, s
->version
, TLS1_RT_CRYPTO_SERVER_RANDOM
,
513 s
->s3
->server_random
, SSL3_RANDOM_SIZE
,
514 s
, s
->msg_callback_arg
);
515 s
->msg_callback(2, s
->version
, TLS1_RT_CRYPTO_MASTER
,
516 tmpout
, SSL3_MASTER_SECRET_SIZE
,
517 s
, s
->msg_callback_arg
);
520 OPENSSL_cleanse(buf
, sizeof(buf
));
522 *secret_size
= ret_secret_size
;
526 int ssl3_alert_code(int code
)
529 case SSL_AD_CLOSE_NOTIFY
:
530 return (SSL3_AD_CLOSE_NOTIFY
);
531 case SSL_AD_UNEXPECTED_MESSAGE
:
532 return (SSL3_AD_UNEXPECTED_MESSAGE
);
533 case SSL_AD_BAD_RECORD_MAC
:
534 return (SSL3_AD_BAD_RECORD_MAC
);
535 case SSL_AD_DECRYPTION_FAILED
:
536 return (SSL3_AD_BAD_RECORD_MAC
);
537 case SSL_AD_RECORD_OVERFLOW
:
538 return (SSL3_AD_BAD_RECORD_MAC
);
539 case SSL_AD_DECOMPRESSION_FAILURE
:
540 return (SSL3_AD_DECOMPRESSION_FAILURE
);
541 case SSL_AD_HANDSHAKE_FAILURE
:
542 return (SSL3_AD_HANDSHAKE_FAILURE
);
543 case SSL_AD_NO_CERTIFICATE
:
544 return (SSL3_AD_NO_CERTIFICATE
);
545 case SSL_AD_BAD_CERTIFICATE
:
546 return (SSL3_AD_BAD_CERTIFICATE
);
547 case SSL_AD_UNSUPPORTED_CERTIFICATE
:
548 return (SSL3_AD_UNSUPPORTED_CERTIFICATE
);
549 case SSL_AD_CERTIFICATE_REVOKED
:
550 return (SSL3_AD_CERTIFICATE_REVOKED
);
551 case SSL_AD_CERTIFICATE_EXPIRED
:
552 return (SSL3_AD_CERTIFICATE_EXPIRED
);
553 case SSL_AD_CERTIFICATE_UNKNOWN
:
554 return (SSL3_AD_CERTIFICATE_UNKNOWN
);
555 case SSL_AD_ILLEGAL_PARAMETER
:
556 return (SSL3_AD_ILLEGAL_PARAMETER
);
557 case SSL_AD_UNKNOWN_CA
:
558 return (SSL3_AD_BAD_CERTIFICATE
);
559 case SSL_AD_ACCESS_DENIED
:
560 return (SSL3_AD_HANDSHAKE_FAILURE
);
561 case SSL_AD_DECODE_ERROR
:
562 return (SSL3_AD_HANDSHAKE_FAILURE
);
563 case SSL_AD_DECRYPT_ERROR
:
564 return (SSL3_AD_HANDSHAKE_FAILURE
);
565 case SSL_AD_EXPORT_RESTRICTION
:
566 return (SSL3_AD_HANDSHAKE_FAILURE
);
567 case SSL_AD_PROTOCOL_VERSION
:
568 return (SSL3_AD_HANDSHAKE_FAILURE
);
569 case SSL_AD_INSUFFICIENT_SECURITY
:
570 return (SSL3_AD_HANDSHAKE_FAILURE
);
571 case SSL_AD_INTERNAL_ERROR
:
572 return (SSL3_AD_HANDSHAKE_FAILURE
);
573 case SSL_AD_USER_CANCELLED
:
574 return (SSL3_AD_HANDSHAKE_FAILURE
);
575 case SSL_AD_NO_RENEGOTIATION
:
576 return (-1); /* Don't send it :-) */
577 case SSL_AD_UNSUPPORTED_EXTENSION
:
578 return (SSL3_AD_HANDSHAKE_FAILURE
);
579 case SSL_AD_CERTIFICATE_UNOBTAINABLE
:
580 return (SSL3_AD_HANDSHAKE_FAILURE
);
581 case SSL_AD_UNRECOGNIZED_NAME
:
582 return (SSL3_AD_HANDSHAKE_FAILURE
);
583 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
:
584 return (SSL3_AD_HANDSHAKE_FAILURE
);
585 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE
:
586 return (SSL3_AD_HANDSHAKE_FAILURE
);
587 case SSL_AD_UNKNOWN_PSK_IDENTITY
:
588 return (TLS1_AD_UNKNOWN_PSK_IDENTITY
);
589 case SSL_AD_INAPPROPRIATE_FALLBACK
:
590 return (TLS1_AD_INAPPROPRIATE_FALLBACK
);
591 case SSL_AD_NO_APPLICATION_PROTOCOL
:
592 return (TLS1_AD_NO_APPLICATION_PROTOCOL
);