blob: b2bd5126a27f8168273de7db01bd70d1e6fddb43 [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 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070097static int sqlite3_bind_text(sqlite3_stmt* statement,
98 int index,
99 const string& value,
100 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700101{
102 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
103}
104
Yingdi Yu87581582014-01-14 14:28:39 -0800105SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700106{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700107 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700108 boost::filesystem::create_directories(identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700109
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800110 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
111 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
112 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700113#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800114 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800115#else
116 0
117#endif
118 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700119 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800120 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700122 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 sqlite3_prepare_v2(m_database,
125 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
126 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700127 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700128
129 bool idTableExists = false;
130 if (res == SQLITE_ROW)
131 idTableExists = true;
132
133 sqlite3_finalize(statement);
134
135 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800137 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700139 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700140 sqlite3_free(errorMessage);
141 }
142 }
143
144 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700145 sqlite3_prepare_v2(m_database,
146 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
147 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700148 res = sqlite3_step(statement);
149
150 bool keyTableExists = false;
151 if (res == SQLITE_ROW)
152 keyTableExists = true;
153
154 sqlite3_finalize(statement);
155
156 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800158 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700160 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700161 sqlite3_free(errorMessage);
162 }
163 }
164
165 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700166 sqlite3_prepare_v2(m_database,
167 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
168 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700169 res = sqlite3_step(statement);
170
171 bool idCertificateTableExists = false;
172 if (res == SQLITE_ROW)
173 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700174
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700175 sqlite3_finalize(statement);
176
177 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800179 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700181 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700182 sqlite3_free(errorMessage);
183 }
184 }
185}
186
Yingdi Yu87581582014-01-14 14:28:39 -0800187SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700188{
189}
190
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191bool
Yingdi Yu87581582014-01-14 14:28:39 -0800192SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700193{
194 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700195
196 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700197 sqlite3_prepare_v2(m_database,
198 "SELECT count(*) FROM Identity WHERE identity_name=?",
199 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700200
201 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
202 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700203
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700204 if (res == SQLITE_ROW) {
205 int countAll = sqlite3_column_int(statement, 0);
206 if (countAll > 0)
207 result = true;
208 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700209
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700210 sqlite3_finalize(statement);
211
212 return result;
213}
214
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700215void
Yingdi Yu87581582014-01-14 14:28:39 -0800216SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700217{
Yingdi Yu05842f22014-04-15 19:21:56 -0700218 if (doesIdentityExist(identityName))
219 return;
220
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700221 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700223 sqlite3_prepare_v2(m_database,
224 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
225 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700227 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700228
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800229 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700230
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700231 sqlite3_finalize(statement);
232}
233
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700234bool
Yingdi Yu87581582014-01-14 14:28:39 -0800235SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700236{
237 //TODO:
238 return false;
239}
240
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700241bool
Yingdi Yu87581582014-01-14 14:28:39 -0800242SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700243{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700244 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800245 throw Error("Incorrect key name " + keyName.toUri());
246
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700247 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800248 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700249
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700250 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700251 sqlite3_prepare_v2(m_database,
252 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
253 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700254
255 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
256 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
257
258 int res = sqlite3_step(statement);
259
260 bool keyIdExist = false;
261 if (res == SQLITE_ROW) {
262 int countAll = sqlite3_column_int(statement, 0);
263 if (countAll > 0)
264 keyIdExist = true;
265 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700266
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700267 sqlite3_finalize(statement);
268
269 return keyIdExist;
270}
271
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272void
Yingdi Yu40b53092014-06-17 17:10:02 -0700273SecPublicInfoSqlite3::addKey(const Name& keyName,
274 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700275{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700276 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800277 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800278
Yingdi Yu05842f22014-04-15 19:21:56 -0700279 if (doesPublicKeyExist(keyName))
280 return;
281
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700282 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800283 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284
Yingdi Yu2e57a582014-02-20 23:34:43 -0800285 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700286
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700287 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700288 sqlite3_prepare_v2(m_database,
289 "INSERT OR REPLACE INTO Key \
290 (identity_name, key_identifier, key_type, public_key) \
291 values (?, ?, ?, ?)",
292 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700293
294 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
295 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700296 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700297 sqlite3_bind_blob(statement, 4,
298 publicKeyDer.get().buf(),
299 publicKeyDer.get().size(),
300 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700301
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800302 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700303
304 sqlite3_finalize(statement);
305}
306
Yingdi Yu2e57a582014-02-20 23:34:43 -0800307shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800308SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700309{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700310 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800311 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800312 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
313 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700314
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700315 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800316 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700317
318 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700319 sqlite3_prepare_v2(m_database,
320 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
321 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700322
323 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
324 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
325
326 int res = sqlite3_step(statement);
327
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700329 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800330 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700331 result =
332 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
333 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800334 sqlite3_finalize(statement);
335 return result;
336 }
337 else
338 {
339 sqlite3_finalize(statement);
340 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
341 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700342}
343
Yingdi Yu40b53092014-06-17 17:10:02 -0700344KeyType
345SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
346{
347 if (keyName.empty())
348 return KEY_TYPE_NULL;
349
350 string keyId = keyName.get(-1).toUri();
351 Name identityName = keyName.getPrefix(-1);
352
353 sqlite3_stmt* statement;
354 sqlite3_prepare_v2(m_database,
355 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
356 -1, &statement, 0);
357
358 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
359 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
360
361 int res = sqlite3_step(statement);
362
363 if (res == SQLITE_ROW)
364 {
365 int typeValue = sqlite3_column_int(statement, 0);
366 sqlite3_finalize(statement);
367 return static_cast<KeyType>(typeValue);
368 }
369 else
370 {
371 sqlite3_finalize(statement);
372 return KEY_TYPE_NULL;
373 }
374
375}
376
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700377bool
Yingdi Yu87581582014-01-14 14:28:39 -0800378SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700379{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700380 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700381 sqlite3_prepare_v2(m_database,
382 "SELECT count(*) FROM Certificate WHERE cert_name=?",
383 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700384
385 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
386
387 int res = sqlite3_step(statement);
388
389 bool certExist = false;
390 if (res == SQLITE_ROW) {
391 int countAll = sqlite3_column_int(statement, 0);
392 if (countAll > 0)
393 certExist = true;
394 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700395
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700397
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700398 return certExist;
399}
400
Yingdi Yu2e57a582014-02-20 23:34:43 -0800401// void
402// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
403// {
404// std::string certificateName = certificate.getName().toUri();
405// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800406
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700407// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800408// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700409
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700410// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800411// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700412
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700413// sqlite3_stmt* statement;
414// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700415// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
416// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800417// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700418
Yingdi Yu2e57a582014-02-20 23:34:43 -0800419// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700420
Yingdi Yu2e57a582014-02-20 23:34:43 -0800421// try
422// {
423// SignatureSha256WithRsa signature(certificate.getSignature());
424// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700425
Yingdi Yu2e57a582014-02-20 23:34:43 -0800426// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
427// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700428// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800429// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800430// return;
431// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700432// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800434// return;
435// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800436
Yingdi Yu2e57a582014-02-20 23:34:43 -0800437// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
438// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700439
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700440// // Convert from time::milliseconds to time::seconds since 1/1/1970.
441// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
442// time::toUnixTimestamp(certificate.getNotBefore()).count()));
443// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
444// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700445
Yingdi Yu2e57a582014-02-20 23:34:43 -0800446// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700447
Yingdi Yu2e57a582014-02-20 23:34:43 -0800448// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700449
Yingdi Yu2e57a582014-02-20 23:34:43 -0800450// sqlite3_finalize(statement);
451// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700452
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700453void
Yingdi Yu87581582014-01-14 14:28:39 -0800454SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700455{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700456 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700457 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700458 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700459 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460
Yingdi Yu40b53092014-06-17 17:10:02 -0700461 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700462
Yingdi Yu05842f22014-04-15 19:21:56 -0700463 if (doesCertificateExist(certificateName))
464 return;
465
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700466 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800467 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700468
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700470 sqlite3_stmt* statement;
471 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700472 "INSERT OR REPLACE INTO Certificate \
473 (cert_name, cert_issuer, identity_name, key_identifier, \
474 not_before, not_after, certificate_data) \
475 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700476 -1, &statement, 0);
477
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700478 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
479
Yingdi Yu2e57a582014-02-20 23:34:43 -0800480 try
481 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700482 // this will throw an exception if the signature is not the standard one
483 // or there is no key locator present
Yingdi Yu4a557052014-07-09 16:40:37 -0700484 std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
485 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800486 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700487 catch (Tlv::Error& e)
488 {
489 return;
490 }
491 catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800492 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800493 return;
494 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700495
496 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800497 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700498
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700499 sqlite3_bind_int64(statement, 5,
500 static_cast<sqlite3_int64>(
501 time::toUnixTimestamp(certificate.getNotBefore()).count()));
502 sqlite3_bind_int64(statement, 6,
503 static_cast<sqlite3_int64>(
504 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700505
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700506 sqlite3_bind_blob(statement, 7,
507 certificate.wireEncode().wire(),
508 certificate.wireEncode().size(),
509 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700510
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800511 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512
513 sqlite3_finalize(statement);
514}
515
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700516shared_ptr<IdentityCertificate>
517SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700518{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700519 sqlite3_stmt* statement;
520
521 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800522 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
523 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700524
Yingdi Yu4270f202014-01-28 14:19:16 -0800525 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700526
Yingdi Yu4270f202014-01-28 14:19:16 -0800527 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700528
Yingdi Yu4270f202014-01-28 14:19:16 -0800529 if (res == SQLITE_ROW)
530 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800531 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700532 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700533 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800534 sqlite3_finalize(statement);
535 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800536 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800537 else
538 {
539 sqlite3_finalize(statement);
540 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
541 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700542}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700543
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700544
545Name
Yingdi Yu87581582014-01-14 14:28:39 -0800546SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700547{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700548 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700549 sqlite3_prepare_v2(m_database,
550 "SELECT identity_name FROM Identity WHERE default_identity=1",
551 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700552
553 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700554
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800556 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700557 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
558 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800559 sqlite3_finalize(statement);
560 return identity;
561 }
562 else
563 {
564 sqlite3_finalize(statement);
565 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
566 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700567}
568
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700569void
Yingdi Yu87581582014-01-14 14:28:39 -0800570SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700571{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800572 addIdentity(identityName);
573
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700574 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700575
576 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700577 sqlite3_prepare_v2(m_database,
578 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
579 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700580
581 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700582 {
583 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700585 sqlite3_finalize(statement);
586
587 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700588 sqlite3_prepare_v2(m_database,
589 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
590 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700591
592 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700593
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700594 sqlite3_step(statement);
595
596 sqlite3_finalize(statement);
597}
598
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700599Name
Yingdi Yu87581582014-01-14 14:28:39 -0800600SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700601{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700602 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700603 sqlite3_prepare_v2(m_database,
604 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
605 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700606
Yingdi Yu87581582014-01-14 14:28:39 -0800607 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
608
609 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800610
611 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800612 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700613 Name keyName = identityName;
614 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
615 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800616 sqlite3_finalize(statement);
617 return keyName;
618 }
619 else
620 {
621 sqlite3_finalize(statement);
622 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
623 }
Yingdi Yu87581582014-01-14 14:28:39 -0800624}
625
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700626void
Yingdi Yu87581582014-01-14 14:28:39 -0800627SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
628{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700629 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700630 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800631
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700632 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800633 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700634
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700635 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700636
637 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700638 sqlite3_prepare_v2(m_database,
639 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
640 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700641
642 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
643
644 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700645 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700646
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700647 sqlite3_finalize(statement);
648
649 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700650 sqlite3_prepare_v2(m_database,
651 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
652 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700653
654 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
655 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700657 sqlite3_step(statement);
658
659 sqlite3_finalize(statement);
660}
661
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662Name
Yingdi Yu87581582014-01-14 14:28:39 -0800663SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700664{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700665 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800666 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800667
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700668 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800669 Name identityName = keyName.getPrefix(-1);
670
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700671 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700672 sqlite3_prepare_v2(m_database,
673 "SELECT cert_name FROM Certificate \
674 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
675 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800676
677 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
678 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
679
680 int res = sqlite3_step(statement);
681
Yingdi Yu87581582014-01-14 14:28:39 -0800682 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800683 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700684 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
685 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800686 sqlite3_finalize(statement);
687 return certName;
688 }
689 else
690 {
691 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700692 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800693 }
Yingdi Yu87581582014-01-14 14:28:39 -0800694}
695
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696void
Yingdi Yu87581582014-01-14 14:28:39 -0800697SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
698{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700699 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700700 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800701
Yingdi Yu2e57a582014-02-20 23:34:43 -0800702 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700703 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800704 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700705
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700706 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700707
708 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700709 sqlite3_prepare_v2(m_database,
710 "UPDATE Certificate SET default_cert=0 \
711 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
712 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700713
714 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
715 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
716
717 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700718 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700719
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700720 sqlite3_finalize(statement);
721
722 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700723 sqlite3_prepare_v2(m_database,
724 "UPDATE Certificate SET default_cert=1 \
725 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
726 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700727
728 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
729 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
730 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700731
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700732 sqlite3_step(statement);
733
734 sqlite3_finalize(statement);
735}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800736
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800737void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800739{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700740 sqlite3_stmt* stmt;
741 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700742 sqlite3_prepare_v2(m_database,
743 "SELECT identity_name FROM Identity WHERE default_identity=1",
744 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800745 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700746 sqlite3_prepare_v2(m_database,
747 "SELECT identity_name FROM Identity WHERE default_identity=0",
748 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800749
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700750 while (sqlite3_step(stmt) == SQLITE_ROW)
751 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
752 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700753
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700754 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700755}
756
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800757void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700758SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800759{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700760 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800761
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700762 if (isDefault)
763 sqlite3_prepare_v2(m_database,
764 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
765 -1, &stmt, 0);
766 else
767 sqlite3_prepare_v2(m_database,
768 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
769 -1, &stmt, 0);
770
771 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800772 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700773 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
774 sqlite3_column_bytes(stmt, 0)));
775 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
776 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800777 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700778 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700779 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800780}
781
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800782void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700783SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
784 vector<Name>& nameList,
785 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800786{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700787 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700788
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700789 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700790 sqlite3_prepare_v2(m_database,
791 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
792 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800793 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700794 sqlite3_prepare_v2(m_database,
795 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
796 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700797
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700798 sqlite3_bind_text(stmt, 1,
799 identity.toUri().c_str(),
800 identity.toUri().size(),
801 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800802
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700803 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800804 {
805 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700806 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
807 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800808 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700809 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700810 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800811}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700812
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800813void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700814SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800815{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700816 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700817
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700818 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700819 sqlite3_prepare_v2(m_database,
820 "SELECT cert_name FROM Certificate WHERE default_cert=1",
821 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800822 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700823 sqlite3_prepare_v2(m_database,
824 "SELECT cert_name FROM Certificate WHERE default_cert=0",
825 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800826
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700827 while (sqlite3_step(stmt) == SQLITE_ROW)
828 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
829 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800830
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700831 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800832}
833
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800834void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700835SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
836 vector<Name>& nameList,
837 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800838{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700839 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800840 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800841
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700842 sqlite3_stmt* stmt;
843 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700844 sqlite3_prepare_v2(m_database,
845 "SELECT cert_name FROM Certificate \
846 WHERE default_cert=1 and identity_name=? and key_identifier=?",
847 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800848 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700849 sqlite3_prepare_v2(m_database,
850 "SELECT cert_name FROM Certificate \
851 WHERE default_cert=0 and identity_name=? and key_identifier=?",
852 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800853
Yingdi Yu87581582014-01-14 14:28:39 -0800854 Name identity = keyName.getPrefix(-1);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700855 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700856
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700857 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800858 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
859
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700860 while (sqlite3_step(stmt) == SQLITE_ROW)
861 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
862 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800863
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700864 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800865}
866
867void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700868SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800869{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700870 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800871 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700872
873 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800874 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700875 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800876 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700877 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800878}
879
880void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700881SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800882{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700883 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800884 return;
885
886 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700887 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800888
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700889 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700890 sqlite3_prepare_v2(m_database,
891 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
892 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700893 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
895 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700896 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800897
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700898 sqlite3_prepare_v2(m_database,
899 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
900 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700901 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800902 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
903 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700904 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800905}
906
907void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700908SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800909{
910 string identity = identityName.toUri();
911
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700912 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800913 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700914 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800915 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700916 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800917
918 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700919 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800920 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700921 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800922
923 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700924 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800925 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700926 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800927}
928
929} // namespace ndn