1818
1919#include " CDoc1Writer.h"
2020
21+ #include " Certificate.h"
2122#include " Crypto.h"
2223#include " DDocWriter.h"
2324#include " Recipient.h"
@@ -119,27 +120,10 @@ int64_t CDoc1Writer::Private::writeDocument(bool use_ddoc, const std::vector<Rec
119120int64_t CDoc1Writer::Private::writeRecipient (const std::vector<uint8_t > &recipient, const Crypto::Key& transportKey)
120121{
121122 auto peerCert = Crypto::toX509 (recipient);
122- if (!peerCert)
123+ if (!peerCert)
123124 return UNSPECIFIED_ERROR;
124- std::string cn = [&]{
125- std::string cn;
126- X509_NAME *name = X509_get_subject_name (peerCert.get ());
127- if (!name)
128- return cn;
129- int pos = X509_NAME_get_index_by_NID (name, NID_commonName, 0 );
130- if (pos == -1 )
131- return cn;
132- X509_NAME_ENTRY *e = X509_NAME_get_entry (name, pos);
133- if (!e)
134- return cn;
135- char *data = nullptr ;
136- int size = ASN1_STRING_to_UTF8 ((uint8_t **)&data, X509_NAME_ENTRY_get_data (e));
137-
138- cn.assign (data, size_t (size));
139- OPENSSL_free (data);
140- return cn;
141- }();
142- return writeElement (DENC, " EncryptedKey" , {{" Recipient" , cn}}, [&] -> int64_t {
125+ return writeElement (DENC, " EncryptedKey" ,
126+ {{" Recipient" , Certificate::getName (peerCert.get (), NID_commonName)}}, [&] -> int64_t {
143127 std::vector<uint8_t > encryptedData;
144128 auto *peerPKey = X509_get0_pubkey (peerCert.get ());
145129 switch (EVP_PKEY_base_id (peerPKey))
@@ -169,18 +153,19 @@ int64_t CDoc1Writer::Private::writeRecipient(const std::vector<uint8_t> &recipie
169153 oid.resize (size_t (OBJ_obj2txt (oid.data (), int (oid.size ()), obj.get (), 1 )));
170154 std::vector<uint8_t > SsDer = Crypto::toPublicKeyDer (ephKey.get ());
171155
172- std::string encryptionMethod (libcdoc::Crypto::KWAES256_MTH);
173- std::string concatDigest = libcdoc::Crypto::SHA384_MTH;
174- switch ((SsDer.size () - 1 ) / 2 ) {
175- case 32 : concatDigest = libcdoc::Crypto::SHA256_MTH; break ;
176- case 48 : concatDigest = libcdoc::Crypto::SHA384_MTH; break ;
177- default : concatDigest = libcdoc::Crypto::SHA512_MTH; break ;
178- }
156+ std::string encryptionMethod (libcdoc::Crypto::KWAES256_MTH);
157+ const std::string &concatDigest = [&] -> const std::string & {
158+ switch ((SsDer.size () - 1 ) / 2 ) {
159+ case 32 : return libcdoc::Crypto::SHA256_MTH; break ;
160+ case 48 : return libcdoc::Crypto::SHA384_MTH; break ;
161+ default : return libcdoc::Crypto::SHA512_MTH; break ;
162+ }
163+ }();
179164
180- std::vector<uint8_t > AlgorithmID (documentFormat.cbegin (), documentFormat.cend ());
181- std::vector<uint8_t > encryptionKey = libcdoc::Crypto::concatKDF (concatDigest, libcdoc::Crypto::keySize (encryptionMethod), sharedSecret ,
182- AlgorithmID, SsDer, recipient);
183- encryptedData = libcdoc::Crypto::AESWrap (encryptionKey, transportKey.key , true );
165+ std::vector<uint8_t > AlgorithmID (documentFormat.cbegin (), documentFormat.cend ());
166+ std::vector<uint8_t > encryptionKey = libcdoc::Crypto::concatKDF (concatDigest,
167+ libcdoc::Crypto::keySize (encryptionMethod), sharedSecret, AlgorithmID, SsDer, recipient);
168+ encryptedData = libcdoc::Crypto::AESWrap (encryptionKey, transportKey.key , true );
184169
185170 LOG_TRACE_KEY (" Ss {}" , SsDer);
186171 LOG_TRACE_KEY (" Ksr {}" , sharedSecret);
@@ -197,25 +182,25 @@ int64_t CDoc1Writer::Private::writeRecipient(const std::vector<uint8_t> &recipie
197182 {" PartyUInfo" , " 00" + toHex (SsDer)},
198183 {" PartyVInfo" , " 00" + toHex (recipient)}}, [&] {
199184 return writeElement (DS, " DigestMethod" , {{" Algorithm" , concatDigest}});
200- });
185+ });
201186 }));
202187 RET_ERROR (writeElement (DENC, " OriginatorKeyInfo" , [&] {
203188 return writeElement (DS, " KeyValue" , [&] {
204189 return writeElement (DSIG11, " ECKeyValue" , [&] {
205190 RET_ERROR (writeElement (DSIG11, " NamedCurve" , {{" URI" , " urn:oid:" + oid}}));
206191 return writeBase64Element (DSIG11, " PublicKey" , SsDer);
207- });
208- });
192+ });
193+ });
209194 }));
210195 return writeElement (DENC, " RecipientKeyInfo" , [&] {
211196 return writeElement (DS, " X509Data" , [&] {
212197 return writeBase64Element (DS, " X509Certificate" , recipient);
213- });
214- });
215- });
198+ });
199+ });
200+ });
216201 }));
217- break ;
218- }
202+ break ;
203+ }
219204 default :
220205 return UNSPECIFIED_ERROR;
221206 }
@@ -224,8 +209,8 @@ int64_t CDoc1Writer::Private::writeRecipient(const std::vector<uint8_t> &recipie
224209 return UNSPECIFIED_ERROR;
225210 return writeElement (DENC, " CipherData" , [&] {
226211 return writeBase64Element (DENC, " CipherValue" , encryptedData);
227- });
228- });
212+ });
213+ });
229214}
230215
231216/* *
0 commit comments