Commit 125e0ed6 authored by Alex Guzman's avatar Alex Guzman Committed by Facebook Github Bot

Add function for converting ASN1_TIME to std::chrono::system_clock::time_point

Summary: As it says on tin.

Reviewed By: yfeldblum

Differential Revision: D15409159

fbshipit-source-id: a99b17b54e0c36d79956213655385d234190ff52
parent a18cdcd5
......@@ -345,5 +345,13 @@ char* strptime(
}
return const_cast<char*>(s + input.tellg());
}
time_t timelocal(tm* tm) {
return mktime(tm);
}
time_t timegm(tm* tm) {
return _mkgmtime(tm);
}
}
#endif
......@@ -66,5 +66,7 @@ char* strptime(
const char* __restrict buf,
const char* __restrict fmt,
struct tm* __restrict tm);
time_t timelocal(tm* tm);
time_t timegm(tm* tm);
}
#endif
......@@ -163,6 +163,27 @@ std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
return getDateTimeStr(X509_get0_notBefore(&x509));
}
std::chrono::system_clock::time_point OpenSSLCertUtils::asnTimeToTimepoint(
ASN1_TIME* asnTime) {
int dSecs = 0;
int dDays = 0;
auto epoch_time_t = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point());
folly::ssl::ASN1TimeUniquePtr epoch_asn(ASN1_TIME_set(nullptr, epoch_time_t));
if (!epoch_asn) {
throw std::runtime_error("failed to allocate epoch asn.1 time");
}
if (ASN1_TIME_diff(&dDays, &dSecs, epoch_asn.get(), asnTime) != 1) {
throw std::runtime_error("invalid asn.1 time");
}
return std::chrono::system_clock::time_point(
std::chrono::seconds(dSecs) + std::chrono::hours(24 * dDays));
}
std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
if (!time) {
return "";
......
......@@ -15,6 +15,7 @@
*/
#pragma once
#include <chrono>
#include <string>
#include <vector>
......@@ -109,6 +110,13 @@ class OpenSSLCertUtils {
*/
static X509StoreUniquePtr readStoreFromBuffer(ByteRange range);
/**
* Converts an ASN1_TIME* into a system clock time point for use with other
* std::chrono classes.
*/
static std::chrono::system_clock::time_point asnTimeToTimepoint(
ASN1_TIME* asnTime);
private:
static std::string getDateTimeStr(const ASN1_TIME* time);
};
......
......@@ -21,6 +21,7 @@
#include <folly/container/Enumerate.h>
#include <folly/portability/GTest.h>
#include <folly/portability/OpenSSL.h>
#include <folly/portability/Time.h>
#include <folly/ssl/Init.h>
#include <folly/ssl/OpenSSLPtrTypes.h>
......@@ -220,6 +221,19 @@ TEST_P(OpenSSLCertUtilsTest, TestX509Dates) {
EXPECT_EQ(notAfter, "Jul 1 23:21:03 2044 GMT");
}
TEST_P(OpenSSLCertUtilsTest, TestASN1TimeToTimePoint) {
auto x509 = readCertFromData(kTestCertWithSan);
EXPECT_NE(x509, nullptr);
std::tm tm = {};
strptime("Feb 13 23:21:03 2017", "%b %d %H:%M:%S %Y", &tm);
auto expected = std::chrono::system_clock::from_time_t(timegm(&tm));
auto notBefore = X509_get_notBefore(x509.get());
auto result = folly::ssl::OpenSSLCertUtils::asnTimeToTimepoint(notBefore);
EXPECT_EQ(
std::chrono::time_point_cast<std::chrono::seconds>(expected),
std::chrono::time_point_cast<std::chrono::seconds>(result));
}
TEST_P(OpenSSLCertUtilsTest, TestX509Summary) {
auto x509 = readCertFromData(kTestCertWithSan);
EXPECT_NE(x509, nullptr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment