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