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 | |
Zhiyi Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/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> |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/validation-policy.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndncert { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 30 | namespace ca { |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 31 | |
Zhiyi Zhang | 5981223 | 2020-10-12 13:11:35 -0700 | [diff] [blame] | 32 | using namespace ndn::util; |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 33 | const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3"; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 34 | |
| 35 | NDNCERT_REGISTER_CA_STORAGE(CaSqlite); |
| 36 | |
Zhiyi Zhang | 5981223 | 2020-10-12 13:11:35 -0700 | [diff] [blame] | 37 | std::string |
| 38 | convertJson2String(const JsonSection& json) |
| 39 | { |
| 40 | std::stringstream ss; |
| 41 | boost::property_tree::write_json(ss, json); |
| 42 | return ss.str(); |
| 43 | } |
| 44 | |
| 45 | JsonSection |
| 46 | convertString2Json(const std::string& jsonContent) |
| 47 | { |
| 48 | std::istringstream ss(jsonContent); |
| 49 | JsonSection json; |
| 50 | boost::property_tree::json_parser::read_json(ss, json); |
| 51 | return json; |
| 52 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 53 | |
| 54 | static const std::string INITIALIZATION = R"_DBTEXT_( |
| 55 | CREATE TABLE IF NOT EXISTS |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 56 | RequestStates( |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 57 | id INTEGER PRIMARY KEY, |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 58 | request_id BLOB NOT NULL, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 59 | ca_name BLOB NOT NULL, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 60 | request_type INTEGER NOT NULL, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | status INTEGER NOT NULL, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 62 | cert_request BLOB NOT NULL, |
| 63 | challenge_type TEXT, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 64 | challenge_status TEXT, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 65 | challenge_tp TEXT, |
| 66 | remaining_tries INTEGER, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 67 | remaining_time INTEGER, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 68 | challenge_secrets TEXT, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 69 | encryption_key BLOB NOT NULL, |
| 70 | aes_block_counter INTEGER |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 71 | ); |
| 72 | CREATE UNIQUE INDEX IF NOT EXISTS |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 73 | RequestStateIdIndex ON RequestStates(request_id); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 74 | )_DBTEXT_"; |
| 75 | |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 76 | CaSqlite::CaSqlite(const Name& caName, const std::string& path) |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 77 | : CaStorage() |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 78 | { |
| 79 | // Determine the path of sqlite db |
| 80 | boost::filesystem::path dbDir; |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 81 | if (!path.empty()) { |
| 82 | dbDir = boost::filesystem::path(path); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 83 | } |
| 84 | else { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 85 | std::string dbName = caName.toUri(); |
| 86 | std::replace(dbName.begin(), dbName.end(), '/', '_'); |
| 87 | dbName += ".db"; |
| 88 | if (getenv("HOME") != nullptr) { |
| 89 | dbDir = boost::filesystem::path(getenv("HOME")) / ".ndncert"; |
| 90 | } |
| 91 | else { |
| 92 | dbDir = boost::filesystem::current_path() / ".ndncert"; |
| 93 | } |
| 94 | boost::filesystem::create_directories(dbDir); |
| 95 | dbDir /= dbName; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 96 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 97 | |
| 98 | // open and initialize database |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 99 | int result = sqlite3_open_v2(dbDir.c_str(), &m_database, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 100 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 101 | #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING |
| 102 | "unix-dotfile" |
| 103 | #else |
| 104 | nullptr |
| 105 | #endif |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 106 | ); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 107 | if (result != SQLITE_OK) |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 108 | NDN_THROW(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string())); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 109 | |
| 110 | // initialize database specific tables |
| 111 | char* errorMessage = nullptr; |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 112 | result = sqlite3_exec(m_database, INITIALIZATION.data(), |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 113 | nullptr, nullptr, &errorMessage); |
| 114 | if (result != SQLITE_OK && errorMessage != nullptr) { |
| 115 | sqlite3_free(errorMessage); |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 116 | NDN_THROW(std::runtime_error("CaSqlite DB cannot be initialized")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | CaSqlite::~CaSqlite() |
| 121 | { |
| 122 | sqlite3_close(m_database); |
| 123 | } |
| 124 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 125 | RequestState |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 126 | CaSqlite::getRequest(const RequestId& requestId) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 127 | { |
| 128 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 129 | R"_SQLTEXT_(SELECT id, ca_name, status, |
| 130 | challenge_status, cert_request, |
| 131 | challenge_type, challenge_secrets, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 132 | challenge_tp, remaining_tries, remaining_time, |
| 133 | request_type, encryption_key, aes_block_counter |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 134 | FROM RequestStates where request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 135 | statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 136 | |
| 137 | if (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 138 | Name caName(statement.getBlock(1)); |
| 139 | auto status = static_cast<Status>(statement.getInt(2)); |
| 140 | auto challengeStatus = statement.getString(3); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 141 | security::Certificate cert(statement.getBlock(4)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 142 | auto challengeType = statement.getString(5); |
| 143 | auto challengeSecrets = statement.getString(6); |
| 144 | auto challengeTp = statement.getString(7); |
| 145 | auto remainingTries = statement.getInt(8); |
| 146 | auto remainingTime = statement.getInt(9); |
| 147 | auto requestType = static_cast<RequestType>(statement.getInt(10)); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 148 | std::array<uint8_t, 16> encryptionKey; |
| 149 | std::memcpy(encryptionKey.data(), statement.getBlob(11), statement.getSize(11)); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 150 | auto aesCounter = statement.getInt(12); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 151 | if (challengeType != "") { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 152 | return RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 153 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 154 | remainingTries, time::seconds(remainingTime), |
| 155 | convertString2Json(challengeSecrets), std::move(encryptionKey), aesCounter); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 156 | } |
| 157 | else { |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 158 | return RequestState(caName, requestId, requestType, status, cert, std::move(encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 159 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 160 | } |
| 161 | else { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 162 | NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) + " cannot be fetched from database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | void |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 167 | CaSqlite::addRequest(const RequestState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 168 | { |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 169 | Sqlite3Statement statement( |
| 170 | m_database, |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 171 | R"_SQLTEXT_(INSERT OR ABORT INTO RequestStates (request_id, ca_name, status, request_type, |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 172 | cert_request, challenge_type, challenge_status, challenge_secrets, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 173 | challenge_tp, remaining_tries, remaining_time, encryption_key, aes_block_counter) |
| 174 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_"); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 175 | statement.bind(1, request.requestId.data(), request.requestId.size(), SQLITE_TRANSIENT); |
| 176 | statement.bind(2, request.caPrefix.wireEncode(), SQLITE_TRANSIENT); |
| 177 | statement.bind(3, static_cast<int>(request.status)); |
| 178 | statement.bind(4, static_cast<int>(request.requestType)); |
| 179 | statement.bind(5, request.cert.wireEncode(), SQLITE_TRANSIENT); |
| 180 | statement.bind(12, request.encryptionKey.data(), request.encryptionKey.size(), SQLITE_TRANSIENT); |
| 181 | statement.bind(13, request.aesBlockCounter); |
| 182 | if (request.challengeState) { |
| 183 | statement.bind(6, request.challengeType, SQLITE_TRANSIENT); |
| 184 | statement.bind(7, request.challengeState->challengeStatus, SQLITE_TRANSIENT); |
| 185 | statement.bind(8, convertJson2String(request.challengeState->secrets), |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 186 | SQLITE_TRANSIENT); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 187 | statement.bind(9, time::toIsoString(request.challengeState->timestamp), SQLITE_TRANSIENT); |
| 188 | statement.bind(10, request.challengeState->remainingTries); |
| 189 | statement.bind(11, request.challengeState->remainingTime.count()); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 190 | } |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 191 | if (statement.step() != SQLITE_DONE) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 192 | NDN_THROW(std::runtime_error("Request " + toHex(request.requestId.data(), request.requestId.size()) + " cannot be added to database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | void |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 197 | CaSqlite::updateRequest(const RequestState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 198 | { |
| 199 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 200 | R"_SQLTEXT_(UPDATE RequestStates |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 201 | SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 202 | challenge_tp = ?, remaining_tries = ?, remaining_time = ?, aes_block_counter = ? |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 203 | WHERE request_id = ?)_SQLTEXT_"); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 204 | statement.bind(1, static_cast<int>(request.status)); |
| 205 | statement.bind(2, request.challengeType, SQLITE_TRANSIENT); |
| 206 | if (request.challengeState) { |
| 207 | statement.bind(3, request.challengeState->challengeStatus, SQLITE_TRANSIENT); |
| 208 | statement.bind(4, convertJson2String(request.challengeState->secrets), SQLITE_TRANSIENT); |
| 209 | statement.bind(5, time::toIsoString(request.challengeState->timestamp), SQLITE_TRANSIENT); |
| 210 | statement.bind(6, request.challengeState->remainingTries); |
| 211 | statement.bind(7, request.challengeState->remainingTime.count()); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 212 | } |
| 213 | else { |
| 214 | statement.bind(3, "", SQLITE_TRANSIENT); |
| 215 | statement.bind(4, "", SQLITE_TRANSIENT); |
| 216 | statement.bind(5, "", SQLITE_TRANSIENT); |
| 217 | statement.bind(6, 0); |
| 218 | statement.bind(7, 0); |
| 219 | } |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 220 | statement.bind(8, request.aesBlockCounter); |
| 221 | statement.bind(9, request.requestId.data(), request.requestId.size(), SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 222 | |
| 223 | if (statement.step() != SQLITE_DONE) { |
| 224 | addRequest(request); |
| 225 | } |
| 226 | } |
| 227 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 228 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 229 | CaSqlite::listAllRequests() |
| 230 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 231 | std::list<RequestState> result; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 232 | Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 233 | challenge_status, cert_request, challenge_type, challenge_secrets, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 234 | challenge_tp, remaining_tries, remaining_time, request_type, |
| 235 | encryption_key, aes_block_counter |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 236 | FROM RequestStates)_SQLTEXT_"); |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 237 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 238 | RequestId requestId; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 239 | std::memcpy(requestId.data(), statement.getBlob(1), statement.getSize(1)); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 240 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 241 | auto status = static_cast<Status>(statement.getInt(3)); |
| 242 | auto challengeStatus = statement.getString(4); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 243 | security::Certificate cert(statement.getBlock(5)); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 244 | auto challengeType = statement.getString(6); |
| 245 | auto challengeSecrets = statement.getString(7); |
| 246 | auto challengeTp = statement.getString(8); |
| 247 | auto remainingTries = statement.getInt(9); |
| 248 | auto remainingTime = statement.getInt(10); |
| 249 | auto requestType = static_cast<RequestType>(statement.getInt(11)); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 250 | std::array<uint8_t, 16> encryptionKey; |
| 251 | std::memcpy(encryptionKey.data(), statement.getBlob(12), statement.getSize(12)); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 252 | auto aesBlockCounter = statement.getInt(13); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 253 | if (challengeType != "") { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 254 | result.push_back(RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 255 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 256 | remainingTries, time::seconds(remainingTime), |
| 257 | convertString2Json(challengeSecrets), |
| 258 | std::move(encryptionKey), aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 259 | } |
| 260 | else { |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 261 | result.push_back(RequestState(caName, requestId, requestType, |
| 262 | status, cert, std::move(encryptionKey), aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 263 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 264 | } |
| 265 | return result; |
| 266 | } |
| 267 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 268 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 269 | CaSqlite::listAllRequests(const Name& caName) |
| 270 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 271 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 272 | Sqlite3Statement statement(m_database, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 273 | R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 274 | challenge_status, cert_request, challenge_type, challenge_secrets, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 275 | challenge_tp, remaining_tries, remaining_time, request_type, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 276 | encryption_key, aes_block_counter |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -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 | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 281 | RequestId requestId; |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 282 | std::memcpy(requestId.data(), statement.getBlob(1), statement.getSize(1)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 283 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 284 | auto status = static_cast<Status>(statement.getInt(3)); |
| 285 | auto challengeStatus = statement.getString(4); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 286 | security::Certificate cert(statement.getBlock(5)); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 287 | auto challengeType = statement.getString(6); |
| 288 | auto challengeSecrets = statement.getString(7); |
| 289 | auto challengeTp = statement.getString(8); |
| 290 | auto remainingTries = statement.getInt(9); |
| 291 | auto remainingTime = statement.getInt(10); |
| 292 | auto requestType = static_cast<RequestType>(statement.getInt(11)); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 293 | std::array<uint8_t, 16> encryptionKey; |
| 294 | std::memcpy(encryptionKey.data(), statement.getBlob(12), statement.getSize(12)); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 295 | auto aesBlockCounter = statement.getInt(13); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 296 | if (challengeType != "") { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 297 | result.push_back(RequestState(caName, requestId, requestType, status, cert, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 298 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 299 | remainingTries, time::seconds(remainingTime), |
| 300 | convertString2Json(challengeSecrets), |
| 301 | std::move(encryptionKey), aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 302 | } |
| 303 | else { |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 304 | result.push_back(RequestState(caName, requestId, requestType, status, |
| 305 | cert, std::move(encryptionKey), aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 306 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 311 | void |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 312 | CaSqlite::deleteRequest(const RequestId& requestId) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 313 | { |
| 314 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 315 | R"_SQLTEXT_(DELETE FROM RequestStates WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 316 | statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 317 | statement.step(); |
| 318 | } |
| 319 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 320 | } // namespace ca |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 321 | } // namespace ndncert |
| 322 | } // namespace ndn |