blob: b392ba11d32f9fcf934419815a9d1ba9f7e10252 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson7ca11f22013-10-04 19:01:30 -07002/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070023 */
24
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "sec-public-info-sqlite3.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070026#include "v1/identity-certificate.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080027#include "signature-sha256-with-rsa.hpp"
Yingdi Yuc8f883c2014-06-20 23:25:22 -070028#include "signature-sha256-with-ecdsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080029#include "../data.hpp"
30
Yingdi Yu874678f2014-01-22 19:30:34 -080031#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070032#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070033#include <stdlib.h>
34#include <sstream>
35#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080036#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080037
Yingdi Yufc40d872014-02-18 12:56:04 -080038namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070039namespace security {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070040
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070041using std::string;
42using std::vector;
43
Alexander Afanasyev07113802015-01-15 19:14:36 -080044const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070045
Yingdi Yu41546342014-11-30 23:37:53 -080046static const string INIT_TPM_INFO_TABLE =
47 "CREATE TABLE IF NOT EXISTS "
48 " TpmInfo( "
49 " tpm_locator BLOB NOT NULL,"
50 " PRIMARY KEY (tpm_locator) "
51 " ); ";
Jeff Thompson7ca11f22013-10-04 19:01:30 -070052
Yingdi Yu41546342014-11-30 23:37:53 -080053static const string INIT_ID_TABLE =
54 "CREATE TABLE IF NOT EXISTS "
55 " Identity( "
56 " identity_name BLOB NOT NULL, "
57 " default_identity INTEGER DEFAULT 0, "
58 " PRIMARY KEY (identity_name) "
59 " ); "
60 "CREATE INDEX identity_index ON Identity(identity_name);";
61
62static const string INIT_KEY_TABLE =
63 "CREATE TABLE IF NOT EXISTS "
64 " Key( "
65 " identity_name BLOB NOT NULL, "
66 " key_identifier BLOB NOT NULL, "
67 " key_type INTEGER, "
68 " public_key BLOB, "
69 " default_key INTEGER DEFAULT 0, "
70 " active INTEGER DEFAULT 0, "
71 " PRIMARY KEY (identity_name, key_identifier)"
72 " ); "
73 "CREATE INDEX key_index ON Key(identity_name); ";
74
75
76static const string INIT_CERT_TABLE =
77 "CREATE TABLE IF NOT EXISTS "
78 " Certificate( "
79 " cert_name BLOB NOT NULL, "
80 " cert_issuer BLOB NOT NULL, "
81 " identity_name BLOB NOT NULL, "
82 " key_identifier BLOB NOT NULL, "
83 " not_before TIMESTAMP, "
84 " not_after TIMESTAMP, "
85 " certificate_data BLOB NOT NULL, "
86 " valid_flag INTEGER DEFAULT 1, "
87 " default_cert INTEGER DEFAULT 0, "
88 " PRIMARY KEY (cert_name) "
89 " ); "
90 "CREATE INDEX cert_index ON Certificate(cert_name); "
91 "CREATE INDEX subject ON Certificate(identity_name);";
Jeff Thompson7ca11f22013-10-04 19:01:30 -070092
93/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 * A utility function to call the normal sqlite3_bind_text where the value and length are
95 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070096 */
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -070097static int
Yingdi Yu41546342014-11-30 23:37:53 -080098sqlite3_bind_string(sqlite3_stmt* statement,
99 int index,
100 const string& value,
101 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700102{
103 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
104}
105
Yingdi Yu41546342014-11-30 23:37:53 -0800106static string
107sqlite3_column_string(sqlite3_stmt* statement, int column)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700108{
Yingdi Yu41546342014-11-30 23:37:53 -0800109 return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)),
110 sqlite3_column_bytes(statement, column));
111}
112
113SecPublicInfoSqlite3::SecPublicInfoSqlite3(const std::string& dir)
114 : SecPublicInfo(dir)
115 , m_database(nullptr)
116{
117 boost::filesystem::path identityDir;
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700118 if (dir == "") {
119#ifdef NDN_CXX_HAVE_TESTS
120 if (getenv("TEST_HOME") != nullptr) {
121 identityDir = boost::filesystem::path(getenv("TEST_HOME")) / ".ndn";
122 }
123 else
124#endif // NDN_CXX_HAVE_TESTS
125 if (getenv("HOME") != nullptr) {
126 identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
127 }
128 else {
129 identityDir = boost::filesystem::path(".") / ".ndn";
130 }
131 }
132 else {
133 identityDir = boost::filesystem::path(dir);
134 }
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700135 boost::filesystem::create_directories(identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700136
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800137 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
138 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
139 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700140#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800141 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800142#else
143 0
144#endif
145 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700146 if (res != SQLITE_OK)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700147 BOOST_THROW_EXCEPTION(Error("identity DB cannot be opened/created"));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148
Yingdi Yu41546342014-11-30 23:37:53 -0800149
Junxiao Shi98acbb02014-12-02 11:00:42 -0700150 BOOST_ASSERT(m_database != nullptr);
151
Yingdi Yu41546342014-11-30 23:37:53 -0800152 initializeTable("TpmInfo", INIT_TPM_INFO_TABLE); // Check if TpmInfo table exists;
153 initializeTable("Identity", INIT_ID_TABLE); // Check if Identity table exists;
154 initializeTable("Key", INIT_KEY_TABLE); // Check if Key table exists;
155 initializeTable("Certificate", INIT_CERT_TABLE); // Check if Certificate table exists;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700156}
157
Yingdi Yu87581582014-01-14 14:28:39 -0800158SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700159{
Junxiao Shi98acbb02014-12-02 11:00:42 -0700160 sqlite3_close(m_database);
161 m_database = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700162}
163
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700164bool
Yingdi Yu41546342014-11-30 23:37:53 -0800165SecPublicInfoSqlite3::doesTableExist(const string& tableName)
166{
167 // Check if the table exists;
168 bool doesTableExist = false;
169 string checkingString =
170 "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
171
Yingdi Yua90ba482014-12-09 10:54:43 -0800172 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800173 sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0);
174
175 int result = sqlite3_step(statement);
176 if (result == SQLITE_ROW)
177 doesTableExist = true;
178 sqlite3_finalize(statement);
179
180 return doesTableExist;
181}
182
183bool
184SecPublicInfoSqlite3::initializeTable(const string& tableName, const string& initCommand)
185{
186 // Create the table if it does not exist
187 if (!doesTableExist(tableName)) {
188 char* errorMessage = 0;
189 int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage);
190
191 if (result != SQLITE_OK && errorMessage != 0) {
192 sqlite3_free(errorMessage);
193 return false;
194 }
195 }
196
197 return true;
198}
199
200void
201SecPublicInfoSqlite3::deleteTable(const string& tableName)
202{
203 string query = "DROP TABLE IF EXISTS " + tableName;
204
Yingdi Yua90ba482014-12-09 10:54:43 -0800205 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800206 sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0);
207
208 sqlite3_step(statement);
209 sqlite3_finalize(statement);
210}
211
212void
213SecPublicInfoSqlite3::setTpmLocator(const string& tpmLocator)
214{
215 string currentTpm;
216 try {
217 currentTpm = getTpmLocator();
218 }
219 catch (SecPublicInfo::Error&) {
220 setTpmLocatorInternal(tpmLocator, false); // set tpmInfo without resetting
221 return;
222 }
223
224 if (currentTpm == tpmLocator)
225 return; // if the same, nothing will be changed
226
227 setTpmLocatorInternal(tpmLocator, true); // set tpmInfo and reset pib
228}
229
230string
231SecPublicInfoSqlite3::getTpmLocator()
232{
Yingdi Yua90ba482014-12-09 10:54:43 -0800233 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800234 sqlite3_prepare_v2(m_database, "SELECT tpm_locator FROM TpmInfo", -1, &statement, 0);
235
236 int res = sqlite3_step(statement);
237
238 if (res == SQLITE_ROW) {
239 string tpmLocator = sqlite3_column_string(statement, 0);
240 sqlite3_finalize(statement);
241 return tpmLocator;
242 }
243 else {
244 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700245 BOOST_THROW_EXCEPTION(SecPublicInfo::Error("TPM info does not exist"));
Yingdi Yu41546342014-11-30 23:37:53 -0800246 }
247}
248
249void
250SecPublicInfoSqlite3::setTpmLocatorInternal(const string& tpmLocator, bool needReset)
251{
Yingdi Yua90ba482014-12-09 10:54:43 -0800252 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800253
254 if (needReset) {
255 deleteTable("Identity");
256 deleteTable("Key");
257 deleteTable("Certificate");
258
259 initializeTable("Identity", INIT_ID_TABLE);
260 initializeTable("Key", INIT_KEY_TABLE);
261 initializeTable("Certificate", INIT_CERT_TABLE);
262
263 sqlite3_prepare_v2(m_database, "UPDATE TpmInfo SET tpm_locator = ?",
264 -1, &statement, 0);
265 sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
266 }
267 else {
268 // no reset implies there is no tpmLocator record, insert one
269 sqlite3_prepare_v2(m_database, "INSERT INTO TpmInfo (tpm_locator) VALUES (?)",
270 -1, &statement, 0);
271 sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
272 }
273
274 sqlite3_step(statement);
275 sqlite3_finalize(statement);
276}
277
278std::string
279SecPublicInfoSqlite3::getPibLocator()
280{
281 return string("pib-sqlite3:").append(m_location);
282}
283
284bool
Yingdi Yu87581582014-01-14 14:28:39 -0800285SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700286{
287 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288
Yingdi Yua90ba482014-12-09 10:54:43 -0800289 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700290 sqlite3_prepare_v2(m_database,
291 "SELECT count(*) FROM Identity WHERE identity_name=?",
292 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700293
Yingdi Yu41546342014-11-30 23:37:53 -0800294 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700295 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700296
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700297 if (res == SQLITE_ROW) {
298 int countAll = sqlite3_column_int(statement, 0);
299 if (countAll > 0)
300 result = true;
301 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700302
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700303 sqlite3_finalize(statement);
304
305 return result;
306}
307
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700308void
Yingdi Yu87581582014-01-14 14:28:39 -0800309SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700310{
Yingdi Yu05842f22014-04-15 19:21:56 -0700311 if (doesIdentityExist(identityName))
312 return;
313
Yingdi Yua90ba482014-12-09 10:54:43 -0800314 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700315
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700316 sqlite3_prepare_v2(m_database,
317 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
318 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700319
Yingdi Yu41546342014-11-30 23:37:53 -0800320 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700321
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800322 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700323
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700324 sqlite3_finalize(statement);
325}
326
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700327bool
Yingdi Yu87581582014-01-14 14:28:39 -0800328SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700329{
330 //TODO:
331 return false;
332}
333
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700334bool
Yingdi Yu87581582014-01-14 14:28:39 -0800335SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700336{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700337 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700338 BOOST_THROW_EXCEPTION(Error("Incorrect key name " + keyName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800339
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700340 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800341 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700342
Yingdi Yua90ba482014-12-09 10:54:43 -0800343 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700344 sqlite3_prepare_v2(m_database,
345 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
346 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700347
Yingdi Yu41546342014-11-30 23:37:53 -0800348 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
349 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700350
351 int res = sqlite3_step(statement);
352
353 bool keyIdExist = false;
354 if (res == SQLITE_ROW) {
355 int countAll = sqlite3_column_int(statement, 0);
356 if (countAll > 0)
357 keyIdExist = true;
358 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700359
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700360 sqlite3_finalize(statement);
361
362 return keyIdExist;
363}
364
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700365void
Yingdi Yu40b53092014-06-17 17:10:02 -0700366SecPublicInfoSqlite3::addKey(const Name& keyName,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700367 const v1::PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700368{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700369 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800370 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800371
Yingdi Yu05842f22014-04-15 19:21:56 -0700372 if (doesPublicKeyExist(keyName))
373 return;
374
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700375 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800376 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700377
Yingdi Yu2e57a582014-02-20 23:34:43 -0800378 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700379
Yingdi Yua90ba482014-12-09 10:54:43 -0800380 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700381 sqlite3_prepare_v2(m_database,
382 "INSERT OR REPLACE INTO Key \
383 (identity_name, key_identifier, key_type, public_key) \
384 values (?, ?, ?, ?)",
385 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700386
Yingdi Yu41546342014-11-30 23:37:53 -0800387 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
388 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu99b2a002015-08-12 12:47:44 -0700389 sqlite3_bind_int(statement, 3, static_cast<int>(publicKeyDer.getKeyType()));
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700390 sqlite3_bind_blob(statement, 4,
391 publicKeyDer.get().buf(),
392 publicKeyDer.get().size(),
393 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700394
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800395 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396
397 sqlite3_finalize(statement);
398}
399
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700400shared_ptr<v1::PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800401SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700403 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700404 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey Empty keyName"));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700405
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700406 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800407 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700408
Yingdi Yua90ba482014-12-09 10:54:43 -0800409 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700410 sqlite3_prepare_v2(m_database,
411 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
412 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Yingdi Yu41546342014-11-30 23:37:53 -0800414 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
415 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700416
417 int res = sqlite3_step(statement);
418
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700419 shared_ptr<v1::PublicKey> result;
Yingdi Yua90ba482014-12-09 10:54:43 -0800420 if (res == SQLITE_ROW) {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700421 result = make_shared<v1::PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
422 sqlite3_column_bytes(statement, 0));
Yingdi Yua90ba482014-12-09 10:54:43 -0800423 sqlite3_finalize(statement);
424 return result;
425 }
426 else {
427 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700428 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey public key does not exist"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800429 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700430}
431
Yingdi Yu40b53092014-06-17 17:10:02 -0700432KeyType
433SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
434{
435 if (keyName.empty())
Yingdi Yu99b2a002015-08-12 12:47:44 -0700436 return KeyType::NONE;
Yingdi Yu40b53092014-06-17 17:10:02 -0700437
438 string keyId = keyName.get(-1).toUri();
439 Name identityName = keyName.getPrefix(-1);
440
Yingdi Yua90ba482014-12-09 10:54:43 -0800441 sqlite3_stmt* statement = nullptr;
Yingdi Yu40b53092014-06-17 17:10:02 -0700442 sqlite3_prepare_v2(m_database,
443 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
444 -1, &statement, 0);
445
Yingdi Yu41546342014-11-30 23:37:53 -0800446 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
447 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700448
449 int res = sqlite3_step(statement);
450
Yingdi Yua90ba482014-12-09 10:54:43 -0800451 if (res == SQLITE_ROW) {
452 int typeValue = sqlite3_column_int(statement, 0);
453 sqlite3_finalize(statement);
454 return static_cast<KeyType>(typeValue);
455 }
456 else {
457 sqlite3_finalize(statement);
Yingdi Yu99b2a002015-08-12 12:47:44 -0700458 return KeyType::NONE;
Yingdi Yua90ba482014-12-09 10:54:43 -0800459 }
Yingdi Yu40b53092014-06-17 17:10:02 -0700460}
461
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700462bool
Yingdi Yu87581582014-01-14 14:28:39 -0800463SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700464{
Yingdi Yua90ba482014-12-09 10:54:43 -0800465 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700466 sqlite3_prepare_v2(m_database,
467 "SELECT count(*) FROM Certificate WHERE cert_name=?",
468 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469
Yingdi Yu41546342014-11-30 23:37:53 -0800470 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700471
472 int res = sqlite3_step(statement);
473
474 bool certExist = false;
475 if (res == SQLITE_ROW) {
476 int countAll = sqlite3_column_int(statement, 0);
477 if (countAll > 0)
478 certExist = true;
479 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700480
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700481 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700482
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700483 return certExist;
484}
485
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700486void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700487SecPublicInfoSqlite3::addCertificate(const v1::IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700488{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700489 const Name& certificateName = certificate.getName();
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700490 // KeyName is from v1::IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700491 Name keyName =
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700492 v1::IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700493
Yingdi Yu40b53092014-06-17 17:10:02 -0700494 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700495
Yingdi Yu05842f22014-04-15 19:21:56 -0700496 if (doesCertificateExist(certificateName))
497 return;
498
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700499 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800500 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700501
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700502 // Insert the certificate
Yingdi Yua90ba482014-12-09 10:54:43 -0800503 sqlite3_stmt* statement = nullptr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700504 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700505 "INSERT OR REPLACE INTO Certificate \
506 (cert_name, cert_issuer, identity_name, key_identifier, \
507 not_before, not_after, certificate_data) \
508 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700509 -1, &statement, 0);
510
Yingdi Yu41546342014-11-30 23:37:53 -0800511 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512
Yingdi Yua90ba482014-12-09 10:54:43 -0800513 try {
514 // this will throw an exception if the signature is not the standard one
515 // or there is no key locator present
516 std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
517 sqlite3_bind_string(statement, 2, signerName, SQLITE_TRANSIENT);
518 }
519 catch (tlv::Error&) {
520 return;
521 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700522
Yingdi Yu41546342014-11-30 23:37:53 -0800523 sqlite3_bind_string(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
524 sqlite3_bind_string(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700525
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700526 sqlite3_bind_int64(statement, 5,
Yingdi Yua90ba482014-12-09 10:54:43 -0800527 static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotBefore()).count()));
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700528 sqlite3_bind_int64(statement, 6,
Yingdi Yua90ba482014-12-09 10:54:43 -0800529 static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700530
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700531 sqlite3_bind_blob(statement, 7,
532 certificate.wireEncode().wire(),
533 certificate.wireEncode().size(),
534 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700535
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800536 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700537
538 sqlite3_finalize(statement);
539}
540
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700541shared_ptr<v1::IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700542SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700543{
Yingdi Yua90ba482014-12-09 10:54:43 -0800544 sqlite3_stmt* statement = nullptr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545
546 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800547 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
548 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700549
Yingdi Yu41546342014-11-30 23:37:53 -0800550 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700551
Yingdi Yu4270f202014-01-28 14:19:16 -0800552 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700553
Yingdi Yua90ba482014-12-09 10:54:43 -0800554 if (res == SQLITE_ROW) {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700555 shared_ptr<v1::IdentityCertificate> certificate = make_shared<v1::IdentityCertificate>();
Yingdi Yua90ba482014-12-09 10:54:43 -0800556 try {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700557 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700558 sqlite3_column_bytes(statement, 0)));
Yingdi Yu4270f202014-01-28 14:19:16 -0800559 }
Yingdi Yua90ba482014-12-09 10:54:43 -0800560 catch (tlv::Error&) {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800561 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700562 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate cannot be "
563 "decoded"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800564 }
Yingdi Yua90ba482014-12-09 10:54:43 -0800565
566 sqlite3_finalize(statement);
567 return certificate;
568 }
569 else {
570 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700571 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate does not "
572 "exist"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800573 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700574}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700575
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700576
577Name
Yingdi Yu87581582014-01-14 14:28:39 -0800578SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700579{
Yingdi Yua90ba482014-12-09 10:54:43 -0800580 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700581 sqlite3_prepare_v2(m_database,
582 "SELECT identity_name FROM Identity WHERE default_identity=1",
583 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700584
585 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700586
Yingdi Yua90ba482014-12-09 10:54:43 -0800587 if (res == SQLITE_ROW) {
588 Name identity(sqlite3_column_string(statement, 0));
589 sqlite3_finalize(statement);
590 return identity;
591 }
592 else {
593 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700594 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800595 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700596}
597
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700598void
Yingdi Yu87581582014-01-14 14:28:39 -0800599SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700600{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800601 addIdentity(identityName);
602
Yingdi Yua90ba482014-12-09 10:54:43 -0800603 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700604
605 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700606 sqlite3_prepare_v2(m_database,
607 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
608 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700609
610 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800611 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700612
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700613 sqlite3_finalize(statement);
614
615 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700616 sqlite3_prepare_v2(m_database,
617 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
618 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700619
Yingdi Yu41546342014-11-30 23:37:53 -0800620 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700621
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700622 sqlite3_step(statement);
623
624 sqlite3_finalize(statement);
625}
626
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700627Name
Yingdi Yu87581582014-01-14 14:28:39 -0800628SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700629{
Yingdi Yua90ba482014-12-09 10:54:43 -0800630 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700631 sqlite3_prepare_v2(m_database,
632 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
633 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700634
Yingdi Yu41546342014-11-30 23:37:53 -0800635 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800636
637 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800638
Yingdi Yua90ba482014-12-09 10:54:43 -0800639 if (res == SQLITE_ROW) {
640 Name keyName = identityName;
641 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
642 sqlite3_column_bytes(statement, 0)));
643 sqlite3_finalize(statement);
644 return keyName;
645 }
646 else {
647 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700648 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not "
649 "found"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800650 }
Yingdi Yu87581582014-01-14 14:28:39 -0800651}
652
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700653void
Yingdi Yu87581582014-01-14 14:28:39 -0800654SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
655{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656 if (!doesPublicKeyExist(keyName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700657 BOOST_THROW_EXCEPTION(Error("Key does not exist:" + keyName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800658
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700659 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800660 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700661
Yingdi Yua90ba482014-12-09 10:54:43 -0800662 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700663
664 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700665 sqlite3_prepare_v2(m_database,
666 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
667 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700668
Yingdi Yu41546342014-11-30 23:37:53 -0800669 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700670
671 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700672 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700673
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700674 sqlite3_finalize(statement);
675
676 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700677 sqlite3_prepare_v2(m_database,
678 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
679 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700680
Yingdi Yu41546342014-11-30 23:37:53 -0800681 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
682 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700683
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700684 sqlite3_step(statement);
685
686 sqlite3_finalize(statement);
687}
688
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700689Name
Yingdi Yu87581582014-01-14 14:28:39 -0800690SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700691{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700692 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700693 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"));
Yingdi Yu88663af2014-01-15 15:21:38 -0800694
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700695 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800696 Name identityName = keyName.getPrefix(-1);
697
Yingdi Yua90ba482014-12-09 10:54:43 -0800698 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700699 sqlite3_prepare_v2(m_database,
700 "SELECT cert_name FROM Certificate \
701 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
702 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800703
Yingdi Yu41546342014-11-30 23:37:53 -0800704 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
705 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800706
707 int res = sqlite3_step(statement);
708
Yingdi Yua90ba482014-12-09 10:54:43 -0800709 if (res == SQLITE_ROW) {
710 Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
711 sqlite3_column_bytes(statement, 0)));
712 sqlite3_finalize(statement);
713 return certName;
714 }
715 else {
716 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700717 BOOST_THROW_EXCEPTION(Error("certificate not found"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800718 }
Yingdi Yu87581582014-01-14 14:28:39 -0800719}
720
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700721void
Yingdi Yu87581582014-01-14 14:28:39 -0800722SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
723{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700724 if (!doesCertificateExist(certificateName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700725 BOOST_THROW_EXCEPTION(Error("certificate does not exist:" + certificateName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800726
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700727 Name keyName = v1::IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700728 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800729 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700730
Yingdi Yua90ba482014-12-09 10:54:43 -0800731 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700732
733 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700734 sqlite3_prepare_v2(m_database,
735 "UPDATE Certificate SET default_cert=0 \
736 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
737 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700738
Yingdi Yu41546342014-11-30 23:37:53 -0800739 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
740 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700741
742 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700743 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700744
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700745 sqlite3_finalize(statement);
746
747 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700748 sqlite3_prepare_v2(m_database,
749 "UPDATE Certificate SET default_cert=1 \
750 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
751 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700752
Yingdi Yu41546342014-11-30 23:37:53 -0800753 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
754 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
755 sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700756
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700757 sqlite3_step(statement);
758
759 sqlite3_finalize(statement);
760}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800761
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800762void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700763SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800764{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700765 sqlite3_stmt* stmt;
766 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700767 sqlite3_prepare_v2(m_database,
768 "SELECT identity_name FROM Identity WHERE default_identity=1",
769 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800770 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700771 sqlite3_prepare_v2(m_database,
772 "SELECT identity_name FROM Identity WHERE default_identity=0",
773 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800774
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700775 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800776 nameList.push_back(Name(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700777 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700778
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700779 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700780}
781
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800782void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700783SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800784{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700785 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800786
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700787 if (isDefault)
788 sqlite3_prepare_v2(m_database,
789 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
790 -1, &stmt, 0);
791 else
792 sqlite3_prepare_v2(m_database,
793 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
794 -1, &stmt, 0);
795
Yingdi Yua90ba482014-12-09 10:54:43 -0800796 while (sqlite3_step(stmt) == SQLITE_ROW) {
797 Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
798 sqlite3_column_bytes(stmt, 0)));
799 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
800 sqlite3_column_bytes(stmt, 1)));
801 nameList.push_back(keyName);
802 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700803 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800804}
805
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800806void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700807SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
808 vector<Name>& nameList,
809 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800810{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700811 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700812
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700813 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700814 sqlite3_prepare_v2(m_database,
815 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
816 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800817 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700818 sqlite3_prepare_v2(m_database,
819 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
820 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700821
Yingdi Yu41546342014-11-30 23:37:53 -0800822 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800823
Yingdi Yua90ba482014-12-09 10:54:43 -0800824 while (sqlite3_step(stmt) == SQLITE_ROW) {
825 Name keyName(identity);
826 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
827 sqlite3_column_bytes(stmt, 0)));
828 nameList.push_back(keyName);
829 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700830 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800831}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700832
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800833void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700834SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800835{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700836 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700837
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700838 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700839 sqlite3_prepare_v2(m_database,
840 "SELECT cert_name FROM Certificate WHERE default_cert=1",
841 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800842 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700843 sqlite3_prepare_v2(m_database,
844 "SELECT cert_name FROM Certificate WHERE default_cert=0",
845 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800846
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700847 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800848 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700849 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800850
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700851 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800852}
853
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800854void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700855SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
856 vector<Name>& nameList,
857 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800858{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700859 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800860 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800861
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700862 sqlite3_stmt* stmt;
863 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700864 sqlite3_prepare_v2(m_database,
865 "SELECT cert_name FROM Certificate \
866 WHERE default_cert=1 and identity_name=? and key_identifier=?",
867 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800868 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700869 sqlite3_prepare_v2(m_database,
870 "SELECT cert_name FROM Certificate \
871 WHERE default_cert=0 and identity_name=? and key_identifier=?",
872 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800873
Yingdi Yu87581582014-01-14 14:28:39 -0800874 Name identity = keyName.getPrefix(-1);
Yingdi Yu41546342014-11-30 23:37:53 -0800875 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700876
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700877 std::string baseKeyName = keyName.get(-1).toUri();
Yingdi Yu41546342014-11-30 23:37:53 -0800878 sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800879
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700880 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800881 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700882 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800883
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700884 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800885}
886
887void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700888SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800889{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700890 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800891 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700892
893 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800895 sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800896 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700897 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800898}
899
900void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700901SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800902{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700903 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800904 return;
905
906 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700907 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800908
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700909 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700910 sqlite3_prepare_v2(m_database,
911 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
912 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800913 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
914 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800915 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700916 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800917
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700918 sqlite3_prepare_v2(m_database,
919 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
920 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800921 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
922 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800923 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700924 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800925}
926
927void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700928SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800929{
930 string identity = identityName.toUri();
931
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700932 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800933 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800934 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800935 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700936 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800937
938 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800939 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800940 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700941 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800942
943 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800944 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800945 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700946 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800947}
948
Yingdi Yu41546342014-11-30 23:37:53 -0800949std::string
950SecPublicInfoSqlite3::getScheme()
951{
952 return SCHEME;
953}
954
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700955} // namespace security
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800956} // namespace ndn