Skip to content

Commit 1e52eca

Browse files
davidbenagl
authored andcommitted
Normalize tls1_enc return values.
The distinction between publicly and non-publicly invalid is barely acted upon and slightly silly now that the CBC padding check has been folded into EVP_AEAD. Change-Id: Idce4b9b8d29d624e3c95243a147265d071612127 Reviewed-on: https://boringssl-review.googlesource.com/2980 Reviewed-by: Adam Langley <[email protected]>
1 parent 66850dd commit 1e52eca

File tree

3 files changed

+13
-46
lines changed

3 files changed

+13
-46
lines changed

ssl/d1_pkt.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ static int dtls1_process_buffered_records(SSL *s) {
329329

330330
static int dtls1_process_record(SSL *s) {
331331
int al;
332-
int enc_err;
333332
SSL3_RECORD *rr;
334333

335334
rr = &(s->s3->rrec);
@@ -357,23 +356,12 @@ static int dtls1_process_record(SSL *s) {
357356
/* decrypt in place in 'rr->input' */
358357
rr->data = rr->input;
359358

360-
enc_err = s->enc_method->enc(s, 0);
361-
/* enc_err is:
362-
* 0: (in non-constant time) if the record is publically invalid.
363-
* 1: if the padding is valid
364-
* -1: if the padding is invalid */
365-
if (enc_err == 0) {
359+
if (!s->enc_method->enc(s, 0)) {
366360
/* For DTLS we simply ignore bad packets. */
367361
rr->length = 0;
368362
s->packet_length = 0;
369363
goto err;
370364
}
371-
if (enc_err < 0) {
372-
/* decryption failed, silently discard message */
373-
rr->length = 0;
374-
s->packet_length = 0;
375-
goto err;
376-
}
377365

378366
if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
379367
al = SSL_AD_RECORD_OVERFLOW;
@@ -1171,7 +1159,7 @@ static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
11711159
wr->data = p;
11721160
wr->length += eivlen;
11731161

1174-
if (s->enc_method->enc(s, 1) < 1) {
1162+
if (!s->enc_method->enc(s, 1)) {
11751163
goto err;
11761164
}
11771165

ssl/s3_pkt.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ int ssl3_read_n(SSL *s, int n, int max, int extend) {
268268
/* used only by ssl3_read_bytes */
269269
static int ssl3_get_record(SSL *s) {
270270
int ssl_major, ssl_minor, al;
271-
int enc_err, n, i, ret = -1;
271+
int n, i, ret = -1;
272272
SSL3_RECORD *rr;
273273
uint8_t *p;
274274
short version;
@@ -373,22 +373,7 @@ static int ssl3_get_record(SSL *s) {
373373
/* decrypt in place in 'rr->input' */
374374
rr->data = rr->input;
375375

376-
enc_err = s->enc_method->enc(s, 0);
377-
/* enc_err is:
378-
* 0: (in non-constant time) if the record is publically invalid.
379-
* 1: if the padding is valid
380-
* -1: if the padding is invalid */
381-
if (enc_err == 0) {
382-
al = SSL_AD_DECRYPTION_FAILED;
383-
OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
384-
goto f_err;
385-
}
386-
if (enc_err < 0) {
387-
/* A separate 'decryption_failed' alert was introduced with TLS 1.0, SSL
388-
* 3.0 only has 'bad_record_mac'. But unless a decryption failure is
389-
* directly visible from the ciphertext anyway, we should not reveal which
390-
* kind of error occured – this might become visible to an attacker (e.g.
391-
* via a logfile) */
376+
if (!s->enc_method->enc(s, 0)) {
392377
al = SSL_AD_BAD_RECORD_MAC;
393378
OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
394379
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
@@ -625,7 +610,7 @@ static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
625610
wr->data = p;
626611
wr->length += eivlen;
627612

628-
if (s->enc_method->enc(s, 1) < 1) {
613+
if (!s->enc_method->enc(s, 1)) {
629614
goto err;
630615
}
631616

ssl/t1_enc.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,7 @@ int tls1_setup_key_block(SSL *s) {
565565
}
566566

567567
/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|,
568-
* respectively.
569-
*
570-
* Returns:
571-
* 0: (in non-constant time) if the record is publically invalid (i.e. too
572-
* short etc).
573-
* 1: if the record's padding is valid / the encryption was successful.
574-
* -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
575-
* an internal error occured. */
568+
* respectively. It returns one on success and zero on failure. */
576569
int tls1_enc(SSL *s, int send) {
577570
SSL3_RECORD *rec;
578571
const SSL_AEAD_CTX *aead;
@@ -586,6 +579,7 @@ int tls1_enc(SSL *s, int send) {
586579
}
587580

588581
if (s->session == NULL || aead == NULL) {
582+
/* Handle the initial NULL cipher. */
589583
memmove(rec->data, rec->input, rec->length);
590584
rec->input = rec->data;
591585
return 1;
@@ -623,7 +617,7 @@ int tls1_enc(SSL *s, int send) {
623617

624618
if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce)) {
625619
OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
626-
return -1; /* internal error - should never happen. */
620+
return 0;
627621
}
628622

629623
memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
@@ -639,14 +633,14 @@ int tls1_enc(SSL *s, int send) {
639633
if (aead->random_variable_nonce) {
640634
assert(aead->variable_nonce_included_in_record);
641635
if (!RAND_bytes(nonce + nonce_used, aead->variable_nonce_len)) {
642-
return -1;
636+
return 0;
643637
}
644638
} else {
645639
/* When sending we use the sequence number as the variable part of the
646640
* nonce. */
647641
if (aead->variable_nonce_len != 8) {
648642
OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
649-
return -1;
643+
return 0;
650644
}
651645
memcpy(nonce + nonce_used, ad, aead->variable_nonce_len);
652646
}
@@ -669,7 +663,7 @@ int tls1_enc(SSL *s, int send) {
669663

670664
if (!EVP_AEAD_CTX_seal(&aead->ctx, out + eivlen, &n, len + aead->tag_len,
671665
nonce, nonce_used, in + eivlen, len, ad, ad_len)) {
672-
return -1;
666+
return 0;
673667
}
674668

675669
if (aead->variable_nonce_included_in_record) {
@@ -681,7 +675,7 @@ int tls1_enc(SSL *s, int send) {
681675

682676
if (rec->data != rec->input) {
683677
OPENSSL_PUT_ERROR(SSL, tls1_enc, ERR_R_INTERNAL_ERROR);
684-
return -1; /* internal error - should never happen. */
678+
return 0;
685679
}
686680
out = in = rec->input;
687681

@@ -711,7 +705,7 @@ int tls1_enc(SSL *s, int send) {
711705

712706
if (!EVP_AEAD_CTX_open(&aead->ctx, out, &n, rec->length, nonce, nonce_used, in,
713707
len, ad, ad_len)) {
714-
return -1;
708+
return 0;
715709
}
716710

717711
rec->data = rec->input = out;

0 commit comments

Comments
 (0)