Skip to content

Commit f70afa7

Browse files
authored
Merge branch 'open-eid:master' into master
2 parents 129976b + e503001 commit f70afa7

File tree

6 files changed

+99
-64
lines changed

6 files changed

+99
-64
lines changed

cdoc/CDoc.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ struct FileInfo {
183183
int64_t size;
184184
};
185185

186+
#ifdef VERSION
187+
#undef VERSION
188+
#endif
189+
190+
namespace CDoc2 {
191+
namespace Label {
192+
/**
193+
* @brief Recipient types for machine-readable labels
194+
*
195+
*/
196+
static constexpr std::string_view TYPE_PASSWORD = "pw";
197+
static constexpr std::string_view TYPE_SYMMETRIC = "secret";
198+
static constexpr std::string_view TYPE_PUBLIC_KEY = "pub_key";
199+
static constexpr std::string_view TYPE_CERTIFICATE = "cert";
200+
static constexpr std::string_view TYPE_UNKNOWN = "Unknown";
201+
static constexpr std::string_view TYPE_ID_CARD = "ID-card";
202+
static constexpr std::string_view TYPE_DIGI_ID = "Digi-ID";
203+
static constexpr std::string_view TYPE_DIGI_ID_E_RESIDENT = "Digi-ID E-RESIDENT";
204+
205+
/**
206+
* @brief Recipient data for machine-readable labels
207+
*
208+
*/
209+
static constexpr std::string_view VERSION = "v";
210+
static constexpr std::string_view TYPE = "type";
211+
static constexpr std::string_view FILE = "file";
212+
static constexpr std::string_view LABEL = "label";
213+
static constexpr std::string_view CN = "cn";
214+
static constexpr std::string_view SERIAL_NUMBER = "serial_number";
215+
static constexpr std::string_view LAST_NAME = "last_name";
216+
static constexpr std::string_view FIRST_NAME = "first_name";
217+
static constexpr std::string_view CERT_SHA1 = "cert_sha1";
218+
}
219+
}
220+
186221
}; // namespace libcdoc
187222

188223
#endif // CDOC_H

cdoc/CDoc2.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef __CDOC2_H__
2020
#define __CDOC2_H__
2121

22+
#include "CDoc.h"
23+
2224
#include <string_view>
2325

2426
namespace libcdoc {
@@ -57,10 +59,10 @@ constexpr std::string_view LABELBASE64IND{";base64,"};
5759
* @brief EID type values for machine-readable label
5860
*/
5961
static constexpr std::string_view eid_strs[] = {
60-
"Unknown",
61-
"ID-card",
62-
"Digi-ID",
63-
"Digi-ID E-RESIDENT"
62+
CDoc2::Label::TYPE_UNKNOWN,
63+
CDoc2::Label::TYPE_ID_CARD,
64+
CDoc2::Label::TYPE_DIGI_ID,
65+
CDoc2::Label::TYPE_DIGI_ID_E_RESIDENT
6466
};
6567

6668
} // namespace CDoc2

cdoc/CDoc2Reader.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ CDoc2Reader::CDoc2Reader(libcdoc::DataSource *src, bool take_ownership)
630630
LOG_ERROR("{}", last_error);
631631
return;
632632
}
633-
//if (libcdoc::CDoc2::LABEL.compare(0, libcdoc::CDoc2::LABEL.size(), (const char *) in)) return;
634633

635634
// Read 32-bit header length in big endian order
636635
uint8_t c[4];

cdoc/Recipient.cpp

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -121,60 +121,44 @@ Recipient::isTheSameRecipient(const std::vector<uint8_t>& public_key) const
121121
}
122122

