blob: 774dfb382b48c37befbd13a92c4203ed8e7125cb [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"
Yingdi Yuc8f883c2014-06-20 23:25:22 -070030#include "signature-sha256-with-ecdsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080031#include "../data.hpp"
32
Yingdi Yu874678f2014-01-22 19:30:34 -080033#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070034#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070035#include <stdlib.h>
36#include <sstream>
37#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080038#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080039
Yingdi Yufc40d872014-02-18 12:56:04 -080040namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070041
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070042using std::string;
43using std::vector;
44
Jeff Thompson7ca11f22013-10-04 19:01:30 -070045static const string INIT_ID_TABLE = "\
46CREATE TABLE IF NOT EXISTS \n \
47 Identity( \n \
48 identity_name BLOB NOT NULL, \n \
49 default_identity INTEGER DEFAULT 0, \n \
50 \
51 PRIMARY KEY (identity_name) \n \
52 ); \n \
53 \
54CREATE INDEX identity_index ON Identity(identity_name); \n \
55";
56
57static const string INIT_KEY_TABLE = "\
58CREATE TABLE IF NOT EXISTS \n \
59 Key( \n \
60 identity_name BLOB NOT NULL, \n \
61 key_identifier BLOB NOT NULL, \n \
62 key_type INTEGER, \n \
63 public_key BLOB, \n \
64 default_key INTEGER DEFAULT 0, \n \
65 active INTEGER DEFAULT 0, \n \
66 \
67 PRIMARY KEY (identity_name, key_identifier) \n \
68 ); \n \
69 \
70CREATE INDEX key_index ON Key(identity_name); \n \
71";
72
73static const string INIT_CERT_TABLE = "\
74CREATE TABLE IF NOT EXISTS \n \
75 Certificate( \n \
76 cert_name BLOB NOT NULL, \n \
77 cert_issuer BLOB NOT NULL, \n \
78 identity_name BLOB NOT NULL, \n \
79 key_identifier BLOB NOT NULL, \n \
80 not_before TIMESTAMP, \n \
81 not_after TIMESTAMP, \n \
82 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070083 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070084 default_cert INTEGER DEFAULT 0, \n \
85 \
86 PRIMARY KEY (cert_name) \n \
87 ); \n \
88 \
89CREATE INDEX cert_index ON Certificate(cert_name); \n \
90CREATE INDEX subject ON Certificate(identity_name); \n \
91";
92
93/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 * A utility function to call the normal sqlite3_bind_text where the value and length are
95 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070096 */
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -070097static int
98sqlite3_bind_text(sqlite3_stmt* statement,
99 int index,
100 const string& value,
101 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700102{
103 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
104}
105
Yingdi Yu87581582014-01-14 14:28:39 -0800106SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700107{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700108 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700109 boost::filesystem::create_directories(identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700110
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800111 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
112 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
113 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700114#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800115 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800116#else
117 0
118#endif
119 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700120 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800121 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700123 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 sqlite3_prepare_v2(m_database,
126 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
127 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700128 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700129
130 bool idTableExists = false;
131 if (res == SQLITE_ROW)
132 idTableExists = true;
133
134 sqlite3_finalize(statement);
135
136 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800138 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700140 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700141 sqlite3_free(errorMessage);
142 }
143 }
144
145 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700146 sqlite3_prepare_v2(m_database,
147 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
148 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700149 res = sqlite3_step(statement);
150
151 bool keyTableExists = false;
152 if (res == SQLITE_ROW)
153 keyTableExists = true;
154
155 sqlite3_finalize(statement);
156
157 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800159 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700161 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700162 sqlite3_free(errorMessage);
163 }
164 }
165
166 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700167 sqlite3_prepare_v2(m_database,
168 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
169 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700170 res = sqlite3_step(statement);
171
172 bool idCertificateTableExists = false;
173 if (res == SQLITE_ROW)
174 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700175
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700176 sqlite3_finalize(statement);
177
178 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700179 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800180 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700181
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700182 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700183 sqlite3_free(errorMessage);
184 }
185 }
186}
187
Yingdi Yu87581582014-01-14 14:28:39 -0800188SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700189{
190}
191
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192bool
Yingdi Yu87581582014-01-14 14:28:39 -0800193SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700194{
195 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700196
197 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700198 sqlite3_prepare_v2(m_database,
199 "SELECT count(*) FROM Identity WHERE identity_name=?",
200 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700201
202 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
203 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700205 if (res == SQLITE_ROW) {
206 int countAll = sqlite3_column_int(statement, 0);
207 if (countAll > 0)
208 result = true;
209 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700210
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700211 sqlite3_finalize(statement);
212
213 return result;
214}
215
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216void
Yingdi Yu87581582014-01-14 14:28:39 -0800217SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700218{
Yingdi Yu05842f22014-04-15 19:21:56 -0700219 if (doesIdentityExist(identityName))
220 return;
221
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700223
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700224 sqlite3_prepare_v2(m_database,
225 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
226 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700227
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700228 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700229
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800230 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700231
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700232 sqlite3_finalize(statement);
233}
234
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700235bool
Yingdi Yu87581582014-01-14 14:28:39 -0800236SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700237{
238 //TODO:
239 return false;
240}
241
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700242bool
Yingdi Yu87581582014-01-14 14:28:39 -0800243SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700244{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700245 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800246 throw Error("Incorrect key name " + keyName.toUri());
247
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700248 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800249 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700250
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700251 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700252 sqlite3_prepare_v2(m_database,
253 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
254 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700255
256 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
257 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
258
259 int res = sqlite3_step(statement);
260
261 bool keyIdExist = false;
262 if (res == SQLITE_ROW) {
263 int countAll = sqlite3_column_int(statement, 0);
264 if (countAll > 0)
265 keyIdExist = true;
266 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700267
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700268 sqlite3_finalize(statement);
269
270 return keyIdExist;
271}
272
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700273void
Yingdi Yu40b53092014-06-17 17:10:02 -0700274SecPublicInfoSqlite3::addKey(const Name& keyName,
275 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700276{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700277 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800278 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800279
Yingdi Yu05842f22014-04-15 19:21:56 -0700280 if (doesPublicKeyExist(keyName))
281 return;
282
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700283 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800284 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700285
Yingdi Yu2e57a582014-02-20 23:34:43 -0800286 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700287
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700289 sqlite3_prepare_v2(m_database,
290 "INSERT OR REPLACE INTO Key \
291 (identity_name, key_identifier, key_type, public_key) \
292 values (?, ?, ?, ?)",
293 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700294
295 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
296 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700297 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700298 sqlite3_bind_blob(statement, 4,
299 publicKeyDer.get().buf(),
300 publicKeyDer.get().size(),
301 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700302
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800303 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700304
305 sqlite3_finalize(statement);
306}
307
Yingdi Yu2e57a582014-02-20 23:34:43 -0800308shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800309SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700310{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700311 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800312 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800313 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
314 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700315
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700316 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800317 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700318
319 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700320 sqlite3_prepare_v2(m_database,
321 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
322 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700323
324 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
325 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
326
327 int res = sqlite3_step(statement);
328
Yingdi Yu2e57a582014-02-20 23:34:43 -0800329 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700330 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800331 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700332 result =
333 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
334 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335 sqlite3_finalize(statement);
336 return result;
337 }
338 else
339 {
340 sqlite3_finalize(statement);
341 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
342 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700343}
344
Yingdi Yu40b53092014-06-17 17:10:02 -0700345KeyType
346SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
347{
348 if (keyName.empty())
349 return KEY_TYPE_NULL;
350
351 string keyId = keyName.get(-1).toUri();
352 Name identityName = keyName.getPrefix(-1);
353
354 sqlite3_stmt* statement;
355 sqlite3_prepare_v2(m_database,
356 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
357 -1, &statement, 0);
358
359 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
360 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
361
362 int res = sqlite3_step(statement);
363
364 if (res == SQLITE_ROW)
365 {
366 int typeValue = sqlite3_column_int(statement, 0);
367 sqlite3_finalize(statement);
368 return static_cast<KeyType>(typeValue);
369 }
370 else
371 {
372 sqlite3_finalize(statement);
373 return KEY_TYPE_NULL;
374 }
375
376}
377
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700378bool
Yingdi Yu87581582014-01-14 14:28:39 -0800379SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700380{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700381 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700382 sqlite3_prepare_v2(m_database,
383 "SELECT count(*) FROM Certificate WHERE cert_name=?",
384 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700385
386 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
387
388 int res = sqlite3_step(statement);
389
390 bool certExist = false;
391 if (res == SQLITE_ROW) {
392 int countAll = sqlite3_column_int(statement, 0);
393 if (countAll > 0)
394 certExist = true;
395 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700396
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700397 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700398
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399 return certExist;
400}
401
Yingdi Yu2e57a582014-02-20 23:34:43 -0800402// void
403// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
404// {
405// std::string certificateName = certificate.getName().toUri();
406// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800407
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700408// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800409// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700410
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700411// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700414// sqlite3_stmt* statement;
415// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700416// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
417// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700419
Yingdi Yu2e57a582014-02-20 23:34:43 -0800420// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700421
Yingdi Yu2e57a582014-02-20 23:34:43 -0800422// try
423// {
424// SignatureSha256WithRsa signature(certificate.getSignature());
425// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700426
Yingdi Yu2e57a582014-02-20 23:34:43 -0800427// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
428// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700429// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800430// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800431// return;
432// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700433// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800434// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800435// return;
436// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800437
Yingdi Yu2e57a582014-02-20 23:34:43 -0800438// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
439// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700440
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700441// // Convert from time::milliseconds to time::seconds since 1/1/1970.
442// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
443// time::toUnixTimestamp(certificate.getNotBefore()).count()));
444// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
445// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700446
Yingdi Yu2e57a582014-02-20 23:34:43 -0800447// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700448
Yingdi Yu2e57a582014-02-20 23:34:43 -0800449// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700450
Yingdi Yu2e57a582014-02-20 23:34:43 -0800451// sqlite3_finalize(statement);
452// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700453
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700454void
Yingdi Yu87581582014-01-14 14:28:39 -0800455SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700456{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700457 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700458 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700459 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700460 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700461
Yingdi Yu40b53092014-06-17 17:10:02 -0700462 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700463
Yingdi Yu05842f22014-04-15 19:21:56 -0700464 if (doesCertificateExist(certificateName))
465 return;
466
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700467 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800468 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700470 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700471 sqlite3_stmt* statement;
472 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700473 "INSERT OR REPLACE INTO Certificate \
474 (cert_name, cert_issuer, identity_name, key_identifier, \
475 not_before, not_after, certificate_data) \
476 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700477 -1, &statement, 0);
478
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700479 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
480
Yingdi Yu2e57a582014-02-20 23:34:43 -0800481 try
482 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700483 // this will throw an exception if the signature is not the standard one
484 // or there is no key locator present
Yingdi Yu4a557052014-07-09 16:40:37 -0700485 std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
Alexander Afanasyevdb4affc2014-07-15 13:37:16 -0700486 sqlite3_bind_text(statement, 2, signerName, SQLITE_TRANSIENT);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800487 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700488 catch (Tlv::Error& e)
489 {
490 return;
491 }
492 catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800493 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800494 return;
495 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700496
497 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800498 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700499
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700500 sqlite3_bind_int64(statement, 5,
501 static_cast<sqlite3_int64>(
502 time::toUnixTimestamp(certificate.getNotBefore()).count()));
503 sqlite3_bind_int64(statement, 6,
504 static_cast<sqlite3_int64>(
505 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700506
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700507 sqlite3_bind_blob(statement, 7,
508 certificate.wireEncode().wire(),
509 certificate.wireEncode().size(),
510 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700511
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800512 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700513
514 sqlite3_finalize(statement);
515}
516
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700517shared_ptr<IdentityCertificate>
518SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700519{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700520 sqlite3_stmt* statement;
521
522 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800523 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
524 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700525
Yingdi Yu4270f202014-01-28 14:19:16 -0800526 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700527
Yingdi Yu4270f202014-01-28 14:19:16 -0800528 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700529
Yingdi Yu4270f202014-01-28 14:19:16 -0800530 if (res == SQLITE_ROW)
531 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800532 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700533 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700534 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800535 sqlite3_finalize(statement);
536 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800537 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800538 else
539 {
540 sqlite3_finalize(statement);
541 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
542 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700543}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700544
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545
546Name
Yingdi Yu87581582014-01-14 14:28:39 -0800547SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700548{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700549 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700550 sqlite3_prepare_v2(m_database,
551 "SELECT identity_name FROM Identity WHERE default_identity=1",
552 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700553
554 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700555
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700556 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800557 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700558 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
559 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800560 sqlite3_finalize(statement);
561 return identity;
562 }
563 else
564 {
565 sqlite3_finalize(statement);
566 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
567 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700568}
569
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700570void
Yingdi Yu87581582014-01-14 14:28:39 -0800571SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700572{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800573 addIdentity(identityName);
574
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700575 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700576
577 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700578 sqlite3_prepare_v2(m_database,
579 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
580 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700581
582 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700583 {
584 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700585
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700586 sqlite3_finalize(statement);
587
588 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700589 sqlite3_prepare_v2(m_database,
590 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
591 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700592
593 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700594
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700595 sqlite3_step(statement);
596
597 sqlite3_finalize(statement);
598}
599
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700600Name
Yingdi Yu87581582014-01-14 14:28:39 -0800601SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700602{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700603 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700604 sqlite3_prepare_v2(m_database,
605 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
606 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700607
Yingdi Yu87581582014-01-14 14:28:39 -0800608 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
609
610 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800611
612 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800613 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700614 Name keyName = identityName;
615 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
616 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800617 sqlite3_finalize(statement);
618 return keyName;
619 }
620 else
621 {
622 sqlite3_finalize(statement);
623 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
624 }
Yingdi Yu87581582014-01-14 14:28:39 -0800625}
626
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700627void
Yingdi Yu87581582014-01-14 14:28:39 -0800628SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
629{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700630 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700631 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800632
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700633 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800634 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700635
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700636 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700637
638 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700639 sqlite3_prepare_v2(m_database,
640 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
641 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700642
643 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
644
645 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700646 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700647
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700648 sqlite3_finalize(statement);
649
650 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700651 sqlite3_prepare_v2(m_database,
652 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
653 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700654
655 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
656 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700657
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700658 sqlite3_step(statement);
659
660 sqlite3_finalize(statement);
661}
662
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700663Name
Yingdi Yu87581582014-01-14 14:28:39 -0800664SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700665{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700666 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800667 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800668
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);
671
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700672 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700673 sqlite3_prepare_v2(m_database,
674 "SELECT cert_name FROM Certificate \
675 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
676 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800677
678 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
679 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
680
681 int res = sqlite3_step(statement);
682
Yingdi Yu87581582014-01-14 14:28:39 -0800683 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800684 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700685 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
686 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800687 sqlite3_finalize(statement);
688 return certName;
689 }
690 else
691 {
692 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700693 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800694 }
Yingdi Yu87581582014-01-14 14:28:39 -0800695}
696
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700697void
Yingdi Yu87581582014-01-14 14:28:39 -0800698SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
699{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700700 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700701 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800702
Yingdi Yu2e57a582014-02-20 23:34:43 -0800703 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700704 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800705 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700706
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700707 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700708
709 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700710 sqlite3_prepare_v2(m_database,
711 "UPDATE Certificate SET default_cert=0 \
712 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
713 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700714
715 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
716 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
717
718 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700719 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700720
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700721 sqlite3_finalize(statement);
722
723 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700724 sqlite3_prepare_v2(m_database,
725 "UPDATE Certificate SET default_cert=1 \
726 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
727 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700728
729 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
730 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
731 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700732
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700733 sqlite3_step(statement);
734
735 sqlite3_finalize(statement);
736}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800737
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800738void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700739SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800740{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700741 sqlite3_stmt* stmt;
742 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700743 sqlite3_prepare_v2(m_database,
744 "SELECT identity_name FROM Identity WHERE default_identity=1",
745 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800746 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700747 sqlite3_prepare_v2(m_database,
748 "SELECT identity_name FROM Identity WHERE default_identity=0",
749 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800750
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700751 while (sqlite3_step(stmt) == SQLITE_ROW)
752 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
753 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700754
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700755 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700756}
757
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800758void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700759SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800760{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700761 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800762
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700763 if (isDefault)
764 sqlite3_prepare_v2(m_database,
765 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
766 -1, &stmt, 0);
767 else
768 sqlite3_prepare_v2(m_database,
769 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
770 -1, &stmt, 0);
771
772 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800773 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700774 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
775 sqlite3_column_bytes(stmt, 0)));
776 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
777 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800778 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700779 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700780 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800781}
782
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800783void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700784SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
785 vector<Name>& nameList,
786 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800787{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700788 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700789
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700790 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700791 sqlite3_prepare_v2(m_database,
792 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
793 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800794 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700795 sqlite3_prepare_v2(m_database,
796 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
797 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700798
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700799 sqlite3_bind_text(stmt, 1,
800 identity.toUri().c_str(),
801 identity.toUri().size(),
802 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800803
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700804 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800805 {
806 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700807 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
808 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800809 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700810 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700811 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800812}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700813
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800814void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700815SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800816{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700817 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700818
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700819 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700820 sqlite3_prepare_v2(m_database,
821 "SELECT cert_name FROM Certificate WHERE default_cert=1",
822 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800823 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700824 sqlite3_prepare_v2(m_database,
825 "SELECT cert_name FROM Certificate WHERE default_cert=0",
826 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800827
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700828 while (sqlite3_step(stmt) == SQLITE_ROW)
829 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
830 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800831
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700832 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800833}
834
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800835void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700836SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
837 vector<Name>& nameList,
838 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800839{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700840 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800841 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800842
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700843 sqlite3_stmt* stmt;
844 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700845 sqlite3_prepare_v2(m_database,
846 "SELECT cert_name FROM Certificate \
847 WHERE default_cert=1 and identity_name=? and key_identifier=?",
848 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800849 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700850 sqlite3_prepare_v2(m_database,
851 "SELECT cert_name FROM Certificate \
852 WHERE default_cert=0 and identity_name=? and key_identifier=?",
853 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800854
Yingdi Yu87581582014-01-14 14:28:39 -0800855 Name identity = keyName.getPrefix(-1);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700856 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700857
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700858 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800859 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
860
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700861 while (sqlite3_step(stmt) == SQLITE_ROW)
862 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
863 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800864
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700865 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800866}
867
868void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700869SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800870{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700871 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800872 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700873
874 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800875 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700876 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800877 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700878 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800879}
880
881void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700882SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800883{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700884 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800885 return;
886
887 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700888 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800889
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700890 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700891 sqlite3_prepare_v2(m_database,
892 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
893 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700894 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800895 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
896 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700897 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800898
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700899 sqlite3_prepare_v2(m_database,
900 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
901 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700902 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800903 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
904 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700905 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800906}
907
908void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700909SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800910{
911 string identity = identityName.toUri();
912
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700913 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800914 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700915 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800916 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700917 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800918
919 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700920 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800921 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700922 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800923
924 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700925 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800926 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700927 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800928}
929
930} // namespace ndn