blob: a74445b8443df3f2d109ca7efcd89f642365612f [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
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)
tylerliu41c11532020-10-10 16:14:45 -070091 NDN_THROW(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);
tylerliu41c11532020-10-10 16:14:45 -070099 NDN_THROW(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);
tylerliua7bea662020-10-08 18:51:02 -0700123 security::Certificate cert(statement.getBlock(4));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700124 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 {
tylerliu41c11532020-10-10 16:14:45 -0700142 NDN_THROW(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) {
tylerliu41c11532020-10-10 16:14:45 -0700171 NDN_THROW(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);
Zhiyi Zhang14f0bc82020-10-12 13:02:23 -0700187 statement.bind(4, convertJson2String(request.m_challengeState->m_secrets), SQLITE_TRANSIENT);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700188 statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
189 statement.bind(6, request.m_challengeState->m_remainingTries);
190 statement.bind(7, request.m_challengeState->m_remainingTime.count());
191 }
192 else {
193 statement.bind(3, "", SQLITE_TRANSIENT);
194 statement.bind(4, "", SQLITE_TRANSIENT);
195 statement.bind(5, "", SQLITE_TRANSIENT);
196 statement.bind(6, 0);
197 statement.bind(7, 0);
198 }
199 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700200
201 if (statement.step() != SQLITE_DONE) {
202 addRequest(request);
203 }
204}
205
tylerliu8704d032020-06-23 10:18:15 -0700206std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700207CaSqlite::listAllRequests()
208{
tylerliu8704d032020-06-23 10:18:15 -0700209 std::list<CaState> result;
tylerliu182bc532020-09-25 01:54:45 -0700210 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700211 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700212 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700213 FROM CaStates)_SQLTEXT_");
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400214 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700215 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700216 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700217 auto status = static_cast<Status>(statement.getInt(3));
218 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700219 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700220 auto challengeType = statement.getString(6);
221 auto challengeSecrets = statement.getString(7);
222 auto challengeTp = statement.getString(8);
223 auto remainingTries = statement.getInt(9);
224 auto remainingTime = statement.getInt(10);
225 auto requestType = static_cast<RequestType>(statement.getInt(11));
226 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700227 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700228 result.push_back(CaState(caName, requestId, requestType, status, cert,
229 challengeType, challengeStatus, time::fromIsoString(challengeTp),
230 remainingTries, time::seconds(remainingTime),
231 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700232 }
233 else {
tylerliu8704d032020-06-23 10:18:15 -0700234 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700235 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700236 }
237 return result;
238}
239
tylerliu8704d032020-06-23 10:18:15 -0700240std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700241CaSqlite::listAllRequests(const Name& caName)
242{
tylerliu8704d032020-06-23 10:18:15 -0700243 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700244 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700245 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700246 challenge_status, cert_request, challenge_type, challenge_secrets,
tylerliu8e170d62020-09-30 01:31:53 -0700247 challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
tylerliu8704d032020-06-23 10:18:15 -0700248 FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700249 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
250
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400251 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700252 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700253 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700254 auto status = static_cast<Status>(statement.getInt(3));
255 auto challengeStatus = statement.getString(4);
tylerliua7bea662020-10-08 18:51:02 -0700256 security::Certificate cert(statement.getBlock(5));
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700257 auto challengeType = statement.getString(6);
258 auto challengeSecrets = statement.getString(7);
259 auto challengeTp = statement.getString(8);
260 auto remainingTries = statement.getInt(9);
261 auto remainingTime = statement.getInt(10);
262 auto requestType = static_cast<RequestType>(statement.getInt(11));
263 auto encryptionKey = statement.getBlock(12);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700264 if (challengeType != "") {
tylerliu8704d032020-06-23 10:18:15 -0700265 result.push_back(CaState(caName, requestId, requestType, status, cert,
266 challengeType, challengeStatus, time::fromIsoString(challengeTp),
267 remainingTries, time::seconds(remainingTime),
268 convertString2Json(challengeSecrets), encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700269 }
270 else {
tylerliu8704d032020-06-23 10:18:15 -0700271 result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700272 }
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700273 }
274 return result;
275}
276
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700277void
278CaSqlite::deleteRequest(const std::string& requestId)
279{
280 Sqlite3Statement statement(m_database,
tylerliu8704d032020-06-23 10:18:15 -0700281 R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700282 statement.bind(1, requestId, SQLITE_TRANSIENT);
283 statement.step();
284}
285
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700286} // namespace ndncert
287} // namespace ndn