summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2022-02-12 21:15:03 +0100
committerCarl Hetherington <cth@carlh.net>2022-02-12 23:13:49 +0100
commitd6b800e1c9a3cdb4f85a2308190afe9f0a856ed4 (patch)
tree439b3c142ac392b9888585d51c463696cef6ff87 /src
parent0a5615c17edf6d9c51d9269c824d6caf71f710e5 (diff)
Cleanup: use dcp::LocalTime for certificate validity times.
Before we were using struct tm but not filling it all in, which seems quite unpleasant.
Diffstat (limited to 'src')
-rw-r--r--src/certificate.cc21
-rw-r--r--src/certificate.h6
-rw-r--r--src/decrypted_kdm.cc4
-rw-r--r--src/local_time.cc31
-rw-r--r--src/local_time.h15
5 files changed, 60 insertions, 17 deletions
diff --git a/src/certificate.cc b/src/certificate.cc
index 93f271ef..1e7714a3 100644
--- a/src/certificate.cc
+++ b/src/certificate.cc
@@ -329,29 +329,24 @@ Certificate::subject_organizational_unit_name () const
static
-struct tm
+LocalTime
convert_time (ASN1_TIME const * time)
{
- struct tm t;
+ LocalTime t;
char const * s = (char const *) time->data;
if (time->type == V_ASN1_UTCTIME) {
- sscanf(s, "%2d%2d%2d%2d%2d%2d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
- if (t.tm_year < 70) {
- t.tm_year += 100;
- }
+ return LocalTime::from_asn1_utc_time (s);
} else if (time->type == V_ASN1_GENERALIZEDTIME) {
- sscanf(s, "%4d%2d%2d%2d%2d%2d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
- t.tm_year -= 1900;
+ return LocalTime::from_asn1_generalized_time (s);
}
- t.tm_mon--;
-
- return t;
+ DCP_ASSERT (false);
+ return {};
}
-struct tm
+LocalTime
Certificate::not_before () const
{
DCP_ASSERT (_certificate);
@@ -363,7 +358,7 @@ Certificate::not_before () const
}
-struct tm
+LocalTime
Certificate::not_after () const
{
DCP_ASSERT (_certificate);
diff --git a/src/certificate.h b/src/certificate.h
index 1b8188c7..49fca02c 100644
--- a/src/certificate.h
+++ b/src/certificate.h
@@ -41,6 +41,7 @@
#define LIBDCP_CERTIFICATE_H
+#include "local_time.h"
#undef X509_NAME
#include <openssl/x509.h>
#include <boost/filesystem.hpp>
@@ -106,8 +107,9 @@ public:
std::string subject_common_name () const;
std::string subject_organization_name () const;
std::string subject_organizational_unit_name () const;
- struct tm not_before () const;
- struct tm not_after () const;
+
+ LocalTime not_before () const;
+ LocalTime not_after () const;
X509* x509 () const {
return _certificate;
diff --git a/src/decrypted_kdm.cc b/src/decrypted_kdm.cc
index b2d8e618..592ab717 100644
--- a/src/decrypted_kdm.cc
+++ b/src/decrypted_kdm.cc
@@ -327,9 +327,9 @@ DecryptedKDM::encrypt (
DCP_ASSERT (!_keys.empty ());
for (auto i: signer->leaf_to_root()) {
- if (day_greater_than_or_equal(dcp::LocalTime(i.not_before()), _not_valid_before)) {
+ if (day_greater_than_or_equal(i.not_before(), _not_valid_before)) {
throw BadKDMDateError (true);
- } else if (day_less_than_or_equal(dcp::LocalTime(i.not_after()), _not_valid_after)) {
+ } else if (day_less_than_or_equal(i.not_after(), _not_valid_after)) {
throw BadKDMDateError (false);
}
}
diff --git a/src/local_time.cc b/src/local_time.cc
index 892ffba4..ad2291f9 100644
--- a/src/local_time.cc
+++ b/src/local_time.cc
@@ -307,3 +307,34 @@ dcp::operator<< (ostream& s, LocalTime const & t)
s << t.as_string ();
return s;
}
+
+
+LocalTime
+LocalTime::from_asn1_utc_time (string time)
+{
+ LocalTime t;
+ sscanf(time.c_str(), "%2d%2d%2d%2d%2d%2d", &t._year, &t._month, &t._day, &t._hour, &t._minute, &t._second);
+
+ if (t._year < 70) {
+ t._year += 100;
+ }
+ t._year += 1900;
+
+ t._tz_hour = t._tz_minute = t._millisecond = 0;
+
+ return t;
+}
+
+
+LocalTime
+LocalTime::from_asn1_generalized_time (string time)
+{
+ LocalTime t;
+ sscanf(time.c_str(), "%4d%2d%2d%2d%2d%2d", &t._year, &t._month, &t._day, &t._hour, &t._minute, &t._second);
+
+ t._tz_hour = t._tz_minute = t._millisecond = 0;
+
+ return t;
+}
+
+
diff --git a/src/local_time.h b/src/local_time.h
index a5a479cd..b84e3098 100644
--- a/src/local_time.h
+++ b/src/local_time.h
@@ -103,6 +103,18 @@ public:
return _year;
}
+ int hour () const {
+ return _hour;
+ }
+
+ int minute () const {
+ return _minute;
+ }
+
+ int second () const {
+ return _second;
+ }
+
void set_year (int y) {
_year = y;
}
@@ -111,6 +123,9 @@ public:
void add_months (int a);
void add_minutes (int a);
+ static LocalTime from_asn1_utc_time (std::string time);
+ static LocalTime from_asn1_generalized_time (std::string time);
+
bool operator== (LocalTime const & other) const;
bool operator!= (LocalTime const & other) const;
bool operator< (LocalTime const & other) const;