Skip to content

Commit ba859d2

Browse files
Merge pull request #9817 from LinuxJedi/static-fixes4
Static code analysis fixes
2 parents 5a72a37 + 110f5cb commit ba859d2

7 files changed

Lines changed: 438 additions & 21 deletions

File tree

src/crl.c

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,35 @@ int InitCRL(WOLFSSL_CRL* crl, WOLFSSL_CERT_MANAGER* cm)
9898
}
9999

100100

101+
#ifdef CRL_STATIC_REVOKED_LIST
102+
/* Compare two RevokedCert entries by (serialSz, serialNumber) for sorting.
103+
* Returns < 0, 0, or > 0 like memcmp. */
104+
static int CompareRevokedCert(const RevokedCert* a, const RevokedCert* b)
105+
{
106+
if (a->serialSz != b->serialSz)
107+
return a->serialSz - b->serialSz;
108+
return XMEMCMP(a->serialNumber, b->serialNumber, (size_t)a->serialSz);
109+
}
110+
111+
/* Sort revoked cert array in-place using insertion sort. The array is bounded
112+
* by CRL_MAX_REVOKED_CERTS so O(n^2) is fine. */
113+
static void SortCRL_CertList(RevokedCert* certs, int totalCerts)
114+
{
115+
int i, j;
116+
RevokedCert tmp;
117+
118+
for (i = 1; i < totalCerts; i++) {
119+
XMEMCPY(&tmp, &certs[i], sizeof(RevokedCert));
120+
j = i - 1;
121+
while (j >= 0 && CompareRevokedCert(&certs[j], &tmp) > 0) {
122+
XMEMCPY(&certs[j + 1], &certs[j], sizeof(RevokedCert));
123+
j--;
124+
}
125+
XMEMCPY(&certs[j + 1], &tmp, sizeof(RevokedCert));
126+
}
127+
}
128+
#endif /* CRL_STATIC_REVOKED_LIST */
129+
101130
/* Initialize CRL Entry */
102131
static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl, const byte* buff,
103132
int verified, void* heap)
@@ -132,12 +161,15 @@ static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl, const byte* buff,
132161
#endif
133162
#ifdef CRL_STATIC_REVOKED_LIST
134163
/* ParseCRL_CertList() has already cached the Revoked certs into
135-
the crle->certs array */
164+
the crle->certs array. Sort it so binary search in
165+
FindRevokedSerial works correctly. */
166+
crle->totalCerts = dcrl->totalCerts;
167+
SortCRL_CertList(crle->certs, crle->totalCerts);
136168
#else
137169
crle->certs = dcrl->certs; /* take ownership */
170+
crle->totalCerts = dcrl->totalCerts;
138171
#endif
139172
dcrl->certs = NULL;
140-
crle->totalCerts = dcrl->totalCerts;
141173
crle->crlNumberSet = dcrl->crlNumberSet;
142174
if (crle->crlNumberSet) {
143175
XMEMCPY(crle->crlNumber, dcrl->crlNumber, sizeof(crle->crlNumber));
@@ -313,25 +345,52 @@ static int FindRevokedSerial(RevokedCert* rc, byte* serial, int serialSz,
313345
int ret = 0;
314346
byte hash[SIGNER_DIGEST_SIZE];
315347
#ifdef CRL_STATIC_REVOKED_LIST
316-
/* do binary search */
317-
int low, high, mid;
348+
if (serialHash == NULL) {
349+
/* Binary search by (serialSz, serialNumber). The array was sorted in
350+
* InitCRL_Entry by the same comparison key. */
351+
int low = 0;
352+
int high = totalCerts - 1;
318353

319-
low = 0;
320-
high = totalCerts - 1;
354+
while (low <= high) {
355+
int mid = (low + high) / 2;
356+
int cmp;
321357

322-
while (low <= high) {
323-
mid = (low + high) / 2;
358+
/* Compare by serial size first, then by serial content. Shorter
359+
* serials sort before longer ones. */
360+
if (rc[mid].serialSz != serialSz) {
361+
cmp = rc[mid].serialSz - serialSz;
362+
}
363+
else {
364+
cmp = XMEMCMP(rc[mid].serialNumber, serial,
365+
(size_t)rc[mid].serialSz);
366+
}
324367

325-
if (XMEMCMP(rc[mid].serialNumber, serial, rc->serialSz) < 0) {
326-
low = mid + 1;
327-
}
328-
else if (XMEMCMP(rc[mid].serialNumber, serial, rc->serialSz) > 0) {
329-
high = mid - 1;
368+
if (cmp < 0) {
369+
low = mid + 1;
370+
}
371+
else if (cmp > 0) {
372+
high = mid - 1;
373+
}
374+
else {
375+
WOLFSSL_MSG("Cert revoked");
376+
ret = CRL_CERT_REVOKED;
377+
break;
378+
}
330379
}
331-
else {
332-
WOLFSSL_MSG("Cert revoked");
333-
ret = CRL_CERT_REVOKED;
334-
break;
380+
}
381+
else {
382+
/* Hash-based lookup -- linear scan required since the array is sorted
383+
* by serial number, not by hash. */
384+
int i;
385+
for (i = 0; i < totalCerts; i++) {
386+
ret = CalcHashId(rc[i].serialNumber, (word32)rc[i].serialSz, hash);
387+
if (ret != 0)
388+
break;
389+
if (XMEMCMP(hash, serialHash, SIGNER_DIGEST_SIZE) == 0) {
390+
WOLFSSL_MSG("Cert revoked");
391+
ret = CRL_CERT_REVOKED;
392+
break;
393+
}
335394
}
336395
}
337396
#else

src/internal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,8 @@ static int ImportKeyState(WOLFSSL* ssl, const byte* exp, word32 len, byte ver,
11231123
idx += OPAQUE16_LEN;
11241124

11251125
if (wordCount > WOLFSSL_DTLS_WINDOW_WORDS) {
1126+
wordAdj = (wordCount - WOLFSSL_DTLS_WINDOW_WORDS) * sizeof(word32);
11261127
wordCount = WOLFSSL_DTLS_WINDOW_WORDS;
1127-
wordAdj = (WOLFSSL_DTLS_WINDOW_WORDS - wordCount) * sizeof(word32);
11281128
}
11291129

11301130
XMEMSET(keys->peerSeq[0].window, 0xFF, DTLS_SEQ_SZ);
@@ -1139,8 +1139,8 @@ static int ImportKeyState(WOLFSSL* ssl, const byte* exp, word32 len, byte ver,
11391139
idx += OPAQUE16_LEN;
11401140

11411141
if (wordCount > WOLFSSL_DTLS_WINDOW_WORDS) {
1142+
wordAdj = (wordCount - WOLFSSL_DTLS_WINDOW_WORDS) * sizeof(word32);
11421143
wordCount = WOLFSSL_DTLS_WINDOW_WORDS;
1143-
wordAdj = (WOLFSSL_DTLS_WINDOW_WORDS - wordCount) * sizeof(word32);
11441144
}
11451145

11461146
XMEMSET(keys->peerSeq[0].prevWindow, 0xFF, DTLS_SEQ_SZ);

src/ssl.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16675,8 +16675,12 @@ int wolfSSL_select_next_proto(unsigned char **out, unsigned char *outLen,
1667516675

1667616676
for (i = 0; i < inLen; i += lenIn) {
1667716677
lenIn = in[i++];
16678+
if (lenIn == 0 || i + lenIn > inLen)
16679+
break;
1667816680
for (j = 0; j < clientLen; j += lenClient) {
1667916681
lenClient = clientNames[j++];
16682+
if (lenClient == 0 || j + lenClient > clientLen)
16683+
break;
1668016684

1668116685
if (lenIn != lenClient)
1668216686
continue;
@@ -16689,8 +16693,14 @@ int wolfSSL_select_next_proto(unsigned char **out, unsigned char *outLen,
1668916693
}
1669016694
}
1669116695

16692-
*out = (unsigned char *)clientNames + 1;
16693-
*outLen = clientNames[0];
16696+
if (clientLen > 0 && (unsigned int)clientNames[0] + 1 <= clientLen) {
16697+
*out = (unsigned char *)clientNames + 1;
16698+
*outLen = clientNames[0];
16699+
}
16700+
else {
16701+
*out = (unsigned char *)clientNames;
16702+
*outLen = 0;
16703+
}
1669416704
return WOLFSSL_NPN_NO_OVERLAP;
1669516705
}
1669616706

src/tls.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13605,6 +13605,9 @@ static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
1360513605
}
1360613606
/* read hello inner len */
1360713607
ato16(readBuf_p, &ech->innerClientHelloLen);
13608+
if (ech->innerClientHelloLen < WC_AES_BLOCK_SIZE) {
13609+
return BUFFER_ERROR;
13610+
}
1360813611
ech->innerClientHelloLen -= WC_AES_BLOCK_SIZE;
1360913612
readBuf_p += 2;
1361013613
ech->outerClientPayload = readBuf_p;

0 commit comments

Comments
 (0)