blob: b8b5af30e30346b37b7e919868b28a2f8bb8d323 [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{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700200
Yingdi Yu2e57a582014-02-20 23:34:43 -0800201 sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Identity (identity_name) values (?)", -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800205 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700206
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700207 sqlite3_finalize(statement);
208}
209
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700210bool
Yingdi Yu87581582014-01-14 14:28:39 -0800211SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700212{
213 //TODO:
214 return false;
215}
216
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700217bool
Yingdi Yu87581582014-01-14 14:28:39 -0800218SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700219{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800221 throw Error("Incorrect key name " + keyName.toUri());
222
Yingdi Yu87581582014-01-14 14:28:39 -0800223 string keyId = keyName.get(-1).toEscapedString();
224 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800227 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 -0700228
229 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
230 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
231
232 int res = sqlite3_step(statement);
233
234 bool keyIdExist = false;
235 if (res == SQLITE_ROW) {
236 int countAll = sqlite3_column_int(statement, 0);
237 if (countAll > 0)
238 keyIdExist = true;
239 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700240
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700241 sqlite3_finalize(statement);
242
243 return keyIdExist;
244}
245
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700246void
Yingdi Yu87581582014-01-14 14:28:39 -0800247SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700248{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700249 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800250 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800251
Yingdi Yu87581582014-01-14 14:28:39 -0800252 string keyId = keyName.get(-1).toEscapedString();
253 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700254
Yingdi Yu2e57a582014-02-20 23:34:43 -0800255 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700256
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700257 sqlite3_stmt* statement;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800258 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 -0700259
260 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
261 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
262 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800263 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700264
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800265 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700266
267 sqlite3_finalize(statement);
268}
269
Yingdi Yu2e57a582014-02-20 23:34:43 -0800270shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800271SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700273 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800274 {
275 _LOG_DEBUG("SecPublicInfoSqlite3::getPublicKey Empty keyName");
276 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
277 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700278
Yingdi Yu87581582014-01-14 14:28:39 -0800279 string keyId = keyName.get(-1).toEscapedString();
280 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700281
282 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800283 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 -0700284
285 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
286 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
287
288 int res = sqlite3_step(statement);
289
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700291 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800292 {
293 result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0));
294 sqlite3_finalize(statement);
295 return result;
296 }
297 else
298 {
299 sqlite3_finalize(statement);
300 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
301 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700302}
303
304bool
Yingdi Yu87581582014-01-14 14:28:39 -0800305SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700306{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700307 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800308 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700309
310 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
311
312 int res = sqlite3_step(statement);
313
314 bool certExist = false;
315 if (res == SQLITE_ROW) {
316 int countAll = sqlite3_column_int(statement, 0);
317 if (countAll > 0)
318 certExist = true;
319 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700320
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700321 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700322
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700323 return certExist;
324}
325
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326// void
327// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
328// {
329// std::string certificateName = certificate.getName().toUri();
330// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800331
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700332// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700334
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335// std::string keyId = keyName.get(-1).toEscapedString();
336// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700337
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700338// sqlite3_stmt* statement;
339// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700340// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
341// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800342// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700343
Yingdi Yu2e57a582014-02-20 23:34:43 -0800344// _LOG_DEBUG("certName: " << certificateName);
345// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346
Yingdi Yu2e57a582014-02-20 23:34:43 -0800347// try
348// {
349// SignatureSha256WithRsa signature(certificate.getSignature());
350// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700351
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
353// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700354// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800355// {
356// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
357// return;
358// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700359// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800360// {
361// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
362// return;
363// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800364
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
366// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700367
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700368// // Convert from time::milliseconds to time::seconds since 1/1/1970.
369// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
370// time::toUnixTimestamp(certificate.getNotBefore()).count()));
371// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
372// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700373
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700377
Yingdi Yu2e57a582014-02-20 23:34:43 -0800378// sqlite3_finalize(statement);
379// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700380
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700381void
Yingdi Yu87581582014-01-14 14:28:39 -0800382SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700384 const Name& certificateName = certificate.getName();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700385 Name keyName =
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); // KeyName is from IdentityCertificate name, so should be qualified.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700387
Yingdi Yu2e57a582014-02-20 23:34:43 -0800388 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 -0700389
Yingdi Yu87581582014-01-14 14:28:39 -0800390 string keyId = keyName.get(-1).toEscapedString();
391 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700392
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700393 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700394 sqlite3_stmt* statement;
395 sqlite3_prepare_v2(m_database,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800396 "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 -0700397 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
398 -1, &statement, 0);
399
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800400 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700401 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
402
Yingdi Yu2e57a582014-02-20 23:34:43 -0800403 try
404 {
405 // this will throw an exception if the signature is not the standard one or there is no key locator present
406 SignatureSha256WithRsa signature(certificate.getSignature());
407 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800408
Yingdi Yu2e57a582014-02-20 23:34:43 -0800409 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
410 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700411 catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412 {
413 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
414 return;
415 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700416 catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800417 {
418 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
419 return;
420 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700421
422 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800423 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700424
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700425 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
426 time::toUnixTimestamp(certificate.getNotBefore()).count()));
427 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
428 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800430 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700431
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800432 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700433
434 sqlite3_finalize(statement);
435}
436
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700437shared_ptr<IdentityCertificate>
438SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700439{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700440 sqlite3_stmt* statement;
441
442 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800443 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
444 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700445
Yingdi Yu4270f202014-01-28 14:19:16 -0800446 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700447
Yingdi Yu4270f202014-01-28 14:19:16 -0800448 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700449
Yingdi Yu4270f202014-01-28 14:19:16 -0800450 if (res == SQLITE_ROW)
451 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800452 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700453 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0),
454 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800455 sqlite3_finalize(statement);
456 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800457 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800458 else
459 {
460 sqlite3_finalize(statement);
461 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
462 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700463}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700464
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700465
466Name
Yingdi Yu87581582014-01-14 14:28:39 -0800467SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700468{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700469 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800470 sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700471
472 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700473
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700474 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800475 {
476 Name identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
477 sqlite3_finalize(statement);
478 return identity;
479 }
480 else
481 {
482 sqlite3_finalize(statement);
483 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
484 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700485}
486
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700487void
Yingdi Yu87581582014-01-14 14:28:39 -0800488SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700489{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800490 addIdentity(identityName);
491
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700492 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700493
494 //Reset previous default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800495 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 -0700496
497 while (sqlite3_step(statement) == SQLITE_ROW)
498 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700499
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700500 sqlite3_finalize(statement);
501
502 //Set current default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800503 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700504
505 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700506
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700507 sqlite3_step(statement);
508
509 sqlite3_finalize(statement);
510}
511
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700512Name
Yingdi Yu87581582014-01-14 14:28:39 -0800513SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700514{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700515 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800516 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 -0700517
Yingdi Yu87581582014-01-14 14:28:39 -0800518 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
519
520 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800521
522 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800523 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700524 Name keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
Yingdi Yu2e57a582014-02-20 23:34:43 -0800525 sqlite3_column_bytes(statement, 0)));
526 sqlite3_finalize(statement);
527 return keyName;
528 }
529 else
530 {
531 sqlite3_finalize(statement);
532 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
533 }
Yingdi Yu87581582014-01-14 14:28:39 -0800534}
535
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700536void
Yingdi Yu87581582014-01-14 14:28:39 -0800537SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
538{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700539 if (!doesPublicKeyExist(keyName))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800540 throw Error("SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800541
Yingdi Yu87581582014-01-14 14:28:39 -0800542 string keyId = keyName.get(-1).toEscapedString();
543 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700544
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700546
547 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800548 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 -0700549
550 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
551
552 while (sqlite3_step(statement) == SQLITE_ROW)
553 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700554
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555 sqlite3_finalize(statement);
556
557 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800558 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 -0700559
560 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
561 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700562
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700563 sqlite3_step(statement);
564
565 sqlite3_finalize(statement);
566}
567
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700568Name
Yingdi Yu87581582014-01-14 14:28:39 -0800569SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700570{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700571 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800572 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800573
Yingdi Yu87581582014-01-14 14:28:39 -0800574 string keyId = keyName.get(-1).toEscapedString();
575 Name identityName = keyName.getPrefix(-1);
576
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700577 sqlite3_stmt* statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800578 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 -0800579
580 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
581 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
582
583 int res = sqlite3_step(statement);
584
Yingdi Yu87581582014-01-14 14:28:39 -0800585 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800586 {
587 Name certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
588 sqlite3_finalize(statement);
589 return certName;
590 }
591 else
592 {
593 sqlite3_finalize(statement);
594 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey certificate not found");
595 }
Yingdi Yu87581582014-01-14 14:28:39 -0800596}
597
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700598void
Yingdi Yu87581582014-01-14 14:28:39 -0800599SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
600{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700601 if (!doesCertificateExist(certificateName))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800602 throw Error("SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800603
Yingdi Yu2e57a582014-02-20 23:34:43 -0800604 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu87581582014-01-14 14:28:39 -0800605 string keyId = keyName.get(-1).toEscapedString();
606 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700607
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700608 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700609
610 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800611 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 -0700612
613 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
614 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
615
616 while (sqlite3_step(statement) == SQLITE_ROW)
617 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700618
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700619 sqlite3_finalize(statement);
620
621 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800622 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 -0700623
624 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
625 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
626 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700627
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700628 sqlite3_step(statement);
629
630 sqlite3_finalize(statement);
631}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800632
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800633void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700634SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800635{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700636 sqlite3_stmt* stmt;
637 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800638 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800639 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800640 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800641
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800642 while(sqlite3_step (stmt) == SQLITE_ROW)
643 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 -0700644
645 sqlite3_finalize (stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700646}
647
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800648void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700649SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800650{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700651 sqlite3_stmt* stmt;
652 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800653 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 -0800654 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800655 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 -0800656
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800657 while(sqlite3_step (stmt) == SQLITE_ROW)
658 {
659 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
660 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
661 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662 }
663 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800664}
665
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800666void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700667SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800668{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700669 sqlite3_stmt* stmt;
670 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800671 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 -0800672 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800673 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 -0700674
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800675 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
676
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800677 while(sqlite3_step (stmt) == SQLITE_ROW)
678 {
679 Name keyName(identity);
680 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
681 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700682 }
683 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800684}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700685
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800686void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700687SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800688{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700689 sqlite3_stmt* stmt;
690 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800691 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800692 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800693 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800694
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800695 while(sqlite3_step (stmt) == SQLITE_ROW)
696 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
697
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700698 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800699}
700
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800701void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700702SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800703{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700704 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800705 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800706
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707 sqlite3_stmt* stmt;
708 if (isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800709 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 -0800710 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800711 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 -0800712
Yingdi Yu87581582014-01-14 14:28:39 -0800713 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800714 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
715 std::string baseKeyName = keyName.get(-1).toEscapedString();
716 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
717
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800718 while(sqlite3_step (stmt) == SQLITE_ROW)
719 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
720
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700721 sqlite3_finalize (stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800722}
723
724void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700725SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800726{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700727 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800728 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700729
730 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800731 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
732 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
733 sqlite3_step(stmt);
734 sqlite3_finalize (stmt);
735}
736
737void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800739{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700740 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800741 return;
742
743 string identity = keyName.getPrefix(-1).toUri();
744 string keyId = keyName.get(-1).toEscapedString();
745
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700746 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800747 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
748 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
749 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
750 sqlite3_step(stmt);
751 sqlite3_finalize (stmt);
752
753 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
754 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
755 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
756 sqlite3_step(stmt);
757 sqlite3_finalize (stmt);
758}
759
760void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700761SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800762{
763 string identity = identityName.toUri();
764
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700765 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800766 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
767 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
768 sqlite3_step(stmt);
769 sqlite3_finalize (stmt);
770
771 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
772 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
773 sqlite3_step(stmt);
774 sqlite3_finalize (stmt);
775
776 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
777 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
778 sqlite3_step(stmt);
779 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800780}
781
782} // namespace ndn