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 * @file CertificateCacheDAO.cpp
20 * @author Tomasz Swierczek (t.swierczek@samsung.com)
22 * @brief CertificateCacheDAO implementation
25 #include "CertificateCacheDAO.h"
26 #include "VCorePrivate.h"
28 #include <dpl/foreach.h>
29 #include <dpl/log/log.h>
30 #include <dpl/db/orm.h>
31 #include <orm_generator_vcore.h>
32 #include <vcore/Database.h>
34 using namespace DPL::DB::ORM;
35 using namespace DPL::DB::ORM::vcore;
37 namespace ValidationCore {
39 void CertificateCacheDAO::setOCSPStatus(const std::string& cert_chain,
40 VerificationStatus ocsp_status,
41 bool end_entity_check,
42 time_t next_update_time)
45 ScopedTransaction transaction(&ThreadInterface());
46 OCSPCachedStatus status;
47 status.cert_chain = cert_chain;
48 status.end_entity_check = end_entity_check;
49 if (getOCSPStatus(&status)) {
50 // only need to update data in DB
51 Equals<OCSPResponseStorage::cert_chain> e1(
52 DPL::FromUTF8String(cert_chain));
53 Equals<OCSPResponseStorage::end_entity_check> e2(
54 end_entity_check ? 1 : 0);
56 OCSPResponseStorage::Row row;
58 row.Set_ocsp_status(ocsp_status);
59 row.Set_next_update_time(next_update_time);
61 VCORE_DB_UPDATE(update, OCSPResponseStorage, &ThreadInterface())
62 update->Where(And(e1,e2));
66 // need to insert data
67 OCSPResponseStorage::Row row;
69 row.Set_cert_chain(DPL::FromUTF8String(cert_chain));
70 row.Set_ocsp_status(ocsp_status);
71 row.Set_next_update_time(next_update_time);
72 row.Set_end_entity_check(end_entity_check ? 1 : 0);
74 VCORE_DB_INSERT(insert, OCSPResponseStorage, &ThreadInterface())
79 } Catch(DPL::DB::SqlConnection::Exception::Base) {
80 ReThrowMsg(Exception::DatabaseError, "Failed to setOCSPStatus");
84 bool CertificateCacheDAO::getOCSPStatus(OCSPCachedStatus* cached_status)
86 if (NULL == cached_status) {
87 LogError("NULL pointer");
91 Equals<OCSPResponseStorage::cert_chain> e1(
92 DPL::FromUTF8String(cached_status->cert_chain));
93 Equals<OCSPResponseStorage::end_entity_check> e2(
94 cached_status->end_entity_check ? 1 : 0);
96 VCORE_DB_SELECT(select, OCSPResponseStorage, &ThreadInterface())
98 select->Where(And(e1,e2));
99 std::list<OCSPResponseStorage::Row> rows = select->GetRowList();
100 if (1 == rows.size()) {
101 OCSPResponseStorage::Row row = rows.front();
102 cached_status->ocsp_status = intToVerificationStatus(
103 *(row.Get_ocsp_status()));
104 cached_status->next_update_time = *(row.Get_next_update_time());
108 LogDebug("Cached OCSP status not found");
111 Catch(DPL::DB::SqlConnection::Exception::Base) {
112 ReThrowMsg(Exception::DatabaseError, "Failed to getOCSPStatus");
116 void CertificateCacheDAO::getOCSPStatusList(
117 OCSPCachedStatusList* cached_status_list)
119 if (NULL == cached_status_list) {
120 LogError("NULL pointer");
124 VCORE_DB_SELECT(select, OCSPResponseStorage, &ThreadInterface())
125 typedef std::list<OCSPResponseStorage::Row> RowList;
126 RowList list = select->GetRowList();
129 OCSPCachedStatus status;
130 status.cert_chain = DPL::ToUTF8String(i->Get_cert_chain());
131 status.ocsp_status = intToVerificationStatus(
132 *(i->Get_ocsp_status()));
133 status.end_entity_check =
134 *(i->Get_end_entity_check()) == 1 ? true : false;
135 status.next_update_time = *(i->Get_next_update_time());
136 cached_status_list->push_back(status);
140 Catch(DPL::DB::SqlConnection::Exception::Base) {
141 ReThrowMsg(Exception::DatabaseError, "Failed to getOCSPStatusList");
146 void CertificateCacheDAO::setCRLResponse(const std::string& distribution_point,
147 const std::string& crl_body,
148 time_t next_update_time)
151 ScopedTransaction transaction(&ThreadInterface());
153 data.distribution_point = distribution_point;
154 if (getCRLResponse(&data)) {
155 // only need to update data in DB
156 VCORE_DB_UPDATE(update, CRLResponseStorage, &ThreadInterface())
157 Equals<CRLResponseStorage::distribution_point> e1(
158 DPL::FromUTF8String(distribution_point));
159 CRLResponseStorage::Row row;
162 row.Set_crl_body(DPL::FromUTF8String(crl_body));
163 row.Set_next_update_time(next_update_time);
167 // need to insert data
168 VCORE_DB_INSERT(insert, CRLResponseStorage, &ThreadInterface())
169 CRLResponseStorage::Row row;
171 row.Set_distribution_point(DPL::FromUTF8String(distribution_point));
172 row.Set_crl_body(DPL::FromUTF8String(crl_body));
173 row.Set_next_update_time(next_update_time);
177 transaction.Commit();
178 } Catch(DPL::DB::SqlConnection::Exception::Base) {
179 ReThrowMsg(Exception::DatabaseError, "Failed to setOCSPStatus");
183 bool CertificateCacheDAO::getCRLResponse(CRLCachedData* cached_data)
185 if (NULL == cached_data) {
186 LogError("NULL pointer");
190 VCORE_DB_SELECT(select, CRLResponseStorage, &ThreadInterface())
191 Equals<CRLResponseStorage::distribution_point> e1(
192 DPL::FromUTF8String(cached_data->distribution_point));
195 std::list<CRLResponseStorage::Row> rows = select->GetRowList();
196 if (1 == rows.size()) {
197 CRLResponseStorage::Row row = rows.front();
198 cached_data->crl_body = DPL::ToUTF8String(row.Get_crl_body());
199 cached_data->next_update_time = *(row.Get_next_update_time());
203 LogDebug("Cached CRL not found");
206 Catch(DPL::DB::SqlConnection::Exception::Base) {
207 ReThrowMsg(Exception::DatabaseError, "Failed to getCRLResponse");
211 void CertificateCacheDAO::getCRLResponseList(
212 CRLCachedDataList* cached_data_list)
214 if (NULL == cached_data_list) {
215 LogError("NULL pointer");
219 VCORE_DB_SELECT(select, CRLResponseStorage, &ThreadInterface())
220 typedef std::list<CRLResponseStorage::Row> RowList;
221 RowList list = select->GetRowList();
224 CRLCachedData response;
225 response.distribution_point = DPL::ToUTF8String(
226 i->Get_distribution_point());
227 response.crl_body = DPL::ToUTF8String(i->Get_crl_body());
228 response.next_update_time = *(i->Get_next_update_time());
229 cached_data_list->push_back(response);
233 Catch(DPL::DB::SqlConnection::Exception::Base) {
234 ReThrowMsg(Exception::DatabaseError, "Failed to getCRLResponses");
238 void CertificateCacheDAO::clearCertificateCache()
241 ScopedTransaction transaction(&ThreadInterface());
242 VCORE_DB_DELETE(del1, OCSPResponseStorage, &ThreadInterface())
244 VCORE_DB_DELETE(del2, CRLResponseStorage, &ThreadInterface())
246 transaction.Commit();
248 Catch(DPL::DB::SqlConnection::Exception::Base) {
249 ReThrowMsg(Exception::DatabaseError, "Failed to clearUserSettings");
253 VerificationStatus CertificateCacheDAO::intToVerificationStatus(int p)
257 return VERIFICATION_STATUS_GOOD;
259 return VERIFICATION_STATUS_REVOKED;
261 return VERIFICATION_STATUS_UNKNOWN;
263 return VERIFICATION_STATUS_VERIFICATION_ERROR;
265 return VERIFICATION_STATUS_NOT_SUPPORT;
267 return VERIFICATION_STATUS_CONNECTION_FAILED;
269 return VERIFICATION_STATUS_ERROR;
271 return VERIFICATION_STATUS_ERROR;
275 } // namespace ValidationCore