Compare commits

...

20 Commits

Author SHA1 Message Date
Martchus e3e7f731bf Apply change of `global.h` template 2024-04-02 11:12:34 +02:00
Martchus acdc1d80ba Allow compilation of tests when doing unity builds
Ensure all formatting overloads are always included before CppUnit's
headers
2024-02-04 20:58:45 +01:00
Martchus 3cb0812c62 Bump patch version 2024-02-04 20:42:27 +01:00
Martchus 17f6d4cfe5 Update `global.h` via updated template in c++utilities 2024-02-04 20:41:25 +01:00
Martchus fd5c19dd9f Update copyright date 2024-02-04 20:38:44 +01:00
Martchus 2e1d9a700b Apply clang-format 2023-11-21 22:10:44 +01:00
Martchus 9f134725c7 Fix handling of non-ASCII characters when resizing file 2023-11-07 23:33:46 +01:00
Martchus 3da25a39a4 Improve documentation of save/write
* Mention std::filesystem::filesystem_error explicitly
* Mention std::runtime_error last as it is the most generic exception type
* Use fully qualified class names
2023-11-07 23:00:33 +01:00
Martchus 7d8d837cb1 Increment patch version 2023-11-07 22:57:28 +01:00
Martchus 1f42f5fffb Truncate file if it became smaller in all cases 2023-11-07 16:18:41 +01:00
Martchus 962054a3fe Increment patch version 2023-11-07 16:17:42 +01:00
Martchus 282c819ea2 Avoid CMake deprecation warning by bumping version 2023-07-23 21:06:28 +02:00
Martchus 92d8324cdc Improve condition for use of `pubsetbuf`
Using this function like this seems only be possible with `libstdc++`. The
standard lib of MSVC does not support it as well. So use it only with
`libstdc++`.
2023-02-28 21:01:23 +01:00
Martchus 7bb9134fd3 Avoid unqualified calls to `std::move` 2023-02-18 19:03:29 +01:00
Martchus 9f8deafd5a Update copyright notice 2023-01-17 18:36:48 +01:00
Martchus 2e2c3ba4fc Avoid using deprecated OpenSSL functions 2022-11-03 22:29:08 +01:00
Martchus facfc879b5 Add stalebot config 2022-04-12 01:08:06 +02:00
Martchus 52a3ac32f7 Add copyright notice 2022-04-05 20:18:35 +02:00
Martchus a725550d17 Increment patch version 2022-03-15 21:40:17 +01:00
Martchus a367edc81f Clarify that license is "GPL-2-or-later" 2022-03-15 21:39:33 +01:00
12 changed files with 81 additions and 38 deletions

19
.github/stale.yml vendored Normal file
View File

@ -0,0 +1,19 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- pinned
- security
- feature request
- enhancement
# Label to use when marking an issue as stale
staleLabel: stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR)
# set meta data
project(passwordfile)
@ -10,7 +10,7 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
set(META_APP_DESCRIPTION "C++ library to read/write passwords from/to encrypted files")
set(META_VERSION_MAJOR 5)
set(META_VERSION_MINOR 0)
set(META_VERSION_PATCH 7)
set(META_VERSION_PATCH 11)
set(META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION ON)
# add project files
@ -46,13 +46,14 @@ endif ()
set(CONFIGURATION_PACKAGE_SUFFIX
""
CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities")
find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.0.0 REQUIRED)
find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.14.0 REQUIRED)
use_cpp_utilities(VISIBILITY PUBLIC)
# find 3rd party libraries
include(3rdParty)
use_zlib()
use_crypto()
use_standard_filesystem()
# include modules to apply configuration
include(BasicConfig)

View File

@ -7,3 +7,8 @@ applying the encryption.
## Build instructions
The passwordfile library depends on c++utilities and is built in the same way.
It also depends on OpenSSL and zlib.
## Copyright notice and license
Copyright © 2015-2024 Marius Kittler
All code is licensed under [GPL-2-or-later](LICENSE).

View File

