Skip to content

Commit

Permalink
MOPP
Browse files Browse the repository at this point in the history
  • Loading branch information
metsma committed Feb 3, 2025
1 parent 65c19d6 commit 76b148e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cdoc/CDoc1Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,24 @@ CDoc1Reader::decrypt(const std::vector<uint8_t>& fmk, libcdoc::MultiDataConsumer
int
CDoc1Reader::beginDecryption(const std::vector<uint8_t>& fmk)
{
if (fmk.empty()) {
setLastError("FMK is missing");
return libcdoc::WORKFLOW_ERROR;
}
if (fmk.size() != 16 && fmk.size() != 24 && fmk.size() != 32) {
setLastError("FMK must be AES key with size 128, 192,2 56 bits");
return libcdoc::WORKFLOW_ERROR;
}
if (!d->files.empty() || (d->f_pos != -1)) {
setLastError("Container is already parsed");
return libcdoc::WORKFLOW_ERROR;
}
std::vector<uint8_t> data = this->decryptData(fmk);
if(data.empty()) {
setLastError("Failed to decrypt data, verify if FMK is correct");
return libcdoc::WORKFLOW_ERROR;
}

std::string mime = d->mime;
if (d->mime == MIME_ZLIB) {
libcdoc::VectorSource vsrc(data);
Expand Down
2 changes: 1 addition & 1 deletion cdoc/CDoc1Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ int
CDoc1Writer::addFile(const std::string& name, size_t size)
{
d->files.push_back({name, size, {}});
return libcdoc::NOT_IMPLEMENTED;
return libcdoc::OK;
}

int64_t
Expand Down
2 changes: 1 addition & 1 deletion cdoc/CDocChipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ void CDocChipher::Locks(const char* file) const
}
}

#if defined(_WIN32) || defined(_WIN64) || defined(__GNUC__)
#if defined(_WIN32) || defined(_WIN64) || !defined(__APPLE__)
uint32_t
arc4random_uniform(uint32_t upperbound)
{
Expand Down
4 changes: 4 additions & 0 deletions cdoc/CDocReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class CDOC_EXPORT CDocReader {
#endif
protected:
explicit CDocReader(int _version) : version(_version) {};
CDocReader (const CDocReader&) = delete;
CDocReader (CDocReader&&) noexcept;
CDocReader& operator= (const CDocReader&) = delete;
CDocReader& operator= (CDocReader&&) noexcept;

void setLastError(const std::string& message) { last_error = message; }

Expand Down
13 changes: 12 additions & 1 deletion cdoc/CDocWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@
#include <cdoc/Exports.h>
#include <cdoc/NetworkBackend.h>

#if __has_include(<swift/bridging>)
#include <swift/bridging>
#endif
#ifndef SWIFT_NONCOPYABLE
#define SWIFT_NONCOPYABLE
#endif

namespace libcdoc {

class CDOC_EXPORT CDocWriter {
class CDOC_EXPORT SWIFT_NONCOPYABLE CDocWriter {
public:
virtual ~CDocWriter();

Expand Down Expand Up @@ -106,6 +113,10 @@ class CDOC_EXPORT CDocWriter {
static CDocWriter *createWriter(int version, const std::string& path, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network);
protected:
explicit CDocWriter(int _version, DataConsumer *dst, bool take_ownership);
CDocWriter (const CDocWriter&) = delete;
CDocWriter (CDocWriter&&) noexcept;
CDocWriter& operator= (const CDocWriter&) = delete;
CDocWriter& operator= (CDocWriter&&) noexcept;

void setLastError(const std::string& message) { last_error = message; }

Expand Down
2 changes: 2 additions & 0 deletions cdoc/CryptoBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ struct CDOC_EXPORT CryptoBackend {
virtual int test(libcdoc::Lock& lock) { return NOT_IMPLEMENTED; }

CryptoBackend (const CryptoBackend&) = delete;
CryptoBackend (CryptoBackend&&) noexcept = default;
CryptoBackend& operator= (const CryptoBackend&) = delete;
CryptoBackend& operator= (CryptoBackend&&) noexcept = default;
};

} // namespace libcdoc
Expand Down
21 changes: 20 additions & 1 deletion cdoc/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@
#include <cdoc/CDoc.h>

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>

#ifdef __swift__
// Swift compiler can't handle std::filesystem; provide a stub
namespace std {
namespace filesystem {
class path {
std::string p;
public:
path(std::string str = {}) : p(std::move(str)) {}
std::string string() const { return p; }
inline path& operator/=(const path &other) {
p += "/" + other.p;
return *this;
}
};
}
}
#else
#include <filesystem>
#endif

namespace libcdoc {

class DataSource;
Expand Down
2 changes: 2 additions & 0 deletions cdoc/Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ struct CDOC_EXPORT Lock
void setString(Params key, const std::string& val) { params[key] = std::vector<uint8_t>(val.cbegin(), val.cend()); }
void setInt(Params key, int32_t val);

#ifndef __swift__
bool operator== (const Lock& other) const = default;
#endif

// Set certificate, rcpt_key and pk_type values
void setCertificate(const std::vector<uint8_t>& cert);
Expand Down
2 changes: 2 additions & 0 deletions cdoc/Recipient.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ struct CDOC_EXPORT Recipient {

static std::map<std::string, std::string> parseLabel(const std::string& label);

#ifndef __swift__
bool operator== (const Recipient& other) const = default;
#endif
protected:
Recipient(Type _type) : type(_type) {};
private:
Expand Down

0 comments on commit 76b148e

Please sign in to comment.