Skip to content

Commit f6416fa

Browse files
committed
Fix copilot, fenrir, internal review
- Copilot: BN_bn2hex NULL guard — Added NULL check on num before calling wolfSSL_BN_bn2hex - Copilot: return 0 on missing args — Changed return ret to return USER_INPUT_ERROR at lines 118 and 194 - Copilot: SHA-224 test assertion — Test now fails if sha224 is NOT found (not just if sha256 is) - Copilot: dilithium_init return value — Capture into ret for proper error logging - Security review: Missing ForceZero on keyBuf — Added ForceZero before XFREE on all keyBuf free paths in both certgen files
1 parent eb1ce8b commit f6416fa

6 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/certgen/clu_certgen_ed25519.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ int make_self_signed_ed25519_certificate(char* keyPath, char* certOut)
6262
}
6363
if (XFSEEK(keyFile, 0, SEEK_SET) != 0 || (int)XFREAD(keyBuf, 1, keyFileSz, keyFile) != keyFileSz) {
6464
XFCLOSE(keyFile);
65+
wolfCLU_ForceZero(keyBuf, keyFileSz);
6566
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6667
return WOLFCLU_FAILURE;
6768
}
@@ -85,6 +86,7 @@ int make_self_signed_ed25519_certificate(char* keyPath, char* certOut)
8586
ED25519_KEY_SIZE,
8687
keyBuf + ED25519_KEY_SIZE,
8788
ED25519_KEY_SIZE, &key);
89+
wolfCLU_ForceZero(keyBuf, keyFileSz);
8890
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
8991
if (ret != 0 ) {
9092
wolfCLU_LogError("Failed to decode private key.\nRET: %d", ret);

src/certgen/clu_certgen_rsa.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int make_self_signed_rsa_certificate(char* keyPath, char* certOut, int oid)
6363
}
6464
if (XFSEEK(keyFile, 0, SEEK_SET) != 0 || (int)XFREAD(keyBuf, 1, keyFileSz, keyFile) != keyFileSz) {
6565
XFCLOSE(keyFile);
66+
wolfCLU_ForceZero(keyBuf, keyFileSz);
6667
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
6768
return WOLFCLU_FAILURE;
6869
}
@@ -86,6 +87,7 @@ int make_self_signed_rsa_certificate(char* keyPath, char* certOut, int oid)
8687
rngInit = 1;
8788

8889
ret = wc_RsaPrivateKeyDecode(keyBuf, &index, &key, keyFileSz);
90+
wolfCLU_ForceZero(keyBuf, keyFileSz);
8991
XFREE(keyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
9092
if (ret != 0 ) {
9193
wolfCLU_LogError("Failed to decode private key.\nRET: %d", ret);

src/sign-verify/clu_sign.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ int wolfCLU_sign_data_dilithium (byte* data, char* out, word32 dataSz, char* pri
612612
XMEMSET(key, 0, sizeof(dilithium_key));
613613

614614
/* init the dilithium key */
615-
if (wc_dilithium_init(key) != 0) {
615+
ret = wc_dilithium_init(key);
616+
if (ret != 0) {
616617
wolfCLU_LogError("Failed to initialize Dilithium Key.\nRET: %d", ret);
617618
#ifdef WOLFSSL_SMALL_STACK
618619
XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);

src/sign-verify/clu_sign_verify_setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int wolfCLU_sign_verify_setup(int argc, char** argv)
115115
"signing or verifying.");
116116
wolfCLU_signHelp(algCheck);
117117
wolfCLU_verifyHelp(algCheck);
118-
return ret;
118+
return USER_INPUT_ERROR;
119119
}
120120

121121
ret = wolfCLU_checkForArg("-inform", 7, argc, argv);
@@ -191,7 +191,7 @@ int wolfCLU_sign_verify_setup(int argc, char** argv)
191191
XFREE(in, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
192192
if (sig)
193193
XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
194-
return ret;
194+
return USER_INPUT_ERROR;
195195
}
196196

197197
ret = wolfCLU_checkForArg("-out", 4, argc, argv);

src/x509/clu_cert_setup.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,12 @@ int wolfCLU_certSetup(int argc, char** argv)
759759
if (rsa != NULL) {
760760
wolfSSL_RSA_get0_key(rsa, &num, NULL, NULL);
761761
}
762-
hex = wolfSSL_BN_bn2hex(num);
762+
if (num == NULL) {
763+
wolfCLU_LogError("Modulus=unavailable");
764+
ret = WOLFCLU_FATAL_ERROR;
765+
}
766+
hex = (num != NULL) ?
767+
wolfSSL_BN_bn2hex(num) : NULL;
763768

764769
if (hex != NULL) {
765770
if (wolfSSL_BIO_write(out, "Modulus=", (int)XSTRLEN("Modulus="))

tests/x509/x509-req-test.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,12 @@ rm -f tmp.cert
181181
run_success "x509 -req -in tmp.csr -days 3650 -sha1 -signkey ./certs/server-key.pem -out tmp.cert"
182182
rm -f tmp.cert
183183
run_success "x509 -req -in tmp.csr -days 3650 -sha224 -signkey ./certs/server-key.pem -out tmp.cert"
184-
# Verify SHA-224 cert uses sha224 signature algorithm, not sha256
184+
# Verify SHA-224 cert uses sha224 signature algorithm
185185
SIGALG=`./wolfssl x509 -in tmp.cert -text -noout 2>&1`
186186
echo "$SIGALG" | grep -i "sha224"
187187
if [ $? -ne 0 ]; then
188-
echo "$SIGALG" | grep -i "sha256"
189-
if [ $? -eq 0 ]; then
190-
echo "SHA-224 cert incorrectly uses SHA-256 signature algorithm"
191-
exit 99
192-
fi
188+
echo "SHA-224 cert does not report SHA-224 signature algorithm"
189+
exit 99
193190
fi
194191
rm -f tmp.cert
195192
run_success "x509 -req -in tmp.csr -days 3650 -sha256 -signkey ./certs/server-key.pem -out tmp.cert"

0 commit comments

Comments
 (0)