keepassxc/src/core/Alloc.cpp

97 lines
2.5 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGlobal>
Replace all crypto libraries with Botan Selected the [Botan crypto library](https://github.com/randombit/botan) due to its feature list, maintainer support, availability across all deployment platforms, and ease of use. Also evaluated Crypto++ as a viable candidate, but the additional features of Botan (PKCS#11, TPM, etc) won out. The random number generator received a backend upgrade. Botan prefers hardware-based RNG's and will provide one if available. This is transparent to KeePassXC and a significant improvement over gcrypt. Replaced Argon2 library with built-in Botan implementation that supports i, d, and id. This requires Botan 2.11.0 or higher. Also simplified the parameter test across KDF's. Aligned SymmetricCipher parameters with available modes. All encrypt and decrypt operations are done in-place instead of returning new objects. This allows use of secure vectors in the future with no additional overhead. Took this opportunity to decouple KeeShare from SSH Agent. Removed leftover code from OpenSSHKey and consolidated the SSH Agent code into the same directory. Removed bcrypt and blowfish inserts since they are provided by Botan. Additionally simplified KeeShare settings interface by removing raw certificate byte data from the user interface. KeeShare will be further refactored in a future PR. NOTE: This PR breaks backwards compatibility with KeeShare certificates due to different RSA key storage with Botan. As a result, new "own" certificates will need to be generated and trust re-established. Removed YKChallengeResponseKeyCLI in favor of just using the original implementation with signal/slots. Removed TestRandom stub since it was just faking random numbers and not actually using the backend. TestRandomGenerator now uses the actual RNG. Greatly simplified Secret Service plugin's use of crypto functions with Botan.
2021-04-04 08:56:00 -04:00
#include <botan/mem_ops.h>
Fix compilation on macOS when clang < 9 or Qt < 5.12 (#7117) * fix compilation on Qt not having QOperatingSystemVersion::MacOSBigSur The code uses 'QOperatingSystemVersion::MacOSBigSur' which doesn't exist in all Qt versions (it has been backported to Qt 5.12.10+ & 5.15.1+ only). On older macos systems like El Capitan the last supported version of Qt is 5.11 This will fix compilation issue on such older systems and on systems running with Qt not supporting QOperatingSystemVersion::MacOSBigSur Compilation error was: error: no member named 'MacOSBigSur' in 'QOperatingSystemVersion' * Fix compilation when osx <= 10.9 * AppKitImpl.mm: button property is new in 10.10. It is used for a feature of KeePassXC that is only available from 10.17 onwards. So we don't need it when compiling on <= 10.9 error: property 'button' not found on object of type 'NSStatusItem *' NSString* appearance = [dummy.button.effectiveAppearance.name lowercaseString]; ^ * The code uses @available syntax which is supported by AppleClang >= 9 or LLVM >= 5. We check __clang_major__ to allow compilation on older versions of macOS that don't have a recent clang. For example on El Capitan. * Fix compilation when osx <= 10.8 * AppKitImpl.mm: AXIsProcessTrustedWithOptions exists from 10.9 onwards error: use of undeclared identifier 'kAXTrustedCheckOptionPrompt' error: use of undeclared identifier 'AXIsProcessTrustedWithOptions' * Fix compilation when osx <= 10.7 * MacUtils.cpp: CoreGraphics exists from 10.8 onwards only, capslock detection feature would have to be implemented on OSX <= 10.7 * AppKitImpl.mm: CGDisplayStreamRef exists from 10.8 onwards only. It is used for a feature of KeePassXC that is only available from 10.15 onwards. So we don't need it when compiling on <= 10.7 error: unknown type name 'CGDisplayStreamRef' * AppKitImpl.mm: Syntax is not understood by 10.7, update it to be understandable by <= 10.7 error: expected method to read dictionary element not found on object of type 'NSDictionary *' NSRunningApplication* app = userInfo[NSWorkspaceApplicationKey]; ^ * The code uses @available syntax which is supported by AppleClang >= 9 or LLVM >= 5. We check __clang_major__ to allow compilation on older versions of macOS that don't have a recent clang. * Fix compilation error on OS X 10.11 src/core/Alloc.cpp:44:10: error: no type named 'free' in namespace 'std' std::free(ptr); ~~~~~^ This is a regression, since it was fixed in [1] Per [2], std::free() needs #include <cstdlib>. That file is included indirectly on newer systems. [1] https://github.com/keepassxreboot/keepassxc/commit/7c6c027d33b06eb706f93e3d178a2305d7bcfd56 [2] https://en.cppreference.com/w/cpp/memory/c/free * fix compilation when macos SDK <= 10.14 These methods are only available from macOS 10.15 - kSecAccessControlWatch - LAPolicy.deviceOwnerAuthenticationWithBiometricsOrWatch The code uses @available syntax which is supported by AppleClang >= 9 or LLVM >= 5. We check __clang_major__ to allow compilation on older versions of macOS that don't have a recent clang.
2021-11-24 22:35:38 -05:00
#include <cstdlib>
#if defined(Q_OS_MACOS)
#include <malloc/malloc.h>
#elif defined(Q_OS_FREEBSD)
#include <malloc_np.h>
#elif defined(HAVE_MALLOC_H)
#include <malloc.h>
#else
#include <cstdlib>
#endif
#if defined(NDEBUG) && !defined(__cpp_sized_deallocation)
#warning "KeePassXC is being compiled without sized deallocation support. Deletes may be slow."
#endif
/**
* Custom sized delete operator which securely zeroes out allocated
* memory before freeing it (requires C++14 sized deallocation support).
*/
void operator delete(void* ptr, std::size_t size) noexcept
{
if (!ptr) {
return;
}
Replace all crypto libraries with Botan Selected the [Botan crypto library](https://github.com/randombit/botan) due to its feature list, maintainer support, availability across all deployment platforms, and ease of use. Also evaluated Crypto++ as a viable candidate, but the additional features of Botan (PKCS#11, TPM, etc) won out. The random number generator received a backend upgrade. Botan prefers hardware-based RNG's and will provide one if available. This is transparent to KeePassXC and a significant improvement over gcrypt. Replaced Argon2 library with built-in Botan implementation that supports i, d, and id. This requires Botan 2.11.0 or higher. Also simplified the parameter test across KDF's. Aligned SymmetricCipher parameters with available modes. All encrypt and decrypt operations are done in-place instead of returning new objects. This allows use of secure vectors in the future with no additional overhead. Took this opportunity to decouple KeeShare from SSH Agent. Removed leftover code from OpenSSHKey and consolidated the SSH Agent code into the same directory. Removed bcrypt and blowfish inserts since they are provided by Botan. Additionally simplified KeeShare settings interface by removing raw certificate byte data from the user interface. KeeShare will be further refactored in a future PR. NOTE: This PR breaks backwards compatibility with KeeShare certificates due to different RSA key storage with Botan. As a result, new "own" certificates will need to be generated and trust re-established. Removed YKChallengeResponseKeyCLI in favor of just using the original implementation with signal/slots. Removed TestRandom stub since it was just faking random numbers and not actually using the backend. TestRandomGenerator now uses the actual RNG. Greatly simplified Secret Service plugin's use of crypto functions with Botan.
2021-04-04 08:56:00 -04:00
Botan::secure_scrub_memory(ptr, size);
std::free(ptr);
}
void operator delete[](void* ptr, std::size_t size) noexcept
{
::operator delete(ptr, size);
}
/**
* Custom delete operator which securely zeroes out
* allocated memory before freeing it.
*/
void operator delete(void* ptr) noexcept
{
if (!ptr) {
return;
}
#if defined(Q_OS_WIN)
::operator delete(ptr, _msize(ptr));
#elif defined(Q_OS_MACOS)
::operator delete(ptr, malloc_size(ptr));
#elif defined(HAVE_MALLOC_USABLE_SIZE)
::operator delete(ptr, malloc_usable_size(ptr));
#else
// whatever OS this is, give up and simply free stuff
std::free(ptr);
#endif
}
void operator delete[](void* ptr) noexcept
{
::operator delete(ptr);
}
// clang-format versions less than 10.0 refuse to put a space before "noexcept"
// clang-format off
/**
* Custom insecure delete operator that does not zero out memory before
* freeing a buffer. Can be used for better performance.
*/
2020-04-11 13:02:57 -04:00
void operator delete(void* ptr, bool) noexcept
{
std::free(ptr);
}
// clang-format on
void operator delete[](void* ptr, bool) noexcept
{
::operator delete(ptr, false);
}