blob: a0f24ecef93c392720f926011f8b9668c9469277 [file] [log] [blame]
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob48bbda2020-07-27 19:41:37 -04002/*
3 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "ca-sqlite.hpp"
Davide Pesaventob48bbda2020-07-27 19:41:37 -040022
Zhiyi Zhanga749f442020-09-29 17:19:51 -070023#include <sqlite3.h>
24
25#include <boost/filesystem.hpp>
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070026#include <ndn-cxx/security/v2/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070027#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070028
29namespace ndn {
30namespace ndncert {
31
Davide Pesaventob48bbda2020-07-27 19:41:37 -040032const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070033
34NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
35
36using namespace ndn::util;
37
38static const std::string INITIALIZATION = R"_DBTEXT_(
39CREATE TABLE IF NOT EXISTS
40 CertRequests(
41 id INTEGER PRIMARY KEY,
42 request_id TEXT NOT NULL,
43 ca_name BLOB NOT NULL,
tylerliu182bc532020-09-25 01:54:45 -070044 request_type INTEGER NOT NULL,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045 status INTEGER NOT NULL,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070046 cert_key_name BLOB NOT NULL,
47 cert_request BLOB NOT NULL,
48 challenge_type TEXT,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070049 challenge_status TEXT,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070050 challenge_tp TEXT,
51 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070052 remaining_time INTEGER,
tylerliu8e170d62020-09-30 01:31:53 -070053 challenge_secrets TEXT,
54 encryption_key BLOB NOT NULL
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070055 );
56CREATE UNIQUE INDEX IF NOT EXISTS
57 CertRequestIdIndex ON CertRequests(request_id);
58CREATE UNIQUE INDEX IF NOT EXISTS
59 CertRequestKeyNameIndex ON CertRequests(cert_key_name);
60
61CREATE TABLE IF NOT EXISTS
62 IssuedCerts(
63 id INTEGER PRIMARY KEY,
64 cert_id TEXT NOT NULL,
65 cert_key_name BLOB NOT NULL,
66 cert BLOB NOT NULL
67 );
68CREATE UNIQUE INDEX IF NOT EXISTS
69 IssuedCertRequestIdIndex ON IssuedCerts(cert_id);
70CREATE UNIQUE INDEX IF NOT EXISTS
71 IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name);
72)_DBTEXT_";
73
74CaSqlite::CaSqlite(const std::string& location)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070075 : CaStorage()
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070076{
77 // Determine the path of sqlite db
78 boost::filesystem::path dbDir;
79 if (!location.empty()) {
80 dbDir = boost::filesystem::path(location);
81 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070082 else if (getenv("HOME") != nullptr) {
83 dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
84 }
85 else {
86 dbDir = boost::filesystem::current_path() / ".ndn";
87 }
88 boost::filesystem::create_directories(dbDir);
89
90 // open and initialize database
91 int result = sqlite3_open_v2((dbDir / "ndncert-ca.db").c_str(), &m_database,
92 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
93#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
94 "unix-dotfile"
95#else
96 nullptr
97#endif
Zhiyi Zhanga749f442020-09-29 17:19:51 -070098 );
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070099 if (result != SQLITE_OK)
100 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
101
102 // initialize database specific tables
103 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400104 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700105 nullptr, nullptr, &errorMessage);
106 if (result != SQLITE_OK && errorMessage != nullptr) {
107 sqlite3_free(errorMessage);
108 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be initialized"));
109 }
110}
111
112CaSqlite::~CaSqlite()
113{
114 sqlite3_close(m_database);
115}
116
Zhiyi Zhange232a742020-09-29 17:34:17 -0700117RequestState
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700118CaSqlite::getRequest(const std::string& requestId)
119{
120 Sqlite3Statement statement(m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700121 R"_SQLTEXT_(SELECT id, ca_name, status,
122 challenge_status, cert_request,
123 challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700124 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700125 FROM CertRequests where request_id = ?)_SQLTEXT_");
126 statement.bind(1, requestId, SQLITE_TRANSIENT);
127
128 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700129 Name caName(statement.getBlock(1));
130 auto status = static_cast<Status>(statement.getInt(2));
131 auto challengeStatus = statement.getString(3);
132 security::v2::Certificate cert(statement.getBlock(4));
133 auto challengeType = statement.getString(5);
134 auto challengeSecrets = statement.getString(6);
135 auto challengeTp = statement.getString(7);
136 auto remainingTries = statement.getInt(8);
137 auto remainingTime = statement.getInt(9);
138 auto requestType = static_cast<RequestType>(statement.getInt(10));
tylerliu8e170d62020-09-30 01:31:53 -0700139 auto encryptionKey = statement.getBlock(11);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700140 if (challengeType != "") {
Zhiyi Zhange232a742020-09-29 17:34:17 -0700141 return RequestState(caName, requestId, requestType, status, cert,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700142 challengeType, challengeStatus, time::fromIsoString(challengeTp),
143 remainingTries, time::seconds(remainingTime),
tylerliu8e170d62020-09-30 01:31:53 -0700144 convertString2Json(challengeSecrets), encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700145 }
146 else {
tylerliu8e170d62020-09-30 01:31:53 -0700147 return RequestState(caName, requestId, requestType, status, cert, encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700148 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700149 }
150 else {
151 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database"));
152 }
153}
154
155void
Zhiyi Zhange232a742020-09-29 17:34:17 -0700156CaSqlite::addRequest(const RequestState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700157{
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700158 // check whether request is there already
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700159 auto keyNameTlv = request.m_cert.getKeyName().wireEncode();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700160 Sqlite3Statement statement1(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700161 R"_SQLTEXT_(SELECT * FROM CertRequests where cert_key_name = ?)_SQLTEXT_");
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700162 statement1.bind(1, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700163 if (statement1.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700164 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700165 }
166
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700167 // check whether certificate is already issued
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700168 Sqlite3Statement statement2(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700169 R"_SQLTEXT_(SELECT * FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_");
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700170 statement2.bind(1, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700171 if (statement2.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700172 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700173 }
174
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700175 Sqlite3Statement statement(
176 m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700177 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status, request_type,
178 cert_key_name, cert_request, challenge_type, challenge_status, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700179 challenge_tp, remaining_tries, remaining_time, encryption_key)
180 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700181 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
182 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
183 statement.bind(3, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700184 statement.bind(4, static_cast<int>(request.m_requestType));
185 statement.bind(5, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700186 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
tylerliu8e170d62020-09-30 01:31:53 -0700187 statement.bind(13, request.m_encryptionKey, SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700188 if (request.m_challengeState) {
189 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
190 statement.bind(8, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
191 statement.bind(9, convertJson2String(request.m_challengeState->m_secrets),
192 SQLITE_TRANSIENT);
193 statement.bind(10, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
194 statement.bind(11, request.m_challengeState->m_remainingTries);
195 statement.bind(12, request.m_challengeState->m_remainingTime.count());
196 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700197 if (statement.step() != SQLITE_DONE) {
198 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700199 }
200}
201
202void
Zhiyi Zhange232a742020-09-29 17:34:17 -0700203CaSqlite::updateRequest(const RequestState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700204{
205 Sqlite3Statement statement(m_database,
206 R"_SQLTEXT_(UPDATE CertRequests
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700207 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
208 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700209 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700210 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700211 statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT);
212 if (request.m_challengeState) {
213 statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
214 statement.bind(4, convertJson2String(request.m_challengeState->m_secrets),
215 SQLITE_TRANSIENT);
216 statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
217 statement.bind(6, request.m_challengeState->m_remainingTries);
218 statement.bind(7, request.m_challengeState->m_remainingTime.count());
219 }
220 else {
221 statement.bind(3, "", SQLITE_TRANSIENT);
222 statement.bind(4, "", SQLITE_TRANSIENT);
223 statement.bind(5, "", SQLITE_TRANSIENT);
224 statement.bind(6, 0);
225 statement.bind(7, 0);
226 }
227 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700228
229 if (statement.step() != SQLITE_DONE) {
230 addRequest(request);
231 }
232}
233
Zhiyi Zhange232a742020-09-29 17:34:17 -0700234std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700235CaSqlite::listAllRequests()
236{
Zhiyi Zhange232a742020-09-29 17:34:17 -0700237 std::list<RequestState> result;
tylerliu182bc532020-09-25 01:54:45 -0700238 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
239 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700240 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu182bc532020-09-25 01:54:45 -0700241 FROM CertRequests)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400242 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700243 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700244 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700245 auto status = static_cast<Status>(statement.getInt(3));
246 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700247 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700248 auto challengeType = statement.getString(7);
249 auto challengeSecrets = statement.getString(8);
250 auto challengeTp = statement.getString(9);
251 auto remainingTries = statement.getInt(10);
252 auto remainingTime = statement.getInt(11);
253 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu8e170d62020-09-30 01:31:53 -0700254 auto encryptionKey = statement.getBlock(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700255 if (challengeType != "") {
Zhiyi Zhange232a742020-09-29 17:34:17 -0700256 result.push_back(RequestState(caName, requestId, requestType, status, cert,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700257 challengeType, challengeStatus, time::fromIsoString(challengeTp),
258 remainingTries, time::seconds(remainingTime),
tylerliu8e170d62020-09-30 01:31:53 -0700259 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700260 }
261 else {
tylerliu8e170d62020-09-30 01:31:53 -0700262 result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700263 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700264 }
265 return result;
266}
267
Zhiyi Zhange232a742020-09-29 17:34:17 -0700268std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700269CaSqlite::listAllRequests(const Name& caName)
270{
Zhiyi Zhange232a742020-09-29 17:34:17 -0700271 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700272 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700273 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
274 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700275 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu182bc532020-09-25 01:54:45 -0700276 FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700277 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
278
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400279 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700280 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700281 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700282 auto status = static_cast<Status>(statement.getInt(3));
283 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700284 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700285 auto challengeType = statement.getString(7);
286 auto challengeSecrets = statement.getString(8);
287 auto challengeTp = statement.getString(9);
288 auto remainingTries = statement.getInt(10);
289 auto remainingTime = statement.getInt(11);
290 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu8e170d62020-09-30 01:31:53 -0700291 auto encryptionKey = statement.getBlock(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700292 if (challengeType != "") {
Zhiyi Zhange232a742020-09-29 17:34:17 -0700293 result.push_back(RequestState(caName, requestId, requestType, status, cert,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700294 challengeType, challengeStatus, time::fromIsoString(challengeTp),
295 remainingTries, time::seconds(remainingTime),
tylerliu8e170d62020-09-30 01:31:53 -0700296 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700297 }
298 else {
tylerliu8e170d62020-09-30 01:31:53 -0700299 result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700300 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700301 }
302 return result;
303}
304
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700305void
306CaSqlite::deleteRequest(const std::string& requestId)
307{
308 Sqlite3Statement statement(m_database,
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700309 R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700310 statement.bind(1, requestId, SQLITE_TRANSIENT);
311 statement.step();
312}
313
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700314security::v2::Certificate
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700315CaSqlite::getCertificate(const std::string& certId)
316{
317 Sqlite3Statement statement(m_database,
318 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
319 statement.bind(1, certId, SQLITE_TRANSIENT);
320
321 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700322 return security::v2::Certificate(statement.getBlock(0));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700323 }
324 else {
325 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
326 }
327}
328
329void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700330CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700331{
332 Sqlite3Statement statement(m_database,
333 R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert)
334 values (?, ?, ?))_SQLTEXT_");
335 statement.bind(1, certId, SQLITE_TRANSIENT);
336 statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
337 statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT);
338
339 if (statement.step() != SQLITE_DONE) {
340 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database"));
341 }
342}
343
344void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700345CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700346{
347 Sqlite3Statement statement(m_database,
348 R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_");
349 statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT);
350 statement.bind(2, certId, SQLITE_TRANSIENT);
351
352 if (statement.step() != SQLITE_DONE) {
353 addCertificate(certId, cert);
354 }
355}
356
357void
358CaSqlite::deleteCertificate(const std::string& certId)
359{
360 Sqlite3Statement statement(m_database,
361 R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_");
362 statement.bind(1, certId, SQLITE_TRANSIENT);
363 statement.step();
364}
365
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700366std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700367CaSqlite::listAllIssuedCertificates()
368{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700369 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700370 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
371
372 while (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400373 result.emplace_back(statement.getBlock(3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700374 }
375 return result;
376}
377
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700378std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700379CaSqlite::listAllIssuedCertificates(const Name& caName)
380{
381 auto allCerts = listAllIssuedCertificates();
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700382 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700383 for (const auto& entry : allCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700384 const auto& klName = entry.getSignature().getKeyLocator().getName();
385 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700386 result.push_back(entry);
387 }
388 }
389 return result;
390}
391
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700392} // namespace ndncert
393} // namespace ndn