blob: e5bca2f887d7bdccb0336aead347a2bf4bee5631 [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 Zhangef6b36a2020-09-22 21:20:59 -070023#include <ndn-cxx/security/v2/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070024#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070025#include <sqlite3.h>
26#include <boost/filesystem.hpp>
27
28namespace ndn {
29namespace ndncert {
30
Davide Pesaventob48bbda2020-07-27 19:41:37 -040031const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070032
33NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
34
35using namespace ndn::util;
36
37static const std::string INITIALIZATION = R"_DBTEXT_(
38CREATE TABLE IF NOT EXISTS
39 CertRequests(
40 id INTEGER PRIMARY KEY,
41 request_id TEXT NOT NULL,
42 ca_name BLOB NOT NULL,
tylerliu182bc532020-09-25 01:54:45 -070043 request_type INTEGER NOT NULL,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 status INTEGER NOT NULL,
45 challenge_status TEXT,
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 Zhangaf7c2902019-03-14 22:13:21 -070049 challenge_secrets TEXT,
50 challenge_tp TEXT,
51 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070052 remaining_time INTEGER,
53 probe_token BLOB
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070054 );
55CREATE UNIQUE INDEX IF NOT EXISTS
56 CertRequestIdIndex ON CertRequests(request_id);
57CREATE UNIQUE INDEX IF NOT EXISTS
58 CertRequestKeyNameIndex ON CertRequests(cert_key_name);
59
60CREATE TABLE IF NOT EXISTS
61 IssuedCerts(
62 id INTEGER PRIMARY KEY,
63 cert_id TEXT NOT NULL,
64 cert_key_name BLOB NOT NULL,
65 cert BLOB NOT NULL
66 );
67CREATE UNIQUE INDEX IF NOT EXISTS
68 IssuedCertRequestIdIndex ON IssuedCerts(cert_id);
69CREATE UNIQUE INDEX IF NOT EXISTS
70 IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name);
71)_DBTEXT_";
72
73CaSqlite::CaSqlite(const std::string& location)
74 : CaStorage()
75{
76 // Determine the path of sqlite db
77 boost::filesystem::path dbDir;
78 if (!location.empty()) {
79 dbDir = boost::filesystem::path(location);
80 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070081 else if (getenv("HOME") != nullptr) {
82 dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
83 }
84 else {
85 dbDir = boost::filesystem::current_path() / ".ndn";
86 }
87 boost::filesystem::create_directories(dbDir);
88
89 // open and initialize database
90 int result = sqlite3_open_v2((dbDir / "ndncert-ca.db").c_str(), &m_database,
91 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
92#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
93 "unix-dotfile"
94#else
95 nullptr
96#endif
97 );
98 if (result != SQLITE_OK)
99 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
100
101 // initialize database specific tables
102 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400103 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700104 nullptr, nullptr, &errorMessage);
105 if (result != SQLITE_OK && errorMessage != nullptr) {
106 sqlite3_free(errorMessage);
107 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be initialized"));
108 }
109}
110
111CaSqlite::~CaSqlite()
112{
113 sqlite3_close(m_database);
114}
115
116CertificateRequest
117CaSqlite::getRequest(const std::string& requestId)
118{
119 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700120 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
121 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
122 challenge_tp, remaining_tries, remaining_time, request_type, probe_token
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700123 FROM CertRequests where request_id = ?)_SQLTEXT_");
124 statement.bind(1, requestId, SQLITE_TRANSIENT);
125
126 if (statement.step() == SQLITE_ROW) {
127 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700128 auto status = static_cast<Status>(statement.getInt(3));
129 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700130 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700131 auto challengeType = statement.getString(7);
132 auto challengeSecrets = statement.getString(8);
133 auto challengeTp = statement.getString(9);
134 auto remainingTries = statement.getInt(10);
135 auto remainingTime = statement.getInt(11);
136 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700137 CertificateRequest request(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138 challengeTp, remainingTime, remainingTries,
139 convertString2Json(challengeSecrets), cert);
tylerliu182bc532020-09-25 01:54:45 -0700140 if (statement.getSize(13) != 0) {
141 shared_ptr<Data> probeToken = make_shared<Data>(statement.getBlock(13));
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700142 request.setProbeToken(probeToken);
143 }
144 return request;
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700145 }
146 else {
147 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database"));
148 }
149}
150
151void
152CaSqlite::addRequest(const CertificateRequest& request)
153{
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700154 // check whether request is there already
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700155 Sqlite3Statement statement1(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700156 R"_SQLTEXT_(SELECT * FROM CertRequests where cert_key_name = ?)_SQLTEXT_");
157 statement1.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700158 if (statement1.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700159 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700160 return;
161 }
162
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700163 // check whether certificate is already issued
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700164 Sqlite3Statement statement2(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700165 R"_SQLTEXT_(SELECT * FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_");
166 statement2.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700167 if (statement2.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700168 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700169 return;
170 }
171
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700172 if (request.m_probeToken != nullptr) {
173 Sqlite3Statement statement(
174 m_database,
175 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700176 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
177 challenge_tp, remaining_tries, remaining_time, request_type, probe_token)
178 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
tylerliu182bc532020-09-25 01:54:45 -0700179 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
180 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700181 statement.bind(3, static_cast<int>(request.m_status));
tylerliu182bc532020-09-25 01:54:45 -0700182 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
183 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
184 SQLITE_TRANSIENT);
185 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
186 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
187 statement.bind(8, convertJson2String(request.m_challengeSecrets),
188 SQLITE_TRANSIENT);
189 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
190 statement.bind(10, request.m_remainingTries);
191 statement.bind(11, request.m_remainingTime);
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700192 statement.bind(12, static_cast<int>(request.m_requestType));
tylerliu182bc532020-09-25 01:54:45 -0700193 statement.bind(13, request.m_probeToken->wireEncode(), SQLITE_TRANSIENT);
194 if (statement.step() != SQLITE_DONE) {
195 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
196 }
197 }
198 else {
199 Sqlite3Statement statement(
200 m_database,
201 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700202 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
203 challenge_tp, remaining_tries, remaining_time, request_type)
204 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700205 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
Suyong Won256c9062020-05-11 02:45:56 -0700206 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700207 statement.bind(3, static_cast<int>(request.m_status));
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700208 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
209 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
210 SQLITE_TRANSIENT);
211 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
212 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
213 statement.bind(8, convertJson2String(request.m_challengeSecrets),
214 SQLITE_TRANSIENT);
215 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
216 statement.bind(10, request.m_remainingTries);
217 statement.bind(11, request.m_remainingTime);
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700218 statement.bind(12, static_cast<int>(request.m_requestType));
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700219 if (statement.step() != SQLITE_DONE) {
220 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
221 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700222 }
223}
224
225void
226CaSqlite::updateRequest(const CertificateRequest& request)
227{
228 Sqlite3Statement statement(m_database,
229 R"_SQLTEXT_(UPDATE CertRequests
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700230 SET status = ?, challenge_status = ?, challenge_type = ?, challenge_secrets = ?,
tylerliu182bc532020-09-25 01:54:45 -0700231 challenge_tp = ?, remaining_tries = ?, remaining_time = ?, request_type = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700232 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700233 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700234 statement.bind(2, request.m_challengeStatus, SQLITE_TRANSIENT);
235 statement.bind(3, request.m_challengeType, SQLITE_TRANSIENT);
236 statement.bind(4, convertJson2String(request.m_challengeSecrets), SQLITE_TRANSIENT);
237 statement.bind(5, request.m_challengeTp, SQLITE_TRANSIENT);
238 statement.bind(6, request.m_remainingTries);
239 statement.bind(7, request.m_remainingTime);
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700240 statement.bind(8, static_cast<int>(request.m_requestType));
tylerliu182bc532020-09-25 01:54:45 -0700241 statement.bind(9, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700242
243 if (statement.step() != SQLITE_DONE) {
244 addRequest(request);
245 }
246}
247
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700248std::list<CertificateRequest>
249CaSqlite::listAllRequests()
250{
251 std::list<CertificateRequest> result;
tylerliu182bc532020-09-25 01:54:45 -0700252 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
253 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
254 challenge_tp, remaining_tries, remaining_time, request_type
255 FROM CertRequests)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700256
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400257 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700258 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700259 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700260 auto status = static_cast<Status>(statement.getInt(3));
261 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700262 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700263 auto challengeType = statement.getString(7);
264 auto challengeSecrets = statement.getString(8);
265 auto challengeTp = statement.getString(9);
266 auto remainingTries = statement.getInt(10);
267 auto remainingTime = statement.getInt(11);
268 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700269 CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700270 challengeTp, remainingTime, remainingTries,
271 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700272 result.push_back(entry);
273 }
274 return result;
275}
276
277std::list<CertificateRequest>
278CaSqlite::listAllRequests(const Name& caName)
279{
280 std::list<CertificateRequest> result;
281 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700282 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
283 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
284 challenge_tp, remaining_tries, remaining_time, request_type
285 FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700286 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
287
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400288 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700289 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700290 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700291 auto status = static_cast<Status>(statement.getInt(3));
292 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700293 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700294 auto challengeType = statement.getString(7);
295 auto challengeSecrets = statement.getString(8);
296 auto challengeTp = statement.getString(9);
297 auto remainingTries = statement.getInt(10);
298 auto remainingTime = statement.getInt(11);
299 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700300 CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700301 challengeTp, remainingTime, remainingTries,
302 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700303 result.push_back(entry);
304 }
305 return result;
306}
307
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700308void
309CaSqlite::deleteRequest(const std::string& requestId)
310{
311 Sqlite3Statement statement(m_database,
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700312 R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700313 statement.bind(1, requestId, SQLITE_TRANSIENT);
314 statement.step();
315}
316
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700317security::v2::Certificate
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700318CaSqlite::getCertificate(const std::string& certId)
319{
320 Sqlite3Statement statement(m_database,
321 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
322 statement.bind(1, certId, SQLITE_TRANSIENT);
323
324 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700325 return security::v2::Certificate(statement.getBlock(0));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700326 }
327 else {
328 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
329 }
330}
331
332void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700333CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700334{
335 Sqlite3Statement statement(m_database,
336 R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert)
337 values (?, ?, ?))_SQLTEXT_");
338 statement.bind(1, certId, SQLITE_TRANSIENT);
339 statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
340 statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT);
341
342 if (statement.step() != SQLITE_DONE) {
343 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database"));
344 }
345}
346
347void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700348CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700349{
350 Sqlite3Statement statement(m_database,
351 R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_");
352 statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT);
353 statement.bind(2, certId, SQLITE_TRANSIENT);
354
355 if (statement.step() != SQLITE_DONE) {
356 addCertificate(certId, cert);
357 }
358}
359
360void
361CaSqlite::deleteCertificate(const std::string& certId)
362{
363 Sqlite3Statement statement(m_database,
364 R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_");
365 statement.bind(1, certId, SQLITE_TRANSIENT);
366 statement.step();
367}
368
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700369std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700370CaSqlite::listAllIssuedCertificates()
371{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700372 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700373 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
374
375 while (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400376 result.emplace_back(statement.getBlock(3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700377 }
378 return result;
379}
380
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700381std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700382CaSqlite::listAllIssuedCertificates(const Name& caName)
383{
384 auto allCerts = listAllIssuedCertificates();
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700385 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700386 for (const auto& entry : allCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700387 const auto& klName = entry.getSignature().getKeyLocator().getName();
388 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700389 result.push_back(entry);
390 }
391 }
392 return result;
393}
394
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700395std::string
396CaSqlite::convertJson2String(const JsonSection& json)
397{
398 std::stringstream ss;
399 boost::property_tree::write_json(ss, json);
400 return ss.str();
401}
402
403JsonSection
404CaSqlite::convertString2Json(const std::string& jsonContent)
405{
406 std::istringstream ss(jsonContent);
407 JsonSection json;
408 boost::property_tree::json_parser::read_json(ss, json);
409 return json;
410}
411
412} // namespace ndncert
413} // namespace ndn