123123
static void
124-
buildLabel(std::ostream& ofs, std::string_view type, std::initializer_list<std::pair<std::string_view, std::string_view>> components)
124+
buildLabel(std::ostream& ofs, std::string_view type, const std::map<std::string_view,std::string_view> lbl_parts, std::initializer_list<std::pair<std::string_view, std::string_view>> extra)
125125
{
126+
auto parts = lbl_parts;
127+
if (parts.contains("v"))
128+
parts.erase("v");
129+
if (parts.contains("type"))
130+
parts.erase("type");
131+
for (const auto& [key, value] : extra) {
132+
if (!value.empty())
133+
parts[key] = value;
134+
}
126135
ofs << CDoc2::LABELPREFIX;
127-
ofs << "v" << '=' << std::to_string(CDoc2::KEYLABELVERSION) << '&'
128-
<< "type" << '=' << type;
129-
for (const auto& [key, value] : components) {
136+
ofs << CDoc2::Label::VERSION << '=' << std::to_string(CDoc2::KEYLABELVERSION) << '&'
137+
<< CDoc2::Label::TYPE << '=' << type;
138+
for (const auto& [key, value] : parts) {
130139
if (!value.empty())
131140
ofs << '&' << urlEncode(key) << '=' << urlEncode(value);
132141
}
133142
}
134143

135144
static void
136-
BuildLabelEID(std::ostream& ofs, Certificate::EIDType type, const Certificate& x509)
137-
{
138-
buildLabel(ofs, CDoc2::eid_strs[type], {
139-
{"cn", x509.getCommonName()},
140-
{"serial_number", x509.getSerialNumber()},
141-
{"last_name", x509.getSurname()},
142-
{"first_name", x509.getGivenName()},
143-
});
144-
}
145-
146-
static void
147-
BuildLabelCertificate(std::ostream &ofs, const std::string& file, const Certificate& x509)
145+
BuildLabelEID(std::ostream& ofs, Certificate::EIDType type, const Certificate& x509, const std::map<std::string_view,std::string_view>& lbl_parts)
148146
{
149-
buildLabel(ofs, "cert", {
150-
{"file", file},
151-
{"cn", x509.getCommonName()},
152-
{"cert_sha1", toHex(x509.getDigest())}
147+
148+
buildLabel(ofs, CDoc2::eid_strs[type], lbl_parts, {
149+
{CDoc2::Label::CN, x509.getCommonName()},
150+
{CDoc2::Label::SERIAL_NUMBER, x509.getSerialNumber()},
151+
{CDoc2::Label::LAST_NAME, x509.getSurname()},
152+
{CDoc2::Label::FIRST_NAME, x509.getGivenName()},
153153
});
154154
}
155155

156156
static void
157-
BuildLabelPublicKey(std::ostream &ofs, const std::string& file)
157+
BuildLabelCertificate(std::ostream &ofs, const Certificate& x509, const std::map<std::string_view,std::string_view>& lbl_parts)
158158
{
159-
buildLabel(ofs, "pub_key", {
160-
{"file", file}
161-
});
162-
}
163-
164-
static void
165-
BuildLabelSymmetricKey(std::ostream &ofs, const std::string& label, const std::string& file)
166-
{
167-
buildLabel(ofs, "secret", {
168-
{"label", label},
169-
{"file", file}
170-
});
171-
}
172-
173-
static void
174-
BuildLabelPassword(std::ostream &ofs, const std::string& label)
175-
{
176-
buildLabel(ofs, "pw", {
177-
{"label", label}
159+
buildLabel(ofs, CDoc2::Label::TYPE_CERTIFICATE, lbl_parts, {
160+
{CDoc2::Label::CN, x509.getCommonName()},
161+
{CDoc2::Label::CERT_SHA1, toHex(x509.getDigest())}
178162
});
179163
}
180164

@@ -183,37 +167,42 @@ Recipient::getLabel(const std::vector<std::pair<std::string_view, std::string_vi
183167
{
184168
LOG_DBG("Generating label");
185169
if (!label.empty()) return label;
170+
std::map<std::string_view,std::string_view> parts;
171+
for (const auto& [key, value] : lbl_parts) {
172+
if (!value.empty())
173+
parts[key] = value;
174+
}
175+
for (const auto& [key, value] : extra) {
176+
if (!value.empty())
177+
parts[key] = value;
178+
}
186179
std::ostringstream ofs;
187180
switch(type) {
188181
case NONE:
189182
LOG_DBG("The recipient is not initialized");
190183
break;
191184
case SYMMETRIC_KEY:
192185
if (kdf_iter > 0) {
193-
BuildLabelPassword(ofs, key_name);
186+
buildLabel(ofs, CDoc2::Label::TYPE_PASSWORD, parts, {});
194187
} else {
195-
BuildLabelSymmetricKey(ofs, key_name, file_name);
188+
buildLabel(ofs, CDoc2::Label::TYPE_SYMMETRIC, parts, {});
196189
}
197190
break;
198191
case PUBLIC_KEY:
199192
if (!cert.empty()) {
200193
Certificate x509(cert);
201194
if (auto eid = x509.getEIDType(); eid != Certificate::Unknown) {
202-
BuildLabelEID(ofs, eid, x509);
195+
BuildLabelEID(ofs, eid, x509, parts);
203196
} else {
204-
BuildLabelCertificate(ofs, file_name, x509);
197+
BuildLabelCertificate(ofs, x509, parts);
205198
}
206199
} else {
207-
BuildLabelPublicKey(ofs, file_name);
200+
buildLabel(ofs, CDoc2::Label::TYPE_PUBLIC_KEY, parts, {});
208201
}
209202
break;
210203
case KEYSHARE:
211204
break;
212205
}
213-
for (const auto& [key, value] : extra) {
214-
if (!value.empty())
215-
ofs << '&' << urlEncode(key) << '=' << urlEncode(value);
216-
}
217206
LOG_DBG("Generated label: {}", ofs.str());
218207
return ofs.str();
219208
}

cdoc/Recipient.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <string>
2525
#include <vector>
26+
#include <map>
2627
#include <cstdint>
2728

2829
namespace libcdoc {
@@ -108,16 +109,6 @@ struct CDOC_EXPORT Recipient {
108109
*
109110
*/
110111
uint64_t expiry_ts = 0;
111-
/**
112-
* @brief key/certificate filename for machine-readable label
113-
*
114-
*/
115-
std::string file_name;
116-
/**
117-
* @brief public key/password name for machine-readable label
118-
*
119-
*/
120-
std::string key_name;
121112

122113
/**
123114
* @brief test whether the Recipient structure is initialized
@@ -234,9 +225,25 @@ struct CDOC_EXPORT Recipient {
234225
*/
235226
std::string getLabel(const std::vector<std::pair<std::string_view, std::string_view>> &extra) const;
236227

228+
/**
229+
* @brief Set a property for automatic label generation
230+
*
231+
* @param key the property name
232+
* @param value the property value
233+
*/
234+
void setLabelValue(std::string_view key, std::string_view value) {
235+
if (!value.empty()) {
236+
lbl_parts[std::string(key)] = value;
237+
} else {
238+
lbl_parts.erase(std::string(key));
239+
}
240+
}
241+
237242
bool operator== (const Recipient& other) const = default;
238243
protected:
239244
Recipient(Type _type) : type(_type) {};
245+
private:
246+
std::map<std::string,std::string> lbl_parts;
240247
};
241248

242249
} // namespace libcdoc

libcdoc.i

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
%ignore libcdoc::FileListConsumer;
5252
%ignore libcdoc::FileListSource;
5353

54+
// Ignore until there is straightfoward string_view translation
55+
%ignore libcdoc::CDoc2;
56+
5457
%ignore libcdoc::CDocWriter::createWriter(int version, DataConsumer *dst, bool take_ownership, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network);
5558
%ignore libcdoc::CDocWriter::createWriter(int version, std::ostream& ofs, Configuration *conf, CryptoBackend *crypto, NetworkBackend *network);
5659
%ignore libcdoc::CDocWriter::encrypt(MultiDataSource& src, const std::vector<libcdoc::Recipient>& recipients);

0 commit comments

Comments
 (0)