@@ -70,26 +70,6 @@ static std::shared_ptr<EVP_PKEY> GetEVP_PKEY(const String& keyfile)
70
70
return std::shared_ptr<EVP_PKEY>(pkey, EVP_PKEY_free);
71
71
}
72
72
73
- /* *
74
- * Returns a time_t value that is the current time plus the specified number of seconds,
75
- * but capped to max time_t on 32-bit systems to avoid the 2038 problem.
76
- *
77
- * @param timestamp The number of seconds to add to the current time.
78
- * @return The resulting time_t value, capped to max time_t on 32-bit systems.
79
- */
80
- time_t ValidCertValidity (long timestamp)
81
- {
82
- time_t t = time (nullptr );
83
- if constexpr (sizeof (t) > sizeof (int32_t )) {
84
- t = t + timestamp;
85
- } else {
86
- // On 32-bit systems time_t is 32-bit and thus limited to 2038.
87
- // So, make sure t+timestamp is never greater than max time_t.
88
- t = std::min (t + timestamp, std::numeric_limits<time_t >::max ());
89
- }
90
- return t;
91
- }
92
-
93
73
/* *
94
74
* Creates a new X509 certificate signed by the Icinga CA based on an existing CA certificate.
95
75
*
@@ -111,30 +91,31 @@ BOOST_FIXTURE_TEST_CASE(create_verify_ca, CertificateFixture)
111
91
auto cacert (GetX509Certificate (m_CaDir.string ()+" /ca.crt" ));
112
92
if constexpr (OPENSSL_VERSION_NUMBER >= 0x10100000L ) {
113
93
// OpenSSL 1.1.x provides https://www.openssl.org/docs/man1.1.0/man3/X509_check_ca.html
114
- BOOST_CHECK_EQUAL ( true , IsCa (cacert));
94
+ BOOST_CHECK ( IsCa (cacert));
115
95
} else {
116
96
BOOST_CHECK_THROW (IsCa (cacert), std::invalid_argument);
117
97
}
118
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (cacert, cacert, String ())); // Self-signed CA!
119
- BOOST_CHECK_EQUAL ( true , IsCaUptodate (cacert.get ())); // Is CA up-to-date after its creation?
98
+ BOOST_CHECK ( VerifyCertificate (cacert, cacert, String ())); // Self-signed CA!
99
+ BOOST_CHECK ( IsCaUptodate (cacert.get ())); // Is CA up-to-date after its creation?
120
100
121
- auto caValidUntil = ValidCertValidity (ROOT_VALID_FOR);
122
- // Due to processing time the expiration date might be off by a few seconds,
123
- // so we just check whether it is less than the expected value and not exactly equal.
124
- BOOST_CHECK_EQUAL (-1 , X509_cmp_time (X509_get_notAfter (cacert.get ()), &caValidUntil));
101
+ time_t caValidUntil = time (nullptr ) + NormalizeCertValidFor (ROOT_VALID_FOR);
102
+ // On 32-bit systems time_t might be a 32-bit integer, so the value overflows for ROOT_VALID_FOR.
103
+ // In this case, the expiration date is probably set to the maximum possible value time_t can hold,
104
+ // so must be the exact same ts as caValidUntil. So, on either platform it must be <= caValidUntil.
105
+ BOOST_CHECK_LE (X509_cmp_time (X509_get_notAfter (cacert.get ()), &caValidUntil), 0 );
125
106
126
107
// Set the CA certificate to expire in 100 days, i.e. less than the LEAF_VALID_FOR threshold of 397 days.
127
- BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), 60 *60 *24 *100 ));
128
- BOOST_CHECK_EQUAL ( false , IsCaUptodate (cacert.get ())); // Is CA outdated now?
108
+ BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), NormalizeCertValidFor ( 60 *60 *24 *100 ) ));
109
+ BOOST_CHECK (! IsCaUptodate (cacert.get ())); // Is CA outdated now?
129
110
130
- BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), 60 *60 *24 *397 ));
111
+ BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), NormalizeCertValidFor ( 60 *60 *24 *397 ) ));
131
112
// Even if the CA is going to expire at exactly the same time as the LEAF_VALID_FOR threshold,
132
113
// it is still considered to be outdated, so IsCaUptodate() should return false.
133
- BOOST_CHECK_EQUAL ( false , IsCaUptodate (cacert.get ()));
114
+ BOOST_CHECK (! IsCaUptodate (cacert.get ()));
134
115
135
116
// Reset the CA expiration date to the original value, i.e. 15 years.
136
- BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), ROOT_VALID_FOR));
137
- BOOST_CHECK_EQUAL ( true , IsCaUptodate (cacert.get ()));
117
+ BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cacert.get ()), NormalizeCertValidFor ( ROOT_VALID_FOR) ));
118
+ BOOST_CHECK ( IsCaUptodate (cacert.get ()));
138
119
}
139
120
140
121
BOOST_FIXTURE_TEST_CASE (create_verify_leaf_certs, CertificateFixture)
@@ -144,53 +125,53 @@ BOOST_FIXTURE_TEST_CASE(create_verify_leaf_certs, CertificateFixture)
144
125
145
126
auto caprivatekey (GetEVP_PKEY (caDir+" /ca.key" ));
146
127
auto cacert (GetX509Certificate (caDir+" /ca.crt" ));
147
- BOOST_CHECK_EQUAL ( true , IsCaUptodate (cacert.get ()));
128
+ BOOST_CHECK ( IsCaUptodate (cacert.get ()));
148
129
BOOST_CHECK_EQUAL (1 , X509_verify (cacert.get (), caprivatekey.get ())); // 1 == equal, 0 == unequal, -1 == error
149
130
150
131
auto certInfo = CertificateFixture::EnsureCertFor (" example.com" , true ); // Generates example.com.{key,csr,crt} files.
151
132
152
133
auto cert (GetX509Certificate (certInfo.crtFile ));
153
134
if constexpr (OPENSSL_VERSION_NUMBER >= 0x10100000L ) {
154
- BOOST_CHECK_EQUAL ( false , IsCa (cert));
135
+ BOOST_CHECK (! IsCa (cert));
155
136
} else {
156
137
BOOST_CHECK_THROW (IsCa (cert), std::invalid_argument);
157
138
}
158
- BOOST_CHECK_EQUAL ( true , IsCertUptodate (cert)); // Is leaf up-to-date after its creation?
159
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (cacert, cert, String ())); // Signed by our CA?
139
+ BOOST_CHECK ( IsCertUptodate (cert)); // Is leaf up-to-date after its creation?
140
+ BOOST_CHECK ( VerifyCertificate (cacert, cert, String ())); // Signed by our CA?
160
141
161
- auto certValidUntil = ValidCertValidity (LEAF_VALID_FOR);
142
+ time_t certValidUntil = time ( nullptr ) + NormalizeCertValidFor (LEAF_VALID_FOR);
162
143
// Due to processing time the expiration date might be off by a few seconds,
163
144
// so we just check whether it is less than the expected value and not exactly equal.
164
- BOOST_CHECK_EQUAL (- 1 , X509_cmp_time (X509_get_notAfter (cert.get ()), &certValidUntil));
145
+ BOOST_CHECK_LE ( X509_cmp_time (X509_get_notAfter (cert.get ()), &certValidUntil), 0 );
165
146
166
147
// Set the certificate to expire in 20 days, i.e. less than the RENEW_THRESHOLD of 30 days.
167
- BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cert.get ()), 60 *60 *24 *20 ));
168
- BOOST_CHECK_EQUAL ( false , IsCertUptodate (cert));
148
+ BOOST_CHECK (X509_gmtime_adj (X509_get_notAfter (cert.get ()), NormalizeCertValidFor ( 60 *60 *24 *20 ) ));
149
+ BOOST_CHECK (! IsCertUptodate (cert));
169
150
170
151
// Check whether expired certificates are correctly detected and verification fails.
171
152
PkiUtility::SignCsr (certInfo.csrFile , certInfo.crtFile , 60 *60 *24 *-10 ); // Expire 10 days ago!
172
153
cert = GetX509Certificate (certInfo.crtFile );
173
- certValidUntil = ValidCertValidity (60 *60 *24 *-10 );
154
+ certValidUntil = time ( nullptr ) + NormalizeCertValidFor (60 *60 *24 *-10 );
174
155
BOOST_CHECK_EQUAL (-1 , X509_cmp_time (X509_get_notAfter (cert.get ()), &certValidUntil)); // Is certificate indeed expired?
175
- BOOST_CHECK_EQUAL ( false , IsCertUptodate (cert)); // It's already expired, so definitely not up-to-date.
156
+ BOOST_CHECK (! IsCertUptodate (cert)); // It's already expired, so definitely not up-to-date.
176
157
BOOST_CHECK_THROW (VerifyCertificate (cacert, cert, String ()), openssl_error);
177
158
178
159
certInfo = CertificateFixture::EnsureCertFor (" example.com" , true );
179
160
cert = GetX509Certificate (certInfo.crtFile );
180
161
// Set the certificate validity start date to 2016, all certificates created before 2017 are considered outdated.
181
162
BOOST_CHECK (X509_gmtime_adj (X509_get_notBefore (cert.get ()), -(time (nullptr )-l_2016)));
182
- BOOST_CHECK_EQUAL ( false , IsCertUptodate (cert));
163
+ BOOST_CHECK (! IsCertUptodate (cert));
183
164
// ... but verification should still work, as the certificate is still valid.
184
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (cacert, cert, String ()));
165
+ BOOST_CHECK ( VerifyCertificate (cacert, cert, String ()));
185
166
186
167
// Reset the certificate validity start date to the least acceptable value, i.e. 2017.
187
168
BOOST_CHECK (X509_gmtime_adj (X509_get_notBefore (cert.get ()), -(time (nullptr )-l_2017)));
188
- BOOST_CHECK_EQUAL ( true , IsCertUptodate (cert));
189
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (cacert, cert, String ()));
169
+ BOOST_CHECK ( IsCertUptodate (cert));
170
+ BOOST_CHECK ( VerifyCertificate (cacert, cert, String ()));
190
171
191
172
cacert = NewCertFromExisting (cacert, 60 *60 *24 *-10 , true ); // Expire the CA 10 days ago.
192
173
BOOST_CHECK_EQUAL (1 , X509_verify (cacert.get (), caprivatekey.get ())); // 1 == equal, 0 == unequal, -1 == error
193
- BOOST_CHECK_EQUAL ( false , IsCaUptodate (cacert.get ()));
174
+ BOOST_CHECK (! IsCaUptodate (cacert.get ()));
194
175
BOOST_CHECK_THROW (VerifyCertificate (cacert, cert, String ()), openssl_error); // Signature failure!
195
176
196
177
PkiUtility::SignCsr (certInfo.csrFile , certInfo.crtFile ); // Resign the CSR with the (now expired) CA.
@@ -202,9 +183,9 @@ BOOST_FIXTURE_TEST_CASE(create_verify_leaf_certs, CertificateFixture)
202
183
auto newCACert = NewCertFromExisting (cacert, ROOT_VALID_FOR, true );
203
184
BOOST_REQUIRE (newCACert);
204
185
BOOST_CHECK_EQUAL (1 , X509_verify (newCACert.get (), caprivatekey.get ())); // 1 == equal, 0 == unequal, -1 == error
205
- BOOST_CHECK_EQUAL ( true , IsCaUptodate (newCACert.get ()));
206
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (newCACert, newCACert, String ()));
207
- BOOST_CHECK_EQUAL ( true , VerifyCertificate (newCACert, cert, String ()));
186
+ BOOST_CHECK ( IsCaUptodate (newCACert.get ()));
187
+ BOOST_CHECK ( VerifyCertificate (newCACert, newCACert, String ()));
188
+ BOOST_CHECK ( VerifyCertificate (newCACert, cert, String ()));
208
189
// ... but verifying the new CA with the old CA or vice versa should fail.
209
190
BOOST_CHECK_THROW (VerifyCertificate (cacert, newCACert, String ()), openssl_error);
210
191
BOOST_CHECK_THROW (VerifyCertificate (newCACert, cacert, String ()), openssl_error);
0 commit comments