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