blob: b0d6af597d9de4b1418254c7b7686297349318d8 [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,
58 request_id TEXT NOT NULL,
59 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,
69 encryption_key BLOB NOT NULL
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070070 );
71CREATE UNIQUE INDEX IF NOT EXISTS
tylerliu8704d032020-06-23 10:18:15 -070072 CaStateIdIndex ON CaStates(request_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070073)_DBTEXT_";
74
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070075CaSqlite::CaSqlite(const Name& caName, const std::string& path)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070076 : CaStorage()
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070077{
78 // Determine the path of sqlite db
79 boost::filesystem::path dbDir;
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070080 if (!path.empty()) {
81 dbDir = boost::filesystem::path(path);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070082 }
83 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070084 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 Zhang91c846b2017-04-12 14:16:31 -070095 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070096
97 // open and initialize database
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070098 int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070099 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
100#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
101 "unix-dotfile"
102#else
103 nullptr
104#endif
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700105 );
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700106 if (result != SQLITE_OK)
tylerliu41c11532020-10-10 16:14:45 -0700107 NDN_THROW(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700108
109 // initialize database specific tables
110 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400111 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700112 nullptr, nullptr, &errorMessage);
113 if (result != SQLITE_OK && errorMessage != nullptr) {
114 sqlite3_free(errorMessage);
tylerliu41c11532020-10-10 16:14:45 -0700115 NDN_THROW(std::runtime_error("CaSqlite DB cannot be initialized"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700116 }
117}
118
119CaSqlite::~CaSqlite()
120{
121 sqlite3_close(m_database);
122}
123
tylerliu8704d032020-06-23 10:18:15 -0700124CaState
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700125CaSqlite::getRequest(const std::string& requestId)
126{
127 Sqlite3Statement statement(m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700128 R"_SQLTEXT_(SELECT id, ca_name, status,
129 challenge_status, cert_request,
130 challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700131 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700132 FROM CaStates where request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700133 statement.bind(1, requestId, SQLITE_TRANSIENT);
134
135 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700136 Name caName(statement.getBlock(1));
137 auto status = static_cast<Status>(statement.getInt(2));
138 auto challengeStatus = statement.getString(3);
tylerliua7bea662020-10-08 18:51:02 -0700139 security::Certificate cert(statement.getBlock(4));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700140 auto challengeType = statement.getString(5);
141 auto challengeSecrets = statement.getString(6);
142 auto challengeTp = statement.getString(7);
143 auto remainingTries = statement.getInt(8);
144 auto remainingTime = statement.getInt(9);
145 auto requestType = static_cast<RequestType>(statement.getInt(10));
tylerliu8e170d62020-09-30 01:31:53 -0700146 auto encryptionKey = statement.getBlock(11);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700147 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700148 return CaState(caName, requestId, requestType, status, cert,
149 challengeType, challengeStatus, time::fromIsoString(challengeTp),
150 remainingTries, time::seconds(remainingTime),
151 convertString2Json(challengeSecrets), encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700152 }
153 else {
tylerliu8704d032020-06-23 10:18:15 -0700154 return CaState(caName, requestId, requestType, status, cert, encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700155 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700156 }
157 else {
tylerliu41c11532020-10-10 16:14:45 -0700158 NDN_THROW(std::runtime_error("Request " + requestId + " cannot be fetched from database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700159 }
160}
161
162void
tylerliu8704d032020-06-23 10:18:15 -0700163CaSqlite::addRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700164{
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700165 Sqlite3Statement statement(
166 m_database,
tylerliu8704d032020-06-23 10:18:15 -0700167 R"_SQLTEXT_(INSERT OR ABORT INTO CaStates (request_id, ca_name, status, request_type,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700168 cert_request, challenge_type, challenge_status, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700169 challenge_tp, remaining_tries, remaining_time, encryption_key)
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700170 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700171 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
172 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
173 statement.bind(3, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700174 statement.bind(4, static_cast<int>(request.m_requestType));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700175 statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
176 statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700177 if (request.m_challengeState) {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700178 statement.bind(6, request.m_challengeType, SQLITE_TRANSIENT);
179 statement.bind(7, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
180 statement.bind(8, convertJson2String(request.m_challengeState->m_secrets),
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700181 SQLITE_TRANSIENT);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700182 statement.bind(9, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
183 statement.bind(10, request.m_challengeState->m_remainingTries);
184 statement.bind(11, request.m_challengeState->m_remainingTime.count());
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700185 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700186 if (statement.step() != SQLITE_DONE) {
tylerliu41c11532020-10-10 16:14:45 -0700187 NDN_THROW(std::runtime_error("Request " + request.m_requestId + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700188 }
189}
190
191void
tylerliu8704d032020-06-23 10:18:15 -0700192CaSqlite::updateRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700193{
194 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700195 R"_SQLTEXT_(UPDATE CaStates
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700196 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
197 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700198 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700199 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700200 statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT);
201 if (request.m_challengeState) {
202 statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -0700203 statement.bind(4, convertJson2String(request.m_challengeState->m_secrets), SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700204 statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
205 statement.bind(6, request.m_challengeState->m_remainingTries);
206 statement.bind(7, request.m_challengeState->m_remainingTime.count());
207 }
208 else {
209 statement.bind(3, "", SQLITE_TRANSIENT);
210 statement.bind(4, "", SQLITE_TRANSIENT);
211 statement.bind(5, "", SQLITE_TRANSIENT);
212 statement.bind(6, 0);
213 statement.bind(7, 0);
214 }
215 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700216
217 if (statement.step() != SQLITE_DONE) {
218 addRequest(request);
219 }
220}
221
tylerliu8704d032020-06-23 10:18:15 -0700222std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700223CaSqlite::listAllRequests()
224{
tylerliu8704d032020-06-23 10:18:15 -0700225 std::list<CaState> result;
tylerliu182bc532020-09-25 01:54:45 -0700226 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700227 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700228 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700229 FROM CaStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400230 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700231 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700232 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700233 auto status = static_cast<Status>(statement.getInt(3));
234 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700235 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700236 auto challengeType = statement.getString(6);
237 auto challengeSecrets = statement.getString(7);
238 auto challengeTp = statement.getString(8);
239 auto remainingTries = statement.getInt(9);
240 auto remainingTime = statement.getInt(10);
241 auto requestType = static_cast<RequestType>(statement.getInt(11));
242 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700243 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700244 result.push_back(CaState(caName, requestId, requestType, status, cert,
245 challengeType, challengeStatus, time::fromIsoString(challengeTp),
246 remainingTries, time::seconds(remainingTime),
247 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700248 }
249 else {
tylerliu8704d032020-06-23 10:18:15 -0700250 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700251 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700252 }
253 return result;
254}
255
tylerliu8704d032020-06-23 10:18:15 -0700256std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700257CaSqlite::listAllRequests(const Name& caName)
258{
tylerliu8704d032020-06-23 10:18:15 -0700259 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700260 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700261 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700262 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700263 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700264 FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700265 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
266
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400267 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700268 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700269 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700270 auto status = static_cast<Status>(statement.getInt(3));
271 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700272 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700273 auto challengeType = statement.getString(6);
274 auto challengeSecrets = statement.getString(7);
275 auto challengeTp = statement.getString(8);
276 auto remainingTries = statement.getInt(9);
277 auto remainingTime = statement.getInt(10);
278 auto requestType = static_cast<RequestType>(statement.getInt(11));
279 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700280 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700281 result.push_back(CaState(caName, requestId, requestType, status, cert,
282 challengeType, challengeStatus, time::fromIsoString(challengeTp),
283 remainingTries, time::seconds(remainingTime),
284 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700285 }
286 else {
tylerliu8704d032020-06-23 10:18:15 -0700287 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700288 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700289 }
290 return result;
291}
292
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700293void
294CaSqlite::deleteRequest(const std::string& requestId)
295{
296 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700297 R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700298 statement.bind(1, requestId, SQLITE_TRANSIENT);
299 statement.step();
300}
301
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700302} // namespace ndncert
303} // namespace ndn