blob: 8292f3622de946967eaeb85bc4a232a3963061da [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>
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070026#include <ndn-cxx/security/v2/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
Davide Pesaventob48bbda2020-07-27 19:41:37 -040032const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070033
34NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
35
36using namespace ndn::util;
37
38static const std::string INITIALIZATION = R"_DBTEXT_(
39CREATE TABLE IF NOT EXISTS
tylerliu8704d032020-06-23 10:18:15 -070040 CaStates(
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070041 id INTEGER PRIMARY KEY,
42 request_id TEXT NOT NULL,
43 ca_name BLOB NOT NULL,
tylerliu182bc532020-09-25 01:54:45 -070044 request_type INTEGER NOT NULL,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045 status INTEGER NOT NULL,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070046 cert_request BLOB NOT NULL,
47 challenge_type TEXT,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070048 challenge_status TEXT,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049 challenge_tp TEXT,
50 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070051 remaining_time INTEGER,
tylerliu8e170d62020-09-30 01:31:53 -070052 challenge_secrets TEXT,
53 encryption_key BLOB NOT NULL
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070054 );
55CREATE UNIQUE INDEX IF NOT EXISTS
tylerliu8704d032020-06-23 10:18:15 -070056 CaStateIdIndex ON CaStates(request_id);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070057)_DBTEXT_";
58
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070059CaSqlite::CaSqlite(const Name& caName, const std::string& path)
Zhiyi Zhanga749f442020-09-29 17:19:51 -070060 : CaStorage()
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070061{
62 // Determine the path of sqlite db
63 boost::filesystem::path dbDir;
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070064 if (!path.empty()) {
65 dbDir = boost::filesystem::path(path);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070066 }
67 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070068 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 Zhang91c846b2017-04-12 14:16:31 -070079 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070080
81 // open and initialize database
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070082 int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070083 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
84#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
85 "unix-dotfile"
86#else
87 nullptr
88#endif
Zhiyi Zhanga749f442020-09-29 17:19:51 -070089 );
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070090 if (result != SQLITE_OK)
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070091 BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070092
93 // initialize database specific tables
94 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -040095 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070096 nullptr, nullptr, &errorMessage);
97 if (result != SQLITE_OK && errorMessage != nullptr) {
98 sqlite3_free(errorMessage);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070099 BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be initialized"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700100 }
101}
102
103CaSqlite::~CaSqlite()
104{
105 sqlite3_close(m_database);
106}
107
tylerliu8704d032020-06-23 10:18:15 -0700108CaState
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700109CaSqlite::getRequest(const std::string& requestId)
110{
111 Sqlite3Statement statement(m_database,
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700112 R"_SQLTEXT_(SELECT id, ca_name, status,
113 challenge_status, cert_request,
114 challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700115 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700116 FROM CaStates where request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700117 statement.bind(1, requestId, SQLITE_TRANSIENT);
118
119 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700120 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));
tylerliu8e170d62020-09-30 01:31:53 -0700130 auto encryptionKey = statement.getBlock(11);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700131 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700132 return CaState(caName, requestId, requestType, status, cert,
133 challengeType, challengeStatus, time::fromIsoString(challengeTp),
134 remainingTries, time::seconds(remainingTime),
135 convertString2Json(challengeSecrets), encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700136 }
137 else {
tylerliu8704d032020-06-23 10:18:15 -0700138 return CaState(caName, requestId, requestType, status, cert, encryptionKey);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700139 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700140 }
141 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700142 BOOST_THROW_EXCEPTION(std::runtime_error("Request " + requestId + " cannot be fetched from database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700143 }
144}
145
146void
tylerliu8704d032020-06-23 10:18:15 -0700147CaSqlite::addRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700148{
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700149 Sqlite3Statement statement(
150 m_database,
tylerliu8704d032020-06-23 10:18:15 -0700151 R"_SQLTEXT_(INSERT OR ABORT INTO CaStates (request_id, ca_name, status, request_type,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700152 cert_request, challenge_type, challenge_status, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700153 challenge_tp, remaining_tries, remaining_time, encryption_key)
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700154 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700155 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 Zhanga749f442020-09-29 17:19:51 -0700158 statement.bind(4, static_cast<int>(request.m_requestType));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700159 statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
160 statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700161 if (request.m_challengeState) {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700162 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 Zhanga749f442020-09-29 17:19:51 -0700165 SQLITE_TRANSIENT);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700166 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 Zhanga749f442020-09-29 17:19:51 -0700169 }
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700170 if (statement.step() != SQLITE_DONE) {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700171 BOOST_THROW_EXCEPTION(std::runtime_error("Request " + request.m_requestId + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700172 }
173}
174
175void
tylerliu8704d032020-06-23 10:18:15 -0700176CaSqlite::updateRequest(const CaState& request)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700177{
178 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700179 R"_SQLTEXT_(UPDATE CaStates
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700180 SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
181 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700182 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700183 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700184 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 Zhang91c846b2017-04-12 14:16:31 -0700201
202 if (statement.step() != SQLITE_DONE) {
203 addRequest(request);
204 }
205}
206
tylerliu8704d032020-06-23 10:18:15 -0700207std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700208CaSqlite::listAllRequests()
209{
tylerliu8704d032020-06-23 10:18:15 -0700210 std::list<CaState> result;
tylerliu182bc532020-09-25 01:54:45 -0700211 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700212 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700213 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700214 FROM CaStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400215 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700216 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700217 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700218 auto status = static_cast<Status>(statement.getInt(3));
219 auto challengeStatus = statement.getString(4);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700220 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 Zhanga749f442020-09-29 17:19:51 -0700228 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700229 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 Zhanga749f442020-09-29 17:19:51 -0700233 }
234 else {
tylerliu8704d032020-06-23 10:18:15 -0700235 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700236 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700237 }
238 return result;
239}
240
tylerliu8704d032020-06-23 10:18:15 -0700241std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700242CaSqlite::listAllRequests(const Name& caName)
243{
tylerliu8704d032020-06-23 10:18:15 -0700244 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700245 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700246 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700247 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700248 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700249 FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700250 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
251
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400252 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700253 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700254 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700255 auto status = static_cast<Status>(statement.getInt(3));
256 auto challengeStatus = statement.getString(4);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700257 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 Zhanga749f442020-09-29 17:19:51 -0700265 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700266 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 Zhanga749f442020-09-29 17:19:51 -0700270 }
271 else {
tylerliu8704d032020-06-23 10:18:15 -0700272 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700273 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700274 }
275 return result;
276}
277
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700278void
279CaSqlite::deleteRequest(const std::string& requestId)
280{
281 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700282 R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700283 statement.bind(1, requestId, SQLITE_TRANSIENT);
284 statement.step();
285}
286
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700287} // namespace ndncert
288} // namespace ndn