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