blob: 86256f8e98c570f9003d324cc5df9eff3e0f2e36 [file] [log] [blame]
Jeff Thompson7ca11f22013-10-04 19:01:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070014 */
15
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080016#include "common.hpp"
17
18#include "sec-public-info-sqlite3.hpp"
19#include "identity-certificate.hpp"
20#include "signature-sha256-with-rsa.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080021#include "../data.hpp"
22
Yingdi Yu874678f2014-01-22 19:30:34 -080023#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070024#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070025#include <stdlib.h>
26#include <sstream>
27#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080028#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080029
Jeff Thompson7ca11f22013-10-04 19:01:30 -070030using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070031
Yingdi Yufc40d872014-02-18 12:56:04 -080032namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070033
34static const string INIT_ID_TABLE = "\
35CREATE TABLE IF NOT EXISTS \n \
36 Identity( \n \
37 identity_name BLOB NOT NULL, \n \
38 default_identity INTEGER DEFAULT 0, \n \
39 \
40 PRIMARY KEY (identity_name) \n \
41 ); \n \
42 \
43CREATE INDEX identity_index ON Identity(identity_name); \n \
44";
45
46static const string INIT_KEY_TABLE = "\
47CREATE TABLE IF NOT EXISTS \n \
48 Key( \n \
49 identity_name BLOB NOT NULL, \n \
50 key_identifier BLOB NOT NULL, \n \
51 key_type INTEGER, \n \
52 public_key BLOB, \n \
53 default_key INTEGER DEFAULT 0, \n \
54 active INTEGER DEFAULT 0, \n \
55 \
56 PRIMARY KEY (identity_name, key_identifier) \n \
57 ); \n \
58 \
59CREATE INDEX key_index ON Key(identity_name); \n \
60";
61
62static const string INIT_CERT_TABLE = "\
63CREATE TABLE IF NOT EXISTS \n \
64 Certificate( \n \
65 cert_name BLOB NOT NULL, \n \
66 cert_issuer BLOB NOT NULL, \n \
67 identity_name BLOB NOT NULL, \n \
68 key_identifier BLOB NOT NULL, \n \
69 not_before TIMESTAMP, \n \
70 not_after TIMESTAMP, \n \
71 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070072 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070073 default_cert INTEGER DEFAULT 0, \n \
74 \
75 PRIMARY KEY (cert_name) \n \
76 ); \n \
77 \
78CREATE INDEX cert_index ON Certificate(cert_name); \n \
79CREATE INDEX subject ON Certificate(identity_name); \n \
80";
81
82/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070083 * A utility function to call the normal sqlite3_bind_text where the value and length are
84 * value.c_str() and value.size().
Jeff Thompson7ca11f22013-10-04 19:01:30 -070085 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070086static int sqlite3_bind_text(sqlite3_stmt* statement,
87 int index,
88 const string& value,
89 void(*destructor)(void*))
Jeff Thompson7ca11f22013-10-04 19:01:30 -070090{
91 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
92}
93
Yingdi Yu87581582014-01-14 14:28:39 -080094SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070095{
Yingdi Yu37e317f2014-03-19 12:16:23 -070096 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
Yingdi Yu0b9c3152014-01-26 16:31:18 -080097 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070098
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080099 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
100 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
101 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700102#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
Alexander Afanasyev44471462014-02-12 11:21:51 -0800103 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800104#else
105 0
106#endif
107 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700108 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800109 throw Error("identity DB cannot be opened/created");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700111 //Check if Key table exists;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700113 sqlite3_prepare_v2(m_database,
114 "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
115 -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700116 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700117
118 bool idTableExists = false;
119 if (res == SQLITE_ROW)
120 idTableExists = true;
121
122 sqlite3_finalize(statement);
123
124 if (!idTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800126 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700127
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700128 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700129 sqlite3_free(errorMessage);
130 }
131 }
132
133 //Check if Key table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700134 sqlite3_prepare_v2(m_database,
135 "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
136 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700137 res = sqlite3_step(statement);
138
139 bool keyTableExists = false;
140 if (res == SQLITE_ROW)
141 keyTableExists = true;
142
143 sqlite3_finalize(statement);
144
145 if (!keyTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800147 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700149 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700150 sqlite3_free(errorMessage);
151 }
152 }
153
154 //Check if Certificate table exists;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 sqlite3_prepare_v2(m_database,
156 "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
157 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700158 res = sqlite3_step(statement);
159
160 bool idCertificateTableExists = false;
161 if (res == SQLITE_ROW)
162 idCertificateTableExists = true;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700163
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700164 sqlite3_finalize(statement);
165
166 if (!idCertificateTableExists) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700167 char* errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800168 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700169
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700170 if (res != SQLITE_OK && errorMessage != 0) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700171 sqlite3_free(errorMessage);
172 }
173 }
174}
175
Yingdi Yu87581582014-01-14 14:28:39 -0800176SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700177{
178}
179
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180bool
Yingdi Yu87581582014-01-14 14:28:39 -0800181SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700182{
183 bool result = false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700184
185 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700186 sqlite3_prepare_v2(m_database,
187 "SELECT count(*) FROM Identity WHERE identity_name=?",
188 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700189
190 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
191 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700193 if (res == SQLITE_ROW) {
194 int countAll = sqlite3_column_int(statement, 0);
195 if (countAll > 0)
196 result = true;
197 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700198
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700199 sqlite3_finalize(statement);
200
201 return result;
202}
203
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204void
Yingdi Yu87581582014-01-14 14:28:39 -0800205SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206{
Yingdi Yu05842f22014-04-15 19:21:56 -0700207 if (doesIdentityExist(identityName))
208 return;
209
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700210 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700211
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700212 sqlite3_prepare_v2(m_database,
213 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
214 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700215
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700216 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700217
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800218 sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700219
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220 sqlite3_finalize(statement);
221}
222
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223bool
Yingdi Yu87581582014-01-14 14:28:39 -0800224SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225{
226 //TODO:
227 return false;
228}
229
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700230bool
Yingdi Yu87581582014-01-14 14:28:39 -0800231SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700232{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700233 if (keyName.empty())
Yingdi Yu88663af2014-01-15 15:21:38 -0800234 throw Error("Incorrect key name " + keyName.toUri());
235
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700236 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800237 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700238
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700239 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700240 sqlite3_prepare_v2(m_database,
241 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
242 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700243
244 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
245 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
246
247 int res = sqlite3_step(statement);
248
249 bool keyIdExist = false;
250 if (res == SQLITE_ROW) {
251 int countAll = sqlite3_column_int(statement, 0);
252 if (countAll > 0)
253 keyIdExist = true;
254 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700255
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700256 sqlite3_finalize(statement);
257
258 return keyIdExist;
259}
260
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700261void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700262SecPublicInfoSqlite3::addPublicKey(const Name& keyName,
263 KeyType keyType,
264 const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700265{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700266 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800267 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800268
Yingdi Yu05842f22014-04-15 19:21:56 -0700269 if (doesPublicKeyExist(keyName))
270 return;
271
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700272 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800273 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700274
Yingdi Yu2e57a582014-02-20 23:34:43 -0800275 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700276
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700277 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700278 sqlite3_prepare_v2(m_database,
279 "INSERT OR REPLACE INTO Key \
280 (identity_name, key_identifier, key_type, public_key) \
281 values (?, ?, ?, ?)",
282 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700283
284 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
285 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
286 sqlite3_bind_int(statement, 3, (int)keyType);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700287 sqlite3_bind_blob(statement, 4,
288 publicKeyDer.get().buf(),
289 publicKeyDer.get().size(),
290 SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700291
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800292 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700293
294 sqlite3_finalize(statement);
295}
296
Yingdi Yu2e57a582014-02-20 23:34:43 -0800297shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800298SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700299{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700300 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800301 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800302 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
303 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700304
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700305 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800306 Name identityName = keyName.getPrefix(-1);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700307
308 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700309 sqlite3_prepare_v2(m_database,
310 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
311 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700312
313 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
314 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
315
316 int res = sqlite3_step(statement);
317
Yingdi Yu2e57a582014-02-20 23:34:43 -0800318 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700319 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800320 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700321 result =
322 make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
323 sqlite3_column_bytes(statement, 0));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800324 sqlite3_finalize(statement);
325 return result;
326 }
327 else
328 {
329 sqlite3_finalize(statement);
330 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
331 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700332}
333
334bool
Yingdi Yu87581582014-01-14 14:28:39 -0800335SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700336{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700337 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700338 sqlite3_prepare_v2(m_database,
339 "SELECT count(*) FROM Certificate WHERE cert_name=?",
340 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700341
342 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
343
344 int res = sqlite3_step(statement);
345
346 bool certExist = false;
347 if (res == SQLITE_ROW) {
348 int countAll = sqlite3_column_int(statement, 0);
349 if (countAll > 0)
350 certExist = true;
351 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700352
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700353 sqlite3_finalize(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700354
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700355 return certExist;
356}
357
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358// void
359// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
360// {
361// std::string certificateName = certificate.getName().toUri();
362// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800363
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700364// if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700366
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700367// std::string keyId = keyName.get(-1).toUri();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800368// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700369
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700370// sqlite3_stmt* statement;
371// sqlite3_prepare_v2(m_database,
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700372// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) "
373// "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700377
Yingdi Yu2e57a582014-02-20 23:34:43 -0800378// try
379// {
380// SignatureSha256WithRsa signature(certificate.getSignature());
381// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700382
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
384// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700385// catch (KeyLocator::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800387// return;
388// }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700389// catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800390// {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800391// return;
392// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800393
Yingdi Yu2e57a582014-02-20 23:34:43 -0800394// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
395// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700397// // Convert from time::milliseconds to time::seconds since 1/1/1970.
398// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
399// time::toUnixTimestamp(certificate.getNotBefore()).count()));
400// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
401// time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402
Yingdi Yu2e57a582014-02-20 23:34:43 -0800403// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700404
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700406
Yingdi Yu2e57a582014-02-20 23:34:43 -0800407// sqlite3_finalize(statement);
408// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700409
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700410void
Yingdi Yu87581582014-01-14 14:28:39 -0800411SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700412{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413 const Name& certificateName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700414 // KeyName is from IdentityCertificate name, so should be qualified.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700415 Name keyName =
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700416 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700417
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700418 //HACK!!! Assume the key type is RSA, we should check more.
419 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700420
Yingdi Yu05842f22014-04-15 19:21:56 -0700421 if (doesCertificateExist(certificateName))
422 return;
423
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700424 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800425 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700426
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700427 // Insert the certificate
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700428 sqlite3_stmt* statement;
429 sqlite3_prepare_v2(m_database,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700430 "INSERT OR REPLACE INTO Certificate \
431 (cert_name, cert_issuer, identity_name, key_identifier, \
432 not_before, not_after, certificate_data) \
433 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700434 -1, &statement, 0);
435
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700436 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
437
Yingdi Yu2e57a582014-02-20 23:34:43 -0800438 try
439 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700440 // this will throw an exception if the signature is not the standard one
441 // or there is no key locator present
Yingdi Yu2e57a582014-02-20 23:34:43 -0800442 SignatureSha256WithRsa signature(certificate.getSignature());
443 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800444
Yingdi Yu2e57a582014-02-20 23:34:43 -0800445 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
446 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700447 catch (std::runtime_error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800448 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800449 return;
450 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700451
452 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800453 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700454
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700455 sqlite3_bind_int64(statement, 5,
456 static_cast<sqlite3_int64>(
457 time::toUnixTimestamp(certificate.getNotBefore()).count()));
458 sqlite3_bind_int64(statement, 6,
459 static_cast<sqlite3_int64>(
460 time::toUnixTimestamp(certificate.getNotAfter()).count()));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700461
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700462 sqlite3_bind_blob(statement, 7,
463 certificate.wireEncode().wire(),
464 certificate.wireEncode().size(),
465 SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700466
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800467 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700468
469 sqlite3_finalize(statement);
470}
471
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700472shared_ptr<IdentityCertificate>
473SecPublicInfoSqlite3::getCertificate(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700474{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700475 sqlite3_stmt* statement;
476
477 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800478 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
479 -1, &statement, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700480
Yingdi Yu4270f202014-01-28 14:19:16 -0800481 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700482
Yingdi Yu4270f202014-01-28 14:19:16 -0800483 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700484
Yingdi Yu4270f202014-01-28 14:19:16 -0800485 if (res == SQLITE_ROW)
486 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800487 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700488 certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700489 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800490 sqlite3_finalize(statement);
491 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800492 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800493 else
494 {
495 sqlite3_finalize(statement);
496 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
497 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700498}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700499
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700500
501Name
Yingdi Yu87581582014-01-14 14:28:39 -0800502SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700503{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700504 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700505 sqlite3_prepare_v2(m_database,
506 "SELECT identity_name FROM Identity WHERE default_identity=1",
507 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700508
509 int res = sqlite3_step(statement);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700510
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700511 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800512 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700513 Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
514 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800515 sqlite3_finalize(statement);
516 return identity;
517 }
518 else
519 {
520 sqlite3_finalize(statement);
521 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
522 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700523}
524
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700525void
Yingdi Yu87581582014-01-14 14:28:39 -0800526SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700527{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800528 addIdentity(identityName);
529
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700530 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700531
532 //Reset previous default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700533 sqlite3_prepare_v2(m_database,
534 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
535 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700536
537 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700538 {
539 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700540
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700541 sqlite3_finalize(statement);
542
543 //Set current default identity
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700544 sqlite3_prepare_v2(m_database,
545 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
546 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700547
548 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700549
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700550 sqlite3_step(statement);
551
552 sqlite3_finalize(statement);
553}
554
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700555Name
Yingdi Yu87581582014-01-14 14:28:39 -0800556SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700557{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700558 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700559 sqlite3_prepare_v2(m_database,
560 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
561 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700562
Yingdi Yu87581582014-01-14 14:28:39 -0800563 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
564
565 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800566
567 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800568 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700569 Name keyName = identityName;
570 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
571 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800572 sqlite3_finalize(statement);
573 return keyName;
574 }
575 else
576 {
577 sqlite3_finalize(statement);
578 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
579 }
Yingdi Yu87581582014-01-14 14:28:39 -0800580}
581
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700582void
Yingdi Yu87581582014-01-14 14:28:39 -0800583SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
584{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700585 if (!doesPublicKeyExist(keyName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700586 throw Error("Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800587
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700588 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800589 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700590
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700591 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700592
593 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700594 sqlite3_prepare_v2(m_database,
595 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
596 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700597
598 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
599
600 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700601 {
602 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700603
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700604 sqlite3_finalize(statement);
605
606 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700607 sqlite3_prepare_v2(m_database,
608 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
609 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700610
611 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
612 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700613
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700614 sqlite3_step(statement);
615
616 sqlite3_finalize(statement);
617}
618
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700619Name
Yingdi Yu87581582014-01-14 14:28:39 -0800620SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700621{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700622 if (keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800623 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800624
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700625 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800626 Name identityName = keyName.getPrefix(-1);
627
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700628 sqlite3_stmt* statement;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700629 sqlite3_prepare_v2(m_database,
630 "SELECT cert_name FROM Certificate \
631 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
632 -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800633
634 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
635 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
636
637 int res = sqlite3_step(statement);
638
Yingdi Yu87581582014-01-14 14:28:39 -0800639 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800640 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700641 Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
642 sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800643 sqlite3_finalize(statement);
644 return certName;
645 }
646 else
647 {
648 sqlite3_finalize(statement);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700649 throw Error("certificate not found");
Yingdi Yu2e57a582014-02-20 23:34:43 -0800650 }
Yingdi Yu87581582014-01-14 14:28:39 -0800651}
652
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700653void
Yingdi Yu87581582014-01-14 14:28:39 -0800654SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
655{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700656 if (!doesCertificateExist(certificateName))
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700657 throw Error("certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800658
Yingdi Yu2e57a582014-02-20 23:34:43 -0800659 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700660 string keyId = keyName.get(-1).toUri();
Yingdi Yu87581582014-01-14 14:28:39 -0800661 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700662
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700663 sqlite3_stmt* statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700664
665 //Reset previous default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700666 sqlite3_prepare_v2(m_database,
667 "UPDATE Certificate SET default_cert=0 \
668 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
669 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700670
671 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
672 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
673
674 while (sqlite3_step(statement) == SQLITE_ROW)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700675 {
676 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700677
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700678 sqlite3_finalize(statement);
679
680 //Set current default Key
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700681 sqlite3_prepare_v2(m_database,
682 "UPDATE Certificate SET default_cert=1 \
683 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
684 -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700685
686 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
687 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
688 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700689
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700690 sqlite3_step(statement);
691
692 sqlite3_finalize(statement);
693}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800694
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800695void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700696SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800697{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700698 sqlite3_stmt* stmt;
699 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700700 sqlite3_prepare_v2(m_database,
701 "SELECT identity_name FROM Identity WHERE default_identity=1",
702 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800703 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700704 sqlite3_prepare_v2(m_database,
705 "SELECT identity_name FROM Identity WHERE default_identity=0",
706 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800707
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700708 while (sqlite3_step(stmt) == SQLITE_ROW)
709 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
710 sqlite3_column_bytes(stmt, 0))));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700711
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700712 sqlite3_finalize(stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700713}
714
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800715void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700716SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800717{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700718 sqlite3_stmt* stmt;
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800719
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700720 if (isDefault)
721 sqlite3_prepare_v2(m_database,
722 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
723 -1, &stmt, 0);
724 else
725 sqlite3_prepare_v2(m_database,
726 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
727 -1, &stmt, 0);
728
729 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800730 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700731 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
732 sqlite3_column_bytes(stmt, 0)));
733 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
734 sqlite3_column_bytes(stmt, 1)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800735 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700736 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700737 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800738}
739
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800740void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700741SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity,
742 vector<Name>& nameList,
743 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800744{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700745 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700746
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700747 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700748 sqlite3_prepare_v2(m_database,
749 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
750 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800751 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700752 sqlite3_prepare_v2(m_database,
753 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
754 -1, &stmt, 0);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700755
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700756 sqlite3_bind_text(stmt, 1,
757 identity.toUri().c_str(),
758 identity.toUri().size(),
759 SQLITE_TRANSIENT);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800760
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700761 while (sqlite3_step(stmt) == SQLITE_ROW)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800762 {
763 Name keyName(identity);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700764 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
765 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800766 nameList.push_back(keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700767 }
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700768 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800769}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700770
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800771void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700772SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800773{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700774 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700775
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700776 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700777 sqlite3_prepare_v2(m_database,
778 "SELECT cert_name FROM Certificate WHERE default_cert=1",
779 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800780 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700781 sqlite3_prepare_v2(m_database,
782 "SELECT cert_name FROM Certificate WHERE default_cert=0",
783 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800784
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700785 while (sqlite3_step(stmt) == SQLITE_ROW)
786 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
787 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800788
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700789 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800790}
791
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800792void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700793SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName,
794 vector<Name>& nameList,
795 bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800796{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700797 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800798 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800799
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700800 sqlite3_stmt* stmt;
801 if (isDefault)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700802 sqlite3_prepare_v2(m_database,
803 "SELECT cert_name FROM Certificate \
804 WHERE default_cert=1 and identity_name=? and key_identifier=?",
805 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800806 else
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700807 sqlite3_prepare_v2(m_database,
808 "SELECT cert_name FROM Certificate \
809 WHERE default_cert=0 and identity_name=? and key_identifier=?",
810 -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800811
Yingdi Yu87581582014-01-14 14:28:39 -0800812 Name identity = keyName.getPrefix(-1);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700813 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
814
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700815 std::string baseKeyName = keyName.get(-1).toUri();
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800816 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
817
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700818 while (sqlite3_step(stmt) == SQLITE_ROW)
819 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
820 sqlite3_column_bytes(stmt, 0)));
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800821
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700822 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800823}
824
825void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700826SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800827{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700828 if (certName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800829 return;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700830
831 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800832 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700833 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800834 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700835 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800836}
837
838void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700839SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800840{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700841 if (keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800842 return;
843
844 string identity = keyName.getPrefix(-1).toUri();
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700845 string keyId = keyName.get(-1).toUri();
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800846
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700847 sqlite3_stmt* stmt;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700848 sqlite3_prepare_v2(m_database,
849 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
850 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800851 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
852 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
853 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700854 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800855
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700856 sqlite3_prepare_v2(m_database,
857 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
858 -1, &stmt, 0);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800859 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
860 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
861 sqlite3_step(stmt);
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::deleteIdentityInfo(const Name& identityName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800867{
868 string identity = identityName.toUri();
869
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700870 sqlite3_stmt* stmt;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800871 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
872 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
873 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700874 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800875
876 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
877 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
878 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700879 sqlite3_finalize(stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800880
881 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
882 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
883 sqlite3_step(stmt);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700884 sqlite3_finalize(stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800885}
886
887} // namespace ndn