diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 141333d1..b3b4a49e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -69,7 +69,6 @@ jobs: - name: Prepare vcpkg uses: lukka/run-vcpkg@v11 with: - vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33 vcpkgJsonGlob: ./vcpkg.json runVcpkgInstall: true env: @@ -116,7 +115,6 @@ jobs: if: matrix.target != 'macos' uses: lukka/run-vcpkg@v11 with: - vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33 vcpkgJsonGlob: ./vcpkg.json runVcpkgInstall: true env: @@ -158,7 +156,6 @@ jobs: - name: Prepare vcpkg uses: lukka/run-vcpkg@v11 with: - vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33 vcpkgJsonGlob: ./vcpkg.json runVcpkgInstall: true runVcpkgFormatString: "[`install`, `--recurse`, `--clean-after-build`, `--x-install-root`, `$[env.VCPKG_INSTALLED_DIR]`, `--triplet`, `$[env.VCPKG_DEFAULT_TRIPLET]`, `--x-feature`, `tests`]" diff --git a/CMakePresets.json b/CMakePresets.json index 0f841746..337ed240 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -28,7 +28,7 @@ }, "cacheVariables": { "CMAKE_OSX_ARCHITECTURES": "arm64", - "CMAKE_OSX_DEPLOYMENT_TARGET": "12.0", + "CMAKE_OSX_DEPLOYMENT_TARGET": "13.3", "CMAKE_FIND_ROOT_PATH": "$env{DEST};/opt/homebrew", "FRAMEWORK_DESTINATION": "$env{DEST}/lib" }, @@ -56,7 +56,8 @@ "CMAKE_SYSTEM_NAME": "iOS", "CMAKE_OSX_SYSROOT": "${presetName}", "CMAKE_OSX_DEPLOYMENT_TARGET": "15.0", - "CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES" + "CMAKE_DISABLE_FIND_PACKAGE_SWIG": "YES", + "CMAKE_CXX_FLAGS": "-D_LIBCPP_DISABLE_AVAILABILITY" } }, { @@ -140,4 +141,4 @@ "configurePreset": "androidx86_64" } ] -} \ No newline at end of file +} diff --git a/cdoc/CDoc.cpp b/cdoc/CDoc.cpp index b1e66e81..97e393b0 100644 --- a/cdoc/CDoc.cpp +++ b/cdoc/CDoc.cpp @@ -98,6 +98,8 @@ libcdoc::CDocReader::getCDocFileVersion(const std::string& path) libcdoc::CDocReader * libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network) { + if(!src) + return nullptr; int version = getCDocFileVersion(src); LOG_DBG("CDocReader::createReader: version {}", version); if (src->seek(0) != libcdoc::OK) return nullptr; @@ -106,7 +108,9 @@ libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configur reader = new CDoc1Reader(src, take_ownership); } else if (version == 2) { reader = new CDoc2Reader(src, take_ownership); - } else { + } else { + if(take_ownership) + delete src; return nullptr; } reader->conf = conf; @@ -118,12 +122,18 @@ libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configur libcdoc::CDocReader * libcdoc::CDocReader::createReader(const std::string& path, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network) { - int version = getCDocFileVersion(path); + if(path.empty()) + return nullptr; + auto isrc = make_unique(path); + int version = getCDocFileVersion(isrc.get()); + LOG_DBG("CDocReader::createReader: version {}", version); + if (isrc->seek(0) != libcdoc::OK) + return nullptr; CDocReader *reader; if (version == 1) { - reader = new CDoc1Reader(path); + reader = new CDoc1Reader(isrc.release(), true); } else if (version == 2) { - reader = new CDoc2Reader(path); + reader = new CDoc2Reader(isrc.release(), true); } else { return nullptr; } @@ -138,6 +148,7 @@ libcdoc::CDocReader::createReader(std::istream& ifs, Configuration *conf, Crypto { libcdoc::IStreamSource *isrc = new libcdoc::IStreamSource(&ifs, false); int version = getCDocFileVersion(isrc); + LOG_DBG("CDocReader::createReader: version {}", version); CDocReader *reader; if (version == 1) { reader = new CDoc1Reader(isrc, true); diff --git a/cdoc/CDoc1Reader.cpp b/cdoc/CDoc1Reader.cpp index d02f4fdf..ddcb6f7a 100644 --- a/cdoc/CDoc1Reader.cpp +++ b/cdoc/CDoc1Reader.cpp @@ -340,11 +340,6 @@ CDoc1Reader::~CDoc1Reader() delete d; } -CDoc1Reader::CDoc1Reader(const std::string &path) - : CDoc1Reader(new libcdoc::IStreamSource(path), true) -{ -} - bool CDoc1Reader::isCDoc1File(libcdoc::DataSource *src) { diff --git a/cdoc/CDoc1Reader.h b/cdoc/CDoc1Reader.h index ec51cb1e..6f7b2f95 100644 --- a/cdoc/CDoc1Reader.h +++ b/cdoc/CDoc1Reader.h @@ -26,7 +26,6 @@ class CDoc1Reader : public libcdoc::CDocReader { public: CDoc1Reader(libcdoc::DataSource *src, bool take_ownership = false); - CDoc1Reader(const std::string& path); ~CDoc1Reader(); const std::vector& getLocks() override final; diff --git a/cdoc/CDoc2Reader.cpp b/cdoc/CDoc2Reader.cpp index e6612185..80ad895e 100644 --- a/cdoc/CDoc2Reader.cpp +++ b/cdoc/CDoc2Reader.cpp @@ -677,21 +677,15 @@ CDoc2Reader::CDoc2Reader(libcdoc::DataSource *src, bool take_ownership) } } -CDoc2Reader::CDoc2Reader(const std::string &path) - : CDoc2Reader(new libcdoc::IStreamSource(path), true) -{ -} - bool CDoc2Reader::isCDoc2File(libcdoc::DataSource *src) { - uint8_t in[libcdoc::CDoc2::LABEL.size()]; - constexpr size_t len = libcdoc::CDoc2::LABEL.size(); - if (src->read(&in[0], len) != len) { - LOG_DBG("CDoc2Reader::isCDoc1File: Cannot read tag"); + std::array in {}; + if (src->read(in.data(), in.size()) != in.size()) { + LOG_DBG("CDoc2Reader::isCDoc2File: Cannot read tag"); return false; } - if (libcdoc::CDoc2::LABEL.compare(0, len, (char *) &in[0], len)) { + if (libcdoc::CDoc2::LABEL.compare(0, in.size(), (char *) in.data(), in.size())) { LOG_DBG("CDoc2Reader::isCDoc2File: Invalid tag: {}", toHex(in)); return false; } diff --git a/cdoc/CDoc2Reader.h b/cdoc/CDoc2Reader.h index 98d20481..b0e012f2 100644 --- a/cdoc/CDoc2Reader.h +++ b/cdoc/CDoc2Reader.h @@ -39,7 +39,6 @@ class CDoc2Reader final: public libcdoc::CDocReader { libcdoc::result_t finishDecryption() override final; CDoc2Reader(libcdoc::DataSource *src, bool take_ownership = false); - CDoc2Reader(const std::string &path); static bool isCDoc2File(const std::string& path); static bool isCDoc2File(libcdoc::DataSource *src); diff --git a/cdoc/CDocReader.h b/cdoc/CDocReader.h index 3c52410b..5c033aa1 100644 --- a/cdoc/CDocReader.h +++ b/cdoc/CDocReader.h @@ -165,6 +165,8 @@ class CDOC_EXPORT CDocReader { * * Creates a new document reader if source is a valid CDoc container (either version 1 or 2). * Configuration and NetworkBackend may be null if keyservers are not used. + * If take_ownership is true, the source is deleted by the reader destructor. If src is not a valid CDoc file, + * the source is deleted before returning null. * @param src the container source * @param take_ownership if true the source is deleted in reader destructor * @param conf a configuration object diff --git a/cdoc/Crypto.cpp b/cdoc/Crypto.cpp index 0aca978a..e09c0e86 100644 --- a/cdoc/Crypto.cpp +++ b/cdoc/Crypto.cpp @@ -23,6 +23,10 @@ #define OPENSSL_SUPPRESS_DEPRECATED +#ifdef _WIN32 +#include // For RAND_screen +#endif + #include #include #include diff --git a/cdoc/PKCS11Backend.cpp b/cdoc/PKCS11Backend.cpp index 8b98d97c..4bccd920 100644 --- a/cdoc/PKCS11Backend.cpp +++ b/cdoc/PKCS11Backend.cpp @@ -37,9 +37,7 @@ #include #ifdef _WIN32 -//#include -//#include -//#include +#include #else #include #endif diff --git a/cdoc/Utils.h b/cdoc/Utils.h index 03a0686b..8518605b 100644 --- a/cdoc/Utils.h +++ b/cdoc/Utils.h @@ -25,10 +25,6 @@ #include #include -#ifdef _WIN32 -#include -#endif - namespace libcdoc { std::string toBase64(const uint8_t *data, size_t len); @@ -79,7 +75,7 @@ join(const std::vector &parts, const std::string_view sep) if (part != parts.front()) result += sep; result += part; } - return std::move(result); + return result; } std::vector JsonToStringArray(std::string_view json); @@ -123,83 +119,7 @@ std::string buildURL(const std::string& host, int port); std::string urlEncode(std::string_view src); std::string urlDecode(const std::string &src); -#ifdef _WIN32 - -static std::wstring toWide(UINT codePage, const std::string &in) -{ - std::wstring result; - if(in.empty()) - return result; - int len = MultiByteToWideChar(codePage, 0, in.data(), int(in.size()), nullptr, 0); - result.resize(size_t(len), 0); - len = MultiByteToWideChar(codePage, 0, in.data(), int(in.size()), &result[0], len); - return result; -} - -static std::wstring -toWide(const std::string& in) -{ - return toWide(CP_UTF8, in); -} - -static std::string toMultiByte(UINT codePage, const std::wstring &in) -{ - std::string result; - if(in.empty()) - return result; - int len = WideCharToMultiByte(codePage, 0, in.data(), int(in.size()), nullptr, 0, nullptr, nullptr); - result.resize(size_t(len), 0); - len = WideCharToMultiByte(codePage, 0, in.data(), int(in.size()), &result[0], len, nullptr, nullptr); - return result; -} - -static std::string -toUTF8(const std::wstring& in) -{ - return toMultiByte(CP_UTF8, in); -} - - -#endif - -static std::string toUTF8(const std::string &in) -{ -#ifdef _WIN32 - return toMultiByte(CP_UTF8, toWide(CP_ACP, in)); -#else - return in; -#endif -} - -static std::vector readFile(const std::string &path) -{ - std::vector data; -#ifdef _WIN32 - std::ifstream f(toWide(CP_UTF8, path).c_str(), std::ifstream::binary); -#else - std::ifstream f(path, std::ifstream::binary); -#endif - if (!f) - return data; - f.seekg(0, std::ifstream::end); - data.resize(size_t(f.tellg())); - f.clear(); - f.seekg(0); - f.read((char*)data.data(), std::streamsize(data.size())); - return data; -} - -static void writeFile(const std::string &path, const std::vector &data) -{ -#ifdef _WIN32 - std::ofstream f(toWide(CP_UTF8, path).c_str(), std::ofstream::binary); -#else - std::ofstream f(path.c_str(), std::ofstream::binary); -#endif - f.write((const char*)data.data(), std::streamsize(data.size())); -} - -} // vectorwrapbuf +} // namespace libcdoc // A source implementation that always keeps last 16 bytes in tag diff --git a/cdoc/WinBackend.cpp b/cdoc/WinBackend.cpp index 56e134d9..5abb5731 100644 --- a/cdoc/WinBackend.cpp +++ b/cdoc/WinBackend.cpp @@ -19,13 +19,16 @@ #include "WinBackend.h" #include "CDoc2.h" -#include "Crypto.h" #include "ILogger.h" -#include "Utils.h" #include #include +static std::wstring toWide(const std::string &in) +{ + return {in.cbegin(), in.cend()}; +} + struct libcdoc::WinBackend::Private { NCRYPT_PROV_HANDLE prov = 0; HCRYPTPROV_OR_NCRYPT_KEY_HANDLE key = 0; @@ -43,8 +46,8 @@ struct libcdoc::WinBackend::Private { void *state = NULL; SECURITY_STATUS result = NCryptEnumKeys(prov, NULL, &wkeyname, &state, NCRYPT_SILENT_FLAG); while (result == ERROR_SUCCESS) { - std::string name = toUTF8(wkeyname->pszName); - std::string algo = toUTF8(wkeyname->pszAlgid); + std::string_view name{(const char*)wkeyname->pszName, wcslen(wkeyname->pszName)}; + std::string_view algo{(const char*)wkeyname->pszAlgid, wcslen(wkeyname->pszAlgid)}; LOG_DBG("Name: {} Algo: {}", name, algo); NCryptFreeBuffer(wkeyname); result = NCryptEnumKeys(prov, NULL, &wkeyname, &state, NCRYPT_SILENT_FLAG); diff --git a/cdoc/cdoc-tool.cpp b/cdoc/cdoc-tool.cpp index 31a978c8..5b7236f7 100644 --- a/cdoc/cdoc-tool.cpp +++ b/cdoc/cdoc-tool.cpp @@ -152,15 +152,14 @@ parse_rcpt(ToolConf& conf, RecipientInfoVector& rcpts, int& arg_idx, int argc, c if (parts.size() != 3) return RESULT_USAGE; rcpt.type = RcptInfo::CERT; - filesystem::path cert_file(toUTF8(parts[2])); - rcpt.cert = std::move(readFile(cert_file.string())); - rcpt.key_file_name = cert_file.filename().string(); + rcpt.cert = readAllBytes(parts[2]); + rcpt.key_file_name = filesystem::path(parts[2]).filename().string(); } else if (method == "pkey") { // label:pkey:PUBLIC_KEY if (parts.size() != 3) return RESULT_USAGE; rcpt.type = RcptInfo::PKEY; - rcpt.secret = std::move(fromHex(parts[2])); + rcpt.secret = fromHex(parts[2]); } else if (method == "pfkey") { // label:pfkey:PUBLIC_KEY_FILE if (parts.size() != 3) return RESULT_USAGE; @@ -180,7 +179,7 @@ parse_rcpt(ToolConf& conf, RecipientInfoVector& rcpts, int& arg_idx, int argc, c if (parts.size() != 3) return RESULT_USAGE; rcpt.type = RcptInfo::SKEY; - rcpt.secret = std::move(fromHex(parts[2])); + rcpt.secret = fromHex(parts[2]); if (rcpt.secret.size() != 32) { LOG_ERROR("Symmetric key has to be exactly 32 bytes long"); return RESULT_ERROR; diff --git a/vcpkg.json b/vcpkg.json index e024029d..71bfd112 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -11,7 +11,7 @@ }, { "name": "flatbuffers", - "platform": "!osx & !ios" + "platform": "!osx" }, { "name": "openssl", @@ -21,8 +21,8 @@ "features": { "tests": { "description": "Build tests", "dependencies": ["boost-test"] } }, - "builtin-baseline": "085820b35f4ef5ad54967c8a46fb822e53c4be33", + "builtin-baseline": "98e7cd3a7ba579efc543f8854af800d033031eae", "vcpkg-configuration": { "overlay-triplets": ["./vcpkg-triplets"] } -} \ No newline at end of file +}