Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 22 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/security/v2/validation-policy.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 25 | #include <sqlite3.h> |
| 26 | #include <boost/filesystem.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 31 | const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3"; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 32 | |
| 33 | NDNCERT_REGISTER_CA_STORAGE(CaSqlite); |
| 34 | |
| 35 | using namespace ndn::util; |
| 36 | |
| 37 | static const std::string INITIALIZATION = R"_DBTEXT_( |
| 38 | CREATE TABLE IF NOT EXISTS |
| 39 | CertRequests( |
| 40 | id INTEGER PRIMARY KEY, |
| 41 | request_id TEXT NOT NULL, |
| 42 | ca_name BLOB NOT NULL, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 43 | request_type INTEGER NOT NULL, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | status INTEGER NOT NULL, |
| 45 | challenge_status TEXT, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 46 | cert_key_name BLOB NOT NULL, |
| 47 | cert_request BLOB NOT NULL, |
| 48 | challenge_type TEXT, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | challenge_secrets TEXT, |
| 50 | challenge_tp TEXT, |
| 51 | remaining_tries INTEGER, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 52 | remaining_time INTEGER, |
| 53 | probe_token BLOB |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 54 | ); |
| 55 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 56 | CertRequestIdIndex ON CertRequests(request_id); |
| 57 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 58 | CertRequestKeyNameIndex ON CertRequests(cert_key_name); |
| 59 | |
| 60 | CREATE 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 | ); |
| 67 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 68 | IssuedCertRequestIdIndex ON IssuedCerts(cert_id); |
| 69 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 70 | IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name); |
| 71 | )_DBTEXT_"; |
| 72 | |
| 73 | CaSqlite::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 Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 81 | 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 Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 103 | result = sqlite3_exec(m_database, INITIALIZATION.data(), |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 104 | 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 | |
| 111 | CaSqlite::~CaSqlite() |
| 112 | { |
| 113 | sqlite3_close(m_database); |
| 114 | } |
| 115 | |
| 116 | CertificateRequest |
| 117 | CaSqlite::getRequest(const std::string& requestId) |
| 118 | { |
| 119 | Sqlite3Statement statement(m_database, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 120 | 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 Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 123 | 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 Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 128 | auto status = static_cast<Status>(statement.getInt(3)); |
| 129 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 130 | security::v2::Certificate cert(statement.getBlock(6)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 131 | 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)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 137 | CertificateRequest request(caName, requestId, requestType, status, challengeStatus, challengeType, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 138 | challengeTp, remainingTime, remainingTries, |
| 139 | convertString2Json(challengeSecrets), cert); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 140 | return request; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 141 | } |
| 142 | else { |
| 143 | BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database")); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | CaSqlite::addRequest(const CertificateRequest& request) |
| 149 | { |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 150 | // check whether request is there already |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 151 | Sqlite3Statement statement1(m_database, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 152 | R"_SQLTEXT_(SELECT * FROM CertRequests where cert_key_name = ?)_SQLTEXT_"); |
| 153 | statement1.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 154 | if (statement1.step() == SQLITE_ROW) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 155 | BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 159 | // check whether certificate is already issued |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 160 | Sqlite3Statement statement2(m_database, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | R"_SQLTEXT_(SELECT * FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_"); |
| 162 | statement2.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 163 | if (statement2.step() == SQLITE_ROW) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame^] | 168 | Sqlite3Statement statement( |
| 169 | m_database, |
| 170 | R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status, |
| 171 | challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets, |
| 172 | challenge_tp, remaining_tries, remaining_time, request_type) |
| 173 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_"); |
| 174 | statement.bind(1, request.m_requestId, SQLITE_TRANSIENT); |
| 175 | statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT); |
| 176 | statement.bind(3, static_cast<int>(request.m_status)); |
| 177 | statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT); |
| 178 | statement.bind(5, request.m_cert.getKeyName().wireEncode(), |
| 179 | SQLITE_TRANSIENT); |
| 180 | statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT); |
| 181 | statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT); |
| 182 | statement.bind(8, convertJson2String(request.m_challengeSecrets), |
| 183 | SQLITE_TRANSIENT); |
| 184 | statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT); |
| 185 | statement.bind(10, request.m_remainingTries); |
| 186 | statement.bind(11, request.m_remainingTime); |
| 187 | statement.bind(12, static_cast<int>(request.m_requestType)); |
| 188 | if (statement.step() != SQLITE_DONE) { |
| 189 | BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | CaSqlite::updateRequest(const CertificateRequest& request) |
| 195 | { |
| 196 | Sqlite3Statement statement(m_database, |
| 197 | R"_SQLTEXT_(UPDATE CertRequests |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 198 | SET status = ?, challenge_status = ?, challenge_type = ?, challenge_secrets = ?, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 199 | challenge_tp = ?, remaining_tries = ?, remaining_time = ?, request_type = ? |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 200 | WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 201 | statement.bind(1, static_cast<int>(request.m_status)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 202 | statement.bind(2, request.m_challengeStatus, SQLITE_TRANSIENT); |
| 203 | statement.bind(3, request.m_challengeType, SQLITE_TRANSIENT); |
| 204 | statement.bind(4, convertJson2String(request.m_challengeSecrets), SQLITE_TRANSIENT); |
| 205 | statement.bind(5, request.m_challengeTp, SQLITE_TRANSIENT); |
| 206 | statement.bind(6, request.m_remainingTries); |
| 207 | statement.bind(7, request.m_remainingTime); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 208 | statement.bind(8, static_cast<int>(request.m_requestType)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 209 | statement.bind(9, request.m_requestId, SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 210 | |
| 211 | if (statement.step() != SQLITE_DONE) { |
| 212 | addRequest(request); |
| 213 | } |
| 214 | } |
| 215 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 216 | std::list<CertificateRequest> |
| 217 | CaSqlite::listAllRequests() |
| 218 | { |
| 219 | std::list<CertificateRequest> result; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 220 | Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
| 221 | challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets, |
| 222 | challenge_tp, remaining_tries, remaining_time, request_type |
| 223 | FROM CertRequests)_SQLTEXT_"); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 224 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 225 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 226 | auto requestId = statement.getString(1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 227 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 228 | auto status = static_cast<Status>(statement.getInt(3)); |
| 229 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 230 | security::v2::Certificate cert(statement.getBlock(6)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 231 | auto challengeType = statement.getString(7); |
| 232 | auto challengeSecrets = statement.getString(8); |
| 233 | auto challengeTp = statement.getString(9); |
| 234 | auto remainingTries = statement.getInt(10); |
| 235 | auto remainingTime = statement.getInt(11); |
| 236 | auto requestType = static_cast<RequestType>(statement.getInt(12)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 237 | CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 238 | challengeTp, remainingTime, remainingTries, |
| 239 | convertString2Json(challengeSecrets), cert); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 240 | result.push_back(entry); |
| 241 | } |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | std::list<CertificateRequest> |
| 246 | CaSqlite::listAllRequests(const Name& caName) |
| 247 | { |
| 248 | std::list<CertificateRequest> result; |
| 249 | Sqlite3Statement statement(m_database, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 250 | R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
| 251 | challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets, |
| 252 | challenge_tp, remaining_tries, remaining_time, request_type |
| 253 | FROM CertRequests WHERE ca_name = ?)_SQLTEXT_"); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 254 | statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT); |
| 255 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 256 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 257 | auto requestId = statement.getString(1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 258 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 259 | auto status = static_cast<Status>(statement.getInt(3)); |
| 260 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 261 | security::v2::Certificate cert(statement.getBlock(6)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 262 | auto challengeType = statement.getString(7); |
| 263 | auto challengeSecrets = statement.getString(8); |
| 264 | auto challengeTp = statement.getString(9); |
| 265 | auto remainingTries = statement.getInt(10); |
| 266 | auto remainingTime = statement.getInt(11); |
| 267 | auto requestType = static_cast<RequestType>(statement.getInt(12)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 268 | CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 269 | challengeTp, remainingTime, remainingTries, |
| 270 | convertString2Json(challengeSecrets), cert); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 271 | result.push_back(entry); |
| 272 | } |
| 273 | return result; |
| 274 | } |
| 275 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 276 | void |
| 277 | CaSqlite::deleteRequest(const std::string& requestId) |
| 278 | { |
| 279 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 280 | R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 281 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 282 | statement.step(); |
| 283 | } |
| 284 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 285 | security::v2::Certificate |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 286 | CaSqlite::getCertificate(const std::string& certId) |
| 287 | { |
| 288 | Sqlite3Statement statement(m_database, |
| 289 | R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_"); |
| 290 | statement.bind(1, certId, SQLITE_TRANSIENT); |
| 291 | |
| 292 | if (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 293 | return security::v2::Certificate(statement.getBlock(0)); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 294 | } |
| 295 | else { |
| 296 | BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database")); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 301 | CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 302 | { |
| 303 | Sqlite3Statement statement(m_database, |
| 304 | R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert) |
| 305 | values (?, ?, ?))_SQLTEXT_"); |
| 306 | statement.bind(1, certId, SQLITE_TRANSIENT); |
| 307 | statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT); |
| 308 | statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT); |
| 309 | |
| 310 | if (statement.step() != SQLITE_DONE) { |
| 311 | BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database")); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 316 | CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 317 | { |
| 318 | Sqlite3Statement statement(m_database, |
| 319 | R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_"); |
| 320 | statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT); |
| 321 | statement.bind(2, certId, SQLITE_TRANSIENT); |
| 322 | |
| 323 | if (statement.step() != SQLITE_DONE) { |
| 324 | addCertificate(certId, cert); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | CaSqlite::deleteCertificate(const std::string& certId) |
| 330 | { |
| 331 | Sqlite3Statement statement(m_database, |
| 332 | R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_"); |
| 333 | statement.bind(1, certId, SQLITE_TRANSIENT); |
| 334 | statement.step(); |
| 335 | } |
| 336 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 337 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 338 | CaSqlite::listAllIssuedCertificates() |
| 339 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 340 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 341 | Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_"); |
| 342 | |
| 343 | while (statement.step() == SQLITE_ROW) { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 344 | result.emplace_back(statement.getBlock(3)); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 345 | } |
| 346 | return result; |
| 347 | } |
| 348 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 349 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 350 | CaSqlite::listAllIssuedCertificates(const Name& caName) |
| 351 | { |
| 352 | auto allCerts = listAllIssuedCertificates(); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 353 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 354 | for (const auto& entry : allCerts) { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 355 | const auto& klName = entry.getSignature().getKeyLocator().getName(); |
| 356 | if (security::v2::extractIdentityFromKeyName(klName) == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 357 | result.push_back(entry); |
| 358 | } |
| 359 | } |
| 360 | return result; |
| 361 | } |
| 362 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 363 | std::string |
| 364 | CaSqlite::convertJson2String(const JsonSection& json) |
| 365 | { |
| 366 | std::stringstream ss; |
| 367 | boost::property_tree::write_json(ss, json); |
| 368 | return ss.str(); |
| 369 | } |
| 370 | |
| 371 | JsonSection |
| 372 | CaSqlite::convertString2Json(const std::string& jsonContent) |
| 373 | { |
| 374 | std::istringstream ss(jsonContent); |
| 375 | JsonSection json; |
| 376 | boost::property_tree::json_parser::read_json(ss, json); |
| 377 | return json; |
| 378 | } |
| 379 | |
| 380 | } // namespace ndncert |
| 381 | } // namespace ndn |