29 #include "../data.hpp" 36 #include <boost/filesystem.hpp> 47 "CREATE TABLE IF NOT EXISTS " 49 " tpm_locator BLOB NOT NULL," 50 " PRIMARY KEY (tpm_locator) " 54 "CREATE TABLE IF NOT EXISTS " 56 " identity_name BLOB NOT NULL, " 57 " default_identity INTEGER DEFAULT 0, " 58 " PRIMARY KEY (identity_name) " 60 "CREATE INDEX identity_index ON Identity(identity_name);";
63 "CREATE TABLE IF NOT EXISTS " 65 " identity_name BLOB NOT NULL, " 66 " key_identifier BLOB NOT NULL, " 69 " default_key INTEGER DEFAULT 0, " 70 " active INTEGER DEFAULT 0, " 71 " PRIMARY KEY (identity_name, key_identifier)" 73 "CREATE INDEX key_index ON Key(identity_name); ";
77 "CREATE TABLE IF NOT EXISTS " 79 " cert_name BLOB NOT NULL, " 80 " cert_issuer BLOB NOT NULL, " 81 " identity_name BLOB NOT NULL, " 82 " key_identifier BLOB NOT NULL, " 83 " not_before TIMESTAMP, " 84 " not_after TIMESTAMP, " 85 " certificate_data BLOB NOT NULL, " 86 " valid_flag INTEGER DEFAULT 1, " 87 " default_cert INTEGER DEFAULT 0, " 88 " PRIMARY KEY (cert_name) " 90 "CREATE INDEX cert_index ON Certificate(cert_name); " 91 "CREATE INDEX subject ON Certificate(identity_name);";
101 void(*destructor)(
void*))
103 return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
109 return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)),
110 sqlite3_column_bytes(statement, column));
115 , m_database(nullptr)
117 boost::filesystem::path identityDir;
119 #ifdef NDN_CXX_HAVE_TESTS 120 if (getenv(
"TEST_HOME") !=
nullptr) {
121 identityDir = boost::filesystem::path(getenv(
"TEST_HOME")) /
".ndn";
124 #endif // NDN_CXX_HAVE_TESTS 125 if (getenv(
"HOME") !=
nullptr) {
126 identityDir = boost::filesystem::path(getenv(
"HOME")) /
".ndn";
129 identityDir = boost::filesystem::path(
".") /
".ndn";
133 identityDir = boost::filesystem::path(dir);
135 boost::filesystem::create_directories(identityDir);
138 int res = sqlite3_open_v2((identityDir /
"ndnsec-public-info.db").c_str(), &m_database,
139 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
140 #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
146 if (res != SQLITE_OK)
147 BOOST_THROW_EXCEPTION(
Error(
"identity DB cannot be opened/created"));
150 BOOST_ASSERT(m_database !=
nullptr);
152 initializeTable(
"TpmInfo", INIT_TPM_INFO_TABLE);
153 initializeTable(
"Identity", INIT_ID_TABLE);
154 initializeTable(
"Key", INIT_KEY_TABLE);
155 initializeTable(
"Certificate", INIT_CERT_TABLE);
160 sqlite3_close(m_database);
161 m_database =
nullptr;
165 SecPublicInfoSqlite3::doesTableExist(
const string& tableName)
168 bool doesTableExist =
false;
169 string checkingString =
170 "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName +
"'";
172 sqlite3_stmt* statement =
nullptr;
173 sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0);
175 int result = sqlite3_step(statement);
176 if (result == SQLITE_ROW)
177 doesTableExist =
true;
178 sqlite3_finalize(statement);
180 return doesTableExist;
184 SecPublicInfoSqlite3::initializeTable(
const string& tableName,
const string& initCommand)
187 if (!doesTableExist(tableName)) {
188 char* errorMessage = 0;
189 int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage);
191 if (result != SQLITE_OK && errorMessage != 0) {
192 sqlite3_free(errorMessage);
201 SecPublicInfoSqlite3::deleteTable(
const string& tableName)
203 string query =
"DROP TABLE IF EXISTS " + tableName;
205 sqlite3_stmt* statement =
nullptr;
206 sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0);
208 sqlite3_step(statement);
209 sqlite3_finalize(statement);
220 setTpmLocatorInternal(tpmLocator,
false);
224 if (currentTpm == tpmLocator)
227 setTpmLocatorInternal(tpmLocator,
true);
233 sqlite3_stmt* statement =
nullptr;
234 sqlite3_prepare_v2(m_database,
"SELECT tpm_locator FROM TpmInfo", -1, &statement, 0);
236 int res = sqlite3_step(statement);
238 if (res == SQLITE_ROW) {
240 sqlite3_finalize(statement);
244 sqlite3_finalize(statement);
250 SecPublicInfoSqlite3::setTpmLocatorInternal(
const string& tpmLocator,
bool needReset)
252 sqlite3_stmt* statement =
nullptr;
255 deleteTable(
"Identity");
257 deleteTable(
"Certificate");
259 initializeTable(
"Identity", INIT_ID_TABLE);
260 initializeTable(
"Key", INIT_KEY_TABLE);
261 initializeTable(
"Certificate", INIT_CERT_TABLE);
263 sqlite3_prepare_v2(m_database,
"UPDATE TpmInfo SET tpm_locator = ?",
269 sqlite3_prepare_v2(m_database,
"INSERT INTO TpmInfo (tpm_locator) VALUES (?)",
274 sqlite3_step(statement);
275 sqlite3_finalize(statement);
281 return string(
"pib-sqlite3:").append(
m_location);
289 sqlite3_stmt* statement =
nullptr;
290 sqlite3_prepare_v2(m_database,
291 "SELECT count(*) FROM Identity WHERE identity_name=?",
295 int res = sqlite3_step(statement);
297 if (res == SQLITE_ROW) {
298 int countAll = sqlite3_column_int(statement, 0);
303 sqlite3_finalize(statement);
314 sqlite3_stmt* statement =
nullptr;
316 sqlite3_prepare_v2(m_database,
317 "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
322 sqlite3_step(statement);
324 sqlite3_finalize(statement);
338 BOOST_THROW_EXCEPTION(
Error(
"Incorrect key name " + keyName.
toUri()));
340 string keyId = keyName.
get(-1).
toUri();
343 sqlite3_stmt* statement =
nullptr;
344 sqlite3_prepare_v2(m_database,
345 "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
351 int res = sqlite3_step(statement);
353 bool keyIdExist =
false;
354 if (res == SQLITE_ROW) {
355 int countAll = sqlite3_column_int(statement, 0);
360 sqlite3_finalize(statement);
375 string keyId = keyName.
get(-1).
toUri();
380 sqlite3_stmt* statement =
nullptr;
381 sqlite3_prepare_v2(m_database,
382 "INSERT OR REPLACE INTO Key \ 383 (identity_name, key_identifier, key_type, public_key) \ 384 values (?, ?, ?, ?)",
389 sqlite3_bind_int(statement, 3, static_cast<int>(publicKeyDer.
getKeyType()));
390 sqlite3_bind_blob(statement, 4,
392 publicKeyDer.
get().size(),
395 sqlite3_step(statement);
397 sqlite3_finalize(statement);
400 shared_ptr<v1::PublicKey>
404 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getPublicKey Empty keyName"));
406 string keyId = keyName.
get(-1).
toUri();
409 sqlite3_stmt* statement =
nullptr;
410 sqlite3_prepare_v2(m_database,
411 "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
417 int res = sqlite3_step(statement);
419 shared_ptr<v1::PublicKey> result;
420 if (res == SQLITE_ROW) {
421 result = make_shared<v1::PublicKey>(
static_cast<const uint8_t*
>(sqlite3_column_blob(statement, 0)),
422 sqlite3_column_bytes(statement, 0));
423 sqlite3_finalize(statement);
427 sqlite3_finalize(statement);
428 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getPublicKey public key does not exist"));
438 string keyId = keyName.
get(-1).
toUri();
441 sqlite3_stmt* statement =
nullptr;
442 sqlite3_prepare_v2(m_database,
443 "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
449 int res = sqlite3_step(statement);
451 if (res == SQLITE_ROW) {
452 int typeValue = sqlite3_column_int(statement, 0);
453 sqlite3_finalize(statement);
454 return static_cast<KeyType>(typeValue);
457 sqlite3_finalize(statement);
465 sqlite3_stmt* statement =
nullptr;
466 sqlite3_prepare_v2(m_database,
467 "SELECT count(*) FROM Certificate WHERE cert_name=?",
472 int res = sqlite3_step(statement);
474 bool certExist =
false;
475 if (res == SQLITE_ROW) {
476 int countAll = sqlite3_column_int(statement, 0);
481 sqlite3_finalize(statement);
489 const Name& certificateName = certificate.
getName();
499 string keyId = keyName.
get(-1).
toUri();
503 sqlite3_stmt* statement =
nullptr;
504 sqlite3_prepare_v2(m_database,
505 "INSERT OR REPLACE INTO Certificate \ 506 (cert_name, cert_issuer, identity_name, key_identifier, \ 507 not_before, not_after, certificate_data) \ 508 values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
526 sqlite3_bind_int64(statement, 5,
528 sqlite3_bind_int64(statement, 6,
531 sqlite3_bind_blob(statement, 7,
536 sqlite3_step(statement);
538 sqlite3_finalize(statement);
541 shared_ptr<v1::IdentityCertificate>
544 sqlite3_stmt* statement =
nullptr;
546 sqlite3_prepare_v2(m_database,
547 "SELECT certificate_data FROM Certificate WHERE cert_name=?",
552 int res = sqlite3_step(statement);
554 if (res == SQLITE_ROW) {
555 shared_ptr<v1::IdentityCertificate> certificate = make_shared<v1::IdentityCertificate>();
557 certificate->wireDecode(
Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
558 sqlite3_column_bytes(statement, 0)));
561 sqlite3_finalize(statement);
562 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getCertificate certificate cannot be " 566 sqlite3_finalize(statement);
570 sqlite3_finalize(statement);
571 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getCertificate certificate does not " 580 sqlite3_stmt* statement =
nullptr;
581 sqlite3_prepare_v2(m_database,
582 "SELECT identity_name FROM Identity WHERE default_identity=1",
585 int res = sqlite3_step(statement);
587 if (res == SQLITE_ROW) {
589 sqlite3_finalize(statement);
593 sqlite3_finalize(statement);
594 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getDefaultIdentity no default identity"));
599 SecPublicInfoSqlite3::setDefaultIdentityInternal(
const Name& identityName)
603 sqlite3_stmt* statement =
nullptr;
606 sqlite3_prepare_v2(m_database,
607 "UPDATE Identity SET default_identity=0 WHERE default_identity=1",
610 while (sqlite3_step(statement) == SQLITE_ROW)
613 sqlite3_finalize(statement);
616 sqlite3_prepare_v2(m_database,
617 "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
622 sqlite3_step(statement);
624 sqlite3_finalize(statement);
630 sqlite3_stmt* statement =
nullptr;
631 sqlite3_prepare_v2(m_database,
632 "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
637 int res = sqlite3_step(statement);
639 if (res == SQLITE_ROW) {
640 Name keyName = identityName;
641 keyName.
append(
string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
642 sqlite3_column_bytes(statement, 0)));
643 sqlite3_finalize(statement);
647 sqlite3_finalize(statement);
648 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not " 654 SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(
const Name& keyName)
657 BOOST_THROW_EXCEPTION(
Error(
"Key does not exist:" + keyName.
toUri()));
659 string keyId = keyName.
get(-1).
toUri();
662 sqlite3_stmt* statement =
nullptr;
665 sqlite3_prepare_v2(m_database,
666 "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
671 while (sqlite3_step(statement) == SQLITE_ROW)
674 sqlite3_finalize(statement);
677 sqlite3_prepare_v2(m_database,
678 "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
684 sqlite3_step(statement);
686 sqlite3_finalize(statement);
693 BOOST_THROW_EXCEPTION(
Error(
"SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"));
695 string keyId = keyName.
get(-1).
toUri();
698 sqlite3_stmt* statement =
nullptr;
699 sqlite3_prepare_v2(m_database,
700 "SELECT cert_name FROM Certificate \ 701 WHERE identity_name=? AND key_identifier=? AND default_cert=1",
707 int res = sqlite3_step(statement);
709 if (res == SQLITE_ROW) {
710 Name certName(
string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)),
711 sqlite3_column_bytes(statement, 0)));
712 sqlite3_finalize(statement);
716 sqlite3_finalize(statement);
717 BOOST_THROW_EXCEPTION(
Error(
"certificate not found"));
722 SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(
const Name& certificateName)
725 BOOST_THROW_EXCEPTION(
Error(
"certificate does not exist:" + certificateName.
toUri()));
728 string keyId = keyName.
get(-1).
toUri();
731 sqlite3_stmt* statement =
nullptr;
734 sqlite3_prepare_v2(m_database,
735 "UPDATE Certificate SET default_cert=0 \ 736 WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
742 while (sqlite3_step(statement) == SQLITE_ROW)
745 sqlite3_finalize(statement);
748 sqlite3_prepare_v2(m_database,
749 "UPDATE Certificate SET default_cert=1 \ 750 WHERE identity_name=? AND key_identifier=? AND cert_name=?",
757 sqlite3_step(statement);
759 sqlite3_finalize(statement);
767 sqlite3_prepare_v2(m_database,
768 "SELECT identity_name FROM Identity WHERE default_identity=1",
771 sqlite3_prepare_v2(m_database,
772 "SELECT identity_name FROM Identity WHERE default_identity=0",
775 while (sqlite3_step(stmt) == SQLITE_ROW)
776 nameList.push_back(
Name(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
777 sqlite3_column_bytes(stmt, 0))));
779 sqlite3_finalize(stmt);
788 sqlite3_prepare_v2(m_database,
789 "SELECT identity_name, key_identifier FROM Key WHERE default_key=1",
792 sqlite3_prepare_v2(m_database,
793 "SELECT identity_name, key_identifier FROM Key WHERE default_key=0",
796 while (sqlite3_step(stmt) == SQLITE_ROW) {
797 Name keyName(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
798 sqlite3_column_bytes(stmt, 0)));
799 keyName.
append(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)),
800 sqlite3_column_bytes(stmt, 1)));
801 nameList.push_back(keyName);
803 sqlite3_finalize(stmt);
808 vector<Name>& nameList,
814 sqlite3_prepare_v2(m_database,
815 "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?",
818 sqlite3_prepare_v2(m_database,
819 "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
824 while (sqlite3_step(stmt) == SQLITE_ROW) {
825 Name keyName(identity);
826 keyName.
append(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
827 sqlite3_column_bytes(stmt, 0)));
828 nameList.push_back(keyName);
830 sqlite3_finalize(stmt);
839 sqlite3_prepare_v2(m_database,
840 "SELECT cert_name FROM Certificate WHERE default_cert=1",
843 sqlite3_prepare_v2(m_database,
844 "SELECT cert_name FROM Certificate WHERE default_cert=0",
847 while (sqlite3_step(stmt) == SQLITE_ROW)
848 nameList.push_back(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
849 sqlite3_column_bytes(stmt, 0)));
851 sqlite3_finalize(stmt);
856 vector<Name>& nameList,
864 sqlite3_prepare_v2(m_database,
865 "SELECT cert_name FROM Certificate \ 866 WHERE default_cert=1 and identity_name=? and key_identifier=?",
869 sqlite3_prepare_v2(m_database,
870 "SELECT cert_name FROM Certificate \ 871 WHERE default_cert=0 and identity_name=? and key_identifier=?",
877 std::string baseKeyName = keyName.
get(-1).
toUri();
880 while (sqlite3_step(stmt) == SQLITE_ROW)
881 nameList.push_back(
string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
882 sqlite3_column_bytes(stmt, 0)));
884 sqlite3_finalize(stmt);
890 if (certName.
empty())
894 sqlite3_prepare_v2(m_database,
"DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
897 sqlite3_finalize(stmt);
907 string keyId = keyName.
get(-1).
toUri();
910 sqlite3_prepare_v2(m_database,
911 "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
916 sqlite3_finalize(stmt);
918 sqlite3_prepare_v2(m_database,
919 "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
924 sqlite3_finalize(stmt);
930 string identity = identityName.
toUri();
933 sqlite3_prepare_v2(m_database,
"DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
936 sqlite3_finalize(stmt);
938 sqlite3_prepare_v2(m_database,
"DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
941 sqlite3_finalize(stmt);
943 sqlite3_prepare_v2(m_database,
"DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
946 sqlite3_finalize(stmt);
950 SecPublicInfoSqlite3::getScheme()
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Encode this name as a URI.
const Buffer & get() const
const Component & get(ssize_t i) const
Get the component at the given index.
const Name & getName() const
Get name of the Data packet.
virtual void addCertificate(const v1::IdentityCertificate &certificate)
Add a certificate to the identity storage.
const Name & getName() const
get Name element
virtual std::string getTpmLocator()
Get TPM Locator.
virtual Name getDefaultCertificateNameForKey(const Name &keyName)
Get name of the default certificate name for the specified key.
virtual void getAllIdentities(std::vector< Name > &nameList, bool isDefault)
Get all the identities from public info.
virtual void deleteIdentityInfo(const Name &identity)
Delete an identity and related public keys and certificates.
Class representing a wire element of NDN-TLV packet format.
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 std::string getPibLocator()
virtual KeyType getPublicKeyType(const Name &keyName)
Get the type of the queried public key.
PublicKey & getPublicKeyInfo()
SecPublicInfoSqlite3(const std::string &dir="")
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
virtual bool doesPublicKeyExist(const Name &keyName)
Check if the specified key already exists.
virtual void getAllCertificateNames(std::vector< Name > &nameList, bool isDefault)
Get all the certificate name in public info.
virtual void getAllKeyNamesOfIdentity(const Name &identity, std::vector< Name > &nameList, bool isDefault)
Get all the key names of a particular identity.
virtual void deletePublicKeyInfo(const Name &keyName)
Delete a public key and related certificates.
Name abstraction to represent an absolute name.
static const std::string SCHEME
virtual shared_ptr< v1::IdentityCertificate > getCertificate(const Name &certificateName)
Get a shared pointer to identity certificate object from the identity storage.
time::system_clock::TimePoint & getNotAfter()
virtual bool doesIdentityExist(const Name &identityName)
Check if the specified identity already exists.
static const string INIT_TPM_INFO_TABLE
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
static const string INIT_CERT_TABLE
virtual void addIdentity(const Name &identityName)
Add a new identity.
virtual bool revokeIdentity()
Revoke the identity.
SecPublicInfo is a base class for the storage of public information.
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
virtual void getAllKeyNames(std::vector< Name > &nameList, bool isDefault)
Get all the key names from public info.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
static string sqlite3_column_string(sqlite3_stmt *statement, int column)
virtual void deleteCertificateInfo(const Name &certificateName)
Delete a certificate.
bool empty() const
Check if name is emtpy.
virtual Name getDefaultIdentity()
Get name of the default identity.
static const string INIT_ID_TABLE
virtual ~SecPublicInfoSqlite3()
virtual void addKey(const Name &keyName, const v1::PublicKey &publicKeyDer)
Add a public key to the identity storage.
const Signature & getSignature() const
const KeyLocator & getKeyLocator() const
Get KeyLocator.
KeyType getKeyType() const
virtual bool doesCertificateExist(const Name &certificateName)
Check if the specified certificate already exists.
virtual void getAllCertificateNamesOfKey(const Name &keyName, std::vector< Name > &nameList, bool isDefault)
Get all the certificate name of a particular key name.
virtual shared_ptr< v1::PublicKey > getPublicKey(const Name &keyName)
Get shared pointer to PublicKey object from the identity storage.
represents an error in TLV encoding or decoding
static const string INIT_KEY_TABLE
virtual Name getDefaultKeyNameForIdentity(const Name &identityName)
Get name of the default key name for the specified identity.
time::system_clock::TimePoint & getNotBefore()