blob: fe96f07584aa8a02b0d01194aab5116dcea08173 [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{
Yingdi Yu4270f202014-01-28 14:19:16 -0800436 sqlite3_stmt *statement;
437
438 sqlite3_prepare_v2(database_,
439 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
440 -1, &statement, 0);
441
442 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
443
444 int res = sqlite3_step(statement);
445
446 ptr_lib::shared_ptr<IdentityCertificate> certificate;
447 if (res == SQLITE_ROW)
448 {
449 certificate = ptr_lib::make_shared<IdentityCertificate>();
450 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
451 }
452 sqlite3_finalize(statement);
453
454 return certificate;
Jeff Thompson1975def2013-10-09 17:06:43 -0700455}
Yingdi Yu4270f202014-01-28 14:19:16 -0800456
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700457
458Name
Yingdi Yu87581582014-01-14 14:28:39 -0800459SecPublicInfoSqlite3::getDefaultIdentity()
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700460{
461 sqlite3_stmt *statement;
462 sqlite3_prepare_v2(database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
463
464 int res = sqlite3_step(statement);
465
466 Name identity;
467
468 if (res == SQLITE_ROW)
469 identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
470
471 sqlite3_finalize(statement);
472
473 return identity;
474}
475
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700476void
Yingdi Yu87581582014-01-14 14:28:39 -0800477SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700478{
479 sqlite3_stmt *statement;
480
481 //Reset previous default identity
482 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0);
483
484 while (sqlite3_step(statement) == SQLITE_ROW)
485 {}
486
487 sqlite3_finalize(statement);
488
489 //Set current default identity
490 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
491
492 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
493
494 sqlite3_step(statement);
495
496 sqlite3_finalize(statement);
497}
498
Yingdi Yu87581582014-01-14 14:28:39 -0800499Name
500SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700501{
Yingdi Yu87581582014-01-14 14:28:39 -0800502 sqlite3_stmt *statement;
503 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 -0700504
Yingdi Yu87581582014-01-14 14:28:39 -0800505 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
506
507 int res = sqlite3_step(statement);
508
509 Name keyName;
510
511 if (res == SQLITE_ROW)
512 keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
513
514 sqlite3_finalize(statement);
515
516 return keyName;
517}
518
519void
520SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
521{
Yingdi Yu88663af2014-01-15 15:21:38 -0800522 if(keyName.empty())
523 throw Error("Incorrect key name " + keyName.toUri());
524
Yingdi Yu87581582014-01-14 14:28:39 -0800525 string keyId = keyName.get(-1).toEscapedString();
526 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700527
528 sqlite3_stmt *statement;
529
530 //Reset previous default Key
531 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0);
532
533 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
534
535 while (sqlite3_step(statement) == SQLITE_ROW)
536 {}
537
538 sqlite3_finalize(statement);
539
540 //Set current default Key
541 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
542
543 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
544 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
545
546 sqlite3_step(statement);
547
548 sqlite3_finalize(statement);
549}
550
Yingdi Yu87581582014-01-14 14:28:39 -0800551Name
552SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700553{
Yingdi Yu88663af2014-01-15 15:21:38 -0800554 if(keyName.empty())
555 return Name();
556
Yingdi Yu87581582014-01-14 14:28:39 -0800557 string keyId = keyName.get(-1).toEscapedString();
558 Name identityName = keyName.getPrefix(-1);
559
560 sqlite3_stmt *statement;
561 sqlite3_prepare_v2(database_, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0);
562
563 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
564 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
565
566 int res = sqlite3_step(statement);
567
568 Name certName;
569
570 if (res == SQLITE_ROW)
571 certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
572
573 sqlite3_finalize(statement);
574
575 return certName;
576}
577
578void
579SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
580{
581 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800582 if(keyName.empty())
583 throw Error("Incorrect key name for certificate " + certificateName.toUri());
584
Yingdi Yu87581582014-01-14 14:28:39 -0800585 string keyId = keyName.get(-1).toEscapedString();
586 Name identityName = keyName.getPrefix(-1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700587
588 sqlite3_stmt *statement;
589
590 //Reset previous default Key
591 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0);
592
593 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
594 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
595
596 while (sqlite3_step(statement) == SQLITE_ROW)
597 {}
598
599 sqlite3_finalize(statement);
600
601 //Set current default Key
602 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0);
603
604 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
605 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
606 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
607
608 sqlite3_step(statement);
609
610 sqlite3_finalize(statement);
611}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800612
613vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800614SecPublicInfoSqlite3::getAllIdentities(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800615{
616 sqlite3_stmt *stmt;
617 if(isDefault)
618 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
619 else
620 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
621
622 vector<Name> nameList;
623 while(sqlite3_step (stmt) == SQLITE_ROW)
624 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 -0700625
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800626 sqlite3_finalize (stmt);
627 return nameList;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700628}
629
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800630vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800631SecPublicInfoSqlite3::getAllKeyNames(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800632{
633 sqlite3_stmt *stmt;
634 if(isDefault)
635 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0);
636 else
637 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0);
638
639 vector<Name> nameList;
640 while(sqlite3_step (stmt) == SQLITE_ROW)
641 {
642 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
643 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
644 nameList.push_back(keyName);
645 }
646 sqlite3_finalize (stmt);
647 return nameList;
648}
649
650vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800651SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800652{
653 sqlite3_stmt *stmt;
654 if(isDefault)
655 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0);
656 else
657 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0);
658
659 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
660
661 vector<Name> nameList;
662 while(sqlite3_step (stmt) == SQLITE_ROW)
663 {
664 Name keyName(identity);
665 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
666 nameList.push_back(keyName);
667 }
668 sqlite3_finalize (stmt);
669 return nameList;
670}
671
672vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800673SecPublicInfoSqlite3::getAllCertificateNames(bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800674{
675 sqlite3_stmt *stmt;
676 if(isDefault)
677 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
678 else
679 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
680
681 vector<Name> nameList;
682 while(sqlite3_step (stmt) == SQLITE_ROW)
683 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
684
685 sqlite3_finalize (stmt);
686 return nameList;
687}
688
689vector<Name>
Yingdi Yu87581582014-01-14 14:28:39 -0800690SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800691{
Yingdi Yu88663af2014-01-15 15:21:38 -0800692 if(keyName.empty())
693 return vector<Name>();
694
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800695 sqlite3_stmt *stmt;
696 if(isDefault)
697 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0);
698 else
699 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0);
700
Yingdi Yu87581582014-01-14 14:28:39 -0800701 Name identity = keyName.getPrefix(-1);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800702 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
703 std::string baseKeyName = keyName.get(-1).toEscapedString();
704 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
705
706 vector<Name> nameList;
707 while(sqlite3_step (stmt) == SQLITE_ROW)
708 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
709
710 sqlite3_finalize (stmt);
711 return nameList;
712}
713
714} // namespace ndn
715