Skip to content

Commit 9d05b0c

Browse files
committed
Harden X509 DER length handling in wolfSSL_X509_get_der and wolfSSL_i2d_X509
1 parent 4e491ed commit 9d05b0c

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/x509.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4457,6 +4457,10 @@ const byte* wolfSSL_X509_get_der(WOLFSSL_X509* x509, int* outSz)
44574457
if (x509 == NULL || x509->derCert == NULL || outSz == NULL)
44584458
return NULL;
44594459

4460+
if (x509->derCert->length > (word32)INT_MAX) {
4461+
return NULL;
4462+
}
4463+
44604464
*outSz = (int)x509->derCert->length;
44614465
return x509->derCert->buffer;
44624466
}
@@ -8758,7 +8762,7 @@ int wolfSSL_i2d_X509(WOLFSSL_X509* x509, unsigned char** out)
87588762
}
87598763

87608764
der = wolfSSL_X509_get_der(x509, &derSz);
8761-
if (der == NULL) {
8765+
if (der == NULL || derSz <= 0) {
87628766
WOLFSSL_LEAVE("wolfSSL_i2d_X509", MEMORY_E);
87638767
return MEMORY_E;
87648768
}

tests/api/test_ossl_x509_io.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <tests/unit.h>
2323

24+
#include <limits.h>
25+
2426
#ifdef NO_INLINE
2527
#include <wolfssl/wolfcrypt/misc.h>
2628
#else
@@ -72,6 +74,67 @@ int test_wolfSSL_i2d_X509(void)
7274
return EXPECT_RESULT();
7375
}
7476

77+
int test_wolfSSL_X509_get_der_length_guards(void)
78+
{
79+
EXPECT_DECLS;
80+
#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
81+
const unsigned char* cert_buf = server_cert_der_2048;
82+
X509* cert = NULL;
83+
int derSz = 0;
84+
word32 origLen = 0;
85+
86+
ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
87+
ExpectNotNull(cert);
88+
ExpectNotNull(cert->derCert);
89+
90+
if (EXPECT_SUCCESS()) {
91+
origLen = cert->derCert->length;
92+
cert->derCert->length = ((word32)INT_MAX) + 1U;
93+
ExpectNull(wolfSSL_X509_get_der(cert, &derSz));
94+
cert->derCert->length = origLen;
95+
}
96+
97+
X509_free(cert);
98+
#endif
99+
return EXPECT_RESULT();
100+
}
101+
102+
int test_wolfSSL_i2d_X509_der_length_guards(void)
103+
{
104+
EXPECT_DECLS;
105+
#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
106+
const unsigned char* cert_buf = server_cert_der_2048;
107+
unsigned char buf[4] = { 0x11, 0x22, 0x33, 0x44 };
108+
const unsigned char origBuf[4] = { 0x11, 0x22, 0x33, 0x44 };
109+
unsigned char* callerOut = buf;
110+
X509* cert = NULL;
111+
word32 origLen = 0;
112+
113+
ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
114+
ExpectNotNull(cert);
115+
ExpectNotNull(cert->derCert);
116+
117+
if (EXPECT_SUCCESS()) {
118+
origLen = cert->derCert->length;
119+
120+
cert->derCert->length = ((word32)INT_MAX) + 1U;
121+
ExpectIntEQ(i2d_X509(cert, &callerOut), MEMORY_E);
122+
ExpectPtrEq(callerOut, buf);
123+
ExpectIntEQ(XMEMCMP(buf, origBuf, sizeof(buf)), 0);
124+
125+
cert->derCert->length = 0;
126+
ExpectIntEQ(i2d_X509(cert, &callerOut), MEMORY_E);
127+
ExpectPtrEq(callerOut, buf);
128+
ExpectIntEQ(XMEMCMP(buf, origBuf, sizeof(buf)), 0);
129+
130+
cert->derCert->length = origLen;
131+
}
132+
133+
X509_free(cert);
134+
#endif
135+
return EXPECT_RESULT();
136+
}
137+
75138
int test_wolfSSL_PEM_read_X509(void)
76139
{
77140
EXPECT_DECLS;
@@ -244,4 +307,3 @@ int test_wolfSSL_PEM_write_bio_X509(void)
244307
#endif
245308
return EXPECT_RESULT();
246309
}
247-

tests/api/test_ossl_x509_io.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
#include <tests/api/api_decl.h>
2626

2727
int test_wolfSSL_i2d_X509(void);
28+
int test_wolfSSL_X509_get_der_length_guards(void);
29+
int test_wolfSSL_i2d_X509_der_length_guards(void);
2830
int test_wolfSSL_PEM_read_X509(void);
2931
int test_wolfSSL_PEM_write_bio_X509(void);
3032

3133
#define TEST_OSSL_X509_IO_DECLS \
3234
TEST_DECL_GROUP("ossl_x509_io", test_wolfSSL_i2d_X509), \
35+
TEST_DECL_GROUP("ossl_x509_io", \
36+
test_wolfSSL_X509_get_der_length_guards), \
37+
TEST_DECL_GROUP("ossl_x509_io", \
38+
test_wolfSSL_i2d_X509_der_length_guards), \
3339
TEST_DECL_GROUP("ossl_x509_io", test_wolfSSL_PEM_read_X509), \
3440
TEST_DECL_GROUP("ossl_x509_io", test_wolfSSL_PEM_write_bio_X509)
3541

0 commit comments

Comments
 (0)