Skip to content

Commit 566a3a2

Browse files
committed
Add always required keys and parse empty field
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent e503001 commit 566a3a2

File tree

12 files changed

+163
-158
lines changed

12 files changed

+163
-158
lines changed

cdoc/CDoc.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ namespace libcdoc {
3131
*/
3232
using result_t = int64_t;
3333

34+
35+
/**
36+
* @brief The public key type
37+
*/
38+
enum class PKType : uint8_t {
39+
/**
40+
* Elliptic curve
41+
*/
42+
ECC,
43+
/**
44+
* RSA
45+
*/
46+
RSA
47+
};
48+
3449
enum {
3550
/**
3651
* @brief Operation completed successfully

cdoc/CDoc1Reader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ CDoc1Reader::getLockForCert(const std::vector<uint8_t>& cert)
8383
ll.encrypted_fmk.empty())
8484
continue;
8585
switch(cc.getAlgorithm()) {
86-
case libcdoc::Certificate::RSA:
86+
case libcdoc::PKType::RSA:
8787
if (ll.getString(Lock::Params::METHOD) == libcdoc::Crypto::RSA_MTH) {
8888
return i;
8989
}
9090
break;
91-
case libcdoc::Certificate::ECC:
91+
case libcdoc::PKType::ECC:
9292
if(!ll.getBytes(Lock::Params::KEY_MATERIAL).empty() &&
9393
std::find(SUPPORTED_KWAES.cbegin(), SUPPORTED_KWAES.cend(), ll.getString(Lock::Params::METHOD)) != SUPPORTED_KWAES.cend()) {
9494
return i;
@@ -310,7 +310,7 @@ CDoc1Reader::CDoc1Reader(libcdoc::DataSource *src, bool delete_on_close)
310310
Certificate ssl(cert);
311311
key.setBytes(Lock::CERT, std::move(cert));
312312
key.setBytes(Lock::RCPT_KEY, ssl.getPublicKey());
313-
key.pk_type = (ssl.getAlgorithm() == libcdoc::Certificate::RSA) ? Lock::RSA : Lock::ECC;
313+
key.pk_type = ssl.getAlgorithm();
314314
}
315315
// EncryptedData/KeyInfo/EncryptedKey/KeyInfo/CipherData/CipherValue
316316
else if(reader.isElement("CipherValue"))

cdoc/CDoc2Reader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ CDoc2Reader::Private::buildLock(Lock& lock, const cdoc20::header::RecipientRecor
511511
if(const auto *key = recipient.capsule_as_recipients_ECCPublicKeyCapsule()) {
512512
if(key->curve() == EllipticCurve::secp384r1) {
513513
lock.type = Lock::Type::PUBLIC_KEY;
514-
lock.pk_type = Lock::PKType::ECC;
514+
lock.pk_type = PKType::ECC;
515515
lock.setBytes(Lock::Params::RCPT_KEY, toUint8Vector(key->recipient_public_key()));
516516
lock.setBytes(Lock::Params::KEY_MATERIAL, toUint8Vector(key->sender_public_key()));
517517
LOG_DBG("Load PK: {}", toHex(lock.getBytes(Lock::Params::RCPT_KEY)));
@@ -524,7 +524,7 @@ CDoc2Reader::Private::buildLock(Lock& lock, const cdoc20::header::RecipientRecor
524524
if(const auto *key = recipient.capsule_as_recipients_RSAPublicKeyCapsule())
525525
{
526526
lock.type = Lock::Type::PUBLIC_KEY;
527-
lock.pk_type = Lock::PKType::RSA;
527+
lock.pk_type = PKType::RSA;
528528
lock.setBytes(Lock::Params::RCPT_KEY, toUint8Vector(key->recipient_public_key()));
529529
lock.setBytes(Lock::Params::KEY_MATERIAL, toUint8Vector(key->encrypted_kek()));
530530
}
@@ -539,13 +539,13 @@ CDoc2Reader::Private::buildLock(Lock& lock, const cdoc20::header::RecipientRecor
539539
LOG_ERROR("Unsupported elliptic curve key type");
540540
return;
541541
}
542-
lock.pk_type = Lock::PKType::ECC;
542+
lock.pk_type = PKType::ECC;
543543
lock.setBytes(Lock::Params::RCPT_KEY, toUint8Vector(eccDetails->recipient_public_key()));
544544
}
545545
break;
546546
case KeyDetailsUnion::RsaKeyDetails:
547547
if(const RsaKeyDetails *rsaDetails = server->recipient_key_details_as_RsaKeyDetails()) {
548-
lock.pk_type = Lock::PKType::RSA;
548+
lock.pk_type = PKType::RSA;
549549
lock.setBytes(Lock::Params::RCPT_KEY, toUint8Vector(rsaDetails->recipient_public_key()));
550550
}
551551
break;

cdoc/CDoc2Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ CDoc2Writer::buildHeader(std::vector<uint8_t>& header, const std::vector<libcdoc
222222
return libcdoc::CONFIGURATION_ERROR;
223223
}
224224
}
225-
if(rcpt.pk_type == libcdoc::Recipient::PKType::RSA) {
225+
if(rcpt.pk_type == libcdoc::PKType::RSA) {
226226
crypto->random(kek, libcdoc::CDoc2::KEY_LEN);
227227
if (libcdoc::Crypto::xor_data(xor_key, fmk, kek) != libcdoc::OK) {
228228
setLastError("Internal error");

cdoc/CDocCipher.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fill_recipients_from_rcpt_info(ToolConf& conf, ToolCrypto& crypto, std::vector<l
280280
LOG_DBG("Creating symmetric key:");
281281
} else if (rcpt.type == RcptInfo::Type::PKEY) {
282282
if (!conf.servers.empty()) {
283-
key = libcdoc::Recipient::makeServer(label, rcpt.secret, libcdoc::Recipient::PKType::ECC, conf.servers[0].ID);
283+
key = libcdoc::Recipient::makeServer(label, rcpt.secret, libcdoc::PKType::ECC, conf.servers[0].ID);
284284
} else {
285285
const uint8_t *der = rcpt.secret.data();
286286
EVP_PKEY *pkey = d2i_PUBKEY(nullptr, &der, rcpt.secret.size());
@@ -289,9 +289,9 @@ fill_recipients_from_rcpt_info(ToolConf& conf, ToolCrypto& crypto, std::vector<l
289289
uint8_t *p = d.data();
290290
i2d_PublicKey(pkey, &p);
291291
if (id == EVP_PKEY_EC) {
292-
key = libcdoc::Recipient::makePublicKey(label, rcpt.secret, libcdoc::Recipient::PKType::ECC);
292+
key = libcdoc::Recipient::makePublicKey(label, rcpt.secret, libcdoc::PKType::ECC);
293293
} else if (id == EVP_PKEY_RSA) {
294-
key = libcdoc::Recipient::makePublicKey(label, rcpt.secret, libcdoc::Recipient::PKType::RSA);
294+
key = libcdoc::Recipient::makePublicKey(label, rcpt.secret, libcdoc::PKType::RSA);
295295
}
296296
}
297297
LOG_DBG("Creating public key:");
@@ -308,9 +308,9 @@ fill_recipients_from_rcpt_info(ToolConf& conf, ToolCrypto& crypto, std::vector<l
308308
}
309309
LOG_DBG("Public key ({}): {}", rsa ? "rsa" : "ecc", toHex(val));
310310
if (!conf.servers.empty()) {
311-
key = libcdoc::Recipient::makeServer(label, val, rsa ? libcdoc::Recipient::PKType::RSA : libcdoc::Recipient::PKType::ECC, conf.servers[0].ID);
311+
key = libcdoc::Recipient::makeServer(label, val, rsa ? libcdoc::PKType::RSA : libcdoc::PKType::ECC, conf.servers[0].ID);
312312
} else {
313-
key = libcdoc::Recipient::makePublicKey(label, val, rsa ? libcdoc::Recipient::PKType::RSA : libcdoc::Recipient::PKType::ECC);
313+
key = libcdoc::Recipient::makePublicKey(label, val, rsa ? libcdoc::PKType::RSA : libcdoc::PKType::ECC);
314314
}
315315
} else if (rcpt.type == RcptInfo::Type::PASSWORD) {
316316
LOG_DBG("Creating password key:");

cdoc/Certificate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Certificate::getPublicKey() const
148148
return {};
149149
}
150150

151-
Certificate::Algorithm
151+
PKType
152152
Certificate::getAlgorithm() const
153153
{
154154
if(!cert)
@@ -157,7 +157,7 @@ Certificate::getAlgorithm() const
157157
EVP_PKEY *pkey = X509_get0_pubkey(cert.get());
158158
int alg = EVP_PKEY_get_base_id(pkey);
159159

160-
return (alg == EVP_PKEY_RSA) ? Algorithm::RSA : Algorithm::ECC;
160+
return (alg == EVP_PKEY_RSA) ? PKType::RSA : PKType::ECC;
161161
}
162162

163163
std::vector<uint8_t> Certificate::getDigest() const

cdoc/Certificate.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SSLCERTIFICATE_H
2020
#define SSLCERTIFICATE_H
2121

22+
#include "CDoc.h"
2223
#include "utils/memory.h"
2324

2425
#include <string>
@@ -30,11 +31,6 @@ namespace libcdoc {
3031

3132
class Certificate {
3233
public:
33-
enum Algorithm : unsigned char {
34-
RSA,
35-
ECC
36-
};
37-
3834
enum EIDType : unsigned char {
3935
Unknown,
4036
IDCard,
@@ -53,7 +49,7 @@ class Certificate {
5349
EIDType getEIDType() const;
5450

5551
std::vector<uint8_t> getPublicKey() const;
56-
Algorithm getAlgorithm() const;
52+
PKType getAlgorithm() const;
5753
time_t getNotAfter() const;
5854

5955
std::vector<uint8_t> getDigest() const;

cdoc/Lock.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include "json/base.h"
2525

26+
#include <ranges>
27+
2628
namespace libcdoc {
2729

2830
std::string
@@ -59,50 +61,46 @@ Lock::parseLabel(const std::string& label)
5961
{
6062
std::map<std::string, std::string> parsed_label;
6163
// Check if provided label starts with the machine generated label prefix.
62-
if (!label.starts_with(CDoc2::LABELPREFIX))
63-
{
64+
if (!label.starts_with(CDoc2::LABELPREFIX)) {
6465
return parsed_label;
6566
}
6667

67-
std::string label_wo_prefix(label.substr(CDoc2::LABELPREFIX.size()));
68+
auto label_wo_prefix = std::string_view(label).substr(CDoc2::LABELPREFIX.size());
6869

6970
// Label to be processed
70-
std::string label_to_prcss;
71+
std::string decodedBase64; // Strong ref
72+
std::string_view label_to_prcss;
7173

7274
// We ignore mediatype part
7375

7476
// Check, if the label is Base64 encoded
75-
auto base64IndPos = label_wo_prefix.find(CDoc2::LABELBASE64IND);
76-
if (base64IndPos == std::string::npos)
77-
{
78-
if (label_wo_prefix.starts_with(",")) {
79-
label_to_prcss = label_wo_prefix.substr(1);
80-
} else {
81-
label_to_prcss = std::move(label_wo_prefix);
82-
}
83-
}
84-
else
77+
if (auto base64IndPos = label_wo_prefix.find(CDoc2::LABELBASE64IND);
78+
base64IndPos != std::string::npos)
8579
{
8680
std::string base64_label(label_wo_prefix.substr(base64IndPos + CDoc2::LABELBASE64IND.size()));
87-
label_to_prcss = jwt::base::decode<jwt::alphabet::base64>(base64_label);
81+
decodedBase64 = jwt::base::decode<jwt::alphabet::base64>(base64_label);
82+
label_to_prcss = decodedBase64;
83+
} else if (label_wo_prefix.starts_with(",")) {
84+
label_to_prcss = label_wo_prefix.substr(1);
85+
} else {
86+
label_to_prcss = label_wo_prefix;
8887
}
8988

90-
auto label_parts(split(label_to_prcss, '&'));
91-
for (auto& part : label_parts)
89+
auto range_to_sv = [](auto range) constexpr {
90+
return std::string_view(&*range.begin(), std::ranges::distance(range));
91+
};
92+
for (const auto &part : std::ranges::split_view(label_to_prcss, '&'))
9293
{
93-
auto label_data_parts(split(part, '='));
94-
if (label_data_parts.size() != 2)
95-
{
96-
// Invalid label data. We just ignore them.
94+
auto label_data_parts = std::ranges::split_view(part, '=');
95+
if (label_data_parts.empty()) {
9796
LOG_ERROR("The label '{}' is invalid", label);
97+
continue;
9898
}
99-
else
100-
{
101-
std::string key = urlDecode(label_data_parts[0]);
102-
std::string value = urlDecode(label_data_parts[1]);
103-
std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c){ return std::tolower(c); });
104-
parsed_label[key] = value;
105-
}
99+
auto it = label_data_parts.begin();
100+
std::string key = urlDecode(range_to_sv(*it));
101+
++it;
102+
std::string value = urlDecode(range_to_sv(*it));
103+
parsed_label[std::move(key)] = std::move(value);
106104
}
107105

108106
return parsed_label;

cdoc/Lock.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef __LOCK_H__
2020
#define __LOCK_H__
2121

22-
#include <cdoc/Exports.h>
22+
#include "CDoc.h"
2323

2424
#include <cstdint>
2525
#include <string>
@@ -74,20 +74,6 @@ struct CDOC_EXPORT Lock
7474
SHARE_SERVER
7575
};
7676

77-
/**
78-
* @brief The public key type
79-
*/
80-
enum PKType : unsigned char {
81-
/**
82-
* Elliptic curve
83-
*/
84-
ECC,
85-
/**
86-
* RSA
87-
*/
88-
RSA
89-
};
90-
9177
/**
9278
* @brief Extra parameters depending on key type
9379
*/

0 commit comments

Comments
 (0)