blob: 462f215694b4cbdfeef6f88cbc6bb1aa46259d40 [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 Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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"
26#include "identity-certificate.hpp"
27#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 {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070039
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070040using std::string;
41using std::vector;
42
Alexander Afanasyev07113802015-01-15 19:14:36 -080043const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070044
Yingdi Yu41546342014-11-30 23:37:53 -080045static const string INIT_TPM_INFO_TABLE =
46 "CREATE TABLE IF NOT EXISTS "
47 " TpmInfo( "
48 " tpm_locator BLOB NOT NULL,"
49 " PRIMARY KEY (tpm_locator) "
50 " ); ";
Jeff Thompson7ca11f22013-10-04 19:01:30 -070051
Yingdi Yu41546342014-11-30 23:37:53 -080052static const string INIT_ID_TABLE =
53 "CREATE TABLE IF NOT EXISTS "
54 " Identity( "
55 " identity_name BLOB NOT NULL, "
56 " default_identity INTEGER DEFAULT 0, "
57 " PRIMARY KEY (identity_name) "
58 " ); "
59 "CREATE INDEX identity_index ON Identity(identity_name);";
60
61static const string INIT_KEY_TABLE =
62 "CREATE TABLE IF NOT EXISTS "
63 " Key( "
64 " identity_name BLOB NOT NULL, "
65 " key_identifier BLOB NOT NULL, "
66 " key_type INTEGER, "
67 " public_key BLOB, "
68 " default_key INTEGER DEFAULT 0, "
69 " active INTEGER DEFAULT 0, "
70 " PRIMARY KEY (identity_name, key_identifier)"
71 " ); "
72 "CREATE INDEX key_index ON Key(identity_name); ";
73
74
75static const string INIT_CERT_TABLE =
76 "CREATE TABLE IF NOT EXISTS "
77 " Certificate( "
78 " cert_name BLOB NOT NULL, "
79 " cert_issuer BLOB NOT NULL, "
80 " identity_name BLOB NOT NULL, "
81 " key_identifier BLOB NOT NULL, "
82 " not_before TIMESTAMP, "
83 " not_after TIMESTAMP, "
84 " certificate_data BLOB NOT NULL, "
85 " valid_flag INTEGER DEFAULT 1, "
86 " default_cert INTEGER DEFAULT 0, "
87 " PRIMARY KEY (cert_name) "
88 " ); "
89 "CREATE INDEX cert_index ON Certificate(cert_name); "
90 "CREATE INDEX subject ON Certificate(identity_name);";
Jeff Thompson7ca11f22013-10-04 19:01:30 -070091
92/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070093 * A utility function to call the normal sqlite3_bind_text where the value and length are
94 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070095 */
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -070096static int
Yingdi Yu41546342014-11-30 23:37:53 -080097sqlite3_bind_string(sqlite3_stmt* statement,
98 int index,
99 const string& value,
100 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700101{
102 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
103}
104
Yingdi Yu41546342014-11-30 23:37:53 -0800105static string
106sqlite3_column_string(sqlite3_stmt* statement, int column)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700107{
Yingdi Yu41546342014-11-30 23:37:53 -0800108 return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)),
109 sqlite3_column_bytes(statement, column));
110}
111
112SecPublicInfoSqlite3::SecPublicInfoSqlite3(const std::string& dir)
113 : SecPublicInfo(dir)
114 , m_database(nullptr)
115{
116 boost::filesystem::path identityDir;
117 if (dir == "")
118 identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
119 else
120 identityDir = boost::filesystem::path(dir) / ".ndn";
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700121 boost::filesystem::create_directories(identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700122
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800123 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
124 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
125 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700126#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800127 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800128#else
129 0
130#endif
131 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700132 if (res != SQLITE_OK)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700133 BOOST_THROW_EXCEPTION(Error("identity DB cannot be opened/created"));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Yingdi Yu41546342014-11-30 23:37:53 -0800135
Junxiao Shi98acbb02014-12-02 11:00:42 -0700136 BOOST_ASSERT(m_database != nullptr);
137
Yingdi Yu41546342014-11-30 23:37:53 -0800138 initializeTable("TpmInfo", INIT_TPM_INFO_TABLE); // Check if TpmInfo table exists;
139 initializeTable("Identity", INIT_ID_TABLE); // Check if Identity table exists;
140 initializeTable("Key", INIT_KEY_TABLE); // Check if Key table exists;
141 initializeTable("Certificate", INIT_CERT_TABLE); // Check if Certificate table exists;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700142}
143
Yingdi Yu87581582014-01-14 14:28:39 -0800144SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700145{
Junxiao Shi98acbb02014-12-02 11:00:42 -0700146 sqlite3_close(m_database);
147 m_database = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700148}
149
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700150bool
Yingdi Yu41546342014-11-30 23:37:53 -0800151SecPublicInfoSqlite3::doesTableExist(const string& tableName)
152{
153 // Check if the table exists;
154 bool doesTableExist = false;
155 string checkingString =
156 "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
157
Yingdi Yua90ba482014-12-09 10:54:43 -0800158 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800159 sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0);
160
161 int result = sqlite3_step(statement);
162 if (result == SQLITE_ROW)
163 doesTableExist = true;
164 sqlite3_finalize(statement);
165
166 return doesTableExist;
167}
168
169bool
170SecPublicInfoSqlite3::initializeTable(const string& tableName, const string& initCommand)
171{
172 // Create the table if it does not exist
173 if (!doesTableExist(tableName)) {
174 char* errorMessage = 0;
175 int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage);
176
177 if (result != SQLITE_OK && errorMessage != 0) {
178 sqlite3_free(errorMessage);
179 return false;
180 }
181 }
182
183 return true;
184}
185
186void
187SecPublicInfoSqlite3::deleteTable(const string& tableName)
188{
189 string query = "DROP TABLE IF EXISTS " + tableName;
190
Yingdi Yua90ba482014-12-09 10:54:43 -0800191 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800192 sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0);
193
194 sqlite3_step(statement);
195 sqlite3_finalize(statement);
196}
197
198void
199SecPublicInfoSqlite3::setTpmLocator(const string& tpmLocator)
200{
201 string currentTpm;
202 try {
203 currentTpm = getTpmLocator();
204 }
205 catch (SecPublicInfo::Error&) {
206 setTpmLocatorInternal(tpmLocator, false); // set tpmInfo without resetting
207 return;
208 }
209
210 if (currentTpm == tpmLocator)
211 return; // if the same, nothing will be changed
212
213 setTpmLocatorInternal(tpmLocator, true); // set tpmInfo and reset pib
214}
215
216string
217SecPublicInfoSqlite3::getTpmLocator()
218{
Yingdi Yua90ba482014-12-09 10:54:43 -0800219 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800220 sqlite3_prepare_v2(m_database, "SELECT tpm_locator FROM TpmInfo", -1, &statement, 0);
221
222 int res = sqlite3_step(statement);
223
224 if (res == SQLITE_ROW) {
225 string tpmLocator = sqlite3_column_string(statement, 0);
226 sqlite3_finalize(statement);
227 return tpmLocator;
228 }
229 else {
230 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700231 BOOST_THROW_EXCEPTION(SecPublicInfo::Error("TPM info does not exist"));
Yingdi Yu41546342014-11-30 23:37:53 -0800232 }
233}
234
235void
236SecPublicInfoSqlite3::setTpmLocatorInternal(const string& tpmLocator, bool needReset)
237{
Yingdi Yua90ba482014-12-09 10:54:43 -0800238 sqlite3_stmt* statement = nullptr;
Yingdi Yu41546342014-11-30 23:37:53 -0800239
240 if (needReset) {
241 deleteTable("Identity");
242 deleteTable("Key");
243 deleteTable("Certificate");
244
245 initializeTable("Identity", INIT_ID_TABLE);
246 initializeTable("Key", INIT_KEY_TABLE);
247 initializeTable("Certificate", INIT_CERT_TABLE);
248
249 sqlite3_prepare_v2(m_database, "UPDATE TpmInfo SET tpm_locator = ?",
250 -1, &statement, 0);
251 sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
252 }
253 else {
254 // no reset implies there is no tpmLocator record, insert one
255 sqlite3_prepare_v2(m_database, "INSERT INTO TpmInfo (tpm_locator) VALUES (?)",
256 -1, &statement, 0);
257 sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
258 }
259
260 sqlite3_step(statement);
261 sqlite3_finalize(statement);
262}
263
264std::string
265SecPublicInfoSqlite3::getPibLocator()
266{
267 return string("pib-sqlite3:").append(m_location);
268}
269
270bool
Yingdi Yu87581582014-01-14 14:28:39 -0800271SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272{
273 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700274
Yingdi Yua90ba482014-12-09 10:54:43 -0800275 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700276 sqlite3_prepare_v2(m_database,
277 "SELECT count(*) FROM Identity WHERE identity_name=?",
278 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700279
Yingdi Yu41546342014-11-30 23:37:53 -0800280 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700281 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700282
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700283 if (res == SQLITE_ROW) {
284 int countAll = sqlite3_column_int(statement, 0);
285 if (countAll > 0)
286 result = true;
287 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700289 sqlite3_finalize(statement);
290
291 return result;
292}
293
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700294void
Yingdi Yu87581582014-01-14 14:28:39 -0800295SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700296{
Yingdi Yu05842f22014-04-15 19:21:56 -0700297 if (doesIdentityExist(identityName))
298 return;
299
Yingdi Yua90ba482014-12-09 10:54:43 -0800300 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700301
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700302 sqlite3_prepare_v2(m_database,
303 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
304 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700305
Yingdi Yu41546342014-11-30 23:37:53 -0800306 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700307
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800308 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700309
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700310 sqlite3_finalize(statement);
311}
312
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700313bool
Yingdi Yu87581582014-01-14 14:28:39 -0800314SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700315{
316 //TODO:
317 return false;
318}
319
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700320bool
Yingdi Yu87581582014-01-14 14:28:39 -0800321SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700322{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700323 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700324 BOOST_THROW_EXCEPTION(Error("Incorrect key name " + keyName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800325
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700326 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800327 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700328
Yingdi Yua90ba482014-12-09 10:54:43 -0800329 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700330 sqlite3_prepare_v2(m_database,
331 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
332 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700333
Yingdi Yu41546342014-11-30 23:37:53 -0800334 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
335 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700336
337 int res = sqlite3_step(statement);
338
339 bool keyIdExist = false;
340 if (res == SQLITE_ROW) {
341 int countAll = sqlite3_column_int(statement, 0);
342 if (countAll > 0)
343 keyIdExist = true;
344 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700345
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346 sqlite3_finalize(statement);
347
348 return keyIdExist;
349}
350
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700351void
Yingdi Yu40b53092014-06-17 17:10:02 -0700352SecPublicInfoSqlite3::addKey(const Name& keyName,
353 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700354{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700355 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800356 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800357
Yingdi Yu05842f22014-04-15 19:21:56 -0700358 if (doesPublicKeyExist(keyName))
359 return;
360
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700361 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800362 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700363
Yingdi Yu2e57a582014-02-20 23:34:43 -0800364 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700365
Yingdi Yua90ba482014-12-09 10:54:43 -0800366 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700367 sqlite3_prepare_v2(m_database,
368 "INSERT OR REPLACE INTO Key \
369 (identity_name, key_identifier, key_type, public_key) \
370 values (?, ?, ?, ?)",
371 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700372
Yingdi Yu41546342014-11-30 23:37:53 -0800373 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
374 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700375 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700376 sqlite3_bind_blob(statement, 4,
377 publicKeyDer.get().buf(),
378 publicKeyDer.get().size(),
379 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700380
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800381 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700382
383 sqlite3_finalize(statement);
384}
385
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800387SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700388{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700389 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700390 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey Empty keyName"));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700391
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700392 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800393 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700394
Yingdi Yua90ba482014-12-09 10:54:43 -0800395 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700396 sqlite3_prepare_v2(m_database,
397 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
398 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399
Yingdi Yu41546342014-11-30 23:37:53 -0800400 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
401 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402
403 int res = sqlite3_step(statement);
404
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405 shared_ptr<PublicKey> result;
Yingdi Yua90ba482014-12-09 10:54:43 -0800406 if (res == SQLITE_ROW) {
407 result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
408 sqlite3_column_bytes(statement, 0));
409 sqlite3_finalize(statement);
410 return result;
411 }
412 else {
413 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700414 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey public key does not exist"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800415 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700416}
417
Yingdi Yu40b53092014-06-17 17:10:02 -0700418KeyType
419SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
420{
421 if (keyName.empty())
422 return KEY_TYPE_NULL;
423
424 string keyId = keyName.get(-1).toUri();
425 Name identityName = keyName.getPrefix(-1);
426
Yingdi Yua90ba482014-12-09 10:54:43 -0800427 sqlite3_stmt* statement = nullptr;
Yingdi Yu40b53092014-06-17 17:10:02 -0700428 sqlite3_prepare_v2(m_database,
429 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
430 -1, &statement, 0);
431
Yingdi Yu41546342014-11-30 23:37:53 -0800432 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
433 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700434
435 int res = sqlite3_step(statement);
436
Yingdi Yua90ba482014-12-09 10:54:43 -0800437 if (res == SQLITE_ROW) {
438 int typeValue = sqlite3_column_int(statement, 0);
439 sqlite3_finalize(statement);
440 return static_cast<KeyType>(typeValue);
441 }
442 else {
443 sqlite3_finalize(statement);
444 return KEY_TYPE_NULL;
445 }
Yingdi Yu40b53092014-06-17 17:10:02 -0700446}
447
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700448bool
Yingdi Yu87581582014-01-14 14:28:39 -0800449SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700450{
Yingdi Yua90ba482014-12-09 10:54:43 -0800451 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700452 sqlite3_prepare_v2(m_database,
453 "SELECT count(*) FROM Certificate WHERE cert_name=?",
454 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700455
Yingdi Yu41546342014-11-30 23:37:53 -0800456 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700457
458 int res = sqlite3_step(statement);
459
460 bool certExist = false;
461 if (res == SQLITE_ROW) {
462 int countAll = sqlite3_column_int(statement, 0);
463 if (countAll > 0)
464 certExist = true;
465 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700466
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700467 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469 return certExist;
470}
471
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700472void
Yingdi Yu87581582014-01-14 14:28:39 -0800473SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700474{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700475 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700476 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700477 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700478 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700479
Yingdi Yu40b53092014-06-17 17:10:02 -0700480 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700481
Yingdi Yu05842f22014-04-15 19:21:56 -0700482 if (doesCertificateExist(certificateName))
483 return;
484
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700485 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800486 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700487
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700488 // Insert the certificate
Yingdi Yua90ba482014-12-09 10:54:43 -0800489 sqlite3_stmt* statement = nullptr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700490 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700491 "INSERT OR REPLACE INTO Certificate \
492 (cert_name, cert_issuer, identity_name, key_identifier, \
493 not_before, not_after, certificate_data) \
494 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700495 -1, &statement, 0);
496
Yingdi Yu41546342014-11-30 23:37:53 -0800497 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700498
Yingdi Yua90ba482014-12-09 10:54:43 -0800499 try {
500 // this will throw an exception if the signature is not the standard one
501 // or there is no key locator present
502 std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
503 sqlite3_bind_string(statement, 2, signerName, SQLITE_TRANSIENT);
504 }
505 catch (tlv::Error&) {
506 return;
507 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700508
Yingdi Yu41546342014-11-30 23:37:53 -0800509 sqlite3_bind_string(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
510 sqlite3_bind_string(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700511
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700512 sqlite3_bind_int64(statement, 5,
Yingdi Yua90ba482014-12-09 10:54:43 -0800513 static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotBefore()).count()));
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700514 sqlite3_bind_int64(statement, 6,
Yingdi Yua90ba482014-12-09 10:54:43 -0800515 static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700516
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700517 sqlite3_bind_blob(statement, 7,
518 certificate.wireEncode().wire(),
519 certificate.wireEncode().size(),
520 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700521
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800522 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700523
524 sqlite3_finalize(statement);
525}
526
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700527shared_ptr<IdentityCertificate>
528SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700529{
Yingdi Yua90ba482014-12-09 10:54:43 -0800530 sqlite3_stmt* statement = nullptr;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700531
532 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800533 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
534 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700535
Yingdi Yu41546342014-11-30 23:37:53 -0800536 sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700537
Yingdi Yu4270f202014-01-28 14:19:16 -0800538 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700539
Yingdi Yua90ba482014-12-09 10:54:43 -0800540 if (res == SQLITE_ROW) {
541 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
542 try {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700543 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700544 sqlite3_column_bytes(statement, 0)));
Yingdi Yu4270f202014-01-28 14:19:16 -0800545 }
Yingdi Yua90ba482014-12-09 10:54:43 -0800546 catch (tlv::Error&) {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800547 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700548 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate cannot be "
549 "decoded"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800550 }
Yingdi Yua90ba482014-12-09 10:54:43 -0800551
552 sqlite3_finalize(statement);
553 return certificate;
554 }
555 else {
556 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700557 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate does not "
558 "exist"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800559 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700560}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700561
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700562
563Name
Yingdi Yu87581582014-01-14 14:28:39 -0800564SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700565{
Yingdi Yua90ba482014-12-09 10:54:43 -0800566 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700567 sqlite3_prepare_v2(m_database,
568 "SELECT identity_name FROM Identity WHERE default_identity=1",
569 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700570
571 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700572
Yingdi Yua90ba482014-12-09 10:54:43 -0800573 if (res == SQLITE_ROW) {
574 Name identity(sqlite3_column_string(statement, 0));
575 sqlite3_finalize(statement);
576 return identity;
577 }
578 else {
579 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700580 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800581 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700582}
583
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584void
Yingdi Yu87581582014-01-14 14:28:39 -0800585SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700586{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800587 addIdentity(identityName);
588
Yingdi Yua90ba482014-12-09 10:54:43 -0800589 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700590
591 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700592 sqlite3_prepare_v2(m_database,
593 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
594 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700595
596 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800597 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700598
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700599 sqlite3_finalize(statement);
600
601 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700602 sqlite3_prepare_v2(m_database,
603 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
604 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700605
Yingdi Yu41546342014-11-30 23:37:53 -0800606 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700607
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700608 sqlite3_step(statement);
609
610 sqlite3_finalize(statement);
611}
612
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700613Name
Yingdi Yu87581582014-01-14 14:28:39 -0800614SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700615{
Yingdi Yua90ba482014-12-09 10:54:43 -0800616 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700617 sqlite3_prepare_v2(m_database,
618 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
619 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700620
Yingdi Yu41546342014-11-30 23:37:53 -0800621 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800622
623 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800624
Yingdi Yua90ba482014-12-09 10:54:43 -0800625 if (res == SQLITE_ROW) {
626 Name keyName = identityName;
627 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
628 sqlite3_column_bytes(statement, 0)));
629 sqlite3_finalize(statement);
630 return keyName;
631 }
632 else {
633 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700634 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not "
635 "found"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800636 }
Yingdi Yu87581582014-01-14 14:28:39 -0800637}
638
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700639void
Yingdi Yu87581582014-01-14 14:28:39 -0800640SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
641{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700642 if (!doesPublicKeyExist(keyName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700643 BOOST_THROW_EXCEPTION(Error("Key does not exist:" + keyName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800644
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700645 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800646 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700647
Yingdi Yua90ba482014-12-09 10:54:43 -0800648 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700649
650 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700651 sqlite3_prepare_v2(m_database,
652 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
653 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700654
Yingdi Yu41546342014-11-30 23:37:53 -0800655 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700656
657 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700658 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700659
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700660 sqlite3_finalize(statement);
661
662 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700663 sqlite3_prepare_v2(m_database,
664 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
665 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700666
Yingdi Yu41546342014-11-30 23:37:53 -0800667 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
668 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700669
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700670 sqlite3_step(statement);
671
672 sqlite3_finalize(statement);
673}
674
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700675Name
Yingdi Yu87581582014-01-14 14:28:39 -0800676SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700677{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700678 if (keyName.empty())
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700679 BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"));
Yingdi Yu88663af2014-01-15 15:21:38 -0800680
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700681 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800682 Name identityName = keyName.getPrefix(-1);
683
Yingdi Yua90ba482014-12-09 10:54:43 -0800684 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700685 sqlite3_prepare_v2(m_database,
686 "SELECT cert_name FROM Certificate \
687 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
688 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800689
Yingdi Yu41546342014-11-30 23:37:53 -0800690 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
691 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800692
693 int res = sqlite3_step(statement);
694
Yingdi Yua90ba482014-12-09 10:54:43 -0800695 if (res == SQLITE_ROW) {
696 Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
697 sqlite3_column_bytes(statement, 0)));
698 sqlite3_finalize(statement);
699 return certName;
700 }
701 else {
702 sqlite3_finalize(statement);
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700703 BOOST_THROW_EXCEPTION(Error("certificate not found"));
Yingdi Yua90ba482014-12-09 10:54:43 -0800704 }
Yingdi Yu87581582014-01-14 14:28:39 -0800705}
706
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707void
Yingdi Yu87581582014-01-14 14:28:39 -0800708SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
709{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700710 if (!doesCertificateExist(certificateName))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700711 BOOST_THROW_EXCEPTION(Error("certificate does not exist:" + certificateName.toUri()));
Yingdi Yu88663af2014-01-15 15:21:38 -0800712
Yingdi Yu2e57a582014-02-20 23:34:43 -0800713 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700714 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800715 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700716
Yingdi Yua90ba482014-12-09 10:54:43 -0800717 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700718
719 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700720 sqlite3_prepare_v2(m_database,
721 "UPDATE Certificate SET default_cert=0 \
722 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
723 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700724
Yingdi Yu41546342014-11-30 23:37:53 -0800725 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
726 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700727
728 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700729 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700730
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700731 sqlite3_finalize(statement);
732
733 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700734 sqlite3_prepare_v2(m_database,
735 "UPDATE Certificate SET default_cert=1 \
736 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
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);
741 sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700742
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700743 sqlite3_step(statement);
744
745 sqlite3_finalize(statement);
746}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800747
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800748void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700749SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800750{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700751 sqlite3_stmt* stmt;
752 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700753 sqlite3_prepare_v2(m_database,
754 "SELECT identity_name FROM Identity WHERE default_identity=1",
755 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800756 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700757 sqlite3_prepare_v2(m_database,
758 "SELECT identity_name FROM Identity WHERE default_identity=0",
759 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800760
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700761 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800762 nameList.push_back(Name(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700763 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700764
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700765 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700766}
767
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800768void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700769SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800770{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700771 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800772
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700773 if (isDefault)
774 sqlite3_prepare_v2(m_database,
775 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
776 -1, &stmt, 0);
777 else
778 sqlite3_prepare_v2(m_database,
779 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
780 -1, &stmt, 0);
781
Yingdi Yua90ba482014-12-09 10:54:43 -0800782 while (sqlite3_step(stmt) == SQLITE_ROW) {
783 Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
784 sqlite3_column_bytes(stmt, 0)));
785 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
786 sqlite3_column_bytes(stmt, 1)));
787 nameList.push_back(keyName);
788 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700789 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800790}
791
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800792void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700793SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
794 vector<Name>& nameList,
795 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800796{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700797 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700798
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700799 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700800 sqlite3_prepare_v2(m_database,
801 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
802 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800803 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700804 sqlite3_prepare_v2(m_database,
805 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
806 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700807
Yingdi Yu41546342014-11-30 23:37:53 -0800808 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800809
Yingdi Yua90ba482014-12-09 10:54:43 -0800810 while (sqlite3_step(stmt) == SQLITE_ROW) {
811 Name keyName(identity);
812 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
813 sqlite3_column_bytes(stmt, 0)));
814 nameList.push_back(keyName);
815 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700816 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800817}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700818
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800819void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700820SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800821{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700822 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700823
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700824 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700825 sqlite3_prepare_v2(m_database,
826 "SELECT cert_name FROM Certificate WHERE default_cert=1",
827 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800828 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700829 sqlite3_prepare_v2(m_database,
830 "SELECT cert_name FROM Certificate WHERE default_cert=0",
831 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800832
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700833 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800834 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700835 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800836
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700837 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800838}
839
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800840void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700841SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
842 vector<Name>& nameList,
843 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800844{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700845 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800846 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800847
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700848 sqlite3_stmt* stmt;
849 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700850 sqlite3_prepare_v2(m_database,
851 "SELECT cert_name FROM Certificate \
852 WHERE default_cert=1 and identity_name=? and key_identifier=?",
853 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800854 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700855 sqlite3_prepare_v2(m_database,
856 "SELECT cert_name FROM Certificate \
857 WHERE default_cert=0 and identity_name=? and key_identifier=?",
858 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800859
Yingdi Yu87581582014-01-14 14:28:39 -0800860 Name identity = keyName.getPrefix(-1);
Yingdi Yu41546342014-11-30 23:37:53 -0800861 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700862
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700863 std::string baseKeyName = keyName.get(-1).toUri();
Yingdi Yu41546342014-11-30 23:37:53 -0800864 sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800865
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700866 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800867 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700868 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800869
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700870 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800871}
872
873void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700874SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800875{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700876 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800877 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700878
879 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800880 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800881 sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800882 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700883 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800884}
885
886void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700887SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800888{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700889 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800890 return;
891
892 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700893 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700895 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700896 sqlite3_prepare_v2(m_database,
897 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
898 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800899 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
900 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800901 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700902 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800903
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700904 sqlite3_prepare_v2(m_database,
905 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
906 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800907 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
908 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800909 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700910 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800911}
912
913void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700914SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800915{
916 string identity = identityName.toUri();
917
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700918 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800919 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800920 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800921 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700922 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800923
924 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800925 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800926 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700927 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800928
929 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800930 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800931 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700932 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800933}
934
Yingdi Yu41546342014-11-30 23:37:53 -0800935std::string
936SecPublicInfoSqlite3::getScheme()
937{
938 return SCHEME;
939}
940
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800941} // namespace ndn