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
11 changes: 9 additions & 2 deletions cdoc/CDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ getVersion()
int
libcdoc::CDocReader::getCDocFileVersion(DataSource *src)
{
if (src->seek(0) != libcdoc::OK) return libcdoc::IO_ERROR;
if (src->seek(0) != libcdoc::OK) {
LOG_DBG("CDocReader::getCDocFileVersion (A): Source does not support seek");
return libcdoc::IO_ERROR;
}
if (CDoc2Reader::isCDoc2File(src)) return 2;
if (src->seek(0) != libcdoc::OK) return libcdoc::IO_ERROR;
if (src->seek(0) != libcdoc::OK) {
LOG_DBG("CDocReader::getCDocFileVersion (B): Source does not support seek");
return libcdoc::IO_ERROR;
}
if (CDoc1Reader::isCDoc1File(src)) return 1;
LOG_DBG("CDocReader::getCDocFileVersion: File not supported");
return libcdoc::NOT_SUPPORTED;
}

Expand Down
39 changes: 16 additions & 23 deletions cdoc/CDoc1Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "DDocReader.h"
#include "ILogger.h"
#include "Lock.h"
#include "Utils.h"
#include "XmlReader.h"
#include "ZStream.h"

Expand Down Expand Up @@ -85,8 +86,7 @@ CDoc1Reader::getLockForCert(const std::vector<uint8_t>& cert)
libcdoc::Certificate cc(cert);
for (size_t i = 0; i < d->locks.size(); i++) {
const Lock &ll = d->locks.at(i);
if (!ll.isCDoc1() ||
ll.getBytes(Lock::Params::CERT) != cert ||
if (ll.getBytes(Lock::Params::CERT) != cert ||
ll.encrypted_fmk.empty())
continue;
switch(cc.getAlgorithm()) {
Expand Down Expand Up @@ -115,11 +115,6 @@ CDoc1Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
{
if (lock_idx >= d->locks.size()) return libcdoc::WRONG_ARGUMENTS;
const Lock &lock = d->locks.at(lock_idx);
if (lock.type != Lock::Type::CDOC1) {
setLastError("Not a CDoc1 key");
LOG_ERROR("{}", last_error);
return libcdoc::UNSPECIFIED_ERROR;
}
setLastError({});
if (lock.isRSA()) {
int result = crypto->decryptRSA(fmk, lock.encrypted_fmk, false, lock_idx);
Expand Down Expand Up @@ -264,7 +259,7 @@ CDoc1Reader::readData(uint8_t *dst, size_t size)

/*
* CDoc1Reader constructor.
* @param file File to open reading
* @param src A DataSource of container
*/
CDoc1Reader::CDoc1Reader(libcdoc::DataSource *src, bool delete_on_close)
: CDocReader(1), d(new Private)
Expand Down Expand Up @@ -306,26 +301,16 @@ CDoc1Reader::CDoc1Reader(libcdoc::DataSource *src, bool delete_on_close)
else if(reader.isElement("EncryptedKey"))
{
Lock &key = d->locks.emplace_back(Lock::Type::CDOC1);
//key.id = reader.attribute("Id");
key.label = reader.attribute("Recipient");
while(reader.read())
{
if(reader.isElement("EncryptedKey") && reader.isEndElement())
break;
if(reader.isEndElement())
continue;
// EncryptedData/KeyInfo/KeyName
//if(reader.isElement("KeyName"))
// key.name = reader.readText();
// EncryptedData/KeyInfo/EncryptedKey/EncryptionMethod
if(reader.isElement("EncryptionMethod"))
key.setString(Lock::Params::METHOD, reader.attribute("Algorithm"));
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/AgreementMethod
//else if(reader.isElement("AgreementMethod"))
// key.agreement = reader.attribute("Algorithm");
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/AgreementMethod/KeyDerivationMethod
//else if(reader.isElement("KeyDerivationMethod"))
// key.derive = reader.attribute("Algorithm");
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/AgreementMethod/KeyDerivationMethod/ConcatKDFParams
else if(reader.isElement("ConcatKDFParams"))
{
Expand Down Expand Up @@ -363,11 +348,18 @@ CDoc1Reader::CDoc1Reader(const std::string &path)
bool
CDoc1Reader::isCDoc1File(libcdoc::DataSource *src)
{
// fixme: better check
static const std::string XML_TAG("<?xml");
std::vector<uint8_t>buf(XML_TAG.size());
if (src->read(buf.data(), XML_TAG.size()) != XML_TAG.size()) return false;
if (XML_TAG.compare(0, XML_TAG.size(), (char *) buf.data())) return false;
// todo: better check
static constexpr std::string_view XML_TAG("<?xml");
std::array<uint8_t,XML_TAG.size()> buf;
if (src->read(buf.data(), XML_TAG.size()) != XML_TAG.size()) {
LOG_DBG("CDoc1Reader::isCDoc1File: Cannot read tag");
return false;
}
if (XML_TAG.compare(0, XML_TAG.size(), (const char *) buf.data(), buf.size())) {
LOG_DBG("CDoc1Reader::isCDoc1File: Invalid tag: {}", toHex(buf));
LOG_DBG("CDoc1Reader::isCDoc1File: Should be : {}", toHex(XML_TAG));
return false;
}
return true;
}

Expand All @@ -393,6 +385,7 @@ result_t CDoc1Reader::decryptData(const std::vector<uint8_t>& fmk, std::string&
return libcdoc::WORKFLOW_ERROR;
}
if (auto result = d->dsrc->seek(0); result != libcdoc::OK) {
LOG_ERROR("{}", d->src->getLastErrorStr(result));
return result;
}

Expand Down
10 changes: 8 additions & 2 deletions cdoc/CDoc2Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,14 @@ 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) return false;
if (libcdoc::CDoc2::LABEL.compare(0, len, (char *) &in[0], len)) return false;
if (src->read(&in[0], len) != len) {
LOG_DBG("CDoc2Reader::isCDoc1File: Cannot read tag");
return false;
}
if (libcdoc::CDoc2::LABEL.compare(0, len, (char *) &in[0], len)) {
LOG_DBG("CDoc2Reader::isCDoc2File: Invalid tag: {}", toHex(in));
return false;
}
return true;
}

Expand Down
9 changes: 6 additions & 3 deletions examples/java/src/main/java/ee/ria/cdoc/CDocTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static void main(String[] args) {
String library = "../../build/macos/cdoc/libcdoc_javad.jnilib";
Action action = Action.INVALID;
ArrayList<String> files = new ArrayList<>();
int version = 2;
String label = null;
String password = null;
String out = "test.cdoc2";
Expand Down Expand Up @@ -68,6 +69,8 @@ public static void main(String[] args) {
} else if (args[i].equals("--label")) {
label = getArg(i, args);
i += 1;
} else if (args[i].equals("--v1")) {
version = 1;
} else if (args[i].equals("--certfile")) {
certfile = getArg(i, args);
i += 1;
Expand Down Expand Up @@ -134,7 +137,7 @@ public static void main(String[] args) {
switch (action) {
case ENCRYPT:
if (certfile != null) {
encryptCertFile(out, label, certfile, files);
encryptCertFile(version, out, label, certfile, files);
} else if (password != null) {
encrypt(out, label, password, files);
} else if (servers != null) {
Expand Down Expand Up @@ -314,11 +317,11 @@ static void encrypt(String file, String label, String password, Collection<Strin
}
}

static void encryptCertFile(String file, String label, String certfile, Collection<String> files) {
static void encryptCertFile(int version, String file, String label, String certfile, Collection<String> files) {
System.out.println("Creating file " + file);
ToolConf conf = new ToolConf();
ToolNetwork network = new ToolNetwork();
CDocWriter wrtr = CDocWriter.createWriter(2, file, conf, null, network);
CDocWriter wrtr = CDocWriter.createWriter(version, file, conf, null, network);
try {
InputStream ifs = new FileInputStream(certfile);
byte[] cert = ifs.readAllBytes();
Expand Down
Loading