blob: db0bdd453ef11d25c6118a8a7b91f637260f048b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson7ca11f22013-10-04 19:01:30 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070023 */
24
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "common.hpp"
26
27#include "sec-public-info-sqlite3.hpp"
28#include "identity-certificate.hpp"
29#include "signature-sha256-with-rsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080030#include "../data.hpp"
31
Yingdi Yu874678f2014-01-22 19:30:34 -080032#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070033#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070034#include <stdlib.h>
35#include <sstream>
36#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080037#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080038
Jeff Thompson7ca11f22013-10-04 19:01:30 -070039using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070040
Yingdi Yufc40d872014-02-18 12:56:04 -080041namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070042
43static const string INIT_ID_TABLE = "\
44CREATE TABLE IF NOT EXISTS \n \
45 Identity( \n \
46 identity_name BLOB NOT NULL, \n \
47 default_identity INTEGER DEFAULT 0, \n \
48 \
49 PRIMARY KEY (identity_name) \n \
50 ); \n \
51 \
52CREATE INDEX identity_index ON Identity(identity_name); \n \
53";
54
55static const string INIT_KEY_TABLE = "\
56CREATE TABLE IF NOT EXISTS \n \
57 Key( \n \
58 identity_name BLOB NOT NULL, \n \
59 key_identifier BLOB NOT NULL, \n \
60 key_type INTEGER, \n \
61 public_key BLOB, \n \
62 default_key INTEGER DEFAULT 0, \n \
63 active INTEGER DEFAULT 0, \n \
64 \
65 PRIMARY KEY (identity_name, key_identifier) \n \
66 ); \n \
67 \
68CREATE INDEX key_index ON Key(identity_name); \n \
69";
70
71static const string INIT_CERT_TABLE = "\
72CREATE TABLE IF NOT EXISTS \n \
73 Certificate( \n \
74 cert_name BLOB NOT NULL, \n \
75 cert_issuer BLOB NOT NULL, \n \
76 identity_name BLOB NOT NULL, \n \
77 key_identifier BLOB NOT NULL, \n \
78 not_before TIMESTAMP, \n \
79 not_after TIMESTAMP, \n \
80 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070081 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070082 default_cert INTEGER DEFAULT 0, \n \
83 \
84 PRIMARY KEY (cert_name) \n \
85 ); \n \
86 \
87CREATE INDEX cert_index ON Certificate(cert_name); \n \
88CREATE INDEX subject ON Certificate(identity_name); \n \
89";
90
91/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 * A utility function to call the normal sqlite3_bind_text where the value and length are
93 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070094 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095static int sqlite3_bind_text(sqlite3_stmt* statement,
96 int index,
97 const string& value,
98 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -070099{
100 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
101}
102
Yingdi Yu87581582014-01-14 14:28:39 -0800103SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700104{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700105 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -0800106 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700107
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800108 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
109 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
110 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700111#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800112 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800113#else
114 0
115#endif
116 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700117 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800118 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700120 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700122 sqlite3_prepare_v2(m_database,
123 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
124 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700125 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700126
127 bool idTableExists = false;
128 if (res == SQLITE_ROW)
129 idTableExists = true;
130
131 sqlite3_finalize(statement);
132
133 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800135 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700137 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700138 sqlite3_free(errorMessage);
139 }
140 }
141
142 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 sqlite3_prepare_v2(m_database,
144 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
145 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700146 res = sqlite3_step(statement);
147
148 bool keyTableExists = false;
149 if (res == SQLITE_ROW)
150 keyTableExists = true;
151
152 sqlite3_finalize(statement);
153
154 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800156 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700158 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700159 sqlite3_free(errorMessage);
160 }
161 }
162
163 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700164 sqlite3_prepare_v2(m_database,
165 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
166 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700167 res = sqlite3_step(statement);
168
169 bool idCertificateTableExists = false;
170 if (res == SQLITE_ROW)
171 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700172
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700173 sqlite3_finalize(statement);
174
175 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700176 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800177 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700179 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700180 sqlite3_free(errorMessage);
181 }
182 }
183}
184
Yingdi Yu87581582014-01-14 14:28:39 -0800185SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700186{
187}
188
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700189bool
Yingdi Yu87581582014-01-14 14:28:39 -0800190SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700191{
192 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193
194 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700195 sqlite3_prepare_v2(m_database,
196 "SELECT count(*) FROM Identity WHERE identity_name=?",
197 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700198
199 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
200 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700202 if (res == SQLITE_ROW) {
203 int countAll = sqlite3_column_int(statement, 0);
204 if (countAll > 0)
205 result = true;
206 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700208 sqlite3_finalize(statement);
209
210 return result;
211}
212
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213void
Yingdi Yu87581582014-01-14 14:28:39 -0800214SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700215{
Yingdi Yu05842f22014-04-15 19:21:56 -0700216 if (doesIdentityExist(identityName))
217 return;
218
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700221 sqlite3_prepare_v2(m_database,
222 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
223 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800227 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700228
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700229 sqlite3_finalize(statement);
230}
231
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232bool
Yingdi Yu87581582014-01-14 14:28:39 -0800233SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700234{
235 //TODO:
236 return false;
237}
238
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700239bool
Yingdi Yu87581582014-01-14 14:28:39 -0800240SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700241{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700242 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800243 throw Error("Incorrect key name " + keyName.toUri());
244
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700245 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800246 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700247
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700248 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700249 sqlite3_prepare_v2(m_database,
250 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
251 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700252
253 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
254 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
255
256 int res = sqlite3_step(statement);
257
258 bool keyIdExist = false;
259 if (res == SQLITE_ROW) {
260 int countAll = sqlite3_column_int(statement, 0);
261 if (countAll > 0)
262 keyIdExist = true;
263 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700264
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700265 sqlite3_finalize(statement);
266
267 return keyIdExist;
268}
269
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700270void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700271SecPublicInfoSqlite3::addPublicKey(const Name& keyName,
272 KeyType keyType,
273 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700274{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700275 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800276 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800277
Yingdi Yu05842f22014-04-15 19:21:56 -0700278 if (doesPublicKeyExist(keyName))
279 return;
280
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700281 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800282 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700283
Yingdi Yu2e57a582014-02-20 23:34:43 -0800284 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700285
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700286 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700287 sqlite3_prepare_v2(m_database,
288 "INSERT OR REPLACE INTO Key \
289 (identity_name, key_identifier, key_type, public_key) \
290 values (?, ?, ?, ?)",
291 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700292
293 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
294 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
295 sqlite3_bind_int(statement, 3, (int)keyType);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700296 sqlite3_bind_blob(statement, 4,
297 publicKeyDer.get().buf(),
298 publicKeyDer.get().size(),
299 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700300
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800301 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700302
303 sqlite3_finalize(statement);
304}
305
Yingdi Yu2e57a582014-02-20 23:34:43 -0800306shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800307SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700308{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700309 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800310 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800311 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
312 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700313
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700314 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800315 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700316
317 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700318 sqlite3_prepare_v2(m_database,
319 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
320 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700321
322 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
323 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
324
325 int res = sqlite3_step(statement);
326
Yingdi Yu2e57a582014-02-20 23:34:43 -0800327 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700328 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800329 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700330 result =
331 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
332 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 sqlite3_finalize(statement);
334 return result;
335 }
336 else
337 {
338 sqlite3_finalize(statement);
339 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
340 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700341}
342
343bool
Yingdi Yu87581582014-01-14 14:28:39 -0800344SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700345{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700346 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700347 sqlite3_prepare_v2(m_database,
348 "SELECT count(*) FROM Certificate WHERE cert_name=?",
349 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700350
351 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
352
353 int res = sqlite3_step(statement);
354
355 bool certExist = false;
356 if (res == SQLITE_ROW) {
357 int countAll = sqlite3_column_int(statement, 0);
358 if (countAll > 0)
359 certExist = true;
360 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700361
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700362 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700363
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700364 return certExist;
365}
366
Yingdi Yu2e57a582014-02-20 23:34:43 -0800367// void
368// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
369// {
370// std::string certificateName = certificate.getName().toUri();
371// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800372
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700373// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700376// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800377// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700378
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700379// sqlite3_stmt* statement;
380// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700381// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
382// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700384
Yingdi Yu2e57a582014-02-20 23:34:43 -0800385// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700386
Yingdi Yu2e57a582014-02-20 23:34:43 -0800387// try
388// {
389// SignatureSha256WithRsa signature(certificate.getSignature());
390// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700391
Yingdi Yu2e57a582014-02-20 23:34:43 -0800392// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
393// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700394// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800395// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800396// return;
397// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700398// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800399// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800400// return;
401// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800402
Yingdi Yu2e57a582014-02-20 23:34:43 -0800403// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
404// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700405
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700406// // Convert from time::milliseconds to time::seconds since 1/1/1970.
407// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
408// time::toUnixTimestamp(certificate.getNotBefore()).count()));
409// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
410// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700411
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Yingdi Yu2e57a582014-02-20 23:34:43 -0800414// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700415
Yingdi Yu2e57a582014-02-20 23:34:43 -0800416// sqlite3_finalize(statement);
417// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700418
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700419void
Yingdi Yu87581582014-01-14 14:28:39 -0800420SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700421{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700422 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700423 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700424 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700425 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700426
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700427 //HACK!!! Assume the key type is RSA, we should check more.
428 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429
Yingdi Yu05842f22014-04-15 19:21:56 -0700430 if (doesCertificateExist(certificateName))
431 return;
432
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700433 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800434 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700435
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700436 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700437 sqlite3_stmt* statement;
438 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700439 "INSERT OR REPLACE INTO Certificate \
440 (cert_name, cert_issuer, identity_name, key_identifier, \
441 not_before, not_after, certificate_data) \
442 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700443 -1, &statement, 0);
444
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700445 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
446
Yingdi Yu2e57a582014-02-20 23:34:43 -0800447 try
448 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700449 // this will throw an exception if the signature is not the standard one
450 // or there is no key locator present
Yingdi Yu2e57a582014-02-20 23:34:43 -0800451 SignatureSha256WithRsa signature(certificate.getSignature());
452 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800453
Yingdi Yu2e57a582014-02-20 23:34:43 -0800454 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
455 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700456 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800457 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800458 return;
459 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460
461 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800462 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700463
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700464 sqlite3_bind_int64(statement, 5,
465 static_cast<sqlite3_int64>(
466 time::toUnixTimestamp(certificate.getNotBefore()).count()));
467 sqlite3_bind_int64(statement, 6,
468 static_cast<sqlite3_int64>(
469 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700470
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700471 sqlite3_bind_blob(statement, 7,
472 certificate.wireEncode().wire(),
473 certificate.wireEncode().size(),
474 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700475
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800476 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700477
478 sqlite3_finalize(statement);
479}
480
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700481shared_ptr<IdentityCertificate>
482SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700483{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700484 sqlite3_stmt* statement;
485
486 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800487 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
488 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700489
Yingdi Yu4270f202014-01-28 14:19:16 -0800490 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700491
Yingdi Yu4270f202014-01-28 14:19:16 -0800492 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700493
Yingdi Yu4270f202014-01-28 14:19:16 -0800494 if (res == SQLITE_ROW)
495 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800496 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700497 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700498 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800499 sqlite3_finalize(statement);
500 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800501 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800502 else
503 {
504 sqlite3_finalize(statement);
505 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
506 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700507}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700508
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700509
510Name
Yingdi Yu87581582014-01-14 14:28:39 -0800511SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700513 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700514 sqlite3_prepare_v2(m_database,
515 "SELECT identity_name FROM Identity WHERE default_identity=1",
516 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700517
518 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700519
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700520 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800521 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700522 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
523 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800524 sqlite3_finalize(statement);
525 return identity;
526 }
527 else
528 {
529 sqlite3_finalize(statement);
530 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
531 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700532}
533
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700534void
Yingdi Yu87581582014-01-14 14:28:39 -0800535SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700536{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800537 addIdentity(identityName);
538
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700539 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700540
541 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700542 sqlite3_prepare_v2(m_database,
543 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
544 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700545
546 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700547 {
548 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700549
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700550 sqlite3_finalize(statement);
551
552 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700553 sqlite3_prepare_v2(m_database,
554 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
555 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700556
557 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700558
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700559 sqlite3_step(statement);
560
561 sqlite3_finalize(statement);
562}
563
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700564Name
Yingdi Yu87581582014-01-14 14:28:39 -0800565SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700566{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700567 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700568 sqlite3_prepare_v2(m_database,
569 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
570 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700571
Yingdi Yu87581582014-01-14 14:28:39 -0800572 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
573
574 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800575
576 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800577 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700578 Name keyName = identityName;
579 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
580 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800581 sqlite3_finalize(statement);
582 return keyName;
583 }
584 else
585 {
586 sqlite3_finalize(statement);
587 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
588 }
Yingdi Yu87581582014-01-14 14:28:39 -0800589}
590
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700591void
Yingdi Yu87581582014-01-14 14:28:39 -0800592SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
593{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700594 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700595 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800596
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700597 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800598 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700599
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700600 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700601
602 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700603 sqlite3_prepare_v2(m_database,
604 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
605 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700606
607 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
608
609 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700610 {
611 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700612
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700613 sqlite3_finalize(statement);
614
615 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700616 sqlite3_prepare_v2(m_database,
617 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
618 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700619
620 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
621 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700622
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700623 sqlite3_step(statement);
624
625 sqlite3_finalize(statement);
626}
627
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700628Name
Yingdi Yu87581582014-01-14 14:28:39 -0800629SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700630{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700631 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800632 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800633
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700634 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800635 Name identityName = keyName.getPrefix(-1);
636
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700637 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700638 sqlite3_prepare_v2(m_database,
639 "SELECT cert_name FROM Certificate \
640 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
641 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800642
643 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
644 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
645
646 int res = sqlite3_step(statement);
647
Yingdi Yu87581582014-01-14 14:28:39 -0800648 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800649 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700650 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
651 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800652 sqlite3_finalize(statement);
653 return certName;
654 }
655 else
656 {
657 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700658 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800659 }
Yingdi Yu87581582014-01-14 14:28:39 -0800660}
661
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662void
Yingdi Yu87581582014-01-14 14:28:39 -0800663SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
664{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700665 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700666 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800667
Yingdi Yu2e57a582014-02-20 23:34:43 -0800668 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700669 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800670 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700671
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700672 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700673
674 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700675 sqlite3_prepare_v2(m_database,
676 "UPDATE Certificate SET default_cert=0 \
677 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
678 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700679
680 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
681 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
682
683 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700684 {
685 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700686
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700687 sqlite3_finalize(statement);
688
689 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700690 sqlite3_prepare_v2(m_database,
691 "UPDATE Certificate SET default_cert=1 \
692 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
693 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700694
695 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
696 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
697 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700698
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700699 sqlite3_step(statement);
700
701 sqlite3_finalize(statement);
702}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800703
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800704void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700705SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800706{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707 sqlite3_stmt* stmt;
708 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700709 sqlite3_prepare_v2(m_database,
710 "SELECT identity_name FROM Identity WHERE default_identity=1",
711 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800712 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700713 sqlite3_prepare_v2(m_database,
714 "SELECT identity_name FROM Identity WHERE default_identity=0",
715 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800716
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700717 while (sqlite3_step(stmt) == SQLITE_ROW)
718 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
719 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700720
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700721 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700722}
723
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800724void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700725SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800726{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700727 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800728
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700729 if (isDefault)
730 sqlite3_prepare_v2(m_database,
731 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
732 -1, &stmt, 0);
733 else
734 sqlite3_prepare_v2(m_database,
735 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
736 -1, &stmt, 0);
737
738 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800739 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700740 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
741 sqlite3_column_bytes(stmt, 0)));
742 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
743 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800744 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700745 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700746 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800747}
748
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800749void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700750SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
751 vector<Name>& nameList,
752 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800753{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700754 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700755
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700756 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700757 sqlite3_prepare_v2(m_database,
758 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
759 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800760 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700761 sqlite3_prepare_v2(m_database,
762 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
763 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700764
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700765 sqlite3_bind_text(stmt, 1,
766 identity.toUri().c_str(),
767 identity.toUri().size(),
768 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800769
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700770 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800771 {
772 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700773 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
774 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800775 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700776 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700777 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800778}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700779
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800780void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700781SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800782{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700783 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700784
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700785 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700786 sqlite3_prepare_v2(m_database,
787 "SELECT cert_name FROM Certificate WHERE default_cert=1",
788 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800789 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700790 sqlite3_prepare_v2(m_database,
791 "SELECT cert_name FROM Certificate WHERE default_cert=0",
792 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800793
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700794 while (sqlite3_step(stmt) == SQLITE_ROW)
795 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
796 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800797
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700798 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800799}
800
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800801void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700802SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
803 vector<Name>& nameList,
804 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800805{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700806 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800807 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800808
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700809 sqlite3_stmt* stmt;
810 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700811 sqlite3_prepare_v2(m_database,
812 "SELECT cert_name FROM Certificate \
813 WHERE default_cert=1 and identity_name=? and key_identifier=?",
814 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800815 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700816 sqlite3_prepare_v2(m_database,
817 "SELECT cert_name FROM Certificate \
818 WHERE default_cert=0 and identity_name=? and key_identifier=?",
819 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800820
Yingdi Yu87581582014-01-14 14:28:39 -0800821 Name identity = keyName.getPrefix(-1);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700822 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
823
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700824 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800825 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
826
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700827 while (sqlite3_step(stmt) == SQLITE_ROW)
828 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
829 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800830
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700831 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800832}
833
834void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700835SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800836{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700837 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800838 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700839
840 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800841 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700842 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800843 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700844 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800845}
846
847void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700848SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800849{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700850 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800851 return;
852
853 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700854 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800855
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700856 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700857 sqlite3_prepare_v2(m_database,
858 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
859 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800860 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
861 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
862 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700863 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800864
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700865 sqlite3_prepare_v2(m_database,
866 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
867 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800868 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
869 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
870 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700871 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800872}
873
874void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700875SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800876{
877 string identity = identityName.toUri();
878
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700879 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800880 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
881 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
882 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700883 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800884
885 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
886 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
887 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700888 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800889
890 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
891 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
892 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700893 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800894}
895
896} // namespace ndn