Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ wolfssl
test-suite.log
tests/*/*.log
tests/*/*.trs
# check_PROGRAMS binaries left in the tree by an in-tree "make check"
tests/tools/tools_unit_test
tests/tools/tools_unit_test.exe
ecckey
src/config.h
src/config.h.in
Expand Down
16 changes: 12 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ endif

include src/include.am
include wolfclu/include.am
include tests/tools/include.am
if HAVE_PYTHON
include tests/dh/include.am
include tests/dsa/include.am
Expand Down Expand Up @@ -117,10 +118,11 @@ TESTS += $(check_PROGRAMS)
check_SCRIPTS+= $(dist_noinst_SCRIPTS)
TESTS += $(check_SCRIPTS)

# Automake's test driver writes .log/.trs files next to each test script.
# When tests live in the source tree (no VPATH), those files land in tests/,
# where EXTRA_DIST+=tests would otherwise sweep them into the tarball and
# break `make distcheck` via stale VPATH lookups.
# Automake's test driver writes .log/.trs files next to each test script, and
# an in-tree build leaves the compiled check_PROGRAMS binaries and their .o
# files there too. When tests live in the source tree (no VPATH), all of that
# lands in tests/, where EXTRA_DIST+=tests would otherwise sweep it into the
# tarball and break `make distcheck` via stale VPATH lookups.
# Generate the compressed manpages into the tarball from their .1 sources,
# so the .gz copies are never hand-maintained in git. These ship in the release
# tarball for downstream packaging; they are intentionally not installed
Expand All @@ -130,6 +132,12 @@ TESTS += $(check_SCRIPTS)
dist-hook:
find $(distdir)/tests -name '*.log' -delete
find $(distdir)/tests -name '*.trs' -delete
find $(distdir)/tests -name '*.o' -delete
find $(distdir)/tests -name '.dirstamp' -delete
find $(distdir)/tests \( -name '.deps' -o -name '.libs' \) -type d -prune -exec rm -rf {} +
for p in $(check_PROGRAMS); do \
rm -f "$(distdir)/$$p"; \
done
# Always strip stale .1.gz from the tarball (local manpages-gz output or a
# prior dist may have left them in manpages/). Regenerate only when enabled.
chmod u+w $(distdir)/manpages 2>/dev/null || true
Expand Down
9 changes: 7 additions & 2 deletions src/crypto/clu_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,

XMEMSET(&rng, 0, sizeof(rng));

/* Opening the output truncates it, destroying the ciphertext mid-read. */
if (wolfCLU_PathsRefEqual(in, out)) {
wolfCLU_LogError("-in and -out name the same file %s", in);
return DECRYPT_ERROR;
}

/* opens input file */
inFile = XFOPEN(in, "rb");
if (inFile == NULL) {
Expand All @@ -68,8 +74,7 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
}
/* opens output file */

