|
2 | 2 |
|
3 | 3 | #include "base/tlsutility.hpp" |
4 | 4 | #include <BoostTestTargetConfig.h> |
| 5 | +#include <functional> |
| 6 | +#include <memory> |
| 7 | +#include <openssl/asn1.h> |
| 8 | +#include <openssl/bn.h> |
| 9 | +#include <openssl/evp.h> |
| 10 | +#include <openssl/obj_mac.h> |
| 11 | +#include <openssl/rsa.h> |
| 12 | +#include <openssl/x509.h> |
5 | 13 | #include <utility> |
6 | 14 | #include <vector> |
7 | 15 |
|
8 | 16 | using namespace icinga; |
9 | 17 |
|
| 18 | +static EVP_PKEY* GenKeypair() |
| 19 | +{ |
| 20 | + InitializeOpenSSL(); |
| 21 | + |
| 22 | + auto e (BN_new()); |
| 23 | + BOOST_REQUIRE(e); |
| 24 | + |
| 25 | + auto rsa (RSA_new()); |
| 26 | + BOOST_REQUIRE(rsa); |
| 27 | + |
| 28 | + auto key (EVP_PKEY_new()); |
| 29 | + BOOST_REQUIRE(key); |
| 30 | + |
| 31 | + BOOST_REQUIRE(BN_set_word(e, RSA_F4)); |
| 32 | + BOOST_REQUIRE(RSA_generate_key_ex(rsa, 4096, e, nullptr)); |
| 33 | + BOOST_REQUIRE(EVP_PKEY_assign_RSA(key, rsa)); |
| 34 | + |
| 35 | + return key; |
| 36 | +} |
| 37 | + |
| 38 | +static std::shared_ptr<X509> MakeCert(const char* issuer, EVP_PKEY* signer, const char* subject, EVP_PKEY* pubkey, std::function<void(ASN1_TIME*, ASN1_TIME*)> setTimes) |
| 39 | +{ |
| 40 | + auto cert (X509_new()); |
| 41 | + BOOST_REQUIRE(cert); |
| 42 | + |
| 43 | + auto serial (BN_new()); |
| 44 | + BOOST_REQUIRE(serial); |
| 45 | + |
| 46 | + BOOST_REQUIRE(X509_set_version(cert, 0x2)); |
| 47 | + BOOST_REQUIRE(BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert))); |
| 48 | + BOOST_REQUIRE(X509_NAME_add_entry_by_NID(X509_get_issuer_name(cert), NID_commonName, MBSTRING_ASC, (unsigned char*)issuer, -1, -1, 0)); |
| 49 | + setTimes(X509_get_notBefore(cert), X509_get_notAfter(cert)); |
| 50 | + BOOST_REQUIRE(X509_NAME_add_entry_by_NID(X509_get_subject_name(cert), NID_commonName, MBSTRING_ASC, (unsigned char*)subject, -1, -1, 0)); |
| 51 | + BOOST_REQUIRE(X509_set_pubkey(cert, pubkey)); |
| 52 | + BOOST_REQUIRE(X509_sign(cert, signer, EVP_sha256())); |
| 53 | + |
| 54 | + return std::shared_ptr<X509>(cert, X509_free); |
| 55 | +} |
| 56 | + |
| 57 | +static const long l_2016 = 1480000000; // Thu Nov 24 15:06:40 UTC 2016 |
| 58 | +static const long l_2017 = 1490000000; // Mon Mar 20 08:53:20 UTC 2017 |
| 59 | + |
10 | 60 | BOOST_AUTO_TEST_SUITE(base_tlsutility) |
11 | 61 |
|
12 | 62 | BOOST_AUTO_TEST_CASE(sha1) |
@@ -35,4 +85,51 @@ BOOST_AUTO_TEST_CASE(sha1) |
35 | 85 | } |
36 | 86 | } |
37 | 87 |
|
| 88 | +BOOST_AUTO_TEST_CASE(iscauptodate_ok) |
| 89 | +{ |
| 90 | + auto key (GenKeypair()); |
| 91 | + |
| 92 | + BOOST_CHECK(IsCaUptodate(MakeCert("Icinga CA", key, "Icinga CA", key, [](ASN1_TIME* notBefore, ASN1_TIME* notAfter) { |
| 93 | + BOOST_REQUIRE(X509_gmtime_adj(notBefore, 0)); |
| 94 | + BOOST_REQUIRE(X509_gmtime_adj(notAfter, LEAF_VALID_FOR + 60 * 60)); |
| 95 | + }).get())); |
| 96 | +} |
| 97 | + |
| 98 | +BOOST_AUTO_TEST_CASE(iscauptodate_expiring) |
| 99 | +{ |
| 100 | + auto key (GenKeypair()); |
| 101 | + |
| 102 | + BOOST_CHECK(!IsCaUptodate(MakeCert("Icinga CA", key, "Icinga CA", key, [](ASN1_TIME* notBefore, ASN1_TIME* notAfter) { |
| 103 | + BOOST_REQUIRE(X509_gmtime_adj(notBefore, 0)); |
| 104 | + BOOST_REQUIRE(X509_gmtime_adj(notAfter, LEAF_VALID_FOR - 60 * 60)); |
| 105 | + }).get())); |
| 106 | +} |
| 107 | + |
| 108 | +BOOST_AUTO_TEST_CASE(iscertuptodate_ok) |
| 109 | +{ |
| 110 | + BOOST_CHECK(IsCertUptodate(MakeCert("Icinga CA", GenKeypair(), "example.com", GenKeypair(), [](ASN1_TIME* notBefore, ASN1_TIME* notAfter) { |
| 111 | + time_t epoch = 0; |
| 112 | + BOOST_REQUIRE(X509_time_adj(notBefore, l_2017, &epoch)); |
| 113 | + BOOST_REQUIRE(X509_gmtime_adj(notAfter, RENEW_THRESHOLD + 60 * 60)); |
| 114 | + }))); |
| 115 | +} |
| 116 | + |
| 117 | +BOOST_AUTO_TEST_CASE(iscertuptodate_expiring) |
| 118 | +{ |
| 119 | + BOOST_CHECK(!IsCertUptodate(MakeCert("Icinga CA", GenKeypair(), "example.com", GenKeypair(), [](ASN1_TIME* notBefore, ASN1_TIME* notAfter) { |
| 120 | + time_t epoch = 0; |
| 121 | + BOOST_REQUIRE(X509_time_adj(notBefore, l_2017, &epoch)); |
| 122 | + BOOST_REQUIRE(X509_gmtime_adj(notAfter, RENEW_THRESHOLD - 60 * 60)); |
| 123 | + }))); |
| 124 | +} |
| 125 | + |
| 126 | +BOOST_AUTO_TEST_CASE(iscertuptodate_old) |
| 127 | +{ |
| 128 | + BOOST_CHECK(!IsCertUptodate(MakeCert("Icinga CA", GenKeypair(), "example.com", GenKeypair(), [](ASN1_TIME* notBefore, ASN1_TIME* notAfter) { |
| 129 | + time_t epoch = 0; |
| 130 | + BOOST_REQUIRE(X509_time_adj(notBefore, l_2016, &epoch)); |
| 131 | + BOOST_REQUIRE(X509_gmtime_adj(notAfter, RENEW_THRESHOLD + 60 * 60)); |
| 132 | + }))); |
| 133 | +} |
| 134 | + |
38 | 135 | BOOST_AUTO_TEST_SUITE_END() |
0 commit comments