blob: 7e86773fb11ebf24183778d22ed5f58e0d0452e6 [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
Jeff Thompson7ca11f22013-10-04 19:01:30 -070040using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070041
Yingdi Yufc40d872014-02-18 12:56:04 -080042namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070043
44static const string INIT_ID_TABLE = "\
45CREATE TABLE IF NOT EXISTS \n \
46 Identity( \n \
47 identity_name BLOB NOT NULL, \n \
48 default_identity INTEGER DEFAULT 0, \n \
49 \
50 PRIMARY KEY (identity_name) \n \
51 ); \n \
52 \
53CREATE INDEX identity_index ON Identity(identity_name); \n \
54";
55
56static const string INIT_KEY_TABLE = "\
57CREATE TABLE IF NOT EXISTS \n \
58 Key( \n \
59 identity_name BLOB NOT NULL, \n \
60 key_identifier BLOB NOT NULL, \n \
61 key_type INTEGER, \n \
62 public_key BLOB, \n \
63 default_key INTEGER DEFAULT 0, \n \
64 active INTEGER DEFAULT 0, \n \
65 \
66 PRIMARY KEY (identity_name, key_identifier) \n \
67 ); \n \
68 \
69CREATE INDEX key_index ON Key(identity_name); \n \
70";
71
72static const string INIT_CERT_TABLE = "\
73CREATE TABLE IF NOT EXISTS \n \
74 Certificate( \n \
75 cert_name BLOB NOT NULL, \n \
76 cert_issuer BLOB NOT NULL, \n \
77 identity_name BLOB NOT NULL, \n \
78 key_identifier BLOB NOT NULL, \n \
79 not_before TIMESTAMP, \n \
80 not_after TIMESTAMP, \n \
81 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070082 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070083 default_cert INTEGER DEFAULT 0, \n \
84 \
85 PRIMARY KEY (cert_name) \n \
86 ); \n \
87 \
88CREATE INDEX cert_index ON Certificate(cert_name); \n \
89CREATE INDEX subject ON Certificate(identity_name); \n \
90";
91
92/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070093 * A utility function to call the normal sqlite3_bind_text where the value and length are
94 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070095 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070096static int sqlite3_bind_text(sqlite3_stmt* statement,
97 int index,
98 const string& value,
99 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700100{
101 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
102}
103
Yingdi Yu87581582014-01-14 14:28:39 -0800104SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700105{
Yingdi Yu37e317f2014-03-19 12:16:23 -0700106 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -0800107 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700108
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800109 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
110 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
111 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700112#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800113 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800114#else
115 0
116#endif
117 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700118 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800119 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700121 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700123 sqlite3_prepare_v2(m_database,
124 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
125 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700126 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700127
128 bool idTableExists = false;
129 if (res == SQLITE_ROW)
130 idTableExists = true;
131
132 sqlite3_finalize(statement);
133
134 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800136 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700138 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700139 sqlite3_free(errorMessage);
140 }
141 }
142
143 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700144 sqlite3_prepare_v2(m_database,
145 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
146 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700147 res = sqlite3_step(statement);
148
149 bool keyTableExists = false;
150 if (res == SQLITE_ROW)
151 keyTableExists = true;
152
153 sqlite3_finalize(statement);
154
155 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700159 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700160 sqlite3_free(errorMessage);
161 }
162 }
163
164 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700165 sqlite3_prepare_v2(m_database,
166 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
167 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700168 res = sqlite3_step(statement);
169
170 bool idCertificateTableExists = false;
171 if (res == SQLITE_ROW)
172 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700173
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700174 sqlite3_finalize(statement);
175
176 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800178 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700179
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700180 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700181 sqlite3_free(errorMessage);
182 }
183 }
184}
185
Yingdi Yu87581582014-01-14 14:28:39 -0800186SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700187{
188}
189
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190bool
Yingdi Yu87581582014-01-14 14:28:39 -0800191SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700192{
193 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700194
195 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700196 sqlite3_prepare_v2(m_database,
197 "SELECT count(*) FROM Identity WHERE identity_name=?",
198 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700199
200 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
201 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203 if (res == SQLITE_ROW) {
204 int countAll = sqlite3_column_int(statement, 0);
205 if (countAll > 0)
206 result = true;
207 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700209 sqlite3_finalize(statement);
210
211 return result;
212}
213
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700214void
Yingdi Yu87581582014-01-14 14:28:39 -0800215SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700216{
Yingdi Yu05842f22014-04-15 19:21:56 -0700217 if (doesIdentityExist(identityName))
218 return;
219
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700221
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700222 sqlite3_prepare_v2(m_database,
223 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
224 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700225
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700226 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700227
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800228 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700229
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700230 sqlite3_finalize(statement);
231}
232
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700233bool
Yingdi Yu87581582014-01-14 14:28:39 -0800234SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700235{
236 //TODO:
237 return false;
238}
239
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700240bool
Yingdi Yu87581582014-01-14 14:28:39 -0800241SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700242{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700243 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800244 throw Error("Incorrect key name " + keyName.toUri());
245
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700246 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800247 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700248
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700249 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700250 sqlite3_prepare_v2(m_database,
251 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
252 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700253
254 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
255 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
256
257 int res = sqlite3_step(statement);
258
259 bool keyIdExist = false;
260 if (res == SQLITE_ROW) {
261 int countAll = sqlite3_column_int(statement, 0);
262 if (countAll > 0)
263 keyIdExist = true;
264 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700265
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700266 sqlite3_finalize(statement);
267
268 return keyIdExist;
269}
270
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700271void
Yingdi Yu40b53092014-06-17 17:10:02 -0700272SecPublicInfoSqlite3::addKey(const Name& keyName,
273 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700274{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700275 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800276 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800277
Yingdi Yu05842f22014-04-15 19:21:56 -0700278 if (doesPublicKeyExist(keyName))
279 return;
280
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700281 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800282 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700283
Yingdi Yu2e57a582014-02-20 23:34:43 -0800284 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700285
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700286 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700287 sqlite3_prepare_v2(m_database,
288 "INSERT OR REPLACE INTO Key \
289 (identity_name, key_identifier, key_type, public_key) \
290 values (?, ?, ?, ?)",
291 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700292
293 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
294 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Yingdi Yu40b53092014-06-17 17:10:02 -0700295 sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700296 sqlite3_bind_blob(statement, 4,
297 publicKeyDer.get().buf(),
298 publicKeyDer.get().size(),
299 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700300
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800301 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700302
303 sqlite3_finalize(statement);
304}
305
Yingdi Yu2e57a582014-02-20 23:34:43 -0800306shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800307SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700308{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700309 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800310 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800311 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
312 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700313
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700314 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800315 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700316
317 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700318 sqlite3_prepare_v2(m_database,
319 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
320 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700321
322 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
323 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
324
325 int res = sqlite3_step(statement);
326
Yingdi Yu2e57a582014-02-20 23:34:43 -0800327 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700328 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800329 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700330 result =
331 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
332 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 sqlite3_finalize(statement);
334 return result;
335 }
336 else
337 {
338 sqlite3_finalize(statement);
339 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
340 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700341}
342
Yingdi Yu40b53092014-06-17 17:10:02 -0700343KeyType
344SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName)
345{
346 if (keyName.empty())
347 return KEY_TYPE_NULL;
348
349 string keyId = keyName.get(-1).toUri();
350 Name identityName = keyName.getPrefix(-1);
351
352 sqlite3_stmt* statement;
353 sqlite3_prepare_v2(m_database,
354 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
355 -1, &statement, 0);
356
357 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
358 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
359
360 int res = sqlite3_step(statement);
361
362 if (res == SQLITE_ROW)
363 {
364 int typeValue = sqlite3_column_int(statement, 0);
365 sqlite3_finalize(statement);
366 return static_cast<KeyType>(typeValue);
367 }
368 else
369 {
370 sqlite3_finalize(statement);
371 return KEY_TYPE_NULL;
372 }
373
374}
375
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700376bool
Yingdi Yu87581582014-01-14 14:28:39 -0800377SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700378{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700379 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700380 sqlite3_prepare_v2(m_database,
381 "SELECT count(*) FROM Certificate WHERE cert_name=?",
382 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383
384 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
385
386 int res = sqlite3_step(statement);
387
388 bool certExist = false;
389 if (res == SQLITE_ROW) {
390 int countAll = sqlite3_column_int(statement, 0);
391 if (countAll > 0)
392 certExist = true;
393 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700394
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700396
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700397 return certExist;
398}
399
Yingdi Yu2e57a582014-02-20 23:34:43 -0800400// void
401// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
402// {
403// std::string certificateName = certificate.getName().toUri();
404// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800405
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700406// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800407// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700408
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700409// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800410// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700411
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700412// sqlite3_stmt* statement;
413// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700414// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
415// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800416// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700417
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700419
Yingdi Yu2e57a582014-02-20 23:34:43 -0800420// try
421// {
422// SignatureSha256WithRsa signature(certificate.getSignature());
423// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700424
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
426// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700427// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800428// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800429// return;
430// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700431// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800432// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433// return;
434// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800435
Yingdi Yu2e57a582014-02-20 23:34:43 -0800436// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
437// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700438
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700439// // Convert from time::milliseconds to time::seconds since 1/1/1970.
440// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
441// time::toUnixTimestamp(certificate.getNotBefore()).count()));
442// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
443// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700444
Yingdi Yu2e57a582014-02-20 23:34:43 -0800445// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700446
Yingdi Yu2e57a582014-02-20 23:34:43 -0800447// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700448
Yingdi Yu2e57a582014-02-20 23:34:43 -0800449// sqlite3_finalize(statement);
450// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700451
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700452void
Yingdi Yu87581582014-01-14 14:28:39 -0800453SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700454{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700455 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700456 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700457 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700458 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700459
Yingdi Yu40b53092014-06-17 17:10:02 -0700460 addKey(keyName, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700461
Yingdi Yu05842f22014-04-15 19:21:56 -0700462 if (doesCertificateExist(certificateName))
463 return;
464
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700465 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800466 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700467
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700468 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700469 sqlite3_stmt* statement;
470 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700471 "INSERT OR REPLACE INTO Certificate \
472 (cert_name, cert_issuer, identity_name, key_identifier, \
473 not_before, not_after, certificate_data) \
474 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700475 -1, &statement, 0);
476
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700477 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
478
Yingdi Yu2e57a582014-02-20 23:34:43 -0800479 try
480 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700481 // this will throw an exception if the signature is not the standard one
482 // or there is no key locator present
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700483 switch (certificate.getSignature().getType())
484 {
485 case Tlv::SignatureSha256WithRsa:
486 {
487 SignatureSha256WithRsa signature(certificate.getSignature());
488 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800489
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700490 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
491 break;
492 }
493 case Tlv::SignatureSha256WithEcdsa:
494 {
495 SignatureSha256WithEcdsa signature(certificate.getSignature());
496 std::string signerName = signature.getKeyLocator().getName().toUri();
497
498 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
499 break;
500 }
501 default:
502 return;
503 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800504 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700505 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800506 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800507 return;
508 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700509
510 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800511 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700513 sqlite3_bind_int64(statement, 5,
514 static_cast<sqlite3_int64>(
515 time::toUnixTimestamp(certificate.getNotBefore()).count()));
516 sqlite3_bind_int64(statement, 6,
517 static_cast<sqlite3_int64>(
518 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700519
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700520 sqlite3_bind_blob(statement, 7,
521 certificate.wireEncode().wire(),
522 certificate.wireEncode().size(),
523 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700524
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800525 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700526
527 sqlite3_finalize(statement);
528}
529
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700530shared_ptr<IdentityCertificate>
531SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700532{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700533 sqlite3_stmt* statement;
534
535 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800536 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
537 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700538
Yingdi Yu4270f202014-01-28 14:19:16 -0800539 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700540
Yingdi Yu4270f202014-01-28 14:19:16 -0800541 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700542
Yingdi Yu4270f202014-01-28 14:19:16 -0800543 if (res == SQLITE_ROW)
544 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800545 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700546 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700547 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800548 sqlite3_finalize(statement);
549 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800550 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800551 else
552 {
553 sqlite3_finalize(statement);
554 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
555 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700556}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700557
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700558
559Name
Yingdi Yu87581582014-01-14 14:28:39 -0800560SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700561{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700562 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700563 sqlite3_prepare_v2(m_database,
564 "SELECT identity_name FROM Identity WHERE default_identity=1",
565 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700566
567 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700568
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700569 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800570 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700571 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
572 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800573 sqlite3_finalize(statement);
574 return identity;
575 }
576 else
577 {
578 sqlite3_finalize(statement);
579 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
580 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700581}
582
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700583void
Yingdi Yu87581582014-01-14 14:28:39 -0800584SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700585{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800586 addIdentity(identityName);
587
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700588 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700589
590 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700591 sqlite3_prepare_v2(m_database,
592 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
593 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700594
595 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700596 {
597 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700598
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700599 sqlite3_finalize(statement);
600
601 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700602 sqlite3_prepare_v2(m_database,
603 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
604 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700605
606 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700607
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700608 sqlite3_step(statement);
609
610 sqlite3_finalize(statement);
611}
612
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700613Name
Yingdi Yu87581582014-01-14 14:28:39 -0800614SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700615{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700616 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700617 sqlite3_prepare_v2(m_database,
618 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
619 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700620
Yingdi Yu87581582014-01-14 14:28:39 -0800621 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
622
623 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800624
625 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800626 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700627 Name keyName = identityName;
628 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
629 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800630 sqlite3_finalize(statement);
631 return keyName;
632 }
633 else
634 {
635 sqlite3_finalize(statement);
636 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
637 }
Yingdi Yu87581582014-01-14 14:28:39 -0800638}
639
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700640void
Yingdi Yu87581582014-01-14 14:28:39 -0800641SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
642{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700643 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700644 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800645
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700646 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800647 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700648
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700649 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700650
651 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700652 sqlite3_prepare_v2(m_database,
653 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
654 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700655
656 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
657
658 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700659 {
660 }
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 Yu4b8c6a22014-04-15 23:00:54 -0700733 {
734 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700735
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700736 sqlite3_finalize(statement);
737
738 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700739 sqlite3_prepare_v2(m_database,
740 "UPDATE Certificate SET default_cert=1 \
741 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
742 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700743
744 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
745 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
746 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700747
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700748 sqlite3_step(statement);
749
750 sqlite3_finalize(statement);
751}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800752
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800753void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700754SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800755{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700756 sqlite3_stmt* stmt;
757 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700758 sqlite3_prepare_v2(m_database,
759 "SELECT identity_name FROM Identity WHERE default_identity=1",
760 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800761 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700762 sqlite3_prepare_v2(m_database,
763 "SELECT identity_name FROM Identity WHERE default_identity=0",
764 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800765
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700766 while (sqlite3_step(stmt) == SQLITE_ROW)
767 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
768 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700769
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700770 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700771}
772
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800773void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700774SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800775{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700776 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800777
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700778 if (isDefault)
779 sqlite3_prepare_v2(m_database,
780 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
781 -1, &stmt, 0);
782 else
783 sqlite3_prepare_v2(m_database,
784 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
785 -1, &stmt, 0);
786
787 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800788 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700789 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
790 sqlite3_column_bytes(stmt, 0)));
791 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
792 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800793 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700794 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700795 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800796}
797
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800798void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700799SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
800 vector<Name>& nameList,
801 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800802{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700803 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700804
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700805 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700806 sqlite3_prepare_v2(m_database,
807 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
808 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800809 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700810 sqlite3_prepare_v2(m_database,
811 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
812 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700813
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700814 sqlite3_bind_text(stmt, 1,
815 identity.toUri().c_str(),
816 identity.toUri().size(),
817 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800818
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700819 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800820 {
821 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700822 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
823 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800824 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700825 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700826 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800827}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700828
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800829void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700830SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800831{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700832 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700833
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700834 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700835 sqlite3_prepare_v2(m_database,
836 "SELECT cert_name FROM Certificate WHERE default_cert=1",
837 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800838 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700839 sqlite3_prepare_v2(m_database,
840 "SELECT cert_name FROM Certificate WHERE default_cert=0",
841 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800842
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700843 while (sqlite3_step(stmt) == SQLITE_ROW)
844 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
845 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800846
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700847 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800848}
849
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800850void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700851SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
852 vector<Name>& nameList,
853 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800854{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700855 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800856 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800857
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700858 sqlite3_stmt* stmt;
859 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700860 sqlite3_prepare_v2(m_database,
861 "SELECT cert_name FROM Certificate \
862 WHERE default_cert=1 and identity_name=? and key_identifier=?",
863 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800864 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700865 sqlite3_prepare_v2(m_database,
866 "SELECT cert_name FROM Certificate \
867 WHERE default_cert=0 and identity_name=? and key_identifier=?",
868 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800869
Yingdi Yu87581582014-01-14 14:28:39 -0800870 Name identity = keyName.getPrefix(-1);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700871 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
872
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700873 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800874 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
875
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700876 while (sqlite3_step(stmt) == SQLITE_ROW)
877 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
878 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800879
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700880 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800881}
882
883void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700884SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800885{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700886 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800887 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700888
889 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800890 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700891 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800892 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700893 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800894}
895
896void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700897SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800898{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700899 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800900 return;
901
902 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700903 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800904
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700905 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700906 sqlite3_prepare_v2(m_database,
907 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
908 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800909 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
910 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
911 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700912 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800913
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700914 sqlite3_prepare_v2(m_database,
915 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
916 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800917 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
918 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
919 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700920 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800921}
922
923void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700924SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800925{
926 string identity = identityName.toUri();
927
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700928 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800929 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
930 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
931 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700932 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800933
934 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
935 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
936 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700937 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800938
939 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
940 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
941 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700942 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800943}
944
945} // namespace ndn