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