blob: b7f1f7ed24500f44edb3c178dabc96d427e2733c [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
Yingdi Yu874678f2014-01-22 19:30:34 -08009#include <sqlite3.h>
Jeff Thompson351ac302013-10-19 18:45:00 -070010#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070011#include <stdlib.h>
12#include <sstream>
13#include <fstream>
Yingdi Yu0b9c3152014-01-26 16:31:18 -080014#include <boost/filesystem.hpp>
Yingdi Yu4f324632014-01-15 18:10:03 -080015#include "../util/logging.hpp"
Alexander Afanasyevd409d592014-01-28 18:36:38 -080016#include "../util/time.hpp"
17
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080018#include "data.hpp"
19#include "security/identity-certificate.hpp"
20#include "security/sec-public-info-sqlite3.hpp"
21#include "security/signature-sha256-with-rsa.hpp"
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -080022
Jeff Thompson7ca11f22013-10-04 19:01:30 -070023
Yingdi Yu87581582014-01-14 14:28:39 -080024INIT_LOGGER("BasicKeyMetaInfo");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070025
26using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070027
28namespace ndn
29{
30
31static const string INIT_ID_TABLE = "\
32CREATE TABLE IF NOT EXISTS \n \
33 Identity( \n \
34 identity_name BLOB NOT NULL, \n \
35 default_identity INTEGER DEFAULT 0, \n \
36 \
37 PRIMARY KEY (identity_name) \n \
38 ); \n \
39 \
40CREATE INDEX identity_index ON Identity(identity_name); \n \
41";
42
43static const string INIT_KEY_TABLE = "\
44CREATE TABLE IF NOT EXISTS \n \
45 Key( \n \
46 identity_name BLOB NOT NULL, \n \
47 key_identifier BLOB NOT NULL, \n \
48 key_type INTEGER, \n \
49 public_key BLOB, \n \
50 default_key INTEGER DEFAULT 0, \n \
51 active INTEGER DEFAULT 0, \n \
52 \
53 PRIMARY KEY (identity_name, key_identifier) \n \
54 ); \n \
55 \
56CREATE INDEX key_index ON Key(identity_name); \n \
57";
58
59static const string INIT_CERT_TABLE = "\
60CREATE TABLE IF NOT EXISTS \n \
61 Certificate( \n \
62 cert_name BLOB NOT NULL, \n \
63 cert_issuer BLOB NOT NULL, \n \
64 identity_name BLOB NOT NULL, \n \
65 key_identifier BLOB NOT NULL, \n \
66 not_before TIMESTAMP, \n \
67 not_after TIMESTAMP, \n \
68 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070069 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070070 default_cert INTEGER DEFAULT 0, \n \
71 \
72 PRIMARY KEY (cert_name) \n \
73 ); \n \
74 \
75CREATE INDEX cert_index ON Certificate(cert_name); \n \
76CREATE INDEX subject ON Certificate(identity_name); \n \
77";
78
79/**
80 * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size().
81 */
82static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*))
83{
84 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
85}
86
Yingdi Yu87581582014-01-14 14:28:39 -080087SecPublicInfoSqlite3::SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -070088{
Yingdi Yu0b9c3152014-01-26 16:31:18 -080089 boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndnx";
90 boost::filesystem::create_directories (identityDir);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070091
Yingdi Yu0b9c3152014-01-26 16:31:18 -080092 int res = sqlite3_open((identityDir / "ndnsec-public-info.db").c_str(), &database_);
Jeff Thompson7ca11f22013-10-04 19:01:30 -070093
94 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -080095 throw Error("identity DB cannot be opened/created");
Jeff Thompson7ca11f22013-10-04 19:01:30 -070096
97 //Check if Key table exists;
98 sqlite3_stmt *statement;
99 sqlite3_prepare_v2(database_, "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'", -1, &statement, 0);
Jeff Thompson351ac302013-10-19 18:45:00 -0700100 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700101
102 bool idTableExists = false;
103 if (res == SQLITE_ROW)
104 idTableExists = true;
105
106 sqlite3_finalize(statement);
107
108 if (!idTableExists) {
109 char *errorMessage = 0;
110 res = sqlite3_exec(database_, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
111
112 if (res != SQLITE_OK && errorMessage != 0) {
113 _LOG_TRACE("Init \"error\" in Identity: " << errorMessage);
114 sqlite3_free(errorMessage);
115 }
116 }
117
118 //Check if Key table exists;
119 sqlite3_prepare_v2(database_, "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", -1, &statement, 0);
120 res = sqlite3_step(statement);
121
122 bool keyTableExists = false;
123 if (res == SQLITE_ROW)
124 keyTableExists = true;
125
126 sqlite3_finalize(statement);
127
128 if (!keyTableExists) {
129 char *errorMessage = 0;
130 res = sqlite3_exec(database_, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
131
132 if (res != SQLITE_OK && errorMessage != 0) {
133 _LOG_TRACE("Init \"error\" in KEY: " << errorMessage);
134 sqlite3_free(errorMessage);
135 }
136 }
137
138 //Check if Certificate table exists;
139 sqlite3_prepare_v2(database_, "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", -1, &statement, 0);
140 res = sqlite3_step(statement);
141
142 bool idCertificateTableExists = false;
143 if (res == SQLITE_ROW)
144 idCertificateTableExists = true;
145
146 sqlite3_finalize(statement);
147
148 if (!idCertificateTableExists) {
149 char *errorMessage = 0;
150 res = sqlite3_exec(database_, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
151
152 if (res != SQLITE_OK && errorMessage != 0) {
153 _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage);
154 sqlite3_free(errorMessage);
155 }
156 }
157}
158
Yingdi Yu87581582014-01-14 14:28:39 -0800159SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700160{
161}
162
163bool
Yingdi Yu87581582014-01-14 14:28:39 -0800164SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700165{
166 bool result = false;
167
168 sqlite3_stmt *statement;
169 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0);
170
171 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
172 int res = sqlite3_step(statement);
173
174 if (res == SQLITE_ROW) {
175 int countAll = sqlite3_column_int(statement, 0);
176 if (countAll > 0)
177 result = true;
178 }
179
180 sqlite3_finalize(statement);
181
182 return result;
183}
184
185void
Yingdi Yu87581582014-01-14 14:28:39 -0800186SecPublicInfoSqlite3::addIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700187{
188 if (doesIdentityExist(identityName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800189 throw Error("Identity already exists");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700190
191 sqlite3_stmt *statement;
192
193 sqlite3_prepare_v2(database_, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0);
194
195 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
196
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800197 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700198
199 sqlite3_finalize(statement);
200}
201
202bool
Yingdi Yu87581582014-01-14 14:28:39 -0800203SecPublicInfoSqlite3::revokeIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700204{
205 //TODO:
206 return false;
207}
208
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700209bool
Yingdi Yu87581582014-01-14 14:28:39 -0800210SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700211{
Yingdi Yu88663af2014-01-15 15:21:38 -0800212 if(keyName.empty())
213 throw Error("Incorrect key name " + keyName.toUri());
214
Yingdi Yu87581582014-01-14 14:28:39 -0800215 string keyId = keyName.get(-1).toEscapedString();
216 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700217
218 sqlite3_stmt *statement;
219 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
220
221 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
222 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
223
224 int res = sqlite3_step(statement);
225
226 bool keyIdExist = false;
227 if (res == SQLITE_ROW) {
228 int countAll = sqlite3_column_int(statement, 0);
229 if (countAll > 0)
230 keyIdExist = true;
231 }
232
233 sqlite3_finalize(statement);
234
235 return keyIdExist;
236}
237
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700238void
Yingdi Yu87581582014-01-14 14:28:39 -0800239SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700240{
Yingdi Yu88663af2014-01-15 15:21:38 -0800241 if(keyName.empty())
242 throw Error("Incorrect key name " + keyName.toUri());
243
Yingdi Yu87581582014-01-14 14:28:39 -0800244 string keyId = keyName.get(-1).toEscapedString();
245 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700246
247
248 if (!doesIdentityExist(identityName))
249 addIdentity(identityName);
250
Yingdi Yu87581582014-01-14 14:28:39 -0800251 if (doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800252 throw Error("a key with the same name already exists!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700253
254 sqlite3_stmt *statement;
255 sqlite3_prepare_v2(database_, "INSERT INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0);
256
257 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
258 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
259 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800260 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700261
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800262 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700263
264 sqlite3_finalize(statement);
265}
266
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800267ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -0800268SecPublicInfoSqlite3::getPublicKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700269{
Yingdi Yu87581582014-01-14 14:28:39 -0800270 if (!doesPublicKeyExist(keyName)) {
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700271 _LOG_DEBUG("keyName does not exist");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800272 return ptr_lib::shared_ptr<PublicKey>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700273 }
274
Yingdi Yu87581582014-01-14 14:28:39 -0800275 string keyId = keyName.get(-1).toEscapedString();
276 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700277
278 sqlite3_stmt *statement;
279 sqlite3_prepare_v2(database_, "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
280
281 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
282 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
283
284 int res = sqlite3_step(statement);
285
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800286 ptr_lib::shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700287 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800288 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 -0700289
290 sqlite3_finalize(statement);
291
292 return result;
293}
294
295void
Yingdi Yu87581582014-01-14 14:28:39 -0800296SecPublicInfoSqlite3::updateKeyStatus(const Name& keyName, bool isActive)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700297{
Yingdi Yu88663af2014-01-15 15:21:38 -0800298 if(keyName.empty())
299 throw Error("Incorrect key name " + keyName.toUri());
300
Yingdi Yu87581582014-01-14 14:28:39 -0800301 string keyId = keyName.get(-1).toEscapedString();
302 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700303
304 sqlite3_stmt *statement;
305 sqlite3_prepare_v2(database_, "UPDATE Key SET active=? WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
306
307 sqlite3_bind_int(statement, 1, (isActive ? 1 : 0));
308 sqlite3_bind_text(statement, 2, identityName.toUri(), SQLITE_TRANSIENT);
309 sqlite3_bind_text(statement, 3, keyId, SQLITE_TRANSIENT);
310
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800311 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700312
313 sqlite3_finalize(statement);
314}
315
316bool
Yingdi Yu87581582014-01-14 14:28:39 -0800317SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700318{
319 sqlite3_stmt *statement;
320 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
321
322 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
323
324 int res = sqlite3_step(statement);
325
326 bool certExist = false;
327 if (res == SQLITE_ROW) {
328 int countAll = sqlite3_column_int(statement, 0);
329 if (countAll > 0)
330 certExist = true;
331 }
332
333 sqlite3_finalize(statement);
334
335 return certExist;
336}
337
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700338void
Yingdi Yu87581582014-01-14 14:28:39 -0800339SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700340{
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800341 std::string certificateName = certificate.getName().toUri();
Yingdi Yu88663af2014-01-15 15:21:38 -0800342 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
343
344 if(keyName.empty())
345 throw Error("Incorrect key name " + keyName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700346
Yingdi Yu87581582014-01-14 14:28:39 -0800347 std::string keyId = keyName.get(-1).toEscapedString();
348 std::string identityName = keyName.getPrefix(-1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700349
350 sqlite3_stmt *statement;
351 sqlite3_prepare_v2(database_,
352 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
353 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
354 -1, &statement, 0);
355
356
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800357 _LOG_DEBUG("certName: " << certificateName);
358 sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700359
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800360 // this will throw an exception if the signature is not the standard one or there is no key locator present
361 SignatureSha256WithRsa signature(certificate.getSignature());
362 std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700363
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800364 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
365
366 sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
367 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700368
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700369 // Convert from milliseconds to seconds since 1/1/1970.
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800370 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
371 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700372
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800373 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700374
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800375 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700376
377 sqlite3_finalize(statement);
378}
379
380void
Yingdi Yu87581582014-01-14 14:28:39 -0800381SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700382{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700383 const Name& certificateName = certificate.getName();
Yingdi Yu88663af2014-01-15 15:21:38 -0800384 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700385
Yingdi Yu87581582014-01-14 14:28:39 -0800386 if (!doesPublicKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800387 throw Error("No corresponding Key record for certificate!" + keyName.toUri() + " " + certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700388
389 // Check if certificate has already existed!
390 if (doesCertificateExist(certificateName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800391 throw Error("Certificate has already been installed!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700392
Yingdi Yu87581582014-01-14 14:28:39 -0800393 string keyId = keyName.get(-1).toEscapedString();
394 Name identity = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395
396 // Check if the public key of certificate is the same as the key record
397
Yingdi Yu87581582014-01-14 14:28:39 -0800398 ptr_lib::shared_ptr<PublicKey> pubKey = getPublicKey(keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800400 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
401 throw Error("Certificate does not match the public key!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700402
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700403 // Insert the certificate
404 sqlite3_stmt *statement;
405 sqlite3_prepare_v2(database_,
406 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
407 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
408 -1, &statement, 0);
409
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800410 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700411 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
412
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800413 // this will throw an exception if the signature is not the standard one or there is no key locator present
414 SignatureSha256WithRsa signature(certificate.getSignature());
415 std::string signerName = signature.getKeyLocator().getName().toUri();
416
417 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700418
419 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800420 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700421
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700422 // Convert from milliseconds to seconds since 1/1/1970.
Yingdi Yu88663af2014-01-15 15:21:38 -0800423 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
424 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700425
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800426 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700427
Alexander Afanasyev736708b2014-01-06 14:45:34 -0800428 sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700429
430 sqlite3_finalize(statement);
431}
432
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800433ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800434SecPublicInfoSqlite3::getCertificate(const Name &certificateName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700435{
436 if (doesCertificateExist(certificateName)) {
437 sqlite3_stmt *statement;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700438
Yingdi Yu88663af2014-01-15 15:21:38 -0800439 sqlite3_prepare_v2(database_,
440 "SELECT certificate_data FROM Certificate \
441 WHERE cert_name=? AND not_before<datetime('now') AND not_after>datetime('now') and valid_flag=1",
442 -1, &statement, 0);
443
444 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700445
446 int res = sqlite3_step(statement);
447
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800448 ptr_lib::shared_ptr<IdentityCertificate> certificate = ptr_lib::make_shared<IdentityCertificate>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700449 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800450 {
451 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
452 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700453 sqlite3_finalize(statement);
454
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800455 return certificate;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700456 }
457 else {
458 _LOG_DEBUG("Certificate does not exist!");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800459 return ptr_lib::shared_ptr<IdentityCertificate>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700461}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700462
463Name
Yingdi Yu87581582014-01-14 14:28:39 -0800464SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700465{
466 sqlite3_stmt *statement;
467 sqlite3_prepare_v2(database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
468
469 int res = sqlite3_step(statement);
470
471 Name identity;
472
473 if (res == SQLITE_ROW)
474 identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
475
476 sqlite3_finalize(statement);
477
478 return identity;
479}
480
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700481void
Yingdi Yu87581582014-01-14 14:28:39 -0800482SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700483{
484 sqlite3_stmt *statement;
485
486 //Reset previous default identity
487 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0);
488
489 while (sqlite3_step(statement) == SQLITE_ROW)
490 {}
491
492 sqlite3_finalize(statement);
493
494 //Set current default identity
495 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
496
497 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
498
499 sqlite3_step(statement);
500
501 sqlite3_finalize(statement);
502}
503
Yingdi Yu87581582014-01-14 14:28:39 -0800504Name
505SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700506{
Yingdi Yu87581582014-01-14 14:28:39 -0800507 sqlite3_stmt *statement;
508 sqlite3_prepare_v2(database_, "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", -1, &statement, 0);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700509
Yingdi Yu87581582014-01-14 14:28:39 -0800510 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
511
512 int res = sqlite3_step(statement);
513
514 Name keyName;
515
516 if (res == SQLITE_ROW)
517 keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
518
519 sqlite3_finalize(statement);
520
521 return keyName;
522}
523
524void
525SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
526{
Yingdi Yu88663af2014-01-15 15:21:38 -0800527 if(keyName.empty())
528 throw Error("Incorrect key name " + keyName.toUri());
529
Yingdi Yu87581582014-01-14 14:28:39 -0800530 string keyId = keyName.get(-1).toEscapedString();
531 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700532
533 sqlite3_stmt *statement;
534
535 //Reset previous default Key
536 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0);
537
538 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
539
540 while (sqlite3_step(statement) == SQLITE_ROW)
541 {}
542
543 sqlite3_finalize(statement);
544
545 //Set current default Key
546 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
547
548 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
549 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
550
551 sqlite3_step(statement);
552
553 sqlite3_finalize(statement);
554}
555
Yingdi Yu87581582014-01-14 14:28:39 -0800556Name
557SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700558{
Yingdi Yu88663af2014-01-15 15:21:38 -0800559 if(keyName.empty())
560 return Name();
561
Yingdi Yu87581582014-01-14 14:28:39 -0800562 string keyId = keyName.get(-1).toEscapedString();
563 Name identityName = keyName.getPrefix(-1);
564
565 sqlite3_stmt *statement;
566 sqlite3_prepare_v2(database_, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0);
567
568 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
569 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
570
571 int res = sqlite3_step(statement);
572
573 Name certName;
574
575 if (res == SQLITE_ROW)
576 certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
577
578 sqlite3_finalize(statement);
579
580 return certName;
581}
582
583void
584SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
585{
586 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800587 if(keyName.empty())
588 throw Error("Incorrect key name for certificate " + certificateName.toUri());
589
Yingdi Yu87581582014-01-14 14:28:39 -0800590 string keyId = keyName.get(-1).toEscapedString();
591 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700592
593 sqlite3_stmt *statement;
594
595 //Reset previous default Key
596 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0);
597
598 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
599 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
600
601 while (sqlite3_step(statement) == SQLITE_ROW)
602 {}
603
604 sqlite3_finalize(statement);
605
606 //Set current default Key
607 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0);
608
609 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
610 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
611 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
612
613 sqlite3_step(statement);
614
615 sqlite3_finalize(statement);
616}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800617
618vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800619SecPublicInfoSqlite3::getAllIdentities(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800620{
621 sqlite3_stmt *stmt;
622 if(isDefault)
623 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
624 else
625 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
626
627 vector<Name> nameList;
628 while(sqlite3_step (stmt) == SQLITE_ROW)
629 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 -0700630
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800631 sqlite3_finalize (stmt);
632 return nameList;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700633}
634
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800635vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800636SecPublicInfoSqlite3::getAllKeyNames(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800637{
638 sqlite3_stmt *stmt;
639 if(isDefault)
640 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0);
641 else
642 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0);
643
644 vector<Name> nameList;
645 while(sqlite3_step (stmt) == SQLITE_ROW)
646 {
647 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
648 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
649 nameList.push_back(keyName);
650 }
651 sqlite3_finalize (stmt);
652 return nameList;
653}
654
655vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800656SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800657{
658 sqlite3_stmt *stmt;
659 if(isDefault)
660 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0);
661 else
662 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0);
663
664 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
665
666 vector<Name> nameList;
667 while(sqlite3_step (stmt) == SQLITE_ROW)
668 {
669 Name keyName(identity);
670 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
671 nameList.push_back(keyName);
672 }
673 sqlite3_finalize (stmt);
674 return nameList;
675}
676
677vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800678SecPublicInfoSqlite3::getAllCertificateNames(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800679{
680 sqlite3_stmt *stmt;
681 if(isDefault)
682 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
683 else
684 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
685
686 vector<Name> nameList;
687 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);
691 return nameList;
692}
693
694vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800695SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800696{
Yingdi Yu88663af2014-01-15 15:21:38 -0800697 if(keyName.empty())
698 return vector<Name>();
699
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800700 sqlite3_stmt *stmt;
701 if(isDefault)
702 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0);
703 else
704 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0);
705
Yingdi Yu87581582014-01-14 14:28:39 -0800706 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800707 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
708 std::string baseKeyName = keyName.get(-1).toEscapedString();
709 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
710
711 vector<Name> nameList;
712 while(sqlite3_step (stmt) == SQLITE_ROW)
713 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
714
715 sqlite3_finalize (stmt);
716 return nameList;
717}
718
719} // namespace ndn
720