347
344
rr->data=rr->input;
349
346
enc_err = s->method->ssl3_enc->enc(s,0);
348
* 0: (in non-constant time) if the record is publically invalid.
349
* 1: if the padding is valid
350
* -1: if the padding is invalid */
353
/* SSLerr() and ssl3_send_alert() have been called */
356
/* Otherwise enc_err == -1, which indicates bad padding
357
* (rec->length has not been changed in this case).
358
* To minimize information leaked via timing, we will perform
359
* the MAC computation anyway. */
360
decryption_failed_or_bad_record_mac = 1;
353
al=SSL_AD_DECRYPTION_FAILED;
354
SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
369
364
/* r->length is now the compressed data plus mac */
370
if ( (sess == NULL) ||
371
(s->enc_read_ctx == NULL) ||
372
(s->read_hash == NULL))
365
if ((sess != NULL) &&
366
(s->enc_read_ctx != NULL) &&
367
(s->read_hash != NULL))
369
/* s->read_hash != NULL => mac_size != -1 */
370
unsigned char *mac = NULL;
371
unsigned char mac_tmp[EVP_MAX_MD_SIZE];
377
372
mac_size=EVP_MD_size(s->read_hash);
373
OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
375
/* kludge: *_cbc_remove_padding passes padding length in rr->type */
376
orig_len = rr->length+((unsigned int)rr->type>>8);
378
/* orig_len is the length of the record before any padding was
379
* removed. This is public information, as is the MAC in use,
380
* therefore we can safely process the record in a different
381
* amount of time if it's too short to possibly contain a MAC.
383
if (orig_len < mac_size ||
384
/* CBC records must have a padding length byte too. */
385
(EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
386
orig_len < mac_size+1))
388
al=SSL_AD_DECODE_ERROR;
389
SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
393
if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE)
395
/* We update the length so that the TLS header bytes
396
* can be constructed correctly but we need to extract
397
* the MAC in constant time from within the record,
398
* without leaking the contents of the padding bytes.
401
ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
402
rr->length -= mac_size;
406
/* In this case there's no padding, so |orig_len|
407
* equals |rec->length| and we checked that there's
408
* enough bytes for |mac_size| above. */
409
rr->length -= mac_size;
410
mac = &rr->data[rr->length];
413
i=s->method->ssl3_enc->mac(s,md,0 /* not send */);
414
if (i < 0 || mac == NULL || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
379
416
if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
381
#if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
382
al=SSL_AD_RECORD_OVERFLOW;
383
SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
386
decryption_failed_or_bad_record_mac = 1;
389
/* check the MAC for rr->input (it's in mac_size bytes at the tail) */
390
if (rr->length >= mac_size)
392
rr->length -= mac_size;
393
mac = &rr->data[rr->length];
397
/* record (minus padding) is too short to contain a MAC */
398
#if 0 /* OK only for stream ciphers */
399
al=SSL_AD_DECODE_ERROR;
400
SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
403
decryption_failed_or_bad_record_mac = 1;
407
i=s->method->ssl3_enc->mac(s,md,0);
408
if (mac == NULL || memcmp(md, mac, mac_size) != 0)
410
decryption_failed_or_bad_record_mac = 1;
414
if (decryption_failed_or_bad_record_mac)
416
422
/* A separate 'decryption_failed' alert was introduced with TLS 1.0,
417
423
* SSL 3.0 only has 'bad_record_mac'. But unless a decryption