Skip to content

Commit e21c2df

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

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
@@ -106,7 +106,7 @@ Recipient
106106
Recipient::makeEIDServer(std::vector<uint8_t> cert, std::string server_id)
107107
{
108108
Certificate x509(cert);
109-
auto label = BuildLabelEID(cert);
109+
auto label = BuildLabelEID(cert, time(nullptr) + 60 * 60 * 24 * 31 * 6); // 6 months
110110
return makeServer(std::move(label),
111111
x509.getPublicKey(), x509.getAlgorithm() == Certificate::Algorithm::RSA ? RSA : ECC, std::move(server_id));
112112
}
@@ -145,7 +145,7 @@ static constexpr std::string_view type_strs[] = {
145145
};
146146

147147
std::string
148-
Recipient::buildLabel(std::vector<std::pair<std::string_view, std::string_view>> components)
148+
Recipient::buildLabel(const std::vector<std::pair<std::string_view, std::string_view>> &components, time_t exp)
149149
{
150150
std::ostringstream ofs;
151151
ofs << LABELPREFIX;
@@ -157,11 +157,14 @@ Recipient::buildLabel(std::vector<std::pair<std::string_view, std::string_view>>
157157
first = false;
158158
}
159159
}
160+
if(exp > 0) {
161+
ofs << "&server_exp=" << exp;
162+
}
160163
return ofs.str();
161164
}
162165

163166
std::string
164-
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)
167+
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)
165168
{
166169
// In case of cards issued to an organization the first name (and last name) are missing. We ommit these parts.
167170
if (first_name.empty()) {
@@ -170,7 +173,7 @@ Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::st
170173
{"type", type_strs[type]},
171174
{"cn", cn},
172175
{"serial_number", serial_number}
173-
});
176+
}, exp);
174177
} else {
175178
return buildLabel({
176179
{"v", std::to_string(version)},
@@ -179,15 +182,15 @@ Recipient::BuildLabelEID(int version, EIDType type, std::string_view cn, std::st
179182
{"serial_number", serial_number},
180183
{"last_name", last_name},
181184
{"first_name", first_name}
182-
});
185+
}, exp);
183186
}
184187
}
185188

186189
std::string
187-
Recipient::BuildLabelEID(const std::vector<uint8_t>& cert)
190+
Recipient::BuildLabelEID(const std::vector<uint8_t>& cert, time_t exp)
188191
{
189192
Certificate x509(cert);
190-
return BuildLabelEID(CDoc2::KEYLABELVERSION, getEIDType(x509.policies()), x509.getCommonName(), x509.getSerialNumber(), x509.getSurname(), x509.getGivenName());
193+
return BuildLabelEID(CDoc2::KEYLABELVERSION, getEIDType(x509.policies()), x509.getCommonName(), x509.getSerialNumber(), x509.getSurname(), x509.getGivenName(), exp ? exp : x509.getNotAfter());
191194
}
192195

193196
std::string

cdoc/Recipient.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ struct CDOC_EXPORT Recipient {
223223
/**
224224
* @brief build machine-readable CDoc2 label
225225
* @param components a list of string pairs
226+
* @param exp a expire date to added to label (if 0, no expire date is added)
226227
* @return a composed label
227228
*/
228-
static std::string buildLabel(std::vector<std::pair<std::string_view, std::string_view>> components);
229+
static std::string buildLabel(const std::vector<std::pair<std::string_view, std::string_view>> &components, time_t exp = 0);
229230
/**
230231
* @brief build machine-readable CDoc2 label for EID recipient
231232
* @param version the label version
@@ -234,16 +235,18 @@ struct CDOC_EXPORT Recipient {
234235
* @param serial_number the serial number
235236
* @param last_name the last name
236237
* @param first_name the first name
238+
* @param exp a expire date to added to label (if 0, no expire date is added)
237239
* @return a composed label
238240
*/
239-
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);
241+
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);
240242
/**
241243
* @brief build machine-readable CDoc2 label for EID recipient filling info from certificate
242244
* @see BuildLabelEID
243245
* @param cert the certificate value (der-encoded)
246+
* @param exp a expire date to added to label (if 0, expire date is taken from certificate)
244247
* @return a composed label
245248
*/
246-
static std::string BuildLabelEID(const std::vector<uint8_t> &cert);
249+
static std::string BuildLabelEID(const std::vector<uint8_t> &cert, time_t exp = 0);
247250
/**
248251
* @brief build machine-readable CDoc2 label for certificate-based recipient
249252
* @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)