blob: bc16d9d709ce2be728b65f333b1c15dce3bbb049 [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
33namespace ndn
34{
35
36static const string INIT_ID_TABLE = "\
37CREATE TABLE IF NOT EXISTS \n \
38 Identity( \n \
39 identity_name BLOB NOT NULL, \n \
40 default_identity INTEGER DEFAULT 0, \n \
41 \
42 PRIMARY KEY (identity_name) \n \
43 ); \n \
44 \
45CREATE INDEX identity_index ON Identity(identity_name); \n \
46";
47
48static const string INIT_KEY_TABLE = "\
49CREATE TABLE IF NOT EXISTS \n \
50 Key( \n \
51 identity_name BLOB NOT NULL, \n \
52 key_identifier BLOB NOT NULL, \n \
53 key_type INTEGER, \n \
54 public_key BLOB, \n \
55 default_key INTEGER DEFAULT 0, \n \
56 active INTEGER DEFAULT 0, \n \
57 \
58 PRIMARY KEY (identity_name, key_identifier) \n \
59 ); \n \
60 \
61CREATE INDEX key_index ON Key(identity_name); \n \
62";
63
64static const string INIT_CERT_TABLE = "\
65CREATE TABLE IF NOT EXISTS \n \
66 Certificate( \n \
67 cert_name BLOB NOT NULL, \n \
68 cert_issuer BLOB NOT NULL, \n \
69 identity_name BLOB NOT NULL, \n \
70 key_identifier BLOB NOT NULL, \n \
71 not_before TIMESTAMP, \n \
72 not_after TIMESTAMP, \n \
73 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070074 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070075 default_cert INTEGER DEFAULT 0, \n \
76 \
77 PRIMARY KEY (cert_name) \n \
78 ); \n \
79 \
80CREATE INDEX cert_index ON Certificate(cert_name); \n \
81CREATE INDEX subject ON Certificate(identity_name); \n \
82";
83
84/**
85 * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size().
86 */
87static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*))
88{
89 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
90}
91
Yingdi Yu87581582014-01-14 14:28:39 -080092SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070093{
Yingdi Yu0b9c3152014-01-26 16:31:18 -080094 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndnx";
95 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070096
Alexander Afanasyev5b60f702014-02-07 12:55:24 -080097 /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
98 int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
99 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
Alexander Afanasyev44471462014-02-12 11:21:51 -0800100#ifdef NDN_CPP_DISABLE_SQLITE3_FS_LOCKING
101 "unix-dotfile"
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800102#else
103 0
104#endif
105 );
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700106 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800107 throw Error("identity DB cannot be opened/created");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700108
109 //Check if Key table exists;
110 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800111 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 -0700112 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700113
114 bool idTableExists = false;
115 if (res == SQLITE_ROW)
116 idTableExists = true;
117
118 sqlite3_finalize(statement);
119
120 if (!idTableExists) {
121 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800122 res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700123
124 if (res != SQLITE_OK && errorMessage != 0) {
125 _LOG_TRACE("Init \"error\" in Identity: " << errorMessage);
126 sqlite3_free(errorMessage);
127 }
128 }
129
130 //Check if Key table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800131 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 -0700132 res = sqlite3_step(statement);
133
134 bool keyTableExists = false;
135 if (res == SQLITE_ROW)
136 keyTableExists = true;
137
138 sqlite3_finalize(statement);
139
140 if (!keyTableExists) {
141 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800142 res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700143
144 if (res != SQLITE_OK && errorMessage != 0) {
145 _LOG_TRACE("Init \"error\" in KEY: " << errorMessage);
146 sqlite3_free(errorMessage);
147 }
148 }
149
150 //Check if Certificate table exists;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800151 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 -0700152 res = sqlite3_step(statement);
153
154 bool idCertificateTableExists = false;
155 if (res == SQLITE_ROW)
156 idCertificateTableExists = true;
157
158 sqlite3_finalize(statement);
159
160 if (!idCertificateTableExists) {
161 char *errorMessage = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800162 res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700163
164 if (res != SQLITE_OK && errorMessage != 0) {
165 _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage);
166 sqlite3_free(errorMessage);
167 }
168 }
169}
170
Yingdi Yu87581582014-01-14 14:28:39 -0800171SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700172{
173}
174
175bool
Yingdi Yu87581582014-01-14 14:28:39 -0800176SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700177{
178 bool result = false;
179
180 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800181 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700182
183 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
184 int res = sqlite3_step(statement);
185
186 if (res == SQLITE_ROW) {
187 int countAll = sqlite3_column_int(statement, 0);
188 if (countAll > 0)
189 result = true;
190 }
191
192 sqlite3_finalize(statement);
193
194 return result;
195}
196
197void
Yingdi Yu87581582014-01-14 14:28:39 -0800198SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700199{
200 if (doesIdentityExist(identityName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800201 throw Error("Identity already exists");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700202
203 sqlite3_stmt *statement;
204
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800205 sqlite3_prepare_v2(m_database, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700206
207 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
208
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800209 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700210
211 sqlite3_finalize(statement);
212}
213
214bool
Yingdi Yu87581582014-01-14 14:28:39 -0800215SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700216{
217 //TODO:
218 return false;
219}
220
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700221bool
Yingdi Yu87581582014-01-14 14:28:39 -0800222SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700223{
Yingdi Yu88663af2014-01-15 15:21:38 -0800224 if(keyName.empty())
225 throw Error("Incorrect key name " + keyName.toUri());
226
Yingdi Yu87581582014-01-14 14:28:39 -0800227 string keyId = keyName.get(-1).toEscapedString();
228 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700229
230 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800231 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 -0700232
233 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
234 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
235
236 int res = sqlite3_step(statement);
237
238 bool keyIdExist = false;
239 if (res == SQLITE_ROW) {
240 int countAll = sqlite3_column_int(statement, 0);
241 if (countAll > 0)
242 keyIdExist = true;
243 }
244
245 sqlite3_finalize(statement);
246
247 return keyIdExist;
248}
249
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700250void
Yingdi Yu87581582014-01-14 14:28:39 -0800251SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700252{
Yingdi Yu88663af2014-01-15 15:21:38 -0800253 if(keyName.empty())
254 throw Error("Incorrect key name " + keyName.toUri());
255
Yingdi Yu87581582014-01-14 14:28:39 -0800256 string keyId = keyName.get(-1).toEscapedString();
257 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700258
259
260 if (!doesIdentityExist(identityName))
261 addIdentity(identityName);
262
Yingdi Yu87581582014-01-14 14:28:39 -0800263 if (doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800264 throw Error("a key with the same name already exists!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700265
266 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800267 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 -0700268
269 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
270 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
271 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800272 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700273
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800274 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700275
276 sqlite3_finalize(statement);
277}
278
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800279ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800280SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700281{
Yingdi Yu87581582014-01-14 14:28:39 -0800282 if (!doesPublicKeyExist(keyName)) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700283 _LOG_DEBUG("keyName does not exist");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800284 return ptr_lib::shared_ptr<PublicKey>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700285 }
286
Yingdi Yu87581582014-01-14 14:28:39 -0800287 string keyId = keyName.get(-1).toEscapedString();
288 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700289
290 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800291 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 -0700292
293 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
294 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
295
296 int res = sqlite3_step(statement);
297
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800298 ptr_lib::shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700299 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800300 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 -0700301
302 sqlite3_finalize(statement);
303
304 return result;
305}
306
307void
Yingdi Yu87581582014-01-14 14:28:39 -0800308SecPublicInfoSqlite3::updateKeyStatus(const Name& keyName, bool isActive)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700309{
Yingdi Yu88663af2014-01-15 15:21:38 -0800310 if(keyName.empty())
311 throw Error("Incorrect key name " + keyName.toUri());
312
Yingdi Yu87581582014-01-14 14:28:39 -0800313 string keyId = keyName.get(-1).toEscapedString();
314 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700315
316 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800317 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 -0700318
319 sqlite3_bind_int(statement, 1, (isActive ? 1 : 0));
320 sqlite3_bind_text(statement, 2, identityName.toUri(), SQLITE_TRANSIENT);
321 sqlite3_bind_text(statement, 3, keyId, SQLITE_TRANSIENT);
322
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800323 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700324
325 sqlite3_finalize(statement);
326}
327
328bool
Yingdi Yu87581582014-01-14 14:28:39 -0800329SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700330{
331 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800332 sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700333
334 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
335
336 int res = sqlite3_step(statement);
337
338 bool certExist = false;
339 if (res == SQLITE_ROW) {
340 int countAll = sqlite3_column_int(statement, 0);
341 if (countAll > 0)
342 certExist = true;
343 }
344
345 sqlite3_finalize(statement);
346
347 return certExist;
348}
349
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700350void
Yingdi Yu87581582014-01-14 14:28:39 -0800351SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700352{
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800353 std::string certificateName = certificate.getName().toUri();
Yingdi Yu88663af2014-01-15 15:21:38 -0800354 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
355
356 if(keyName.empty())
357 throw Error("Incorrect key name " + keyName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700358
Yingdi Yu87581582014-01-14 14:28:39 -0800359 std::string keyId = keyName.get(-1).toEscapedString();
360 std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700361
362 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800363 sqlite3_prepare_v2(m_database,
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700364 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
365 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
366 -1, &statement, 0);
367
368
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800369 _LOG_DEBUG("certName: " << certificateName);
370 sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700371
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800372 // this will throw an exception if the signature is not the standard one or there is no key locator present
373 SignatureSha256WithRsa signature(certificate.getSignature());
374 std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700375
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800376 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
377
378 sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
379 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700380
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700381 // Convert from milliseconds to seconds since 1/1/1970.
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800382 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
383 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700384
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800385 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700386
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800387 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700388
389 sqlite3_finalize(statement);
390}
391
392void
Yingdi Yu87581582014-01-14 14:28:39 -0800393SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700394{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395 const Name& certificateName = certificate.getName();
Yingdi Yu88663af2014-01-15 15:21:38 -0800396 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700397
Yingdi Yu87581582014-01-14 14:28:39 -0800398 if (!doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800399 throw Error("No corresponding Key record for certificate!" + keyName.toUri() + " " + certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700400
401 // Check if certificate has already existed!
402 if (doesCertificateExist(certificateName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800403 throw Error("Certificate has already been installed!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700404
Yingdi Yu87581582014-01-14 14:28:39 -0800405 string keyId = keyName.get(-1).toEscapedString();
406 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700407
408 // Check if the public key of certificate is the same as the key record
409
Yingdi Yu87581582014-01-14 14:28:39 -0800410 ptr_lib::shared_ptr<PublicKey> pubKey = getPublicKey(keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700411
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800412 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
413 throw Error("Certificate does not match the public key!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700414
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700415 // Insert the certificate
416 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800417 sqlite3_prepare_v2(m_database,
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700418 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
419 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
420 -1, &statement, 0);
421
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800422 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700423 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
424
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800425 // this will throw an exception if the signature is not the standard one or there is no key locator present
426 SignatureSha256WithRsa signature(certificate.getSignature());
427 std::string signerName = signature.getKeyLocator().getName().toUri();
428
429 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700430
431 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800432 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700433
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700434 // Convert from milliseconds to seconds since 1/1/1970.
Yingdi Yu88663af2014-01-15 15:21:38 -0800435 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
436 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700437
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800438 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700439
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800440 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700441
442 sqlite3_finalize(statement);
443}
444
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800445ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800446SecPublicInfoSqlite3::getCertificate(const Name &certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700447{
Yingdi Yu4270f202014-01-28 14:19:16 -0800448 sqlite3_stmt *statement;
449
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800450 sqlite3_prepare_v2(m_database,
Yingdi Yu4270f202014-01-28 14:19:16 -0800451 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
452 -1, &statement, 0);
453
454 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
455
456 int res = sqlite3_step(statement);
457
458 ptr_lib::shared_ptr<IdentityCertificate> certificate;
459 if (res == SQLITE_ROW)
460 {
461 certificate = ptr_lib::make_shared<IdentityCertificate>();
462 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
463 }
464 sqlite3_finalize(statement);
465
466 return certificate;
Jeff Thompson1975def2013-10-09 17:06:43 -0700467}
Yingdi Yu4270f202014-01-28 14:19:16 -0800468
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469
470Name
Yingdi Yu87581582014-01-14 14:28:39 -0800471SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700472{
473 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800474 sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700475
476 int res = sqlite3_step(statement);
477
478 Name identity;
479
480 if (res == SQLITE_ROW)
481 identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
482
483 sqlite3_finalize(statement);
484
485 return identity;
486}
487
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700488void
Yingdi Yu87581582014-01-14 14:28:39 -0800489SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700490{
491 sqlite3_stmt *statement;
492
493 //Reset previous default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800494 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 -0700495
496 while (sqlite3_step(statement) == SQLITE_ROW)
497 {}
498
499 sqlite3_finalize(statement);
500
501 //Set current default identity
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800502 sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700503
504 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
505
506 sqlite3_step(statement);
507
508 sqlite3_finalize(statement);
509}
510
Yingdi Yu87581582014-01-14 14:28:39 -0800511Name
512SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700513{
Yingdi Yu87581582014-01-14 14:28:39 -0800514 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800515 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 -0700516
Yingdi Yu87581582014-01-14 14:28:39 -0800517 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
518
519 int res = sqlite3_step(statement);
520
521 Name keyName;
522
523 if (res == SQLITE_ROW)
524 keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
525
526 sqlite3_finalize(statement);
527
528 return keyName;
529}
530
531void
532SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
533{
Yingdi Yu88663af2014-01-15 15:21:38 -0800534 if(keyName.empty())
535 throw Error("Incorrect key name " + keyName.toUri());
536
Yingdi Yu87581582014-01-14 14:28:39 -0800537 string keyId = keyName.get(-1).toEscapedString();
538 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700539
540 sqlite3_stmt *statement;
541
542 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800543 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 -0700544
545 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
546
547 while (sqlite3_step(statement) == SQLITE_ROW)
548 {}
549
550 sqlite3_finalize(statement);
551
552 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800553 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 -0700554
555 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
556 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
557
558 sqlite3_step(statement);
559
560 sqlite3_finalize(statement);
561}
562
Yingdi Yu87581582014-01-14 14:28:39 -0800563Name
564SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700565{
Yingdi Yu88663af2014-01-15 15:21:38 -0800566 if(keyName.empty())
567 return Name();
568
Yingdi Yu87581582014-01-14 14:28:39 -0800569 string keyId = keyName.get(-1).toEscapedString();
570 Name identityName = keyName.getPrefix(-1);
571
572 sqlite3_stmt *statement;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800573 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 -0800574
575 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
576 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
577
578 int res = sqlite3_step(statement);
579
580 Name certName;
581
582 if (res == SQLITE_ROW)
583 certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
584
585 sqlite3_finalize(statement);
586
587 return certName;
588}
589
590void
591SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
592{
593 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800594 if(keyName.empty())
595 throw Error("Incorrect key name for certificate " + certificateName.toUri());
596
Yingdi Yu87581582014-01-14 14:28:39 -0800597 string keyId = keyName.get(-1).toEscapedString();
598 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700599
600 sqlite3_stmt *statement;
601
602 //Reset previous default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800603 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 -0700604
605 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
606 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
607
608 while (sqlite3_step(statement) == SQLITE_ROW)
609 {}
610
611 sqlite3_finalize(statement);
612
613 //Set current default Key
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800614 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 -0700615
616 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
617 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
618 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
619
620 sqlite3_step(statement);
621
622 sqlite3_finalize(statement);
623}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800624
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800625void
626SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800627{
628 sqlite3_stmt *stmt;
629 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800630 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800631 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800632 sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800633
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800634 while(sqlite3_step (stmt) == SQLITE_ROW)
635 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 -0700636
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800637 sqlite3_finalize (stmt);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700638}
639
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800640void
641SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800642{
643 sqlite3_stmt *stmt;
644 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800645 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 -0800646 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800647 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 -0800648
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800649 while(sqlite3_step (stmt) == SQLITE_ROW)
650 {
651 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
652 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
653 nameList.push_back(keyName);
654 }
655 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800656}
657
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800658void
659SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800660{
661 sqlite3_stmt *stmt;
662 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800663 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 -0800664 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800665 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 -0800666
667 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
668
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800669 while(sqlite3_step (stmt) == SQLITE_ROW)
670 {
671 Name keyName(identity);
672 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
673 nameList.push_back(keyName);
674 }
675 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800676}
677
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800678void
679SecPublicInfoSqlite3::getAllCertificateNames(vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800680{
681 sqlite3_stmt *stmt;
682 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800683 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800684 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800685 sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800686
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800687 while(sqlite3_step (stmt) == SQLITE_ROW)
688 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
689
690 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800691}
692
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800693void
694SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800695{
Yingdi Yu88663af2014-01-15 15:21:38 -0800696 if(keyName.empty())
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800697 return;
Yingdi Yu88663af2014-01-15 15:21:38 -0800698
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800699 sqlite3_stmt *stmt;
700 if(isDefault)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800701 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 -0800702 else
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800703 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 -0800704
Yingdi Yu87581582014-01-14 14:28:39 -0800705 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800706 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
707 std::string baseKeyName = keyName.get(-1).toEscapedString();
708 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
709
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800710 while(sqlite3_step (stmt) == SQLITE_ROW)
711 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
712
713 sqlite3_finalize (stmt);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800714}
715
716void
717SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName)
718{
719 if(certName.empty())
720 return;
721
722 sqlite3_stmt *stmt;
723 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
724 sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT);
725 sqlite3_step(stmt);
726 sqlite3_finalize (stmt);
727}
728
729void
730SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName)
731{
732 if(keyName.empty())
733 return;
734
735 string identity = keyName.getPrefix(-1).toUri();
736 string keyId = keyName.get(-1).toEscapedString();
737
738 sqlite3_stmt *stmt;
739 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
740 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
741 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
742 sqlite3_step(stmt);
743 sqlite3_finalize (stmt);
744
745 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0);
746 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
747 sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
748 sqlite3_step(stmt);
749 sqlite3_finalize (stmt);
750}
751
752void
753SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName)
754{
755 string identity = identityName.toUri();
756
757 sqlite3_stmt *stmt;
758 sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
759 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
760 sqlite3_step(stmt);
761 sqlite3_finalize (stmt);
762
763 sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
764 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
765 sqlite3_step(stmt);
766 sqlite3_finalize (stmt);
767
768 sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
769 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
770 sqlite3_step(stmt);
771 sqlite3_finalize (stmt);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800772}
773
774} // namespace ndn
775