blob: a690beff5c71dfc78baaa793174611a9eee1f96c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson7ca11f22013-10-04 19:01:30 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070023 */
24
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "common.hpp"
26
27#include "sec-public-info-sqlite3.hpp"
28#include "identity-certificate.hpp"
29#include "signature-sha256-with-rsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080030#include "../data.hpp"
31
Yingdi Yu874678f2014-01-22 19:30:34 -080032#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070033#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070034#include <stdlib.h>
35#include <sstream>
36#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080037#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080038
Jeff Thompson7ca11f22013-10-04 19:01:30 -070039using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070040
Yingdi Yufc40d872014-02-18 12:56:04 -080041namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070042
43static const string INIT_ID_TABLE = "\
44CREATE TABLE IF NOT EXISTS \n \
45 Identity( \n \
46 identity_name BLOB NOT NULL, \n \
47 default_identity INTEGER DEFAULT 0, \n \
48 \
49 PRIMARY KEY (identity_name) \n \
50 ); \n \
51 \
52CREATE INDEX identity_index ON Identity(identity_name); \n \
53";
54
55static const string INIT_KEY_TABLE = "\
56CREATE TABLE IF NOT EXISTS \n \
57 Key( \n \
58 identity_name BLOB NOT NULL, \n \
59 key_identifier BLOB NOT NULL, \n \
60 key_type INTEGER, \n \
61 public_key BLOB, \n \
62 default_key INTEGER DEFAULT 0, \n \
63 active INTEGER DEFAULT 0, \n \
64 \
65 PRIMARY KEY (identity_name, key_identifier) \n \
66 ); \n \
67 \
68CREATE INDEX key_index ON Key(identity_name); \n \
69";
70
71static const string INIT_CERT_TABLE = "\
72CREATE TABLE IF NOT EXISTS \n \
73 Certificate( \n \
74 cert_name BLOB NOT NULL, \n \
75 cert_issuer BLOB NOT NULL, \n \
76 identity_name BLOB NOT NULL, \n \
77 key_identifier BLOB NOT NULL, \n \
78 not_before TIMESTAMP, \n \
79 not_after TIMESTAMP, \n \
80 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070081 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070082 default_cert INTEGER DEFAULT 0, \n \
83 \
84 PRIMARY KEY (cert_name) \n \
85 ); \n \
86 \
87CREATE INDEX cert_index ON Certificate(cert_name); \n \
88CREATE INDEX subject ON Certificate(identity_name); \n \
89";
90
91/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 * A utility function to call the normal sqlite3_bind_text where the value and length are
93 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070094 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095static int sqlite3_bind_text(sqlite3_stmt* statement,
96 int index,
97 const string& value,
98 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -070099{
100 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
101}
102
Yingdi Yu87581582014-01-14 14:28:39 -0800103SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700104{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700105 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -0800106 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700107
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800108 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
109 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
110 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700111#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800112 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800113#else
114 0
115#endif
116 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700117 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800118 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700120 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700122 sqlite3_prepare_v2(m_database,
123 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
124 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700125 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700126
127 bool idTableExists = false;
128 if (res == SQLITE_ROW)
129 idTableExists = true;
130
131 sqlite3_finalize(statement);
132
133 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800135 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700137 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700138 sqlite3_free(errorMessage);
139 }
140 }
141
142 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 sqlite3_prepare_v2(m_database,
144 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
145 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700146 res = sqlite3_step(statement);
147
148 bool keyTableExists = false;
149 if (res == SQLITE_ROW)
150 keyTableExists = true;
151
152 sqlite3_finalize(statement);
153
154 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800156 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700158 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700159 sqlite3_free(errorMessage);
160 }
161 }
162
163 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700164 sqlite3_prepare_v2(m_database,
165 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
166 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700167 res = sqlite3_step(statement);
168
169 bool idCertificateTableExists = false;
170 if (res == SQLITE_ROW)
171 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700172
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700173 sqlite3_finalize(statement);
174
175 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700176 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800177 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700179 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700180 sqlite3_free(errorMessage);
181 }
182 }
183}
184
Yingdi Yu87581582014-01-14 14:28:39 -0800185SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700186{
187}
188
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700189bool
Yingdi Yu87581582014-01-14 14:28:39 -0800190SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700191{
192 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193
194 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700195 sqlite3_prepare_v2(m_database,
196 "SELECT count(*) FROM Identity WHERE identity_name=?",
197 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700198
199 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
200 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700202 if (res == SQLITE_ROW) {
203 int countAll = sqlite3_column_int(statement, 0);
204 if (countAll > 0)
205 result = true;
206 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700208 sqlite3_finalize(statement);
209
210 return result;
211}
212
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700213void
Yingdi Yu87581582014-01-14 14:28:39 -0800214SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700215{
Yingdi Yu05842f22014-04-15 19:21:56 -0700216 if (doesIdentityExist(identityName))
217 return;
218
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700221 sqlite3_prepare_v2(m_database,
222 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
223 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800227 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700228
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700229 sqlite3_finalize(statement);
230}
231
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700232bool
Yingdi Yu87581582014-01-14 14:28:39 -0800233SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700234{
235 //TODO:
236 return false;
237}
238
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700239bool
Yingdi Yu87581582014-01-14 14:28:39 -0800240SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700241{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700242 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800243 throw Error("Incorrect key name " + keyName.toUri());
244
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700245 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800246 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700247
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700248 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700249 sqlite3_prepare_v2(m_database,
250 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
251 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700252
253 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
254 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
255
256 int res = sqlite3_step(statement);
257
258 bool keyIdExist = false;
259 if (res == SQLITE_ROW) {
260 int countAll = sqlite3_column_int(statement, 0);
261 if (countAll > 0)
262 keyIdExist = true;
263 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700264
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700265 sqlite3_finalize(statement);
266
267 return keyIdExist;
268}
269
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700270void
Yingdi Yu40b53092014-06-17 17:10:02 -0700271SecPublicInfoSqlite3::addKey(const Name& keyName,
272 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700273{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700274 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800275 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800276
Yingdi Yu05842f22014-04-15 19:21:56 -0700277 if (doesPublicKeyExist(keyName))
278 return;
279
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700280 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800281 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700282
Yingdi Yu2e57a582014-02-20 23:34:43 -0800283 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700285 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700286 sqlite3_prepare_v2(m_database,
287 "INSERT OR REPLACE INTO Key \
288 (identity_name, key_identifier, key_type, public_key) \
289 values (?, ?, ?, ?)",
290 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700291
292 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
293 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700294 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700295 sqlite3_bind_blob(statement, 4,
296 publicKeyDer.get().buf(),
297 publicKeyDer.get().size(),
298 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700299
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800300 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700301
302 sqlite3_finalize(statement);
303}
304
Yingdi Yu2e57a582014-02-20 23:34:43 -0800305shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800306SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700307{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700308 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800309 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800310 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
311 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700312
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700313 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800314 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700315
316 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700317 sqlite3_prepare_v2(m_database,
318 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
319 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700320
321 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
322 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
323
324 int res = sqlite3_step(statement);
325
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700327 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700329 result =
330 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
331 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332 sqlite3_finalize(statement);
333 return result;
334 }
335 else
336 {
337 sqlite3_finalize(statement);
338 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
339 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700340}
341
Yingdi Yu40b53092014-06-17 17:10:02 -0700342KeyType
343SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
344{
345 if (keyName.empty())
346 return KEY_TYPE_NULL;
347
348 string keyId = keyName.get(-1).toUri();
349 Name identityName = keyName.getPrefix(-1);
350
351 sqlite3_stmt* statement;
352 sqlite3_prepare_v2(m_database,
353 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
354 -1, &statement, 0);
355
356 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
357 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
358
359 int res = sqlite3_step(statement);
360
361 if (res == SQLITE_ROW)
362 {
363 int typeValue = sqlite3_column_int(statement, 0);
364 sqlite3_finalize(statement);
365 return static_cast<KeyType>(typeValue);
366 }
367 else
368 {
369 sqlite3_finalize(statement);
370 return KEY_TYPE_NULL;
371 }
372
373}
374
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375bool
Yingdi Yu87581582014-01-14 14:28:39 -0800376SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700377{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700378 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700379 sqlite3_prepare_v2(m_database,
380 "SELECT count(*) FROM Certificate WHERE cert_name=?",
381 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700382
383 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
384
385 int res = sqlite3_step(statement);
386
387 bool certExist = false;
388 if (res == SQLITE_ROW) {
389 int countAll = sqlite3_column_int(statement, 0);
390 if (countAll > 0)
391 certExist = true;
392 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700393
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700394 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700395
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396 return certExist;
397}
398
Yingdi Yu2e57a582014-02-20 23:34:43 -0800399// void
400// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
401// {
402// std::string certificateName = certificate.getName().toUri();
403// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800404
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700405// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800406// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700407
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700408// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800409// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700410
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700411// sqlite3_stmt* statement;
412// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700413// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
414// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800415// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700416
Yingdi Yu2e57a582014-02-20 23:34:43 -0800417// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700418
Yingdi Yu2e57a582014-02-20 23:34:43 -0800419// try
420// {
421// SignatureSha256WithRsa signature(certificate.getSignature());
422// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700423
Yingdi Yu2e57a582014-02-20 23:34:43 -0800424// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
425// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700426// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800427// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800428// return;
429// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700430// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800431// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800432// return;
433// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800434
Yingdi Yu2e57a582014-02-20 23:34:43 -0800435// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
436// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700437
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700438// // Convert from time::milliseconds to time::seconds since 1/1/1970.
439// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
440// time::toUnixTimestamp(certificate.getNotBefore()).count()));
441// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
442// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700443
Yingdi Yu2e57a582014-02-20 23:34:43 -0800444// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700445
Yingdi Yu2e57a582014-02-20 23:34:43 -0800446// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700447
Yingdi Yu2e57a582014-02-20 23:34:43 -0800448// sqlite3_finalize(statement);
449// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700450
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700451void
Yingdi Yu87581582014-01-14 14:28:39 -0800452SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700453{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700454 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700455 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700456 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700457 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700458
Yingdi Yu40b53092014-06-17 17:10:02 -0700459 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460
Yingdi Yu05842f22014-04-15 19:21:56 -0700461 if (doesCertificateExist(certificateName))
462 return;
463
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700464 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800465 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700466
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700467 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468 sqlite3_stmt* statement;
469 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700470 "INSERT OR REPLACE INTO Certificate \
471 (cert_name, cert_issuer, identity_name, key_identifier, \
472 not_before, not_after, certificate_data) \
473 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700474 -1, &statement, 0);
475
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700476 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
477
Yingdi Yu2e57a582014-02-20 23:34:43 -0800478 try
479 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700480 // this will throw an exception if the signature is not the standard one
481 // or there is no key locator present
Yingdi Yu2e57a582014-02-20 23:34:43 -0800482 SignatureSha256WithRsa signature(certificate.getSignature());
483 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800484
Yingdi Yu2e57a582014-02-20 23:34:43 -0800485 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
486 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700487 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800488 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800489 return;
490 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700491
492 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800493 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700494
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700495 sqlite3_bind_int64(statement, 5,
496 static_cast<sqlite3_int64>(
497 time::toUnixTimestamp(certificate.getNotBefore()).count()));
498 sqlite3_bind_int64(statement, 6,
499 static_cast<sqlite3_int64>(
500 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700501
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700502 sqlite3_bind_blob(statement, 7,
503 certificate.wireEncode().wire(),
504 certificate.wireEncode().size(),
505 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700506
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800507 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700508
509 sqlite3_finalize(statement);
510}
511
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700512shared_ptr<IdentityCertificate>
513SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700514{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700515 sqlite3_stmt* statement;
516
517 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800518 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
519 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700520
Yingdi Yu4270f202014-01-28 14:19:16 -0800521 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700522
Yingdi Yu4270f202014-01-28 14:19:16 -0800523 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700524
Yingdi Yu4270f202014-01-28 14:19:16 -0800525 if (res == SQLITE_ROW)
526 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800527 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700528 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700529 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800530 sqlite3_finalize(statement);
531 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800532 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800533 else
534 {
535 sqlite3_finalize(statement);
536 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
537 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700538}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700539
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700540
541Name
Yingdi Yu87581582014-01-14 14:28:39 -0800542SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700543{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700544 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700545 sqlite3_prepare_v2(m_database,
546 "SELECT identity_name FROM Identity WHERE default_identity=1",
547 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700548
549 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700550
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700551 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800552 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700553 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
554 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800555 sqlite3_finalize(statement);
556 return identity;
557 }
558 else
559 {
560 sqlite3_finalize(statement);
561 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
562 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700563}
564
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700565void
Yingdi Yu87581582014-01-14 14:28:39 -0800566SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700567{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800568 addIdentity(identityName);
569
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700570 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700571
572 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700573 sqlite3_prepare_v2(m_database,
574 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
575 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700576
577 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700578 {
579 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700580
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700581 sqlite3_finalize(statement);
582
583 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700584 sqlite3_prepare_v2(m_database,
585 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
586 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700587
588 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700589
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700590 sqlite3_step(statement);
591
592 sqlite3_finalize(statement);
593}
594
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700595Name
Yingdi Yu87581582014-01-14 14:28:39 -0800596SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700597{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700598 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700599 sqlite3_prepare_v2(m_database,
600 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
601 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700602
Yingdi Yu87581582014-01-14 14:28:39 -0800603 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
604
605 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800606
607 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800608 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700609 Name keyName = identityName;
610 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
611 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800612 sqlite3_finalize(statement);
613 return keyName;
614 }
615 else
616 {
617 sqlite3_finalize(statement);
618 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
619 }
Yingdi Yu87581582014-01-14 14:28:39 -0800620}
621
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700622void
Yingdi Yu87581582014-01-14 14:28:39 -0800623SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
624{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700625 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700626 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800627
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700628 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800629 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700630
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700631 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700632
633 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700634 sqlite3_prepare_v2(m_database,
635 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
636 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700637
638 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
639
640 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700641 {
642 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700643
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700644 sqlite3_finalize(statement);
645
646 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700647 sqlite3_prepare_v2(m_database,
648 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
649 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700650
651 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
652 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700653
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700654 sqlite3_step(statement);
655
656 sqlite3_finalize(statement);
657}
658
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700659Name
Yingdi Yu87581582014-01-14 14:28:39 -0800660SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700661{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700662 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800663 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800664
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700665 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800666 Name identityName = keyName.getPrefix(-1);
667
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700668 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700669 sqlite3_prepare_v2(m_database,
670 "SELECT cert_name FROM Certificate \
671 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
672 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800673
674 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
675 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
676
677 int res = sqlite3_step(statement);
678
Yingdi Yu87581582014-01-14 14:28:39 -0800679 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800680 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700681 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
682 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800683 sqlite3_finalize(statement);
684 return certName;
685 }
686 else
687 {
688 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700689 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800690 }
Yingdi Yu87581582014-01-14 14:28:39 -0800691}
692
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700693void
Yingdi Yu87581582014-01-14 14:28:39 -0800694SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
695{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700697 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800698
Yingdi Yu2e57a582014-02-20 23:34:43 -0800699 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700700 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800701 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700702
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700703 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700704
705 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700706 sqlite3_prepare_v2(m_database,
707 "UPDATE Certificate SET default_cert=0 \
708 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
709 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700710
711 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
712 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
713
714 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700715 {
716 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700717
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700718 sqlite3_finalize(statement);
719
720 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700721 sqlite3_prepare_v2(m_database,
722 "UPDATE Certificate SET default_cert=1 \
723 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
724 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700725
726 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
727 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
728 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700729
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700730 sqlite3_step(statement);
731
732 sqlite3_finalize(statement);
733}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800734
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800735void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700736SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800737{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700738 sqlite3_stmt* stmt;
739 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700740 sqlite3_prepare_v2(m_database,
741 "SELECT identity_name FROM Identity WHERE default_identity=1",
742 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800743 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700744 sqlite3_prepare_v2(m_database,
745 "SELECT identity_name FROM Identity WHERE default_identity=0",
746 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800747
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700748 while (sqlite3_step(stmt) == SQLITE_ROW)
749 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
750 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700751
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700752 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700753}
754
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800755void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700756SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800757{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700758 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800759
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700760 if (isDefault)
761 sqlite3_prepare_v2(m_database,
762 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
763 -1, &stmt, 0);
764 else
765 sqlite3_prepare_v2(m_database,
766 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
767 -1, &stmt, 0);
768
769 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800770 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700771 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
772 sqlite3_column_bytes(stmt, 0)));
773 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
774 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800775 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700776 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700777 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800778}
779
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800780void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700781SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
782 vector<Name>& nameList,
783 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800784{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700785 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700786
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700787 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700788 sqlite3_prepare_v2(m_database,
789 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
790 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800791 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700792 sqlite3_prepare_v2(m_database,
793 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
794 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700795
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700796 sqlite3_bind_text(stmt, 1,
797 identity.toUri().c_str(),
798 identity.toUri().size(),
799 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800800
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700801 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800802 {
803 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700804 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
805 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800806 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700807 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700808 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800809}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700810
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800811void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700812SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800813{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700814 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700815
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700816 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700817 sqlite3_prepare_v2(m_database,
818 "SELECT cert_name FROM Certificate WHERE default_cert=1",
819 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800820 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700821 sqlite3_prepare_v2(m_database,
822 "SELECT cert_name FROM Certificate WHERE default_cert=0",
823 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800824
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700825 while (sqlite3_step(stmt) == SQLITE_ROW)
826 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
827 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800828
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700829 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800830}
831
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800832void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700833SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
834 vector<Name>& nameList,
835 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800836{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700837 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800838 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800839
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700840 sqlite3_stmt* stmt;
841 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700842 sqlite3_prepare_v2(m_database,
843 "SELECT cert_name FROM Certificate \
844 WHERE default_cert=1 and identity_name=? and key_identifier=?",
845 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800846 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700847 sqlite3_prepare_v2(m_database,
848 "SELECT cert_name FROM Certificate \
849 WHERE default_cert=0 and identity_name=? and key_identifier=?",
850 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800851
Yingdi Yu87581582014-01-14 14:28:39 -0800852 Name identity = keyName.getPrefix(-1);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700853 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
854
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700855 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800856 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
857
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700858 while (sqlite3_step(stmt) == SQLITE_ROW)
859 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
860 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800861
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700862 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800863}
864
865void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700866SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800867{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700868 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800869 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700870
871 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800872 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700873 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800874 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700875 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800876}
877
878void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700879SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800880{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700881 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800882 return;
883
884 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700885 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800886
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700887 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700888 sqlite3_prepare_v2(m_database,
889 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
890 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800891 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
892 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
893 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700894 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800895
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700896 sqlite3_prepare_v2(m_database,
897 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
898 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800899 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
900 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
901 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700902 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800903}
904
905void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700906SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800907{
908 string identity = identityName.toUri();
909
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700910 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800911 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
912 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
913 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700914 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800915
916 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
917 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.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 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
922 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
923 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700924 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800925}
926
927} // namespace ndn