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> |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/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 | |
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 |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 56 | CaStates( |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 57 | id INTEGER PRIMARY KEY, |
| 58 | request_id TEXT NOT NULL, |
| 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 |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 73 | CaStateIdIndex ON CaStates(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 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 125 | CaState |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 126 | CaSqlite::getRequest(const std::string& requestId) |
| 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 |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 134 | FROM CaStates where request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 135 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 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)); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 148 | auto encryptionKey = statement.getBlock(11); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 149 | auto aesCounter = statement.getInt(12); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 150 | if (challengeType != "") { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 151 | return CaState(caName, requestId, requestType, status, cert, |
| 152 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 153 | remainingTries, time::seconds(remainingTime), |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 154 | convertString2Json(challengeSecrets), encryptionKey, aesCounter); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 155 | } |
| 156 | else { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 157 | return CaState(caName, requestId, requestType, status, cert, encryptionKey); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 158 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 159 | } |
| 160 | else { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 161 | NDN_THROW(std::runtime_error("Request " + requestId + " cannot be fetched from database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 166 | CaSqlite::addRequest(const CaState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 167 | { |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 168 | Sqlite3Statement statement( |
| 169 | m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 170 | 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] | 171 | cert_request, challenge_type, challenge_status, challenge_secrets, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 172 | challenge_tp, remaining_tries, remaining_time, encryption_key, aes_block_counter) |
| 173 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_"); |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 174 | statement.bind(1, request.m_requestId, SQLITE_TRANSIENT); |
| 175 | statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT); |
| 176 | statement.bind(3, static_cast<int>(request.m_status)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 177 | statement.bind(4, static_cast<int>(request.m_requestType)); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 178 | statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT); |
| 179 | statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 180 | statement.bind(13, request.m_aesBlockCounter); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 181 | if (request.m_challengeState) { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 182 | statement.bind(6, request.m_challengeType, SQLITE_TRANSIENT); |
| 183 | statement.bind(7, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT); |
| 184 | statement.bind(8, convertJson2String(request.m_challengeState->m_secrets), |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 185 | SQLITE_TRANSIENT); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 186 | statement.bind(9, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT); |
| 187 | statement.bind(10, request.m_challengeState->m_remainingTries); |
| 188 | statement.bind(11, request.m_challengeState->m_remainingTime.count()); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 189 | } |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 190 | if (statement.step() != SQLITE_DONE) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 191 | NDN_THROW(std::runtime_error("Request " + request.m_requestId + " cannot be added to database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 196 | CaSqlite::updateRequest(const CaState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 197 | { |
| 198 | Sqlite3Statement statement(m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 199 | R"_SQLTEXT_(UPDATE CaStates |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 200 | SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 201 | challenge_tp = ?, remaining_tries = ?, remaining_time = ?, aes_block_counter = ? |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 202 | WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 203 | statement.bind(1, static_cast<int>(request.m_status)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 204 | statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT); |
| 205 | if (request.m_challengeState) { |
| 206 | statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT); |
Zhiyi Zhang | 14f0bc8 | 2020-10-12 13:02:23 -0700 | [diff] [blame] | 207 | statement.bind(4, convertJson2String(request.m_challengeState->m_secrets), SQLITE_TRANSIENT); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 208 | statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT); |
| 209 | statement.bind(6, request.m_challengeState->m_remainingTries); |
| 210 | statement.bind(7, request.m_challengeState->m_remainingTime.count()); |
| 211 | } |
| 212 | else { |
| 213 | statement.bind(3, "", SQLITE_TRANSIENT); |
| 214 | statement.bind(4, "", SQLITE_TRANSIENT); |
| 215 | statement.bind(5, "", SQLITE_TRANSIENT); |
| 216 | statement.bind(6, 0); |
| 217 | statement.bind(7, 0); |
| 218 | } |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 219 | statement.bind(8, request.m_aesBlockCounter); |
| 220 | statement.bind(9, request.m_requestId, SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 221 | |
| 222 | if (statement.step() != SQLITE_DONE) { |
| 223 | addRequest(request); |
| 224 | } |
| 225 | } |
| 226 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 227 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 228 | CaSqlite::listAllRequests() |
| 229 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 230 | std::list<CaState> result; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 231 | 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] | 232 | challenge_status, cert_request, challenge_type, challenge_secrets, |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 233 | challenge_tp, remaining_tries, remaining_time, request_type, |
| 234 | encryption_key, aes_block_counter |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 235 | FROM CaStates)_SQLTEXT_"); |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 236 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 237 | auto requestId = statement.getString(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 | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 276 | auto requestId = statement.getString(1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 277 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 278 | auto status = static_cast<Status>(statement.getInt(3)); |
| 279 | auto challengeStatus = statement.getString(4); |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 280 | security::Certificate cert(statement.getBlock(5)); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 281 | auto challengeType = statement.getString(6); |
| 282 | auto challengeSecrets = statement.getString(7); |
| 283 | auto challengeTp = statement.getString(8); |
| 284 | auto remainingTries = statement.getInt(9); |
| 285 | auto remainingTime = statement.getInt(10); |
| 286 | auto requestType = static_cast<RequestType>(statement.getInt(11)); |
| 287 | auto encryptionKey = statement.getBlock(12); |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 288 | auto aesBlockCounter = statement.getInt(13); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 289 | if (challengeType != "") { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 290 | result.push_back(CaState(caName, requestId, requestType, status, cert, |
| 291 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 292 | remainingTries, time::seconds(remainingTime), |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 293 | convertString2Json(challengeSecrets), encryptionKey, aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 294 | } |
| 295 | else { |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 296 | result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey, aesBlockCounter)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 297 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 298 | } |
| 299 | return result; |
| 300 | } |
| 301 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 302 | void |
| 303 | CaSqlite::deleteRequest(const std::string& requestId) |
| 304 | { |
| 305 | Sqlite3Statement statement(m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 306 | R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 307 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 308 | statement.step(); |
| 309 | } |
| 310 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 311 | } // namespace ndncert |
| 312 | } // namespace ndn |