blob: 8d7ccb2856bacfb7bc15d647ce1dfc0a537b97c8 [file] [log] [blame]
Jeff Thompson7ca11f22013-10-04 19:01:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @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"
14
15#include "../util/logging.hpp"
16#include "../util/time.hpp"
17
18#include "../data.hpp"
19
Yingdi Yu874678f2014-01-22 19:30:34 -080020#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070021#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070022#include <stdlib.h>
23#include <sstream>
24#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080025#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080026
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -080027
Jeff Thompson7ca11f22013-10-04 19:01:30 -070028
Yingdi Yu21157162014-02-28 13:02:34 -080029INIT_LOGGER("ndn.SecPublicInfoSqlite3");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070030
31using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070032
Yingdi Yufc40d872014-02-18 12:56:04 -080033namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070034
35static const string INIT_ID_TABLE = "\
36CREATE TABLE IF NOT EXISTS \n \
37 Identity( \n \
38 identity_name BLOB NOT NULL, \n \
39 default_identity INTEGER DEFAULT 0, \n \
40 \
41 PRIMARY KEY (identity_name) \n \
42 ); \n \
43 \
44CREATE INDEX identity_index ON Identity(identity_name); \n \
45";
46
47static const string INIT_KEY_TABLE = "\
48CREATE TABLE IF NOT EXISTS \n \
49 Key( \n \
50 identity_name BLOB NOT NULL, \n \
51 key_identifier BLOB NOT NULL, \n \
52 key_type INTEGER, \n \
53 public_key BLOB, \n \
54 default_key INTEGER DEFAULT 0, \n \
55 active INTEGER DEFAULT 0, \n \
56 \
57 PRIMARY KEY (identity_name, key_identifier) \n \
58 ); \n \
59 \
60CREATE INDEX key_index ON Key(identity_name); \n \
61";
62
63static const string INIT_CERT_TABLE = "\
64CREATE TABLE IF NOT EXISTS \n \
65 Certificate( \n \
66 cert_name BLOB NOT NULL, \n \
67 cert_issuer BLOB NOT NULL, \n \
68 identity_name BLOB NOT NULL, \n \
69 key_identifier BLOB NOT NULL, \n \
70 not_before TIMESTAMP, \n \
71 not_after TIMESTAMP, \n \
72 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070073 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070074 default_cert INTEGER DEFAULT 0, \n \
75 \
76 PRIMARY KEY (cert_name) \n \
77 ); \n \
78 \
79CREATE INDEX cert_index ON Certificate(cert_name); \n \
80CREATE INDEX subject ON Certificate(identity_name); \n \
81";
82
83/**
84 * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size().
85 */
86static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*))
87{
88 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
89}
90
Yingdi Yu87581582014-01-14 14:28:39 -080091SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070092{
Yingdi Yu37e317f2014-03-19 12:16:23 -070093 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -080094 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070095
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080096 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
97 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
98 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev44471462014-02-12 11:21:51 -080099#ifdef NDN_CPP_DISABLE_SQLITE3_FS_LOCKING
100 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800101#else
102 0
103#endif
104 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700105 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800106 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700108 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800110 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'", -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700111 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700112
113 bool idTableExists = false;
114 if (res == SQLITE_ROW)
115 idTableExists = true;
116
117 sqlite3_finalize(statement);
118
119 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800121 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700123 if (res != SQLITE_OK && errorMessage != 0) {
124 _LOG_TRACE("Init \"error\" in Identity: " << errorMessage);
125 sqlite3_free(errorMessage);
126 }
127 }
128
129 //Check if Key table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800130 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700131 res = sqlite3_step(statement);
132
133 bool keyTableExists = false;
134 if (res == SQLITE_ROW)
135 keyTableExists = true;
136
137 sqlite3_finalize(statement);
138
139 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800141 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700143 if (res != SQLITE_OK && errorMessage != 0) {
144 _LOG_TRACE("Init \"error\" in KEY: " << errorMessage);
145 sqlite3_free(errorMessage);
146 }
147 }
148
149 //Check if Certificate table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800150 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", -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) {
164 _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage);
165 sqlite3_free(errorMessage);
166 }
167 }
168}
169
Yingdi Yu87581582014-01-14 14:28:39 -0800170SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700171{
172}
173
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700174bool
Yingdi Yu87581582014-01-14 14:28:39 -0800175SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700176{
177 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178
179 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800180 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700181
182 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
183 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700184
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700185 if (res == SQLITE_ROW) {
186 int countAll = sqlite3_column_int(statement, 0);
187 if (countAll > 0)
188 result = true;
189 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700191 sqlite3_finalize(statement);
192
193 return result;
194}
195
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700196void
Yingdi Yu87581582014-01-14 14:28:39 -0800197SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700198{
Yingdi Yu05842f22014-04-15 19:21:56 -0700199 if (doesIdentityExist(identityName))
200 return;
201
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203
Yingdi Yu2e57a582014-02-20 23:34:43 -0800204 sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Identity (identity_name) values (?)", -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700205
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800208 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700209
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700210 sqlite3_finalize(statement);
211}
212
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213bool
Yingdi Yu87581582014-01-14 14:28:39 -0800214SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700215{
216 //TODO:
217 return false;
218}
219
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220bool
Yingdi Yu87581582014-01-14 14:28:39 -0800221SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800224 throw Error("Incorrect key name " + keyName.toUri());
225
Yingdi Yu87581582014-01-14 14:28:39 -0800226 string keyId = keyName.get(-1).toEscapedString();
227 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700228
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700229 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800230 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700231
232 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
233 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
234
235 int res = sqlite3_step(statement);
236
237 bool keyIdExist = false;
238 if (res == SQLITE_ROW) {
239 int countAll = sqlite3_column_int(statement, 0);
240 if (countAll > 0)
241 keyIdExist = true;
242 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700243
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700244 sqlite3_finalize(statement);
245
246 return keyIdExist;
247}
248
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700249void
Yingdi Yu87581582014-01-14 14:28:39 -0800250SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700251{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700252 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800253 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800254
Yingdi Yu05842f22014-04-15 19:21:56 -0700255 if (doesPublicKeyExist(keyName))
256 return;
257
Yingdi Yu87581582014-01-14 14:28:39 -0800258 string keyId = keyName.get(-1).toEscapedString();
259 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700260
Yingdi Yu2e57a582014-02-20 23:34:43 -0800261 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700262
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700263 sqlite3_stmt* statement;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800264 sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700265
266 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
267 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
268 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800269 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700270
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800271 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272
273 sqlite3_finalize(statement);
274}
275
Yingdi Yu2e57a582014-02-20 23:34:43 -0800276shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800277SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700278{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700279 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800280 {
281 _LOG_DEBUG("SecPublicInfoSqlite3::getPublicKey Empty keyName");
282 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
283 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284
Yingdi Yu87581582014-01-14 14:28:39 -0800285 string keyId = keyName.get(-1).toEscapedString();
286 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700287
288 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800289 sqlite3_prepare_v2(m_database, "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700290
291 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
292 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
293
294 int res = sqlite3_step(statement);
295
Yingdi Yu2e57a582014-02-20 23:34:43 -0800296 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700297 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800298 {
299 result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0));
300 sqlite3_finalize(statement);
301 return result;
302 }
303 else
304 {
305 sqlite3_finalize(statement);
306 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
307 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700308}
309
310bool
Yingdi Yu87581582014-01-14 14:28:39 -0800311SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700312{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700313 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800314 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700315
316 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
317
318 int res = sqlite3_step(statement);
319
320 bool certExist = false;
321 if (res == SQLITE_ROW) {
322 int countAll = sqlite3_column_int(statement, 0);
323 if (countAll > 0)
324 certExist = true;
325 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700326
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700327 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700328
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700329 return certExist;
330}
331
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332// void
333// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
334// {
335// std::string certificateName = certificate.getName().toUri();
336// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800337
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700338// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800339// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700340
Yingdi Yu2e57a582014-02-20 23:34:43 -0800341// std::string keyId = keyName.get(-1).toEscapedString();
342// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700343
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700344// sqlite3_stmt* statement;
345// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700346// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
347// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800348// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700349
Yingdi Yu2e57a582014-02-20 23:34:43 -0800350// _LOG_DEBUG("certName: " << certificateName);
351// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700352
Yingdi Yu2e57a582014-02-20 23:34:43 -0800353// try
354// {
355// SignatureSha256WithRsa signature(certificate.getSignature());
356// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700357
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
359// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700360// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800361// {
362// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
363// return;
364// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700365// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800366// {
367// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
368// return;
369// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800370
Yingdi Yu2e57a582014-02-20 23:34:43 -0800371// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
372// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700373
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700374// // Convert from time::milliseconds to time::seconds since 1/1/1970.
375// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
376// time::toUnixTimestamp(certificate.getNotBefore()).count()));
377// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
378// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700379
Yingdi Yu2e57a582014-02-20 23:34:43 -0800380// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700381
Yingdi Yu2e57a582014-02-20 23:34:43 -0800382// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383
Yingdi Yu2e57a582014-02-20 23:34:43 -0800384// sqlite3_finalize(statement);
385// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700386
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700387void
Yingdi Yu87581582014-01-14 14:28:39 -0800388SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700389{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700390 const Name& certificateName = certificate.getName();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700391 Name keyName =
Yingdi Yu2e57a582014-02-20 23:34:43 -0800392 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); // KeyName is from IdentityCertificate name, so should be qualified.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700393
Yingdi Yu2e57a582014-02-20 23:34:43 -0800394 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo()); //HACK!!! Assume the key type is RSA, we should check more.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395
Yingdi Yu05842f22014-04-15 19:21:56 -0700396 if (doesCertificateExist(certificateName))
397 return;
398
Yingdi Yu87581582014-01-14 14:28:39 -0800399 string keyId = keyName.get(-1).toEscapedString();
400 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700401
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700403 sqlite3_stmt* statement;
404 sqlite3_prepare_v2(m_database,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405 "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700406 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
407 -1, &statement, 0);
408
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800409 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700410 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
411
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412 try
413 {
414 // this will throw an exception if the signature is not the standard one or there is no key locator present
415 SignatureSha256WithRsa signature(certificate.getSignature());
416 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800417
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
419 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700420 catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800421 {
422 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
423 return;
424 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700425 catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800426 {
427 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
428 return;
429 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700430
431 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800432 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700433
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700434 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
435 time::toUnixTimestamp(certificate.getNotBefore()).count()));
436 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
437 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700438
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800439 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700440
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800441 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700442
443 sqlite3_finalize(statement);
444}
445
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700446shared_ptr<IdentityCertificate>
447SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700448{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700449 sqlite3_stmt* statement;
450
451 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800452 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
453 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700454
Yingdi Yu4270f202014-01-28 14:19:16 -0800455 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700456
Yingdi Yu4270f202014-01-28 14:19:16 -0800457 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700458
Yingdi Yu4270f202014-01-28 14:19:16 -0800459 if (res == SQLITE_ROW)
460 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800461 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700462 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0),
463 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800464 sqlite3_finalize(statement);
465 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800466 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800467 else
468 {
469 sqlite3_finalize(statement);
470 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
471 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700472}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700473
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700474
475Name
Yingdi Yu87581582014-01-14 14:28:39 -0800476SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700477{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700478 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800479 sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700480
481 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700482
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700483 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800484 {
485 Name identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
486 sqlite3_finalize(statement);
487 return identity;
488 }
489 else
490 {
491 sqlite3_finalize(statement);
492 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
493 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700494}
495
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700496void
Yingdi Yu87581582014-01-14 14:28:39 -0800497SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700498{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800499 addIdentity(identityName);
500
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700501 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700502
503 //Reset previous default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800504 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700505
506 while (sqlite3_step(statement) == SQLITE_ROW)
507 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700508
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700509 sqlite3_finalize(statement);
510
511 //Set current default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800512 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700513
514 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700515
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700516 sqlite3_step(statement);
517
518 sqlite3_finalize(statement);
519}
520
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700521Name
Yingdi Yu87581582014-01-14 14:28:39 -0800522SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700523{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700524 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800525 sqlite3_prepare_v2(m_database, "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700526
Yingdi Yu87581582014-01-14 14:28:39 -0800527 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
528
529 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800530
531 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800532 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700533 Name keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
Yingdi Yu2e57a582014-02-20 23:34:43 -0800534 sqlite3_column_bytes(statement, 0)));
535 sqlite3_finalize(statement);
536 return keyName;
537 }
538 else
539 {
540 sqlite3_finalize(statement);
541 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
542 }
Yingdi Yu87581582014-01-14 14:28:39 -0800543}
544
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545void
Yingdi Yu87581582014-01-14 14:28:39 -0800546SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
547{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700548 if (!doesPublicKeyExist(keyName))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800549 throw Error("SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800550
Yingdi Yu87581582014-01-14 14:28:39 -0800551 string keyId = keyName.get(-1).toEscapedString();
552 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700553
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700554 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555
556 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800557 sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700558
559 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
560
561 while (sqlite3_step(statement) == SQLITE_ROW)
562 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700563
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700564 sqlite3_finalize(statement);
565
566 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800567 sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700568
569 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
570 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700571
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700572 sqlite3_step(statement);
573
574 sqlite3_finalize(statement);
575}
576
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700577Name
Yingdi Yu87581582014-01-14 14:28:39 -0800578SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700579{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700580 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800581 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800582
Yingdi Yu87581582014-01-14 14:28:39 -0800583 string keyId = keyName.get(-1).toEscapedString();
584 Name identityName = keyName.getPrefix(-1);
585
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700586 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800587 sqlite3_prepare_v2(m_database, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800588
589 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
590 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
591
592 int res = sqlite3_step(statement);
593
Yingdi Yu87581582014-01-14 14:28:39 -0800594 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800595 {
596 Name certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
597 sqlite3_finalize(statement);
598 return certName;
599 }
600 else
601 {
602 sqlite3_finalize(statement);
603 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey certificate not found");
604 }
Yingdi Yu87581582014-01-14 14:28:39 -0800605}
606
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700607void
Yingdi Yu87581582014-01-14 14:28:39 -0800608SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
609{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700610 if (!doesCertificateExist(certificateName))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800611 throw Error("SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800612
Yingdi Yu2e57a582014-02-20 23:34:43 -0800613 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu87581582014-01-14 14:28:39 -0800614 string keyId = keyName.get(-1).toEscapedString();
615 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700616
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700617 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700618
619 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800620 sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700621
622 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
623 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
624
625 while (sqlite3_step(statement) == SQLITE_ROW)
626 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700627
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700628 sqlite3_finalize(statement);
629
630 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800631 sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700632
633 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
634 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
635 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700636
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700637 sqlite3_step(statement);
638
639 sqlite3_finalize(statement);
640}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800641
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800642void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700643SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800644{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700645 sqlite3_stmt* stmt;
646 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800647 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800648 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800649 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800650
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800651 while(sqlite3_step (stmt) == SQLITE_ROW)
652 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700653
654 sqlite3_finalize (stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700655}
656
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800657void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700658SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800659{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700660 sqlite3_stmt* stmt;
661 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800662 sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800663 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800664 sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800665
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800666 while(sqlite3_step (stmt) == SQLITE_ROW)
667 {
668 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
669 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
670 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700671 }
672 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800673}
674
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800675void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700676SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800677{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700678 sqlite3_stmt* stmt;
679 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800680 sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800681 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800682 sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700683
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800684 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
685
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800686 while(sqlite3_step (stmt) == SQLITE_ROW)
687 {
688 Name keyName(identity);
689 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
690 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700691 }
692 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800693}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700694
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800695void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800697{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700698 sqlite3_stmt* stmt;
699 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800700 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800701 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800702 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800703
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800704 while(sqlite3_step (stmt) == SQLITE_ROW)
705 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
706
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800708}
709
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800710void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700711SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800712{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700713 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800714 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800715
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700716 sqlite3_stmt* stmt;
717 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800718 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800719 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800720 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800721
Yingdi Yu87581582014-01-14 14:28:39 -0800722 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800723 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
724 std::string baseKeyName = keyName.get(-1).toEscapedString();
725 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
726
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800727 while(sqlite3_step (stmt) == SQLITE_ROW)
728 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
729
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700730 sqlite3_finalize (stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800731}
732
733void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700734SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800735{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700736 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800737 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738
739 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800740 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
741 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
742 sqlite3_step(stmt);
743 sqlite3_finalize (stmt);
744}
745
746void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700747SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800748{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700749 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800750 return;
751
752 string identity = keyName.getPrefix(-1).toUri();
753 string keyId = keyName.get(-1).toEscapedString();
754
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700755 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800756 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
757 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
758 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
759 sqlite3_step(stmt);
760 sqlite3_finalize (stmt);
761
762 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
763 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
764 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
765 sqlite3_step(stmt);
766 sqlite3_finalize (stmt);
767}
768
769void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700770SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800771{
772 string identity = identityName.toUri();
773
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700774 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800775 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
776 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
777 sqlite3_step(stmt);
778 sqlite3_finalize (stmt);
779
780 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
781 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
782 sqlite3_step(stmt);
783 sqlite3_finalize (stmt);
784
785 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
786 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
787 sqlite3_step(stmt);
788 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800789}
790
791} // namespace ndn