From d8eea14443ab890b0a6d00a40d2dbf6d3bf79484 Mon Sep 17 00:00:00 2001 From: Steven Valdez Date: Wed, 24 Feb 2016 14:00:22 -0500 Subject: [PATCH] BIO_new_mem_buf should take const void * BIO_FLAGS_MEM_RDONLY keeps the invariant. (Imported from upstream's a38a159bfcbc94214dda00e0e6b1fc6454a23b78) Change-Id: I4cb35615d76b77929915e370dbb7fec1455da069 Reviewed-on: https://boringssl-review.googlesource.com/7214 Reviewed-by: David Benjamin --- crypto/bio/bio_mem.c | 5 +++-- crypto/bio/bio_test.cc | 2 +- crypto/pkcs8/pkcs12_test.cc | 2 +- crypto/x509/pkcs7_test.c | 5 ++--- crypto/x509/x509_test.cc | 2 +- include/openssl/bio.h | 4 ++-- ssl/ssl_test.cc | 5 ++--- 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/crypto/bio/bio_mem.c b/crypto/bio/bio_mem.c index 6864f6f19..844fba7e3 100644 --- a/crypto/bio/bio_mem.c +++ b/crypto/bio/bio_mem.c @@ -64,7 +64,7 @@ #include -BIO *BIO_new_mem_buf(void *buf, int len) { +BIO *BIO_new_mem_buf(const void *buf, int len) { BIO *ret; BUF_MEM *b; const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len; @@ -80,7 +80,8 @@ BIO *BIO_new_mem_buf(void *buf, int len) { } b = (BUF_MEM *)ret->ptr; - b->data = buf; + /* BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to. */ + b->data = (void *)buf; b->length = size; b->max = size; diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc index bc755c104..3615ab468 100644 --- a/crypto/bio/bio_test.cc +++ b/crypto/bio/bio_test.cc @@ -331,7 +331,7 @@ static bool TestPrintf() { static bool ReadASN1(bool should_succeed, const uint8_t *data, size_t data_len, size_t expected_len, size_t max_len) { - ScopedBIO bio(BIO_new_mem_buf(const_cast(data), data_len)); + ScopedBIO bio(BIO_new_mem_buf(data, data_len)); uint8_t *out; size_t out_len; diff --git a/crypto/pkcs8/pkcs12_test.cc b/crypto/pkcs8/pkcs12_test.cc index f1a1fcd26..17bcd2737 100644 --- a/crypto/pkcs8/pkcs12_test.cc +++ b/crypto/pkcs8/pkcs12_test.cc @@ -708,7 +708,7 @@ static bool Test(const char *name, const uint8_t *der, size_t der_len) { } static bool TestCompat(const uint8_t *der, size_t der_len) { - ScopedBIO bio(BIO_new_mem_buf((void*) der, der_len)); + ScopedBIO bio(BIO_new_mem_buf(der, der_len)); if (!bio) { return false; } diff --git a/crypto/x509/pkcs7_test.c b/crypto/x509/pkcs7_test.c index 38beb3e8e..7e763226c 100644 --- a/crypto/x509/pkcs7_test.c +++ b/crypto/x509/pkcs7_test.c @@ -596,7 +596,7 @@ static int test_crl_reparse(const uint8_t *der_bytes, size_t der_len) { } static int test_pem_certs(const char *pem) { - BIO *bio = BIO_new_mem_buf((char *) pem, strlen(pem)); + BIO *bio = BIO_new_mem_buf(pem, strlen(pem)); STACK_OF(X509) *certs = sk_X509_new_null(); if (!PKCS7_get_PEM_certificates(certs, bio)) { @@ -618,7 +618,7 @@ static int test_pem_certs(const char *pem) { } static int test_pem_crls(const char *pem) { - BIO *bio = BIO_new_mem_buf((char *) pem, strlen(pem)); + BIO *bio = BIO_new_mem_buf(pem, strlen(pem)); STACK_OF(X509_CRL) *crls = sk_X509_CRL_new_null(); if (!PKCS7_get_PEM_CRLs(crls, bio)) { @@ -653,4 +653,3 @@ int main(void) { printf("PASS\n"); return 0; } - diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc index 486fc4c4c..95c8b1f05 100644 --- a/crypto/x509/x509_test.cc +++ b/crypto/x509/x509_test.cc @@ -163,7 +163,7 @@ static const char kForgeryPEM[] = // CertFromPEM parses the given, NUL-terminated PEM block and returns an // |X509*|. static X509* CertFromPEM(const char *pem) { - ScopedBIO bio(BIO_new_mem_buf(const_cast(pem), strlen(pem))); + ScopedBIO bio(BIO_new_mem_buf(pem, strlen(pem))); return PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr); } diff --git a/include/openssl/bio.h b/include/openssl/bio.h index 6ca357518..38b896f9f 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -368,12 +368,12 @@ OPENSSL_EXPORT int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, /* BIO_s_mem returns a |BIO_METHOD| that uses a in-memory buffer. */ OPENSSL_EXPORT const BIO_METHOD *BIO_s_mem(void); -/* BIO_new_mem_buf creates BIO that reads and writes from |len| bytes at |buf|. +/* BIO_new_mem_buf creates read-only BIO that reads from |len| bytes at |buf|. * It does not take ownership of |buf|. It returns the BIO or NULL on error. * * If |len| is negative, then |buf| is treated as a NUL-terminated string, but * don't depend on this in new code. */ -OPENSSL_EXPORT BIO *BIO_new_mem_buf(void *buf, int len); +OPENSSL_EXPORT BIO *BIO_new_mem_buf(const void *buf, int len); /* BIO_mem_contents sets |*out_contents| to point to the current contents of * |bio| and |*out_len| to contain the length of that data. It returns one on diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc index ed1d3c7da..590a2c166 100644 --- a/ssl/ssl_test.cc +++ b/ssl/ssl_test.cc @@ -1001,8 +1001,7 @@ static ScopedX509 GetTestCertificate() { "T5oQpHL9z/cCDLAKCKRa4uV0fhEdOWBqyR9p8y5jJtye72t6CuFUV5iqcpF4BH4f\n" "j2VNHwsSrJwkD4QUGlUtH7vwnQmyCFxZMmWAJg==\n" "-----END CERTIFICATE-----\n"; - ScopedBIO bio( - BIO_new_mem_buf(const_cast(kCertPEM), strlen(kCertPEM))); + ScopedBIO bio(BIO_new_mem_buf(kCertPEM, strlen(kCertPEM))); return ScopedX509(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); } @@ -1023,7 +1022,7 @@ static ScopedEVP_PKEY GetTestKey() { "tfDwbqkta4xcux67//khAkEAvvRXLHTaa6VFzTaiiO8SaFsHV3lQyXOtMrBpB5jd\n" "moZWgjHvB2W9Ckn7sDqsPB+U2tyX0joDdQEyuiMECDY8oQ==\n" "-----END RSA PRIVATE KEY-----\n"; - ScopedBIO bio(BIO_new_mem_buf(const_cast(kKeyPEM), strlen(kKeyPEM))); + ScopedBIO bio(BIO_new_mem_buf(kKeyPEM, strlen(kKeyPEM))); return ScopedEVP_PKEY( PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); }