@@ -49,20 +49,20 @@ static constexpr std::string_view eid_strs[] = {
4949};
5050
5151Recipient
52- Recipient::makeSymmetric (const std::string& label, int32_t kdf_iter)
52+ Recipient::makeSymmetric (std::string label, int32_t kdf_iter)
5353{
5454 Recipient rcpt (Type::SYMMETRIC_KEY);
55- rcpt.label = label;
55+ rcpt.label = std::move ( label) ;
5656 rcpt.kdf_iter = kdf_iter;
5757 return rcpt;
5858}
5959
6060Recipient
61- Recipient::makePublicKey (const std::string& label, const std::vector<uint8_t >& public_key, PKType pk_type)
61+ Recipient::makePublicKey (std::string label, const std::vector<uint8_t >& public_key, PKType pk_type)
6262{
6363 if (public_key.empty ()) return Recipient (Type::NONE);
6464 Recipient rcpt (Type::PUBLIC_KEY);
65- rcpt.label = label;
65+ rcpt.label = std::move ( label) ;
6666 rcpt.pk_type = pk_type;
6767 if (pk_type == PKType::ECC && public_key[0 ] == 0x30 ) {
6868 // 0x30 identifies SEQUENCE tag in ASN.1 encoding
@@ -77,13 +77,14 @@ Recipient::makePublicKey(const std::string& label, const std::vector<uint8_t>& p
7777Recipient
7878Recipient::makeCertificate (std::string label, std::vector<uint8_t > cert)
7979{
80- Recipient rcpt (Type::PUBLIC_KEY);
81- rcpt.label = std::move (label);
80+ Recipient rcpt (Type::PUBLIC_KEY);
81+ rcpt.label = std::move (label);
8282 rcpt.cert = std::move (cert);
83- Certificate ssl (rcpt.cert );
84- rcpt.rcpt_key = ssl.getPublicKey ();
85- rcpt.pk_type = (ssl.getAlgorithm () == libcdoc::Certificate::RSA) ? PKType::RSA : PKType::ECC;
86- return rcpt;
83+ Certificate x509 (rcpt.cert );
84+ rcpt.rcpt_key = x509.getPublicKey ();
85+ rcpt.pk_type = (x509.getAlgorithm () == libcdoc::Certificate::RSA) ? PKType::RSA : PKType::ECC;
86+ rcpt.expiry_ts = x509.getNotAfter ();
87+ return rcpt;
8788}
8889
8990Recipient
@@ -138,26 +139,10 @@ Recipient::isTheSameRecipient(const std::vector<uint8_t>& public_key) const
138139 return rcpt_key == public_key;
139140}
140141
141- static std::string
142- buildLabel (std::vector<std::pair<std::string_view, std::string_view>> components)
143- {
144- std::ostringstream ofs;
145- ofs << LABELPREFIX;
146- bool first = true ;
147- for (auto & [key, value] : components) {
148- if (!value.empty ()) {
149- if (!first) ofs << ' &' ;
150- ofs << libcdoc::urlEncode (key) << ' =' << libcdoc::urlEncode (value);
151- first = false ;
152- }
153- }
154- return ofs.str ();
155- }
156-
157142static Recipient::EIDType
158143getEIDType (const std::vector<std::string>& policies)
159144{
160- for (std::vector<std::string>::const_reference policy : policies)
145+ for (const auto & policy : policies)
161146 {
162147 if (policy.starts_with (" 1.3.6.1.4.1.51361.1.1.3" ) ||
163148 policy.starts_with (" 1.3.6.1.4.1.51361.1.2.3" )) {
@@ -181,113 +166,103 @@ getEIDType(const std::vector<std::string>& policies)
181166 return Recipient::EIDType::Unknown;
182167}
183168
184- static std::string
185- BuildLabelEID ( const std::vector< uint8_t >& cert )
169+ static void
170+ buildLabel (std::ostream& ofs, std::string_view type, const std::initializer_list<std::pair<std::string_view, std::string_view>> &components )
186171{
187- Certificate x509 (cert);
188- Recipient::EIDType type = getEIDType (x509.policies ());
189- std::string cn = x509.getCommonName ();
190- std::string sn = x509.getSerialNumber ();
191- std::string gn = x509.getGivenName ();
192- if (!gn.empty ()) {
193- return buildLabel ({
194- {" v" , std::to_string (CDoc2::KEYLABELVERSION)},
195- {" type" , eid_strs[type]},
196- {" cn" , cn},
197- {" serial_number" , sn}
198- });
199- } else {
200- return buildLabel ({
201- {" v" , std::to_string (CDoc2::KEYLABELVERSION)},
202- {" type" , eid_strs[type]},
203- {" cn" , cn},
204- {" serial_number" , sn},
205- {" last_name" , x509.getSurname ()},
206- {" first_name" , gn}
207- });
172+ ofs << LABELPREFIX;
173+ ofs << " v" << ' =' << std::to_string (CDoc2::KEYLABELVERSION) << ' &'
174+ << " type" << ' =' << type;
175+ for (auto & [key, value] : components) {
176+ if (value.empty ())
177+ continue ;
178+ ofs << ' &' ;
179+ ofs << urlEncode (key) << ' =' << urlEncode (value);
208180 }
209181}
210182
211- static std::string
212- BuildLabelCertificate (std::string_view file, const std::vector< uint8_t >& cert )
183+ static void
184+ BuildLabelEID (std::ostream& ofs, Recipient::EIDType type, const Certificate& x509 )
213185{
214- Certificate x509 (cert);
215- return buildLabel ({
216- {" v" , std::to_string (CDoc2::KEYLABELVERSION)},
217- {" type" , " cert" },
186+ buildLabel (ofs, eid_strs[type], {
187+ {" cn" , x509.getCommonName ()},
188+ {" serial_number" , x509.getSerialNumber ()},
189+ {" last_name" , x509.getSurname ()},
190+ {" first_name" , x509.getGivenName ()},
191+ });
192+ }
193+
194+ static void
195+ BuildLabelCertificate (std::ostream &ofs, std::string_view file, const Certificate& x509)
196+ {
197+ buildLabel (ofs, " cert" , {
218198 {" file" , file},
219199 {" cn" , x509.getCommonName ()},
220200 {" cert_sha1" , toHex (x509.getDigest ())}
221201 });
222202}
223203
224- static std::string
225- BuildLabelPublicKey (int version , const std::string file)
204+ static void
205+ BuildLabelPublicKey (std::ostream &ofs , const std::string file)
226206{
227- return buildLabel ({
228- {" v" , std::to_string (version)},
229- {" type" , " pub_key" },
207+ buildLabel (ofs, " pub_key" , {
230208 {" file" , file}
231209 });
232210}
233211
234- static std::string
235- BuildLabelSymmetricKey (int version , const std::string& label, const std::string file)
212+ static void
213+ BuildLabelSymmetricKey (std::ostream &ofs , const std::string& label, const std::string file)
236214{
237- return buildLabel ({
238- {" v" , std::to_string (version)},
239- {" type" , " secret" },
215+ buildLabel (ofs, " secret" , {
240216 {" label" , label},
241217 {" file" , file}
242218 });
243219}
244220
245- static std::string
246- BuildLabelPassword (int version , const std::string& label)
221+ static void
222+ BuildLabelPassword (std::ostream &ofs , const std::string& label)
247223{
248- return buildLabel ({
249- {" v" , std::to_string (version)},
250- {" type" , " pw" },
224+ buildLabel (ofs, " pw" , {
251225 {" label" , label}
252226 });
253227}
254228
255229std::string
256- Recipient::getLabel (std::vector<std::pair<std::string_view, std::string_view>> extra) const
230+ Recipient::getLabel (const std::vector<std::pair<std::string_view, std::string_view>> & extra) const
257231{
258232 LOG_DBG (" Generating label" );
259233 if (!label.empty ()) return label;
260234 std::ostringstream ofs;
261235 switch (type) {
262- case NONE:
236+ case NONE:
263237 LOG_DBG (" The recipient is not initialized" );
264238 break ;
265239 case SYMMETRIC_KEY:
266240 if (kdf_iter > 0 ) {
267- ofs << BuildLabelPassword (CDoc2::KEYLABELVERSION , key_name);
241+ BuildLabelPassword (ofs , key_name);
268242 } else {
269- ofs << BuildLabelSymmetricKey (CDoc2::KEYLABELVERSION , key_name, file_name);
243+ BuildLabelSymmetricKey (ofs , key_name, file_name);
270244 }
245+ break ;
271246 case PUBLIC_KEY:
272247 if (!cert.empty ()) {
273248 Certificate x509 (cert);
274- EIDType eid_type = getEIDType (x509.policies ());
275- if (eid_type != EIDType::Unknown) {
276- ofs << BuildLabelEID (cert);
249+ if (auto type = getEIDType (x509.policies ()); type != EIDType::Unknown) {
250+ BuildLabelEID (ofs, type, x509);
277251 } else {
278- ofs << BuildLabelCertificate (file_name, cert );
252+ BuildLabelCertificate (ofs, file_name, x509 );
279253 }
280254 } else {
281- ofs << BuildLabelPublicKey (CDoc2::KEYLABELVERSION , file_name);
255+ BuildLabelPublicKey (ofs , file_name);
282256 }
257+ break ;
283258 case KEYSHARE:
284259 break ;
285260 }
286261 for (auto & [key, value] : extra) {
287- if (! value.empty ()) {
288- ofs << ' & ' ;
289- ofs << libcdoc::urlEncode (key) << ' = ' << libcdoc::urlEncode (value) ;
290- }
262+ if (value.empty ())
263+ continue ;
264+ ofs << ' & ' ;
265+ ofs << urlEncode (key) << ' = ' << urlEncode (value);
291266 }
292267 LOG_DBG (" Generated label: {}" , ofs.str ());
293268 return ofs.str ();
0 commit comments