1 1.1.1.2 christos /* 2 1.1.1.2 christos * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * Copyright 2005 Nokia. All rights reserved. 4 1.1 christos * 5 1.1.1.2 christos * Licensed under the OpenSSL license (the "License"). You may not use 6 1.1.1.2 christos * this file except in compliance with the License. You can obtain a copy 7 1.1.1.2 christos * in the file LICENSE in the source distribution or at 8 1.1.1.2 christos * https://www.openssl.org/source/license.html 9 1.1 christos */ 10 1.1 christos 11 1.1 christos #include <stdio.h> 12 1.1.1.2 christos #include "ssl_local.h" 13 1.1 christos #include <openssl/evp.h> 14 1.1 christos #include <openssl/md5.h> 15 1.1.1.2 christos #include "internal/cryptlib.h" 16 1.1 christos 17 1.1 christos static int ssl3_generate_key_block(SSL *s, unsigned char *km, int num) 18 1.1 christos { 19 1.1.1.2 christos EVP_MD_CTX *m5; 20 1.1.1.2 christos EVP_MD_CTX *s1; 21 1.1 christos unsigned char buf[16], smd[SHA_DIGEST_LENGTH]; 22 1.1 christos unsigned char c = 'A'; 23 1.1 christos unsigned int i, j, k; 24 1.1.1.2 christos int ret = 0; 25 1.1 christos 26 1.1 christos #ifdef CHARSET_EBCDIC 27 1.1 christos c = os_toascii[c]; /* 'A' in ASCII */ 28 1.1 christos #endif 29 1.1 christos k = 0; 30 1.1.1.2 christos m5 = EVP_MD_CTX_new(); 31 1.1.1.2 christos s1 = EVP_MD_CTX_new(); 32 1.1.1.2 christos if (m5 == NULL || s1 == NULL) { 33 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK, 34 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 35 1.1.1.2 christos goto err; 36 1.1.1.2 christos } 37 1.1.1.2 christos EVP_MD_CTX_set_flags(m5, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 38 1.1 christos for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) { 39 1.1 christos k++; 40 1.1.1.2 christos if (k > sizeof(buf)) { 41 1.1 christos /* bug: 'buf' is too small for this ciphersuite */ 42 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK, 43 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 44 1.1.1.2 christos goto err; 45 1.1 christos } 46 1.1 christos 47 1.1 christos for (j = 0; j < k; j++) 48 1.1 christos buf[j] = c; 49 1.1 christos c++; 50 1.1.1.2 christos if (!EVP_DigestInit_ex(s1, EVP_sha1(), NULL) 51 1.1.1.2 christos || !EVP_DigestUpdate(s1, buf, k) 52 1.1.1.2 christos || !EVP_DigestUpdate(s1, s->session->master_key, 53 1.1.1.2 christos s->session->master_key_length) 54 1.1.1.2 christos || !EVP_DigestUpdate(s1, s->s3->server_random, SSL3_RANDOM_SIZE) 55 1.1.1.2 christos || !EVP_DigestUpdate(s1, s->s3->client_random, SSL3_RANDOM_SIZE) 56 1.1.1.2 christos || !EVP_DigestFinal_ex(s1, smd, NULL) 57 1.1.1.2 christos || !EVP_DigestInit_ex(m5, EVP_md5(), NULL) 58 1.1.1.2 christos || !EVP_DigestUpdate(m5, s->session->master_key, 59 1.1.1.2 christos s->session->master_key_length) 60 1.1.1.2 christos || !EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH)) { 61 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_KEY_BLOCK, 62 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 63 1.1.1.2 christos goto err; 64 1.1.1.2 christos } 65 1.1 christos if ((int)(i + MD5_DIGEST_LENGTH) > num) { 66 1.1.1.2 christos if (!EVP_DigestFinal_ex(m5, smd, NULL)) { 67 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 68 1.1.1.2 christos SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR); 69 1.1.1.2 christos goto err; 70 1.1.1.2 christos } 71 1.1 christos memcpy(km, smd, (num - i)); 72 1.1.1.2 christos } else { 73 1.1.1.2 christos if (!EVP_DigestFinal_ex(m5, km, NULL)) { 74 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 75 1.1.1.2 christos SSL_F_SSL3_GENERATE_KEY_BLOCK, ERR_R_INTERNAL_ERROR); 76 1.1.1.2 christos goto err; 77 1.1.1.2 christos } 78 1.1.1.2 christos } 79 1.1 christos 80 1.1 christos km += MD5_DIGEST_LENGTH; 81 1.1 christos } 82 1.1.1.2 christos OPENSSL_cleanse(smd, sizeof(smd)); 83 1.1.1.2 christos ret = 1; 84 1.1.1.2 christos err: 85 1.1.1.2 christos EVP_MD_CTX_free(m5); 86 1.1.1.2 christos EVP_MD_CTX_free(s1); 87 1.1.1.2 christos return ret; 88 1.1 christos } 89 1.1 christos 90 1.1 christos int ssl3_change_cipher_state(SSL *s, int which) 91 1.1 christos { 92 1.1 christos unsigned char *p, *mac_secret; 93 1.1.1.2 christos unsigned char *ms, *key, *iv; 94 1.1 christos EVP_CIPHER_CTX *dd; 95 1.1 christos const EVP_CIPHER *c; 96 1.1 christos #ifndef OPENSSL_NO_COMP 97 1.1 christos COMP_METHOD *comp; 98 1.1 christos #endif 99 1.1 christos const EVP_MD *m; 100 1.1.1.2 christos int mdi; 101 1.1.1.2 christos size_t n, i, j, k, cl; 102 1.1 christos int reuse_dd = 0; 103 1.1 christos 104 1.1 christos c = s->s3->tmp.new_sym_enc; 105 1.1 christos m = s->s3->tmp.new_hash; 106 1.1 christos /* m == NULL will lead to a crash later */ 107 1.1.1.2 christos if (!ossl_assert(m != NULL)) { 108 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 109 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 110 1.1.1.2 christos goto err; 111 1.1.1.2 christos } 112 1.1 christos #ifndef OPENSSL_NO_COMP 113 1.1 christos if (s->s3->tmp.new_compression == NULL) 114 1.1 christos comp = NULL; 115 1.1 christos else 116 1.1 christos comp = s->s3->tmp.new_compression->method; 117 1.1 christos #endif 118 1.1 christos 119 1.1 christos if (which & SSL3_CC_READ) { 120 1.1.1.2 christos if (s->enc_read_ctx != NULL) { 121 1.1 christos reuse_dd = 1; 122 1.1.1.2 christos } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) { 123 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 124 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 125 1.1 christos goto err; 126 1.1.1.2 christos } else { 127 1.1 christos /* 128 1.1.1.2 christos * make sure it's initialised in case we exit later with an error 129 1.1 christos */ 130 1.1.1.2 christos EVP_CIPHER_CTX_reset(s->enc_read_ctx); 131 1.1.1.2 christos } 132 1.1 christos dd = s->enc_read_ctx; 133 1.1 christos 134 1.1 christos if (ssl_replace_hash(&s->read_hash, m) == NULL) { 135 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 136 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 137 1.1.1.2 christos goto err; 138 1.1 christos } 139 1.1 christos #ifndef OPENSSL_NO_COMP 140 1.1 christos /* COMPRESS */ 141 1.1.1.2 christos COMP_CTX_free(s->expand); 142 1.1.1.2 christos s->expand = NULL; 143 1.1 christos if (comp != NULL) { 144 1.1 christos s->expand = COMP_CTX_new(comp); 145 1.1 christos if (s->expand == NULL) { 146 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 147 1.1.1.2 christos SSL_F_SSL3_CHANGE_CIPHER_STATE, 148 1.1.1.2 christos SSL_R_COMPRESSION_LIBRARY_ERROR); 149 1.1 christos goto err; 150 1.1.1.2 christos } 151 1.1 christos } 152 1.1 christos #endif 153 1.1.1.2 christos RECORD_LAYER_reset_read_sequence(&s->rlayer); 154 1.1 christos mac_secret = &(s->s3->read_mac_secret[0]); 155 1.1 christos } else { 156 1.1.1.2 christos s->statem.enc_write_state = ENC_WRITE_STATE_INVALID; 157 1.1.1.2 christos if (s->enc_write_ctx != NULL) { 158 1.1 christos reuse_dd = 1; 159 1.1.1.2 christos } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) { 160 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 161 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 162 1.1 christos goto err; 163 1.1.1.2 christos } else { 164 1.1 christos /* 165 1.1.1.2 christos * make sure it's initialised in case we exit later with an error 166 1.1 christos */ 167 1.1.1.2 christos EVP_CIPHER_CTX_reset(s->enc_write_ctx); 168 1.1.1.2 christos } 169 1.1 christos dd = s->enc_write_ctx; 170 1.1 christos if (ssl_replace_hash(&s->write_hash, m) == NULL) { 171 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 172 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 173 1.1.1.2 christos goto err; 174 1.1 christos } 175 1.1 christos #ifndef OPENSSL_NO_COMP 176 1.1 christos /* COMPRESS */ 177 1.1.1.2 christos COMP_CTX_free(s->compress); 178 1.1.1.2 christos s->compress = NULL; 179 1.1 christos if (comp != NULL) { 180 1.1 christos s->compress = COMP_CTX_new(comp); 181 1.1 christos if (s->compress == NULL) { 182 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 183 1.1.1.2 christos SSL_F_SSL3_CHANGE_CIPHER_STATE, 184 1.1.1.2 christos SSL_R_COMPRESSION_LIBRARY_ERROR); 185 1.1.1.2 christos goto err; 186 1.1 christos } 187 1.1 christos } 188 1.1 christos #endif 189 1.1.1.2 christos RECORD_LAYER_reset_write_sequence(&s->rlayer); 190 1.1 christos mac_secret = &(s->s3->write_mac_secret[0]); 191 1.1 christos } 192 1.1 christos 193 1.1 christos if (reuse_dd) 194 1.1.1.2 christos EVP_CIPHER_CTX_reset(dd); 195 1.1 christos 196 1.1 christos p = s->s3->tmp.key_block; 197 1.1.1.2 christos mdi = EVP_MD_size(m); 198 1.1.1.2 christos if (mdi < 0) { 199 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 200 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 201 1.1.1.2 christos goto err; 202 1.1.1.2 christos } 203 1.1.1.2 christos i = mdi; 204 1.1 christos cl = EVP_CIPHER_key_length(c); 205 1.1.1.2 christos j = cl; 206 1.1 christos k = EVP_CIPHER_iv_length(c); 207 1.1 christos if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) || 208 1.1 christos (which == SSL3_CHANGE_CIPHER_SERVER_READ)) { 209 1.1 christos ms = &(p[0]); 210 1.1 christos n = i + i; 211 1.1 christos key = &(p[n]); 212 1.1 christos n += j + j; 213 1.1 christos iv = &(p[n]); 214 1.1 christos n += k + k; 215 1.1 christos } else { 216 1.1 christos n = i; 217 1.1 christos ms = &(p[n]); 218 1.1 christos n += i + j; 219 1.1 christos key = &(p[n]); 220 1.1 christos n += j + k; 221 1.1 christos iv = &(p[n]); 222 1.1 christos n += k; 223 1.1 christos } 224 1.1 christos 225 1.1 christos if (n > s->s3->tmp.key_block_length) { 226 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 227 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 228 1.1.1.2 christos goto err; 229 1.1 christos } 230 1.1 christos 231 1.1 christos memcpy(mac_secret, ms, i); 232 1.1 christos 233 1.1.1.2 christos if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) { 234 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_CHANGE_CIPHER_STATE, 235 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 236 1.1.1.2 christos goto err; 237 1.1 christos } 238 1.1 christos 239 1.1.1.2 christos s->statem.enc_write_state = ENC_WRITE_STATE_VALID; 240 1.1.1.2 christos return 1; 241 1.1 christos err: 242 1.1.1.2 christos return 0; 243 1.1 christos } 244 1.1 christos 245 1.1 christos int ssl3_setup_key_block(SSL *s) 246 1.1 christos { 247 1.1 christos unsigned char *p; 248 1.1 christos const EVP_CIPHER *c; 249 1.1 christos const EVP_MD *hash; 250 1.1 christos int num; 251 1.1 christos int ret = 0; 252 1.1 christos SSL_COMP *comp; 253 1.1 christos 254 1.1 christos if (s->s3->tmp.key_block_length != 0) 255 1.1.1.2 christos return 1; 256 1.1 christos 257 1.1.1.2 christos if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, &comp, 0)) { 258 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_SETUP_KEY_BLOCK, 259 1.1.1.2 christos SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 260 1.1.1.2 christos return 0; 261 1.1 christos } 262 1.1 christos 263 1.1 christos s->s3->tmp.new_sym_enc = c; 264 1.1 christos s->s3->tmp.new_hash = hash; 265 1.1 christos #ifdef OPENSSL_NO_COMP 266 1.1 christos s->s3->tmp.new_compression = NULL; 267 1.1 christos #else 268 1.1 christos s->s3->tmp.new_compression = comp; 269 1.1 christos #endif 270 1.1 christos 271 1.1 christos num = EVP_MD_size(hash); 272 1.1 christos if (num < 0) 273 1.1 christos return 0; 274 1.1 christos 275 1.1 christos num = EVP_CIPHER_key_length(c) + num + EVP_CIPHER_iv_length(c); 276 1.1 christos num *= 2; 277 1.1 christos 278 1.1 christos ssl3_cleanup_key_block(s); 279 1.1 christos 280 1.1.1.2 christos if ((p = OPENSSL_malloc(num)) == NULL) { 281 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_SETUP_KEY_BLOCK, 282 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 283 1.1.1.2 christos return 0; 284 1.1.1.2 christos } 285 1.1 christos 286 1.1 christos s->s3->tmp.key_block_length = num; 287 1.1 christos s->s3->tmp.key_block = p; 288 1.1 christos 289 1.1.1.2 christos /* Calls SSLfatal() as required */ 290 1.1 christos ret = ssl3_generate_key_block(s, p, num); 291 1.1 christos 292 1.1 christos if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) { 293 1.1 christos /* 294 1.1 christos * enable vulnerability countermeasure for CBC ciphers with known-IV 295 1.1 christos * problem (http://www.openssl.org/~bodo/tls-cbc.txt) 296 1.1 christos */ 297 1.1 christos s->s3->need_empty_fragments = 1; 298 1.1 christos 299 1.1 christos if (s->session->cipher != NULL) { 300 1.1 christos if (s->session->cipher->algorithm_enc == SSL_eNULL) 301 1.1 christos s->s3->need_empty_fragments = 0; 302 1.1 christos 303 1.1 christos #ifndef OPENSSL_NO_RC4 304 1.1 christos if (s->session->cipher->algorithm_enc == SSL_RC4) 305 1.1 christos s->s3->need_empty_fragments = 0; 306 1.1 christos #endif 307 1.1 christos } 308 1.1 christos } 309 1.1 christos 310 1.1 christos return ret; 311 1.1 christos } 312 1.1 christos 313 1.1 christos void ssl3_cleanup_key_block(SSL *s) 314 1.1 christos { 315 1.1.1.2 christos OPENSSL_clear_free(s->s3->tmp.key_block, s->s3->tmp.key_block_length); 316 1.1.1.2 christos s->s3->tmp.key_block = NULL; 317 1.1 christos s->s3->tmp.key_block_length = 0; 318 1.1 christos } 319 1.1 christos 320 1.1.1.2 christos int ssl3_init_finished_mac(SSL *s) 321 1.1 christos { 322 1.1.1.2 christos BIO *buf = BIO_new(BIO_s_mem()); 323 1.1 christos 324 1.1.1.2 christos if (buf == NULL) { 325 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_INIT_FINISHED_MAC, 326 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 327 1.1.1.2 christos return 0; 328 1.1 christos } 329 1.1.1.2 christos ssl3_free_digest_list(s); 330 1.1.1.2 christos s->s3->handshake_buffer = buf; 331 1.1 christos (void)BIO_set_close(s->s3->handshake_buffer, BIO_CLOSE); 332 1.1.1.2 christos return 1; 333 1.1 christos } 334 1.1 christos 335 1.1.1.2 christos /* 336 1.1.1.2 christos * Free digest list. Also frees handshake buffer since they are always freed 337 1.1.1.2 christos * together. 338 1.1.1.2 christos */ 339 1.1.1.2 christos 340 1.1 christos void ssl3_free_digest_list(SSL *s) 341 1.1 christos { 342 1.1.1.2 christos BIO_free(s->s3->handshake_buffer); 343 1.1.1.2 christos s->s3->handshake_buffer = NULL; 344 1.1.1.2 christos EVP_MD_CTX_free(s->s3->handshake_dgst); 345 1.1 christos s->s3->handshake_dgst = NULL; 346 1.1 christos } 347 1.1 christos 348 1.1.1.2 christos int ssl3_finish_mac(SSL *s, const unsigned char *buf, size_t len) 349 1.1 christos { 350 1.1.1.2 christos int ret; 351 1.1.1.2 christos 352 1.1.1.2 christos if (s->s3->handshake_dgst == NULL) { 353 1.1.1.2 christos /* Note: this writes to a memory BIO so a failure is a fatal error */ 354 1.1.1.2 christos if (len > INT_MAX) { 355 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC, 356 1.1.1.2 christos SSL_R_OVERFLOW_ERROR); 357 1.1.1.2 christos return 0; 358 1.1.1.2 christos } 359 1.1.1.2 christos ret = BIO_write(s->s3->handshake_buffer, (void *)buf, (int)len); 360 1.1.1.2 christos if (ret <= 0 || ret != (int)len) { 361 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC, 362 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 363 1.1.1.2 christos return 0; 364 1.1.1.2 christos } 365 1.1.1.2 christos } else { 366 1.1.1.2 christos ret = EVP_DigestUpdate(s->s3->handshake_dgst, buf, len); 367 1.1.1.2 christos if (!ret) { 368 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINISH_MAC, 369 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 370 1.1.1.2 christos return 0; 371 1.1 christos } 372 1.1 christos } 373 1.1.1.2 christos return 1; 374 1.1 christos } 375 1.1 christos 376 1.1.1.2 christos int ssl3_digest_cached_records(SSL *s, int keep) 377 1.1 christos { 378 1.1 christos const EVP_MD *md; 379 1.1 christos long hdatalen; 380 1.1 christos void *hdata; 381 1.1 christos 382 1.1 christos if (s->s3->handshake_dgst == NULL) { 383 1.1.1.2 christos hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); 384 1.1.1.2 christos if (hdatalen <= 0) { 385 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS, 386 1.1.1.2 christos SSL_R_BAD_HANDSHAKE_LENGTH); 387 1.1.1.2 christos return 0; 388 1.1.1.2 christos } 389 1.1 christos 390 1.1.1.2 christos s->s3->handshake_dgst = EVP_MD_CTX_new(); 391 1.1.1.2 christos if (s->s3->handshake_dgst == NULL) { 392 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS, 393 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 394 1.1.1.2 christos return 0; 395 1.1.1.2 christos } 396 1.1.1.2 christos 397 1.1.1.2 christos md = ssl_handshake_md(s); 398 1.1.1.2 christos if (md == NULL || !EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL) 399 1.1.1.2 christos || !EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen)) { 400 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_DIGEST_CACHED_RECORDS, 401 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 402 1.1.1.2 christos return 0; 403 1.1 christos } 404 1.1 christos } 405 1.1.1.2 christos if (keep == 0) { 406 1.1 christos BIO_free(s->s3->handshake_buffer); 407 1.1 christos s->s3->handshake_buffer = NULL; 408 1.1 christos } 409 1.1 christos 410 1.1 christos return 1; 411 1.1 christos } 412 1.1 christos 413 1.1.1.2 christos size_t ssl3_final_finish_mac(SSL *s, const char *sender, size_t len, 414 1.1.1.2 christos unsigned char *p) 415 1.1 christos { 416 1.1.1.2 christos int ret; 417 1.1.1.2 christos EVP_MD_CTX *ctx = NULL; 418 1.1 christos 419 1.1.1.2 christos if (!ssl3_digest_cached_records(s, 0)) { 420 1.1.1.2 christos /* SSLfatal() already called */ 421 1.1 christos return 0; 422 1.1.1.2 christos } 423 1.1 christos 424 1.1.1.2 christos if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) { 425 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC, 426 1.1.1.2 christos SSL_R_NO_REQUIRED_DIGEST); 427 1.1 christos return 0; 428 1.1 christos } 429 1.1.1.2 christos 430 1.1.1.2 christos ctx = EVP_MD_CTX_new(); 431 1.1.1.2 christos if (ctx == NULL) { 432 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC, 433 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 434 1.1 christos return 0; 435 1.1 christos } 436 1.1.1.2 christos if (!EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst)) { 437 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC, 438 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 439 1.1 christos ret = 0; 440 1.1.1.2 christos goto err; 441 1.1 christos } 442 1.1 christos 443 1.1.1.2 christos ret = EVP_MD_CTX_size(ctx); 444 1.1.1.2 christos if (ret < 0) { 445 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC, 446 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 447 1.1.1.2 christos ret = 0; 448 1.1.1.2 christos goto err; 449 1.1 christos } 450 1.1 christos 451 1.1.1.2 christos if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0) 452 1.1.1.2 christos || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET, 453 1.1.1.2 christos (int)s->session->master_key_length, 454 1.1.1.2 christos s->session->master_key) <= 0 455 1.1.1.2 christos || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) { 456 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_FINAL_FINISH_MAC, 457 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 458 1.1.1.2 christos ret = 0; 459 1.1.1.2 christos } 460 1.1 christos 461 1.1.1.2 christos err: 462 1.1.1.2 christos EVP_MD_CTX_free(ctx); 463 1.1 christos 464 1.1.1.2 christos return ret; 465 1.1 christos } 466 1.1 christos 467 1.1 christos int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, 468 1.1.1.2 christos size_t len, size_t *secret_size) 469 1.1 christos { 470 1.1 christos static const unsigned char *salt[3] = { 471 1.1 christos #ifndef CHARSET_EBCDIC 472 1.1 christos (const unsigned char *)"A", 473 1.1 christos (const unsigned char *)"BB", 474 1.1 christos (const unsigned char *)"CCC", 475 1.1 christos #else 476 1.1 christos (const unsigned char *)"\x41", 477 1.1 christos (const unsigned char *)"\x42\x42", 478 1.1 christos (const unsigned char *)"\x43\x43\x43", 479 1.1 christos #endif 480 1.1 christos }; 481 1.1 christos unsigned char buf[EVP_MAX_MD_SIZE]; 482 1.1.1.2 christos EVP_MD_CTX *ctx = EVP_MD_CTX_new(); 483 1.1.1.2 christos int i, ret = 1; 484 1.1 christos unsigned int n; 485 1.1.1.2 christos size_t ret_secret_size = 0; 486 1.1 christos 487 1.1.1.2 christos if (ctx == NULL) { 488 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_MASTER_SECRET, 489 1.1.1.2 christos ERR_R_MALLOC_FAILURE); 490 1.1.1.2 christos return 0; 491 1.1.1.2 christos } 492 1.1 christos for (i = 0; i < 3; i++) { 493 1.1.1.2 christos if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0 494 1.1.1.2 christos || EVP_DigestUpdate(ctx, salt[i], 495 1.1.1.2 christos strlen((const char *)salt[i])) <= 0 496 1.1.1.2 christos || EVP_DigestUpdate(ctx, p, len) <= 0 497 1.1.1.2 christos || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]), 498 1.1.1.2 christos SSL3_RANDOM_SIZE) <= 0 499 1.1.1.2 christos || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]), 500 1.1.1.2 christos SSL3_RANDOM_SIZE) <= 0 501 1.1.1.2 christos /* TODO(size_t) : convert me */ 502 1.1.1.2 christos || EVP_DigestFinal_ex(ctx, buf, &n) <= 0 503 1.1.1.2 christos || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0 504 1.1.1.2 christos || EVP_DigestUpdate(ctx, p, len) <= 0 505 1.1.1.2 christos || EVP_DigestUpdate(ctx, buf, n) <= 0 506 1.1.1.2 christos || EVP_DigestFinal_ex(ctx, out, &n) <= 0) { 507 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 508 1.1.1.2 christos SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR); 509 1.1 christos ret = 0; 510 1.1 christos break; 511 1.1 christos } 512 1.1 christos out += n; 513 1.1.1.2 christos ret_secret_size += n; 514 1.1 christos } 515 1.1.1.2 christos EVP_MD_CTX_free(ctx); 516 1.1 christos 517 1.1.1.2 christos OPENSSL_cleanse(buf, sizeof(buf)); 518 1.1.1.2 christos if (ret) 519 1.1.1.2 christos *secret_size = ret_secret_size; 520 1.1.1.2 christos return ret; 521 1.1 christos } 522 1.1 christos 523 1.1 christos int ssl3_alert_code(int code) 524 1.1 christos { 525 1.1 christos switch (code) { 526 1.1 christos case SSL_AD_CLOSE_NOTIFY: 527 1.1.1.2 christos return SSL3_AD_CLOSE_NOTIFY; 528 1.1 christos case SSL_AD_UNEXPECTED_MESSAGE: 529 1.1.1.2 christos return SSL3_AD_UNEXPECTED_MESSAGE; 530 1.1 christos case SSL_AD_BAD_RECORD_MAC: 531 1.1.1.2 christos return SSL3_AD_BAD_RECORD_MAC; 532 1.1 christos case SSL_AD_DECRYPTION_FAILED: 533 1.1.1.2 christos return SSL3_AD_BAD_RECORD_MAC; 534 1.1 christos case SSL_AD_RECORD_OVERFLOW: 535 1.1.1.2 christos return SSL3_AD_BAD_RECORD_MAC; 536 1.1 christos case SSL_AD_DECOMPRESSION_FAILURE: 537 1.1.1.2 christos return SSL3_AD_DECOMPRESSION_FAILURE; 538 1.1 christos case SSL_AD_HANDSHAKE_FAILURE: 539 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 540 1.1 christos case SSL_AD_NO_CERTIFICATE: 541 1.1.1.2 christos return SSL3_AD_NO_CERTIFICATE; 542 1.1 christos case SSL_AD_BAD_CERTIFICATE: 543 1.1.1.2 christos return SSL3_AD_BAD_CERTIFICATE; 544 1.1 christos case SSL_AD_UNSUPPORTED_CERTIFICATE: 545 1.1.1.2 christos return SSL3_AD_UNSUPPORTED_CERTIFICATE; 546 1.1 christos case SSL_AD_CERTIFICATE_REVOKED: 547 1.1.1.2 christos return SSL3_AD_CERTIFICATE_REVOKED; 548 1.1 christos case SSL_AD_CERTIFICATE_EXPIRED: 549 1.1.1.2 christos return SSL3_AD_CERTIFICATE_EXPIRED; 550 1.1 christos case SSL_AD_CERTIFICATE_UNKNOWN: 551 1.1.1.2 christos return SSL3_AD_CERTIFICATE_UNKNOWN; 552 1.1 christos case SSL_AD_ILLEGAL_PARAMETER: 553 1.1.1.2 christos return SSL3_AD_ILLEGAL_PARAMETER; 554 1.1 christos case SSL_AD_UNKNOWN_CA: 555 1.1.1.2 christos return SSL3_AD_BAD_CERTIFICATE; 556 1.1 christos case SSL_AD_ACCESS_DENIED: 557 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 558 1.1 christos case SSL_AD_DECODE_ERROR: 559 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 560 1.1 christos case SSL_AD_DECRYPT_ERROR: 561 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 562 1.1 christos case SSL_AD_EXPORT_RESTRICTION: 563 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 564 1.1 christos case SSL_AD_PROTOCOL_VERSION: 565 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 566 1.1 christos case SSL_AD_INSUFFICIENT_SECURITY: 567 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 568 1.1 christos case SSL_AD_INTERNAL_ERROR: 569 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 570 1.1 christos case SSL_AD_USER_CANCELLED: 571 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 572 1.1 christos case SSL_AD_NO_RENEGOTIATION: 573 1.1.1.2 christos return -1; /* Don't send it :-) */ 574 1.1 christos case SSL_AD_UNSUPPORTED_EXTENSION: 575 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 576 1.1 christos case SSL_AD_CERTIFICATE_UNOBTAINABLE: 577 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 578 1.1 christos case SSL_AD_UNRECOGNIZED_NAME: 579 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 580 1.1 christos case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: 581 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 582 1.1 christos case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: 583 1.1.1.2 christos return SSL3_AD_HANDSHAKE_FAILURE; 584 1.1 christos case SSL_AD_UNKNOWN_PSK_IDENTITY: 585 1.1.1.2 christos return TLS1_AD_UNKNOWN_PSK_IDENTITY; 586 1.1 christos case SSL_AD_INAPPROPRIATE_FALLBACK: 587 1.1.1.2 christos return TLS1_AD_INAPPROPRIATE_FALLBACK; 588 1.1.1.2 christos case SSL_AD_NO_APPLICATION_PROTOCOL: 589 1.1.1.2 christos return TLS1_AD_NO_APPLICATION_PROTOCOL; 590 1.1.1.2 christos case SSL_AD_CERTIFICATE_REQUIRED: 591 1.1.1.2 christos return SSL_AD_HANDSHAKE_FAILURE; 592 1.1.1.2 christos case SSL_AD_MISSING_EXTENSION: 593 1.1.1.2 christos return SSL_AD_HANDSHAKE_FAILURE; 594 1.1 christos default: 595 1.1.1.2 christos return -1; 596 1.1 christos } 597 1.1 christos } 598