2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @author Tomasz Swierczek (t.swierczek@samsung.com)
21 * @brief Cached CRL class implementation
27 #include <dpl/foreach.h>
28 #include <dpl/log/log.h>
29 #include <dpl/foreach.h>
32 #include "CachedCRL.h"
33 #include "Certificate.h"
34 #include "CertificateCacheDAO.h"
36 namespace ValidationCore {
38 const time_t CachedCRL::CRL_minTimeValid = 3600; // one hour in seconds
40 const time_t CachedCRL::CRL_maxTimeValid = 3600 * 24 * 7; // one week in seconds
42 const time_t CachedCRL::CRL_refreshBefore = 3600; // one hour in seconds
44 VerificationStatus CachedCRL::check(const CertificateCollection &certs)
48 // we dont check CRL validity since
49 // we may use crl for longer time
50 // in smart cache than in regular CRL class (time clamping)
51 crl.addToStore(certs);
53 CRL::StringList crlUris = crl.getCrlUris(*cert);
54 FOREACH(uri, crlUris) {
55 allValid = allValid && updateCRLForUri(*uri,false);
59 // problems with CRL validity
60 LogDebug("Some CRLs not valid");
62 CRL::RevocationStatus stat = crl.checkCertificateChain(certs);
64 LogDebug("Status REVOKED");
65 return VERIFICATION_STATUS_REVOKED;
67 LogDebug("Status GOOD");
68 return VERIFICATION_STATUS_GOOD;
71 VerificationStatus CachedCRL::checkEndEntity(CertificateCollection &certs)
74 LogError("Collection empty. This should never happen.");
75 LogDebug("Status ERROR");
76 return VERIFICATION_STATUS_ERROR;
79 LogError("Could not find End Entity certificate. "
80 "Collection does not form chain.");
81 LogDebug("Status ERROR");
82 return VERIFICATION_STATUS_ERROR;
86 // we dont check CRL validity since
87 // we may use crl for longer time
88 // in smart cache than in regular CRL class (time clamping)
89 crl.addToStore(certs);
90 CertificateList::const_iterator icert = certs.begin();
91 if (icert != certs.end()) {
92 CRL::StringList crlUris = crl.getCrlUris(*icert);
93 FOREACH(uri, crlUris) {
94 allValid = allValid && updateCRLForUri(*uri,false);
98 // problems with CRL validity
99 LogDebug("Some CRLs not valid");
101 CertificateList::const_iterator iter = certs.begin();
102 CRL::RevocationStatus stat = crl.checkCertificate(*iter);
103 if (stat.isRevoked) {
104 LogDebug("Status REVOKED");
105 return VERIFICATION_STATUS_REVOKED;
107 LogDebug("Status GOOD");
108 return VERIFICATION_STATUS_GOOD;
111 void CachedCRL::updateCache()
113 CRLCachedDataList list;
114 CertificateCacheDAO::getCRLResponseList(&list);
115 FOREACH(db_crl, list) {
116 updateCRLForUri(db_crl->distribution_point, true);
120 bool CachedCRL::updateCRLForUri(const std::string & uri, bool useExpiredShift)
122 CRLCachedData cachedCRL;
123 cachedCRL.distribution_point = uri;
126 if (useExpiredShift) {
127 now += CRL_refreshBefore;
129 if (CertificateCacheDAO::getCRLResponse(&cachedCRL)) {
130 if (now < cachedCRL.next_update_time) {
131 LogDebug("Cached CRL still valid for: " << uri);
135 // need to download new CRL
137 CRL::CRLDataPtr list = crl.downloadCRL(uri);
139 LogWarning("Could not retreive CRL from " << uri);
143 CertificateCacheDAO::getCRLResponse(&cachedCRL); // save it the way CRL does
144 cachedCRL.next_update_time =
145 getNextUpdateTime(now,cachedCRL.next_update_time);
146 CertificateCacheDAO::setCRLResponse(cachedCRL.distribution_point,
148 cachedCRL.next_update_time);
152 time_t CachedCRL::getNextUpdateTime(time_t now, time_t response_validity)
154 time_t min = now + CRL_minTimeValid;
155 time_t max = now + CRL_maxTimeValid;
156 if (response_validity < min) {
159 if (response_validity > max) {
162 return response_validity;
165 } // namespace ValidationCore