blob: 6b5b4a2aefa91e1a659f4d7ad6d4f27da4240b79 [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
23#include <ndn-cxx/security/validation-policy.hpp>
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070024#include <ndn-cxx/util/sqlite3-statement.hpp>
25
26#include <sqlite3.h>
27#include <boost/filesystem.hpp>
28
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
40 CertRequests(
41 id INTEGER PRIMARY KEY,
42 request_id TEXT NOT NULL,
43 ca_name BLOB 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,
120 R"_SQLTEXT_(SELECT *
121 FROM CertRequests where request_id = ?)_SQLTEXT_");
122 statement.bind(1, requestId, SQLITE_TRANSIENT);
123
124 if (statement.step() == SQLITE_ROW) {
125 Name caName(statement.getBlock(2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700126 int status = statement.getInt(3);
127 std::string challengeStatus = statement.getString(4);
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400128 security::Certificate cert(statement.getBlock(6));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700129 std::string challengeType = statement.getString(7);
130 std::string challengeSecrets = statement.getString(8);
131 std::string challengeTp = statement.getString(9);
132 int remainingTries = statement.getInt(10);
133 int remainingTime = statement.getInt(11);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700134 CertificateRequest request(caName, requestId, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700135 challengeTp, remainingTime, remainingTries,
136 convertString2Json(challengeSecrets), cert);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700137 if (statement.getSize(12) != 0) {
138 shared_ptr<Data> probeToken = make_shared<Data>(statement.getBlock(12));
139 request.setProbeToken(probeToken);
140 }
141 return request;
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700142 }
143 else {
144 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " cannot be fetched from database"));
145 }
146}
147
148void
149CaSqlite::addRequest(const CertificateRequest& request)
150{
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700151 // check whether request is there already
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700152 Sqlite3Statement statement1(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700153 R"_SQLTEXT_(SELECT * FROM CertRequests where cert_key_name = ?)_SQLTEXT_");
154 statement1.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700155 if (statement1.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700156 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700157 return;
158 }
159
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700160 // check whether certificate is already issued
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700161 Sqlite3Statement statement2(m_database,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700162 R"_SQLTEXT_(SELECT * FROM IssuedCerts where cert_key_name = ?)_SQLTEXT_");
163 statement2.bind(1, request.m_cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700164 if (statement2.step() == SQLITE_ROW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700165 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700166 return;
167 }
168
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700169 if (request.m_probeToken != nullptr) {
170 Sqlite3Statement statement(
171 m_database,
172 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
173 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
174 challenge_tp, remaining_tries, remaining_time, probe_token)
175 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
176 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
177 statement.bind(2, request.m_caName.wireEncode(), SQLITE_TRANSIENT);
178 statement.bind(3, request.m_status);
179 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
180 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
181 SQLITE_TRANSIENT);
182 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
183 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
184 statement.bind(8, convertJson2String(request.m_challengeSecrets),
185 SQLITE_TRANSIENT);
186 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
187 statement.bind(10, request.m_remainingTries);
188 statement.bind(11, request.m_remainingTime);
189 statement.bind(12, request.m_probeToken->wireEncode(), SQLITE_TRANSIENT);
190 if (statement.step() != SQLITE_DONE) {
191 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
192 }
193 }
194 else {
195 Sqlite3Statement statement(
196 m_database,
197 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700198 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
199 challenge_tp, remaining_tries, remaining_time)
200 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700201 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
202 statement.bind(2, request.m_caName.wireEncode(), SQLITE_TRANSIENT);
203 statement.bind(3, request.m_status);
204 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
205 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
206 SQLITE_TRANSIENT);
207 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
208 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
209 statement.bind(8, convertJson2String(request.m_challengeSecrets),
210 SQLITE_TRANSIENT);
211 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
212 statement.bind(10, request.m_remainingTries);
213 statement.bind(11, request.m_remainingTime);
214 if (statement.step() != SQLITE_DONE) {
215 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
216 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700217 }
218}
219
220void
221CaSqlite::updateRequest(const CertificateRequest& request)
222{
223 Sqlite3Statement statement(m_database,
224 R"_SQLTEXT_(UPDATE CertRequests
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700225 SET status = ?, challenge_status = ?, challenge_type = ?, challenge_secrets = ?,
226 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700227 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700228 statement.bind(1, request.m_status);
229 statement.bind(2, request.m_challengeStatus, SQLITE_TRANSIENT);
230 statement.bind(3, request.m_challengeType, SQLITE_TRANSIENT);
231 statement.bind(4, convertJson2String(request.m_challengeSecrets), SQLITE_TRANSIENT);
232 statement.bind(5, request.m_challengeTp, SQLITE_TRANSIENT);
233 statement.bind(6, request.m_remainingTries);
234 statement.bind(7, request.m_remainingTime);
235 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700236
237 if (statement.step() != SQLITE_DONE) {
238 addRequest(request);
239 }
240}
241
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700242std::list<CertificateRequest>
243CaSqlite::listAllRequests()
244{
245 std::list<CertificateRequest> result;
246 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM CertRequests)_SQLTEXT_");
247
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400248 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700249 std::string requestId = statement.getString(1);
250 Name caName(statement.getBlock(2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700251 int status = statement.getInt(3);
252 std::string challengeStatus = statement.getString(4);
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400253 security::Certificate cert(statement.getBlock(6));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700254 std::string challengeType = statement.getString(7);
255 std::string challengeSecrets = statement.getString(8);
256 std::string challengeTp = statement.getString(9);
257 int remainingTries = statement.getInt(10);
258 int remainingTime = statement.getInt(11);
259 CertificateRequest entry(caName, requestId, status, challengeStatus, challengeType,
260 challengeTp, remainingTime, remainingTries,
261 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700262 result.push_back(entry);
263 }
264 return result;
265}
266
267std::list<CertificateRequest>
268CaSqlite::listAllRequests(const Name& caName)
269{
270 std::list<CertificateRequest> result;
271 Sqlite3Statement statement(m_database,
272 R"_SQLTEXT_(SELECT * FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
273 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
274
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400275 while (statement.step() == SQLITE_ROW) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700276 std::string requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700277 Name caName(statement.getBlock(2));
278 int status = statement.getInt(3);
279 std::string challengeStatus = statement.getString(4);
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400280 security::Certificate cert(statement.getBlock(6));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700281 std::string challengeType = statement.getString(7);
282 std::string challengeSecrets = statement.getString(8);
283 std::string challengeTp = statement.getString(9);
284 int remainingTries = statement.getInt(10);
285 int remainingTime = statement.getInt(11);
286 CertificateRequest entry(caName, requestId, status, challengeStatus, challengeType,
287 challengeTp, remainingTime, remainingTries,
288 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700289 result.push_back(entry);
290 }
291 return result;
292}
293
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700294void
295CaSqlite::deleteRequest(const std::string& requestId)
296{
297 Sqlite3Statement statement(m_database,
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700298 R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700299 statement.bind(1, requestId, SQLITE_TRANSIENT);
300 statement.step();
301}
302
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400303security::Certificate
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700304CaSqlite::getCertificate(const std::string& certId)
305{
306 Sqlite3Statement statement(m_database,
307 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
308 statement.bind(1, certId, SQLITE_TRANSIENT);
309
310 if (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400311 return security::Certificate(statement.getBlock(0));
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700312 }
313 else {
314 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
315 }
316}
317
318void
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400319CaSqlite::addCertificate(const std::string& certId, const security::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700320{
321 Sqlite3Statement statement(m_database,
322 R"_SQLTEXT_(INSERT INTO IssuedCerts (cert_id, cert_key_name, cert)
323 values (?, ?, ?))_SQLTEXT_");
324 statement.bind(1, certId, SQLITE_TRANSIENT);
325 statement.bind(2, cert.getKeyName().wireEncode(), SQLITE_TRANSIENT);
326 statement.bind(3, cert.wireEncode(), SQLITE_TRANSIENT);
327
328 if (statement.step() != SQLITE_DONE) {
329 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " cannot be added to database"));
330 }
331}
332
333void
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400334CaSqlite::updateCertificate(const std::string& certId, const security::Certificate& cert)
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700335{
336 Sqlite3Statement statement(m_database,
337 R"_SQLTEXT_(UPDATE IssuedCerts SET cert = ? WHERE cert_id = ?)_SQLTEXT_");
338 statement.bind(1, cert.wireEncode(), SQLITE_TRANSIENT);
339 statement.bind(2, certId, SQLITE_TRANSIENT);
340
341 if (statement.step() != SQLITE_DONE) {
342 addCertificate(certId, cert);
343 }
344}
345
346void
347CaSqlite::deleteCertificate(const std::string& certId)
348{
349 Sqlite3Statement statement(m_database,
350 R"_SQLTEXT_(DELETE FROM IssuedCerts WHERE cert_id = ?)_SQLTEXT_");
351 statement.bind(1, certId, SQLITE_TRANSIENT);
352 statement.step();
353}
354
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400355std::list<security::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700356CaSqlite::listAllIssuedCertificates()
357{
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400358 std::list<security::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700359 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
360
361 while (statement.step() == SQLITE_ROW) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400362 result.emplace_back(statement.getBlock(3));
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700363 }
364 return result;
365}
366
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400367std::list<security::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700368CaSqlite::listAllIssuedCertificates(const Name& caName)
369{
370 auto allCerts = listAllIssuedCertificates();
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400371 std::list<security::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700372 for (const auto& entry : allCerts) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400373 const auto& klName = entry.getSignatureInfo().getKeyLocator().getName();
374 if (security::extractIdentityNameFromKeyLocator(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700375 result.push_back(entry);
376 }
377 }
378 return result;
379}
380
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700381std::string
382CaSqlite::convertJson2String(const JsonSection& json)
383{
384 std::stringstream ss;
385 boost::property_tree::write_json(ss, json);
386 return ss.str();
387}
388
389JsonSection
390CaSqlite::convertString2Json(const std::string& jsonContent)
391{
392 std::istringstream ss(jsonContent);
393 JsonSection json;
394 boost::property_tree::json_parser::read_json(ss, json);
395 return json;
396}
397
398} // namespace ndncert
399} // namespace ndn