@@ -121,60 +121,44 @@ Recipient::isTheSameRecipient(const std::vector<uint8_t>& public_key) const
121121}
122122
123123static 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
135144static 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
156156static 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}
0 commit comments