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