blob: 1cddfeda3ec425dd6cab00a4b4bf0fd58b33174e [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
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/ca-sqlite.hpp"
Davide Pesaventob48bbda2020-07-27 19:41:37 -040022
Zhiyi Zhanga749f442020-09-29 17:19:51 -070023#include <sqlite3.h>
Zhiyi Zhanga749f442020-09-29 17:19:51 -070024#include <boost/filesystem.hpp>
tylerliua7bea662020-10-08 18:51:02 -070025#include <ndn-cxx/security/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070026#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070027
28namespace ndn {
29namespace ndncert {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070030namespace ca {
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070031
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
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070056 RequestStates(
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,
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -080070 encryption_iv BLOB,
71 decryption_iv BLOB
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070072 );
73CREATE UNIQUE INDEX IF NOT EXISTS
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070074 RequestStateIdIndex ON RequestStates(request_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070075)_DBTEXT_";
76
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070077CaSqlite::CaSqlite(const Name& caName, const std::string& path)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070078 : CaStorage()
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070079{
80 // Determine the path of sqlite db
81 boost::filesystem::path dbDir;
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070082 if (!path.empty()) {
83 dbDir = boost::filesystem::path(path);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070084 }
85 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070086 std::string dbName = caName.toUri();
87 std::replace(dbName.begin(), dbName.end(), '/', '_');
88 dbName += ".db";
89 if (getenv("HOME") != nullptr) {
90 dbDir = boost::filesystem::path(getenv("HOME")) / ".ndncert";
91 }
92 else {
93 dbDir = boost::filesystem::current_path() / ".ndncert";
94 }
95 boost::filesystem::create_directories(dbDir);
96 dbDir /= dbName;
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070097 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070098
99 // open and initialize database
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700100 int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700101 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
102#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
103 "unix-dotfile"
104#else
105 nullptr
106#endif
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700107 );
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700108 if (result != SQLITE_OK)
tylerliu41c11532020-10-10 16:14:45 -0700109 NDN_THROW(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700110
111 // initialize database specific tables
112 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400113 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700114 nullptr, nullptr, &errorMessage);
115 if (result != SQLITE_OK && errorMessage != nullptr) {
116 sqlite3_free(errorMessage);
tylerliu41c11532020-10-10 16:14:45 -0700117 NDN_THROW(std::runtime_error("CaSqlite DB cannot be initialized"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700118 }
119}
120
121CaSqlite::~CaSqlite()
122{
123 sqlite3_close(m_database);
124}
125
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700126RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700127CaSqlite::getRequest(const RequestId& requestId)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700128{
129 Sqlite3Statement statement(m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700130 R"_SQLTEXT_(SELECT id, ca_name, status,
131 challenge_status, cert_request,
132 challenge_type, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700133 challenge_tp, remaining_tries, remaining_time,
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800134 request_type, encryption_key, encryption_iv, decryption_iv
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700135 FROM RequestStates where request_id = ?)_SQLTEXT_");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700136 statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700137
138 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800139 RequestState state;
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800140 state.requestId = requestId;
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800141 state.caPrefix = Name(statement.getBlock(1));
142 state.status = static_cast<Status>(statement.getInt(2));
143 state.cert = security::Certificate(statement.getBlock(4));
144 state.challengeType = statement.getString(5);
145 state.requestType = static_cast<RequestType>(statement.getInt(10));
146 std::memcpy(state.encryptionKey.data(), statement.getBlob(11), statement.getSize(11));
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800147 state.encryptionIv = std::vector<uint8_t>(statement.getBlob(12), statement.getBlob(12) + statement.getSize(12));
148 state.decryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800149 if (state.challengeType != "") {
150 ChallengeState challengeState(statement.getString(3), time::fromIsoString(statement.getString(7)),
151 statement.getInt(8), time::seconds(statement.getInt(9)),
152 convertString2Json(statement.getString(6)));
153 state.challengeState = challengeState;
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700154 }
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800155 return state;
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700156 }
157 else {
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800158 NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) +
159 " cannot be fetched from database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700160 }
161}
162
163void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700164CaSqlite::addRequest(const RequestState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700165{
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700166 Sqlite3Statement statement(
167 m_database,
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700168 R"_SQLTEXT_(INSERT OR ABORT INTO RequestStates (request_id, ca_name, status, request_type,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700169 cert_request, challenge_type, challenge_status, challenge_secrets,
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800170 challenge_tp, remaining_tries, remaining_time, encryption_key, encryption_iv, decryption_iv)
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800171 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
tylerliu7b9185c2020-11-24 12:15:18 -0800172 statement.bind(1, request.requestId.data(), request.requestId.size(), SQLITE_TRANSIENT);
173 statement.bind(2, request.caPrefix.wireEncode(), SQLITE_TRANSIENT);
174 statement.bind(3, static_cast<int>(request.status));
175 statement.bind(4, static_cast<int>(request.requestType));
176 statement.bind(5, request.cert.wireEncode(), SQLITE_TRANSIENT);
177 statement.bind(12, request.encryptionKey.data(), request.encryptionKey.size(), SQLITE_TRANSIENT);
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800178 statement.bind(13, request.encryptionIv.data(), request.encryptionIv.size(), SQLITE_TRANSIENT);
179 statement.bind(14, request.decryptionIv.data(), request.decryptionIv.size(), SQLITE_TRANSIENT);
tylerliu7b9185c2020-11-24 12:15:18 -0800180 if (request.challengeState) {
181 statement.bind(6, request.challengeType, SQLITE_TRANSIENT);
182 statement.bind(7, request.challengeState->challengeStatus, SQLITE_TRANSIENT);
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800183 statement.bind(8, convertJson2String(request.challengeState->secrets), SQLITE_TRANSIENT);
tylerliu7b9185c2020-11-24 12:15:18 -0800184 statement.bind(9, time::toIsoString(request.challengeState->timestamp), SQLITE_TRANSIENT);
185 statement.bind(10, request.challengeState->remainingTries);
186 statement.bind(11, request.challengeState->remainingTime.count());
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700187 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700188 if (statement.step() != SQLITE_DONE) {
tylerliu7b9185c2020-11-24 12:15:18 -0800189 NDN_THROW(std::runtime_error("Request " + toHex(request.requestId.data(), request.requestId.size()) + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700190 }
191}
192
193void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700194CaSqlite::updateRequest(const RequestState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700195{
196 Sqlite3Statement statement(m_database,
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700197 R"_SQLTEXT_(UPDATE RequestStates
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700198 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800199 challenge_tp = ?, remaining_tries = ?, remaining_time = ?, encryption_iv = ?, decryption_iv = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700200 WHERE request_id = ?)_SQLTEXT_");
tylerliu7b9185c2020-11-24 12:15:18 -0800201 statement.bind(1, static_cast<int>(request.status));
202 statement.bind(2, request.challengeType, SQLITE_TRANSIENT);
203 if (request.challengeState) {
204 statement.bind(3, request.challengeState->challengeStatus, SQLITE_TRANSIENT);
205 statement.bind(4, convertJson2String(request.challengeState->secrets), SQLITE_TRANSIENT);
206 statement.bind(5, time::toIsoString(request.challengeState->timestamp), SQLITE_TRANSIENT);
207 statement.bind(6, request.challengeState->remainingTries);
208 statement.bind(7, request.challengeState->remainingTime.count());
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700209 }
210 else {
211 statement.bind(3, "", SQLITE_TRANSIENT);
212 statement.bind(4, "", SQLITE_TRANSIENT);
213 statement.bind(5, "", SQLITE_TRANSIENT);
214 statement.bind(6, 0);
215 statement.bind(7, 0);
216 }
Zhiyi Zhang4f1c0102020-12-21 15:08:09 -0800217 statement.bind(8, request.encryptionIv.data(), request.encryptionIv.size(), SQLITE_TRANSIENT);
218 statement.bind(9, request.decryptionIv.data(), request.decryptionIv.size(), SQLITE_TRANSIENT);
219 statement.bind(10, request.requestId.data(), request.requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700220
221 if (statement.step() != SQLITE_DONE) {
222 addRequest(request);
223 }
224}
225
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700226std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700227CaSqlite::listAllRequests()
228{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700229 std::list<RequestState> result;
tylerliu182bc532020-09-25 01:54:45 -0700230 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700231 challenge_status, cert_request, challenge_type, challenge_secrets,
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700232 challenge_tp, remaining_tries, remaining_time, request_type,
Zhiyi Zhangf9d94eb2020-12-21 15:39:38 -0800233 encryption_key, encryption_iv, decryption_iv
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700234 FROM RequestStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400235 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800236 RequestState state;
237 std::memcpy(state.requestId.data(), statement.getBlob(1), statement.getSize(1));
238 state.caPrefix = Name(statement.getBlock(2));
239 state.status = static_cast<Status>(statement.getInt(3));
240 state.challengeType = statement.getString(6);
241 state.cert = security::Certificate(statement.getBlock(5));
242 state.requestType = static_cast<RequestType>(statement.getInt(11));
243 std::memcpy(state.encryptionKey.data(), statement.getBlob(12), statement.getSize(12));
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800244 state.encryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
245 state.decryptionIv = std::vector<uint8_t>(statement.getBlob(14), statement.getBlob(14) + statement.getSize(14));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800246 if (state.challengeType != "") {
247 ChallengeState challengeState(statement.getString(4), time::fromIsoString(statement.getString(8)),
248 statement.getInt(9), time::seconds(statement.getInt(10)),
249 convertString2Json(statement.getString(7)));
250 state.challengeState = challengeState;
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700251 }
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800252 result.push_back(state);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700253 }
254 return result;
255}
256
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700257std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700258CaSqlite::listAllRequests(const Name& caName)
259{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700260 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700261 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700262 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700263 challenge_status, cert_request, challenge_type, challenge_secrets,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700264 challenge_tp, remaining_tries, remaining_time, request_type,
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800265 encryption_key, encryption_iv, decryption_iv
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700266 FROM RequestStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700267 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
268
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400269 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800270 RequestState state;
271 std::memcpy(state.requestId.data(), statement.getBlob(1), statement.getSize(1));
272 state.caPrefix = Name(statement.getBlock(2));
273 state.status = static_cast<Status>(statement.getInt(3));
274 state.challengeType = statement.getString(6);
275 state.cert = security::Certificate(statement.getBlock(5));
276 state.requestType = static_cast<RequestType>(statement.getInt(11));
277 std::memcpy(state.encryptionKey.data(), statement.getBlob(12), statement.getSize(12));
Zhiyi Zhang48f546f2020-12-24 17:31:02 -0800278 state.encryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
279 state.decryptionIv = std::vector<uint8_t>(statement.getBlob(14), statement.getBlob(14) + statement.getSize(14));
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800280 if (state.challengeType != "") {
281 ChallengeState challengeState(statement.getString(4), time::fromIsoString(statement.getString(8)),
282 statement.getInt(9), time::seconds(statement.getInt(10)),
283 convertString2Json(statement.getString(7)));
284 state.challengeState = challengeState;
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700285 }
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800286 result.push_back(state);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700287 }
288 return result;
289}
290
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700291void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -0700292CaSqlite::deleteRequest(const RequestId& requestId)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700293{
294 Sqlite3Statement statement(m_database,
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700295 R"_SQLTEXT_(DELETE FROM RequestStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -0700296 statement.bind(1, requestId.data(), requestId.size(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700297 statement.step();
298}
299
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700300} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700301} // namespace ndncert
302} // namespace ndn