Skip to content

Commit 33dc78a

Browse files
Read size from DER header to avoid having to seek back and forth in the file.
Also support other types (CSR, CRL, PKCS12 etc).
1 parent 7e8c2c4 commit 33dc78a

6 files changed

Lines changed: 138 additions & 15 deletions

File tree

‎certs/crl/gencrls.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ check_result $?
201201
echo "Step 26"
202202
openssl crl -in crl.pem -inform PEM -out crl.der -outform DER
203203
openssl crl -in crl2.pem -inform PEM -out crl2.der -outform DER
204+
cat crl.der crl2.der > crl-chain.der
204205

205206
# clear state for RSA-PSS revoke
206207
cp blank.index.txt demoCA/index.txt

‎certs/crl/include.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ EXTRA_DIST += \
1616
certs/crl/wolfssl.cnf \
1717
certs/crl/crl.der \
1818
certs/crl/crl2.der \
19+
certs/crl/crl-chain.der \
1920
certs/crl/crl_rsapss.pem \
2021
certs/crl/crl_reason.pem \
2122
certs/crl/bad_time_fmt.pem

‎certs/include.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ EXTRA_DIST += \
8787
certs/csr.dsa.pem \
8888
certs/csr.signed.der \
8989
certs/csr.ext.der \
90+
certs/csr-chain.der \
9091
certs/entity-no-ca-bool-cert.pem \
9192
certs/entity-no-ca-bool-key.pem \
9293
certs/x942dh2048.der \

‎src/x509.c‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9893,12 +9893,35 @@ int wolfSSL_X509_REQ_verify(WOLFSSL_X509* x509, WOLFSSL_EVP_PKEY* pkey)
98939893
#endif /* WOLFSSL_CERT_REQ */
98949894

98959895
#if !defined(NO_FILESYSTEM)
9896+
/* Size of the DER SEQUENCE at the file's current position, or 0 when it can't
9897+
* be determined or exceeds avail. The file position is unchanged. */
9898+
static long d2i_fp_der_sz(XFILE file, long avail)
9899+
{
9900+
byte hdr[6];
9901+
word32 idx = 0;
9902+
int len = 0;
9903+
long start = XFTELL(file);
9904+
long hdrSz = (avail < (long)sizeof(hdr)) ? avail : (long)sizeof(hdr);
9905+
long rdSz;
9906+
9907+
if (start < 0) {
9908+
return 0;
9909+
}
9910+
rdSz = (long)XFREAD(hdr, 1, (size_t)hdrSz, file);
9911+
if (XFSEEK(file, start, XSEEK_SET) != 0 || rdSz != hdrSz ||
9912+
GetSequence_ex(hdr, &idx, &len, (word32)hdrSz, 0) < 0 ||
9913+
len == 0 || (long)idx + len > avail) {
9914+
return 0;
9915+
}
9916+
return (long)idx + len;
9917+
}
9918+
98969919
static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
98979920
{
98989921
void *newx509 = NULL;
98999922
byte *fileBuffer = NULL;
99009923
long sz = 0;
9901-
long start = 0;
9924+
long derSz;
99029925

99039926
/* init variable */
99049927
if (x509)
@@ -9910,31 +9933,30 @@ static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
99109933
}
99119934

