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 | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 23 | #include <sqlite3.h> |
| 24 | |
| 25 | #include <boost/filesystem.hpp> |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/v2/validation-policy.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 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 |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 40 | RequestStates( |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 41 | id INTEGER PRIMARY KEY, |
| 42 | request_id TEXT NOT NULL, |
| 43 | ca_name BLOB NOT NULL, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 44 | request_type INTEGER NOT NULL, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | status INTEGER NOT NULL, |
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 | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 49 | challenge_status TEXT, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 50 | challenge_tp TEXT, |
| 51 | remaining_tries INTEGER, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 52 | remaining_time INTEGER, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 53 | challenge_secrets TEXT, |
| 54 | encryption_key BLOB NOT NULL |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 55 | ); |
| 56 | CREATE UNIQUE INDEX IF NOT EXISTS |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 57 | RequestStateIdIndex ON RequestStates(request_id); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 58 | CREATE UNIQUE INDEX IF NOT EXISTS |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 59 | RequestStateKeyNameIndex ON RequestStates(cert_key_name); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 60 | |
| 61 | CREATE 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 | ); |
| 68 | CREATE UNIQUE INDEX IF NOT EXISTS |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 69 | IssuedCertIdIndex ON IssuedCerts(cert_id); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 70 | CREATE UNIQUE INDEX IF NOT EXISTS |
| 71 | IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name); |
| 72 | )_DBTEXT_"; |
| 73 | |
| 74 | CaSqlite::CaSqlite(const std::string& location) |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 75 | : CaStorage() |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 76 | { |
| 77 | // Determine the path of sqlite db |
| 78 | boost::filesystem::path dbDir; |
| 79 | if (!location.empty()) { |
| 80 | dbDir = boost::filesystem::path(location); |
| 81 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 82 | 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 Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 98 | ); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 99 | 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 Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 104 | result = sqlite3_exec(m_database, INITIALIZATION.data(), |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 105 | 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 | |
| 112 | CaSqlite::~CaSqlite() |
| 113 | { |
| 114 | sqlite3_close(m_database); |
| 115 | } |
| 116 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 117 | RequestState |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 118 | CaSqlite::getRequest(const std::string& requestId) |
| 119 | { |
| 120 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 121 | R"_SQLTEXT_(SELECT id, ca_name, status, |
| 122 | challenge_status, cert_request, |
| 123 | challenge_type, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 124 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 125 | FROM RequestStates where request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 126 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 127 | |
| 128 | if (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 129 | 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)); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 139 | auto encryptionKey = statement.getBlock(11); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 140 | if (challengeType != "") { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 141 | return RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 142 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 143 | remainingTries, time::seconds(remainingTime), |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 144 | convertString2Json(challengeSecrets), encryptionKey); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 145 | } |
| 146 | else { |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 147 | return RequestState(caName, requestId, requestType, status, cert, encryptionKey); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 148 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 149 | } |
| 150 | else { |
| 151 | BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database")); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 156 | CaSqlite::addRequest(const RequestState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 157 | { |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 158 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 159 | // check whether request is there already |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 160 | auto keyNameTlv = request.m_cert.getKeyName().wireEncode(); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 161 | Sqlite3Statement statement1(m_database, |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 162 | R"_SQLTEXT_(SELECT 1 FROM RequestStates where cert_key_name = ?)_SQLTEXT_"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 163 | statement1.bind(1, keyNameTlv, SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 164 | if (statement1.step() == SQLITE_ROW) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 165 | 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] | 166 | } |
| 167 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 168 | // check whether certificate is already issued |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 169 | Sqlite3Statement statement2(m_database, |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 170 | R"_SQLTEXT_(SELECT 1 FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 171 | statement2.bind(1, keyNameTlv, SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 172 | if (statement2.step() == SQLITE_ROW) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 173 | 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] | 174 | } |
| 175 | |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 176 | Sqlite3Statement statement( |
| 177 | m_database, |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 178 | R"_SQLTEXT_(INSERT OR ABORT INTO RequestStates (request_id, ca_name, status, request_type, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 179 | cert_key_name, cert_request, challenge_type, challenge_status, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 180 | challenge_tp, remaining_tries, remaining_time, encryption_key) |
| 181 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_"); |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 182 | 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 Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 185 | statement.bind(4, static_cast<int>(request.m_requestType)); |
| 186 | statement.bind(5, keyNameTlv, SQLITE_TRANSIENT); |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 187 | statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 188 | statement.bind(13, request.m_encryptionKey, SQLITE_TRANSIENT); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 189 | 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 Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 198 | if (statement.step() != SQLITE_DONE) { |
| 199 | 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] | 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 204 | CaSqlite::updateRequest(const RequestState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 205 | { |
| 206 | Sqlite3Statement statement(m_database, |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 207 | R"_SQLTEXT_(UPDATE RequestStates |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 208 | SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?, |
| 209 | challenge_tp = ?, remaining_tries = ?, remaining_time = ? |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 210 | WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 211 | statement.bind(1, static_cast<int>(request.m_status)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 212 | 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 Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 229 | |
| 230 | if (statement.step() != SQLITE_DONE) { |
| 231 | addRequest(request); |
| 232 | } |
| 233 | } |
| 234 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 235 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 236 | CaSqlite::listAllRequests() |
| 237 | { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 238 | std::list<RequestState> result; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 239 | 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, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 241 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 242 | FROM RequestStates)_SQLTEXT_"); |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 243 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 244 | auto requestId = statement.getString(1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 245 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 246 | auto status = static_cast<Status>(statement.getInt(3)); |
| 247 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 248 | security::v2::Certificate cert(statement.getBlock(6)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 249 | 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)); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 255 | auto encryptionKey = statement.getBlock(13); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 256 | if (challengeType != "") { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 257 | result.push_back(RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 258 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 259 | remainingTries, time::seconds(remainingTime), |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 260 | convertString2Json(challengeSecrets), encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 261 | } |
| 262 | else { |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 263 | result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 264 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 265 | } |
| 266 | return result; |
| 267 | } |
| 268 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 269 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 270 | CaSqlite::listAllRequests(const Name& caName) |
| 271 | { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 272 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 273 | Sqlite3Statement statement(m_database, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 274 | R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
| 275 | challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 276 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 277 | FROM RequestStates WHERE ca_name = ?)_SQLTEXT_"); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 278 | statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT); |
| 279 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 280 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 281 | auto requestId = statement.getString(1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 282 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 283 | auto status = static_cast<Status>(statement.getInt(3)); |
| 284 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 285 | security::v2::Certificate cert(statement.getBlock(6)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 286 | 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)); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 292 | auto encryptionKey = statement.getBlock(13); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 293 | if (challengeType != "") { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 294 | result.push_back(RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 295 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 296 | remainingTries, time::seconds(remainingTime), |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 297 | convertString2Json(challengeSecrets), encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 298 | } |
| 299 | else { |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 300 | result.push_back(RequestState(caName, requestId, requestType, status, cert, encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 301 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 302 | } |
| 303 | return result; |
| 304 | } |
| 305 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 306 | void |
| 307 | CaSqlite::deleteRequest(const std::string& requestId) |
| 308 | { |
| 309 | Sqlite3Statement statement(m_database, |
tylerliu | 63900d5 | 2020-09-30 03:01:18 -0700 | [diff] [blame] | 310 | R"_SQLTEXT_(DELETE FROM RequestStates WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 311 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 312 | statement.step(); |
| 313 | } |
| 314 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 315 | security::v2::Certificate |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 316 | CaSqlite::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 Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 323 | return security::v2::Certificate(statement.getBlock(0)); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 324 | } |
| 325 | else { |
| 326 | BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database")); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 331 | CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 332 | { |
| 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 | |
| 345 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 346 | CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 347 | { |
| 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 | |
| 358 | void |
| 359 | CaSqlite::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 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() |
| 369 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 370 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 371 | Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_"); |
| 372 | |
| 373 | while (statement.step() == SQLITE_ROW) { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 374 | result.emplace_back(statement.getBlock(3)); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 375 | } |
| 376 | return result; |
| 377 | } |
| 378 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 379 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 380 | CaSqlite::listAllIssuedCertificates(const Name& caName) |
| 381 | { |
| 382 | auto allCerts = listAllIssuedCertificates(); |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 383 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 384 | for (const auto& entry : allCerts) { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 385 | const auto& klName = entry.getSignature().getKeyLocator().getName(); |
| 386 | if (security::v2::extractIdentityFromKeyName(klName) == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 387 | result.push_back(entry); |
| 388 | } |
| 389 | } |
| 390 | return result; |
| 391 | } |
| 392 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 393 | } // namespace ndncert |
| 394 | } // namespace ndn |