blob: cf530222584efd9caac16eaab9a33f5ace3cf69f [file] [log] [blame]
Zhiyi Zhang91c846b2017-04-12 14:16:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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"
22#include <ndn-cxx/util/sqlite3-statement.hpp>
23
24#include <sqlite3.h>
25#include <boost/filesystem.hpp>
26
27namespace ndn {
28namespace ndncert {
29
30const std::string
31CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
32
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,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043 status INTEGER NOT NULL,
44 challenge_status TEXT,
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070045 cert_key_name BLOB NOT NULL,
46 cert_request BLOB NOT NULL,
47 challenge_type TEXT,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 challenge_secrets TEXT,
49 challenge_tp TEXT,
50 remaining_tries INTEGER,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070051 remaining_time INTEGER,
52 probe_token BLOB
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070053 );
54CREATE UNIQUE INDEX IF NOT EXISTS
55 CertRequestIdIndex ON CertRequests(request_id);
56CREATE UNIQUE INDEX IF NOT EXISTS
57 CertRequestKeyNameIndex ON CertRequests(cert_key_name);
58
59CREATE TABLE IF NOT EXISTS
60 IssuedCerts(
61 id INTEGER PRIMARY KEY,
62 cert_id TEXT NOT NULL,
63 cert_key_name BLOB NOT NULL,
64 cert BLOB NOT NULL
65 );
66CREATE UNIQUE INDEX IF NOT EXISTS
67 IssuedCertRequestIdIndex ON IssuedCerts(cert_id);
68CREATE UNIQUE INDEX IF NOT EXISTS
69 IssuedCertKeyNameIndex ON IssuedCerts(cert_key_name);
70)_DBTEXT_";
71
72CaSqlite::CaSqlite(const std::string& location)
73 : CaStorage()
74{
75 // Determine the path of sqlite db
76 boost::filesystem::path dbDir;
77 if (!location.empty()) {
78 dbDir = boost::filesystem::path(location);
79 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -070080 else if (getenv("HOME") != nullptr) {
81 dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
82 }
83 else {
84 dbDir = boost::filesystem::current_path() / ".ndn";
85 }
86 boost::filesystem::create_directories(dbDir);
87
88 // open and initialize database
89 int result = sqlite3_open_v2((dbDir / "ndncert-ca.db").c_str(), &m_database,
90 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
91#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
92 "unix-dotfile"
93#else
94 nullptr
95#endif
96 );
97 if (result != SQLITE_OK)
98 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
99
100 // initialize database specific tables
101 char* errorMessage = nullptr;
102 result = sqlite3_exec(m_database, INITIALIZATION.c_str(),
103 nullptr, nullptr, &errorMessage);
104 if (result != SQLITE_OK && errorMessage != nullptr) {
105 sqlite3_free(errorMessage);
106 BOOST_THROW_EXCEPTION(Error("CaSqlite DB cannot be initialized"));
107 }
108}
109
110CaSqlite::~CaSqlite()
111{
112 sqlite3_close(m_database);
113}
114
115CertificateRequest
116CaSqlite::getRequest(const std::string& requestId)
117{
118 Sqlite3Statement statement(m_database,
119 R"_SQLTEXT_(SELECT *
120 FROM CertRequests where request_id = ?)_SQLTEXT_");
121 statement.bind(1, requestId, SQLITE_TRANSIENT);
122
123 if (statement.step() == SQLITE_ROW) {
124 Name caName(statement.getBlock(2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700125 int status = statement.getInt(3);
126 std::string challengeStatus = statement.getString(4);
127 security::v2::Certificate cert(statement.getBlock(6));
128 std::string challengeType = statement.getString(7);
129 std::string challengeSecrets = statement.getString(8);
130 std::string challengeTp = statement.getString(9);
131 int remainingTries = statement.getInt(10);
132 int remainingTime = statement.getInt(11);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700133 CertificateRequest request(caName, requestId, status, challengeStatus, challengeType,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700134 challengeTp, remainingTime, remainingTries,
135 convertString2Json(challengeSecrets), cert);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700136 if (statement.getSize(12) != 0) {
137 shared_ptr<Data> probeToken = make_shared<Data>(statement.getBlock(12));
138 request.setProbeToken(probeToken);
139 }
140 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 Zhang5f749a22019-06-12 17:02:33 -0700168 if (request.m_probeToken != nullptr) {
169 Sqlite3Statement statement(
170 m_database,
171 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
172 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
173 challenge_tp, remaining_tries, remaining_time, probe_token)
174 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
175 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
176 statement.bind(2, request.m_caName.wireEncode(), SQLITE_TRANSIENT);
177 statement.bind(3, request.m_status);
178 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
179 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
180 SQLITE_TRANSIENT);
181 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
182 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
183 statement.bind(8, convertJson2String(request.m_challengeSecrets),
184 SQLITE_TRANSIENT);
185 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
186 statement.bind(10, request.m_remainingTries);
187 statement.bind(11, request.m_remainingTime);
188 statement.bind(12, request.m_probeToken->wireEncode(), SQLITE_TRANSIENT);
189 if (statement.step() != SQLITE_DONE) {
190 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
191 }
192 }
193 else {
194 Sqlite3Statement statement(
195 m_database,
196 R"_SQLTEXT_(INSERT INTO CertRequests (request_id, ca_name, status,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700197 challenge_status, cert_key_name, cert_request, challenge_type, challenge_secrets,
198 challenge_tp, remaining_tries, remaining_time)
199 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700200 statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
201 statement.bind(2, request.m_caName.wireEncode(), SQLITE_TRANSIENT);
202 statement.bind(3, request.m_status);
203 statement.bind(4, request.m_challengeStatus, SQLITE_TRANSIENT);
204 statement.bind(5, request.m_cert.getKeyName().wireEncode(),
205 SQLITE_TRANSIENT);
206 statement.bind(6, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
207 statement.bind(7, request.m_challengeType, SQLITE_TRANSIENT);
208 statement.bind(8, convertJson2String(request.m_challengeSecrets),
209 SQLITE_TRANSIENT);
210 statement.bind(9, request.m_challengeTp, SQLITE_TRANSIENT);
211 statement.bind(10, request.m_remainingTries);
212 statement.bind(11, request.m_remainingTime);
213 if (statement.step() != SQLITE_DONE) {
214 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " cannot be added to database"));
215 }
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700216 }
217}
218
219void
220CaSqlite::updateRequest(const CertificateRequest& request)
221{
222 Sqlite3Statement statement(m_database,
223 R"_SQLTEXT_(UPDATE CertRequests
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700224 SET status = ?, challenge_status = ?, challenge_type = ?, challenge_secrets = ?,
225 challenge_tp = ?, remaining_tries = ?, remaining_time = ?
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700226 WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700227 statement.bind(1, request.m_status);
228 statement.bind(2, request.m_challengeStatus, SQLITE_TRANSIENT);
229 statement.bind(3, request.m_challengeType, SQLITE_TRANSIENT);
230 statement.bind(4, convertJson2String(request.m_challengeSecrets), SQLITE_TRANSIENT);
231 statement.bind(5, request.m_challengeTp, SQLITE_TRANSIENT);
232 statement.bind(6, request.m_remainingTries);
233 statement.bind(7, request.m_remainingTime);
234 statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700235
236 if (statement.step() != SQLITE_DONE) {
237 addRequest(request);
238 }
239}
240
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700241std::list<CertificateRequest>
242CaSqlite::listAllRequests()
243{
244 std::list<CertificateRequest> result;
245 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM CertRequests)_SQLTEXT_");
246
247 while(statement.step() == SQLITE_ROW) {
248 std::string requestId = statement.getString(1);
249 Name caName(statement.getBlock(2));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700250 int status = statement.getInt(3);
251 std::string challengeStatus = statement.getString(4);
252 security::v2::Certificate cert(statement.getBlock(6));
253 std::string challengeType = statement.getString(7);
254 std::string challengeSecrets = statement.getString(8);
255 std::string challengeTp = statement.getString(9);
256 int remainingTries = statement.getInt(10);
257 int remainingTime = statement.getInt(11);
258 CertificateRequest entry(caName, requestId, status, challengeStatus, challengeType,
259 challengeTp, remainingTime, remainingTries,
260 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700261 result.push_back(entry);
262 }
263 return result;
264}
265
266std::list<CertificateRequest>
267CaSqlite::listAllRequests(const Name& caName)
268{
269 std::list<CertificateRequest> result;
270 Sqlite3Statement statement(m_database,
271 R"_SQLTEXT_(SELECT * FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
272 statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
273
274 while(statement.step() == SQLITE_ROW) {
275 std::string requestId = statement.getString(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700276 Name caName(statement.getBlock(2));
277 int status = statement.getInt(3);
278 std::string challengeStatus = statement.getString(4);
279 security::v2::Certificate cert(statement.getBlock(6));
280 std::string challengeType = statement.getString(7);
281 std::string challengeSecrets = statement.getString(8);
282 std::string challengeTp = statement.getString(9);
283 int remainingTries = statement.getInt(10);
284 int remainingTime = statement.getInt(11);
285 CertificateRequest entry(caName, requestId, status, challengeStatus, challengeType,
286 challengeTp, remainingTime, remainingTries,
287 convertString2Json(challengeSecrets), cert);
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700288 result.push_back(entry);
289 }
290 return result;
291}
292
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700293void
294CaSqlite::deleteRequest(const std::string& requestId)
295{
296 Sqlite3Statement statement(m_database,
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700297 R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
Zhiyi Zhang91c846b2017-04-12 14:16:31 -0700298 statement.bind(1, requestId, SQLITE_TRANSIENT);
299 statement.step();
300}
301
302security::v2::Certificate
303CaSqlite::getCertificate(const std::string& certId)
304{
305 Sqlite3Statement statement(m_database,
306 R"_SQLTEXT_(SELECT cert FROM IssuedCerts where cert_id = ?)_SQLTEXT_");
307 statement.bind(1, certId, SQLITE_TRANSIENT);
308
309 if (statement.step() == SQLITE_ROW) {
310 security::v2::Certificate cert(statement.getBlock(0));
311 return cert;
312 }
313 else {
314 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " cannot be fetched from database"));
315 }
316}
317
318void
319CaSqlite::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
320{
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
334CaSqlite::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
335{
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
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700355std::list<security::v2::Certificate>
356CaSqlite::listAllIssuedCertificates()
357{
358 std::list<security::v2::Certificate> result;
359 Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
360
361 while (statement.step() == SQLITE_ROW) {
362 security::v2::Certificate cert(statement.getBlock(3));
363 result.push_back(cert);
364 }
365 return result;
366}
367
368std::list<security::v2::Certificate>
369CaSqlite::listAllIssuedCertificates(const Name& caName)
370{
371 auto allCerts = listAllIssuedCertificates();
372 std::list<security::v2::Certificate> result;
373 for (const auto& entry : allCerts) {
374 if (entry.getSignature().getKeyLocator().getName().getPrefix(-2) == caName) {
375 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