NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
sec-public-info-sqlite3.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "identity-certificate.hpp"
29 #include "../data.hpp"
30 
31 #include <sqlite3.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sstream>
35 #include <fstream>
36 #include <boost/filesystem.hpp>
37 
38 namespace ndn {
39 
40 using std::string;
41 using std::vector;
42 
43 const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3");
44 
45 static const string INIT_TPM_INFO_TABLE =
46  "CREATE TABLE IF NOT EXISTS "
47  " TpmInfo( "
48  " tpm_locator BLOB NOT NULL,"
49  " PRIMARY KEY (tpm_locator) "
50  " ); ";
51 
52 static const string INIT_ID_TABLE =
53  "CREATE TABLE IF NOT EXISTS "
54  " Identity( "
55  " identity_name BLOB NOT NULL, "
56  " default_identity INTEGER DEFAULT 0, "
57  " PRIMARY KEY (identity_name) "
58  " ); "
59  "CREATE INDEX identity_index ON Identity(identity_name);";
60 
61 static const string INIT_KEY_TABLE =
62  "CREATE TABLE IF NOT EXISTS "
63  " Key( "
64  " identity_name BLOB NOT NULL, "
65  " key_identifier BLOB NOT NULL, "
66  " key_type INTEGER, "
67  " public_key BLOB, "
68  " default_key INTEGER DEFAULT 0, "
69  " active INTEGER DEFAULT 0, "
70  " PRIMARY KEY (identity_name, key_identifier)"
71  " ); "
72  "CREATE INDEX key_index ON Key(identity_name); ";
73 
74 
75 static const string INIT_CERT_TABLE =
76  "CREATE TABLE IF NOT EXISTS "
77  " Certificate( "
78  " cert_name BLOB NOT NULL, "
79  " cert_issuer BLOB NOT NULL, "
80  " identity_name BLOB NOT NULL, "
81  " key_identifier BLOB NOT NULL, "
82  " not_before TIMESTAMP, "
83  " not_after TIMESTAMP, "
84  " certificate_data BLOB NOT NULL, "
85  " valid_flag INTEGER DEFAULT 1, "
86  " default_cert INTEGER DEFAULT 0, "
87  " PRIMARY KEY (cert_name) "
88  " ); "
89  "CREATE INDEX cert_index ON Certificate(cert_name); "
90  "CREATE INDEX subject ON Certificate(identity_name);";
91 
96 static int
97 sqlite3_bind_string(sqlite3_stmt* statement,
98  int index,
99  const string& value,
100  void(*destructor)(void*))
101 {
102  return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
103 }
104 
105 static string
106 sqlite3_column_string(sqlite3_stmt* statement, int column)
107 {
108  return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)),
109  sqlite3_column_bytes(statement, column));
110 }
111 
113  : SecPublicInfo(dir)
114  , m_database(nullptr)
115 {
116  boost::filesystem::path identityDir;
117  if (dir == "")
118  identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
119  else
120  identityDir = boost::filesystem::path(dir) / ".ndn";
121  boost::filesystem::create_directories(identityDir);
122 
124  int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database,
125  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
126 #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
127  "unix-dotfile"
128 #else
129  0
130 #endif
131  );
132  if (res != SQLITE_OK)
133  BOOST_THROW_EXCEPTION(Error("identity DB cannot be opened/created"));
134 
135 
136  BOOST_ASSERT(m_database != nullptr);
137 
138  initializeTable("TpmInfo", INIT_TPM_INFO_TABLE); // Check if TpmInfo table exists;
139  initializeTable("Identity", INIT_ID_TABLE); // Check if Identity table exists;
140  initializeTable("Key", INIT_KEY_TABLE); // Check if Key table exists;
141  initializeTable("Certificate", INIT_CERT_TABLE); // Check if Certificate table exists;
142 }
143 
145 {
146  sqlite3_close(m_database);
147  m_database = nullptr;
148 }
149 
150 bool
151 SecPublicInfoSqlite3::doesTableExist(const string& tableName)
152 {
153  // Check if the table exists;
154  bool doesTableExist = false;
155  string checkingString =
156  "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
157 
158  sqlite3_stmt* statement = nullptr;
159  sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0);
160 
161  int result = sqlite3_step(statement);
162  if (result == SQLITE_ROW)
163  doesTableExist = true;
164  sqlite3_finalize(statement);
165 
166  return doesTableExist;
167 }
168 
169 bool
170 SecPublicInfoSqlite3::initializeTable(const string& tableName, const string& initCommand)
171 {
172  // Create the table if it does not exist
173  if (!doesTableExist(tableName)) {
174  char* errorMessage = 0;
175  int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage);
176 
177  if (result != SQLITE_OK && errorMessage != 0) {
178  sqlite3_free(errorMessage);
179  return false;
180  }
181  }
182 
183  return true;
184 }
185 
186 void
187 SecPublicInfoSqlite3::deleteTable(const string& tableName)
188 {
189  string query = "DROP TABLE IF EXISTS " + tableName;
190 
191  sqlite3_stmt* statement = nullptr;
192  sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0);
193 
194  sqlite3_step(statement);
195  sqlite3_finalize(statement);
196 }
197 
198 void
199 SecPublicInfoSqlite3::setTpmLocator(const string& tpmLocator)
200 {
201  string currentTpm;
202  try {
203  currentTpm = getTpmLocator();
204  }
205  catch (SecPublicInfo::Error&) {
206  setTpmLocatorInternal(tpmLocator, false); // set tpmInfo without resetting
207  return;
208  }
209 
210  if (currentTpm == tpmLocator)
211  return; // if the same, nothing will be changed
212 
213  setTpmLocatorInternal(tpmLocator, true); // set tpmInfo and reset pib
214 }
215 
216 string
218 {
219  sqlite3_stmt* statement = nullptr;
220  sqlite3_prepare_v2(m_database, "SELECT tpm_locator FROM TpmInfo", -1, &statement, 0);
221 
222  int res = sqlite3_step(statement);
223 
224  if (res == SQLITE_ROW) {
225  string tpmLocator = sqlite3_column_string(statement, 0);
226  sqlite3_finalize(statement);
227  return tpmLocator;
228  }
229  else {
230  sqlite3_finalize(statement);
231  BOOST_THROW_EXCEPTION(SecPublicInfo::Error("TPM info does not exist"));
232  }
233 }
234 
235 void
236 SecPublicInfoSqlite3::setTpmLocatorInternal(const string& tpmLocator, bool needReset)
237 {
238  sqlite3_stmt* statement = nullptr;
239 
240  if (needReset) {
241  deleteTable("Identity");
242  deleteTable("Key");
243  deleteTable("Certificate");
244 
245  initializeTable("Identity", INIT_ID_TABLE);
246  initializeTable("Key", INIT_KEY_TABLE);
247  initializeTable("Certificate", INIT_CERT_TABLE);
248 
249  sqlite3_prepare_v2(m_database, "UPDATE TpmInfo SET tpm_locator = ?",
250  -1, &statement, 0);
251  sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
252  }
253  else {
254  // no reset implies there is no tpmLocator record, insert one
255  sqlite3_prepare_v2(m_database, "INSERT INTO TpmInfo (tpm_locator) VALUES (?)",
256  -1, &statement, 0);
257  sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
258  }
259 
260  sqlite3_step(statement);
261  sqlite3_finalize(statement);
262 }
263 
264 std::string
266 {
267  return string("pib-sqlite3:").append(m_location);
268 }
269 
270 bool
272 {
273  bool result = false;
274 
275  sqlite3_stmt* statement = nullptr;
276  sqlite3_prepare_v2(m_database,
277  "SELECT count(*) FROM Identity WHERE identity_name=?",
278  -1, &statement, 0);
279 
280  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
281  int res = sqlite3_step(statement);
282 
283  if (res == SQLITE_ROW) {
284  int countAll = sqlite3_column_int(statement, 0);
285  if (countAll > 0)
286  result = true;
287  }
288 
289  sqlite3_finalize(statement);
290 
291  return result;
292 }
293 
294 void
296 {
297  if (doesIdentityExist(identityName))
298  return;
299 
300  sqlite3_stmt* statement = nullptr;
301 
302  sqlite3_prepare_v2(m_database,
303  "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
304  -1, &statement, 0);
305 
306  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
307 
308  sqlite3_step(statement);
309 
310  sqlite3_finalize(statement);
311 }
312 
313 bool
315 {
316  //TODO:
317  return false;
318 }
319 
320 bool
322 {
323  if (keyName.empty())
324  BOOST_THROW_EXCEPTION(Error("Incorrect key name " + keyName.toUri()));
325 
326  string keyId = keyName.get(-1).toUri();
327  Name identityName = keyName.getPrefix(-1);
328 
329  sqlite3_stmt* statement = nullptr;
330  sqlite3_prepare_v2(m_database,
331  "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
332  -1, &statement, 0);
333 
334  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
335  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
336 
337  int res = sqlite3_step(statement);
338 
339  bool keyIdExist = false;
340  if (res == SQLITE_ROW) {
341  int countAll = sqlite3_column_int(statement, 0);
342  if (countAll > 0)
343  keyIdExist = true;
344  }
345 
346  sqlite3_finalize(statement);
347 
348  return keyIdExist;
349 }
350 
351 void
353  const PublicKey& publicKeyDer)
354 {
355  if (keyName.empty())
356  return;
357 
358  if (doesPublicKeyExist(keyName))
359  return;
360 
361  string keyId = keyName.get(-1).toUri();
362  Name identityName = keyName.getPrefix(-1);
363 
364  addIdentity(identityName);
365 
366  sqlite3_stmt* statement = nullptr;
367  sqlite3_prepare_v2(m_database,
368  "INSERT OR REPLACE INTO Key \
369  (identity_name, key_identifier, key_type, public_key) \
370  values (?, ?, ?, ?)",
371  -1, &statement, 0);
372 
373  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
374  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
375  sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
376  sqlite3_bind_blob(statement, 4,
377  publicKeyDer.get().buf(),
378  publicKeyDer.get().size(),
379  SQLITE_STATIC);
380 
381  sqlite3_step(statement);
382 
383  sqlite3_finalize(statement);
384 }
385 
386 shared_ptr<PublicKey>
388 {
389  if (keyName.empty())
390  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey Empty keyName"));
391 
392  string keyId = keyName.get(-1).toUri();
393  Name identityName = keyName.getPrefix(-1);
394 
395  sqlite3_stmt* statement = nullptr;
396  sqlite3_prepare_v2(m_database,
397  "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
398  -1, &statement, 0);
399 
400  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
401  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
402 
403  int res = sqlite3_step(statement);
404 
405  shared_ptr<PublicKey> result;
406  if (res == SQLITE_ROW) {
407  result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
408  sqlite3_column_bytes(statement, 0));
409  sqlite3_finalize(statement);
410  return result;
411  }
412  else {
413  sqlite3_finalize(statement);
414  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey public key does not exist"));
415  }
416 }
417 
418 KeyType
420 {
421  if (keyName.empty())
422  return KEY_TYPE_NULL;
423 
424  string keyId = keyName.get(-1).toUri();
425  Name identityName = keyName.getPrefix(-1);
426 
427  sqlite3_stmt* statement = nullptr;
428  sqlite3_prepare_v2(m_database,
429  "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
430  -1, &statement, 0);
431 
432  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
433  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
434 
435  int res = sqlite3_step(statement);
436 
437  if (res == SQLITE_ROW) {
438  int typeValue = sqlite3_column_int(statement, 0);
439  sqlite3_finalize(statement);
440  return static_cast<KeyType>(typeValue);
441  }
442  else {
443  sqlite3_finalize(statement);
444  return KEY_TYPE_NULL;
445  }
446 }
447 
448 bool
450 {
451  sqlite3_stmt* statement = nullptr;
452  sqlite3_prepare_v2(m_database,
453  "SELECT count(*) FROM Certificate WHERE cert_name=?",
454  -1, &statement, 0);
455 
456  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
457 
458  int res = sqlite3_step(statement);
459 
460  bool certExist = false;
461  if (res == SQLITE_ROW) {
462  int countAll = sqlite3_column_int(statement, 0);
463  if (countAll > 0)
464  certExist = true;
465  }
466 
467  sqlite3_finalize(statement);
468 
469  return certExist;
470 }
471 
472 void
474 {
475  const Name& certificateName = certificate.getName();
476  // KeyName is from IdentityCertificate name, so should be qualified.
477  Name keyName =
479 
480  addKey(keyName, certificate.getPublicKeyInfo());
481 
482  if (doesCertificateExist(certificateName))
483  return;
484 
485  string keyId = keyName.get(-1).toUri();
486  Name identity = keyName.getPrefix(-1);
487 
488  // Insert the certificate
489  sqlite3_stmt* statement = nullptr;
490  sqlite3_prepare_v2(m_database,
491  "INSERT OR REPLACE INTO Certificate \
492  (cert_name, cert_issuer, identity_name, key_identifier, \
493  not_before, not_after, certificate_data) \
494  values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
495  -1, &statement, 0);
496 
497  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
498 
499  try {
500  // this will throw an exception if the signature is not the standard one
501  // or there is no key locator present
502  std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
503  sqlite3_bind_string(statement, 2, signerName, SQLITE_TRANSIENT);
504  }
505  catch (tlv::Error&) {
506  return;
507  }
508 
509  sqlite3_bind_string(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
510  sqlite3_bind_string(statement, 4, keyId, SQLITE_STATIC);
511 
512  sqlite3_bind_int64(statement, 5,
513  static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotBefore()).count()));
514  sqlite3_bind_int64(statement, 6,
515  static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotAfter()).count()));
516 
517  sqlite3_bind_blob(statement, 7,
518  certificate.wireEncode().wire(),
519  certificate.wireEncode().size(),
520  SQLITE_TRANSIENT);
521 
522  sqlite3_step(statement);
523 
524  sqlite3_finalize(statement);
525 }
526 
527 shared_ptr<IdentityCertificate>
529 {
530  sqlite3_stmt* statement = nullptr;
531 
532  sqlite3_prepare_v2(m_database,
533  "SELECT certificate_data FROM Certificate WHERE cert_name=?",
534  -1, &statement, 0);
535 
536  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
537 
538  int res = sqlite3_step(statement);
539 
540  if (res == SQLITE_ROW) {
541  shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
542  try {
543  certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
544  sqlite3_column_bytes(statement, 0)));
545  }
546  catch (tlv::Error&) {
547  sqlite3_finalize(statement);
548  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate cannot be "
549  "decoded"));
550  }
551 
552  sqlite3_finalize(statement);
553  return certificate;
554  }
555  else {
556  sqlite3_finalize(statement);
557  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate does not "
558  "exist"));
559  }
560 }
561 
562 
563 Name
565 {
566  sqlite3_stmt* statement = nullptr;
567  sqlite3_prepare_v2(m_database,
568  "SELECT identity_name FROM Identity WHERE default_identity=1",
569  -1, &statement, 0);
570 
571  int res = sqlite3_step(statement);
572 
573  if (res == SQLITE_ROW) {
574  Name identity(sqlite3_column_string(statement, 0));
575  sqlite3_finalize(statement);
576  return identity;
577  }
578  else {
579  sqlite3_finalize(statement);
580  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity"));
581  }
582 }
583 
584 void
585 SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName)
586 {
587  addIdentity(identityName);
588 
589  sqlite3_stmt* statement = nullptr;
590 
591  //Reset previous default identity
592  sqlite3_prepare_v2(m_database,
593  "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
594  -1, &statement, 0);
595 
596  while (sqlite3_step(statement) == SQLITE_ROW)
597  ;
598 
599  sqlite3_finalize(statement);
600 
601  //Set current default identity
602  sqlite3_prepare_v2(m_database,
603  "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
604  -1, &statement, 0);
605 
606  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
607 
608  sqlite3_step(statement);
609 
610  sqlite3_finalize(statement);
611 }
612 
613 Name
615 {
616  sqlite3_stmt* statement = nullptr;
617  sqlite3_prepare_v2(m_database,
618  "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
619  -1, &statement, 0);
620 
621  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
622 
623  int res = sqlite3_step(statement);
624 
625  if (res == SQLITE_ROW) {
626  Name keyName = identityName;
627  keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
628  sqlite3_column_bytes(statement, 0)));
629  sqlite3_finalize(statement);
630  return keyName;
631  }
632  else {
633  sqlite3_finalize(statement);
634  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not "
635  "found"));
636  }
637 }
638 
639 void
640 SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName)
641 {
642  if (!doesPublicKeyExist(keyName))
643  BOOST_THROW_EXCEPTION(Error("Key does not exist:" + keyName.toUri()));
644 
645  string keyId = keyName.get(-1).toUri();
646  Name identityName = keyName.getPrefix(-1);
647 
648  sqlite3_stmt* statement = nullptr;
649 
650  //Reset previous default Key
651  sqlite3_prepare_v2(m_database,
652  "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
653  -1, &statement, 0);
654 
655  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
656 
657  while (sqlite3_step(statement) == SQLITE_ROW)
658  ;
659 
660  sqlite3_finalize(statement);
661 
662  //Set current default Key
663  sqlite3_prepare_v2(m_database,
664  "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
665  -1, &statement, 0);
666 
667  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
668  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
669 
670  sqlite3_step(statement);
671 
672  sqlite3_finalize(statement);
673 }
674 
675 Name
677 {
678  if (keyName.empty())
679  BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"));
680 
681  string keyId = keyName.get(-1).toUri();
682  Name identityName = keyName.getPrefix(-1);
683 
684  sqlite3_stmt* statement = nullptr;
685  sqlite3_prepare_v2(m_database,
686  "SELECT cert_name FROM Certificate \
687  WHERE identity_name=? AND key_identifier=? AND default_cert=1",
688  -1, &statement, 0);
689 
690  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
691  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
692 
693  int res = sqlite3_step(statement);
694 
695  if (res == SQLITE_ROW) {
696  Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
697  sqlite3_column_bytes(statement, 0)));
698  sqlite3_finalize(statement);
699  return certName;
700  }
701  else {
702  sqlite3_finalize(statement);
703  BOOST_THROW_EXCEPTION(Error("certificate not found"));
704  }
705 }
706 
707 void
708 SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName)
709 {
710  if (!doesCertificateExist(certificateName))
711  BOOST_THROW_EXCEPTION(Error("certificate does not exist:" + certificateName.toUri()));
712 
714  string keyId = keyName.get(-1).toUri();
715  Name identityName = keyName.getPrefix(-1);
716 
717  sqlite3_stmt* statement = nullptr;
718 
719  //Reset previous default Key
720  sqlite3_prepare_v2(m_database,
721  "UPDATE Certificate SET default_cert=0 \
722  WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
723  -1, &statement, 0);
724 
725  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
726  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
727 
728  while (sqlite3_step(statement) == SQLITE_ROW)
729  ;
730 
731  sqlite3_finalize(statement);
732 
733  //Set current default Key
734  sqlite3_prepare_v2(m_database,
735  "UPDATE Certificate SET default_cert=1 \
736  WHERE identity_name=? AND key_identifier=? AND cert_name=?",
737  -1, &statement, 0);
738 
739  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
740  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
741  sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
742 
743  sqlite3_step(statement);
744 
745  sqlite3_finalize(statement);
746 }
747 
748 void
749 SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault)
750 {
751  sqlite3_stmt* stmt;
752  if (isDefault)
753  sqlite3_prepare_v2(m_database,
754  "SELECT identity_name FROM Identity WHERE default_identity=1",
755  -1, &stmt, 0);
756  else
757  sqlite3_prepare_v2(m_database,
758  "SELECT identity_name FROM Identity WHERE default_identity=0",
759  -1, &stmt, 0);
760 
761  while (sqlite3_step(stmt) == SQLITE_ROW)
762  nameList.push_back(Name(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
763  sqlite3_column_bytes(stmt, 0))));
764 
765  sqlite3_finalize(stmt);
766 }
767 
768 void
769 SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault)
770 {
771  sqlite3_stmt* stmt;
772 
773  if (isDefault)
774  sqlite3_prepare_v2(m_database,
775  "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
776  -1, &stmt, 0);
777  else
778  sqlite3_prepare_v2(m_database,
779  "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
780  -1, &stmt, 0);
781 
782  while (sqlite3_step(stmt) == SQLITE_ROW) {
783  Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
784  sqlite3_column_bytes(stmt, 0)));
785  keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
786  sqlite3_column_bytes(stmt, 1)));
787  nameList.push_back(keyName);
788  }
789  sqlite3_finalize(stmt);
790 }
791 
792 void
794  vector<Name>& nameList,
795  bool isDefault)
796 {
797  sqlite3_stmt* stmt;
798 
799  if (isDefault)
800  sqlite3_prepare_v2(m_database,
801  "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
802  -1, &stmt, 0);
803  else
804  sqlite3_prepare_v2(m_database,
805  "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
806  -1, &stmt, 0);
807 
808  sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
809 
810  while (sqlite3_step(stmt) == SQLITE_ROW) {
811  Name keyName(identity);
812  keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
813  sqlite3_column_bytes(stmt, 0)));
814  nameList.push_back(keyName);
815  }
816  sqlite3_finalize(stmt);
817 }
818 
819 void
820 SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault)
821 {
822  sqlite3_stmt* stmt;
823 
824  if (isDefault)
825  sqlite3_prepare_v2(m_database,
826  "SELECT cert_name FROM Certificate WHERE default_cert=1",
827  -1, &stmt, 0);
828  else
829  sqlite3_prepare_v2(m_database,
830  "SELECT cert_name FROM Certificate WHERE default_cert=0",
831  -1, &stmt, 0);
832 
833  while (sqlite3_step(stmt) == SQLITE_ROW)
834  nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
835  sqlite3_column_bytes(stmt, 0)));
836 
837  sqlite3_finalize(stmt);
838 }
839 
840 void
842  vector<Name>& nameList,
843  bool isDefault)
844 {
845  if (keyName.empty())
846  return;
847 
848  sqlite3_stmt* stmt;
849  if (isDefault)
850  sqlite3_prepare_v2(m_database,
851  "SELECT cert_name FROM Certificate \
852  WHERE default_cert=1 and identity_name=? and key_identifier=?",
853  -1, &stmt, 0);
854  else
855  sqlite3_prepare_v2(m_database,
856  "SELECT cert_name FROM Certificate \
857  WHERE default_cert=0 and identity_name=? and key_identifier=?",
858  -1, &stmt, 0);
859 
860  Name identity = keyName.getPrefix(-1);
861  sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
862 
863  std::string baseKeyName = keyName.get(-1).toUri();
864  sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT);
865 
866  while (sqlite3_step(stmt) == SQLITE_ROW)
867  nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
868  sqlite3_column_bytes(stmt, 0)));
869 
870  sqlite3_finalize(stmt);
871 }
872 
873 void
875 {
876  if (certName.empty())
877  return;
878 
879  sqlite3_stmt* stmt;
880  sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
881  sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT);
882  sqlite3_step(stmt);
883  sqlite3_finalize(stmt);
884 }
885 
886 void
888 {
889  if (keyName.empty())
890  return;
891 
892  string identity = keyName.getPrefix(-1).toUri();
893  string keyId = keyName.get(-1).toUri();
894 
895  sqlite3_stmt* stmt;
896  sqlite3_prepare_v2(m_database,
897  "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
898  -1, &stmt, 0);
899  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
900  sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
901  sqlite3_step(stmt);
902  sqlite3_finalize(stmt);
903 
904  sqlite3_prepare_v2(m_database,
905  "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
906  -1, &stmt, 0);
907  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
908  sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
909  sqlite3_step(stmt);
910  sqlite3_finalize(stmt);
911 }
912 
913 void
915 {
916  string identity = identityName.toUri();
917 
918  sqlite3_stmt* stmt;
919  sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
920  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
921  sqlite3_step(stmt);
922  sqlite3_finalize(stmt);
923 
924  sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
925  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
926  sqlite3_step(stmt);
927  sqlite3_finalize(stmt);
928 
929  sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
930  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
931  sqlite3_step(stmt);
932  sqlite3_finalize(stmt);
933 }
934 
935 std::string
936 SecPublicInfoSqlite3::getScheme()
937 {
938  return SCHEME;
939 }
940 
941 } // namespace ndn
time::system_clock::TimePoint & getNotBefore()
Copyright (c) 2011-2015 Regents of the University of California.
virtual bool doesIdentityExist(const Name &identityName)
Check if the specified identity already exists.
time::system_clock::TimePoint & getNotAfter()
virtual void getAllKeyNames(std::vector< Name > &nameList, bool isDefault)
Get all the key names from public info.
virtual shared_ptr< IdentityCertificate > getCertificate(const Name &certificateName)
Get a shared pointer to identity certificate object from the identity storage.
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Definition: signature.hpp:134
virtual void getAllCertificateNamesOfKey(const Name &keyName, std::vector< Name > &nameList, bool isDefault)
Get all the certificate name of a particular key name.
static const string INIT_KEY_TABLE
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
virtual bool doesCertificateExist(const Name &certificateName)
Check if the specified certificate already exists.
static const string INIT_TPM_INFO_TABLE
PublicKey & getPublicKeyInfo()
virtual Name getDefaultKeyNameForIdentity(const Name &identityName)
Get name of the default key name for the specified identity.
virtual std::string getTpmLocator()
Get TPM Locator.
virtual bool doesPublicKeyExist(const Name &keyName)
Check if the specified key already exists.
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:183
const Name & getName() const
get Name element
virtual shared_ptr< PublicKey > getPublicKey(const Name &keyName)
Get shared pointer to PublicKey object from the identity storage.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
static int sqlite3_bind_string(sqlite3_stmt *statement, int index, const string &value, void(*destructor)(void *))
A utility function to call the normal sqlite3_bind_text where the value and length are value...
uint8_t * buf()
Definition: buffer.hpp:87
virtual bool revokeIdentity()
Revoke the identity.
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
KeyType getKeyType() const
Definition: public-key.hpp:83
virtual KeyType getPublicKeyType(const Name &keyName)
Get the type of the queried public key.
virtual void deletePublicKeyInfo(const Name &keyName)
Delete a public key and related certificates.
static const string INIT_ID_TABLE
Name abstraction to represent an absolute name.
Definition: name.hpp:46
static const std::string SCHEME
virtual void getAllIdentities(std::vector< Name > &nameList, bool isDefault)
Get all the identities from public info.
virtual Name getDefaultIdentity()
Get name of the default identity.
const Signature & getSignature() const
Definition: data.hpp:373
const Buffer & get() const
Definition: public-key.hpp:70
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:118
virtual void addIdentity(const Name &identityName)
Add a new identity.
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
virtual void addKey(const Name &keyName, const PublicKey &publicKeyDer)
Add a public key to the identity storage.
bool empty() const
Check if name is emtpy.
Definition: name.hpp:398
virtual void deleteCertificateInfo(const Name &certificateName)
Delete a certificate.
SecPublicInfoSqlite3(const std::string &dir="")
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
static string sqlite3_column_string(sqlite3_stmt *statement, int column)
virtual void getAllKeyNamesOfIdentity(const Name &identity, std::vector< Name > &nameList, bool isDefault)
Get all the key names of a particular identity.
virtual Name getDefaultCertificateNameForKey(const Name &keyName)
Get name of the default certificate name for the specified key.
virtual void addCertificate(const IdentityCertificate &certificate)
Add a certificate to the identity storage.
virtual void deleteIdentityInfo(const Name &identity)
Delete an identity and related public keys and certificates.
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419
virtual void getAllCertificateNames(std::vector< Name > &nameList, bool isDefault)
Get all the certificate name in public info.
SecPublicInfo is a base class for the storage of public information.
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
static const string INIT_CERT_TABLE
virtual void setTpmLocator(const std::string &tpmLocator)
Set the corresponding TPM information to tpmLocator.