Skip to content

Commit

Permalink
Merge pull request #23 from erkkiarus/feature/logger
Browse files Browse the repository at this point in the history
Logger integration and Windows build fix
  • Loading branch information
lauris71 authored Feb 13, 2025
2 parents 7a83e4f + 8182d9f commit 87b6aa0
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 249 deletions.
24 changes: 14 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ endif()
include(CPack)
include(GNUInstallDirs)

option(LIBCDOC_WITH_DOCS "Generate documentation with Doxygen" ON)

find_package(OpenSSL 3.0.0 REQUIRED)
find_package(ZLIB REQUIRED)
find_package(LibXml2 REQUIRED)
Expand All @@ -53,16 +55,18 @@ if(SWIG_FOUND)
find_package(Python3 COMPONENTS Interpreter Development)
endif()

find_package(Doxygen)
if( DOXYGEN_FOUND )
configure_file(${PROJECT_SOURCE_DIR}/etc/Doxyfile.in Doxyfile @ONLY)
add_custom_target(docs ALL
Doxygen::doxygen Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
install(DIRECTORY ${CMAKE_BINARY_DIR}/doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(DIRECTORY doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
if(LIBCDOC_WITH_DOCS)
find_package(Doxygen)
if( DOXYGEN_FOUND )
configure_file(${PROJECT_SOURCE_DIR}/etc/Doxyfile.in Doxyfile @ONLY)
add_custom_target(docs ALL
Doxygen::doxygen Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
install(DIRECTORY ${CMAKE_BINARY_DIR}/doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
install(DIRECTORY doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
endif()
endif()

add_subdirectory(cdoc)
Expand Down
26 changes: 14 additions & 12 deletions cdoc/CDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CDoc1Reader.h"
#include "CDoc2Writer.h"
#include "CDoc2Reader.h"
#include "ILogger.h"

#include <iostream>

Expand Down Expand Up @@ -59,9 +60,7 @@ getErrorStr(int64_t code) {
for (auto& r : results) {
if (r.code == code) return std::string(r.message);
}
return "Unknown result code " + std::to_string(code);
}

return FORMAT("Unknown result code {}", code);
}

bool
Expand All @@ -84,7 +83,7 @@ libcdoc::Configuration::getInt(const std::string_view& param, int def_val)
int64_t
libcdoc::Configuration::test(std::vector<uint8_t>& dst)
{
std::cerr << "Configuration::test::Native superclass" << std::endl;
LOG_TRACE("Configuration::test::Native superclass");
return OK;
}
#endif
Expand All @@ -111,7 +110,7 @@ libcdoc::NetworkBackend::getLastErrorStr(int code) const
int64_t
libcdoc::NetworkBackend::test(std::vector<std::vector<uint8_t>> &dst)
{
std::cerr << "NetworkBackend::test::Native superclass" << std::endl;
LOG_TRACE("NetworkBackend::test::Native superclass");
return OK;
}
#endif
Expand All @@ -138,7 +137,7 @@ libcdoc::CDocReader *
libcdoc::CDocReader::createReader(DataSource *src, bool take_ownership, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network)
{
int version = getCDocFileVersion(src);
std::cerr << "CDocReader::createReader: version " << version << std::endl;
LOG_DBG("CDocReader::createReader: version ", version);
if (src->seek(0) != libcdoc::OK) return nullptr;
CDocReader *reader;
if (version == 1) {
Expand Down Expand Up @@ -196,24 +195,24 @@ libcdoc::CDocReader::createReader(std::istream& ifs, Configuration *conf, Crypto
int64_t
libcdoc::CDocReader::testConfig(std::vector<uint8_t>& dst)
{
std::cerr << "CDocReader::testConfig::Native superclass" << std::endl;
LOG_TRACE("CDocReader::testConfig::Native superclass");
if (conf) {
std::cerr << "CDocReader::testConfig this=" << this << " conf=" << conf << std::endl;
LOG_DBG("CDocReader::testConfig this={} conf={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(conf));
return conf->test(dst);
}
std::cerr << "CDocReader::testConfig::conf is null" << std::endl;
LOG_ERROR("CDocReader::testConfig::conf is null");
return WORKFLOW_ERROR;
}

int64_t
libcdoc::CDocReader::testNetwork(std::vector<std::vector<uint8_t>>& dst)
{
std::cerr << "CDocReader::testNetwork::Native superclass" << std::endl;
LOG_TRACE("CDocReader::testNetwork::Native superclass");
if (network) {
std::cerr << "CDocReader::testNetwork this=" << this << " network=" << network << std::endl;
LOG_DBG("CDocReader::testNetwork this={} network={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(network));
return network->test(dst);
}
std::cerr << "CDocReader::testNetwork::network is null" << std::endl;
LOG_ERROR("CDocReader::testNetwork::network is null");
return WORKFLOW_ERROR;
}
#endif
Expand Down Expand Up @@ -258,3 +257,6 @@ libcdoc::CDocWriter::createWriter(int version, const std::string& path, Configur
libcdoc::DataConsumer *dst = new libcdoc::OStreamConsumer(path);
return createWriter(version, dst, true, conf, crypto, network);
}

}

18 changes: 15 additions & 3 deletions cdoc/CDoc1Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
*/

#include <iostream>
#include <map>
#include <set>

Expand All @@ -27,11 +26,14 @@
#include "CDoc.h"
#include "Crypto.h"
#include "DDocReader.h"
#include "ILogger.h"
#include "XmlReader.h"
#include "ZStream.h"

#include "CDoc1Reader.h"

using namespace libcdoc;

static const std::string MIME_ZLIB = "http://www.isi.edu/in-noes/iana/assignments/media-types/application/zip";
static const std::string MIME_DDOC = "http://www.sk.ee/DigiDoc/v1.3.0/digidoc.xsd";
static const std::string MIME_DDOC_OLD = "http://www.sk.ee/DigiDoc/1.3.0/digidoc.xsd";
Expand Down Expand Up @@ -116,6 +118,7 @@ CDoc1Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
const libcdoc::Lock *lock = d->locks.at(lock_idx);
if (lock->type != libcdoc::Lock::Type::CDOC1) {
setLastError("Not a CDoc1 key");
LOG_ERROR("{}", last_error);
return libcdoc::UNSPECIFIED_ERROR;
}
setLastError({});
Expand All @@ -124,6 +127,7 @@ CDoc1Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
int result = crypto->decryptRSA(decrypted_key, lock->encrypted_fmk, false, lock_idx);
if (result < 0) {
setLastError(crypto->getLastErrorStr(result));
LOG_ERROR("{}", last_error);
return libcdoc::CRYPTO_ERROR;
}
} else {
Expand All @@ -135,11 +139,13 @@ CDoc1Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
lock_idx);
if (result < 0) {
setLastError(crypto->getLastErrorStr(result));
LOG_ERROR("{}", last_error);
return libcdoc::CRYPTO_ERROR;
}
}
if(decrypted_key.empty()) {
setLastError("Failed to decrypt/derive key");
LOG_ERROR("{}", last_error);
return libcdoc::CRYPTO_ERROR;
}
if(lock->isRSA()) {
Expand All @@ -149,6 +155,7 @@ CDoc1Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
}
if (fmk.empty()) {
setLastError("Failed to decrypt/derive fmk");
LOG_ERROR("{}", last_error);
return libcdoc::CRYPTO_ERROR;
}
setLastError({});
Expand Down Expand Up @@ -208,9 +215,10 @@ CDoc1Reader::decrypt(const std::vector<uint8_t>& fmk, libcdoc::MultiDataConsumer
}
libcdoc::VectorSource vsrc(data);
if(mime == MIME_DDOC || mime == MIME_DDOC_OLD) {
std::cerr << "Contains DDoc content" << mime;
LOG_DBG("Contains DDoc content {}", mime);
if (!DDOCReader::parse(&vsrc, dst)) {
setLastError("Failed to parse DDOC file");
LOG_ERROR("{}", last_error);
return libcdoc::UNSPECIFIED_ERROR;
}
return libcdoc::OK;
Expand All @@ -227,6 +235,7 @@ CDoc1Reader::beginDecryption(const std::vector<uint8_t>& fmk)
{
if (!d->files.empty() || (d->f_pos != -1)) {
setLastError("Container is already parsed");
LOG_ERROR("{}", last_error);
return libcdoc::WORKFLOW_ERROR;
}
std::vector<uint8_t> data = this->decryptData(fmk);
Expand All @@ -241,7 +250,7 @@ CDoc1Reader::beginDecryption(const std::vector<uint8_t>& fmk)
mime = d->properties["OriginalMimeType"];
}
if(mime == MIME_DDOC || mime == MIME_DDOC_OLD) {
std::cerr << "Contains DDoc content" << mime;
LOG_DBG("Contains DDoc content {}", mime);
d->files = DDOCReader::files(data);
} else {
d->files.push_back({
Expand All @@ -252,6 +261,7 @@ CDoc1Reader::beginDecryption(const std::vector<uint8_t>& fmk)
}
if (d->files.empty()) {
setLastError("Cannot parse container");
LOG_ERROR("{}", last_error);
return libcdoc::IO_ERROR;
}
setLastError({});
Expand All @@ -271,6 +281,7 @@ CDoc1Reader::nextFile(std::string& name, int64_t& size)
{
if (d->files.empty()) {
setLastError("Cannot parse container");
LOG_ERROR("{}", last_error);
return libcdoc::WORKFLOW_ERROR;
}
d->f_pos += 1;
Expand All @@ -288,6 +299,7 @@ CDoc1Reader::readData(uint8_t *dst, size_t size)
{
if (!d->src) {
setLastError("Cannot parse container");
LOG_ERROR("{}", last_error);
return libcdoc::WORKFLOW_ERROR;
}
return d->src->read(dst, size);
Expand Down
21 changes: 13 additions & 8 deletions cdoc/CDoc1Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "Utils.h"
#include "CDoc.h"
#include "XmlWriter.h"
#include "CDoc1Writer.h"
#include "ILogger.h"

#if defined(_WIN32) || defined(_WIN64)
#include <IntSafe.h>
Expand All @@ -30,10 +32,11 @@

#include <openssl/x509.h>

#include "CDoc1Writer.h"

#define SCOPE(TYPE, VAR, DATA) std::unique_ptr<TYPE,decltype(&TYPE##_free)> VAR(DATA, TYPE##_free)

using namespace libcdoc;

struct FileEntry {
std::string name;
size_t size;
Expand Down Expand Up @@ -145,13 +148,11 @@ bool CDoc1Writer::Private::writeRecipient(XMLWriter *xmlw, const std::vector<uin
AlgorithmID, SsDer, recipient);
encryptedData = libcdoc::Crypto::AESWrap(encryptionKey, transportKey.key, true);

#ifndef NDEBUG
printf("Ss %s\n", libcdoc::toHex(SsDer).c_str());
printf("Ksr %s\n", libcdoc::toHex(sharedSecret).c_str());
printf("ConcatKDF %s\n", libcdoc::toHex(encryptionKey).c_str());
printf("iv %s\n", libcdoc::toHex(transportKey.iv).c_str());
printf("transport %s\n", libcdoc::toHex(transportKey.key).c_str());
#endif
LOG_TRACE("Ss {}", toHex(SsDer));
LOG_TRACE("Ksr {}", toHex(sharedSecret));
LOG_TRACE("ConcatKDF {}", toHex(encryptionKey));
LOG_TRACE("iv {}", libcdoc::toHex(transportKey.iv));
LOG_TRACE("transport {}", toHex(transportKey.key));

xmlw->writeElement(Private::DENC, "EncryptionMethod", {{"Algorithm", encryptionMethod}});
xmlw->writeElement(Private::DS, "KeyInfo", [&]{
Expand Down Expand Up @@ -208,10 +209,12 @@ CDoc1Writer::encrypt(libcdoc::MultiDataSource& src, const std::vector<libcdoc::R
for (const libcdoc::Recipient& key : keys) {
if (!key.isCertificate()) {
d->lastError = "Invalid recipient type";
LOG_ERROR("{}", d->lastError);
return libcdoc::UNSPECIFIED_ERROR;
}
if(!d->writeRecipient(d->_xml.get(), key.cert, transportKey)) {
d->lastError = "Failed to write Recipient info";
LOG_ERROR("{}", d->lastError);
return libcdoc::IO_ERROR;
}
}
Expand Down Expand Up @@ -313,10 +316,12 @@ CDoc1Writer::finishEncryption()
for (const libcdoc::Recipient& key : d->rcpts) {
if (!key.isCertificate()) {
d->lastError = "Invalid recipient type";
LOG_ERROR("{}", d->lastError);
return libcdoc::UNSPECIFIED_ERROR;
}
if(!d->writeRecipient(d->_xml.get(), key.cert, transportKey)) {
d->lastError = "Failed to write Recipient info";
LOG_ERROR("{}", d->lastError);
return libcdoc::IO_ERROR;
}
}
Expand Down
Loading

0 comments on commit 87b6aa0

Please sign in to comment.