Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "ca-sqlite.hpp" |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 22 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 23 | #include <sqlite3.h> |
| 24 | |
| 25 | #include <boost/filesystem.hpp> |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/security/v2/validation-policy.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndncert { |
| 31 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 32 | const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3"; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 33 | |
| 34 | NDNCERT_REGISTER_CA_STORAGE(CaSqlite); |
| 35 | |
| 36 | using namespace ndn::util; |
| 37 | |
| 38 | static const std::string INITIALIZATION = R"_DBTEXT_( |
| 39 | CREATE TABLE IF NOT EXISTS |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 40 | CaStates( |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 41 | id INTEGER PRIMARY KEY, |
| 42 | request_id TEXT NOT NULL, |
| 43 | ca_name BLOB NOT NULL, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 44 | request_type INTEGER NOT NULL, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | status INTEGER NOT NULL, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 46 | cert_request BLOB NOT NULL, |
| 47 | challenge_type TEXT, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 48 | challenge_status TEXT, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | challenge_tp TEXT, |
| 50 | remaining_tries INTEGER, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 51 | remaining_time INTEGER, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 52 | challenge_secrets TEXT, |
| 53 | encryption_key BLOB NOT NULL |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 54 | ); |
| 55 | CREATE UNIQUE INDEX IF NOT EXISTS |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 56 | CaStateIdIndex ON CaStates(request_id); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 57 | )_DBTEXT_"; |
| 58 | |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 59 | CaSqlite::CaSqlite(const Name& caName, const std::string& path) |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 60 | : CaStorage() |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 61 | { |
| 62 | // Determine the path of sqlite db |
| 63 | boost::filesystem::path dbDir; |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 64 | if (!path.empty()) { |
| 65 | dbDir = boost::filesystem::path(path); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 66 | } |
| 67 | else { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 68 | std::string dbName = caName.toUri(); |
| 69 | std::replace(dbName.begin(), dbName.end(), '/', '_'); |
| 70 | dbName += ".db"; |
| 71 | if (getenv("HOME") != nullptr) { |
| 72 | dbDir = boost::filesystem::path(getenv("HOME")) / ".ndncert"; |
| 73 | } |
| 74 | else { |
| 75 | dbDir = boost::filesystem::current_path() / ".ndncert"; |
| 76 | } |
| 77 | boost::filesystem::create_directories(dbDir); |
| 78 | dbDir /= dbName; |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 79 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 80 | |
| 81 | // open and initialize database |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 82 | int result = sqlite3_open_v2(dbDir.c_str(), &m_database, |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 83 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 84 | #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING |
| 85 | "unix-dotfile" |
| 86 | #else |
| 87 | nullptr |
| 88 | #endif |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 89 | ); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 90 | if (result != SQLITE_OK) |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 91 | BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string())); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 92 | |
| 93 | // initialize database specific tables |
| 94 | char* errorMessage = nullptr; |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 95 | result = sqlite3_exec(m_database, INITIALIZATION.data(), |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 96 | nullptr, nullptr, &errorMessage); |
| 97 | if (result != SQLITE_OK && errorMessage != nullptr) { |
| 98 | sqlite3_free(errorMessage); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 99 | BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be initialized")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | CaSqlite::~CaSqlite() |
| 104 | { |
| 105 | sqlite3_close(m_database); |
| 106 | } |
| 107 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 108 | CaState |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 109 | CaSqlite::getRequest(const std::string& requestId) |
| 110 | { |
| 111 | Sqlite3Statement statement(m_database, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 112 | R"_SQLTEXT_(SELECT id, ca_name, status, |
| 113 | challenge_status, cert_request, |
| 114 | challenge_type, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 115 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 116 | FROM CaStates where request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 117 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 118 | |
| 119 | if (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 120 | Name caName(statement.getBlock(1)); |
| 121 | auto status = static_cast<Status>(statement.getInt(2)); |
| 122 | auto challengeStatus = statement.getString(3); |
| 123 | security::v2::Certificate cert(statement.getBlock(4)); |
| 124 | auto challengeType = statement.getString(5); |
| 125 | auto challengeSecrets = statement.getString(6); |
| 126 | auto challengeTp = statement.getString(7); |
| 127 | auto remainingTries = statement.getInt(8); |
| 128 | auto remainingTime = statement.getInt(9); |
| 129 | auto requestType = static_cast<RequestType>(statement.getInt(10)); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 130 | auto encryptionKey = statement.getBlock(11); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 131 | if (challengeType != "") { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 132 | return CaState(caName, requestId, requestType, status, cert, |
| 133 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 134 | remainingTries, time::seconds(remainingTime), |
| 135 | convertString2Json(challengeSecrets), encryptionKey); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 136 | } |
| 137 | else { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 138 | return CaState(caName, requestId, requestType, status, cert, encryptionKey); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 139 | } |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 140 | } |
| 141 | else { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 142 | BOOST_THROW_EXCEPTION(std::runtime_error("Request " + requestId + " cannot be fetched from database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 147 | CaSqlite::addRequest(const CaState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 148 | { |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 149 | Sqlite3Statement statement( |
| 150 | m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 151 | 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^] | 152 | cert_request, challenge_type, challenge_status, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 153 | challenge_tp, remaining_tries, remaining_time, encryption_key) |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 154 | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_"); |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 155 | statement.bind(1, request.m_requestId, SQLITE_TRANSIENT); |
| 156 | statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT); |
| 157 | statement.bind(3, static_cast<int>(request.m_status)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 158 | statement.bind(4, static_cast<int>(request.m_requestType)); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 159 | statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT); |
| 160 | statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 161 | if (request.m_challengeState) { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 162 | statement.bind(6, request.m_challengeType, SQLITE_TRANSIENT); |
| 163 | statement.bind(7, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT); |
| 164 | statement.bind(8, convertJson2String(request.m_challengeState->m_secrets), |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 165 | SQLITE_TRANSIENT); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 166 | statement.bind(9, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT); |
| 167 | statement.bind(10, request.m_challengeState->m_remainingTries); |
| 168 | statement.bind(11, request.m_challengeState->m_remainingTime.count()); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 169 | } |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 170 | if (statement.step() != SQLITE_DONE) { |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 171 | BOOST_THROW_EXCEPTION(std::runtime_error("Request " + request.m_requestId + " cannot be added to database")); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 176 | CaSqlite::updateRequest(const CaState& request) |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 177 | { |
| 178 | Sqlite3Statement statement(m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 179 | R"_SQLTEXT_(UPDATE CaStates |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 180 | SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?, |
| 181 | challenge_tp = ?, remaining_tries = ?, remaining_time = ? |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 182 | WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 183 | statement.bind(1, static_cast<int>(request.m_status)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 184 | statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT); |
| 185 | if (request.m_challengeState) { |
| 186 | statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT); |
| 187 | statement.bind(4, convertJson2String(request.m_challengeState->m_secrets), |
| 188 | SQLITE_TRANSIENT); |
| 189 | statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT); |
| 190 | statement.bind(6, request.m_challengeState->m_remainingTries); |
| 191 | statement.bind(7, request.m_challengeState->m_remainingTime.count()); |
| 192 | } |
| 193 | else { |
| 194 | statement.bind(3, "", SQLITE_TRANSIENT); |
| 195 | statement.bind(4, "", SQLITE_TRANSIENT); |
| 196 | statement.bind(5, "", SQLITE_TRANSIENT); |
| 197 | statement.bind(6, 0); |
| 198 | statement.bind(7, 0); |
| 199 | } |
| 200 | statement.bind(8, request.m_requestId, SQLITE_TRANSIENT); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 201 | |
| 202 | if (statement.step() != SQLITE_DONE) { |
| 203 | addRequest(request); |
| 204 | } |
| 205 | } |
| 206 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 207 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 208 | CaSqlite::listAllRequests() |
| 209 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 210 | std::list<CaState> result; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 211 | 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^] | 212 | challenge_status, cert_request, challenge_type, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 213 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 214 | FROM CaStates)_SQLTEXT_"); |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 215 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 216 | auto requestId = statement.getString(1); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 217 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 218 | auto status = static_cast<Status>(statement.getInt(3)); |
| 219 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 220 | security::v2::Certificate cert(statement.getBlock(5)); |
| 221 | auto challengeType = statement.getString(6); |
| 222 | auto challengeSecrets = statement.getString(7); |
| 223 | auto challengeTp = statement.getString(8); |
| 224 | auto remainingTries = statement.getInt(9); |
| 225 | auto remainingTime = statement.getInt(10); |
| 226 | auto requestType = static_cast<RequestType>(statement.getInt(11)); |
| 227 | auto encryptionKey = statement.getBlock(12); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 228 | if (challengeType != "") { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 229 | result.push_back(CaState(caName, requestId, requestType, status, cert, |
| 230 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 231 | remainingTries, time::seconds(remainingTime), |
| 232 | convertString2Json(challengeSecrets), encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 233 | } |
| 234 | else { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 235 | result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 236 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 237 | } |
| 238 | return result; |
| 239 | } |
| 240 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 241 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 242 | CaSqlite::listAllRequests(const Name& caName) |
| 243 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 244 | std::list<CaState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 245 | Sqlite3Statement statement(m_database, |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 246 | R"_SQLTEXT_(SELECT id, request_id, ca_name, status, |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 247 | challenge_status, cert_request, challenge_type, challenge_secrets, |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame] | 248 | challenge_tp, remaining_tries, remaining_time, request_type, encryption_key |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 249 | FROM CaStates WHERE ca_name = ?)_SQLTEXT_"); |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 250 | statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT); |
| 251 | |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 252 | while (statement.step() == SQLITE_ROW) { |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 253 | auto requestId = statement.getString(1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 254 | Name caName(statement.getBlock(2)); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 255 | auto status = static_cast<Status>(statement.getInt(3)); |
| 256 | auto challengeStatus = statement.getString(4); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame^] | 257 | security::v2::Certificate cert(statement.getBlock(5)); |
| 258 | auto challengeType = statement.getString(6); |
| 259 | auto challengeSecrets = statement.getString(7); |
| 260 | auto challengeTp = statement.getString(8); |
| 261 | auto remainingTries = statement.getInt(9); |
| 262 | auto remainingTime = statement.getInt(10); |
| 263 | auto requestType = static_cast<RequestType>(statement.getInt(11)); |
| 264 | auto encryptionKey = statement.getBlock(12); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 265 | if (challengeType != "") { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 266 | result.push_back(CaState(caName, requestId, requestType, status, cert, |
| 267 | challengeType, challengeStatus, time::fromIsoString(challengeTp), |
| 268 | remainingTries, time::seconds(remainingTime), |
| 269 | convertString2Json(challengeSecrets), encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 270 | } |
| 271 | else { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 272 | result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey)); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 273 | } |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 274 | } |
| 275 | return result; |
| 276 | } |
| 277 | |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 278 | void |
| 279 | CaSqlite::deleteRequest(const std::string& requestId) |
| 280 | { |
| 281 | Sqlite3Statement statement(m_database, |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 282 | R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_"); |
Zhiyi Zhang | 91c846b | 2017-04-12 14:16:31 -0700 | [diff] [blame] | 283 | statement.bind(1, requestId, SQLITE_TRANSIENT); |
| 284 | statement.step(); |
| 285 | } |
| 286 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 287 | } // namespace ndncert |
| 288 | } // namespace ndn |