blob: 8371cf40743aad351b521b50ec7c11dbc93f7e0d [file] [log] [blame]
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob48bbda2020-07-27 19:41:37 -04002/*
3 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07004 *
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 Pesaventob48bbda2020-07-27 19:41:37 -040022
Zhiyi Zhanga749f442020-09-29 17:19:51 -070023#include <sqlite3.h>
24
25#include <boost/filesystem.hpp>
tylerliua7bea662020-10-08 18:51:02 -070026#include <ndn-cxx/security/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070027#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070028
29namespace ndn {
30namespace ndncert {
31
Zhiyi Zhang59812232020-10-12 13:11:35 -070032using namespace ndn::util;
Davide Pesaventob48bbda2020-07-27 19:41:37 -040033const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070034
35NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
36
Zhiyi Zhang59812232020-10-12 13:11:35 -070037std::string
38convertJson2String(const JsonSection& json)
39{
40 std::stringstream ss;
41 boost::property_tree::write_json(ss, json);
42 return ss.str();
43}
44
45JsonSection
46convertString2Json(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 Zhang91c846b2017-04-12 14:16:31 -070053
54static const std::string INITIALIZATION = R"_DBTEXT_(
55CREATE TABLE IF NOT EXISTS
tylerliu8704d032020-06-23 10:18:15 -070056 CaStates(
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070057 id INTEGER PRIMARY KEY,
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070058 request_id BLOB NOT NULL,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070059 ca_name BLOB NOT NULL,
tylerliu182bc532020-09-25 01:54:45 -070060 request_type INTEGER NOT NULL,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 status INTEGER NOT NULL,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070062 cert_request BLOB NOT NULL,
63 challenge_type TEXT,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070064 challenge_status TEXT,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 challenge_tp TEXT,
66 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070067 remaining_time INTEGER,
tylerliu8e170d62020-09-30 01:31:53 -070068 challenge_secrets TEXT,
Zhiyi Zhang222810b2020-10-16 21:50:35 -070069 encryption_key BLOB NOT NULL,
70 aes_block_counter INTEGER
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070071 );
72CREATE UNIQUE INDEX IF NOT EXISTS
tylerliu8704d032020-06-23 10:18:15 -070073 CaStateIdIndex ON CaStates(request_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070074)_DBTEXT_";
75
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070076CaSqlite::CaSqlite(const Name& caName, const std::string& path)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070077 : CaStorage()
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070078{
79 // Determine the path of sqlite db
80 boost::filesystem::path dbDir;
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070081 if (!path.empty()) {
82 dbDir = boost::filesystem::path(path);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070083 }
84 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070085 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 Zhang91c846b2017-04-12 14:16:31 -070096 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070097
98 // open and initialize database
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070099 int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700100 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
101#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
102 "unix-dotfile"
103#else
104 nullptr
105#endif
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700106 );
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700107 if (result != SQLITE_OK)
tylerliu41c11532020-10-10 16:14:45 -0700108 NDN_THROW(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700109
110 // initialize database specific tables
111 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400112 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700113 nullptr, nullptr, &errorMessage);
114 if (result != SQLITE_OK && errorMessage != nullptr) {
115 sqlite3_free(errorMessage);
tylerliu41c11532020-10-10 16:14:45 -0700116 NDN_THROW(std::runtime_error("CaSqlite DB cannot be initialized"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700117 }
118}
119
120CaSqlite::~CaSqlite()
121{
122 sqlite3_close(m_database);
123}
124
tylerliu8704d032020-06-23 10:18:15 -0700125CaState
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700126CaSqlite::getRequest(const RequestID& requestId)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700127{
128 Sqlite3Statement statement(m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700129 R"_SQLTEXT_(SELECT id, ca_name, status,
130 challenge_status, cert_request,
131 challenge_type, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700132 challenge_tp, remaining_tries, remaining_time,
133 request_type, encryption_key, aes_block_counter
tylerliu8704d032020-06-23 10:18:15 -0700134 FROM CaStates where request_id = ?)_SQLTEXT_");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700135 statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700136
137 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700138 Name caName(statement.getBlock(1));
139 auto status = static_cast<Status>(statement.getInt(2));
140 auto challengeStatus = statement.getString(3);
tylerliua7bea662020-10-08 18:51:02 -0700141 security::Certificate cert(statement.getBlock(4));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700142 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));
tylerliu8e170d62020-09-30 01:31:53 -0700148 auto encryptionKey = statement.getBlock(11);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700149 auto aesCounter = statement.getInt(12);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700150 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700151 return CaState(caName, requestId, requestType, status, cert,
152 challengeType, challengeStatus, time::fromIsoString(challengeTp),
153 remainingTries, time::seconds(remainingTime),
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700154 convertString2Json(challengeSecrets), encryptionKey, aesCounter);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700155 }
156 else {
tylerliu8704d032020-06-23 10:18:15 -0700157 return CaState(caName, requestId, requestType, status, cert, encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700158 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700159 }
160 else {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700161 NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) + " cannot be fetched from database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700162 }
163}
164
165void
tylerliu8704d032020-06-23 10:18:15 -0700166CaSqlite::addRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700167{
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700168 Sqlite3Statement statement(
169 m_database,
tylerliu8704d032020-06-23 10:18:15 -0700170 R"_SQLTEXT_(INSERT OR ABORT INTO CaStates (request_id, ca_name, status, request_type,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700171 cert_request, challenge_type, challenge_status, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700172 challenge_tp, remaining_tries, remaining_time, encryption_key, aes_block_counter)
173 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700174 statement.bind(1, request.m_requestId.data(), request.m_requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700175 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
176 statement.bind(3, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700177 statement.bind(4, static_cast<int>(request.m_requestType));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700178 statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
179 statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700180 statement.bind(13, request.m_aesBlockCounter);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700181 if (request.m_challengeState) {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700182 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 Zhanga749f442020-09-29 17:19:51 -0700185 SQLITE_TRANSIENT);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700186 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 Zhanga749f442020-09-29 17:19:51 -0700189 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700190 if (statement.step() != SQLITE_DONE) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700191 NDN_THROW(std::runtime_error("Request " + toHex(request.m_requestId.data(), request.m_requestId.size()) + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700192 }
193}
194
195void
tylerliu8704d032020-06-23 10:18:15 -0700196CaSqlite::updateRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700197{
198 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700199 R"_SQLTEXT_(UPDATE CaStates
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700200 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700201 challenge_tp = ?, remaining_tries = ?, remaining_time = ?, aes_block_counter = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700202 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700203 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700204 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 Zhang14f0bc82020-10-12 13:02:23 -0700207 statement.bind(4, convertJson2String(request.m_challengeState->m_secrets), SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700208 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 Zhang222810b2020-10-16 21:50:35 -0700219 statement.bind(8, request.m_aesBlockCounter);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700220 statement.bind(9, request.m_requestId.data(), request.m_requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700221
222 if (statement.step() != SQLITE_DONE) {
223 addRequest(request);
224 }
225}
226
tylerliu8704d032020-06-23 10:18:15 -0700227std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700228CaSqlite::listAllRequests()
229{
tylerliu8704d032020-06-23 10:18:15 -0700230 std::list<CaState> result;
tylerliu182bc532020-09-25 01:54:45 -0700231 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700232 challenge_status, cert_request, challenge_type, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700233 challenge_tp, remaining_tries, remaining_time, request_type,
234 encryption_key, aes_block_counter
tylerliu8704d032020-06-23 10:18:15 -0700235 FROM CaStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400236 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700237 RequestID requestId;
238 std::memcpy(requestId.data(), statement.getBlob(1), statement.getSize(1));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700239 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700240 auto status = static_cast<Status>(statement.getInt(3));
241 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700242 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700243 auto challengeType = statement.getString(6);
244 auto challengeSecrets = statement.getString(7);
245 auto challengeTp = statement.getString(8);
246 auto remainingTries = statement.getInt(9);
247 auto remainingTime = statement.getInt(10);
248 auto requestType = static_cast<RequestType>(statement.getInt(11));
249 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700250 auto aesBlockCounter = statement.getInt(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700251 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700252 result.push_back(CaState(caName, requestId, requestType, status, cert,
253 challengeType, challengeStatus, time::fromIsoString(challengeTp),
254 remainingTries, time::seconds(remainingTime),
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700255 convertString2Json(challengeSecrets), encryptionKey, aesBlockCounter));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700256 }
257 else {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700258 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey, aesBlockCounter));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700259 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700260 }
261 return result;
262}
263
tylerliu8704d032020-06-23 10:18:15 -0700264std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700265CaSqlite::listAllRequests(const Name& caName)
266{
tylerliu8704d032020-06-23 10:18:15 -0700267 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700268 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700269 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700270 challenge_status, cert_request, challenge_type, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700271 challenge_tp, remaining_tries, remaining_time, request_type,
272 encryption_key, aes_block_counter
tylerliu8704d032020-06-23 10:18:15 -0700273 FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700274 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
275
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400276 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700277 RequestID requestId;
278 std::memcpy(requestId.data(), statement.getBlob(1), statement.getSize(1));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700279 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700280 auto status = static_cast<Status>(statement.getInt(3));
281 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700282 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700283 auto challengeType = statement.getString(6);
284 auto challengeSecrets = statement.getString(7);
285 auto challengeTp = statement.getString(8);
286 auto remainingTries = statement.getInt(9);
287 auto remainingTime = statement.getInt(10);
288 auto requestType = static_cast<RequestType>(statement.getInt(11));
289 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700290 auto aesBlockCounter = statement.getInt(13);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700291 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700292 result.push_back(CaState(caName, requestId, requestType, status, cert,
293 challengeType, challengeStatus, time::fromIsoString(challengeTp),
294 remainingTries, time::seconds(remainingTime),
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700295 convertString2Json(challengeSecrets), encryptionKey, aesBlockCounter));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700296 }
297 else {
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700298 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey, aesBlockCounter));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700299 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700300 }
301 return result;
302}
303
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700304void
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700305CaSqlite::deleteRequest(const RequestID& requestId)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700306{
307 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700308 R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700309 statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700310 statement.step();
311}
312
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700313} // namespace ndncert
314} // namespace ndn