Skip to content
Merged
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: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ jobs:
- name: Prepare vcpkg
uses: lukka/run-vcpkg@v11
with:
vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33
vcpkgJsonGlob: ./vcpkg.json
runVcpkgInstall: true
env:
Expand Down Expand Up @@ -116,7 +115,6 @@ jobs:
if: matrix.target != 'macos'
uses: lukka/run-vcpkg@v11
with:
vcpkgGitCommitId: 085820b35f4ef5ad54967c8a46fb822e53c4be33
vcpkgJsonGlob: ./vcpkg.json
runVcpkgInstall: true
env:
Expand Down Expand Up @@ -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`]"
Expand Down
7 changes: 4 additions & 3 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -140,4 +141,4 @@
"configurePreset": "androidx86_64"
}
]
}
}
19 changes: 15 additions & 4 deletions cdoc/CDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<IStreamSource>(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;
}
Expand All @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions cdoc/CDoc1Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
1 change: 0 additions & 1 deletion cdoc/CDoc1Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<libcdoc::Lock>& getLocks() override final;
Expand Down
14 changes: 4 additions & 10 deletions cdoc/CDoc2Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t,libcdoc::CDoc2::LABEL.size()> 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;
}
Expand Down
1 change: 0 additions & 1 deletion cdoc/CDoc2Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions cdoc/CDocReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cdoc/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#define OPENSSL_SUPPRESS_DEPRECATED

#ifdef _WIN32
#include <windows.h> // For RAND_screen
#endif

#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/kdf.h>
Expand Down
4 changes: 1 addition & 3 deletions cdoc/PKCS11Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
#include <functional>

#ifdef _WIN32
//#include <Windows.h>
//#include <wincrypt.h>
//#include <cryptuiapi.h>
#include <Windows.h>
#else
#include <dlfcn.h>
#endif
Expand Down
84 changes: 2 additions & 82 deletions cdoc/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
#include <iostream>
#include <sstream>

#ifdef _WIN32
#include <Windows.h>
#endif

namespace libcdoc {

std::string toBase64(const uint8_t *data, size_t len);
Expand Down Expand Up @@ -79,7 +75,7 @@ join(const std::vector<std::string> &parts, const std::string_view sep)
if (part != parts.front()) result += sep;
result += part;
}
return std::move(result);
return result;
}

std::vector<std::string> JsonToStringArray(std::string_view json);
Expand Down Expand Up @@ -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<unsigned char> readFile(const std::string &path)
{
std::vector<unsigned char> 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<unsigned char> &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

Expand Down
11 changes: 7 additions & 4 deletions cdoc/WinBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
#include "WinBackend.h"

#include "CDoc2.h"
#include "Crypto.h"
#include "ILogger.h"
#include "Utils.h"

#include <Windows.h>
#include <wincrypt.h>

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;
Expand All @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions cdoc/cdoc-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"name": "flatbuffers",
"platform": "!osx & !ios"
"platform": "!osx"
},
{
"name": "openssl",
Expand All @@ -21,8 +21,8 @@
"features": {
"tests": { "description": "Build tests", "dependencies": ["boost-test"] }
},
"builtin-baseline": "085820b35f4ef5ad54967c8a46fb822e53c4be33",
"builtin-baseline": "98e7cd3a7ba579efc543f8854af800d033031eae",
"vcpkg-configuration": {
"overlay-triplets": ["./vcpkg-triplets"]
}
}
}
Loading