update main-with-bazel from master branch

This commit is contained in:
BoringSSL Robot
2021-11-24 00:49:02 +00:00
30 changed files with 1822 additions and 1614 deletions
+21 -22
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -56,6 +56,7 @@
#include <openssl/asn1.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
+13
View File
@@ -122,6 +122,19 @@ int EVP_EncodedLength(size_t *out_len, size_t len) {
return 1;
}
EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) {
EVP_ENCODE_CTX *ret = OPENSSL_malloc(sizeof(EVP_ENCODE_CTX));
if (ret == NULL) {
return NULL;
}
OPENSSL_memset(ret, 0, sizeof(EVP_ENCODE_CTX));
return ret;
}
void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) {
OPENSSL_free(ctx);
}
void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
}
+21
View File
@@ -550,6 +550,27 @@ void DSA_SIG_free(DSA_SIG *sig) {
OPENSSL_free(sig);
}
void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
const BIGNUM **out_s) {
if (out_r != NULL) {
*out_r = sig->r;
}
if (out_s != NULL) {
*out_s = sig->s;
}
}
int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
if (r == NULL || s == NULL) {
return 0;
}
BN_free(sig->r);
BN_free(sig->s);
sig->r = r;
sig->s = s;
return 1;
}
// mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
// |b| as secret. This function internally uses Montgomery reduction, but
// neither inputs nor outputs are in Montgomery form.
+12
View File
@@ -629,6 +629,18 @@ int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
return EVP_CipherInit(ctx, cipher, key, iv, 0);
}
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
return EVP_CipherFinal_ex(ctx, out, out_len);
}
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
return EVP_EncryptFinal_ex(ctx, out, out_len);
}
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
return EVP_DecryptFinal_ex(ctx, out, out_len);
}
int EVP_add_cipher_alias(const char *a, const char *b) {
return 1;
}
+1 -2
View File
@@ -68,8 +68,7 @@
// digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
// ECDSA. Note this value is not fully reduced modulo the order, only the
// correct number of bits.
// ECDSA.
static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
const uint8_t *digest, size_t digest_len) {
const BIGNUM *order = &group->order;
+2 -2
View File
@@ -30,7 +30,7 @@
#include "../internal.h"
// This file implements draft-irtf-cfrg-hpke-08.
// This file implements draft-irtf-cfrg-hpke-12.
#define MAX_SEED_LEN X25519_PRIVATE_KEY_LEN
#define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH
@@ -115,7 +115,7 @@ static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key,
// KEM implementations.
// dhkem_extract_and_expand implements the ExtractAndExpand operation in the
// DHKEM construction. See section 4.1 of draft-irtf-cfrg-hpke-08.
// DHKEM construction. See section 4.1 of draft-irtf-cfrg-hpke-12.
static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md,
uint8_t *out_key, size_t out_len,
const uint8_t *dh, size_t dh_len,
+2 -2
View File
@@ -241,8 +241,8 @@ bool HPKETestVector::ReadFromFileTest(FileTest *t) {
for (int i = 1; t->HasAttribute(BuildAttrName("aad", i)); i++) {
Encryption encryption;
if (!t->GetBytes(&encryption.aad, BuildAttrName("aad", i)) ||
!t->GetBytes(&encryption.ciphertext, BuildAttrName("ciphertext", i)) ||
!t->GetBytes(&encryption.plaintext, BuildAttrName("plaintext", i))) {
!t->GetBytes(&encryption.ciphertext, BuildAttrName("ct", i)) ||
!t->GetBytes(&encryption.plaintext, BuildAttrName("pt", i))) {
return false;
}
encryptions_.push_back(std::move(encryption));
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -18,9 +18,9 @@
Usage: translate_test_vectors.py TEST_VECTORS_JSON_FILE
The TEST_VECTORS_JSON_FILE is expected to come from the HPKE reference
implementation at https://github.com/cisco/go-hpke. The output file is
hardcoded as "hpke_test_vectors.txt".
The TEST_VECTORS_JSON_FILE is expected to come from the JSON copy of
draft-irtf-cfrg-hpke-12's test vectors, linked from its [TestVectors] citation.
The output is written to "hpke_test_vectors.txt".
"""
import collections
@@ -66,7 +66,7 @@ def read_test_vectors_and_generate_code(json_file_in_path, test_file_out_path):
for i, enc in enumerate(test["encryptions"]):
lines.append("# encryptions[{}]".format(i))
for key in ("aad", "ciphertext", "plaintext"):
for key in ("aad", "ct", "pt"):
lines.append("{} = {}".format(key, str(enc[key])))
for i, exp in enumerate(test["exports"]):
+1 -1
View File
@@ -132,7 +132,7 @@ static const uint8_t kBoringSSLBinaryTag[18] = {
0x8c, 0x62, 0x20, 0x0b, 0xd2, 0xa0, 0x72, 0x58,
0x44, 0xa8, 0x96, 0x69, 0xad, 0x55, 0x7e, 0xec,
// Current source iteration. Incremented ~monthly.
1, 0,
2, 0,
};
void *OPENSSL_malloc(size_t size) {
+29
View File
@@ -131,6 +131,35 @@ err:
return ret;
}
static int pkcs7_bundle_raw_certificates_cb(CBB *out, const void *arg) {
const STACK_OF(CRYPTO_BUFFER) *certs = arg;
CBB certificates;
// See https://tools.ietf.org/html/rfc2315#section-9.1
if (!CBB_add_asn1(out, &certificates,
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
return 0;
}
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(certs); i++) {
CRYPTO_BUFFER *cert = sk_CRYPTO_BUFFER_value(certs, i);
if (!CBB_add_bytes(&certificates, CRYPTO_BUFFER_data(cert),
CRYPTO_BUFFER_len(cert))) {
return 0;
}
}
// |certificates| is a implicitly-tagged SET OF.
return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out);
}
int PKCS7_bundle_raw_certificates(CBB *out,
const STACK_OF(CRYPTO_BUFFER) *certs) {
return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL,
pkcs7_bundle_raw_certificates_cb,
/*signer_infos_cb=*/NULL, certs);
}
int pkcs7_add_signed_data(CBB *out,
int (*digest_algos_cb)(CBB *out, const void *arg),
int (*cert_crl_cb)(CBB *out, const void *arg),
+50
View File
@@ -708,6 +708,56 @@ TEST(PKCS7Test, SortCerts) {
check_order(cert2, cert3, cert1);
}
// Test that we output certificates in the canonical DER order, using the
// CRYPTO_BUFFER version of the parse and bundle functions.
TEST(PKCS7Test, SortCertsRaw) {
// kPKCS7NSS contains three certificates in the canonical DER order.
CBS pkcs7;
CBS_init(&pkcs7, kPKCS7NSS, sizeof(kPKCS7NSS));
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null());
ASSERT_TRUE(certs);
ASSERT_TRUE(PKCS7_get_raw_certificates(certs.get(), &pkcs7, nullptr));
ASSERT_EQ(3u, sk_CRYPTO_BUFFER_num(certs.get()));
CRYPTO_BUFFER *cert1 = sk_CRYPTO_BUFFER_value(certs.get(), 0);
CRYPTO_BUFFER *cert2 = sk_CRYPTO_BUFFER_value(certs.get(), 1);
CRYPTO_BUFFER *cert3 = sk_CRYPTO_BUFFER_value(certs.get(), 2);
auto check_order = [&](CRYPTO_BUFFER *new_cert1, CRYPTO_BUFFER *new_cert2,
CRYPTO_BUFFER *new_cert3) {
// Bundle the certificates in the new order.
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> new_certs(
sk_CRYPTO_BUFFER_new_null());
ASSERT_TRUE(new_certs);
ASSERT_TRUE(bssl::PushToStack(new_certs.get(), bssl::UpRef(new_cert1)));
ASSERT_TRUE(bssl::PushToStack(new_certs.get(), bssl::UpRef(new_cert2)));
ASSERT_TRUE(bssl::PushToStack(new_certs.get(), bssl::UpRef(new_cert3)));
bssl::ScopedCBB cbb;
ASSERT_TRUE(CBB_init(cbb.get(), sizeof(kPKCS7NSS)));
ASSERT_TRUE(PKCS7_bundle_raw_certificates(cbb.get(), new_certs.get()));
// The bundle should be sorted back to the original order.
CBS cbs;
CBS_init(&cbs, CBB_data(cbb.get()), CBB_len(cbb.get()));
bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> result(
sk_CRYPTO_BUFFER_new_null());
ASSERT_TRUE(result);
ASSERT_TRUE(PKCS7_get_raw_certificates(result.get(), &cbs, nullptr));
ASSERT_EQ(sk_CRYPTO_BUFFER_num(certs.get()),
sk_CRYPTO_BUFFER_num(result.get()));
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(certs.get()); i++) {
CRYPTO_BUFFER *a = sk_CRYPTO_BUFFER_value(certs.get(), i);
CRYPTO_BUFFER *b = sk_CRYPTO_BUFFER_value(result.get(), i);
EXPECT_EQ(Bytes(CRYPTO_BUFFER_data(a), CRYPTO_BUFFER_len(a)),
Bytes(CRYPTO_BUFFER_data(b), CRYPTO_BUFFER_len(b)));
}
};
check_order(cert1, cert2, cert3);
check_order(cert3, cert2, cert1);
check_order(cert2, cert3, cert1);
}
// Test that we output CRLs in the canonical DER order.
TEST(PKCS7Test, SortCRLs) {
static const char kCRL1[] = R"(
-1
View File
@@ -112,7 +112,6 @@ struct pbe_suite {
const char *pass, size_t pass_len, CBS *param);
};
#define PKCS5_DEFAULT_ITERATIONS 2048
#define PKCS5_SALT_LEN 8
int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
+1 -1
View File
@@ -469,7 +469,7 @@ int PKCS8_marshal_encrypted_private_key(CBB *out, int pbe_nid,
}
if (iterations <= 0) {
iterations = PKCS5_DEFAULT_ITERATIONS;
iterations = PKCS12_DEFAULT_ITER;
}
// Serialize the input key.
+1 -1
View File
@@ -1161,7 +1161,7 @@ PKCS12 *PKCS12_create(const char *password, const char *name,
cert_nid = NID_pbe_WithSHA1And40BitRC2_CBC;
}
if (iterations == 0) {
iterations = PKCS5_DEFAULT_ITERATIONS;
iterations = PKCS12_DEFAULT_ITER;
}
if (mac_iterations == 0) {
mac_iterations = 1;
+8
View File
@@ -111,6 +111,14 @@ OPENSSL_EXPORT int EVP_DecodeBase64(uint8_t *out, size_t *out_len,
// very specific to PEM. It is also very lenient of invalid input. Use of any of
// these functions is thus deprecated.
// EVP_ENCODE_CTX_new returns a newly-allocated |EVP_ENCODE_CTX| or NULL on
// error. The caller must release the result with |EVP_ENCODE_CTX_free| when
// done.
OPENSSL_EXPORT EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void);
// EVP_ENCODE_CTX_free releases memory associated with |ctx|.
OPENSSL_EXPORT void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx);
// EVP_EncodeInit initialises |*ctx|, which is typically stack
// allocated, for an encoding operation.
//
+13 -1
View File
@@ -201,7 +201,7 @@ OPENSSL_EXPORT int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out,
//
// WARNING: it is unsafe to call this function with unauthenticated
// ciphertext if padding is enabled.
OPENSSL_EXPORT int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out,
OPENSSL_EXPORT int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out,
int *out_len);
// EVP_Cipher performs a one-shot encryption/decryption operation. No partial
@@ -408,6 +408,18 @@ OPENSSL_EXPORT int EVP_DecryptInit(EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *cipher, const uint8_t *key,
const uint8_t *iv);
// EVP_CipherFinal calls |EVP_CipherFinal_ex|.
OPENSSL_EXPORT int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out,
int *out_len);
// EVP_EncryptFinal calls |EVP_EncryptFinal_ex|.
OPENSSL_EXPORT int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out,
int *out_len);
// EVP_DecryptFinal calls |EVP_DecryptFinal_ex|.
OPENSSL_EXPORT int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out,
int *out_len);
// EVP_add_cipher_alias does nothing and returns one.
OPENSSL_EXPORT int EVP_add_cipher_alias(const char *a, const char *b);
+10
View File
@@ -189,6 +189,16 @@ OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void);
// DSA_SIG_free frees the contents of |sig| and then frees |sig| itself.
OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig);
// DSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two components
// of |sig|.
OPENSSL_EXPORT void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
const BIGNUM **out_s);
// DSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may be
// NULL. On success, it takes ownership of each argument and returns one.
// Otherwise, it returns zero.
OPENSSL_EXPORT int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
// DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa|
// and returns an allocated, DSA_SIG structure, or NULL on error.
OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len,
+1 -1
View File
@@ -30,7 +30,7 @@ extern "C" {
// Hybrid Public Key Encryption (HPKE) enables a sender to encrypt messages to a
// receiver with a public key.
//
// See https://tools.ietf.org/html/draft-irtf-cfrg-hpke-08.
// See https://tools.ietf.org/html/draft-irtf-cfrg-hpke-12.
// Parameters.
+9 -4
View File
@@ -49,10 +49,15 @@ OPENSSL_EXPORT int PKCS7_get_raw_certificates(
// them into |X509| objects.
OPENSSL_EXPORT int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs);
// PKCS7_bundle_certificates appends a PKCS#7, SignedData structure containing
// |certs| to |out|. It returns one on success and zero on error. Note that
// certificates in SignedData structures are unordered. The order in |certs|
// will not be preserved.
// PKCS7_bundle_raw_certificates appends a PKCS#7, SignedData structure
// containing |certs| to |out|. It returns one on success and zero on error.
// Note that certificates in SignedData structures are unordered. The order in
// |certs| will not be preserved.
OPENSSL_EXPORT int PKCS7_bundle_raw_certificates(
CBB *out, const STACK_OF(CRYPTO_BUFFER) *certs);
// PKCS7_bundle_certificates behaves like |PKCS7_bundle_raw_certificates| but
// takes |X509| objects as input.
OPENSSL_EXPORT int PKCS7_bundle_certificates(
CBB *out, const STACK_OF(X509) *certs);
+6 -1
View File
@@ -197,6 +197,10 @@ OPENSSL_EXPORT int PKCS12_parse(const PKCS12 *p12, const char *password,
OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
int password_len);
// PKCS12_DEFAULT_ITER is the default number of KDF iterations used when
// creating a |PKCS12| object.
#define PKCS12_DEFAULT_ITER 2048
// PKCS12_create returns a newly-allocated |PKCS12| object containing |pkey|,
// |cert|, and |chain|, encrypted with the specified password. |name|, if not
// NULL, specifies a user-friendly name to encode with the key and
@@ -207,7 +211,8 @@ OPENSSL_EXPORT int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
//
// Each of |key_nid|, |cert_nid|, |iterations|, and |mac_iterations| may be zero
// to use defaults, which are |NID_pbe_WithSHA1And3_Key_TripleDES_CBC|,
// |NID_pbe_WithSHA1And40BitRC2_CBC|, 2048, and one, respectively.
// |NID_pbe_WithSHA1And40BitRC2_CBC|, |PKCS12_DEFAULT_ITER|, and one,
// respectively.
//
// |key_nid| or |cert_nid| may also be -1 to disable encryption of the key or
// certificate, respectively. This option is not recommended and is only
+23 -2
View File
@@ -362,10 +362,31 @@ OPENSSL_EXPORT int SSL_read(SSL *ssl, void *buf, int num);
// SSL_peek behaves like |SSL_read| but does not consume any bytes returned.
OPENSSL_EXPORT int SSL_peek(SSL *ssl, void *buf, int num);
// SSL_pending returns the number of bytes available in |ssl|. It does not read
// from the transport.
// SSL_pending returns the number of buffered, decrypted bytes available for
// read in |ssl|. It does not read from the transport.
//
// In DTLS, it is possible for this function to return zero while there is
// buffered, undecrypted data from the transport in |ssl|. For example,
// |SSL_read| may read a datagram with two records, decrypt the first, and leave
// the second buffered for a subsequent call to |SSL_read|. Callers that wish to
// detect this case can use |SSL_has_pending|.
OPENSSL_EXPORT int SSL_pending(const SSL *ssl);
// SSL_has_pending returns one if |ssl| has buffered, decrypted bytes available
// for read, or if |ssl| has buffered data from the transport that has not yet
// been decrypted. If |ssl| has neither, this function returns zero.
//
// In TLS, BoringSSL does not implement read-ahead, so this function returns one
// if and only if |SSL_pending| would return a non-zero value. In DTLS, it is
// possible for this function to return one while |SSL_pending| returns zero.
// For example, |SSL_read| may read a datagram with two records, decrypt the
// first, and leave the second buffered for a subsequent call to |SSL_read|.
//
// As a result, if this function returns one, the next call to |SSL_read| may
// still fail, read from the transport, or both. The buffered, undecrypted data
// may be invalid or incomplete.
OPENSSL_EXPORT int SSL_has_pending(const SSL *ssl);
// SSL_write writes up to |num| bytes from |buf| into |ssl|. It implicitly runs
// any pending handshakes, including renegotiations when enabled. On success, it
// returns the number of bytes written. Otherwise, it returns <= 0. The caller
+4
View File
@@ -1697,6 +1697,10 @@ int SSL_pending(const SSL *ssl) {
return static_cast<int>(ssl->s3->pending_app_data.size());
}
int SSL_has_pending(const SSL *ssl) {
return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty();
}
int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
return ssl_cert_check_private_key(ctx->cert.get(),
ctx->cert->privatekey.get());
+20
View File
@@ -4966,23 +4966,43 @@ TEST_P(SSLVersionTest, SSLPending) {
ASSERT_TRUE(Connect());
EXPECT_EQ(0, SSL_pending(client_.get()));
EXPECT_EQ(0, SSL_has_pending(client_.get()));
ASSERT_EQ(5, SSL_write(server_.get(), "hello", 5));
ASSERT_EQ(5, SSL_write(server_.get(), "world", 5));
EXPECT_EQ(0, SSL_pending(client_.get()));
EXPECT_EQ(0, SSL_has_pending(client_.get()));
char buf[10];
ASSERT_EQ(1, SSL_peek(client_.get(), buf, 1));
EXPECT_EQ(5, SSL_pending(client_.get()));
EXPECT_EQ(1, SSL_has_pending(client_.get()));
ASSERT_EQ(1, SSL_read(client_.get(), buf, 1));
EXPECT_EQ(4, SSL_pending(client_.get()));
EXPECT_EQ(1, SSL_has_pending(client_.get()));
ASSERT_EQ(4, SSL_read(client_.get(), buf, 10));
EXPECT_EQ(0, SSL_pending(client_.get()));
if (is_dtls()) {
// In DTLS, the two records would have been read as a single datagram and
// buffered inside |client_|. Thus, |SSL_has_pending| should return true.
//
// This test is slightly unrealistic. It relies on |ConnectClientAndServer|
// using a |BIO| pair, which does not preserve datagram boundaries. Reading
// 1 byte, then 4 bytes, from the first record also relies on
// https://crbug.com/boringssl/65. But it does test the codepaths. When
// fixing either of these bugs, this test may need to be redone.
EXPECT_EQ(1, SSL_has_pending(client_.get()));
} else {
// In TLS, we do not overread, so |SSL_has_pending| should report no data is
// buffered.
EXPECT_EQ(0, SSL_has_pending(client_.get()));
}
ASSERT_EQ(2, SSL_read(client_.get(), buf, 2));
EXPECT_EQ(3, SSL_pending(client_.get()));
EXPECT_EQ(1, SSL_has_pending(client_.get()));
}
// Test that post-handshake tickets consumed by |SSL_shutdown| are ignored.
+1 -1
View File
@@ -14,7 +14,7 @@
// Package hpke implements Hybrid Public Key Encryption (HPKE).
//
// See https://tools.ietf.org/html/draft-irtf-cfrg-hpke-08.
// See https://tools.ietf.org/html/draft-irtf-cfrg-hpke-12.
package hpke
import (
+2 -2
View File
@@ -86,9 +86,9 @@ type HpkeTestVector struct {
Exports []ExportTestVector `json:"exports"`
}
type EncryptionTestVector struct {
Plaintext HexString `json:"plaintext"`
Plaintext HexString `json:"pt"`
AdditionalData HexString `json:"aad"`
Ciphertext HexString `json:"ciphertext"`
Ciphertext HexString `json:"ct"`
}
type ExportTestVector struct {
ExportContext HexString `json:"exporter_context"`
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1 +1 @@
This file is ignored. It exists to make no-op commits to trigger new builds.
This file is ignored. It exists to make no-op commits to trigger new builds.