Skip to content

Commit 402548e

Browse files
authored
Merge pull request #197 from kojo1/path_concat
Fix path concatenation in wolfCLU_CertSignAppendOut
2 parents e55f9e5 + 2a4b13d commit 402548e

2 files changed

Lines changed: 84 additions & 7 deletions

File tree

src/x509/clu_x509_sign.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,15 +1045,34 @@ int wolfCLU_CertSignAppendOut(WOLFCLU_CERT_SIGN* csign, char* out)
10451045
if (ret == WOLFCLU_SUCCESS && csign->outDir != NULL && out != NULL) {
10461046
int currentSz = (int)XSTRLEN(csign->outDir);
10471047

1048-
s = (char*)XMALLOC(outSz + currentSz + 1, HEAP_HINT,
1049-
DYNAMIC_TYPE_TMP_BUFFER);
1050-
if (s == NULL) {
1051-
ret = MEMORY_E;
1048+
/* If out is an absolute path, use it directly instead of appending */
1049+
if (out[0] == '/') {
1050+
s = (char*)XMALLOC(outSz + 1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
1051+
if (s == NULL) {
1052+
ret = MEMORY_E;
1053+
}
1054+
else {
1055+
XMEMCPY(s, out, outSz);
1056+
s[outSz] = '\0';
1057+
}
10521058
}
10531059
else {
1054-
XMEMCPY(s, csign->outDir, currentSz);
1055-
XMEMCPY(s + currentSz, out, outSz);
1056-
s[outSz + currentSz] = '\0';
1060+
/* Relative path: append with separator if needed */
1061+
int needSep = (csign->outDir[currentSz - 1] != '/') ? 1 : 0;
1062+
1063+
s = (char*)XMALLOC(outSz + currentSz + needSep + 1, HEAP_HINT,
1064+
DYNAMIC_TYPE_TMP_BUFFER);
1065+
if (s == NULL) {
1066+
ret = MEMORY_E;
1067+
}
1068+
else {
1069+
XMEMCPY(s, csign->outDir, currentSz);
1070+
if (needSep) {
1071+
s[currentSz] = '/';
1072+
}
1073+
XMEMCPY(s + currentSz + needSep, out, outSz);
1074+
s[outSz + currentSz + needSep] = '\0';
1075+
}
10571076
}
10581077
}
10591078

tests/x509/x509-ca-test.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,64 @@ if ./wolfssl ca -help 2>&1 | grep altextend &> /dev/null; then
278278
rm -f tmp-server-chimera-cert.pem
279279
fi
280280

281+
# Test path concatenation fix for -out with new_certs_dir
282+
echo "Testing -out path handling with new_certs_dir"
283+
mkdir -p outdir-test/certs
284+
cat << EOF > ca-outdir.conf
285+
[ ca ]
286+
default_ca = CA_default
287+
288+
[ CA_default ]
289+
dir = ./certs
290+
database = ./index.txt
291+
new_certs_dir = ./outdir-test/certs
292+
certificate = \$dir/ca-cert.pem
293+
private_key = \$dir/ca-key.pem
294+
rand_serial = yes
295+
default_days = 365
296+
default_md = sha256
297+
policy = policy_any
298+
299+
[ policy_any ]
300+
countryName = supplied
301+
stateOrProvinceName = optional
302+
organizationName = optional
303+
organizationalUnitName = optional
304+
commonName = supplied
305+
emailAddress = optional
306+
EOF
307+
308+
rm index.txt
309+
touch index.txt
310+
run_success "req -key ./certs/server-key.pem -subj /O=wolfSSL/C=US/ST=MT/L=Bozeman/CN=wolfSSL/OU=org-unit -out tmp-outdir.csr"
311+
312+
# Test 1: absolute -out path should override new_certs_dir
313+
ABS_OUT_PATH="$(pwd)/outdir-test/absolute-out.pem"
314+
run_success "ca -config ca-outdir.conf -in tmp-outdir.csr -out $ABS_OUT_PATH"
315+
if [ ! -f "$ABS_OUT_PATH" ]; then
316+
echo "Absolute -out path test failed: file not found at $ABS_OUT_PATH"
317+
exit 99
318+
fi
319+
if [ -f ./outdir-test/certs"$ABS_OUT_PATH" ]; then
320+
echo "Absolute -out path test failed: file incorrectly concatenated"
321+
exit 99
322+
fi
323+
echo "Absolute -out path test passed"
324+
325+
# Test 2: relative -out path should be appended to new_certs_dir
326+
rm index.txt
327+
touch index.txt
328+
run_success "ca -config ca-outdir.conf -in tmp-outdir.csr -out relative-out.pem"
329+
if [ ! -f ./outdir-test/certs/relative-out.pem ]; then
330+
echo "Relative -out path test failed: file not found at ./outdir-test/certs/relative-out.pem"
331+
exit 99
332+
fi
333+
echo "Relative -out path test passed"
334+
335+
rm -rf outdir-test
336+
rm -f ca-outdir.conf
337+
rm -f tmp-outdir.csr
338+
281339
rm -f test_ca.pem
282340
rm -f tmp.pem
283341
rm -f rand-file-test

0 commit comments

Comments
 (0)