blob: d392bdd900a89e17ca1b145ed2a1c871600df18c [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
tylerliu63900d52020-09-30 03:01:18 -070040 RequestStates(
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070041 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
tylerliu63900d52020-09-30 03:01:18 -070057 RequestStateIdIndex ON RequestStates(request_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070058CREATE UNIQUE INDEX IF NOT EXISTS
tylerliu63900d52020-09-30 03:01:18 -070059 RequestStateKeyNameIndex ON RequestStates(cert_key_name);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070060
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
tylerliu63900d52020-09-30 03:01:18 -070069 IssuedCertIdIndex ON IssuedCerts(cert_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070070CREATE 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
tylerliu63900d52020-09-30 03:01:18 -0700125 FROM RequestStates where request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700126 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{
tylerliu63900d52020-09-30 03:01:18 -0700158
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700159 // check whether request is there already
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700160 auto keyNameTlv = request.m_cert.getKeyName().wireEncode();
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700161 Sqlite3Statement statement1(m_database,
tylerliu63900d52020-09-30 03:01:18 -0700162 R"_SQLTEXT_(SELECT 1 FROM RequestStates where cert_key_name = ?)_SQLTEXT_");
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700163 statement1.bind(1, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700164 if (statement1.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700165 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700166 }
167
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700168 // check whether certificate is already issued
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700169 Sqlite3Statement statement2(m_database,
tylerliu63900d52020-09-30 03:01:18 -0700170 R"_SQLTEXT_(SELECT 1 FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_");
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700171 statement2.bind(1, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700172 if (statement2.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700173 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700174 }
175
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700176 Sqlite3Statement statement(
177 m_database,
tylerliu63900d52020-09-30 03:01:18 -0700178 R"_SQLTEXT_(INSERT OR ABORT INTO RequestStates (request_id, ca_name, status, request_type,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700179 cert_key_name, cert_request, challenge_type, challenge_status, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700180 challenge_tp, remaining_tries, remaining_time, encryption_key)
181 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700182 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
183 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
184 statement.bind(3, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700185 statement.bind(4, static_cast<int>(request.m_requestType));
186 statement.bind(5, keyNameTlv, SQLITE_TRANSIENT);
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700187 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
tylerliu8e170d62020-09-30 01:31:53 -0700188 statement.bind(13, request.m_encryptionKey, SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700189 if (request.m_challengeState) {
190 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
191 statement.bind(8, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
192 statement.bind(9, convertJson2String(request.m_challengeState->m_secrets),
193 SQLITE_TRANSIENT);
194 statement.bind(10, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
195 statement.bind(11, request.m_challengeState->m_remainingTries);
196 statement.bind(12, request.m_challengeState->m_remainingTime.count());
197 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700198 if (statement.step() != SQLITE_DONE) {
199 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700200 }
201}
202
203void
Zhiyi Zhange232a742020-09-29 17:34:17 -0700204CaSqlite::updateRequest(const RequestState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700205{
206 Sqlite3Statement statement(m_database,
tylerliu63900d52020-09-30 03:01:18 -0700207 R"_SQLTEXT_(UPDATE RequestStates
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700208 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
209 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700210 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700211 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700212 statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT);
213 if (request.m_challengeState) {
214 statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
215 statement.bind(4, convertJson2String(request.m_challengeState->m_secrets),
216 SQLITE_TRANSIENT);
217 statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
218 statement.bind(6, request.m_challengeState->m_remainingTries);
219 statement.bind(7, request.m_challengeState->m_remainingTime.count());
220 }
221 else {
222 statement.bind(3, "", SQLITE_TRANSIENT);
223 statement.bind(4, "", SQLITE_TRANSIENT);
224 statement.bind(5, "", SQLITE_TRANSIENT);
225 statement.bind(6, 0);
226 statement.bind(7, 0);
227 }
228 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700229
230 if (statement.step() != SQLITE_DONE) {
231 addRequest(request);
232 }
233}
234
Zhiyi Zhange232a742020-09-29 17:34:17 -0700235std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700236CaSqlite::listAllRequests()
237{
Zhiyi Zhange232a742020-09-29 17:34:17 -0700238 std::list<RequestState> result;
tylerliu182bc532020-09-25 01:54:45 -0700239 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
240 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700241 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu63900d52020-09-30 03:01:18 -0700242 FROM RequestStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400243 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700244 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700245 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700246 auto status = static_cast<Status>(statement.getInt(3));
247 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700248 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700249 auto challengeType = statement.getString(7);
250 auto challengeSecrets = statement.getString(8);
251 auto challengeTp = statement.getString(9);
252 auto remainingTries = statement.getInt(10);
253 auto remainingTime = statement.getInt(11);
254 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu8e170d62020-09-30 01:31:53 -0700255 auto encryptionKey = statement.getBlock(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700256 if (challengeType != "") {
Zhiyi Zhange232a742020-09-29 17:34:17 -0700257 result.push_back(RequestState(caName, requestId, requestType, status, cert,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700258 challengeType, challengeStatus, time::fromIsoString(challengeTp),
259 remainingTries, time::seconds(remainingTime),
tylerliu8e170d62020-09-30 01:31:53 -0700260 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700261 }
262 else {
tylerliu8e170d62020-09-30 01:31:53 -0700263 result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700264 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700265 }
266 return result;
267}
268
Zhiyi Zhange232a742020-09-29 17:34:17 -0700269std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700270CaSqlite::listAllRequests(const Name& caName)
271{
Zhiyi Zhange232a742020-09-29 17:34:17 -0700272 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700273 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700274 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
275 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700276 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu63900d52020-09-30 03:01:18 -0700277 FROM RequestStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700278 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
279
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400280 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700281 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700282 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700283 auto status = static_cast<Status>(statement.getInt(3));
284 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700285 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700286 auto challengeType = statement.getString(7);
287 auto challengeSecrets = statement.getString(8);
288 auto challengeTp = statement.getString(9);
289 auto remainingTries = statement.getInt(10);
290 auto remainingTime = statement.getInt(11);
291 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu8e170d62020-09-30 01:31:53 -0700292 auto encryptionKey = statement.getBlock(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700293 if (challengeType != "") {
Zhiyi Zhange232a742020-09-29 17:34:17 -0700294 result.push_back(RequestState(caName, requestId, requestType, status, cert,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700295 challengeType, challengeStatus, time::fromIsoString(challengeTp),
296 remainingTries, time::seconds(remainingTime),
tylerliu8e170d62020-09-30 01:31:53 -0700297 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700298 }
299 else {
tylerliu8e170d62020-09-30 01:31:53 -0700300 result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700301 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700302 }
303 return result;
304}
305
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700306void
307CaSqlite::deleteRequest(const std::string& requestId)
308{
309 Sqlite3Statement statement(m_database,
tylerliu63900d52020-09-30 03:01:18 -0700310 R"_SQLTEXT_(DELETE FROM RequestStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700311 statement.bind(1, requestId, SQLITE_TRANSIENT);
312 statement.step();
313}
314
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700315security::v2::Certificate
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700316CaSqlite::getCertificate(const std::string& certId)
317{
318 Sqlite3Statement statement(m_database,
319 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
320 statement.bind(1, certId, SQLITE_TRANSIENT);
321
322 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700323 return security::v2::Certificate(statement.getBlock(0));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700324 }
325 else {
326 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
327 }
328}
329
330void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700331CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700332{
333 Sqlite3Statement statement(m_database,
334 R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert)
335 values (?, ?, ?))_SQLTEXT_");
336 statement.bind(1, certId, SQLITE_TRANSIENT);
337 statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
338 statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT);
339
340 if (statement.step() != SQLITE_DONE) {
341 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database"));
342 }
343}
344
345void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700346CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700347{
348 Sqlite3Statement statement(m_database,
349 R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_");
350 statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT);
351 statement.bind(2, certId, SQLITE_TRANSIENT);
352
353 if (statement.step() != SQLITE_DONE) {
354 addCertificate(certId, cert);
355 }
356}
357
358void
359CaSqlite::deleteCertificate(const std::string& certId)
360{
361 Sqlite3Statement statement(m_database,
362 R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_");
363 statement.bind(1, certId, SQLITE_TRANSIENT);
364 statement.step();
365}
366
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700367std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700368CaSqlite::listAllIssuedCertificates()
369{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700370 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700371 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
372
373 while (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400374 result.emplace_back(statement.getBlock(3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700375 }
376 return result;
377}
378
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700379std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700380CaSqlite::listAllIssuedCertificates(const Name& caName)
381{
382 auto allCerts = listAllIssuedCertificates();
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700383 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700384 for (const auto& entry : allCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700385 const auto& klName = entry.getSignature().getKeyLocator().getName();
386 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700387 result.push_back(entry);
388 }
389 }
390 return result;
391}
392
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700393} // namespace ndncert
394} // namespace ndn