blob: 354c5aa772f6ef0f41aba412846c45be989b572b [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 Yuc8f883c2014-06-20 23:25:22 -0700484 switch (certificate.getSignature().getType())
485 {
486 case Tlv::SignatureSha256WithRsa:
487 {
488 SignatureSha256WithRsa signature(certificate.getSignature());
489 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800490
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700491 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
492 break;
493 }
494 case Tlv::SignatureSha256WithEcdsa:
495 {
496 SignatureSha256WithEcdsa signature(certificate.getSignature());
497 std::string signerName = signature.getKeyLocator().getName().toUri();
498
499 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
500 break;
501 }
502 default:
503 return;
504 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800505 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700506 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800507 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800508 return;
509 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700510
511 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800512 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700513
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700514 sqlite3_bind_int64(statement, 5,
515 static_cast<sqlite3_int64>(
516 time::toUnixTimestamp(certificate.getNotBefore()).count()));
517 sqlite3_bind_int64(statement, 6,
518 static_cast<sqlite3_int64>(
519 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700520
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700521 sqlite3_bind_blob(statement, 7,
522 certificate.wireEncode().wire(),
523 certificate.wireEncode().size(),
524 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700525
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800526 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700527
528 sqlite3_finalize(statement);
529}
530
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700531shared_ptr<IdentityCertificate>
532SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700533{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700534 sqlite3_stmt* statement;
535
536 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800537 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
538 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700539
Yingdi Yu4270f202014-01-28 14:19:16 -0800540 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700541
Yingdi Yu4270f202014-01-28 14:19:16 -0800542 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700543
Yingdi Yu4270f202014-01-28 14:19:16 -0800544 if (res == SQLITE_ROW)
545 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800546 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700547 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700548 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800549 sqlite3_finalize(statement);
550 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800551 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800552 else
553 {
554 sqlite3_finalize(statement);
555 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
556 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700557}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700558
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700559
560Name
Yingdi Yu87581582014-01-14 14:28:39 -0800561SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700562{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700563 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700564 sqlite3_prepare_v2(m_database,
565 "SELECT identity_name FROM Identity WHERE default_identity=1",
566 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700567
568 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700569
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700570 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800571 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700572 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
573 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800574 sqlite3_finalize(statement);
575 return identity;
576 }
577 else
578 {
579 sqlite3_finalize(statement);
580 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
581 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700582}
583
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700584void
Yingdi Yu87581582014-01-14 14:28:39 -0800585SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700586{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800587 addIdentity(identityName);
588
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700589 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700590
591 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700592 sqlite3_prepare_v2(m_database,
593 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
594 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700595
596 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700597 {
598 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700599
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700600 sqlite3_finalize(statement);
601
602 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700603 sqlite3_prepare_v2(m_database,
604 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
605 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700606
607 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700608
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700609 sqlite3_step(statement);
610
611 sqlite3_finalize(statement);
612}
613
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700614Name
Yingdi Yu87581582014-01-14 14:28:39 -0800615SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700616{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700617 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700618 sqlite3_prepare_v2(m_database,
619 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
620 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700621
Yingdi Yu87581582014-01-14 14:28:39 -0800622 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
623
624 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800625
626 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800627 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700628 Name keyName = identityName;
629 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
630 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800631 sqlite3_finalize(statement);
632 return keyName;
633 }
634 else
635 {
636 sqlite3_finalize(statement);
637 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
638 }
Yingdi Yu87581582014-01-14 14:28:39 -0800639}
640
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700641void
Yingdi Yu87581582014-01-14 14:28:39 -0800642SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
643{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700644 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700645 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800646
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700647 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800648 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700649
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700650 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700651
652 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700653 sqlite3_prepare_v2(m_database,
654 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
655 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700656
657 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
658
659 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700660 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700661
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700662 sqlite3_finalize(statement);
663
664 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700665 sqlite3_prepare_v2(m_database,
666 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
667 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700668
669 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
670 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700671
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700672 sqlite3_step(statement);
673
674 sqlite3_finalize(statement);
675}
676
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700677Name
Yingdi Yu87581582014-01-14 14:28:39 -0800678SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700679{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700680 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800681 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800682
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700683 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800684 Name identityName = keyName.getPrefix(-1);
685
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700686 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700687 sqlite3_prepare_v2(m_database,
688 "SELECT cert_name FROM Certificate \
689 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
690 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800691
692 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
693 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
694
695 int res = sqlite3_step(statement);
696
Yingdi Yu87581582014-01-14 14:28:39 -0800697 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800698 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700699 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
700 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800701 sqlite3_finalize(statement);
702 return certName;
703 }
704 else
705 {
706 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700707 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800708 }
Yingdi Yu87581582014-01-14 14:28:39 -0800709}
710
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700711void
Yingdi Yu87581582014-01-14 14:28:39 -0800712SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
713{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700714 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700715 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800716
Yingdi Yu2e57a582014-02-20 23:34:43 -0800717 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700718 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800719 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700720
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700721 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700722
723 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700724 sqlite3_prepare_v2(m_database,
725 "UPDATE Certificate SET default_cert=0 \
726 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
727 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700728
729 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
730 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
731
732 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700733 ;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700734
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700735 sqlite3_finalize(statement);
736
737 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700738 sqlite3_prepare_v2(m_database,
739 "UPDATE Certificate SET default_cert=1 \
740 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
741 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700742
743 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
744 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
745 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700746
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700747 sqlite3_step(statement);
748
749 sqlite3_finalize(statement);
750}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800751
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800752void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700753SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800754{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700755 sqlite3_stmt* stmt;
756 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700757 sqlite3_prepare_v2(m_database,
758 "SELECT identity_name FROM Identity WHERE default_identity=1",
759 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800760 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700761 sqlite3_prepare_v2(m_database,
762 "SELECT identity_name FROM Identity WHERE default_identity=0",
763 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800764
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700765 while (sqlite3_step(stmt) == SQLITE_ROW)
766 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
767 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700768
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700769 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700770}
771
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800772void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700773SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800774{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700775 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800776
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700777 if (isDefault)
778 sqlite3_prepare_v2(m_database,
779 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
780 -1, &stmt, 0);
781 else
782 sqlite3_prepare_v2(m_database,
783 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
784 -1, &stmt, 0);
785
786 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800787 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700788 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
789 sqlite3_column_bytes(stmt, 0)));
790 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
791 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800792 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700793 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700794 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800795}
796
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800797void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700798SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
799 vector<Name>& nameList,
800 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800801{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700802 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700803
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700804 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700805 sqlite3_prepare_v2(m_database,
806 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
807 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800808 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700809 sqlite3_prepare_v2(m_database,
810 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
811 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700812
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700813 sqlite3_bind_text(stmt, 1,
814 identity.toUri().c_str(),
815 identity.toUri().size(),
816 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800817
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700818 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800819 {
820 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700821 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
822 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800823 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700824 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700825 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800826}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700827
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800828void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700829SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800830{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700831 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700832
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700833 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700834 sqlite3_prepare_v2(m_database,
835 "SELECT cert_name FROM Certificate WHERE default_cert=1",
836 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800837 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700838 sqlite3_prepare_v2(m_database,
839 "SELECT cert_name FROM Certificate WHERE default_cert=0",
840 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800841
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700842 while (sqlite3_step(stmt) == SQLITE_ROW)
843 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
844 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800845
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700846 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800847}
848
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800849void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700850SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
851 vector<Name>& nameList,
852 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800853{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700854 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800855 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800856
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700857 sqlite3_stmt* stmt;
858 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700859 sqlite3_prepare_v2(m_database,
860 "SELECT cert_name FROM Certificate \
861 WHERE default_cert=1 and identity_name=? and key_identifier=?",
862 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800863 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700864 sqlite3_prepare_v2(m_database,
865 "SELECT cert_name FROM Certificate \
866 WHERE default_cert=0 and identity_name=? and key_identifier=?",
867 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800868
Yingdi Yu87581582014-01-14 14:28:39 -0800869 Name identity = keyName.getPrefix(-1);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700870 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700871
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700872 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800873 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
874
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700875 while (sqlite3_step(stmt) == SQLITE_ROW)
876 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
877 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800878
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700879 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800880}
881
882void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700883SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800884{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700885 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800886 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700887
888 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800889 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700890 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800891 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700892 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800893}
894
895void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700896SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800897{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700898 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800899 return;
900
901 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700902 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800903
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700904 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700905 sqlite3_prepare_v2(m_database,
906 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
907 -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700908 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800909 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
910 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700911 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800912
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700913 sqlite3_prepare_v2(m_database,
914 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
915 -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_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
918 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700919 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800920}
921
922void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700923SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800924{
925 string identity = identityName.toUri();
926
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700927 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800928 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700929 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800930 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700931 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800932
933 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700934 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800935 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700936 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800937
938 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700939 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800940 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700941 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800942}
943
944} // namespace ndn