blob: 04938b4bb671e8a48c0cf3fbc530258dcf96cc62 [file] [log] [blame]
Jeff Thompson7ca11f22013-10-04 19:01:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyev766cea72014-04-24 19:16:42 -07003 * Copyright (c) 2013-2014 Regents of the University of California.
Jeff Thompson7ca11f22013-10-04 19:01:30 -07004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson22285ec2013-10-22 17:43:02 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -07006 * See COPYING for copyright and distribution information.
7 */
8
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009#include "common.hpp"
10
11#include "sec-public-info-sqlite3.hpp"
12#include "identity-certificate.hpp"
13#include "signature-sha256-with-rsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080014#include "../data.hpp"
15
Yingdi Yu874678f2014-01-22 19:30:34 -080016#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070017#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070018#include <stdlib.h>
19#include <sstream>
20#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080021#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080022
Jeff Thompson7ca11f22013-10-04 19:01:30 -070023using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070024
Yingdi Yufc40d872014-02-18 12:56:04 -080025namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070026
27static const string INIT_ID_TABLE = "\
28CREATE TABLE IF NOT EXISTS \n \
29 Identity( \n \
30 identity_name BLOB NOT NULL, \n \
31 default_identity INTEGER DEFAULT 0, \n \
32 \
33 PRIMARY KEY (identity_name) \n \
34 ); \n \
35 \
36CREATE INDEX identity_index ON Identity(identity_name); \n \
37";
38
39static const string INIT_KEY_TABLE = "\
40CREATE TABLE IF NOT EXISTS \n \
41 Key( \n \
42 identity_name BLOB NOT NULL, \n \
43 key_identifier BLOB NOT NULL, \n \
44 key_type INTEGER, \n \
45 public_key BLOB, \n \
46 default_key INTEGER DEFAULT 0, \n \
47 active INTEGER DEFAULT 0, \n \
48 \
49 PRIMARY KEY (identity_name, key_identifier) \n \
50 ); \n \
51 \
52CREATE INDEX key_index ON Key(identity_name); \n \
53";
54
55static const string INIT_CERT_TABLE = "\
56CREATE TABLE IF NOT EXISTS \n \
57 Certificate( \n \
58 cert_name BLOB NOT NULL, \n \
59 cert_issuer BLOB NOT NULL, \n \
60 identity_name BLOB NOT NULL, \n \
61 key_identifier BLOB NOT NULL, \n \
62 not_before TIMESTAMP, \n \
63 not_after TIMESTAMP, \n \
64 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070065 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070066 default_cert INTEGER DEFAULT 0, \n \
67 \
68 PRIMARY KEY (cert_name) \n \
69 ); \n \
70 \
71CREATE INDEX cert_index ON Certificate(cert_name); \n \
72CREATE INDEX subject ON Certificate(identity_name); \n \
73";
74
75/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070076 * A utility function to call the normal sqlite3_bind_text where the value and length are
77 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070078 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070079static int sqlite3_bind_text(sqlite3_stmt* statement,
80 int index,
81 const string& value,
82 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -070083{
84 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
85}
86
Yingdi Yu87581582014-01-14 14:28:39 -080087SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070088{
Yingdi Yu37e317f2014-03-19 12:16:23 -070089 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -080090 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070091
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080092 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
93 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
94 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -070095#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -080096 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080097#else
98 0
99#endif
100 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700101 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800102 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700104 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700106 sqlite3_prepare_v2(m_database,
107 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
108 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700109 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700110
111 bool idTableExists = false;
112 if (res == SQLITE_ROW)
113 idTableExists = true;
114
115 sqlite3_finalize(statement);
116
117 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800119 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700121 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700122 sqlite3_free(errorMessage);
123 }
124 }
125
126 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700127 sqlite3_prepare_v2(m_database,
128 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
129 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700130 res = sqlite3_step(statement);
131
132 bool keyTableExists = false;
133 if (res == SQLITE_ROW)
134 keyTableExists = true;
135
136 sqlite3_finalize(statement);
137
138 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800140 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700142 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700143 sqlite3_free(errorMessage);
144 }
145 }
146
147 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700148 sqlite3_prepare_v2(m_database,
149 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
150 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700151 res = sqlite3_step(statement);
152
153 bool idCertificateTableExists = false;
154 if (res == SQLITE_ROW)
155 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700157 sqlite3_finalize(statement);
158
159 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800161 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700162
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700163 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700164 sqlite3_free(errorMessage);
165 }
166 }
167}
168
Yingdi Yu87581582014-01-14 14:28:39 -0800169SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700170{
171}
172
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700173bool
Yingdi Yu87581582014-01-14 14:28:39 -0800174SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700175{
176 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177
178 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700179 sqlite3_prepare_v2(m_database,
180 "SELECT count(*) FROM Identity WHERE identity_name=?",
181 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700182
183 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
184 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700185
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700186 if (res == SQLITE_ROW) {
187 int countAll = sqlite3_column_int(statement, 0);
188 if (countAll > 0)
189 result = true;
190 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700192 sqlite3_finalize(statement);
193
194 return result;
195}
196
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197void
Yingdi Yu87581582014-01-14 14:28:39 -0800198SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700199{
Yingdi Yu05842f22014-04-15 19:21:56 -0700200 if (doesIdentityExist(identityName))
201 return;
202
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700203 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700204
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700205 sqlite3_prepare_v2(m_database,
206 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
207 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700209 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700210
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800211 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700212
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700213 sqlite3_finalize(statement);
214}
215
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216bool
Yingdi Yu87581582014-01-14 14:28:39 -0800217SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700218{
219 //TODO:
220 return false;
221}
222
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223bool
Yingdi Yu87581582014-01-14 14:28:39 -0800224SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800227 throw Error("Incorrect key name " + keyName.toUri());
228
Yingdi Yu87581582014-01-14 14:28:39 -0800229 string keyId = keyName.get(-1).toEscapedString();
230 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700231
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700233 sqlite3_prepare_v2(m_database,
234 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
235 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700236
237 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
238 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
239
240 int res = sqlite3_step(statement);
241
242 bool keyIdExist = false;
243 if (res == SQLITE_ROW) {
244 int countAll = sqlite3_column_int(statement, 0);
245 if (countAll > 0)
246 keyIdExist = true;
247 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700248
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700249 sqlite3_finalize(statement);
250
251 return keyIdExist;
252}
253
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700254void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700255SecPublicInfoSqlite3::addPublicKey(const Name& keyName,
256 KeyType keyType,
257 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700258{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700259 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800260 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800261
Yingdi Yu05842f22014-04-15 19:21:56 -0700262 if (doesPublicKeyExist(keyName))
263 return;
264
Yingdi Yu87581582014-01-14 14:28:39 -0800265 string keyId = keyName.get(-1).toEscapedString();
266 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700267
Yingdi Yu2e57a582014-02-20 23:34:43 -0800268 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700269
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700270 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700271 sqlite3_prepare_v2(m_database,
272 "INSERT OR REPLACE INTO Key \
273 (identity_name, key_identifier, key_type, public_key) \
274 values (?, ?, ?, ?)",
275 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700276
277 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
278 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
279 sqlite3_bind_int(statement, 3, (int)keyType);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700280 sqlite3_bind_blob(statement, 4,
281 publicKeyDer.get().buf(),
282 publicKeyDer.get().size(),
283 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800285 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700286
287 sqlite3_finalize(statement);
288}
289
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800291SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700292{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700293 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800294 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800295 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
296 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700297
Yingdi Yu87581582014-01-14 14:28:39 -0800298 string keyId = keyName.get(-1).toEscapedString();
299 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700300
301 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700302 sqlite3_prepare_v2(m_database,
303 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
304 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700305
306 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
307 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
308
309 int res = sqlite3_step(statement);
310
Yingdi Yu2e57a582014-02-20 23:34:43 -0800311 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700312 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800313 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700314 result =
315 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
316 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800317 sqlite3_finalize(statement);
318 return result;
319 }
320 else
321 {
322 sqlite3_finalize(statement);
323 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
324 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700325}
326
327bool
Yingdi Yu87581582014-01-14 14:28:39 -0800328SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700329{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700330 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700331 sqlite3_prepare_v2(m_database,
332 "SELECT count(*) FROM Certificate WHERE cert_name=?",
333 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700334
335 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
336
337 int res = sqlite3_step(statement);
338
339 bool certExist = false;
340 if (res == SQLITE_ROW) {
341 int countAll = sqlite3_column_int(statement, 0);
342 if (countAll > 0)
343 certExist = true;
344 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700345
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700347
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700348 return certExist;
349}
350
Yingdi Yu2e57a582014-02-20 23:34:43 -0800351// void
352// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
353// {
354// std::string certificateName = certificate.getName().toUri();
355// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800356
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700357// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700359
Yingdi Yu2e57a582014-02-20 23:34:43 -0800360// std::string keyId = keyName.get(-1).toEscapedString();
361// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700362
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700363// sqlite3_stmt* statement;
364// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700365// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
366// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800367// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700368
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700370
Yingdi Yu2e57a582014-02-20 23:34:43 -0800371// try
372// {
373// SignatureSha256WithRsa signature(certificate.getSignature());
374// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
377// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700378// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800379// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800380// return;
381// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700382// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800384// return;
385// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800386
Yingdi Yu2e57a582014-02-20 23:34:43 -0800387// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
388// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700389
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700390// // Convert from time::milliseconds to time::seconds since 1/1/1970.
391// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
392// time::toUnixTimestamp(certificate.getNotBefore()).count()));
393// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
394// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395
Yingdi Yu2e57a582014-02-20 23:34:43 -0800396// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700397
Yingdi Yu2e57a582014-02-20 23:34:43 -0800398// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399
Yingdi Yu2e57a582014-02-20 23:34:43 -0800400// sqlite3_finalize(statement);
401// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700403void
Yingdi Yu87581582014-01-14 14:28:39 -0800404SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700405{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700406 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700407 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700408 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700409 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700410
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700411 //HACK!!! Assume the key type is RSA, we should check more.
412 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Yingdi Yu05842f22014-04-15 19:21:56 -0700414 if (doesCertificateExist(certificateName))
415 return;
416
Yingdi Yu87581582014-01-14 14:28:39 -0800417 string keyId = keyName.get(-1).toEscapedString();
418 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700419
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700420 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700421 sqlite3_stmt* statement;
422 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700423 "INSERT OR REPLACE INTO Certificate \
424 (cert_name, cert_issuer, identity_name, key_identifier, \
425 not_before, not_after, certificate_data) \
426 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700427 -1, &statement, 0);
428
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
430
Yingdi Yu2e57a582014-02-20 23:34:43 -0800431 try
432 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700433 // this will throw an exception if the signature is not the standard one
434 // or there is no key locator present
Yingdi Yu2e57a582014-02-20 23:34:43 -0800435 SignatureSha256WithRsa signature(certificate.getSignature());
436 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800437
Yingdi Yu2e57a582014-02-20 23:34:43 -0800438 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
439 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700440 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800441 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800442 return;
443 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700444
445 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800446 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700447
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700448 sqlite3_bind_int64(statement, 5,
449 static_cast<sqlite3_int64>(
450 time::toUnixTimestamp(certificate.getNotBefore()).count()));
451 sqlite3_bind_int64(statement, 6,
452 static_cast<sqlite3_int64>(
453 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700454
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700455 sqlite3_bind_blob(statement, 7,
456 certificate.wireEncode().wire(),
457 certificate.wireEncode().size(),
458 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700459
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800460 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700461
462 sqlite3_finalize(statement);
463}
464
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700465shared_ptr<IdentityCertificate>
466SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700467{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468 sqlite3_stmt* statement;
469
470 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800471 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
472 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700473
Yingdi Yu4270f202014-01-28 14:19:16 -0800474 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700475
Yingdi Yu4270f202014-01-28 14:19:16 -0800476 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700477
Yingdi Yu4270f202014-01-28 14:19:16 -0800478 if (res == SQLITE_ROW)
479 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800480 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700481 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700482 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800483 sqlite3_finalize(statement);
484 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800485 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800486 else
487 {
488 sqlite3_finalize(statement);
489 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
490 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700491}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700492
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700493
494Name
Yingdi Yu87581582014-01-14 14:28:39 -0800495SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700496{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700497 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700498 sqlite3_prepare_v2(m_database,
499 "SELECT identity_name FROM Identity WHERE default_identity=1",
500 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700501
502 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700503
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700504 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800505 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700506 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
507 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800508 sqlite3_finalize(statement);
509 return identity;
510 }
511 else
512 {
513 sqlite3_finalize(statement);
514 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
515 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700516}
517
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700518void
Yingdi Yu87581582014-01-14 14:28:39 -0800519SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700520{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800521 addIdentity(identityName);
522
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700523 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700524
525 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700526 sqlite3_prepare_v2(m_database,
527 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
528 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700529
530 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700531 {
532 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700533
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700534 sqlite3_finalize(statement);
535
536 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700537 sqlite3_prepare_v2(m_database,
538 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
539 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700540
541 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700542
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700543 sqlite3_step(statement);
544
545 sqlite3_finalize(statement);
546}
547
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700548Name
Yingdi Yu87581582014-01-14 14:28:39 -0800549SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700550{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700551 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700552 sqlite3_prepare_v2(m_database,
553 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
554 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555
Yingdi Yu87581582014-01-14 14:28:39 -0800556 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
557
558 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800559
560 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800561 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700562 Name keyName = identityName;
563 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
564 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800565 sqlite3_finalize(statement);
566 return keyName;
567 }
568 else
569 {
570 sqlite3_finalize(statement);
571 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
572 }
Yingdi Yu87581582014-01-14 14:28:39 -0800573}
574
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700575void
Yingdi Yu87581582014-01-14 14:28:39 -0800576SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
577{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700578 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700579 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800580
Yingdi Yu87581582014-01-14 14:28:39 -0800581 string keyId = keyName.get(-1).toEscapedString();
582 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700583
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700585
586 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700587 sqlite3_prepare_v2(m_database,
588 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
589 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700590
591 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
592
593 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700594 {
595 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700596
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700597 sqlite3_finalize(statement);
598
599 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700600 sqlite3_prepare_v2(m_database,
601 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
602 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700603
604 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
605 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700606
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700607 sqlite3_step(statement);
608
609 sqlite3_finalize(statement);
610}
611
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700612Name
Yingdi Yu87581582014-01-14 14:28:39 -0800613SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700614{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700615 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800616 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800617
Yingdi Yu87581582014-01-14 14:28:39 -0800618 string keyId = keyName.get(-1).toEscapedString();
619 Name identityName = keyName.getPrefix(-1);
620
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700621 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700622 sqlite3_prepare_v2(m_database,
623 "SELECT cert_name FROM Certificate \
624 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
625 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800626
627 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
628 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
629
630 int res = sqlite3_step(statement);
631
Yingdi Yu87581582014-01-14 14:28:39 -0800632 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800633 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700634 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
635 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800636 sqlite3_finalize(statement);
637 return certName;
638 }
639 else
640 {
641 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700642 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800643 }
Yingdi Yu87581582014-01-14 14:28:39 -0800644}
645
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700646void
Yingdi Yu87581582014-01-14 14:28:39 -0800647SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
648{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700649 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700650 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800651
Yingdi Yu2e57a582014-02-20 23:34:43 -0800652 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu87581582014-01-14 14:28:39 -0800653 string keyId = keyName.get(-1).toEscapedString();
654 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700655
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700657
658 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700659 sqlite3_prepare_v2(m_database,
660 "UPDATE Certificate SET default_cert=0 \
661 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
662 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700663
664 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
665 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
666
667 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700668 {
669 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700670
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700671 sqlite3_finalize(statement);
672
673 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700674 sqlite3_prepare_v2(m_database,
675 "UPDATE Certificate SET default_cert=1 \
676 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
677 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700678
679 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
680 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
681 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700682
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700683 sqlite3_step(statement);
684
685 sqlite3_finalize(statement);
686}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800687
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800688void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700689SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800690{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700691 sqlite3_stmt* stmt;
692 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700693 sqlite3_prepare_v2(m_database,
694 "SELECT identity_name FROM Identity WHERE default_identity=1",
695 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800696 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700697 sqlite3_prepare_v2(m_database,
698 "SELECT identity_name FROM Identity WHERE default_identity=0",
699 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800700
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700701 while (sqlite3_step(stmt) == SQLITE_ROW)
702 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
703 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700704
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700705 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700706}
707
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800708void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700709SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800710{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700711 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800712
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700713 if (isDefault)
714 sqlite3_prepare_v2(m_database,
715 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
716 -1, &stmt, 0);
717 else
718 sqlite3_prepare_v2(m_database,
719 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
720 -1, &stmt, 0);
721
722 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800723 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700724 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
725 sqlite3_column_bytes(stmt, 0)));
726 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
727 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800728 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700729 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700730 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800731}
732
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800733void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700734SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
735 vector<Name>& nameList,
736 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800737{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700739
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700740 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700741 sqlite3_prepare_v2(m_database,
742 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
743 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800744 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700745 sqlite3_prepare_v2(m_database,
746 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
747 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700748
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700749 sqlite3_bind_text(stmt, 1,
750 identity.toUri().c_str(),
751 identity.toUri().size(),
752 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800753
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700754 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800755 {
756 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700757 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
758 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800759 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700760 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700761 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800762}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700763
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800764void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700765SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800766{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700767 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700768
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700769 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700770 sqlite3_prepare_v2(m_database,
771 "SELECT cert_name FROM Certificate WHERE default_cert=1",
772 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800773 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700774 sqlite3_prepare_v2(m_database,
775 "SELECT cert_name FROM Certificate WHERE default_cert=0",
776 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800777
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700778 while (sqlite3_step(stmt) == SQLITE_ROW)
779 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
780 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800781
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700782 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800783}
784
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800785void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700786SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
787 vector<Name>& nameList,
788 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800789{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700790 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800791 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800792
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700793 sqlite3_stmt* stmt;
794 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700795 sqlite3_prepare_v2(m_database,
796 "SELECT cert_name FROM Certificate \
797 WHERE default_cert=1 and identity_name=? and key_identifier=?",
798 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800799 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700800 sqlite3_prepare_v2(m_database,
801 "SELECT cert_name FROM Certificate \
802 WHERE default_cert=0 and identity_name=? and key_identifier=?",
803 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800804
Yingdi Yu87581582014-01-14 14:28:39 -0800805 Name identity = keyName.getPrefix(-1);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700806 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
807
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800808 std::string baseKeyName = keyName.get(-1).toEscapedString();
809 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
810
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700811 while (sqlite3_step(stmt) == SQLITE_ROW)
812 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
813 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800814
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700815 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800816}
817
818void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700819SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800820{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700821 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800822 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700823
824 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800825 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700826 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800827 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700828 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800829}
830
831void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700832SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800833{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700834 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800835 return;
836
837 string identity = keyName.getPrefix(-1).toUri();
838 string keyId = keyName.get(-1).toEscapedString();
839
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700840 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700841 sqlite3_prepare_v2(m_database,
842 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
843 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800844 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
845 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
846 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700847 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800848
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700849 sqlite3_prepare_v2(m_database,
850 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
851 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800852 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
853 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
854 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700855 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800856}
857
858void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700859SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800860{
861 string identity = identityName.toUri();
862
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700863 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800864 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
865 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
866 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700867 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800868
869 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
870 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
871 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700872 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800873
874 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
875 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
876 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700877 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800878}
879
880} // namespace ndn