summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Sloan <varomodt@google.com>2018-10-15 10:03:22 -0700
committerRobert Sloan <varomodt@google.com>2018-10-15 10:04:26 -0700
commit23894f8e9b0b914799a2c8bded6b4eaf39769be6 (patch)
treeb8a132ee1f4b56cf6ba2937cc52a263a788c9752 /src
parentf068def344212558323b66ff902a5d6f58fb5460 (diff)
downloadboringssl-23894f8e9b0b914799a2c8bded6b4eaf39769be6.tar.gz
external/boringssl: Sync to 80aa6949756d327476750f9ea2c9700aa2a027c5.
This includes the following changes: https://boringssl.googlesource.com/boringssl/+log/2d98d49cf712ca7dc6f4b23b9c5f5542385d8dbe..80aa6949756d327476750f9ea2c9700aa2a027c5 Test: BoringSSL CTS Presubmits Change-Id: I065541ad98c960c3842903637d084c6ba5078ebf
Diffstat (limited to 'src')
-rw-r--r--src/crypto/bio/bio.c39
-rw-r--r--src/crypto/x509/x509_test.cc13
2 files changed, 47 insertions, 5 deletions
diff --git a/src/crypto/bio/bio.c b/src/crypto/bio/bio.c
index 881c14e1..fe40578b 100644
--- a/src/crypto/bio/bio.c
+++ b/src/crypto/bio/bio.c
@@ -61,6 +61,7 @@
#include <limits.h>
#include <string.h>
+#include <openssl/asn1.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include <openssl/thread.h>
@@ -481,11 +482,28 @@ static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
}
}
+// For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses
+// |ERR_LIB_ASN1| errors.
+OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR)
+OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG)
+OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA)
+OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_TOO_LONG)
+
int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
uint8_t header[6];
static const size_t kInitialHeaderLen = 2;
- if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
+ int ret = BIO_read(bio, header, kInitialHeaderLen);
+ if (ret == 0) {
+ // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when |d2i_*_bio|
+ // could not read anything. CPython conditions on this to determine if |bio|
+ // was empty.
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
+ return 0;
+ }
+
+ if (ret != (int) kInitialHeaderLen) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
return 0;
}
@@ -494,6 +512,7 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
if ((tag & 0x1f) == 0x1f) {
// Long form tags are not supported.
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
return 0;
}
@@ -507,34 +526,41 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
// indefinite length.
- return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
- max_len);
+ if (!bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
+ max_len)) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
+ return 0;
+ }
+ return 1;
}
if (num_bytes == 0 || num_bytes > 4) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
return 0;
}
if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
(int)num_bytes) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
return 0;
}
header_len = kInitialHeaderLen + num_bytes;
uint32_t len32 = 0;
- unsigned i;
- for (i = 0; i < num_bytes; i++) {
+ for (unsigned i = 0; i < num_bytes; i++) {
len32 <<= 8;
len32 |= header[kInitialHeaderLen + i];
}
if (len32 < 128) {
// Length should have used short-form encoding.
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
return 0;
}
if ((len32 >> ((num_bytes-1)*8)) == 0) {
// Length should have been at least one byte shorter.
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
return 0;
}
@@ -544,6 +570,7 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
if (len + header_len < len ||
len + header_len > max_len ||
len > INT_MAX) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
return 0;
}
len += header_len;
@@ -551,11 +578,13 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
*out = OPENSSL_malloc(len);
if (*out == NULL) {
+ OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
return 0;
}
OPENSSL_memcpy(*out, header, header_len);
if (BIO_read(bio, (*out) + header_len, len - header_len) !=
(int) (len - header_len)) {
+ OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
OPENSSL_free(*out);
return 0;
}
diff --git a/src/crypto/x509/x509_test.cc b/src/crypto/x509/x509_test.cc
index bf0b29a2..c42a7c82 100644
--- a/src/crypto/x509/x509_test.cc
+++ b/src/crypto/x509/x509_test.cc
@@ -1671,3 +1671,16 @@ TEST(X509Test, PEMX509Info) {
PEM_X509_INFO_read_bio(bio.get(), infos.get(), nullptr, nullptr));
EXPECT_EQ(2 * OPENSSL_ARRAY_SIZE(kExpected), sk_X509_INFO_num(infos.get()));
}
+
+TEST(X509Test, ReadBIOEmpty) {
+ bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(nullptr, 0));
+ ASSERT_TRUE(bio);
+
+ // CPython expects |ASN1_R_HEADER_TOO_LONG| on EOF, to terminate a series of
+ // certificates.
+ bssl::UniquePtr<X509> x509(d2i_X509_bio(bio.get(), nullptr));
+ EXPECT_FALSE(x509);
+ uint32_t err = ERR_get_error();
+ EXPECT_EQ(ERR_LIB_ASN1, ERR_GET_LIB(err));
+ EXPECT_EQ(ASN1_R_HEADER_TOO_LONG, ERR_GET_REASON(err));
+}