Skip to content

Commit 7e8c2c4

Browse files
Fix wolfSSL_d2i_X509_fp's handling of multiple certs in a single DER.
1 parent 017fade commit 7e8c2c4

3 files changed

Lines changed: 78 additions & 10 deletions

File tree

‎src/x509.c‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9898,6 +9898,7 @@ static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
98989898
void *newx509 = NULL;
98999899
byte *fileBuffer = NULL;
99009900
long sz = 0;
9901+
long start = 0;
99019902

99029903
/* init variable */
99039904
if (x509)
@@ -9908,16 +9909,11 @@ static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
99089909
return NULL;
99099910
}
99109911

9911-
/* determine file size */
9912-
if (XFSEEK(file, 0, XSEEK_END) != 0) {
9913-
return NULL;
9914-
}
9915-
sz = XFTELL(file);
9916-
if (XFSEEK(file, 0, XSEEK_SET) != 0) {
9917-
return NULL;
9912+
/* Read from the current position. */
9913+
if (wolfssl_file_len(file, &sz) == 0) {
9914+
start = XFTELL(file);
99189915
}
9919-
9920-
if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
9916+
if (sz <= 0 || start < 0) {
99219917
WOLFSSL_MSG("d2i_X509_fp_ex file size error");
99229918
return NULL;
99239919
}
@@ -9929,7 +9925,16 @@ static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
99299925
goto err_exit;
99309926
}
99319927
if (type == CERT_TYPE) {
9932-
newx509 = (void *)wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
9928+
const byte* p = fileBuffer;
9929+
9930+
/* Decode one certificate and leave the file after it. */
9931+
newx509 = (void *)wolfSSL_d2i_X509(NULL, &p, (int)sz);
9932+
if (newx509 != NULL &&
9933+
XFSEEK(file, start + (long)(p - fileBuffer), XSEEK_SET)
9934+
!= 0) {
9935+
wolfSSL_X509_free((WOLFSSL_X509*)newx509);
9936+
newx509 = NULL;
9937+
}
99339938
}
99349939
#ifdef HAVE_CRL
99359940
else if (type == CRL_TYPE) {

‎tests/api/test_ossl_x509_io.c‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,66 @@ int test_wolfSSL_d2i_X509_concatenated(void)
136136
return EXPECT_RESULT();
137137
}
138138

139+
int test_wolfSSL_d2i_X509_fp_bio_concatenated(void)
140+
{
141+
EXPECT_DECLS;
142+
#if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && \
143+
!defined(NO_RSA) && !defined(NO_FILESYSTEM)
144+
/* server-cert.der followed by ca-cert.der */
145+
const char* fname = "./certs/server-cert-chain.der";
146+
const int srvSz = (int)sizeof_server_cert_der_2048;
147+
const int caSz = (int)sizeof_ca_cert_der_2048;
148+
XFILE f = XBADFILE;
149+
X509* cert = NULL;
150+
const byte* der = NULL;
151+
int derSz = 0;
152+
#ifndef NO_BIO
153+
BIO* bio = NULL;
154+
#endif
155+
156+
/* Each call decodes one certificate and leaves the file after it. */
157+
ExpectTrue((f = XFOPEN(fname, "rb")) != XBADFILE);
158+
ExpectNotNull(cert = d2i_X509_fp(f, NULL));
159+
ExpectIntEQ((int)XFTELL(f), srvSz);
160+
ExpectNotNull(der = wolfSSL_X509_get_der(cert, &derSz));
161+
ExpectIntEQ(derSz, srvSz);
162+
ExpectBufEQ(der, server_cert_der_2048, srvSz);
163+
X509_free(cert);
164+
cert = NULL;
165+
ExpectNotNull(cert = d2i_X509_fp(f, NULL));
166+
ExpectIntEQ((int)XFTELL(f), srvSz + caSz);
167+
ExpectNotNull(der = wolfSSL_X509_get_der(cert, &derSz));
168+
ExpectIntEQ(derSz, caSz);
169+
ExpectBufEQ(der, ca_cert_der_2048, caSz);
170+
X509_free(cert);
171+
cert = NULL;
172+
ExpectNull(d2i_X509_fp(f, NULL));
173+
if (f != XBADFILE) {
174+
XFCLOSE(f);
175+
f = XBADFILE;
176+
}
177+
178+
#ifndef NO_BIO
179+
ExpectNotNull(bio = BIO_new_file(fname, "rb"));
180+
ExpectNotNull(cert = d2i_X509_bio(bio, NULL));
181+
ExpectNotNull(der = wolfSSL_X509_get_der(cert, &derSz));
182+
ExpectIntEQ(derSz, srvSz);
183+
ExpectBufEQ(der, server_cert_der_2048, srvSz);
184+
X509_free(cert);
185+
cert = NULL;
186+
ExpectNotNull(cert = d2i_X509_bio(bio, NULL));
187+
ExpectNotNull(der = wolfSSL_X509_get_der(cert, &derSz));
188+
ExpectIntEQ(derSz, caSz);
189+
ExpectBufEQ(der, ca_cert_der_2048, caSz);
190+
X509_free(cert);
191+
cert = NULL;
192+
ExpectNull(d2i_X509_bio(bio, NULL));
193+
BIO_free(bio);
194+
#endif
195+
#endif
196+
return EXPECT_RESULT();
197+
}
198+
139199
int test_wolfSSL_X509_get_der_length_guards(void)
140200
{
141201
EXPECT_DECLS;

‎tests/api/test_ossl_x509_io.h‎

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

2727
int test_wolfSSL_i2d_X509(void);
2828
int test_wolfSSL_d2i_X509_concatenated(void);
29+
int test_wolfSSL_d2i_X509_fp_bio_concatenated(void);
2930
int test_wolfSSL_X509_get_der_length_guards(void);
3031
int test_wolfSSL_i2d_X509_der_length_guards(void);
3132
int test_wolfSSL_PEM_read_X509(void);
@@ -35,6 +36,8 @@ int test_wolfSSL_PEM_write_bio_X509(void);
3536
TEST_DECL_GROUP("ossl_x509_io", test_wolfSSL_i2d_X509), \
3637
TEST_DECL_GROUP("ossl_x509_io", \
3738
test_wolfSSL_d2i_X509_concatenated), \
39+
TEST_DECL_GROUP("ossl_x509_io", \
40+
test_wolfSSL_d2i_X509_fp_bio_concatenated), \
3841
TEST_DECL_GROUP("ossl_x509_io", \
3942
test_wolfSSL_X509_get_der_length_guards), \
4043
TEST_DECL_GROUP("ossl_x509_io", \

0 commit comments

Comments
 (0)