99129935
/* Read from the current position. */
9913-
if (wolfssl_file_len(file, &sz) == 0) {
9914-
start = XFTELL(file);
9915-
}
9916-
if (sz <= 0 || start < 0) {
9936+
if (wolfssl_file_len(file, &sz) != 0) {
99179937
WOLFSSL_MSG("d2i_X509_fp_ex file size error");
99189938
return NULL;
99199939
}
99209940

9941+
/* Read only the first object, leaving the file after it. */
9942+
derSz = d2i_fp_der_sz(file, sz);
9943+
if (derSz > 0) {
9944+
sz = derSz;
9945+
}
9946+
/* BER PKCS#12 may use an indefinite length, so read it all. */
9947+
else if (type != PKCS12_TYPE) {
9948+
WOLFSSL_MSG("d2i_X509_fp_ex bad DER header");
9949+
return NULL;
9950+
}
9951+
99219952
fileBuffer = (byte *)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
99229953
if (fileBuffer != NULL) {
99239954
if ((long)XFREAD(fileBuffer, 1, (size_t)sz, file) != sz) {
99249955
WOLFSSL_MSG("File read failed");
99259956
goto err_exit;
99269957
}
99279958
if (type == CERT_TYPE) {
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-
}
9959+
newx509 = (void *)wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
99389960
}
99399961
#ifdef HAVE_CRL
99409962
else if (type == CRL_TYPE) {

‎tests/api/test_ossl_x509_io.c‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,102 @@ int test_wolfSSL_d2i_X509_fp_bio_concatenated(void)
196196
return EXPECT_RESULT();
197197
}
198198

199+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
200+
(defined(HAVE_CRL) || defined(WOLFSSL_CERT_REQ) || \
201+
(!defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12)))
202+
static long test_file_sz(const char* fname)
203+
{
204+
long sz = -1;
205+
XFILE f = XFOPEN(fname, "rb");
206+
207+
if (f != XBADFILE) {
208+
if (XFSEEK(f, 0, XSEEK_END) == 0)
209+
sz = (long)XFTELL(f);
210+
XFCLOSE(f);
211+
}
212+
return sz;
213+
}
214+
#endif
215+
216+
int test_wolfSSL_d2i_fp_concatenated(void)
217+
{
218+
EXPECT_DECLS;
219+
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
220+
XFILE f = XBADFILE;
221+
long sz1 = 0;
222+
long sz2 = 0;
223+
224+
#ifdef HAVE_CRL
225+
{
226+
X509_CRL* crl = NULL;
227+
228+
/* crl-chain.der is crl.der followed by crl2.der */
229+
ExpectIntGT(sz1 = test_file_sz("./certs/crl/crl.der"), 0);
230+
ExpectIntGT(sz2 = test_file_sz("./certs/crl/crl2.der"), 0);
231+
ExpectTrue((f = XFOPEN("./certs/crl/crl-chain.der", "rb")) !=
232+
XBADFILE);
233+
ExpectNotNull(crl = d2i_X509_CRL_fp(f, NULL));
234+
ExpectIntEQ((long)XFTELL(f), sz1);
235+
X509_CRL_free(crl);
236+
crl = NULL;
237+
ExpectNotNull(crl = d2i_X509_CRL_fp(f, NULL));
238+
ExpectIntEQ((long)XFTELL(f), sz1 + sz2);
239+
X509_CRL_free(crl);
240+
crl = NULL;
241+
ExpectNull(d2i_X509_CRL_fp(f, NULL));
242+
if (f != XBADFILE) {
243+
XFCLOSE(f);
244+
f = XBADFILE;
245+
}
246+
}
247+
#endif
248+
#ifdef WOLFSSL_CERT_REQ
249+
{
250+
X509_REQ* req = NULL;
251+
252+
/* csr-chain.der is csr.signed.der followed by csr.ext.der */
253+
ExpectIntGT(sz1 = test_file_sz("./certs/csr.signed.der"), 0);
254+
ExpectIntGT(sz2 = test_file_sz("./certs/csr.ext.der"), 0);
255+
ExpectTrue((f = XFOPEN("./certs/csr-chain.der", "rb")) != XBADFILE);
256+
ExpectNotNull(req = d2i_X509_REQ_fp(f, NULL));
257+
ExpectIntEQ((long)XFTELL(f), sz1);
258+
X509_REQ_free(req);
259+
req = NULL;
260+
ExpectNotNull(req = d2i_X509_REQ_fp(f, NULL));
261+
ExpectIntEQ((long)XFTELL(f), sz1 + sz2);
262+
X509_REQ_free(req);
263+
req = NULL;
264+
ExpectNull(d2i_X509_REQ_fp(f, NULL));
265+
if (f != XBADFILE) {
266+
XFCLOSE(f);
267+
f = XBADFILE;
268+
}
269+
}
270+
#endif
271+
#if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12)
272+
{
273+
WOLFSSL_X509_PKCS12* p12 = NULL;
274+
275+
ExpectIntGT(sz1 = test_file_sz("./certs/ecc-rsa-server.p12"), 0);
276+
ExpectTrue((f = XFOPEN("./certs/ecc-rsa-server.p12", "rb")) !=
277+
XBADFILE);
278+
ExpectNotNull(p12 = wolfSSL_d2i_PKCS12_fp(f, NULL));
279+
ExpectIntEQ((long)XFTELL(f), sz1);
280+
wc_PKCS12_free(p12);
281+
ExpectNull(wolfSSL_d2i_PKCS12_fp(f, NULL));
282+
if (f != XBADFILE) {
283+
XFCLOSE(f);
284+
f = XBADFILE;
285+
}
286+
}
287+
#endif
288+
(void)sz1;
289+
(void)sz2;
290+
(void)f;
291+
#endif
292+
return EXPECT_RESULT();
293+
}
294+
199295
int test_wolfSSL_X509_get_der_length_guards(void)
200296
{
201297
EXPECT_DECLS;

‎tests/api/test_ossl_x509_io.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
int test_wolfSSL_i2d_X509(void);
2828
int test_wolfSSL_d2i_X509_concatenated(void);
2929
int test_wolfSSL_d2i_X509_fp_bio_concatenated(void);
30+
int test_wolfSSL_d2i_fp_concatenated(void);
3031
int test_wolfSSL_X509_get_der_length_guards(void);
3132
int test_wolfSSL_i2d_X509_der_length_guards(void);
3233
int test_wolfSSL_PEM_read_X509(void);
@@ -38,6 +39,7 @@ int test_wolfSSL_PEM_write_bio_X509(void);
3839
test_wolfSSL_d2i_X509_concatenated), \
3940
TEST_DECL_GROUP("ossl_x509_io", \
4041
test_wolfSSL_d2i_X509_fp_bio_concatenated), \
42+
TEST_DECL_GROUP("ossl_x509_io", test_wolfSSL_d2i_fp_concatenated), \
4143
TEST_DECL_GROUP("ossl_x509_io", \
4244
test_wolfSSL_X509_get_der_length_guards), \
4345
TEST_DECL_GROUP("ossl_x509_io", \

0 commit comments

Comments
 (0)