Skip to content

Commit 8a986ea

Browse files
committed
Add expire date to label
Signed-off-by: Raul Metsma <[email protected]>
1 parent e22f090 commit 8a986ea

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

cdoc/Certificate.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ Certificate::getSerialNumber() const
7474
return getName(cert, NID_serialNumber);
7575
}
7676

77+
time_t
78+
Certificate::getNotAfter() const
79+
{
80+
if(!cert)
81+
return 0;
82+
tm tm{};
83+
if(ASN1_TIME_to_tm(X509_get0_notAfter(cert.get()), &tm) != 1)
84+
return 0;
85+
#ifdef _WIN32
86+
return _mkgmtime(&tm);
87+
#else
88+
return timegm(&tm);
89+
#endif
90+
}
91+
7792

7893

7994
std::vector<std::string>
@@ -85,8 +100,8 @@ Certificate::policies() const
85100
if(!cert)
86101
return list;
87102

88-
auto p = static_cast<CERTIFICATEPOLICIES *>(X509_get_ext_d2i(cert.get(), NID_certificate_policies, nullptr, nullptr));
89-
auto cp = std::unique_ptr<CERTIFICATEPOLICIES,decltype(&CERTIFICATEPOLICIES_free)>(p,CERTIFICATEPOLICIES_free);
103+
auto cp = make_unique_cast<CERTIFICATEPOLICIES_free>(X509_get_ext_d2i(
104+
cert.get(), NID_certificate_policies, nullptr, nullptr));
90105
if(!cp)
91106
return list;
92107

cdoc/Certificate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Certificate {
5050

5151
std::vector<uint8_t> getPublicKey() const;
5252
Algorithm getAlgorithm() const;
53+
time_t getNotAfter() const;
5354

5455
std::vector<uint8_t> getDigest();
5556
};

cdoc/Io.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
namespace libcdoc {
2828

29-
class DataSource;
29+
struct DataSource;
3030

3131
/**
3232
* @brief The DataConsumer class

cdoc/Recipient.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Recipient
104104
Recipient::makeEIDServer(std::vector<uint8_t> cert, std::string server_id)
105105
{
106106
Certificate x509(cert);
107-
auto label = BuildLabelEID(cert);
107+
auto label = BuildLabelEID(cert, time(nullptr) + 60 * 60 * 24 * 31 * 6); // 6 months
108108
return makeServer(std::move(label),
109109
x509.getPublicKey(), x509.getAlgorithm() == Certificate::Algorithm::RSA ? RSA : ECC, std::move(server_id));
110110
}
@@ -143,7 +143,7 @@ static constexpr std::string_view type_strs[] = {
143143
};
144144

145145
std::string
146-
Recipient::buildLabel(std::vector<std::pair<std::string_view, std::string_view>> components)
146+
Recipient::buildLabel(const std::vector<std::pair<std::string_view, std::string_view>> &components, time_t exp)
147147
{
148148
std::ostringstream ofs;
149149
ofs << LABELPREFIX;
@@ -155,11 +155,14 @@ Recipient::buildLabel(std::vector<std::pair<std::string_view, std::string_view>>
155155
first = false;
156156
}
157157
}
158+
if(exp > 0) {
159+
ofs << "&server_exp=" << exp;
160+
}
158161
return ofs.str();
159162
}
160163

161164
std::string
162-
Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::string_view serial_number, std::string_view last_name, std::string_view first_name)
165+
Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::string_view serial_number, std::string_view last_name, std::string_view first_name, time_t exp)
163166
{
164167
// In case of cards issued to an organization the first name (and last name) are missing. We ommit these parts.
165168
if (first_name.empty()) {
@@ -168,7 +171,7 @@ Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::st
168171
{"type", type_strs[type]},
169172
{"cn", cn},
170173
{"serial_number", serial_number}
171-
});
174+
}, exp);
172175
} else {
173176
return buildLabel({
174177
{"v", std::to_string(version)},
@@ -177,15 +180,15 @@ Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::st
177180
{"serial_number", serial_number},
178181
{"last_name", last_name},
179182
{"first_name", first_name}
180-
});
183+
}, exp);
181184
}
182185
}
183186

184187
std::string
185-
Recipient::BuildLabelEID(const std::vector<uint8_t>& cert)
188+
Recipient::BuildLabelEID(const std::vector<uint8_t>& cert, time_t exp)
186189
{
187190
Certificate x509(cert);
188-
return BuildLabelEID(CDoc2::KEYLABELVERSION, getEIDType(x509.policies()), x509.getCommonName(), x509.getSerialNumber(), x509.getSurname(), x509.getGivenName());
191+
return BuildLabelEID(CDoc2::KEYLABELVERSION, getEIDType(x509.policies()), x509.getCommonName(), x509.getSerialNumber(), x509.getSurname(), x509.getGivenName(), exp ? exp : x509.getNotAfter());
189192
}
190193

191194
std::string

cdoc/Recipient.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ struct CDOC_EXPORT Recipient {
231231
/**
232232
* @brief build machine-readable CDoc2 label
233233
* @param components a list of string pairs
234+
* @param exp a expire date to added to label (if 0, no expire date is added)
234235
* @return a composed label
235236
*/
236-
static std::string buildLabel(std::vector<std::pair<std::string_view, std::string_view>> components);
237+
static std::string buildLabel(const std::vector<std::pair<std::string_view, std::string_view>> &components, time_t exp = 0);
237238
/**
238239
* @brief build machine-readable CDoc2 label for EID recipient
239240
* @param version the label version
@@ -242,16 +243,18 @@ struct CDOC_EXPORT Recipient {
242243
* @param serial_number the serial number
243244
* @param last_name the last name
244245
* @param first_name the first name
246+
* @param exp a expire date to added to label (if 0, no expire date is added)
245247
* @return a composed label
246248
*/
247-
static std::string BuildLabelEID(int version, EIDType type, std::string_view cn, std::string_view serial_number, std::string_view last_name, std::string_view first_name);
249+
static std::string BuildLabelEID(int version, EIDType type, std::string_view cn, std::string_view serial_number, std::string_view last_name, std::string_view first_name, time_t exp = 0);
248250
/**
249251
* @brief build machine-readable CDoc2 label for EID recipient filling info from certificate
250252
* @see BuildLabelEID
251253
* @param cert the certificate value (der-encoded)
254+
* @param exp a expire date to added to label (if 0, expire date is taken from certificate)
252255
* @return a composed label
253256
*/
254-
static std::string BuildLabelEID(const std::vector<uint8_t> &cert);
257+
static std::string BuildLabelEID(const std::vector<uint8_t> &cert, time_t exp = 0);
255258
/**
256259
* @brief build machine-readable CDoc2 label for certificate-based recipient
257260
* @param version the label version

cdoc/utils/memory.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct free_argument<R (*)(T *)>
3838
{
3939
using type = T;
4040
};
41+
template<class T, class R>
42+
struct free_argument<R (&)(T *)>
43+
{
44+
using type = T;
45+
};
4146

4247
template <class T>
4348
using unique_free_t = std::unique_ptr<T, void(*)(T*)>;

0 commit comments

Comments
 (0)