Skip to content

Commit b19a33d

Browse files
fenrir fixes for closing fd, sanity check init calls, buffer scope, sanity check argmunts passed in
1 parent 7f1b177 commit b19a33d

8 files changed

Lines changed: 96 additions & 36 deletions

File tree

src/crypto/clu_decrypt.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
217217
wolfCLU_LogError("bad length %d found", length);
218218
ret = -1;
219219
}
220-
/* reset tempMax for smaller decryption */
221-
XFWRITE(output, 1, length, outFile);
220+
else {
221+
/* reset tempMax for smaller decryption */
222+
XFWRITE(output, 1, length, outFile);
223+
}
222224
}
223225
else {
224226
if (output != NULL)

src/dh/clu_dh.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
381381
byte genKey = 0;
382382
byte check = 0;
383383
byte noOut = 0;
384+
byte rngInited = 0;
385+
byte dhInited = 0;
384386
WOLFSSL_BIO *bioIn = NULL;
385387
WOLFSSL_BIO *bioOut = NULL;
386388

@@ -457,11 +459,20 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
457459
}
458460
}
459461

460-
/* try initializing both because both get free'd regardless at the end */
461-
if (wc_InitRng(&rng) != 0 || wc_InitDhKey(&dh) != 0) {
462-
wolfCLU_LogError("Unable to initialize rng and dh");
462+
if (wc_InitRng(&rng) != 0) {
463+
wolfCLU_LogError("Unable to initialize rng");
463464
ret = WOLFCLU_FATAL_ERROR;
464465
}
466+
else {
467+
rngInited = 1;
468+
}
469+
if (ret == WOLFCLU_SUCCESS && wc_InitDhKey(&dh) != 0) {
470+
wolfCLU_LogError("Unable to initialize dh");
471+
ret = WOLFCLU_FATAL_ERROR;
472+
}
473+
else if (ret == WOLFCLU_SUCCESS) {
474+
dhInited = 1;
475+
}
465476

466477
/* read in parameters */
467478
if (ret == WOLFCLU_SUCCESS && bioIn != NULL) {
@@ -789,8 +800,10 @@ int wolfCLU_DhParamSetup(int argc, char** argv)
789800
wolfSSL_BIO_free(bioIn);
790801
wolfSSL_BIO_free(bioOut);
791802

792-
wc_FreeDhKey(&dh);
793-
wc_FreeRng(&rng);
803+
if (dhInited)
804+
wc_FreeDhKey(&dh);
805+
if (rngInited)
806+
wc_FreeRng(&rng);
794807

795808
return ret;
796809
#else

src/dsa/clu_dsa.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ int wolfCLU_DsaParamSetup(int argc, char** argv)
5959
char* out = NULL;
6060
byte genKey = 0;
6161
byte noOut = 0;
62+
byte rngInited = 0;
63+
byte dsaInited = 0;
6264
WOLFSSL_BIO *bioIn = NULL;
6365
WOLFSSL_BIO *bioOut = NULL;
6466

@@ -120,11 +122,20 @@ int wolfCLU_DsaParamSetup(int argc, char** argv)
120122
}
121123
}
122124

