Skip to content

Commit b7acc6b

Browse files
1 parent 69e2177 commit b7acc6b

8 files changed

Lines changed: 244 additions & 64 deletions

File tree

src/crypto/clu_evp_crypto.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,36 @@ int wolfCLU_evp_crypto(const WOLFSSL_EVP_CIPHER* cphr, char* mode, byte* pwdKey,
8888
in = wolfSSL_BIO_new_file(fileIn, "rb");
8989
if (in != NULL && !enc && isBase64) {
9090
word32 decodeSz;
91+
long bioSz;
9192

92-
decodeSz = wolfSSL_BIO_get_len(in);
93-
decodedBase64 = (byte*)XMALLOC(decodeSz, HEAP_HINT,
94-
DYNAMIC_TYPE_TMP_BUFFER);
95-
if (decodedBase64 == NULL) {
93+
if ((bioSz = wolfSSL_BIO_get_len(in)) < 0) {
9694
ret = WOLFCLU_FATAL_ERROR;
9795
}
9896
else {
99-
if (wolfSSL_BIO_read(in, decodedBase64, decodeSz) !=
100-
(int)decodeSz) {
97+
decodeSz = (word32)bioSz;
98+
decodedBase64 = (byte*)XMALLOC(decodeSz, HEAP_HINT,
99+
DYNAMIC_TYPE_TMP_BUFFER);
100+
if (decodedBase64 == NULL) {
101101
ret = WOLFCLU_FATAL_ERROR;
102102
}
103+
else {
104+
if (wolfSSL_BIO_read(in, decodedBase64, decodeSz) !=
105+
(int)decodeSz) {
106+
ret = WOLFCLU_FATAL_ERROR;
107+
}
103108

104-
if (ret == WOLFCLU_SUCCESS &&
105-
Base64_Decode(decodedBase64, decodeSz,
106-
decodedBase64, &decodeSz) != 0) {
107-
ret = WOLFCLU_FATAL_ERROR;
108-
}
109+
if (ret == WOLFCLU_SUCCESS &&
110+
Base64_Decode(decodedBase64, decodeSz,
111+
decodedBase64, &decodeSz) != 0) {
112+
ret = WOLFCLU_FATAL_ERROR;
113+
}
109114

110-
if (ret == WOLFCLU_SUCCESS) {
111-
wolfSSL_BIO_free(in);
112-
in = wolfSSL_BIO_new_mem_buf(decodedBase64, decodeSz);
115+
if (ret == WOLFCLU_SUCCESS) {
116+
wolfSSL_BIO_free(in);
117+
in = wolfSSL_BIO_new_mem_buf(decodedBase64, decodeSz);
118+
}
113119
}
114120
}
115-
116121
}
117122
}
118123
else {

src/dh/clu_dh.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
478478
if (ret == WOLFCLU_SUCCESS && bioIn != NULL) {
479479
DerBuffer* pDer = NULL;
480480
byte* in = NULL;
481-
word32 inSz = 0;
481+
long inSz = 0;
482482
word32 idx = 0;
483483

484484
inSz = wolfSSL_BIO_get_len(bioIn);
@@ -489,7 +489,7 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
489489
}
490490

491491
if (ret == WOLFCLU_SUCCESS &&
492-
wolfSSL_BIO_read(bioIn, in, inSz) <= 0) {
492+
wolfSSL_BIO_read(bioIn, in, (int)inSz) <= 0) {
493493
ret = WOLFCLU_FATAL_ERROR;
494494
}
495495

@@ -510,7 +510,7 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
510510
}
511511

512512
if (ret == WOLFCLU_SUCCESS &&
513-
wc_DhKeyDecode(in, &idx, &dh, inSz) != 0) {
513+
wc_DhKeyDecode(in, &idx, &dh, (int)inSz) != 0) {
514514
wolfCLU_LogError("Unable to decode input params");
515515
ret = WOLFCLU_FATAL_ERROR;
516516
}

src/ocsp/clu_ocsp.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static int ocspClient(OcspClientConfig* config)
395395
typedef struct IndexEntry {
396396
char status;
397397
time_t revocationTime;
398-
char serial[64];
398+
char serial[65];
399399
struct IndexEntry* next;
400400
} IndexEntry;
401401

@@ -466,7 +466,14 @@ static IndexEntry* parseIndexFile(const char* filename)
466466
}
467467
break;
468468
case 3: /* Serial (hex) */
469+
if (XSTRLEN(field) > sizeof(entry->serial)-1) {
470+
wolfCLU_LogError("Field %s too long to fit in entry "
471+
"with max size %lu", field,
472+
(unsigned long)(sizeof(entry->serial)-1));
473+
break;
474+
}
469475
XSTRNCPY(entry->serial, field, sizeof(entry->serial) - 1);
476+
entry->serial[sizeof(entry->serial) - 1] = '\0';
470477
break;
471478
}
472479
fieldNum++;
@@ -478,7 +485,7 @@ static IndexEntry* parseIndexFile(const char* filename)
478485
entry = NULL;
479486
continue;
480487
}
481-
488+
482489
/* For revoked certificates, revocationTime must be valid */
483490
if (entry->status == 'R' && entry->revocationTime == (time_t)-1) {
484491
wolfCLU_LogError("Invalid revocation time for serial %s", entry->serial);

src/sign-verify/clu_dgst_setup.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#include "wolfclu/clu_error_codes.h"
2223
#include <wolfclu/clu_header_main.h>
2324
#include <wolfclu/clu_log.h>
2425
#include <wolfclu/clu_optargs.h>
@@ -202,7 +203,7 @@ int wolfCLU_dgst_setup(int argc, char** argv)
202203

203204
opterr = 0; /* do not display unrecognized options */
204205
optind = 0; /* start at indent 0 */
205-
while ((option = wolfCLU_GetOpt(argc, argv, "",
206+
while (ret == WOLFCLU_SUCCESS && (option = wolfCLU_GetOpt(argc-1, argv, "",
206207
dgst_options, &longIndex )) != -1) {
207208

208209
switch (option) {
@@ -273,6 +274,16 @@ int wolfCLU_dgst_setup(int argc, char** argv)
273274
}
274275
}
275276

277+
/* Detect malformed arguments: if the trailing positional data file was
278+
* instead consumed as the value of a required-argument option, optarg will
279+
* string-match argv[argc-1]. The argc >= 2 guard keeps the argv[argc-2]
280+
* access in bounds. */
281+
if (argc >= 2 && optarg != NULL && XSTRCMP(optarg, argv[argc-1]) == 0) {
282+
wolfCLU_LogError("Malformed arguments last argument read as value for "
283+
"%s", argv[argc-2]);
284+
ret = WOLFCLU_FATAL_ERROR;
285+
}
286+
276287
if (ret == WOLFCLU_SUCCESS) {
277288
if (dataBio == NULL || sigFile == NULL) {
278289
wolfCLU_LogError("error with reading signature or data");

0 commit comments

Comments
 (0)