blob: 941b8b0efe2ca9393abde5da5590eaa55826438d [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 Zhangef6b36a2020-09-22 21:20:59 -070023#include <ndn-cxx/security/v2/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070024#include <ndn-cxx/util/sqlite3-statement.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070025#include <sqlite3.h>
26#include <boost/filesystem.hpp>
27
28namespace ndn {
29namespace ndncert {
30
Davide Pesaventob48bbda2020-07-27 19:41:37 -040031const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070032
33NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
34
35using namespace ndn::util;
36
37static const std::string INITIALIZATION = R"_DBTEXT_(
38CREATE TABLE IF NOT EXISTS
39 CertRequests(
40 id INTEGER PRIMARY KEY,
41 request_id TEXT NOT NULL,
42 ca_name BLOB NOT NULL,
tylerliu182bc532020-09-25 01:54:45 -070043 request_type INTEGER NOT NULL,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 status INTEGER NOT NULL,
45 challenge_status TEXT,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070046 cert_key_name BLOB NOT NULL,
47 cert_request BLOB NOT NULL,
48 challenge_type TEXT,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049 challenge_secrets TEXT,
50 challenge_tp TEXT,
51 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070052 remaining_time INTEGER,
53 probe_token BLOB
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070054 );
55CREATE UNIQUE INDEX IF NOT EXISTS
56 CertRequestIdIndex ON CertRequests(request_id);
57CREATE UNIQUE INDEX IF NOT EXISTS
58 CertRequestKeyNameIndex ON CertRequests(cert_key_name);
59
60CREATE TABLE IF NOT EXISTS
61 IssuedCerts(
62 id INTEGER PRIMARY KEY,
63 cert_id TEXT NOT NULL,
64 cert_key_name BLOB NOT NULL,
65 cert BLOB NOT NULL
66 );
67CREATE UNIQUE INDEX IF NOT EXISTS
68 IssuedCertRequestIdIndex ON IssuedCerts(cert_id);
69CREATE UNIQUE INDEX IF NOT EXISTS
70 IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name);
71)_DBTEXT_";
72
73CaSqlite::CaSqlite(const std::string& location)
74 : CaStorage()
75{
76 // Determine the path of sqlite db
77 boost::filesystem::path dbDir;
78 if (!location.empty()) {
79 dbDir = boost::filesystem::path(location);
80 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070081 else if (getenv("HOME") != nullptr) {
82 dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
83 }
84 else {
85 dbDir = boost::filesystem::current_path() / ".ndn";
86 }
87 boost::filesystem::create_directories(dbDir);
88
89 // open and initialize database
90 int result = sqlite3_open_v2((dbDir / "ndncert-ca.db").c_str(), &m_database,
91 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
92#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
93 "unix-dotfile"
94#else
95 nullptr
96#endif
97 );
98 if (result != SQLITE_OK)
99 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
100
101 // initialize database specific tables
102 char* errorMessage = nullptr;
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400103 result = sqlite3_exec(m_database, INITIALIZATION.data(),
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700104 nullptr, nullptr, &errorMessage);
105 if (result != SQLITE_OK && errorMessage != nullptr) {
106 sqlite3_free(errorMessage);
107 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be initialized"));
108 }
109}
110
111CaSqlite::~CaSqlite()
112{
113 sqlite3_close(m_database);
114}
115
116CertificateRequest
117CaSqlite::getRequest(const std::string& requestId)
118{
119 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700120 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
121 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
122 challenge_tp, remaining_tries, remaining_time, request_type, probe_token
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700123 FROM CertRequests where request_id = ?)_SQLTEXT_");
124 statement.bind(1, requestId, SQLITE_TRANSIENT);
125
126 if (statement.step() == SQLITE_ROW) {
127 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700128 auto status = static_cast<Status>(statement.getInt(3));
129 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700130 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700131 auto challengeType = statement.getString(7);
132 auto challengeSecrets = statement.getString(8);
133 auto challengeTp = statement.getString(9);
134 auto remainingTries = statement.getInt(10);
135 auto remainingTime = statement.getInt(11);
136 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700137 CertificateRequest request(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138 challengeTp, remainingTime, remainingTries,
139 convertString2Json(challengeSecrets), cert);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700140 return request;
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700141 }
142 else {
143 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database"));
144 }
145}
146
147void
148CaSqlite::addRequest(const CertificateRequest& request)
149{
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700150 // check whether request is there already
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700151 Sqlite3Statement statement1(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700152 R"_SQLTEXT_(SELECT * FROM CertRequests where cert_key_name = ?)_SQLTEXT_");
153 statement1.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700154 if (statement1.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700155 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700156 return;
157 }
158
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700159 // check whether certificate is already issued
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700160 Sqlite3Statement statement2(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700161 R"_SQLTEXT_(SELECT * FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_");
162 statement2.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700163 if (statement2.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700164 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700165 return;
166 }
167
Zhiyi Zhangb8bbc642020-09-29 14:08:26 -0700168 Sqlite3Statement statement(
169 m_database,
170 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
171 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
172 challenge_tp, remaining_tries, remaining_time, request_type)
173 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
174 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
175 statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
176 statement.bind(3, static_cast<int>(request.m_status));
177 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
178 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
179 SQLITE_TRANSIENT);
180 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
181 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
182 statement.bind(8, convertJson2String(request.m_challengeSecrets),
183 SQLITE_TRANSIENT);
184 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
185 statement.bind(10, request.m_remainingTries);
186 statement.bind(11, request.m_remainingTime);
187 statement.bind(12, static_cast<int>(request.m_requestType));
188 if (statement.step() != SQLITE_DONE) {
189 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700190 }
191}
192
193void
194CaSqlite::updateRequest(const CertificateRequest& request)
195{
196 Sqlite3Statement statement(m_database,
197 R"_SQLTEXT_(UPDATE CertRequests
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700198 SET status = ?, challenge_status = ?, challenge_type = ?, challenge_secrets = ?,
tylerliu182bc532020-09-25 01:54:45 -0700199 challenge_tp = ?, remaining_tries = ?, remaining_time = ?, request_type = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700200 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700201 statement.bind(1, static_cast<int>(request.m_status));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700202 statement.bind(2, request.m_challengeStatus, SQLITE_TRANSIENT);
203 statement.bind(3, request.m_challengeType, SQLITE_TRANSIENT);
204 statement.bind(4, convertJson2String(request.m_challengeSecrets), SQLITE_TRANSIENT);
205 statement.bind(5, request.m_challengeTp, SQLITE_TRANSIENT);
206 statement.bind(6, request.m_remainingTries);
207 statement.bind(7, request.m_remainingTime);
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700208 statement.bind(8, static_cast<int>(request.m_requestType));
tylerliu182bc532020-09-25 01:54:45 -0700209 statement.bind(9, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700210
211 if (statement.step() != SQLITE_DONE) {
212 addRequest(request);
213 }
214}
215
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700216std::list<CertificateRequest>
217CaSqlite::listAllRequests()
218{
219 std::list<CertificateRequest> result;
tylerliu182bc532020-09-25 01:54:45 -0700220 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
221 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
222 challenge_tp, remaining_tries, remaining_time, request_type
223 FROM CertRequests)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700224
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400225 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700226 auto requestId = statement.getString(1);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700227 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700228 auto status = static_cast<Status>(statement.getInt(3));
229 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700230 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700231 auto challengeType = statement.getString(7);
232 auto challengeSecrets = statement.getString(8);
233 auto challengeTp = statement.getString(9);
234 auto remainingTries = statement.getInt(10);
235 auto remainingTime = statement.getInt(11);
236 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700237 CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700238 challengeTp, remainingTime, remainingTries,
239 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700240 result.push_back(entry);
241 }
242 return result;
243}
244
245std::list<CertificateRequest>
246CaSqlite::listAllRequests(const Name& caName)
247{
248 std::list<CertificateRequest> result;
249 Sqlite3Statement statement(m_database,
tylerliu182bc532020-09-25 01:54:45 -0700250 R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
251 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
252 challenge_tp, remaining_tries, remaining_time, request_type
253 FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700254 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
255
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400256 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700257 auto requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700258 Name caName(statement.getBlock(2));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700259 auto status = static_cast<Status>(statement.getInt(3));
260 auto challengeStatus = statement.getString(4);
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700261 security::v2::Certificate cert(statement.getBlock(6));
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -0700262 auto challengeType = statement.getString(7);
263 auto challengeSecrets = statement.getString(8);
264 auto challengeTp = statement.getString(9);
265 auto remainingTries = statement.getInt(10);
266 auto remainingTime = statement.getInt(11);
267 auto requestType = static_cast<RequestType>(statement.getInt(12));
tylerliu182bc532020-09-25 01:54:45 -0700268 CertificateRequest entry(caName, requestId, requestType, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700269 challengeTp, remainingTime, remainingTries,
270 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700271 result.push_back(entry);
272 }
273 return result;
274}
275
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700276void
277CaSqlite::deleteRequest(const std::string& requestId)
278{
279 Sqlite3Statement statement(m_database,
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700280 R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700281 statement.bind(1, requestId, SQLITE_TRANSIENT);
282 statement.step();
283}
284
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700285security::v2::Certificate
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700286CaSqlite::getCertificate(const std::string& certId)
287{
288 Sqlite3Statement statement(m_database,
289 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
290 statement.bind(1, certId, SQLITE_TRANSIENT);
291
292 if (statement.step() == SQLITE_ROW) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700293 return security::v2::Certificate(statement.getBlock(0));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700294 }
295 else {
296 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
297 }
298}
299
300void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700301CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700302{
303 Sqlite3Statement statement(m_database,
304 R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert)
305 values (?, ?, ?))_SQLTEXT_");
306 statement.bind(1, certId, SQLITE_TRANSIENT);
307 statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
308 statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT);
309
310 if (statement.step() != SQLITE_DONE) {
311 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database"));
312 }
313}
314
315void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700316CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700317{
318 Sqlite3Statement statement(m_database,
319 R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_");
320 statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT);
321 statement.bind(2, certId, SQLITE_TRANSIENT);
322
323 if (statement.step() != SQLITE_DONE) {
324 addCertificate(certId, cert);
325 }
326}
327
328void
329CaSqlite::deleteCertificate(const std::string& certId)
330{
331 Sqlite3Statement statement(m_database,
332 R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_");
333 statement.bind(1, certId, SQLITE_TRANSIENT);
334 statement.step();
335}
336
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700337std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700338CaSqlite::listAllIssuedCertificates()
339{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700340 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700341 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
342
343 while (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400344 result.emplace_back(statement.getBlock(3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700345 }
346 return result;
347}
348
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700349std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700350CaSqlite::listAllIssuedCertificates(const Name& caName)
351{
352 auto allCerts = listAllIssuedCertificates();
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700353 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700354 for (const auto& entry : allCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700355 const auto& klName = entry.getSignature().getKeyLocator().getName();
356 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700357 result.push_back(entry);
358 }
359 }
360 return result;
361}
362
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700363std::string
364CaSqlite::convertJson2String(const JsonSection& json)
365{
366 std::stringstream ss;
367 boost::property_tree::write_json(ss, json);
368 return ss.str();
369}
370
371JsonSection
372CaSqlite::convertString2Json(const std::string& jsonContent)
373{
374 std::istringstream ss(jsonContent);
375 JsonSection json;
376 boost::property_tree::json_parser::read_json(ss, json);
377 return json;
378}
379
380} // namespace ndncert
381} // namespace ndn