Skip to content

Commit 6c2f027

Browse files
author
Lauris Kaplinski
committed
Merge commit '40c6fe0d026ca10fc83bd8bf6691f38c6618cdf2' into installer
2 parents ff0f88d + 40c6fe0 commit 6c2f027

File tree

10 files changed

+83
-98
lines changed

10 files changed

+83
-98
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if(LIBCDOC_WITH_DOCS)
6464
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
6565
COMMENT "Generating API documentation with Doxygen" VERBATIM
6666
)
67-
install(DIRECTORY ${CMAKE_BINARY_DIR}/doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
67+
install(DIRECTORY ${PROJECT_BINARY_DIR}/doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
6868
install(DIRECTORY doc/ DESTINATION ${CMAKE_INSTALL_DOCDIR})
6969
endif()
7070
endif()

cdoc/CDoc1Writer.cpp

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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
119120
int64_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
/**

cdoc/CDocCipher.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "CDocWriter.h"
2424
#include "RcptInfo.h"
2525
#include "ToolConf.h"
26+
#include "Exports.h"
2627

2728
#include <map>
2829
#include <memory>
@@ -31,7 +32,7 @@ namespace libcdoc
3132
{
3233

3334

34-
class CDocCipher
35+
class CDOC_EXPORT CDocCipher
3536
{
3637
public:
3738
CDocCipher() = default;

cdoc/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_library(cdoc
6161
CDoc2.h
6262
Wrapper.h
6363
utils/memory.h
64+
CDocCipher.cpp CDocCipher.h
6465
)
6566
set_target_properties(cdoc PROPERTIES
6667
VERSION ${PROJECT_VERSION}
@@ -92,9 +93,9 @@ target_link_libraries(cdoc PRIVATE
9293
)
9394

9495
if(BUILD_TOOLS)
95-
add_executable(cdoc-tool cdoc-tool.cpp CDocCipher.cpp)
96+
add_executable(cdoc-tool cdoc-tool.cpp)
9697
target_include_directories(cdoc-tool PRIVATE ${OPENSSL_INCLUDE_DIR})
97-
target_link_libraries(cdoc-tool cdoc_ver cdoc OpenSSL::SSL)
98+
target_link_libraries(cdoc-tool cdoc_ver cdoc)
9899
set_target_properties(cdoc-tool PROPERTIES
99100
INSTALL_RPATH $<$<PLATFORM_ID:Darwin>:@executable_path/../lib>
100101
)
@@ -191,7 +192,7 @@ if(APPLE)
191192
list(TRANSFORM MODULE_LIST PREPEND " header \"")
192193
list(TRANSFORM MODULE_LIST APPEND "\"")
193194
list(JOIN MODULE_LIST "\n" MODULE_LIST)
194-
set(MODULE_MAP ${CMAKE_BINARY_DIR}/module.modulemap)
195+
set(MODULE_MAP ${PROJECT_BINARY_DIR}/module.modulemap)
195196
file(WRITE ${MODULE_MAP} "framework module cdoc {
196197
${MODULE_LIST}
197198
export *

cdoc/Certificate.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ Certificate::Certificate(const std::vector<uint8_t>& cert)
2828
{
2929
}
3030

31-
static std::string
32-
getName(const unique_free_t<X509>& cert, int NID)
31+
std::string Certificate::getName(X509 *cert, int NID)
3332
{
3433
std::string cn;
3534
if(!cert)
3635
return cn;
37-
X509_NAME *name = X509_get_subject_name(cert.get());
36+
X509_NAME *name = X509_get_subject_name(cert);
3837
if(!name)
3938
return cn;
4039
int pos = X509_NAME_get_index_by_NID(name, NID, -1);
@@ -44,34 +43,34 @@ getName(const unique_free_t<X509>& cert, int NID)
4443
if(!e)
4544
return cn;
4645
char *data = nullptr;
47-
int size = ASN1_STRING_to_UTF8((uint8_t**)&data, X509_NAME_ENTRY_get_data(e));
48-
cn.assign(data, size_t(size));
46+
if(int size = ASN1_STRING_to_UTF8((uint8_t**)&data, X509_NAME_ENTRY_get_data(e)); size > 0)
47+
cn.assign(data, size_t(size));
4948
OPENSSL_free(data);
5049
return cn;
5150
}
5251

5352
std::string
5453
Certificate::getCommonName() const
5554
{
56-
return getName(cert, NID_commonName);
55+
return getName(cert.get(), NID_commonName);
5756
}
5857

5958
std::string
6059
Certificate::getGivenName() const
6160
{
62-
return getName(cert, NID_givenName);
61+
return getName(cert.get(), NID_givenName);
6362
}
6463

6564
std::string
6665
Certificate::getSurname() const
6766
{
68-
return getName(cert, NID_surname);
67+
return getName(cert.get(), NID_surname);
6968
}
7069

7170
std::string
7271
Certificate::getSerialNumber() const
7372
{
74-
return getName(cert, NID_serialNumber);
73+
return getName(cert.get(), NID_serialNumber);
7574
}
7675

7776
time_t

cdoc/Certificate.h

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

22-
#include "Exports.h"
23-
2422
#include "utils/memory.h"
2523

2624
#include <string>
@@ -48,9 +46,10 @@ class Certificate {
4846

4947
explicit Certificate(const std::vector<uint8_t>& cert);
5048

51-
std::string getCommonName() const;
52-
std::string getGivenName() const;
53-
std::string getSurname() const;
49+
static std::string getName(X509 *cert, int NID);
50+
std::string getCommonName() const;
51+
std::string getGivenName() const;
52+
std::string getSurname() const;
5453
std::string getSerialNumber() const;
5554

5655
EIDType getEIDType() const;

cdoc/XmlWriter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ XMLWriter::~XMLWriter() noexcept
4545
xmlTextWriterEndDocument(w.get());
4646
}
4747

48-
int64_t XMLWriter::writeStartElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr)
48+
int64_t XMLWriter::writeStartElement(NS ns, const char *name, const std::map<const char *, std::string> &attr)
4949
{
5050
if(!w)
5151
return WRONG_ARGUMENTS;
5252
auto &count = nsmap[ns.prefix ? ns.prefix : std::string_view{}];
5353
count++;
54-
if(xmlTextWriterStartElementNS(w.get(), pcxmlChar(ns.prefix), pcxmlChar(name.c_str()), count > 1 ? nullptr : pcxmlChar(ns.ns)) == -1)
54+
if(xmlTextWriterStartElementNS(w.get(), pcxmlChar(ns.prefix), pcxmlChar(name), count > 1 ? nullptr : pcxmlChar(ns.ns)) == -1)
5555
return IO_ERROR;
5656
for(const auto &[name, content]: attr)
5757
{
58-
if(xmlTextWriterWriteAttribute(w.get(), pcxmlChar(name.c_str()), pcxmlChar(content.c_str())) == -1)
58+
if(xmlTextWriterWriteAttribute(w.get(), pcxmlChar(name), pcxmlChar(content.c_str())) == -1)
5959
return IO_ERROR;
6060
}
6161
return OK;
@@ -72,7 +72,7 @@ int64_t XMLWriter::writeEndElement(NS ns)
7272
return OK;
7373
}
7474

75-
int64_t XMLWriter::writeElement(NS ns, const std::string &name, const std::function<int64_t()> &f)
75+
int64_t XMLWriter::writeElement(NS ns, const char *name, const std::function<int64_t()> &f)
7676
{
7777
if(auto rv = writeStartElement(ns, name, {}); rv != OK)
7878
return rv;
@@ -81,7 +81,7 @@ int64_t XMLWriter::writeElement(NS ns, const std::string &name, const std::funct
8181
return writeEndElement(ns);
8282
}
8383

84-
int64_t XMLWriter::writeElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<int64_t()> &f)
84+
int64_t XMLWriter::writeElement(NS ns, const char *name, const std::map<const char *, std::string> &attr, const std::function<int64_t()> &f)
8585
{
8686
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
8787
return rv;
@@ -90,7 +90,7 @@ int64_t XMLWriter::writeElement(NS ns, const std::string &name, const std::map<s
9090
return writeEndElement(ns);
9191
}
9292

93-
int64_t XMLWriter::writeBase64Element(NS ns, const std::string &name, const std::function<int64_t(DataConsumer&)> &f, const std::map<std::string, std::string> &attr)
93+
int64_t XMLWriter::writeBase64Element(NS ns, const char *name, const std::function<int64_t(DataConsumer&)> &f, const std::map<const char *, std::string> &attr)
9494
{
9595
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
9696
return rv;
@@ -150,7 +150,7 @@ int64_t XMLWriter::writeBase64Element(NS ns, const std::string &name, const std:
150150
return writeEndElement(ns);
151151
}
152152

153-
int64_t XMLWriter::writeBase64Element(NS ns, const std::string &name, const std::vector<xmlChar> &data, const std::map<std::string, std::string> &attr)
153+
int64_t XMLWriter::writeBase64Element(NS ns, const char *name, const std::vector<xmlChar> &data, const std::map<const char *, std::string> &attr)
154154
{
155155
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
156156
return rv;
@@ -159,7 +159,7 @@ int64_t XMLWriter::writeBase64Element(NS ns, const std::string &name, const std:
159159
return writeEndElement(ns);
160160
}
161161

162-
int64_t XMLWriter::writeTextElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data)
162+
int64_t XMLWriter::writeTextElement(NS ns, const char *name, const std::map<const char *, std::string> &attr, const std::string &data)
163163
{
164164
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
165165
return rv;

cdoc/XmlWriter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class XMLWriter
3939
XMLWriter(DataConsumer &dst);
4040
virtual ~XMLWriter() noexcept;
4141

42-
int64_t writeStartElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr);
42+
int64_t writeStartElement(NS ns, const char *name, const std::map<const char *, std::string> &attr);
4343
int64_t writeEndElement(NS ns);
44-
int64_t writeElement(NS ns, const std::string &name, const std::function<int64_t()> &f = nullptr);
45-
int64_t writeElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<int64_t()> &f = nullptr);
46-
int64_t writeBase64Element(NS ns, const std::string &name, const std::function<int64_t(DataConsumer &)> &f, const std::map<std::string, std::string> &attr = {});
47-
int64_t writeBase64Element(NS ns, const std::string &name, const std::vector<unsigned char> &data, const std::map<std::string, std::string> &attr = {});
48-
int64_t writeTextElement(NS ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data);
44+
int64_t writeElement(NS ns, const char *name, const std::function<int64_t()> &f = nullptr);
45+
int64_t writeElement(NS ns, const char *name, const std::map<const char *, std::string> &attr, const std::function<int64_t()> &f = nullptr);
46+
int64_t writeBase64Element(NS ns, const char *name, const std::function<int64_t(DataConsumer &)> &f, const std::map<const char *, std::string> &attr = {});
47+
int64_t writeBase64Element(NS ns, const char *name, const std::vector<unsigned char> &data, const std::map<const char *, std::string> &attr = {});
48+
int64_t writeTextElement(NS ns, const char *name, const std::map<const char *, std::string> &attr, const std::string &data);
4949

5050
private:
5151
unique_free_t<_xmlTextWriter> w;

0 commit comments

Comments
 (0)