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