if ((outFile = XFOPEN(out, "wb")) == NULL) {
wolfCLU_LogError("Error creating output file.");
if ((outFile = wolfCLU_OpenOutFile(out)) == NULL) {
XFCLOSE(inFile);
return DECRYPT_ERROR;
}
Expand Down
38 changes: 22 additions & 16 deletions src/crypto/clu_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
char* userInputBuffer = NULL; /* buffer when input is not a file */


/* Checked before the branch below, which treats a non-existent -in as a
* literal string and writes it out to that same path: opening the output
* truncates it, destroying the plaintext mid-read. */
if (wolfCLU_PathsRefEqual(in, out)) {
wolfCLU_LogError("-in and -out name the same file %s", in);
return WOLFCLU_FATAL_ERROR;
}

if (access (in, F_OK) == -1) {
WOLFCLU_LOG(WOLFCLU_L0, "file did not exist, encrypting string following \"-i\""
"instead.");
Expand All @@ -75,9 +83,8 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
XMEMCPY(userInputBuffer, in, inputLength);

/* open the file to write */
tempInFile = XFOPEN(in, "wb");
tempInFile = wolfCLU_OpenOutFile(in);
if (tempInFile == NULL) {
wolfCLU_LogError("unable to open file %s", in);
XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
return BAD_FUNC_ARG;
}
Expand Down Expand Up @@ -146,25 +153,25 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
}

/* open the outFile in write mode */
outFile = XFOPEN(out, "wb");
outFile = wolfCLU_OpenOutFile(out);
if (outFile == NULL) {
wolfCLU_LogError("unable to open output file %s", out);
XFCLOSE(inFile);
return WOLFCLU_FATAL_ERROR;
}
XFWRITE(salt, 1, SALT_SIZE, outFile);
XFWRITE(iv, 1, block, outFile);
XFCLOSE(outFile);

/* MALLOC 1kB buffers */
input = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (input == NULL) {
XFCLOSE(inFile);
XFCLOSE(outFile);
return MEMORY_E;
}
output = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
if (output == NULL) {
XFCLOSE(inFile);
XFCLOSE(outFile);
wolfCLU_freeBins(input, NULL, NULL, NULL, NULL);
return MEMORY_E;
}
Expand Down Expand Up @@ -196,7 +203,12 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
if (hexRet != WOLFCLU_SUCCESS) {
wolfCLU_LogError("failed during conversion of input,"
" ret = %d", hexRet);
/* wolfCLU_hexToBin() already freed and NULLed its
* own allocation, so this is really here to free
* 'output' on the way out. */
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
XFCLOSE(inFile);
XFCLOSE(outFile);
return hexRet;
}
}/* end hex or ascii */
Expand All @@ -211,6 +223,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
else { /* otherwise we got a file read error */
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
XFCLOSE(inFile);
XFCLOSE(outFile);
return FREAD_ERROR;
}/* End feof check */
}/* End fread check */
Expand All @@ -221,6 +234,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
ret = wc_CamelliaSetKey(&camellia, key, size / 8, iv);
if (ret != 0) {
XFCLOSE(inFile);
XFCLOSE(outFile);
wolfCLU_LogError("CamelliaSetKey failed.");
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
return ret;
Expand All @@ -230,6 +244,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
}
else {
XFCLOSE(inFile);
XFCLOSE(outFile);
wolfCLU_LogError("Incompatible mode while using Camellia.");
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
return FATAL_ERROR;
Expand All @@ -253,15 +268,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
WOLFCLU_LOG(WOLFCLU_L0, " ]\n");
} /* end visual confirmation */

/* Open the outFile in append mode */
outFile = XFOPEN(out, "ab");
if (outFile == NULL) {
XFCLOSE(inFile);
wolfCLU_LogError("failed to open file.");
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
return FWRITE_ERROR;
}

/* write this chunk to the already-open outFile */
ret = (int)XFWRITE(output, 1, tempMax, outFile);

if (ferror(outFile)) {
Expand All @@ -278,15 +285,14 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
wolfCLU_freeBins(input, output, NULL, NULL, NULL);
return FWRITE_ERROR;
}
/* close the outFile */
XFCLOSE(outFile);

length -= tempMax;
if (length < 0)
WOLFCLU_LOG(WOLFCLU_L0, "length went past zero.");
}

/* closes the opened files and frees the memory */
XFCLOSE(outFile);
XFCLOSE(inFile);
XMEMSET(key, 0, size);
XMEMSET(iv, 0 , block);
Expand Down
6 changes: 6 additions & 0 deletions src/crypto/clu_evp_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ int wolfCLU_evp_crypto(const WOLFSSL_EVP_CIPHER* cphr, char* mode, byte* pwdKey,
return BAD_FUNC_ARG;
}

/* Opening the output truncates it, destroying the input mid-read. */
if (wolfCLU_PathsRefEqual(fileIn, fileOut)) {
wolfCLU_LogError("-in and -out name the same file %s", fileIn);
return WOLFCLU_FATAL_ERROR;
}

/* Start up the random number generator */
if (wc_InitRng(&rng) != 0) {
wolfCLU_LogError("Random Number Generator failed to start.");
Expand Down
Loading
Loading