2424#include " ILogger.h"
2525#include " Utils.h"
2626
27+ #include < algorithm>
28+ #include < chrono>
29+
2730using namespace std ;
2831
2932namespace libcdoc {
@@ -58,9 +61,10 @@ Recipient::makeSymmetric(std::string label, int32_t kdf_iter)
5861}
5962
6063Recipient
61- Recipient::makePublicKey (std::string label, const std::vector<uint8_t >& public_key, PKType pk_type)
64+ Recipient::makePublicKey (std::string label, std::vector<uint8_t > public_key, PKType pk_type)
6265{
63- if (public_key.empty ()) return Recipient (Type::NONE);
66+ if (public_key.empty ())
67+ return {Type::NONE};
6468 Recipient rcpt (Type::PUBLIC_KEY);
6569 rcpt.label = std::move (label);
6670 rcpt.pk_type = pk_type;
@@ -69,18 +73,20 @@ Recipient::makePublicKey(std::string label, const std::vector<uint8_t>& public_k
6973 auto evp = Crypto::fromECPublicKeyDer (public_key);
7074 rcpt.rcpt_key = Crypto::toPublicKeyDer (evp.get ());
7175 } else {
72- rcpt.rcpt_key = public_key;
76+ rcpt.rcpt_key = std::move ( public_key) ;
7377 }
7478 return rcpt;
7579}
7680
7781Recipient
7882Recipient::makeCertificate (std::string label, std::vector<uint8_t > cert)
7983{
84+ Certificate x509 (cert);
85+ if (!x509.cert )
86+ return {Type::NONE};
8087 Recipient rcpt (Type::PUBLIC_KEY);
8188 rcpt.label = std::move (label);
8289 rcpt.cert = std::move (cert);
83- Certificate x509 (rcpt.cert );
8490 rcpt.rcpt_key = x509.getPublicKey ();
8591 rcpt.pk_type = (x509.getAlgorithm () == libcdoc::Certificate::RSA) ? PKType::RSA : PKType::ECC;
8692 rcpt.expiry_ts = x509.getNotAfter ();
@@ -90,36 +96,32 @@ Recipient::makeCertificate(std::string label, std::vector<uint8_t> cert)
9096Recipient
9197Recipient::makeServer (std::string label, std::vector<uint8_t > public_key, PKType pk_type, std::string server_id)
9298{
93- Recipient rcpt (Type::PUBLIC_KEY);
94- rcpt.label = std::move (label);
95- rcpt.pk_type = pk_type;
96- if (pk_type == PKType::ECC && public_key[0 ] == 0x30 ) {
97- // 0x30 identifies SEQUENCE tag in ASN.1 encoding
98- auto evp = Crypto::fromECPublicKeyDer (public_key);
99- rcpt.rcpt_key = Crypto::toPublicKeyDer (evp.get ());
100- } else {
101- rcpt.rcpt_key = std::move (public_key);
102- }
99+ Recipient rcpt = makePublicKey (std::move (label), std::move (public_key), pk_type);
103100 rcpt.server_id = std::move (server_id);
101+ const auto six_months_from_now = std::chrono::system_clock::now () + std::chrono::months (6 );
102+ const auto expiry_ts = std::chrono::system_clock::to_time_t (six_months_from_now);
103+ rcpt.expiry_ts = uint64_t (expiry_ts);
104104 return rcpt;
105105}
106106
107107Recipient
108108Recipient::makeServer (std::string label, std::vector<uint8_t > cert, std::string server_id)
109109{
110- Certificate x509 (cert);
111- Recipient rcpt = makeServer (std::move (label), x509.getPublicKey (), x509.getAlgorithm () == Certificate::Algorithm::RSA ? RSA : ECC, std::move (server_id));
112- rcpt.cert = cert;
113- return std::move (rcpt);
110+ Recipient rcpt = makeCertificate (std::move (label), std::move (cert));
111+ rcpt.server_id = std::move (server_id);
112+ const auto six_months_from_now = std::chrono::system_clock::now () + std::chrono::months (6 );
113+ const auto expiry_ts = std::chrono::system_clock::to_time_t (six_months_from_now);
114+ rcpt.expiry_ts = std::min (rcpt.expiry_ts , uint64_t (expiry_ts));
115+ return rcpt;
114116}
115117
116118Recipient
117- Recipient::makeShare (const std::string& label, const std::string& server_id, const std::string& recipient_id)
119+ Recipient::makeShare (std::string label, std::string server_id, std::string recipient_id)
118120{
119121 Recipient rcpt (Type::KEYSHARE);
120- rcpt.label = label;
121- rcpt.server_id = server_id;
122- rcpt.id = recipient_id;
122+ rcpt.label = std::move ( label) ;
123+ rcpt.server_id = std::move ( server_id) ;
124+ rcpt.id = std::move ( recipient_id) ;
123125 return rcpt;
124126}
125127
@@ -139,54 +141,20 @@ Recipient::isTheSameRecipient(const std::vector<uint8_t>& public_key) const
139141 return rcpt_key == public_key;
140142}
141143
142- static Recipient::EIDType
143- getEIDType (const std::vector<std::string>& policies)
144- {
145- for (const auto & pol : policies)
146- {
147- std::string_view policy = pol;
148- if (policy.starts_with (" 2.999." )) { // Zetes TEST OID prefix
149- policy = policy.substr (6 );
150- }
151-
152- if (policy.starts_with (" 1.3.6.1.4.1.51361.1.1.3" ) ||
153- policy.starts_with (" 1.3.6.1.4.1.51361.1.2.3" )) {
154- return Recipient::EIDType::DigiID;
155- }
156-
157- if (policy.starts_with (" 1.3.6.1.4.1.51361.1.1.4" ) ||
158- policy.starts_with (" 1.3.6.1.4.1.51361.1.2.4" )) {
159- return Recipient::EIDType::DigiID_EResident;
160- }
161-
162- if (policy.starts_with (" 1.3.6.1.4.1.51361.1.1" ) ||
163- policy.starts_with (" 1.3.6.1.4.1.51455.1.1" ) ||
164- policy.starts_with (" 1.3.6.1.4.1.51361.1.2" ) ||
165- policy.starts_with (" 1.3.6.1.4.1.51455.1.2" )) {
166- return Recipient::EIDType::IDCard;
167- }
168- }
169-
170- // If the execution reaches so far then EID type determination failed.
171- return Recipient::EIDType::Unknown;
172- }
173-
174144static void
175- buildLabel (std::ostream& ofs, std::string_view type, const std::initializer_list<std::pair<std::string_view, std::string_view>> & components)
145+ buildLabel (std::ostream& ofs, std::string_view type, std::initializer_list<std::pair<std::string_view, std::string_view>> components)
176146{
177147 ofs << LABELPREFIX;
178148 ofs << " v" << ' =' << std::to_string (CDoc2::KEYLABELVERSION) << ' &'
179149 << " type" << ' =' << type;
180- for (auto & [key, value] : components) {
181- if (value.empty ())
182- continue ;
183- ofs << ' &' ;
184- ofs << urlEncode (key) << ' =' << urlEncode (value);
150+ for (const auto & [key, value] : components) {
151+ if (!value.empty ())
152+ ofs << ' &' << urlEncode (key) << ' =' << urlEncode (value);
185153 }
186154}
187155
188156static void
189- BuildLabelEID (std::ostream& ofs, Recipient ::EIDType type, const Certificate& x509)
157+ BuildLabelEID (std::ostream& ofs, Certificate ::EIDType type, const Certificate& x509)
190158{
191159 buildLabel (ofs, eid_strs[type], {
192160 {" cn" , x509.getCommonName ()},
@@ -197,7 +165,7 @@ BuildLabelEID(std::ostream& ofs, Recipient::EIDType type, const Certificate& x50
197165}
198166
199167static void
200- BuildLabelCertificate (std::ostream &ofs, std::string_view file, const Certificate& x509)
168+ BuildLabelCertificate (std::ostream &ofs, const std::string& file, const Certificate& x509)
201169{
202170 buildLabel (ofs, " cert" , {
203171 {" file" , file},
@@ -207,15 +175,15 @@ BuildLabelCertificate(std::ostream &ofs, std::string_view file, const Certificat
207175}
208176
209177static void
210- BuildLabelPublicKey (std::ostream &ofs, const std::string file)
178+ BuildLabelPublicKey (std::ostream &ofs, const std::string& file)
211179{
212180 buildLabel (ofs, " pub_key" , {
213181 {" file" , file}
214182 });
215183}
216184
217185static void
218- BuildLabelSymmetricKey (std::ostream &ofs, const std::string& label, const std::string file)
186+ BuildLabelSymmetricKey (std::ostream &ofs, const std::string& label, const std::string& file)
219187{
220188 buildLabel (ofs, " secret" , {
221189 {" label" , label},
@@ -251,8 +219,8 @@ Recipient::getLabel(const std::vector<std::pair<std::string_view, std::string_vi
251219 case PUBLIC_KEY:
252220 if (!cert.empty ()) {
253221 Certificate x509 (cert);
254- if (auto type = getEIDType ( x509.policies ()); type != EIDType ::Unknown) {
255- BuildLabelEID (ofs, type , x509);
222+ if (auto eid = x509.getEIDType (); eid != Certificate ::Unknown) {
223+ BuildLabelEID (ofs, eid , x509);
256224 } else {
257225 BuildLabelCertificate (ofs, file_name, x509);
258226 }
@@ -263,11 +231,9 @@ Recipient::getLabel(const std::vector<std::pair<std::string_view, std::string_vi
263231 case KEYSHARE:
264232 break ;
265233 }
266- for (auto & [key, value] : extra) {
267- if (value.empty ())
268- continue ;
269- ofs << ' &' ;
270- ofs << urlEncode (key) << ' =' << urlEncode (value);
234+ for (const auto & [key, value] : extra) {
235+ if (!value.empty ())
236+ ofs << ' &' << urlEncode (key) << ' =' << urlEncode (value);
271237 }
272238 LOG_DBG (" Generated label: {}" , ofs.str ());
273239 return ofs.str ();
0 commit comments