blob: da0e991b69143c3db94a0795ce6bb65238c2d513 [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
Jeff Thompsonb7523002013-10-09 10:25:00 -07009// Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3.
Jeff Thompson6e229042013-10-10 11:09:49 -070010#include <ndn-cpp/ndn-cpp-config.h>
Jeff Thompson1975def2013-10-09 17:06:43 -070011#ifdef NDN_CPP_HAVE_SQLITE3
Jeff Thompson7ca11f22013-10-04 19:01:30 -070012
Jeff Thompson351ac302013-10-19 18:45:00 -070013#include <stdio.h>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070014#include <stdlib.h>
15#include <sstream>
16#include <fstream>
Jeff Thompson25b4e612013-10-10 16:03:24 -070017#include <ndn-cpp/security/identity/basic-identity-storage.hpp>
Jeff Thompson7ca11f22013-10-04 19:01:30 -070018#include "../../util/logging.hpp"
Jeff Thompson7ca11f22013-10-04 19:01:30 -070019#include "ndn-cpp/data.hpp"
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -070020#include <ndn-cpp/security/certificate/identity-certificate.hpp>
Jeff Thompson3bd90bc2013-10-19 16:40:14 -070021#include "../../c/util/time.h"
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -080022#include <ndn-cpp/signature/signature-sha256-with-rsa.hpp>
23
Jeff Thompson7ca11f22013-10-04 19:01:30 -070024
25INIT_LOGGER("BasicIdentityStorage");
26
27using namespace std;
Jeff Thompson7ca11f22013-10-04 19:01:30 -070028
29namespace ndn
30{
31
32static const string INIT_ID_TABLE = "\
33CREATE TABLE IF NOT EXISTS \n \
34 Identity( \n \
35 identity_name BLOB NOT NULL, \n \
36 default_identity INTEGER DEFAULT 0, \n \
37 \
38 PRIMARY KEY (identity_name) \n \
39 ); \n \
40 \
41CREATE INDEX identity_index ON Identity(identity_name); \n \
42";
43
44static const string INIT_KEY_TABLE = "\
45CREATE TABLE IF NOT EXISTS \n \
46 Key( \n \
47 identity_name BLOB NOT NULL, \n \
48 key_identifier BLOB NOT NULL, \n \
49 key_type INTEGER, \n \
50 public_key BLOB, \n \
51 default_key INTEGER DEFAULT 0, \n \
52 active INTEGER DEFAULT 0, \n \
53 \
54 PRIMARY KEY (identity_name, key_identifier) \n \
55 ); \n \
56 \
57CREATE INDEX key_index ON Key(identity_name); \n \
58";
59
60static const string INIT_CERT_TABLE = "\
61CREATE TABLE IF NOT EXISTS \n \
62 Certificate( \n \
63 cert_name BLOB NOT NULL, \n \
64 cert_issuer BLOB NOT NULL, \n \
65 identity_name BLOB NOT NULL, \n \
66 key_identifier BLOB NOT NULL, \n \
67 not_before TIMESTAMP, \n \
68 not_after TIMESTAMP, \n \
69 certificate_data BLOB NOT NULL, \n \
Jeff Thompson22285ec2013-10-22 17:43:02 -070070 valid_flag INTEGER DEFAULT 1, \n \
Jeff Thompson7ca11f22013-10-04 19:01:30 -070071 default_cert INTEGER DEFAULT 0, \n \
72 \
73 PRIMARY KEY (cert_name) \n \
74 ); \n \
75 \
76CREATE INDEX cert_index ON Certificate(cert_name); \n \
77CREATE INDEX subject ON Certificate(identity_name); \n \
78";
79
80/**
81 * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size().
82 */
83static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*))
84{
85 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
86}
87
88BasicIdentityStorage::BasicIdentityStorage()
89{
Jeff Thompson351ac302013-10-19 18:45:00 -070090 // Note: We don't use <filesystem> support because it is not "header-only" and require linking to libraries.
Jeff Thompsonab5440f2013-10-22 11:54:00 -070091 // TODO: Handle non-unix file system paths which don't use '/'.
Jeff Thompson351ac302013-10-19 18:45:00 -070092 const char* home = getenv("HOME");
93 if (!home || *home == '\0')
94 // Don't expect this to happen;
95 home = ".";
96 string homeDir(home);
97 if (homeDir[homeDir.size() - 1] == '/')
98 // Strip the ending '/'.
99 homeDir.erase(homeDir.size() - 1);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700100
Jeff Thompsonac64b132013-11-25 15:04:53 -0800101 string identityDir = homeDir + '/' + ".ndnx";
102 // TODO: Handle non-unix file systems which don't have "mkdir -p".
Jeff Thompsonab5440f2013-10-22 11:54:00 -0700103 ::system(("mkdir -p " + identityDir).c_str());
Jeff Thompson351ac302013-10-19 18:45:00 -0700104
Jeff Thompsonac64b132013-11-25 15:04:53 -0800105 int res = sqlite3_open((identityDir + '/' + "ndnsec-identity.db").c_str(), &database_);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700106
107 if (res != SQLITE_OK)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800108 throw Error("identity DB cannot be opened/created");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700109
110 //Check if Key table exists;
111 sqlite3_stmt *statement;
112 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 -0700113 res = sqlite3_step(statement);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700114
115 bool idTableExists = false;
116 if (res == SQLITE_ROW)
117 idTableExists = true;
118
119 sqlite3_finalize(statement);
120
121 if (!idTableExists) {
122 char *errorMessage = 0;
123 res = sqlite3_exec(database_, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
124
125 if (res != SQLITE_OK && errorMessage != 0) {
126 _LOG_TRACE("Init \"error\" in Identity: " << errorMessage);
127 sqlite3_free(errorMessage);
128 }
129 }
130
131 //Check if Key table exists;
132 sqlite3_prepare_v2(database_, "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", -1, &statement, 0);
133 res = sqlite3_step(statement);
134
135 bool keyTableExists = false;
136 if (res == SQLITE_ROW)
137 keyTableExists = true;
138
139 sqlite3_finalize(statement);
140
141 if (!keyTableExists) {
142 char *errorMessage = 0;
143 res = sqlite3_exec(database_, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
144
145 if (res != SQLITE_OK && errorMessage != 0) {
146 _LOG_TRACE("Init \"error\" in KEY: " << errorMessage);
147 sqlite3_free(errorMessage);
148 }
149 }
150
151 //Check if Certificate table exists;
152 sqlite3_prepare_v2(database_, "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", -1, &statement, 0);
153 res = sqlite3_step(statement);
154
155 bool idCertificateTableExists = false;
156 if (res == SQLITE_ROW)
157 idCertificateTableExists = true;
158
159 sqlite3_finalize(statement);
160
161 if (!idCertificateTableExists) {
162 char *errorMessage = 0;
163 res = sqlite3_exec(database_, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
164
165 if (res != SQLITE_OK && errorMessage != 0) {
166 _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage);
167 sqlite3_free(errorMessage);
168 }
169 }
170}
171
172BasicIdentityStorage::~BasicIdentityStorage()
173{
174}
175
176bool
177BasicIdentityStorage::doesIdentityExist(const Name& identityName)
178{
179 bool result = false;
180
181 sqlite3_stmt *statement;
182 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0);
183
184 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
185 int res = sqlite3_step(statement);
186
187 if (res == SQLITE_ROW) {
188 int countAll = sqlite3_column_int(statement, 0);
189 if (countAll > 0)
190 result = true;
191 }
192
193 sqlite3_finalize(statement);
194
195 return result;
196}
197
198void
199BasicIdentityStorage::addIdentity(const Name& identityName)
200{
201 if (doesIdentityExist(identityName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800202 throw Error("Identity already exists");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700203
204 sqlite3_stmt *statement;
205
206 sqlite3_prepare_v2(database_, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0);
207
208 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
209
210 int res = sqlite3_step(statement);
211
212 sqlite3_finalize(statement);
213}
214
215bool
216BasicIdentityStorage::revokeIdentity()
217{
218 //TODO:
219 return false;
220}
221
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700222bool
223BasicIdentityStorage::doesKeyExist(const Name& keyName)
224{
225 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
226 Name identityName = keyName.getSubName(0, keyName.size() - 1);
227
228 sqlite3_stmt *statement;
229 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
230
231 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
232 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
233
234 int res = sqlite3_step(statement);
235
236 bool keyIdExist = false;
237 if (res == SQLITE_ROW) {
238 int countAll = sqlite3_column_int(statement, 0);
239 if (countAll > 0)
240 keyIdExist = true;
241 }
242
243 sqlite3_finalize(statement);
244
245 return keyIdExist;
246}
247
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700248void
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800249BasicIdentityStorage::addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700250{
251 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
252 Name identityName = keyName.getSubName(0, keyName.size() - 1);
253
254
255 if (!doesIdentityExist(identityName))
256 addIdentity(identityName);
257
258 if (doesKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800259 throw Error("a key with the same name already exists!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700260
261 sqlite3_stmt *statement;
262 sqlite3_prepare_v2(database_, "INSERT INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0);
263
264 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
265 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
266 sqlite3_bind_int(statement, 3, (int)keyType);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800267 sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700268
269 int res = sqlite3_step(statement);
270
271 sqlite3_finalize(statement);
272}
273
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800274ptr_lib::shared_ptr<PublicKey>
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700275BasicIdentityStorage::getKey(const Name& keyName)
276{
277 if (!doesKeyExist(keyName)) {
278 _LOG_DEBUG("keyName does not exist");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800279 return ptr_lib::shared_ptr<PublicKey>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700280 }
281
282 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
283 Name identityName = keyName.getSubName(0, keyName.size() - 1);
284
285 sqlite3_stmt *statement;
286 sqlite3_prepare_v2(database_, "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
287
288 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
289 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
290
291 int res = sqlite3_step(statement);
292
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800293 ptr_lib::shared_ptr<PublicKey> result;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700294 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800295 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 -0700296
297 sqlite3_finalize(statement);
298
299 return result;
300}
301
302void
303BasicIdentityStorage::activateKey(const Name& keyName)
304{
305 updateKeyStatus(keyName, true);
306}
307
308void
309BasicIdentityStorage::deactivateKey(const Name& keyName)
310{
311 updateKeyStatus(keyName, false);
312}
313
314void
315BasicIdentityStorage::updateKeyStatus(const Name& keyName, bool isActive)
316{
317 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
318 Name identityName = keyName.getSubName(0, keyName.size() - 1);
319
320 sqlite3_stmt *statement;
321 sqlite3_prepare_v2(database_, "UPDATE Key SET active=? WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
322
323 sqlite3_bind_int(statement, 1, (isActive ? 1 : 0));
324 sqlite3_bind_text(statement, 2, identityName.toUri(), SQLITE_TRANSIENT);
325 sqlite3_bind_text(statement, 3, keyId, SQLITE_TRANSIENT);
326
327 int res = sqlite3_step(statement);
328
329 sqlite3_finalize(statement);
330}
331
332bool
333BasicIdentityStorage::doesCertificateExist(const Name& certificateName)
334{
335 sqlite3_stmt *statement;
336 sqlite3_prepare_v2(database_, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0);
337
338 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
339
340 int res = sqlite3_step(statement);
341
342 bool certExist = false;
343 if (res == SQLITE_ROW) {
344 int countAll = sqlite3_column_int(statement, 0);
345 if (countAll > 0)
346 certExist = true;
347 }
348
349 sqlite3_finalize(statement);
350
351 return certExist;
352}
353
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700354void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700355BasicIdentityStorage::addAnyCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700356{
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800357 std::string certificateName = certificate.getName().toUri();
Jeff Thompson22285ec2013-10-22 17:43:02 -0700358 Name keyName = certificate.getPublicKeyName();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700359
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800360 std::string keyId = keyName.get(keyName.size() - 1).toEscapedString();
361 std::string identityName = keyName.getSubName(0, keyName.size() - 1).toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700362
363 sqlite3_stmt *statement;
364 sqlite3_prepare_v2(database_,
365 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
366 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
367 -1, &statement, 0);
368
369
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800370 _LOG_DEBUG("certName: " << certificateName);
371 sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700372
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800373 // this will throw an exception if the signature is not the standard one or there is no key locator present
374 SignatureSha256WithRsa signature(certificate.getSignature());
375 std::string signerName = signature.getKeyLocator().getName().toUri();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700376
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800377 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
378
379 sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
380 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700381
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700382 // Convert from milliseconds to seconds since 1/1/1970.
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800383 sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
384 sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700385
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800386 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700387
388 int res = sqlite3_step(statement);
389
390 sqlite3_finalize(statement);
391}
392
393void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700394BasicIdentityStorage::addCertificate(const IdentityCertificate& certificate)
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700395{
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700396 const Name& certificateName = certificate.getName();
Jeff Thompson22285ec2013-10-22 17:43:02 -0700397 Name keyName = certificate.getPublicKeyName();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700398
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700399 if (!doesKeyExist(keyName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800400 throw Error("No corresponding Key record for certificate!" + keyName.toUri() + " " + certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700401
402 // Check if certificate has already existed!
403 if (doesCertificateExist(certificateName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800404 throw Error("Certificate has already been installed!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700405
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700406 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
407 Name identity = keyName.getSubName(0, keyName.size() - 1);
408
409 // Check if the public key of certificate is the same as the key record
410
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800411 ptr_lib::shared_ptr<PublicKey> pubKey = getKey(keyName);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700412
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800413 if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo())
414 throw Error("Certificate does not match the public key!");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700415
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700416 // Insert the certificate
417 sqlite3_stmt *statement;
418 sqlite3_prepare_v2(database_,
419 "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\
420 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
421 -1, &statement, 0);
422
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800423 _LOG_DEBUG("certName: " << certificateName.toUri());
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700424 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
425
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800426 // this will throw an exception if the signature is not the standard one or there is no key locator present
427 SignatureSha256WithRsa signature(certificate.getSignature());
428 std::string signerName = signature.getKeyLocator().getName().toUri();
429
430 sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700431
432 sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800433 sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700434
Jeff Thompson9f2b9fc2013-10-19 18:00:12 -0700435 // Convert from milliseconds to seconds since 1/1/1970.
436 sqlite3_bind_int64(statement, 5, (sqlite3_int64)floor(certificate.getNotBefore() / 1000.0));
437 sqlite3_bind_int64(statement, 6, (sqlite3_int64)floor(certificate.getNotAfter() / 1000.0));
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700438
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800439 sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700440
441 int res = sqlite3_step(statement);
442
443 sqlite3_finalize(statement);
444}
445
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800446ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700447BasicIdentityStorage::getCertificate(const Name &certificateName, bool allowAny)
448{
449 if (doesCertificateExist(certificateName)) {
450 sqlite3_stmt *statement;
451 if (!allowAny) {
452 sqlite3_prepare_v2(database_,
453 "SELECT certificate_data FROM Certificate \
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800454 WHERE cert_name=? AND not_before<datetime('now') AND not_after>datetime('now') and valid_flag=1",
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700455 -1, &statement, 0);
456
457 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700458 }
459 else {
460 sqlite3_prepare_v2(database_,
461 "SELECT certificate_data FROM Certificate WHERE cert_name=?", -1, &statement, 0);
462
463 sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
464 }
465
466 int res = sqlite3_step(statement);
467
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800468 ptr_lib::shared_ptr<IdentityCertificate> certificate = ptr_lib::make_shared<IdentityCertificate>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700469 if (res == SQLITE_ROW)
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800470 {
471 certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
472 }
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700473 sqlite3_finalize(statement);
474
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800475 return certificate;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700476 }
477 else {
478 _LOG_DEBUG("Certificate does not exist!");
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800479 return ptr_lib::shared_ptr<IdentityCertificate>();
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700480 }
Jeff Thompson1975def2013-10-09 17:06:43 -0700481}
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700482
483Name
484BasicIdentityStorage::getDefaultIdentity()
485{
486 sqlite3_stmt *statement;
487 sqlite3_prepare_v2(database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0);
488
489 int res = sqlite3_step(statement);
490
491 Name identity;
492
493 if (res == SQLITE_ROW)
494 identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
495
496 sqlite3_finalize(statement);
497
498 return identity;
499}
500
501Name
502BasicIdentityStorage::getDefaultKeyNameForIdentity(const Name& identityName)
503{
504 sqlite3_stmt *statement;
505 sqlite3_prepare_v2(database_, "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", -1, &statement, 0);
506
507 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
508
509 int res = sqlite3_step(statement);
510
511 Name keyName;
512
513 if (res == SQLITE_ROW)
514 keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
515
516 sqlite3_finalize(statement);
517
518 return keyName;
519}
520
521Name
522BasicIdentityStorage::getDefaultCertificateNameForKey(const Name& keyName)
523{
524 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
525 Name identityName = keyName.getSubName(0, keyName.size() - 1);
526
527 sqlite3_stmt *statement;
528 sqlite3_prepare_v2(database_, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0);
529
530 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
531 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
532
533 int res = sqlite3_step(statement);
534
535 Name certName;
536
537 if (res == SQLITE_ROW)
538 certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0)));
539
540 sqlite3_finalize(statement);
541
542 return certName;
543}
544
545void
546BasicIdentityStorage::setDefaultIdentity(const Name& identityName)
547{
548 sqlite3_stmt *statement;
549
550 //Reset previous default identity
551 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0);
552
553 while (sqlite3_step(statement) == SQLITE_ROW)
554 {}
555
556 sqlite3_finalize(statement);
557
558 //Set current default identity
559 sqlite3_prepare_v2(database_, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0);
560
561 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
562
563 sqlite3_step(statement);
564
565 sqlite3_finalize(statement);
566}
567
568void
569BasicIdentityStorage::setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck)
570{
571 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
572 Name identityName = keyName.getSubName(0, keyName.size() - 1);
573
574 if (identityNameCheck.size() > 0 && !identityNameCheck.equals(identityName))
Alexander Afanasyevfab95ed2014-01-05 23:26:30 -0800575 throw Error("Specified identity name does not match the key name");
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700576
577 sqlite3_stmt *statement;
578
579 //Reset previous default Key
580 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0);
581
582 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
583
584 while (sqlite3_step(statement) == SQLITE_ROW)
585 {}
586
587 sqlite3_finalize(statement);
588
589 //Set current default Key
590 sqlite3_prepare_v2(database_, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0);
591
592 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
593 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
594
595 sqlite3_step(statement);
596
597 sqlite3_finalize(statement);
598}
599
600void
601BasicIdentityStorage::setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName)
602{
603 string keyId = keyName.get(keyName.size() - 1).toEscapedString();
604 Name identityName = keyName.getSubName(0, keyName.size() - 1);
605
606 sqlite3_stmt *statement;
607
608 //Reset previous default Key
609 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0);
610
611 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
612 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
613
614 while (sqlite3_step(statement) == SQLITE_ROW)
615 {}
616
617 sqlite3_finalize(statement);
618
619 //Set current default Key
620 sqlite3_prepare_v2(database_, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0);
621
622 sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
623 sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
624 sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
625
626 sqlite3_step(statement);
627
628 sqlite3_finalize(statement);
629}
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800630
631vector<Name>
632BasicIdentityStorage::getAllIdentities(bool isDefault)
633{
634 sqlite3_stmt *stmt;
635 if(isDefault)
636 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0);
637 else
638 sqlite3_prepare_v2 (database_, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0);
639
640 vector<Name> nameList;
641 while(sqlite3_step (stmt) == SQLITE_ROW)
642 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 -0700643
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800644 sqlite3_finalize (stmt);
645 return nameList;
Jeff Thompson7ca11f22013-10-04 19:01:30 -0700646}
647
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800648vector<Name>
649BasicIdentityStorage::getAllKeyNames(bool isDefault)
650{
651 sqlite3_stmt *stmt;
652 if(isDefault)
653 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0);
654 else
655 sqlite3_prepare_v2 (database_, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0);
656
657 vector<Name> nameList;
658 while(sqlite3_step (stmt) == SQLITE_ROW)
659 {
660 Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
661 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
662 nameList.push_back(keyName);
663 }
664 sqlite3_finalize (stmt);
665 return nameList;
666}
667
668vector<Name>
669BasicIdentityStorage::getAllKeyNamesOfIdentity(const Name& identity, bool isDefault)
670{
671 sqlite3_stmt *stmt;
672 if(isDefault)
673 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0);
674 else
675 sqlite3_prepare_v2 (database_, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0);
676
677 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
678
679 vector<Name> nameList;
680 while(sqlite3_step (stmt) == SQLITE_ROW)
681 {
682 Name keyName(identity);
683 keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
684 nameList.push_back(keyName);
685 }
686 sqlite3_finalize (stmt);
687 return nameList;
688}
689
690vector<Name>
691BasicIdentityStorage::getAllCertificateNames(bool isDefault)
692{
693 sqlite3_stmt *stmt;
694 if(isDefault)
695 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0);
696 else
697 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0);
698
699 vector<Name> nameList;
700 while(sqlite3_step (stmt) == SQLITE_ROW)
701 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
702
703 sqlite3_finalize (stmt);
704 return nameList;
705}
706
707vector<Name>
708BasicIdentityStorage::getAllCertificateNamesOfKey(const Name& keyName, bool isDefault)
709{
710 sqlite3_stmt *stmt;
711 if(isDefault)
712 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0);
713 else
714 sqlite3_prepare_v2 (database_, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0);
715
716 Name identity = keyName.getSubName(0, keyName.size()-1);
717 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
718 std::string baseKeyName = keyName.get(-1).toEscapedString();
719 sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
720
721 vector<Name> nameList;
722 while(sqlite3_step (stmt) == SQLITE_ROW)
723 nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
724
725 sqlite3_finalize (stmt);
726 return nameList;
727}
728
729} // namespace ndn
730
Jeff Thompsonb7523002013-10-09 10:25:00 -0700731#endif // NDN_CPP_HAVE_SQLITE3