blob: 99a3d4a3729984f3c57324edabba2723426fb88d [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yu41546342014-11-30 23:37:53 -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)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800133 throw 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);
231 throw SecPublicInfo::Error("TPM info does not exist");
232 }
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())
Yingdi Yu88663af2014-01-15 15:21:38 -0800324 throw Error("Incorrect key name " + keyName.toUri());
325
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())
Yingdi Yua90ba482014-12-09 10:54:43 -0800390 throw 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);
414 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
415 }
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);
Yingdi Yua90ba482014-12-09 10:54:43 -0800548 throw Error("SecPublicInfoSqlite3::getCertificate certificate cannot be decoded");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800549 }
Yingdi Yua90ba482014-12-09 10:54:43 -0800550
551 sqlite3_finalize(statement);
552 return certificate;
553 }
554 else {
555 sqlite3_finalize(statement);
556 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
557 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700558}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700559
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700560
561Name
Yingdi Yu87581582014-01-14 14:28:39 -0800562SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700563{
Yingdi Yua90ba482014-12-09 10:54:43 -0800564 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700565 sqlite3_prepare_v2(m_database,
566 "SELECT identity_name FROM Identity WHERE default_identity=1",
567 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700568
569 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700570
Yingdi Yua90ba482014-12-09 10:54:43 -0800571 if (res == SQLITE_ROW) {
572 Name identity(sqlite3_column_string(statement, 0));
573 sqlite3_finalize(statement);
574 return identity;
575 }
576 else {
577 sqlite3_finalize(statement);
578 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
579 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700580}
581
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700582void
Yingdi Yu87581582014-01-14 14:28:39 -0800583SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700584{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800585 addIdentity(identityName);
586
Yingdi Yua90ba482014-12-09 10:54:43 -0800587 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700588
589 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700590 sqlite3_prepare_v2(m_database,
591 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
592 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700593
594 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800595 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700596
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700597 sqlite3_finalize(statement);
598
599 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700600 sqlite3_prepare_v2(m_database,
601 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
602 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700603
Yingdi Yu41546342014-11-30 23:37:53 -0800604 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700605
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700606 sqlite3_step(statement);
607
608 sqlite3_finalize(statement);
609}
610
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700611Name
Yingdi Yu87581582014-01-14 14:28:39 -0800612SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700613{
Yingdi Yua90ba482014-12-09 10:54:43 -0800614 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700615 sqlite3_prepare_v2(m_database,
616 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
617 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700618
Yingdi Yu41546342014-11-30 23:37:53 -0800619 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800620
621 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800622
Yingdi Yua90ba482014-12-09 10:54:43 -0800623 if (res == SQLITE_ROW) {
624 Name keyName = identityName;
625 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
626 sqlite3_column_bytes(statement, 0)));
627 sqlite3_finalize(statement);
628 return keyName;
629 }
630 else {
631 sqlite3_finalize(statement);
632 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
633 }
Yingdi Yu87581582014-01-14 14:28:39 -0800634}
635
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700636void
Yingdi Yu87581582014-01-14 14:28:39 -0800637SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
638{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700639 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700640 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800641
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700642 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800643 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700644
Yingdi Yua90ba482014-12-09 10:54:43 -0800645 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700646
647 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700648 sqlite3_prepare_v2(m_database,
649 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
650 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700651
Yingdi Yu41546342014-11-30 23:37:53 -0800652 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700653
654 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700655 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700657 sqlite3_finalize(statement);
658
659 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700660 sqlite3_prepare_v2(m_database,
661 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
662 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700663
Yingdi Yu41546342014-11-30 23:37:53 -0800664 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
665 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700666
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700667 sqlite3_step(statement);
668
669 sqlite3_finalize(statement);
670}
671
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700672Name
Yingdi Yu87581582014-01-14 14:28:39 -0800673SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700674{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700675 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800676 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800677
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700678 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800679 Name identityName = keyName.getPrefix(-1);
680
Yingdi Yua90ba482014-12-09 10:54:43 -0800681 sqlite3_stmt* statement = nullptr;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700682 sqlite3_prepare_v2(m_database,
683 "SELECT cert_name FROM Certificate \
684 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
685 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800686
Yingdi Yu41546342014-11-30 23:37:53 -0800687 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
688 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu87581582014-01-14 14:28:39 -0800689
690 int res = sqlite3_step(statement);
691
Yingdi Yua90ba482014-12-09 10:54:43 -0800692 if (res == SQLITE_ROW) {
693 Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
694 sqlite3_column_bytes(statement, 0)));
695 sqlite3_finalize(statement);
696 return certName;
697 }
698 else {
699 sqlite3_finalize(statement);
700 throw Error("certificate not found");
701 }
Yingdi Yu87581582014-01-14 14:28:39 -0800702}
703
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700704void
Yingdi Yu87581582014-01-14 14:28:39 -0800705SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
706{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700708 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800709
Yingdi Yu2e57a582014-02-20 23:34:43 -0800710 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700711 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800712 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700713
Yingdi Yua90ba482014-12-09 10:54:43 -0800714 sqlite3_stmt* statement = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700715
716 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700717 sqlite3_prepare_v2(m_database,
718 "UPDATE Certificate SET default_cert=0 \
719 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
720 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700721
Yingdi Yu41546342014-11-30 23:37:53 -0800722 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
723 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700724
725 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700726 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700727
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700728 sqlite3_finalize(statement);
729
730 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700731 sqlite3_prepare_v2(m_database,
732 "UPDATE Certificate SET default_cert=1 \
733 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
734 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700735
Yingdi Yu41546342014-11-30 23:37:53 -0800736 sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
737 sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
738 sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700739
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700740 sqlite3_step(statement);
741
742 sqlite3_finalize(statement);
743}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800744
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800745void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700746SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800747{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700748 sqlite3_stmt* stmt;
749 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700750 sqlite3_prepare_v2(m_database,
751 "SELECT identity_name FROM Identity WHERE default_identity=1",
752 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800753 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700754 sqlite3_prepare_v2(m_database,
755 "SELECT identity_name FROM Identity WHERE default_identity=0",
756 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800757
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700758 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800759 nameList.push_back(Name(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700760 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700761
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700762 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700763}
764
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800765void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700766SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800767{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700768 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800769
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700770 if (isDefault)
771 sqlite3_prepare_v2(m_database,
772 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
773 -1, &stmt, 0);
774 else
775 sqlite3_prepare_v2(m_database,
776 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
777 -1, &stmt, 0);
778
Yingdi Yua90ba482014-12-09 10:54:43 -0800779 while (sqlite3_step(stmt) == SQLITE_ROW) {
780 Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
781 sqlite3_column_bytes(stmt, 0)));
782 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
783 sqlite3_column_bytes(stmt, 1)));
784 nameList.push_back(keyName);
785 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700786 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800787}
788
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800789void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700790SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
791 vector<Name>& nameList,
792 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800793{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700794 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700795
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700796 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700797 sqlite3_prepare_v2(m_database,
798 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
799 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800800 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700801 sqlite3_prepare_v2(m_database,
802 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
803 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700804
Yingdi Yu41546342014-11-30 23:37:53 -0800805 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800806
Yingdi Yua90ba482014-12-09 10:54:43 -0800807 while (sqlite3_step(stmt) == SQLITE_ROW) {
808 Name keyName(identity);
809 keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
810 sqlite3_column_bytes(stmt, 0)));
811 nameList.push_back(keyName);
812 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700813 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800814}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700815
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800816void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700817SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800818{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700819 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700820
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700821 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700822 sqlite3_prepare_v2(m_database,
823 "SELECT cert_name FROM Certificate WHERE default_cert=1",
824 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800825 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700826 sqlite3_prepare_v2(m_database,
827 "SELECT cert_name FROM Certificate WHERE default_cert=0",
828 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800829
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700830 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800831 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700832 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800833
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700834 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800835}
836
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800837void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700838SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
839 vector<Name>& nameList,
840 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800841{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700842 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800843 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800844
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700845 sqlite3_stmt* stmt;
846 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700847 sqlite3_prepare_v2(m_database,
848 "SELECT cert_name FROM Certificate \
849 WHERE default_cert=1 and identity_name=? and key_identifier=?",
850 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800851 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700852 sqlite3_prepare_v2(m_database,
853 "SELECT cert_name FROM Certificate \
854 WHERE default_cert=0 and identity_name=? and key_identifier=?",
855 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800856
Yingdi Yu87581582014-01-14 14:28:39 -0800857 Name identity = keyName.getPrefix(-1);
Yingdi Yu41546342014-11-30 23:37:53 -0800858 sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700859
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700860 std::string baseKeyName = keyName.get(-1).toUri();
Yingdi Yu41546342014-11-30 23:37:53 -0800861 sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800862
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700863 while (sqlite3_step(stmt) == SQLITE_ROW)
Yingdi Yua90ba482014-12-09 10:54:43 -0800864 nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700865 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800866
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700867 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800868}
869
870void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700871SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800872{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700873 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800874 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700875
876 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800877 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800878 sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800879 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700880 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800881}
882
883void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700884SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800885{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700886 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800887 return;
888
889 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700890 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800891
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700892 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700893 sqlite3_prepare_v2(m_database,
894 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
895 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800896 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
897 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800898 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700899 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800900
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700901 sqlite3_prepare_v2(m_database,
902 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
903 -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800904 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
905 sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800906 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700907 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800908}
909
910void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700911SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800912{
913 string identity = identityName.toUri();
914
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700915 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800916 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800917 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800918 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700919 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800920
921 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800922 sqlite3_bind_string(stmt, 1, identity, 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 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu41546342014-11-30 23:37:53 -0800927 sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800928 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700929 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800930}
931
Yingdi Yu41546342014-11-30 23:37:53 -0800932std::string
933SecPublicInfoSqlite3::getScheme()
934{
935 return SCHEME;
936}
937
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800938} // namespace ndn