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