blob: 16041d33459776c53eeb746e8e0cea8ed2f70db3 [file] [log] [blame]
Jeff Thompson7ca11f22013-10-04 19:01:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson22285ec2013-10-22 17:43:02 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7ca11f22013-10-04 19:01:30 -07006 * See COPYING for copyright and distribution information.
7 */
8
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009#include "common.hpp"
10
11#include "sec-public-info-sqlite3.hpp"
12#include "identity-certificate.hpp"
13#include "signature-sha256-with-rsa.hpp"
14
15#include "../util/logging.hpp"
16#include "../util/time.hpp"
17
18#include "../data.hpp"
19
Yingdi Yu874678f2014-01-22 19:30:34 -080020#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070021#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070022#include <stdlib.h>
23#include <sstream>
24#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080025#include <boost/filesystem.hpp>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080026
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -080027
Jeff Thompson7ca11f22013-10-04 19:01:30 -070028
Yingdi Yu21157162014-02-28 13:02:34 -080029INIT_LOGGER("ndn.SecPublicInfoSqlite3");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070030
31using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070032
Yingdi Yufc40d872014-02-18 12:56:04 -080033namespace ndn {
Jeff Thompson7ca11f22013-10-04 19:01:30 -070034
35static const string INIT_ID_TABLE = "\
36CREATE TABLE IF NOT EXISTS \n \
37 Identity( \n \
38 identity_name BLOB NOT NULL, \n \
39 default_identity INTEGER DEFAULT 0, \n \
40 \
41 PRIMARY KEY (identity_name) \n \
42 ); \n \
43 \
44CREATE INDEX identity_index ON Identity(identity_name); \n \
45";
46
47static const string INIT_KEY_TABLE = "\
48CREATE TABLE IF NOT EXISTS \n \
49 Key( \n \
50 identity_name BLOB NOT NULL, \n \
51 key_identifier BLOB NOT NULL, \n \
52 key_type INTEGER, \n \
53 public_key BLOB, \n \
54 default_key INTEGER DEFAULT 0, \n \
55 active INTEGER DEFAULT 0, \n \
56 \
57 PRIMARY KEY (identity_name, key_identifier) \n \
58 ); \n \
59 \
60CREATE INDEX key_index ON Key(identity_name); \n \
61";
62
63static const string INIT_CERT_TABLE = "\
64CREATE TABLE IF NOT EXISTS \n \
65 Certificate( \n \
66 cert_name BLOB NOT NULL, \n \
67 cert_issuer BLOB NOT NULL, \n \
68 identity_name BLOB NOT NULL, \n \
69 key_identifier BLOB NOT NULL, \n \
70 not_before TIMESTAMP, \n \
71 not_after TIMESTAMP, \n \
72 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070073 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070074 default_cert INTEGER DEFAULT 0, \n \
75 \
76 PRIMARY KEY (cert_name) \n \
77 ); \n \
78 \
79CREATE INDEX cert_index ON Certificate(cert_name); \n \
80CREATE INDEX subject ON Certificate(identity_name); \n \
81";
82
83/**
84 * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size().
85 */
86static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*))
87{
88 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
89}
90
Yingdi Yu87581582014-01-14 14:28:39 -080091SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070092{
Yingdi Yu0b9c3152014-01-26 16:31:18 -080093 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndnx";
94 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070095
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080096 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
97 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
98 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev44471462014-02-12 11:21:51 -080099#ifdef NDN_CPP_DISABLE_SQLITE3_FS_LOCKING
100 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800101#else
102 0
103#endif
104 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700105 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800106 throw Error("identity DB cannot be opened/created");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700107
108 //Check if Key table exists;
109 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800110 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'", -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700111 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700112
113 bool idTableExists = false;
114 if (res == SQLITE_ROW)
115 idTableExists = true;
116
117 sqlite3_finalize(statement);
118
119 if (!idTableExists) {
120 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800121 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700122
123 if (res != SQLITE_OK && errorMessage != 0) {
124 _LOG_TRACE("Init \"error\" in Identity: " << errorMessage);
125 sqlite3_free(errorMessage);
126 }
127 }
128
129 //Check if Key table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800130 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700131 res = sqlite3_step(statement);
132
133 bool keyTableExists = false;
134 if (res == SQLITE_ROW)
135 keyTableExists = true;
136
137 sqlite3_finalize(statement);
138
139 if (!keyTableExists) {
140 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800141 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700142
143 if (res != SQLITE_OK && errorMessage != 0) {
144 _LOG_TRACE("Init \"error\" in KEY: " << errorMessage);
145 sqlite3_free(errorMessage);
146 }
147 }
148
149 //Check if Certificate table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800150 sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700151 res = sqlite3_step(statement);
152
153 bool idCertificateTableExists = false;
154 if (res == SQLITE_ROW)
155 idCertificateTableExists = true;
156
157 sqlite3_finalize(statement);
158
159 if (!idCertificateTableExists) {
160 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800161 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700162
163 if (res != SQLITE_OK && errorMessage != 0) {
164 _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage);
165 sqlite3_free(errorMessage);
166 }
167 }
168}
169
Yingdi Yu87581582014-01-14 14:28:39 -0800170SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700171{
172}
173
174bool
Yingdi Yu87581582014-01-14 14:28:39 -0800175SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700176{
177 bool result = false;
178
179 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800180 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700181
182 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
183 int res = sqlite3_step(statement);
184
185 if (res == SQLITE_ROW) {
186 int countAll = sqlite3_column_int(statement, 0);
187 if (countAll > 0)
188 result = true;
189 }
190
191 sqlite3_finalize(statement);
192
193 return result;
194}
195
196void
Yingdi Yu87581582014-01-14 14:28:39 -0800197SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700198{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700199 sqlite3_stmt *statement;
200
Yingdi Yu2e57a582014-02-20 23:34:43 -0800201 sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Identity (identity_name) values (?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700202
203 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
204
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800205 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206
207 sqlite3_finalize(statement);
208}
209
210bool
Yingdi Yu87581582014-01-14 14:28:39 -0800211SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700212{
213 //TODO:
214 return false;
215}
216
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700217bool
Yingdi Yu87581582014-01-14 14:28:39 -0800218SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700219{
Yingdi Yu88663af2014-01-15 15:21:38 -0800220 if(keyName.empty())
221 throw Error("Incorrect key name " + keyName.toUri());
222
Yingdi Yu87581582014-01-14 14:28:39 -0800223 string keyId = keyName.get(-1).toEscapedString();
224 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700225
226 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800227 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700228
229 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
230 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
231
232 int res = sqlite3_step(statement);
233
234 bool keyIdExist = false;
235 if (res == SQLITE_ROW) {
236 int countAll = sqlite3_column_int(statement, 0);
237 if (countAll > 0)
238 keyIdExist = true;
239 }
240
241 sqlite3_finalize(statement);
242
243 return keyIdExist;
244}
245
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700246void
Yingdi Yu87581582014-01-14 14:28:39 -0800247SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700248{
Yingdi Yu88663af2014-01-15 15:21:38 -0800249 if(keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800250 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800251
Yingdi Yu87581582014-01-14 14:28:39 -0800252 string keyId = keyName.get(-1).toEscapedString();
253 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700254
Yingdi Yu2e57a582014-02-20 23:34:43 -0800255 addIdentity(identityName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700256
257 sqlite3_stmt *statement;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800258 sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700259
260 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
261 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
262 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800263 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700264
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800265 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700266
267 sqlite3_finalize(statement);
268}
269
Yingdi Yu2e57a582014-02-20 23:34:43 -0800270shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800271SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800273 if (keyName.empty())
274 {
275 _LOG_DEBUG("SecPublicInfoSqlite3::getPublicKey Empty keyName");
276 throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName");
277 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700278
Yingdi Yu87581582014-01-14 14:28:39 -0800279 string keyId = keyName.get(-1).toEscapedString();
280 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700281
282 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800283 sqlite3_prepare_v2(m_database, "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284
285 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
286 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
287
288 int res = sqlite3_step(statement);
289
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700291 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800292 {
293 result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0));
294 sqlite3_finalize(statement);
295 return result;
296 }
297 else
298 {
299 sqlite3_finalize(statement);
300 throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist");
301 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700302}
303
304bool
Yingdi Yu87581582014-01-14 14:28:39 -0800305SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700306{
307 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800308 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700309
310 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
311
312 int res = sqlite3_step(statement);
313
314 bool certExist = false;
315 if (res == SQLITE_ROW) {
316 int countAll = sqlite3_column_int(statement, 0);
317 if (countAll > 0)
318 certExist = true;
319 }
320
321 sqlite3_finalize(statement);
322
323 return certExist;
324}
325
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326// void
327// SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
328// {
329// std::string certificateName = certificate.getName().toUri();
330// Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Yingdi Yu88663af2014-01-15 15:21:38 -0800331
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332// if(keyName.empty())
333// return;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700334
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335// std::string keyId = keyName.get(-1).toEscapedString();
336// std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700337
Yingdi Yu2e57a582014-02-20 23:34:43 -0800338// sqlite3_stmt *statement;
339// sqlite3_prepare_v2(m_database,
340// "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
341// values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
342// -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700343
Yingdi Yu2e57a582014-02-20 23:34:43 -0800344// _LOG_DEBUG("certName: " << certificateName);
345// sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346
Yingdi Yu2e57a582014-02-20 23:34:43 -0800347// try
348// {
349// SignatureSha256WithRsa signature(certificate.getSignature());
350// std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700351
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352// sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
353// }
354// catch(KeyLocator::Error& e)
355// {
356// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
357// return;
358// }
359// catch(SignatureSha256WithRsa::Error& e)
360// {
361// _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
362// return;
363// }
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800364
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365// sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
366// sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700367
Yingdi Yu2e57a582014-02-20 23:34:43 -0800368// // Convert from milliseconds to seconds since 1/1/1970.
369// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
370// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700371
Yingdi Yu2e57a582014-02-20 23:34:43 -0800372// sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700373
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374// sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376// sqlite3_finalize(statement);
377// }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700378
379void
Yingdi Yu87581582014-01-14 14:28:39 -0800380SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700381{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700382 const Name& certificateName = certificate.getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383 Name keyName =
384 IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); // KeyName is from IdentityCertificate name, so should be qualified.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700385
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386 addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo()); //HACK!!! Assume the key type is RSA, we should check more.
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700387
Yingdi Yu87581582014-01-14 14:28:39 -0800388 string keyId = keyName.get(-1).toEscapedString();
389 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700390
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700391 // Insert the certificate
392 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800393 sqlite3_prepare_v2(m_database,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800394 "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
396 -1, &statement, 0);
397
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800398 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
400
Yingdi Yu2e57a582014-02-20 23:34:43 -0800401 try
402 {
403 // this will throw an exception if the signature is not the standard one or there is no key locator present
404 SignatureSha256WithRsa signature(certificate.getSignature());
405 std::string signerName = signature.getKeyLocator().getName().toUri();
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800406
Yingdi Yu2e57a582014-02-20 23:34:43 -0800407 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
408 }
409 catch(KeyLocator::Error& e)
410 {
411 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type");
412 return;
413 }
414 catch(SignatureSha256WithRsa::Error& e)
415 {
416 _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type");
417 return;
418 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700419
420 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800421 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700422
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700423 // Convert from milliseconds to seconds since 1/1/1970.
Yingdi Yu88663af2014-01-15 15:21:38 -0800424 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
425 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700426
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800427 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700428
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800429 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700430
431 sqlite3_finalize(statement);
432}
433
Yingdi Yu2e57a582014-02-20 23:34:43 -0800434shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800435SecPublicInfoSqlite3::getCertificate(const Name &certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700436{
Yingdi Yu4270f202014-01-28 14:19:16 -0800437 sqlite3_stmt *statement;
438
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800439 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800440 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
441 -1, &statement, 0);
442
443 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
444
445 int res = sqlite3_step(statement);
446
Yingdi Yu4270f202014-01-28 14:19:16 -0800447 if (res == SQLITE_ROW)
448 {
Yingdi Yu2e57a582014-02-20 23:34:43 -0800449 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu4270f202014-01-28 14:19:16 -0800450 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800451 sqlite3_finalize(statement);
452 return certificate;
Yingdi Yu4270f202014-01-28 14:19:16 -0800453 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800454 else
455 {
456 sqlite3_finalize(statement);
457 throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist");
458 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700459}
Yingdi Yu4270f202014-01-28 14:19:16 -0800460
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700461
462Name
Yingdi Yu87581582014-01-14 14:28:39 -0800463SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700464{
465 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800466 sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700467
468 int res = sqlite3_step(statement);
469
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700470 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800471 {
472 Name identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
473 sqlite3_finalize(statement);
474 return identity;
475 }
476 else
477 {
478 sqlite3_finalize(statement);
479 throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity");
480 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700481}
482
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700483void
Yingdi Yu87581582014-01-14 14:28:39 -0800484SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700485{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800486 addIdentity(identityName);
487
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700488 sqlite3_stmt *statement;
489
490 //Reset previous default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800491 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700492
493 while (sqlite3_step(statement) == SQLITE_ROW)
494 {}
495
496 sqlite3_finalize(statement);
497
498 //Set current default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800499 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700500
501 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
502
503 sqlite3_step(statement);
504
505 sqlite3_finalize(statement);
506}
507
Yingdi Yu87581582014-01-14 14:28:39 -0800508Name
509SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700510{
Yingdi Yu87581582014-01-14 14:28:39 -0800511 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800512 sqlite3_prepare_v2(m_database, "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700513
Yingdi Yu87581582014-01-14 14:28:39 -0800514 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
515
516 int res = sqlite3_step(statement);
Yingdi Yu87581582014-01-14 14:28:39 -0800517
518 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800519 {
520 Name keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
521 sqlite3_column_bytes(statement, 0)));
522 sqlite3_finalize(statement);
523 return keyName;
524 }
525 else
526 {
527 sqlite3_finalize(statement);
528 throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found");
529 }
Yingdi Yu87581582014-01-14 14:28:39 -0800530}
531
532void
533SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
534{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800535 if(!doesPublicKeyExist(keyName))
536 throw Error("SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal Key does not exist:" + keyName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800537
Yingdi Yu87581582014-01-14 14:28:39 -0800538 string keyId = keyName.get(-1).toEscapedString();
539 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700540
541 sqlite3_stmt *statement;
542
543 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800544 sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700545
546 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
547
548 while (sqlite3_step(statement) == SQLITE_ROW)
549 {}
550
551 sqlite3_finalize(statement);
552
553 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800554 sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700555
556 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
557 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
558
559 sqlite3_step(statement);
560
561 sqlite3_finalize(statement);
562}
563
Yingdi Yu87581582014-01-14 14:28:39 -0800564Name
565SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700566{
Yingdi Yu88663af2014-01-15 15:21:38 -0800567 if(keyName.empty())
Yingdi Yu2e57a582014-02-20 23:34:43 -0800568 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key");
Yingdi Yu88663af2014-01-15 15:21:38 -0800569
Yingdi Yu87581582014-01-14 14:28:39 -0800570 string keyId = keyName.get(-1).toEscapedString();
571 Name identityName = keyName.getPrefix(-1);
572
573 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800574 sqlite3_prepare_v2(m_database, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0);
Yingdi Yu87581582014-01-14 14:28:39 -0800575
576 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
577 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
578
579 int res = sqlite3_step(statement);
580
Yingdi Yu87581582014-01-14 14:28:39 -0800581 if (res == SQLITE_ROW)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800582 {
583 Name certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
584 sqlite3_finalize(statement);
585 return certName;
586 }
587 else
588 {
589 sqlite3_finalize(statement);
590 throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey certificate not found");
591 }
Yingdi Yu87581582014-01-14 14:28:39 -0800592}
593
594void
595SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
596{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800597 if(!doesCertificateExist(certificateName))
598 throw Error("SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal certificate does not exist:" + certificateName.toUri());
Yingdi Yu88663af2014-01-15 15:21:38 -0800599
Yingdi Yu2e57a582014-02-20 23:34:43 -0800600 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu87581582014-01-14 14:28:39 -0800601 string keyId = keyName.get(-1).toEscapedString();
602 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700603
604 sqlite3_stmt *statement;
605
606 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800607 sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700608
609 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
610 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
611
612 while (sqlite3_step(statement) == SQLITE_ROW)
613 {}
614
615 sqlite3_finalize(statement);
616
617 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800618 sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700619
620 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
621 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
622 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
623
624 sqlite3_step(statement);
625
626 sqlite3_finalize(statement);
627}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800628
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800629void
630SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800631{
632 sqlite3_stmt *stmt;
633 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800634 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800635 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800636 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800637
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800638 while(sqlite3_step (stmt) == SQLITE_ROW)
639 nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700640
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800641 sqlite3_finalize (stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700642}
643
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800644void
645SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800646{
647 sqlite3_stmt *stmt;
648 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800649 sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800650 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800651 sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800652
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800653 while(sqlite3_step (stmt) == SQLITE_ROW)
654 {
655 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
656 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
657 nameList.push_back(keyName);
658 }
659 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800660}
661
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800662void
663SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800664{
665 sqlite3_stmt *stmt;
666 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800667 sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800668 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800669 sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800670
671 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
672
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800673 while(sqlite3_step (stmt) == SQLITE_ROW)
674 {
675 Name keyName(identity);
676 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
677 nameList.push_back(keyName);
678 }
679 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800680}
681
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800682void
683SecPublicInfoSqlite3::getAllCertificateNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800684{
685 sqlite3_stmt *stmt;
686 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800687 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800688 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800689 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800690
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800691 while(sqlite3_step (stmt) == SQLITE_ROW)
692 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
693
694 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800695}
696
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800697void
698SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800699{
Yingdi Yu88663af2014-01-15 15:21:38 -0800700 if(keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800701 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800702
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800703 sqlite3_stmt *stmt;
704 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800705 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800706 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800707 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800708
Yingdi Yu87581582014-01-14 14:28:39 -0800709 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800710 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
711 std::string baseKeyName = keyName.get(-1).toEscapedString();
712 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
713
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800714 while(sqlite3_step (stmt) == SQLITE_ROW)
715 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
716
717 sqlite3_finalize (stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800718}
719
720void
721SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName)
722{
723 if(certName.empty())
724 return;
725
726 sqlite3_stmt *stmt;
727 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
728 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
729 sqlite3_step(stmt);
730 sqlite3_finalize (stmt);
731}
732
733void
734SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName)
735{
736 if(keyName.empty())
737 return;
738
739 string identity = keyName.getPrefix(-1).toUri();
740 string keyId = keyName.get(-1).toEscapedString();
741
742 sqlite3_stmt *stmt;
743 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
744 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
745 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
746 sqlite3_step(stmt);
747 sqlite3_finalize (stmt);
748
749 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
750 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
751 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
752 sqlite3_step(stmt);
753 sqlite3_finalize (stmt);
754}
755
756void
757SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName)
758{
759 string identity = identityName.toUri();
760
761 sqlite3_stmt *stmt;
762 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
763 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
764 sqlite3_step(stmt);
765 sqlite3_finalize (stmt);
766
767 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
768 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
769 sqlite3_step(stmt);
770 sqlite3_finalize (stmt);
771
772 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
773 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
774 sqlite3_step(stmt);
775 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800776}
777
778} // namespace ndn
779