Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | #include "sec-public-info-sqlite3.hpp" |
| 26 | #include "identity-certificate.hpp" |
| 27 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 28 | #include "signature-sha256-with-ecdsa.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 29 | #include "../data.hpp" |
| 30 | |
Yingdi Yu | 874678f | 2014-01-22 19:30:34 -0800 | [diff] [blame] | 31 | #include <sqlite3.h> |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <sstream> |
| 35 | #include <fstream> |
Yingdi Yu | 0b9c315 | 2014-01-26 16:31:18 -0800 | [diff] [blame] | 36 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 38 | namespace ndn { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::vector; |
| 42 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 43 | const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 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 | " ); "; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 51 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 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);"; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 93 | * A utility function to call the normal sqlite3_bind_text where the value and length are |
| 94 | * value.c_str() and value.size(). |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 95 | */ |
Alexander Afanasyev | db4affc | 2014-07-15 13:37:16 -0700 | [diff] [blame] | 96 | static int |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 97 | sqlite3_bind_string(sqlite3_stmt* statement, |
| 98 | int index, |
| 99 | const string& value, |
| 100 | void(*destructor)(void*)) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 101 | { |
| 102 | return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor); |
| 103 | } |
| 104 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 105 | static string |
| 106 | sqlite3_column_string(sqlite3_stmt* statement, int column) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 107 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 108 | return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)), |
| 109 | sqlite3_column_bytes(statement, column)); |
| 110 | } |
| 111 | |
| 112 | SecPublicInfoSqlite3::SecPublicInfoSqlite3(const std::string& dir) |
| 113 | : SecPublicInfo(dir) |
| 114 | , m_database(nullptr) |
| 115 | { |
| 116 | boost::filesystem::path identityDir; |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 117 | if (dir == "") { |
| 118 | #ifdef NDN_CXX_HAVE_TESTS |
| 119 | if (getenv("TEST_HOME") != nullptr) { |
| 120 | identityDir = boost::filesystem::path(getenv("TEST_HOME")) / ".ndn"; |
| 121 | } |
| 122 | else |
| 123 | #endif // NDN_CXX_HAVE_TESTS |
| 124 | if (getenv("HOME") != nullptr) { |
| 125 | identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn"; |
| 126 | } |
| 127 | else { |
| 128 | identityDir = boost::filesystem::path(".") / ".ndn"; |
| 129 | } |
| 130 | } |
| 131 | else { |
| 132 | identityDir = boost::filesystem::path(dir); |
| 133 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 134 | boost::filesystem::create_directories(identityDir); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 5b60f70 | 2014-02-07 12:55:24 -0800 | [diff] [blame] | 136 | /// @todo Add define for windows/unix in wscript. The following may completely fail on windows |
| 137 | int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database, |
| 138 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 139 | #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING |
Alexander Afanasyev | 4447146 | 2014-02-12 11:21:51 -0800 | [diff] [blame] | 140 | "unix-dotfile" |
Alexander Afanasyev | 5b60f70 | 2014-02-07 12:55:24 -0800 | [diff] [blame] | 141 | #else |
| 142 | 0 |
| 143 | #endif |
| 144 | ); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 145 | if (res != SQLITE_OK) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 146 | BOOST_THROW_EXCEPTION(Error("identity DB cannot be opened/created")); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 148 | |
Junxiao Shi | 98acbb0 | 2014-12-02 11:00:42 -0700 | [diff] [blame] | 149 | BOOST_ASSERT(m_database != nullptr); |
| 150 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 151 | initializeTable("TpmInfo", INIT_TPM_INFO_TABLE); // Check if TpmInfo table exists; |
| 152 | initializeTable("Identity", INIT_ID_TABLE); // Check if Identity table exists; |
| 153 | initializeTable("Key", INIT_KEY_TABLE); // Check if Key table exists; |
| 154 | initializeTable("Certificate", INIT_CERT_TABLE); // Check if Certificate table exists; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 157 | SecPublicInfoSqlite3::~SecPublicInfoSqlite3() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 158 | { |
Junxiao Shi | 98acbb0 | 2014-12-02 11:00:42 -0700 | [diff] [blame] | 159 | sqlite3_close(m_database); |
| 160 | m_database = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 163 | bool |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 164 | SecPublicInfoSqlite3::doesTableExist(const string& tableName) |
| 165 | { |
| 166 | // Check if the table exists; |
| 167 | bool doesTableExist = false; |
| 168 | string checkingString = |
| 169 | "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'"; |
| 170 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 171 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 172 | sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0); |
| 173 | |
| 174 | int result = sqlite3_step(statement); |
| 175 | if (result == SQLITE_ROW) |
| 176 | doesTableExist = true; |
| 177 | sqlite3_finalize(statement); |
| 178 | |
| 179 | return doesTableExist; |
| 180 | } |
| 181 | |
| 182 | bool |
| 183 | SecPublicInfoSqlite3::initializeTable(const string& tableName, const string& initCommand) |
| 184 | { |
| 185 | // Create the table if it does not exist |
| 186 | if (!doesTableExist(tableName)) { |
| 187 | char* errorMessage = 0; |
| 188 | int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage); |
| 189 | |
| 190 | if (result != SQLITE_OK && errorMessage != 0) { |
| 191 | sqlite3_free(errorMessage); |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | SecPublicInfoSqlite3::deleteTable(const string& tableName) |
| 201 | { |
| 202 | string query = "DROP TABLE IF EXISTS " + tableName; |
| 203 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 204 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 205 | sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0); |
| 206 | |
| 207 | sqlite3_step(statement); |
| 208 | sqlite3_finalize(statement); |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | SecPublicInfoSqlite3::setTpmLocator(const string& tpmLocator) |
| 213 | { |
| 214 | string currentTpm; |
| 215 | try { |
| 216 | currentTpm = getTpmLocator(); |
| 217 | } |
| 218 | catch (SecPublicInfo::Error&) { |
| 219 | setTpmLocatorInternal(tpmLocator, false); // set tpmInfo without resetting |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (currentTpm == tpmLocator) |
| 224 | return; // if the same, nothing will be changed |
| 225 | |
| 226 | setTpmLocatorInternal(tpmLocator, true); // set tpmInfo and reset pib |
| 227 | } |
| 228 | |
| 229 | string |
| 230 | SecPublicInfoSqlite3::getTpmLocator() |
| 231 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 232 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 233 | sqlite3_prepare_v2(m_database, "SELECT tpm_locator FROM TpmInfo", -1, &statement, 0); |
| 234 | |
| 235 | int res = sqlite3_step(statement); |
| 236 | |
| 237 | if (res == SQLITE_ROW) { |
| 238 | string tpmLocator = sqlite3_column_string(statement, 0); |
| 239 | sqlite3_finalize(statement); |
| 240 | return tpmLocator; |
| 241 | } |
| 242 | else { |
| 243 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 244 | BOOST_THROW_EXCEPTION(SecPublicInfo::Error("TPM info does not exist")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | SecPublicInfoSqlite3::setTpmLocatorInternal(const string& tpmLocator, bool needReset) |
| 250 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 251 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 252 | |
| 253 | if (needReset) { |
| 254 | deleteTable("Identity"); |
| 255 | deleteTable("Key"); |
| 256 | deleteTable("Certificate"); |
| 257 | |
| 258 | initializeTable("Identity", INIT_ID_TABLE); |
| 259 | initializeTable("Key", INIT_KEY_TABLE); |
| 260 | initializeTable("Certificate", INIT_CERT_TABLE); |
| 261 | |
| 262 | sqlite3_prepare_v2(m_database, "UPDATE TpmInfo SET tpm_locator = ?", |
| 263 | -1, &statement, 0); |
| 264 | sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT); |
| 265 | } |
| 266 | else { |
| 267 | // no reset implies there is no tpmLocator record, insert one |
| 268 | sqlite3_prepare_v2(m_database, "INSERT INTO TpmInfo (tpm_locator) VALUES (?)", |
| 269 | -1, &statement, 0); |
| 270 | sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT); |
| 271 | } |
| 272 | |
| 273 | sqlite3_step(statement); |
| 274 | sqlite3_finalize(statement); |
| 275 | } |
| 276 | |
| 277 | std::string |
| 278 | SecPublicInfoSqlite3::getPibLocator() |
| 279 | { |
| 280 | return string("pib-sqlite3:").append(m_location); |
| 281 | } |
| 282 | |
| 283 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 284 | SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 285 | { |
| 286 | bool result = false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 287 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 288 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 289 | sqlite3_prepare_v2(m_database, |
| 290 | "SELECT count(*) FROM Identity WHERE identity_name=?", |
| 291 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 292 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 293 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 294 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 295 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 296 | if (res == SQLITE_ROW) { |
| 297 | int countAll = sqlite3_column_int(statement, 0); |
| 298 | if (countAll > 0) |
| 299 | result = true; |
| 300 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 301 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 302 | sqlite3_finalize(statement); |
| 303 | |
| 304 | return result; |
| 305 | } |
| 306 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 307 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 308 | SecPublicInfoSqlite3::addIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 309 | { |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 310 | if (doesIdentityExist(identityName)) |
| 311 | return; |
| 312 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 313 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 314 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 315 | sqlite3_prepare_v2(m_database, |
| 316 | "INSERT OR REPLACE INTO Identity (identity_name) values (?)", |
| 317 | -1, &statement, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 318 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 319 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 320 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 321 | sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 322 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 323 | sqlite3_finalize(statement); |
| 324 | } |
| 325 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 326 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 327 | SecPublicInfoSqlite3::revokeIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 328 | { |
| 329 | //TODO: |
| 330 | return false; |
| 331 | } |
| 332 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 333 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 334 | SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 335 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 336 | if (keyName.empty()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 337 | BOOST_THROW_EXCEPTION(Error("Incorrect key name " + keyName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 339 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 340 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 341 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 342 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 343 | sqlite3_prepare_v2(m_database, |
| 344 | "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", |
| 345 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 346 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 347 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 348 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 349 | |
| 350 | int res = sqlite3_step(statement); |
| 351 | |
| 352 | bool keyIdExist = false; |
| 353 | if (res == SQLITE_ROW) { |
| 354 | int countAll = sqlite3_column_int(statement, 0); |
| 355 | if (countAll > 0) |
| 356 | keyIdExist = true; |
| 357 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 358 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 359 | sqlite3_finalize(statement); |
| 360 | |
| 361 | return keyIdExist; |
| 362 | } |
| 363 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 364 | void |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 365 | SecPublicInfoSqlite3::addKey(const Name& keyName, |
| 366 | const PublicKey& publicKeyDer) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 367 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 368 | if (keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 369 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 370 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 371 | if (doesPublicKeyExist(keyName)) |
| 372 | return; |
| 373 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 374 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 375 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 376 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 377 | addIdentity(identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 378 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 379 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 380 | sqlite3_prepare_v2(m_database, |
| 381 | "INSERT OR REPLACE INTO Key \ |
| 382 | (identity_name, key_identifier, key_type, public_key) \ |
| 383 | values (?, ?, ?, ?)", |
| 384 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 385 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 386 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 387 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame^] | 388 | sqlite3_bind_int(statement, 3, static_cast<int>(publicKeyDer.getKeyType())); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 389 | sqlite3_bind_blob(statement, 4, |
| 390 | publicKeyDer.get().buf(), |
| 391 | publicKeyDer.get().size(), |
| 392 | SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 393 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 394 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 395 | |
| 396 | sqlite3_finalize(statement); |
| 397 | } |
| 398 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 399 | shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 400 | SecPublicInfoSqlite3::getPublicKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 401 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 402 | if (keyName.empty()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 403 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey Empty keyName")); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 404 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 405 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 406 | Name identityName = keyName.getPrefix(-1); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 407 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 408 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 409 | sqlite3_prepare_v2(m_database, |
| 410 | "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", |
| 411 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 412 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 413 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 414 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 415 | |
| 416 | int res = sqlite3_step(statement); |
| 417 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 418 | shared_ptr<PublicKey> result; |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 419 | if (res == SQLITE_ROW) { |
| 420 | result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), |
| 421 | sqlite3_column_bytes(statement, 0)); |
| 422 | sqlite3_finalize(statement); |
| 423 | return result; |
| 424 | } |
| 425 | else { |
| 426 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 427 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey public key does not exist")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 428 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 431 | KeyType |
| 432 | SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName) |
| 433 | { |
| 434 | if (keyName.empty()) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame^] | 435 | return KeyType::NONE; |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 436 | |
| 437 | string keyId = keyName.get(-1).toUri(); |
| 438 | Name identityName = keyName.getPrefix(-1); |
| 439 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 440 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 441 | sqlite3_prepare_v2(m_database, |
| 442 | "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?", |
| 443 | -1, &statement, 0); |
| 444 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 445 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 446 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 447 | |
| 448 | int res = sqlite3_step(statement); |
| 449 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 450 | if (res == SQLITE_ROW) { |
| 451 | int typeValue = sqlite3_column_int(statement, 0); |
| 452 | sqlite3_finalize(statement); |
| 453 | return static_cast<KeyType>(typeValue); |
| 454 | } |
| 455 | else { |
| 456 | sqlite3_finalize(statement); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame^] | 457 | return KeyType::NONE; |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 458 | } |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 461 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 462 | SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 463 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 464 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 465 | sqlite3_prepare_v2(m_database, |
| 466 | "SELECT count(*) FROM Certificate WHERE cert_name=?", |
| 467 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 468 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 469 | sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 470 | |
| 471 | int res = sqlite3_step(statement); |
| 472 | |
| 473 | bool certExist = false; |
| 474 | if (res == SQLITE_ROW) { |
| 475 | int countAll = sqlite3_column_int(statement, 0); |
| 476 | if (countAll > 0) |
| 477 | certExist = true; |
| 478 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 479 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 480 | sqlite3_finalize(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 481 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 482 | return certExist; |
| 483 | } |
| 484 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 485 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 486 | SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 487 | { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 488 | const Name& certificateName = certificate.getName(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 489 | // KeyName is from IdentityCertificate name, so should be qualified. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 490 | Name keyName = |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 491 | IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 492 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 493 | addKey(keyName, certificate.getPublicKeyInfo()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 494 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 495 | if (doesCertificateExist(certificateName)) |
| 496 | return; |
| 497 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 498 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 499 | Name identity = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 500 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 501 | // Insert the certificate |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 502 | sqlite3_stmt* statement = nullptr; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 503 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 504 | "INSERT OR REPLACE INTO Certificate \ |
| 505 | (cert_name, cert_issuer, identity_name, key_identifier, \ |
| 506 | not_before, not_after, certificate_data) \ |
| 507 | values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 508 | -1, &statement, 0); |
| 509 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 510 | sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 511 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 512 | try { |
| 513 | // this will throw an exception if the signature is not the standard one |
| 514 | // or there is no key locator present |
| 515 | std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri(); |
| 516 | sqlite3_bind_string(statement, 2, signerName, SQLITE_TRANSIENT); |
| 517 | } |
| 518 | catch (tlv::Error&) { |
| 519 | return; |
| 520 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 521 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 522 | sqlite3_bind_string(statement, 3, identity.toUri(), SQLITE_TRANSIENT); |
| 523 | sqlite3_bind_string(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 524 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 525 | sqlite3_bind_int64(statement, 5, |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 526 | static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotBefore()).count())); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 527 | sqlite3_bind_int64(statement, 6, |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 528 | static_cast<sqlite3_int64>(time::toUnixTimestamp(certificate.getNotAfter()).count())); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 529 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 530 | sqlite3_bind_blob(statement, 7, |
| 531 | certificate.wireEncode().wire(), |
| 532 | certificate.wireEncode().size(), |
| 533 | SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 534 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 535 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 536 | |
| 537 | sqlite3_finalize(statement); |
| 538 | } |
| 539 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 540 | shared_ptr<IdentityCertificate> |
| 541 | SecPublicInfoSqlite3::getCertificate(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 542 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 543 | sqlite3_stmt* statement = nullptr; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 544 | |
| 545 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 546 | "SELECT certificate_data FROM Certificate WHERE cert_name=?", |
| 547 | -1, &statement, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 548 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 549 | sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 550 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 551 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 552 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 553 | if (res == SQLITE_ROW) { |
| 554 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
| 555 | try { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 556 | certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 557 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 558 | } |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 559 | catch (tlv::Error&) { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 560 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 561 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate cannot be " |
| 562 | "decoded")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 563 | } |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 564 | |
| 565 | sqlite3_finalize(statement); |
| 566 | return certificate; |
| 567 | } |
| 568 | else { |
| 569 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 570 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate does not " |
| 571 | "exist")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 572 | } |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 573 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 574 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 575 | |
| 576 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 577 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 578 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 579 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 580 | sqlite3_prepare_v2(m_database, |
| 581 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 582 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 583 | |
| 584 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 585 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 586 | if (res == SQLITE_ROW) { |
| 587 | Name identity(sqlite3_column_string(statement, 0)); |
| 588 | sqlite3_finalize(statement); |
| 589 | return identity; |
| 590 | } |
| 591 | else { |
| 592 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 593 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 594 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 597 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 598 | SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 599 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 600 | addIdentity(identityName); |
| 601 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 602 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 603 | |
| 604 | //Reset previous default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 605 | sqlite3_prepare_v2(m_database, |
| 606 | "UPDATE Identity SET default_identity=0 WHERE default_identity=1", |
| 607 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 608 | |
| 609 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 610 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 611 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 612 | sqlite3_finalize(statement); |
| 613 | |
| 614 | //Set current default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 615 | sqlite3_prepare_v2(m_database, |
| 616 | "UPDATE Identity SET default_identity=1 WHERE identity_name=?", |
| 617 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 618 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 619 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 620 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 621 | sqlite3_step(statement); |
| 622 | |
| 623 | sqlite3_finalize(statement); |
| 624 | } |
| 625 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 626 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 627 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 628 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 629 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 630 | sqlite3_prepare_v2(m_database, |
| 631 | "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", |
| 632 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 633 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 634 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 635 | |
| 636 | int res = sqlite3_step(statement); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 637 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 638 | if (res == SQLITE_ROW) { |
| 639 | Name keyName = identityName; |
| 640 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)), |
| 641 | sqlite3_column_bytes(statement, 0))); |
| 642 | sqlite3_finalize(statement); |
| 643 | return keyName; |
| 644 | } |
| 645 | else { |
| 646 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 647 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not " |
| 648 | "found")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 649 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 652 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 653 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 654 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 655 | if (!doesPublicKeyExist(keyName)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 656 | BOOST_THROW_EXCEPTION(Error("Key does not exist:" + keyName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 657 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 658 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 659 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 660 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 661 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 662 | |
| 663 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 664 | sqlite3_prepare_v2(m_database, |
| 665 | "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", |
| 666 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 667 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 668 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 669 | |
| 670 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 671 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 672 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 673 | sqlite3_finalize(statement); |
| 674 | |
| 675 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 676 | sqlite3_prepare_v2(m_database, |
| 677 | "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", |
| 678 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 679 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 680 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 681 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 682 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 683 | sqlite3_step(statement); |
| 684 | |
| 685 | sqlite3_finalize(statement); |
| 686 | } |
| 687 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 688 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 689 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 690 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 691 | if (keyName.empty()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 692 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key")); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 693 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 694 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 695 | Name identityName = keyName.getPrefix(-1); |
| 696 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 697 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 698 | sqlite3_prepare_v2(m_database, |
| 699 | "SELECT cert_name FROM Certificate \ |
| 700 | WHERE identity_name=? AND key_identifier=? AND default_cert=1", |
| 701 | -1, &statement, 0); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 702 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 703 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 704 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 705 | |
| 706 | int res = sqlite3_step(statement); |
| 707 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 708 | if (res == SQLITE_ROW) { |
| 709 | Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)), |
| 710 | sqlite3_column_bytes(statement, 0))); |
| 711 | sqlite3_finalize(statement); |
| 712 | return certName; |
| 713 | } |
| 714 | else { |
| 715 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 716 | BOOST_THROW_EXCEPTION(Error("certificate not found")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 717 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 720 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 721 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 722 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 723 | if (!doesCertificateExist(certificateName)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 724 | BOOST_THROW_EXCEPTION(Error("certificate does not exist:" + certificateName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 725 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 726 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 727 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 728 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 729 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 730 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 731 | |
| 732 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 733 | sqlite3_prepare_v2(m_database, |
| 734 | "UPDATE Certificate SET default_cert=0 \ |
| 735 | WHERE default_cert=1 AND identity_name=? AND key_identifier=?", |
| 736 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 737 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 738 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 739 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 740 | |
| 741 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 742 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 743 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 744 | sqlite3_finalize(statement); |
| 745 | |
| 746 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 747 | sqlite3_prepare_v2(m_database, |
| 748 | "UPDATE Certificate SET default_cert=1 \ |
| 749 | WHERE identity_name=? AND key_identifier=? AND cert_name=?", |
| 750 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 751 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 752 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 753 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
| 754 | sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 755 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 756 | sqlite3_step(statement); |
| 757 | |
| 758 | sqlite3_finalize(statement); |
| 759 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 760 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 761 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 762 | SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 763 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 764 | sqlite3_stmt* stmt; |
| 765 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 766 | sqlite3_prepare_v2(m_database, |
| 767 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 768 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 769 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 770 | sqlite3_prepare_v2(m_database, |
| 771 | "SELECT identity_name FROM Identity WHERE default_identity=0", |
| 772 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 773 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 774 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 775 | nameList.push_back(Name(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 776 | sqlite3_column_bytes(stmt, 0)))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 777 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 778 | sqlite3_finalize(stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 781 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 782 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 783 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 784 | sqlite3_stmt* stmt; |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 785 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 786 | if (isDefault) |
| 787 | sqlite3_prepare_v2(m_database, |
| 788 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", |
| 789 | -1, &stmt, 0); |
| 790 | else |
| 791 | sqlite3_prepare_v2(m_database, |
| 792 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", |
| 793 | -1, &stmt, 0); |
| 794 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 795 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 796 | Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 797 | sqlite3_column_bytes(stmt, 0))); |
| 798 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)), |
| 799 | sqlite3_column_bytes(stmt, 1))); |
| 800 | nameList.push_back(keyName); |
| 801 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 802 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 805 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 806 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, |
| 807 | vector<Name>& nameList, |
| 808 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 809 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 810 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 811 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 812 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 813 | sqlite3_prepare_v2(m_database, |
| 814 | "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", |
| 815 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 816 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 817 | sqlite3_prepare_v2(m_database, |
| 818 | "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", |
| 819 | -1, &stmt, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 820 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 821 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 822 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 823 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 824 | Name keyName(identity); |
| 825 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 826 | sqlite3_column_bytes(stmt, 0))); |
| 827 | nameList.push_back(keyName); |
| 828 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 829 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 830 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 831 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 832 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 833 | SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 834 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 835 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 836 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 837 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 838 | sqlite3_prepare_v2(m_database, |
| 839 | "SELECT cert_name FROM Certificate WHERE default_cert=1", |
| 840 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 841 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 842 | sqlite3_prepare_v2(m_database, |
| 843 | "SELECT cert_name FROM Certificate WHERE default_cert=0", |
| 844 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 845 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 846 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 847 | nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 848 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 849 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 850 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 853 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 854 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, |
| 855 | vector<Name>& nameList, |
| 856 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 857 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 858 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 859 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 860 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 861 | sqlite3_stmt* stmt; |
| 862 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 863 | sqlite3_prepare_v2(m_database, |
| 864 | "SELECT cert_name FROM Certificate \ |
| 865 | WHERE default_cert=1 and identity_name=? and key_identifier=?", |
| 866 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 867 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 868 | sqlite3_prepare_v2(m_database, |
| 869 | "SELECT cert_name FROM Certificate \ |
| 870 | WHERE default_cert=0 and identity_name=? and key_identifier=?", |
| 871 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 872 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 873 | Name identity = keyName.getPrefix(-1); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 874 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 875 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 876 | std::string baseKeyName = keyName.get(-1).toUri(); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 877 | sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 878 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 879 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 880 | nameList.push_back(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 881 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 882 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 883 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 887 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 888 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 889 | if (certName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 890 | return; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 891 | |
| 892 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 893 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 894 | sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 895 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 896 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 900 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 901 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 902 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 903 | return; |
| 904 | |
| 905 | string identity = keyName.getPrefix(-1).toUri(); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 906 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 907 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 908 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 909 | sqlite3_prepare_v2(m_database, |
| 910 | "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", |
| 911 | -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 912 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 913 | sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 914 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 915 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 916 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 917 | sqlite3_prepare_v2(m_database, |
| 918 | "DELETE FROM Key WHERE identity_name=? and key_identifier=?", |
| 919 | -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 920 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 921 | sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 922 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 923 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 927 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 928 | { |
| 929 | string identity = identityName.toUri(); |
| 930 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 931 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 932 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 933 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 934 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 935 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 936 | |
| 937 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 938 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 939 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 940 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 941 | |
| 942 | sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 943 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 944 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 945 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 948 | std::string |
| 949 | SecPublicInfoSqlite3::getScheme() |
| 950 | { |
| 951 | return SCHEME; |
| 952 | } |
| 953 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 954 | } // namespace ndn |