Skip to content

Commit afa1d89

Browse files
committed
addressed copilot comments
1 parent 11176f8 commit afa1d89

2 files changed

Lines changed: 85 additions & 11 deletions

File tree

src/crypto/clu_crypto_setup.c

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,25 @@ int wolfCLU_setup(int argc, char** argv, char action)
346346
while (ret == 0) {
347347
WOLFCLU_LOG(WOLFCLU_L0,
348348
"-in flag was not set, please enter a string or"
349-
"file name to be encrypted: ");
350-
ret = (fgets(inName, sizeof(inName), stdin) != NULL) ? 1 : 0;
351-
if (ret > 0) {
349+
" file name to be encrypted: ");
350+
if (fgets(inName, sizeof(inName), stdin) == NULL) {
351+
/* Failed to read input, continue */
352+
continue;
353+
}
354+
/* If no newline is present, the line was too long. */
355+
if (strchr(inName, '\n') == NULL) {
356+
int ch;
357+
do {
358+
ch = getchar();
359+
} while (ch != '\n' && ch != EOF);
360+
} else {
352361
inName[strcspn(inName, "\n")] = '\0';
353362
}
363+
/* Do not accept an empty string as valid input */
364+
if (inName[0] == '\0') {
365+
continue;
366+
}
367+
ret = 1;
354368
}
355369
in = inName;
356370
WOLFCLU_LOG(WOLFCLU_L0, "Encrypting :\"%s\"", inName);
@@ -400,12 +414,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
400414
while (ret == 0) {
401415
WOLFCLU_LOG(WOLFCLU_L0,
402416
"Please enter a name for the output file: ");
403-
ret = (fgets(outNameEnc, sizeof(outNameEnc), stdin) != NULL)
404-
? 1 : 0;
405-
if (ret > 0) {
417+
if (fgets(outNameEnc, sizeof(outNameEnc), stdin) == NULL) {
418+
continue;
419+
}
420+
if (strchr(outNameEnc, '\n') == NULL) {
421+
int ch;
422+
do {
423+
ch = getchar();
424+
} while (ch != '\n' && ch != EOF);
425+
} else {
406426
outNameEnc[strcspn(outNameEnc, "\n")] = '\0';
407427
}
408-
out = (ret > 0) ? outNameEnc : '\0';
428+
if (outNameEnc[0] == '\0') {
429+
continue;
430+
}
431+
out = outNameEnc;
432+
ret = 1;
409433
}
410434
}
411435
ret = wolfCLU_encrypt(alg, mode, pwdKey, key, keySize, in, out,
@@ -426,12 +450,22 @@ int wolfCLU_setup(int argc, char** argv, char action)
426450
while (ret == 0) {
427451
WOLFCLU_LOG(WOLFCLU_L0,
428452
"Please enter a name for the output file: ");
429-
ret = (fgets(outNameDec, sizeof(outNameDec), stdin) != NULL)
430-
? 1 : 0;
431-
if (ret > 0) {
453+
if (fgets(outNameDec, sizeof(outNameDec), stdin) == NULL) {
454+
continue;
455+
}
456+
if (strchr(outNameDec, '\n') == NULL) {
457+
int ch;
458+
do {
459+
ch = getchar();
460+
} while (ch != '\n' && ch != EOF);
461+
} else {
432462
outNameDec[strcspn(outNameDec, "\n")] = '\0';
433463
}
434-
out = (ret > 0) ? outNameDec : '\0';
464+
if (outNameDec[0] == '\0') {
465+
continue;
466+
}
467+
out = outNameDec;
468+
ret = 1;
435469
}
436470
}
437471
ret = wolfCLU_decrypt(alg, mode, pwdKey, key, keySize, in, out,

tests/encrypt/enc-test.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,45 @@ if [ $? -eq 0 ]; then
227227
rm -f test-cam-probe.enc test-cam-stdin.enc test-cam-stdin.dec
228228
fi
229229

230+
# Test: inName empty line is rejected, re-prompt accepts valid filename
231+
printf "\ncerts/crl.der\n" | ./wolfssl enc -aes-128-cbc -out test-empty-in.enc -k "testpass" > /dev/null 2>&1
232+
if [ $? != 0 ]; then
233+
echo "Failed: enc should accept filename after empty line on stdin (-in path)"
234+
exit 99
235+
fi
236+
./wolfssl enc -d -aes-128-cbc -in test-empty-in.enc -out test-empty-in.dec -k "testpass" > /dev/null 2>&1
237+
diff certs/crl.der test-empty-in.dec > /dev/null 2>&1
238+
if [ $? != 0 ]; then
239+
echo "Failed: enc/dec roundtrip mismatch after empty-line re-prompt (-in path)"
240+
exit 99
241+
fi
242+
rm -f test-empty-in.enc test-empty-in.dec
243+
244+
# Test: outNameEnc/outNameDec empty line is rejected (non-EVP path, Camellia)
245+
./wolfssl enc -camellia-128-cbc -in certs/crl.der -out test-cam-probe2.enc -k "testpass" > /dev/null 2>&1
246+
if [ $? -eq 0 ]; then
247+
rm -f test-cam-probe2.enc
248+
249+
# outNameEnc: empty line rejected, then valid output name accepted
250+
printf "\ntest-cam-empty.enc\n" | ./wolfssl enc -camellia-128-cbc -in certs/crl.der -k "testpass" > /dev/null 2>&1
251+
if [ $? != 0 ]; then
252+
echo "Failed: Camellia enc should accept output name after empty line (outNameEnc)"
253+
exit 99
254+
fi
255+
256+
# outNameDec: empty line rejected, then valid output name accepted
257+
printf "\ntest-cam-empty.dec\n" | ./wolfssl enc -d -camellia-128-cbc -in test-cam-empty.enc -k "testpass" > /dev/null 2>&1
258+
if [ $? != 0 ]; then
259+
echo "Failed: Camellia dec should accept output name after empty line (outNameDec)"
260+
exit 99
261+
fi
262+
diff certs/crl.der test-cam-empty.dec > /dev/null 2>&1
263+
if [ $? != 0 ]; then
264+
echo "Failed: enc/dec roundtrip mismatch after empty-line re-prompt (outNameEnc/Dec)"
265+
exit 99
266+
fi
267+
rm -f test-cam-empty.enc test-cam-empty.dec
268+
fi
269+
230270
echo "Done"
231271
exit 0

0 commit comments

Comments
 (0)