blob: 946eb56ea6748180efa1ff0ce73d459af0e89456 [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
Jeff Thompson7ca11f22013-10-04 19:01:30 -070043static const string INIT_ID_TABLE = "\
44CREATE TABLE IF NOT EXISTS \n \
45 Identity( \n \
46 identity_name BLOB NOT NULL, \n \
47 default_identity INTEGER DEFAULT 0, \n \
48 \
49 PRIMARY KEY (identity_name) \n \
50 ); \n \
51 \
52CREATE INDEX identity_index ON Identity(identity_name); \n \
53";
54
55static const string INIT_KEY_TABLE = "\
56CREATE TABLE IF NOT EXISTS \n \
57 Key( \n \
58 identity_name BLOB NOT NULL, \n \
59 key_identifier BLOB NOT NULL, \n \
60 key_type INTEGER, \n \
61 public_key BLOB, \n \
62 default_key INTEGER DEFAULT 0, \n \
63 active INTEGER DEFAULT 0, \n \
64 \
65 PRIMARY KEY (identity_name, key_identifier) \n \
66 ); \n \
67 \
68CREATE INDEX key_index ON Key(identity_name); \n \
69";
70
71static const string INIT_CERT_TABLE = "\
72CREATE TABLE IF NOT EXISTS \n \
73 Certificate( \n \
74 cert_name BLOB NOT NULL, \n \
75 cert_issuer BLOB NOT NULL, \n \
76 identity_name BLOB NOT NULL, \n \
77 key_identifier BLOB NOT NULL, \n \
78 not_before TIMESTAMP, \n \
79 not_after TIMESTAMP, \n \
80 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070081 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070082 default_cert INTEGER DEFAULT 0, \n \
83 \
84 PRIMARY KEY (cert_name) \n \
85 ); \n \
86 \
87CREATE INDEX cert_index ON Certificate(cert_name); \n \
88CREATE INDEX subject ON Certificate(identity_name); \n \
89";
90
91/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 * A utility function to call the normal sqlite3_bind_text where the value and length are
93 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070094 */
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -070095static int
96sqlite3_bind_text(sqlite3_stmt* statement,
97 int index,
98 const string& value,
99 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700100{
101 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
102}
103
Yingdi Yu87581582014-01-14 14:28:39 -0800104SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Junxiao Shi98acbb02014-12-02 11:00:42 -0700105 : m_database(nullptr)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700106{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700107 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700108 boost::filesystem::create_directories(identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700109
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800110 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
111 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
112 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700113#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800114 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800115#else
116 0
117#endif
118 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700119 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800120 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121
Junxiao Shi98acbb02014-12-02 11:00:42 -0700122 BOOST_ASSERT(m_database != nullptr);
123
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700124 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700126 sqlite3_prepare_v2(m_database,
127 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
128 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700129 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700130
131 bool idTableExists = false;
132 if (res == SQLITE_ROW)
133 idTableExists = true;
134
135 sqlite3_finalize(statement);
136
137 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800139 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700141 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700142 sqlite3_free(errorMessage);
143 }
144 }
145
146 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700147 sqlite3_prepare_v2(m_database,
148 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
149 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700150 res = sqlite3_step(statement);
151
152 bool keyTableExists = false;
153 if (res == SQLITE_ROW)
154 keyTableExists = true;
155
156 sqlite3_finalize(statement);
157
158 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800160 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700161
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700162 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700163 sqlite3_free(errorMessage);
164 }
165 }
166
167 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700168 sqlite3_prepare_v2(m_database,
169 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
170 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700171 res = sqlite3_step(statement);
172
173 bool idCertificateTableExists = false;
174 if (res == SQLITE_ROW)
175 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700176
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700177 sqlite3_finalize(statement);
178
179 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800181 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700182
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700183 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700184 sqlite3_free(errorMessage);
185 }
186 }
187}
188
Yingdi Yu87581582014-01-14 14:28:39 -0800189SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700190{
Junxiao Shi98acbb02014-12-02 11:00:42 -0700191 sqlite3_close(m_database);
192 m_database = nullptr;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700193}
194
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700195bool
Yingdi Yu87581582014-01-14 14:28:39 -0800196SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700197{
198 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199
200 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700201 sqlite3_prepare_v2(m_database,
202 "SELECT count(*) FROM Identity WHERE identity_name=?",
203 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700204
205 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
206 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700208 if (res == SQLITE_ROW) {
209 int countAll = sqlite3_column_int(statement, 0);
210 if (countAll > 0)
211 result = true;
212 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700214 sqlite3_finalize(statement);
215
216 return result;
217}
218
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219void
Yingdi Yu87581582014-01-14 14:28:39 -0800220SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700221{
Yingdi Yu05842f22014-04-15 19:21:56 -0700222 if (doesIdentityExist(identityName))
223 return;
224
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700225 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700226
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700227 sqlite3_prepare_v2(m_database,
228 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
229 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700230
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700231 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800233 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700234
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700235 sqlite3_finalize(statement);
236}
237
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700238bool
Yingdi Yu87581582014-01-14 14:28:39 -0800239SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700240{
241 //TODO:
242 return false;
243}
244
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700245bool
Yingdi Yu87581582014-01-14 14:28:39 -0800246SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700247{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700248 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800249 throw Error("Incorrect key name " + keyName.toUri());
250
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700251 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800252 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700253
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700254 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700255 sqlite3_prepare_v2(m_database,
256 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
257 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700258
259 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
260 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
261
262 int res = sqlite3_step(statement);
263
264 bool keyIdExist = false;
265 if (res == SQLITE_ROW) {
266 int countAll = sqlite3_column_int(statement, 0);
267 if (countAll > 0)
268 keyIdExist = true;
269 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700270
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700271 sqlite3_finalize(statement);
272
273 return keyIdExist;
274}
275
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700276void
Yingdi Yu40b53092014-06-17 17:10:02 -0700277SecPublicInfoSqlite3::addKey(const Name& keyName,
278 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700279{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700280 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800281 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800282
Yingdi Yu05842f22014-04-15 19:21:56 -0700283 if (doesPublicKeyExist(keyName))
284 return;
285
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700286 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800287 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700288
Yingdi Yu2e57a582014-02-20 23:34:43 -0800289 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700290
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700291 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700292 sqlite3_prepare_v2(m_database,
293 "INSERT OR REPLACE INTO Key \
294 (identity_name, key_identifier, key_type, public_key) \
295 values (?, ?, ?, ?)",
296 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700297
298 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
299 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700300 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700301 sqlite3_bind_blob(statement, 4,
302 publicKeyDer.get().buf(),
303 publicKeyDer.get().size(),
304 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700305
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800306 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700307
308 sqlite3_finalize(statement);
309}
310
Yingdi Yu2e57a582014-02-20 23:34:43 -0800311shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800312SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700313{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700314 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800315 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800316 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
317 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700318
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700319 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800320 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700321
322 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700323 sqlite3_prepare_v2(m_database,
324 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
325 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700326
327 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
328 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
329
330 int res = sqlite3_step(statement);
331
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700333 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800334 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700335 result =
336 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
337 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800338 sqlite3_finalize(statement);
339 return result;
340 }
341 else
342 {
343 sqlite3_finalize(statement);
344 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
345 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346}
347
Yingdi Yu40b53092014-06-17 17:10:02 -0700348KeyType
349SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
350{
351 if (keyName.empty())
352 return KEY_TYPE_NULL;
353
354 string keyId = keyName.get(-1).toUri();
355 Name identityName = keyName.getPrefix(-1);
356
357 sqlite3_stmt* statement;
358 sqlite3_prepare_v2(m_database,
359 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
360 -1, &statement, 0);
361
362 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
363 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
364
365 int res = sqlite3_step(statement);
366
367 if (res == SQLITE_ROW)
368 {
369 int typeValue = sqlite3_column_int(statement, 0);
370 sqlite3_finalize(statement);
371 return static_cast<KeyType>(typeValue);
372 }
373 else
374 {
375 sqlite3_finalize(statement);
376 return KEY_TYPE_NULL;
377 }
378
379}
380
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700381bool
Yingdi Yu87581582014-01-14 14:28:39 -0800382SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700384 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700385 sqlite3_prepare_v2(m_database,
386 "SELECT count(*) FROM Certificate WHERE cert_name=?",
387 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700388
389 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
390
391 int res = sqlite3_step(statement);
392
393 bool certExist = false;
394 if (res == SQLITE_ROW) {
395 int countAll = sqlite3_column_int(statement, 0);
396 if (countAll > 0)
397 certExist = true;
398 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700399
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700400 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700401
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402 return certExist;
403}
404
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405// void
406// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
407// {
408// std::string certificateName = certificate.getName().toUri();
409// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800410
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700411// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700414// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800415// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700416
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700417// sqlite3_stmt* statement;
418// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700419// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
420// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800421// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700422
Yingdi Yu2e57a582014-02-20 23:34:43 -0800423// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700424
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425// try
426// {
427// SignatureSha256WithRsa signature(certificate.getSignature());
428// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429
Yingdi Yu2e57a582014-02-20 23:34:43 -0800430// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
431// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700432// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800434// return;
435// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700436// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800437// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800438// return;
439// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800440
Yingdi Yu2e57a582014-02-20 23:34:43 -0800441// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
442// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700443
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700444// // Convert from time::milliseconds to time::seconds since 1/1/1970.
445// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
446// time::toUnixTimestamp(certificate.getNotBefore()).count()));
447// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
448// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700449
Yingdi Yu2e57a582014-02-20 23:34:43 -0800450// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700451
Yingdi Yu2e57a582014-02-20 23:34:43 -0800452// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700453
Yingdi Yu2e57a582014-02-20 23:34:43 -0800454// sqlite3_finalize(statement);
455// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700456
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700457void
Yingdi Yu87581582014-01-14 14:28:39 -0800458SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700459{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700461 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700462 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700463 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700464
Yingdi Yu40b53092014-06-17 17:10:02 -0700465 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700466
Yingdi Yu05842f22014-04-15 19:21:56 -0700467 if (doesCertificateExist(certificateName))
468 return;
469
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700470 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800471 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700472
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700473 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700474 sqlite3_stmt* statement;
475 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700476 "INSERT OR REPLACE INTO Certificate \
477 (cert_name, cert_issuer, identity_name, key_identifier, \
478 not_before, not_after, certificate_data) \
479 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700480 -1, &statement, 0);
481
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700482 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
483
Yingdi Yu2e57a582014-02-20 23:34:43 -0800484 try
485 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700486 // this will throw an exception if the signature is not the standard one
487 // or there is no key locator present
Yingdi Yu4a557052014-07-09 16:40:37 -0700488 std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -0700489 sqlite3_bind_text(statement, 2, signerName, SQLITE_TRANSIENT);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800490 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600491 catch (tlv::Error& e)
Yingdi Yu4a557052014-07-09 16:40:37 -0700492 {
493 return;
494 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700495
496 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800497 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700498
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700499 sqlite3_bind_int64(statement, 5,
500 static_cast<sqlite3_int64>(
501 time::toUnixTimestamp(certificate.getNotBefore()).count()));
502 sqlite3_bind_int64(statement, 6,
503 static_cast<sqlite3_int64>(
504 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700505
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700506 sqlite3_bind_blob(statement, 7,
507 certificate.wireEncode().wire(),
508 certificate.wireEncode().size(),
509 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700510
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800511 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512
513 sqlite3_finalize(statement);
514}
515
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700516shared_ptr<IdentityCertificate>
517SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700518{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700519 sqlite3_stmt* statement;
520
521 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800522 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
523 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700524
Yingdi Yu4270f202014-01-28 14:19:16 -0800525 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700526
Yingdi Yu4270f202014-01-28 14:19:16 -0800527 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700528
Yingdi Yu4270f202014-01-28 14:19:16 -0800529 if (res == SQLITE_ROW)
530 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800531 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700532 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700533 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800534 sqlite3_finalize(statement);
535 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800536 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800537 else
538 {
539 sqlite3_finalize(statement);
540 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
541 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700542}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700543
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700544
545Name
Yingdi Yu87581582014-01-14 14:28:39 -0800546SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700547{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700548 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700549 sqlite3_prepare_v2(m_database,
550 "SELECT identity_name FROM Identity WHERE default_identity=1",
551 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700552
553 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700554
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800556 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700557 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
558 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800559 sqlite3_finalize(statement);
560 return identity;
561 }
562 else
563 {
564 sqlite3_finalize(statement);
565 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
566 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700567}
568
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700569void
Yingdi Yu87581582014-01-14 14:28:39 -0800570SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700571{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800572 addIdentity(identityName);
573
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700574 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700575
576 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700577 sqlite3_prepare_v2(m_database,
578 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
579 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700580
581 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700582 {
583 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700585 sqlite3_finalize(statement);
586
587 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700588 sqlite3_prepare_v2(m_database,
589 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
590 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700591
592 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700593
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700594 sqlite3_step(statement);
595
596 sqlite3_finalize(statement);
597}
598
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700599Name
Yingdi Yu87581582014-01-14 14:28:39 -0800600SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700601{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700602 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700603 sqlite3_prepare_v2(m_database,
604 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
605 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700606
Yingdi Yu87581582014-01-14 14:28:39 -0800607 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
608
609 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800610
611 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800612 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700613 Name keyName = identityName;
614 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
615 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800616 sqlite3_finalize(statement);
617 return keyName;
618 }
619 else
620 {
621 sqlite3_finalize(statement);
622 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
623 }
Yingdi Yu87581582014-01-14 14:28:39 -0800624}
625
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700626void
Yingdi Yu87581582014-01-14 14:28:39 -0800627SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
628{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700629 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700630 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800631
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700632 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800633 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700634
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700635 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700636
637 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700638 sqlite3_prepare_v2(m_database,
639 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
640 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700641
642 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
643
644 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700645 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700646
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700647 sqlite3_finalize(statement);
648
649 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700650 sqlite3_prepare_v2(m_database,
651 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
652 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700653
654 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
655 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700657 sqlite3_step(statement);
658
659 sqlite3_finalize(statement);
660}
661
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662Name
Yingdi Yu87581582014-01-14 14:28:39 -0800663SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700664{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700665 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800666 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800667
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700668 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800669 Name identityName = keyName.getPrefix(-1);
670
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700671 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700672 sqlite3_prepare_v2(m_database,
673 "SELECT cert_name FROM Certificate \
674 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
675 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800676
677 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
678 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
679
680 int res = sqlite3_step(statement);
681
Yingdi Yu87581582014-01-14 14:28:39 -0800682 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800683 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700684 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
685 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800686 sqlite3_finalize(statement);
687 return certName;
688 }
689 else
690 {
691 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700692 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800693 }
Yingdi Yu87581582014-01-14 14:28:39 -0800694}
695
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696void
Yingdi Yu87581582014-01-14 14:28:39 -0800697SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
698{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700699 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700700 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800701
Yingdi Yu2e57a582014-02-20 23:34:43 -0800702 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700703 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800704 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700705
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700706 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700707
708 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700709 sqlite3_prepare_v2(m_database,
710 "UPDATE Certificate SET default_cert=0 \
711 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
712 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700713
714 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
715 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
716
717 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700718 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700719
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700720 sqlite3_finalize(statement);
721
722 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700723 sqlite3_prepare_v2(m_database,
724 "UPDATE Certificate SET default_cert=1 \
725 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
726 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700727
728 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
729 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
730 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700731
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700732 sqlite3_step(statement);
733
734 sqlite3_finalize(statement);
735}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800736
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800737void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800739{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700740 sqlite3_stmt* stmt;
741 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700742 sqlite3_prepare_v2(m_database,
743 "SELECT identity_name FROM Identity WHERE default_identity=1",
744 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800745 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700746 sqlite3_prepare_v2(m_database,
747 "SELECT identity_name FROM Identity WHERE default_identity=0",
748 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800749
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700750 while (sqlite3_step(stmt) == SQLITE_ROW)
751 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
752 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700753
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700754 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700755}
756
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800757void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700758SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800759{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700760 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800761
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700762 if (isDefault)
763 sqlite3_prepare_v2(m_database,
764 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
765 -1, &stmt, 0);
766 else
767 sqlite3_prepare_v2(m_database,
768 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
769 -1, &stmt, 0);
770
771 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800772 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700773 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
774 sqlite3_column_bytes(stmt, 0)));
775 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
776 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800777 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700778 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700779 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800780}
781
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800782void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700783SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
784 vector<Name>& nameList,
785 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800786{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700787 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700788
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700789 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700790 sqlite3_prepare_v2(m_database,
791 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
792 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800793 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700794 sqlite3_prepare_v2(m_database,
795 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
796 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700797
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700798 sqlite3_bind_text(stmt, 1,
799 identity.toUri().c_str(),
800 identity.toUri().size(),
801 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800802
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700803 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800804 {
805 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700806 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
807 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800808 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700809 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700810 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800811}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700812
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800813void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700814SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800815{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700816 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700817
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700818 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700819 sqlite3_prepare_v2(m_database,
820 "SELECT cert_name FROM Certificate WHERE default_cert=1",
821 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800822 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700823 sqlite3_prepare_v2(m_database,
824 "SELECT cert_name FROM Certificate WHERE default_cert=0",
825 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800826
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700827 while (sqlite3_step(stmt) == SQLITE_ROW)
828 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
829 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800830
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700831 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800832}
833
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800834void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700835SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
836 vector<Name>& nameList,
837 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800838{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700839 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800840 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800841
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700842 sqlite3_stmt* stmt;
843 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700844 sqlite3_prepare_v2(m_database,
845 "SELECT cert_name FROM Certificate \
846 WHERE default_cert=1 and identity_name=? and key_identifier=?",
847 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800848 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700849 sqlite3_prepare_v2(m_database,
850 "SELECT cert_name FROM Certificate \
851 WHERE default_cert=0 and identity_name=? and key_identifier=?",
852 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800853
Yingdi Yu87581582014-01-14 14:28:39 -0800854 Name identity = keyName.getPrefix(-1);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700855 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700856
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700857 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800858 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
859
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700860 while (sqlite3_step(stmt) == SQLITE_ROW)
861 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
862 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800863
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700864 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800865}
866
867void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700868SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800869{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700870 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800871 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700872
873 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800874 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700875 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800876 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700877 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800878}
879
880void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700881SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800882{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700883 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800884 return;
885
886 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700887 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800888
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700889 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700890 sqlite3_prepare_v2(m_database,
891 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
892 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700893 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
895 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700896 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800897
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700898 sqlite3_prepare_v2(m_database,
899 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
900 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700901 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800902 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
903 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700904 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800905}
906
907void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700908SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800909{
910 string identity = identityName.toUri();
911
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700912 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800913 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700914 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800915 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700916 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800917
918 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700919 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800920 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700921 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800922
923 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700924 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800925 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700926 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800927}
928
929} // namespace ndn