blob: c147bf138114c6c700717e99d1ef4e8ac4452739 [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 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600488 catch (tlv::Error& e)
Yingdi Yu4a557052014-07-09 16:40:37 -0700489 {
490 return;
491 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700492
493 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800494 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700495
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700496 sqlite3_bind_int64(statement, 5,
497 static_cast<sqlite3_int64>(
498 time::toUnixTimestamp(certificate.getNotBefore()).count()));
499 sqlite3_bind_int64(statement, 6,
500 static_cast<sqlite3_int64>(
501 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700502
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700503 sqlite3_bind_blob(statement, 7,
504 certificate.wireEncode().wire(),
505 certificate.wireEncode().size(),
506 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700507
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800508 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700509
510 sqlite3_finalize(statement);
511}
512
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700513shared_ptr<IdentityCertificate>
514SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700515{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700516 sqlite3_stmt* statement;
517
518 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800519 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
520 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700521
Yingdi Yu4270f202014-01-28 14:19:16 -0800522 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700523
Yingdi Yu4270f202014-01-28 14:19:16 -0800524 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700525
Yingdi Yu4270f202014-01-28 14:19:16 -0800526 if (res == SQLITE_ROW)
527 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800528 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700529 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700530 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800531 sqlite3_finalize(statement);
532 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800533 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800534 else
535 {
536 sqlite3_finalize(statement);
537 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
538 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700539}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700540
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700541
542Name
Yingdi Yu87581582014-01-14 14:28:39 -0800543SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700544{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700546 sqlite3_prepare_v2(m_database,
547 "SELECT identity_name FROM Identity WHERE default_identity=1",
548 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700549
550 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700551
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700552 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800553 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700554 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
555 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800556 sqlite3_finalize(statement);
557 return identity;
558 }
559 else
560 {
561 sqlite3_finalize(statement);
562 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
563 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700564}
565
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700566void
Yingdi Yu87581582014-01-14 14:28:39 -0800567SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700568{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800569 addIdentity(identityName);
570
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700571 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700572
573 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700574 sqlite3_prepare_v2(m_database,
575 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
576 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700577
578 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700579 {
580 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700581
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700582 sqlite3_finalize(statement);
583
584 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700585 sqlite3_prepare_v2(m_database,
586 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
587 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700588
589 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700590
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700591 sqlite3_step(statement);
592
593 sqlite3_finalize(statement);
594}
595
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700596Name
Yingdi Yu87581582014-01-14 14:28:39 -0800597SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700598{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700599 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700600 sqlite3_prepare_v2(m_database,
601 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
602 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700603
Yingdi Yu87581582014-01-14 14:28:39 -0800604 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
605
606 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800607
608 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800609 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700610 Name keyName = identityName;
611 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
612 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800613 sqlite3_finalize(statement);
614 return keyName;
615 }
616 else
617 {
618 sqlite3_finalize(statement);
619 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
620 }
Yingdi Yu87581582014-01-14 14:28:39 -0800621}
622
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700623void
Yingdi Yu87581582014-01-14 14:28:39 -0800624SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
625{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700626 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700627 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800628
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700629 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800630 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700631
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700632 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700633
634 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700635 sqlite3_prepare_v2(m_database,
636 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
637 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700638
639 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
640
641 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700642 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700643
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700644 sqlite3_finalize(statement);
645
646 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700647 sqlite3_prepare_v2(m_database,
648 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
649 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700650
651 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
652 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700653
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700654 sqlite3_step(statement);
655
656 sqlite3_finalize(statement);
657}
658
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700659Name
Yingdi Yu87581582014-01-14 14:28:39 -0800660SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700661{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800663 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800664
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700665 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800666 Name identityName = keyName.getPrefix(-1);
667
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700668 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700669 sqlite3_prepare_v2(m_database,
670 "SELECT cert_name FROM Certificate \
671 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
672 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800673
674 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
675 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
676
677 int res = sqlite3_step(statement);
678
Yingdi Yu87581582014-01-14 14:28:39 -0800679 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800680 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700681 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
682 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800683 sqlite3_finalize(statement);
684 return certName;
685 }
686 else
687 {
688 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700689 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800690 }
Yingdi Yu87581582014-01-14 14:28:39 -0800691}
692
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700693void
Yingdi Yu87581582014-01-14 14:28:39 -0800694SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
695{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700697 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800698
Yingdi Yu2e57a582014-02-20 23:34:43 -0800699 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700700 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800701 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700702
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700703 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700704
705 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700706 sqlite3_prepare_v2(m_database,
707 "UPDATE Certificate SET default_cert=0 \
708 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
709 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700710
711 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
712 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
713
714 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700715 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700716
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700717 sqlite3_finalize(statement);
718
719 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700720 sqlite3_prepare_v2(m_database,
721 "UPDATE Certificate SET default_cert=1 \
722 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
723 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700724
725 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
726 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
727 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700728
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700729 sqlite3_step(statement);
730
731 sqlite3_finalize(statement);
732}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800733
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800734void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700735SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800736{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700737 sqlite3_stmt* stmt;
738 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700739 sqlite3_prepare_v2(m_database,
740 "SELECT identity_name FROM Identity WHERE default_identity=1",
741 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800742 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700743 sqlite3_prepare_v2(m_database,
744 "SELECT identity_name FROM Identity WHERE default_identity=0",
745 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800746
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700747 while (sqlite3_step(stmt) == SQLITE_ROW)
748 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
749 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700750
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700751 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700752}
753
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800754void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700755SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800756{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700757 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800758
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700759 if (isDefault)
760 sqlite3_prepare_v2(m_database,
761 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
762 -1, &stmt, 0);
763 else
764 sqlite3_prepare_v2(m_database,
765 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
766 -1, &stmt, 0);
767
768 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800769 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700770 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
771 sqlite3_column_bytes(stmt, 0)));
772 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
773 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800774 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700775 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700776 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800777}
778
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800779void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700780SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
781 vector<Name>& nameList,
782 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800783{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700784 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700785
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700786 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700787 sqlite3_prepare_v2(m_database,
788 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
789 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800790 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700791 sqlite3_prepare_v2(m_database,
792 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
793 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700794
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700795 sqlite3_bind_text(stmt, 1,
796 identity.toUri().c_str(),
797 identity.toUri().size(),
798 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800799
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700800 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800801 {
802 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700803 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
804 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800805 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700806 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700807 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800808}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700809
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800810void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700811SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800812{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700813 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700814
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700815 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700816 sqlite3_prepare_v2(m_database,
817 "SELECT cert_name FROM Certificate WHERE default_cert=1",
818 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800819 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700820 sqlite3_prepare_v2(m_database,
821 "SELECT cert_name FROM Certificate WHERE default_cert=0",
822 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800823
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700824 while (sqlite3_step(stmt) == SQLITE_ROW)
825 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
826 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800827
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700828 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800829}
830
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800831void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700832SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
833 vector<Name>& nameList,
834 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800835{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700836 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800837 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800838
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700839 sqlite3_stmt* stmt;
840 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700841 sqlite3_prepare_v2(m_database,
842 "SELECT cert_name FROM Certificate \
843 WHERE default_cert=1 and identity_name=? and key_identifier=?",
844 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800845 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700846 sqlite3_prepare_v2(m_database,
847 "SELECT cert_name FROM Certificate \
848 WHERE default_cert=0 and identity_name=? and key_identifier=?",
849 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800850
Yingdi Yu87581582014-01-14 14:28:39 -0800851 Name identity = keyName.getPrefix(-1);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700852 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700853
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700854 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800855 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
856
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700857 while (sqlite3_step(stmt) == SQLITE_ROW)
858 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
859 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800860
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700861 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800862}
863
864void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700865SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800866{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700867 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800868 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700869
870 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800871 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700872 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800873 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700874 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800875}
876
877void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700878SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800879{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700880 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800881 return;
882
883 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700884 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800885
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700886 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700887 sqlite3_prepare_v2(m_database,
888 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
889 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700890 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800891 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
892 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700893 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700895 sqlite3_prepare_v2(m_database,
896 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
897 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700898 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800899 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
900 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700901 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800902}
903
904void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700905SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800906{
907 string identity = identityName.toUri();
908
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700909 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800910 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700911 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800912 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700913 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800914
915 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700916 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800917 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700918 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800919
920 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700921 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800922 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700923 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800924}
925
926} // namespace ndn