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