Skip to content

Commit ad6a98e

Browse files
committed
Add SHRT_MAX caps to bound iteration and input lengths
1 parent 9651480 commit ad6a98e

5 files changed

Lines changed: 36 additions & 0 deletions

File tree

crypto/bytestring/cbb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
614614
return 0;
615615
}
616616

617+
if (len > (size_t)SHRT_MAX) {
618+
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
619+
return 0;
620+
}
621+
617622
CBS cbs;
618623
CBS_init(&cbs, (const uint8_t *)text, len);
619624

crypto/fipsmodule/kdf/sskdf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

44
#include <assert.h>
5+
#include <limits.h>
56
#include <openssl/base.h>
67
#include <openssl/digest.h>
78
#include <openssl/hmac.h>
@@ -328,6 +329,11 @@ int SSKDF_hmac(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
328329
sskdf_variant_ctx ctx = {0};
329330
int ret = 0;
330331

332+
if (salt_len > SHRT_MAX) {
333+
OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
334+
goto end;
335+
}
336+
331337
if (!sskdf_variant_hmac_ctx_init(&ctx, digest, salt, salt_len)) {
332338
FIPS_service_indicator_unlock_state();
333339
return 0;

crypto/ocsp/ocsp_verify.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

4+
#include <limits.h>
45
#include <string.h>
56
#include "../internal.h"
67
#include "internal.h"
@@ -346,6 +347,11 @@ int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs, X509_STORE *st,
346347
return -1;
347348
}
348349

350+
if (sk_X509_num(certs) > SHRT_MAX || sk_X509_num(bs->certs) > SHRT_MAX) {
351+
OPENSSL_PUT_ERROR(OCSP, ERR_R_OVERFLOW);
352+
return -1;
353+
}
354+
349355
X509 *signer;
350356
STACK_OF(X509) *chain = NULL;
351357
STACK_OF(X509) *untrusted = NULL;

crypto/pkcs7/pkcs7.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <openssl/pkcs7.h>
55

6+
#include <limits.h>
67
#include <openssl/bytestring.h>
78
#include <openssl/err.h>
89
#include <openssl/mem.h>
@@ -748,6 +749,10 @@ BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) {
748749
}
749750

750751

752+
if (md_sk != NULL && sk_X509_ALGOR_num(md_sk) > SHRT_MAX) {
753+
OPENSSL_PUT_ERROR(PKCS7, ERR_R_OVERFLOW);
754+
goto err;
755+
}
751756
for (size_t i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
752757
if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) {
753758
goto err;
@@ -1269,6 +1274,11 @@ static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) {
12691274
goto err;
12701275
}
12711276

1277+
if (sk_PKCS7_RECIP_INFO_num(rsk) > SHRT_MAX) {
1278+
OPENSSL_PUT_ERROR(PKCS7, ERR_R_OVERFLOW);
1279+
goto err;
1280+
}
1281+
12721282
if ((cipher_bio = BIO_new(BIO_f_cipher())) == NULL) {
12731283
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB);
12741284
goto err;
@@ -1667,6 +1677,10 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
16671677
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
16681678
goto out;
16691679
}
1680+
if (sk_PKCS7_SIGNER_INFO_num(sinfos) > SHRT_MAX) {
1681+
OPENSSL_PUT_ERROR(PKCS7, ERR_R_OVERFLOW);
1682+
goto out;
1683+
}
16701684

16711685
if ((signers = PKCS7_get0_signers(p7, certs, flags)) == NULL) {
16721686
goto out;

crypto/x509/x509spki.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) 1999 The OpenSSL Project. All rights reserved.
33
// SPDX-License-Identifier: Apache-2.0
44

5+
#include <limits.h>
56
#include <string.h>
67

78
#include <openssl/base64.h>
@@ -38,6 +39,10 @@ NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, ossl_ssize_t len) {
3839
if (len <= 0) {
3940
len = strlen(str);
4041
}
42+
if (len > SHRT_MAX) {
43+
OPENSSL_PUT_ERROR(X509, ERR_R_OVERFLOW);
44+
return NULL;
45+
}
4146
if (!EVP_DecodedLength(&spki_len, len)) {
4247
OPENSSL_PUT_ERROR(X509, X509_R_BASE64_DECODE_ERROR);
4348
return NULL;

0 commit comments

Comments
 (0)