123-
/* try initializing both because both get free'd regardless at the end */
124-
if (wc_InitRng(&rng) != 0 || wc_InitDsaKey(&dsa) != 0) {
125-
wolfCLU_LogError("Unable to initialize rng and dsa");
125+
if (wc_InitRng(&rng) != 0) {
126+
wolfCLU_LogError("Unable to initialize rng");
126127
ret = WOLFCLU_FATAL_ERROR;
127128
}
129+
else {
130+
rngInited = 1;
131+
}
132+
if (ret == WOLFCLU_SUCCESS && wc_InitDsaKey(&dsa) != 0) {
133+
wolfCLU_LogError("Unable to initialize dsa");
134+
ret = WOLFCLU_FATAL_ERROR;
135+
}
136+
else if (ret == WOLFCLU_SUCCESS) {
137+
dsaInited = 1;
138+
}
128139

129140
/* read in parameters */
130141
if (ret == WOLFCLU_SUCCESS && bioIn != NULL) {
@@ -342,8 +353,10 @@ int wolfCLU_DsaParamSetup(int argc, char** argv)
342353
wolfSSL_BIO_free(bioIn);
343354
wolfSSL_BIO_free(bioOut);
344355

345-
wc_FreeDsaKey(&dsa);
346-
wc_FreeRng(&rng);
356+
if (dsaInited)
357+
wc_FreeDsaKey(&dsa);
358+
if (rngInited)
359+
wc_FreeRng(&rng);
347360

348361
return ret;
349362
#else

src/ecparam/clu_ecparam.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ int wolfCLU_ecparam(int argc, char** argv)
151151
break;
152152
}
153153
XSTRNCPY(name, optarg, ECC_MAXNAME);
154+
name[ECC_MAXNAME - 1] = '\0';
154155

155156
/* convert name to upper case */
156157
for (i = 0; i < (int)XSTRLEN(name); i++)

src/genkey/clu_genkey.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,13 @@ int wolfCLU_genKey_ML_DSA(WC_RNG* rng, char* fName, int directive, int fmt,
14371437
XMEMCPY(fOutNameBuf + fNameSz, fExtPub, fExtSz);
14381438
WOLFCLU_LOG(WOLFCLU_L0, "Public key file = %s", fOutNameBuf);
14391439

1440+
/* free any prior derBuf (from PRIV path or initial alloc)
1441+
* before reallocating for the public key */
1442+
if (derBuf != NULL) {
1443+
wolfCLU_ForceZero(derBuf, keySz);
1444+
XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
1445+
derBuf = NULL;
1446+
}
14401447
derBuf = (byte*)XMALLOC(keySz, HEAP_HINT,
14411448
DYNAMIC_TYPE_TMP_BUFFER);
14421449
if (derBuf == NULL) {
@@ -1564,6 +1571,7 @@ enum wc_XmssRc wolfCLU_XmssKey_WriteCb(const byte * priv,
15641571
if (n_write != privSz) {
15651572
fprintf(stderr, "error: wrote %zu, expected %d: %d\n", n_write, privSz,
15661573
ferror(file));
1574+
fclose(file);
15671575
return WC_XMSS_RC_WRITE_FAIL;
15681576
}
15691577

@@ -1584,6 +1592,7 @@ enum wc_XmssRc wolfCLU_XmssKey_WriteCb(const byte * priv,
15841592
buff = malloc(privSz);
15851593
if (buff == NULL) {
15861594
fprintf(stderr, "error: malloc(%d) failed\n", privSz);
1595+
fclose(file);
15871596
return WC_XMSS_RC_WRITE_FAIL;
15881597
}
15891598

@@ -1595,6 +1604,7 @@ enum wc_XmssRc wolfCLU_XmssKey_WriteCb(const byte * priv,
15951604
fprintf(stderr, "error: read %zu, expected %zu: %d\n", n_read, n_write,
15961605
ferror(file));
15971606
free(buff);
1607+
fclose(file);
15981608
return WC_XMSS_RC_WRITE_FAIL;
15991609
}
16001610

@@ -1604,6 +1614,7 @@ enum wc_XmssRc wolfCLU_XmssKey_WriteCb(const byte * priv,
16041614

16051615
if (n_cmp != 0) {
16061616
fprintf(stderr, "error: write data was corrupted: %d\n", n_cmp);
1617+
fclose(file);
16071618
return WC_XMSS_RC_WRITE_FAIL;
16081619
}
16091620

@@ -1641,6 +1652,7 @@ enum wc_XmssRc wolfCLU_XmssKey_ReadCb(byte * priv,
16411652
if (n_read != privSz) {
16421653
fprintf(stderr, "error: read %zu, expected %d: %d\n", n_read, privSz,
16431654
ferror(file));
1655+
fclose(file);
16441656
return WC_XMSS_RC_READ_FAIL;
16451657
}
16461658

src/genkey/clu_genkey_setup.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ int wolfCLU_genKeySetup(int argc, char** argv)
4747

4848
XMEMSET(keyOutFName, 0, MAX_FILENAME_SZ);
4949

50+
if (argc < 3) {
51+
wolfCLU_LogError("ERROR: missing key type argument");
52+
wolfCLU_genKeyHelp();
53+
return USER_INPUT_ERROR;
54+
}
5055
keyType = argv[2];
5156

5257
ret = wc_InitRng(&rng);

src/pkcs/clu_pkcs8.c

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int wolfCLU_PKCS8(int argc, char** argv)
8787
char password[MAX_PASSWORD_SIZE];
8888
int passwordSz = MAX_PASSWORD_SIZE;
8989
byte* pass = NULL;
90+
byte* stdinKeyBuf = NULL;
9091

9192
opterr = 0; /* do not display unrecognized options */
9293
optind = 0; /* start at indent 0 */
@@ -155,35 +156,44 @@ int wolfCLU_PKCS8(int argc, char** argv)
155156

156157
/* currently only supporting PKCS8 parsing, input is expected */
157158
if (ret == WOLFCLU_SUCCESS && bioIn == NULL) {
158-
byte keyBuffer[MAX_STDINSZ];
159159
word32 keyLen = 0;
160160

161-
XMEMSET(keyBuffer, 0, MAX_STDINSZ);
162-
keyLen = (int)XFREAD(keyBuffer, 1, sizeof(keyBuffer) - 1, stdin);
163-
if (keyLen <= 0) {
164-
WOLFCLU_LOG(WOLFCLU_E0, "Error reading private key from stdin");
165-
ret = WOLFCLU_FATAL_ERROR;
161+
/* Heap-allocate so the buffer outlives this block; the BIO created
162+
* below stores a pointer to it and is freed at function exit. */
163+
stdinKeyBuf = (byte*)XMALLOC(MAX_STDINSZ, HEAP_HINT,
164+
DYNAMIC_TYPE_TMP_BUFFER);
165+
if (stdinKeyBuf == NULL) {
166+
ret = MEMORY_E;
166167
}
167168
else {
168-
/* Null-terminate the key buffer */
169-
keyBuffer[keyLen] = '\0';
169+
XMEMSET(stdinKeyBuf, 0, MAX_STDINSZ);
170+
keyLen = (int)XFREAD(stdinKeyBuf, 1, MAX_STDINSZ - 1, stdin);
171+
if (keyLen <= 0) {
172+
WOLFCLU_LOG(WOLFCLU_E0,
173+
"Error reading private key from stdin");
174+
ret = WOLFCLU_FATAL_ERROR;
175+
}
176+
else {
177+
/* Null-terminate the key buffer */
178+
stdinKeyBuf[keyLen] = '\0';
170179

171-
bioIn = wolfSSL_BIO_new_mem_buf(keyBuffer, keyLen);
180+
bioIn = wolfSSL_BIO_new_mem_buf(stdinKeyBuf, keyLen);
172181

173-
if (bioIn == NULL) {
174-
wolfCLU_LogError("Unable to open pkcs8 file %s",
175-
optarg);
176-
ret = MEMORY_E;
177-
}
178-
else if (pass == NULL) {
179-
/* Reopen terminal since we might get password data
180-
* from stdin later */
181-
#ifdef USE_WINDOWS_API
182-
if (freopen("CON", "r", stdin) == NULL) {
183-
#else
184-
if (freopen("/dev/tty", "r", stdin) == NULL) {
185-
#endif
186-
ret = WOLFCLU_FATAL_ERROR;
182+
if (bioIn == NULL) {
183+
wolfCLU_LogError("Unable to open pkcs8 file %s",
184+
optarg);
185+
ret = MEMORY_E;
186+
}
187+
else if (pass == NULL) {
188+
/* Reopen terminal since we might get password data
189+
* from stdin later */
190+
#ifdef USE_WINDOWS_API
191+
if (freopen("CON", "r", stdin) == NULL) {
192+
#else
193+
if (freopen("/dev/tty", "r", stdin) == NULL) {
194+
#endif
195+
ret = WOLFCLU_FATAL_ERROR;
196+
}
187197
}
188198
}
189199
}
@@ -282,6 +292,10 @@ int wolfCLU_PKCS8(int argc, char** argv)
282292
wolfSSL_BIO_free(bioIn);
283293
wolfSSL_BIO_free(bioOut);
284294
wolfSSL_EVP_PKEY_free(pkey);
295+
if (stdinKeyBuf != NULL) {
296+
wolfCLU_ForceZero(stdinKeyBuf, MAX_STDINSZ);
297+
XFREE(stdinKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
298+
}
285299

286300
return ret;
287301
#else

src/x509/clu_request_setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static int _wolfSSL_X509_extensions_print(WOLFSSL_BIO* bio, WOLFSSL_X509* x509,
224224
XSTRLCAT(scratch, buf, MAX_WIDTH);
225225

226226
int crit = wolfSSL_X509_EXTENSION_get_critical(ext) ? 1 : 0;
227-
XSTRLCAT(scratch, crit ? ": Critical\n" : ":\n", crit ? 11 : 2);
227+
XSTRLCAT(scratch, crit ? ": Critical\n" : ":\n", MAX_WIDTH);
228228
(void)crit;
229229

230230
wolfSSL_BIO_write(bio, scratch, (int)XSTRLEN(scratch));

0 commit comments

Comments
 (0)