@ -4,6 +4,7 @@
#ifndef PASSWORD_FILE_GLOBAL
#define PASSWORD_FILE_GLOBAL
#include "passwordfile-definitions.h"
#include <c++utilities/application/global.h>
#ifdef PASSWORD_FILE_STATIC

View File

@ -8,6 +8,7 @@
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/path.h>
#include <openssl/conf.h>
#include <openssl/err.h>
@ -17,6 +18,7 @@
#include <zlib.h>
#include <cstring>
#include <filesystem>
#include <functional>
#include <limits>
#include <memory>
@ -89,12 +91,12 @@ PasswordFile::PasswordFile(const PasswordFile &other)
* \brief Moves the password file.
*/
PasswordFile::PasswordFile(PasswordFile &&other)
: m_path(move(other.m_path))
, m_password(move(other.m_password))
, m_rootEntry(move(other.m_rootEntry))
, m_extendedHeader(move(other.m_extendedHeader))
, m_encryptedExtendedHeader(move(other.m_encryptedExtendedHeader))
, m_file(move(other.m_file))
: m_path(std::move(other.m_path))
, m_password(std::move(other.m_password))
, m_rootEntry(std::move(other.m_rootEntry))
, m_extendedHeader(std::move(other.m_extendedHeader))
, m_encryptedExtendedHeader(std::move(other.m_encryptedExtendedHeader))
, m_file(std::move(other.m_file))
, m_freader(BinaryReader(&m_file))
, m_fwriter(BinaryWriter(&m_file))
, m_version(other.m_version)
@ -294,7 +296,7 @@ void PasswordFile::load()
msg += ERR_error_string(errorCode, nullptr);
errorCode = ERR_get_error();
}
throw CryptoException(move(msg));
throw CryptoException(std::move(msg));
}
if (ctx) {
@ -349,10 +351,10 @@ void PasswordFile::load()
stringstream decryptedStream(stringstream::in | stringstream::out | stringstream::binary);
decryptedStream.exceptions(ios_base::failbit | ios_base::badbit);
try {
#ifdef _LIBCPP_VERSION
decryptedStream.write(decryptedData.data(), static_cast<streamsize>(remainingSize));
#else
#if defined(__GLIBCXX__) && !defined(_LIBCPP_VERSION)
decryptedStream.rdbuf()->pubsetbuf(decryptedData.data(), static_cast<streamsize>(remainingSize));
#else
decryptedStream.write(decryptedData.data(), static_cast<streamsize>(remainingSize));
#endif
if (m_version >= 0x5u) {
BinaryReader reader(&decryptedStream);
@ -389,9 +391,10 @@ std::uint32_t PasswordFile::mininumVersion(PasswordFileSaveFlags options) const
/*!
* \brief Writes the current root entry to the file under path() replacing its previous contents.
* \param options Specify the features (like encryption and compression) to be used.
* \throws Throws ios_base::failure when an IO error occurs.
* \throws Throws runtime_error when no root entry is present or a compression error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws std::filesystem::filesystem_error when a filesystem error occurs.
* \throws Throws Io::CryptoException when an encryption error occurs.
* \throws Throws std::runtime_error when no root entry is present or a compression error occurs.
*/
void PasswordFile::save(PasswordFileSaveFlags options)
{
@ -400,7 +403,9 @@ void PasswordFile::save(PasswordFileSaveFlags options)
}
// use already opened and writable file; otherwise re-open the file
auto fileSize = std::size_t();
if (m_file.good() && m_file.is_open() && !(m_openOptions & PasswordFileOpenFlags::ReadOnly)) {
fileSize = size();
m_file.seekp(0);
} else {
m_file.clear();
@ -418,16 +423,24 @@ void PasswordFile::save(PasswordFileSaveFlags options)
}
}
// write entries
write(options);
m_file.flush();
// truncate file if it became smaller
const auto newSize = static_cast<std::size_t>(m_file.tellp());
if (fileSize && newSize < fileSize) {
m_file.close();
std::filesystem::resize_file(makeNativePath(m_path), newSize);
}
}
/*!
* \brief Writes the current root entry to the file which is assumed to be opened and writeable.
* \param options Specify the features (like encryption and compression) to be used.
* \throws Throws ios_base::failure when an IO error occurs.
* \throws Throws runtime_error when no root entry is present or a compression error occurs.
* \throws Throws std::ios_base::failure when an IO error occurs.
* \throws Throws Io::CryptoException when an encryption error occurs.
* \throws Throws std::runtime_error when no root entry is present, a compression error occurs.
*/
void PasswordFile::write(PasswordFileSaveFlags options)
{
@ -550,7 +563,7 @@ void PasswordFile::write(PasswordFileSaveFlags options)
msg += ERR_error_string(errorCode, nullptr);
errorCode = ERR_get_error();
}
throw CryptoException(move(msg));
throw CryptoException(std::move(msg));
}
if (ctx) {

View File

@ -2,9 +2,6 @@
#include "./utils.h"
#include <c++utilities/tests/testutils.h>
using namespace CppUtilities;
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

View File

@ -3,9 +3,6 @@
#include "./utils.h"
#include <c++utilities/tests/testutils.h>
using namespace CppUtilities;
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

View File

@ -1,6 +1,6 @@
#include "../util/opensslrandomdevice.h"
#include <c++utilities/tests/testutils.h>
#include "./utils.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

View File

@ -2,7 +2,7 @@
#include "../io/entry.h"
#include "../io/passwordfile.h"
#include <c++utilities/tests/testutils.h>
#include "./utils.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
@ -208,7 +208,7 @@ void PasswordFileTests::testExtendedWriting()
CPPUNIT_ASSERT_EQUAL(""s, file.extendedHeader());
CPPUNIT_ASSERT_EQUAL(""s, file.encryptedExtendedHeader());
file.rootEntry()->setLabel("testfile2 - modified");
new AccountEntry("newAccount", file.rootEntry());
auto *const newAccount = new AccountEntry("newAccount", file.rootEntry());
file.setPassword("654321");
file.extendedHeader() = "foo";
file.encryptedExtendedHeader() = "bar";
@ -220,4 +220,18 @@ void PasswordFileTests::testExtendedWriting()
// check backup files
testReading("extended writing", testfile1 + ".backup", "123456", testfile2 + ".backup", string(), false, false);
// remove newAccount again to check what happens if the file size decreases
const auto fileSize = file.size();
delete newAccount;
file.save(PasswordFileSaveFlags::Encryption | PasswordFileSaveFlags::PasswordHashing);
file.close();
file.clearEntries();
file.open();
CPPUNIT_ASSERT_LESS(fileSize, file.size());
file.load();
auto path = std::list<std::string>{ "newAccount" };
CPPUNIT_ASSERT(file.rootEntry());
CPPUNIT_ASSERT(!file.rootEntry()->entryByPath(path));
}

View File

@ -23,4 +23,8 @@ inline std::ostream &operator<<(std::ostream &out, const Io::Field *field)
} // namespace CppUtilities
#include <c++utilities/tests/testutils.h>
using namespace CppUtilities;
#endif // PASSWORDFILE_TESTS_UTILS_H

View File

@ -49,16 +49,8 @@ void clean()
*/
Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
{
// init sha256 hashing
SHA256_CTX sha256;
SHA256_Init(&sha256);
// do the actual hashing
SHA256_Update(&sha256, buffer, size);
// finalize the hashing
Sha256Sum hash;
SHA256_Final(hash.data, &sha256);
auto hash = Sha256Sum();
SHA256(buffer, size, hash.data);
return hash;
}

View File

@ -45,7 +45,7 @@ OpenSslRandomDevice::result_type OpenSslRandomDevice::operator()() const
errorMsg += ERR_error_string(errorCode, nullptr);
errorCode = ERR_get_error();
}
throw Io::CryptoException(move(errorMsg));
throw Io::CryptoException(std::move(errorMsg));
}
/*!