blob: fbef7130723a7f0fa9970bd7f5a706e4089be764 [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 Yu87581582014-01-14 14:28:39 -080029INIT_LOGGER("BasicKeyMetaInfo");
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{
199 if (doesIdentityExist(identityName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800200 throw Error("Identity already exists");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700201
202 sqlite3_stmt *statement;
203
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800204 sqlite3_prepare_v2(m_database, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700205
206 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
207
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800208 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700209
210 sqlite3_finalize(statement);
211}
212
213bool
Yingdi Yu87581582014-01-14 14:28:39 -0800214SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700215{
216 //TODO:
217 return false;
218}
219
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700220bool
Yingdi Yu87581582014-01-14 14:28:39 -0800221SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222{
Yingdi Yu88663af2014-01-15 15:21:38 -0800223 if(keyName.empty())
224 throw Error("Incorrect key name " + keyName.toUri());
225
Yingdi Yu87581582014-01-14 14:28:39 -0800226 string keyId = keyName.get(-1).toEscapedString();
227 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700228
229 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800230 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 -0700231
232 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
233 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
234
235 int res = sqlite3_step(statement);
236
237 bool keyIdExist = false;
238 if (res == SQLITE_ROW) {
239 int countAll = sqlite3_column_int(statement, 0);
240 if (countAll > 0)
241 keyIdExist = true;
242 }
243
244 sqlite3_finalize(statement);
245
246 return keyIdExist;
247}
248
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700249void
Yingdi Yu87581582014-01-14 14:28:39 -0800250SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700251{
Yingdi Yu88663af2014-01-15 15:21:38 -0800252 if(keyName.empty())
253 throw Error("Incorrect key name " + keyName.toUri());
254
Yingdi Yu87581582014-01-14 14:28:39 -0800255 string keyId = keyName.get(-1).toEscapedString();
256 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700257
258
259 if (!doesIdentityExist(identityName))
260 addIdentity(identityName);
261
Yingdi Yu87581582014-01-14 14:28:39 -0800262 if (doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800263 throw Error("a key with the same name already exists!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700264
265 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800266 sqlite3_prepare_v2(m_database, "INSERT INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700267
268 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
269 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
270 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800271 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700272
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800273 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700274
275 sqlite3_finalize(statement);
276}
277
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800278ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800279SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700280{
Yingdi Yu87581582014-01-14 14:28:39 -0800281 if (!doesPublicKeyExist(keyName)) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700282 _LOG_DEBUG("keyName does not exist");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800283 return ptr_lib::shared_ptr<PublicKey>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700284 }
285
Yingdi Yu87581582014-01-14 14:28:39 -0800286 string keyId = keyName.get(-1).toEscapedString();
287 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700288
289 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800290 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 -0700291
292 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
293 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
294
295 int res = sqlite3_step(statement);
296
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800297 ptr_lib::shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700298 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800299 result = ptr_lib::make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700300
301 sqlite3_finalize(statement);
302
303 return result;
304}
305
306void
Yingdi Yu87581582014-01-14 14:28:39 -0800307SecPublicInfoSqlite3::updateKeyStatus(const Name& keyName, bool isActive)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700308{
Yingdi Yu88663af2014-01-15 15:21:38 -0800309 if(keyName.empty())
310 throw Error("Incorrect key name " + keyName.toUri());
311
Yingdi Yu87581582014-01-14 14:28:39 -0800312 string keyId = keyName.get(-1).toEscapedString();
313 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700314
315 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800316 sqlite3_prepare_v2(m_database, "UPDATE Key SET active=? WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700317
318 sqlite3_bind_int(statement, 1, (isActive ? 1 : 0));
319 sqlite3_bind_text(statement, 2, identityName.toUri(), SQLITE_TRANSIENT);
320 sqlite3_bind_text(statement, 3, keyId, SQLITE_TRANSIENT);
321
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800322 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700323
324 sqlite3_finalize(statement);
325}
326
327bool
Yingdi Yu87581582014-01-14 14:28:39 -0800328SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700329{
330 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800331 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700332
333 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
334
335 int res = sqlite3_step(statement);
336
337 bool certExist = false;
338 if (res == SQLITE_ROW) {
339 int countAll = sqlite3_column_int(statement, 0);
340 if (countAll > 0)
341 certExist = true;
342 }
343
344 sqlite3_finalize(statement);
345
346 return certExist;
347}
348
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700349void
Yingdi Yu87581582014-01-14 14:28:39 -0800350SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700351{
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800352 std::string certificateName = certificate.getName().toUri();
Yingdi Yu88663af2014-01-15 15:21:38 -0800353 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
354
355 if(keyName.empty())
356 throw Error("Incorrect key name " + keyName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700357
Yingdi Yu87581582014-01-14 14:28:39 -0800358 std::string keyId = keyName.get(-1).toEscapedString();
359 std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700360
361 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800362 sqlite3_prepare_v2(m_database,
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700363 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
364 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
365 -1, &statement, 0);
366
367
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800368 _LOG_DEBUG("certName: " << certificateName);
369 sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700370
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800371 // this will throw an exception if the signature is not the standard one or there is no key locator present
372 SignatureSha256WithRsa signature(certificate.getSignature());
373 std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700374
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800375 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
376
377 sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
378 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700379
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700380 // Convert from milliseconds to seconds since 1/1/1970.
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800381 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
382 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800384 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700385
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800386 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700387
388 sqlite3_finalize(statement);
389}
390
391void
Yingdi Yu87581582014-01-14 14:28:39 -0800392SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700393{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700394 const Name& certificateName = certificate.getName();
Yingdi Yu88663af2014-01-15 15:21:38 -0800395 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396
Yingdi Yu87581582014-01-14 14:28:39 -0800397 if (!doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800398 throw Error("No corresponding Key record for certificate!" + keyName.toUri() + " " + certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399
400 // Check if certificate has already existed!
401 if (doesCertificateExist(certificateName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800402 throw Error("Certificate has already been installed!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700403
Yingdi Yu87581582014-01-14 14:28:39 -0800404 string keyId = keyName.get(-1).toEscapedString();
405 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700406
407 // Check if the public key of certificate is the same as the key record
408
Yingdi Yu87581582014-01-14 14:28:39 -0800409 ptr_lib::shared_ptr<PublicKey> pubKey = getPublicKey(keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700410
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800411 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
412 throw Error("Certificate does not match the public key!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700413
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700414 // Insert the certificate
415 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800416 sqlite3_prepare_v2(m_database,
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700417 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
418 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
419 -1, &statement, 0);
420
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800421 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700422 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
423
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800424 // this will throw an exception if the signature is not the standard one or there is no key locator present
425 SignatureSha256WithRsa signature(certificate.getSignature());
426 std::string signerName = signature.getKeyLocator().getName().toUri();
427
428 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429
430 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800431 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700432
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700433 // Convert from milliseconds to seconds since 1/1/1970.
Yingdi Yu88663af2014-01-15 15:21:38 -0800434 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
435 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700436
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800437 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700438
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800439 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700440
441 sqlite3_finalize(statement);
442}
443
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800444ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800445SecPublicInfoSqlite3::getCertificate(const Name &certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700446{
Yingdi Yu4270f202014-01-28 14:19:16 -0800447 sqlite3_stmt *statement;
448
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800449 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800450 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
451 -1, &statement, 0);
452
453 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
454
455 int res = sqlite3_step(statement);
456
457 ptr_lib::shared_ptr<IdentityCertificate> certificate;
458 if (res == SQLITE_ROW)
459 {
460 certificate = ptr_lib::make_shared<IdentityCertificate>();
461 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
462 }
463 sqlite3_finalize(statement);
464
465 return certificate;
Jeff Thompson1975def2013-10-09 17:06:43 -0700466}
Yingdi Yu4270f202014-01-28 14:19:16 -0800467
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700468
469Name
Yingdi Yu87581582014-01-14 14:28:39 -0800470SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700471{
472 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800473 sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700474
475 int res = sqlite3_step(statement);
476
477 Name identity;
478
479 if (res == SQLITE_ROW)
480 identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
481
482 sqlite3_finalize(statement);
483
484 return identity;
485}
486
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700487void
Yingdi Yu87581582014-01-14 14:28:39 -0800488SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700489{
490 sqlite3_stmt *statement;
491
492 //Reset previous default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800493 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 -0700494
495 while (sqlite3_step(statement) == SQLITE_ROW)
496 {}
497
498 sqlite3_finalize(statement);
499
500 //Set current default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800501 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700502
503 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
504
505 sqlite3_step(statement);
506
507 sqlite3_finalize(statement);
508}
509
Yingdi Yu87581582014-01-14 14:28:39 -0800510Name
511SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700512{
Yingdi Yu87581582014-01-14 14:28:39 -0800513 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800514 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 -0700515
Yingdi Yu87581582014-01-14 14:28:39 -0800516 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
517
518 int res = sqlite3_step(statement);
519
520 Name keyName;
521
522 if (res == SQLITE_ROW)
523 keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
524
525 sqlite3_finalize(statement);
526
527 return keyName;
528}
529
530void
531SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
532{
Yingdi Yu88663af2014-01-15 15:21:38 -0800533 if(keyName.empty())
534 throw Error("Incorrect key name " + keyName.toUri());
535
Yingdi Yu87581582014-01-14 14:28:39 -0800536 string keyId = keyName.get(-1).toEscapedString();
537 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700538
539 sqlite3_stmt *statement;
540
541 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800542 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 -0700543
544 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
545
546 while (sqlite3_step(statement) == SQLITE_ROW)
547 {}
548
549 sqlite3_finalize(statement);
550
551 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800552 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 -0700553
554 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
555 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
556
557 sqlite3_step(statement);
558
559 sqlite3_finalize(statement);
560}
561
Yingdi Yu87581582014-01-14 14:28:39 -0800562Name
563SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700564{
Yingdi Yu88663af2014-01-15 15:21:38 -0800565 if(keyName.empty())
566 return Name();
567
Yingdi Yu87581582014-01-14 14:28:39 -0800568 string keyId = keyName.get(-1).toEscapedString();
569 Name identityName = keyName.getPrefix(-1);
570
571 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800572 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 -0800573
574 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
575 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
576
577 int res = sqlite3_step(statement);
578
579 Name certName;
580
581 if (res == SQLITE_ROW)
582 certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
583
584 sqlite3_finalize(statement);
585
586 return certName;
587}
588
589void
590SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
591{
592 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800593 if(keyName.empty())
594 throw Error("Incorrect key name for certificate " + certificateName.toUri());
595
Yingdi Yu87581582014-01-14 14:28:39 -0800596 string keyId = keyName.get(-1).toEscapedString();
597 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700598
599 sqlite3_stmt *statement;
600
601 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800602 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 -0700603
604 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
605 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
606
607 while (sqlite3_step(statement) == SQLITE_ROW)
608 {}
609
610 sqlite3_finalize(statement);
611
612 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800613 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 -0700614
615 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
616 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
617 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
618
619 sqlite3_step(statement);
620
621 sqlite3_finalize(statement);
622}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800623
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800624void
625SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800626{
627 sqlite3_stmt *stmt;
628 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800629 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800630 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800631 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800632
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800633 while(sqlite3_step (stmt) == SQLITE_ROW)
634 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 -0700635
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800636 sqlite3_finalize (stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700637}
638
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800639void
640SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800641{
642 sqlite3_stmt *stmt;
643 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800644 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 -0800645 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800646 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 -0800647
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800648 while(sqlite3_step (stmt) == SQLITE_ROW)
649 {
650 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
651 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
652 nameList.push_back(keyName);
653 }
654 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800655}
656
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800657void
658SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800659{
660 sqlite3_stmt *stmt;
661 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800662 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 -0800663 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800664 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 -0800665
666 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
667
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800668 while(sqlite3_step (stmt) == SQLITE_ROW)
669 {
670 Name keyName(identity);
671 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
672 nameList.push_back(keyName);
673 }
674 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800675}
676
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800677void
678SecPublicInfoSqlite3::getAllCertificateNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800679{
680 sqlite3_stmt *stmt;
681 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800682 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800683 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800684 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800685
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800686 while(sqlite3_step (stmt) == SQLITE_ROW)
687 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
688
689 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800690}
691
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800692void
693SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800694{
Yingdi Yu88663af2014-01-15 15:21:38 -0800695 if(keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800696 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800697
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800698 sqlite3_stmt *stmt;
699 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800700 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 -0800701 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800702 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 -0800703
Yingdi Yu87581582014-01-14 14:28:39 -0800704 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800705 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
706 std::string baseKeyName = keyName.get(-1).toEscapedString();
707 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
708
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800709 while(sqlite3_step (stmt) == SQLITE_ROW)
710 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
711
712 sqlite3_finalize (stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800713}
714
715void
716SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName)
717{
718 if(certName.empty())
719 return;
720
721 sqlite3_stmt *stmt;
722 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
723 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
724 sqlite3_step(stmt);
725 sqlite3_finalize (stmt);
726}
727
728void
729SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName)
730{
731 if(keyName.empty())
732 return;
733
734 string identity = keyName.getPrefix(-1).toUri();
735 string keyId = keyName.get(-1).toEscapedString();
736
737 sqlite3_stmt *stmt;
738 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
739 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
740 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
741 sqlite3_step(stmt);
742 sqlite3_finalize (stmt);
743
744 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
745 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
746 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
747 sqlite3_step(stmt);
748 sqlite3_finalize (stmt);
749}
750
751void
752SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName)
753{
754 string identity = identityName.toUri();
755
756 sqlite3_stmt *stmt;
757 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
758 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
759 sqlite3_step(stmt);
760 sqlite3_finalize (stmt);
761
762 sqlite3_prepare_v2(m_database, "DELETE FROM Key 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 Identity 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);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800771}
772
773} // namespace ndn
774