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 | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | #include "sec-public-info-sqlite3.hpp" |
| 26 | #include "identity-certificate.hpp" |
| 27 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 28 | #include "signature-sha256-with-ecdsa.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 29 | #include "../data.hpp" |
| 30 | |
Yingdi Yu | 874678f | 2014-01-22 19:30:34 -0800 | [diff] [blame] | 31 | #include <sqlite3.h> |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <sstream> |
| 35 | #include <fstream> |
Yingdi Yu | 0b9c315 | 2014-01-26 16:31:18 -0800 | [diff] [blame] | 36 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 38 | namespace ndn { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::vector; |
| 42 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 43 | const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 45 | static const string INIT_TPM_INFO_TABLE = |
| 46 | "CREATE TABLE IF NOT EXISTS " |
| 47 | " TpmInfo( " |
| 48 | " tpm_locator BLOB NOT NULL," |
| 49 | " PRIMARY KEY (tpm_locator) " |
| 50 | " ); "; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 51 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 52 | static const string INIT_ID_TABLE = |
| 53 | "CREATE TABLE IF NOT EXISTS " |
| 54 | " Identity( " |
| 55 | " identity_name BLOB NOT NULL, " |
| 56 | " default_identity INTEGER DEFAULT 0, " |
| 57 | " PRIMARY KEY (identity_name) " |
| 58 | " ); " |
| 59 | "CREATE INDEX identity_index ON Identity(identity_name);"; |
| 60 | |
| 61 | static const string INIT_KEY_TABLE = |
| 62 | "CREATE TABLE IF NOT EXISTS " |
| 63 | " Key( " |
| 64 | " identity_name BLOB NOT NULL, " |
| 65 | " key_identifier BLOB NOT NULL, " |
| 66 | " key_type INTEGER, " |
| 67 | " public_key BLOB, " |
| 68 | " default_key INTEGER DEFAULT 0, " |
| 69 | " active INTEGER DEFAULT 0, " |
| 70 | " PRIMARY KEY (identity_name, key_identifier)" |
| 71 | " ); " |
| 72 | "CREATE INDEX key_index ON Key(identity_name); "; |
| 73 | |
| 74 | |
| 75 | static const string INIT_CERT_TABLE = |
| 76 | "CREATE TABLE IF NOT EXISTS " |
| 77 | " Certificate( " |
| 78 | " cert_name BLOB NOT NULL, " |
| 79 | " cert_issuer BLOB NOT NULL, " |
| 80 | " identity_name BLOB NOT NULL, " |
| 81 | " key_identifier BLOB NOT NULL, " |
| 82 | " not_before TIMESTAMP, " |
| 83 | " not_after TIMESTAMP, " |
| 84 | " certificate_data BLOB NOT NULL, " |
| 85 | " valid_flag INTEGER DEFAULT 1, " |
| 86 | " default_cert INTEGER DEFAULT 0, " |
| 87 | " PRIMARY KEY (cert_name) " |
| 88 | " ); " |
| 89 | "CREATE INDEX cert_index ON Certificate(cert_name); " |
| 90 | "CREATE INDEX subject ON Certificate(identity_name);"; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 93 | * A utility function to call the normal sqlite3_bind_text where the value and length are |
| 94 | * value.c_str() and value.size(). |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 95 | */ |
Alexander Afanasyev | db4affc | 2014-07-15 13:37:16 -0700 | [diff] [blame] | 96 | static int |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 97 | sqlite3_bind_string(sqlite3_stmt* statement, |
| 98 | int index, |
| 99 | const string& value, |
| 100 | void(*destructor)(void*)) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 101 | { |
| 102 | return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor); |
| 103 | } |
| 104 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 105 | static string |
| 106 | sqlite3_column_string(sqlite3_stmt* statement, int column) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 107 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 108 | return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)), |
| 109 | sqlite3_column_bytes(statement, column)); |
| 110 | } |
| 111 | |
| 112 | SecPublicInfoSqlite3::SecPublicInfoSqlite3(const std::string& dir) |
| 113 | : SecPublicInfo(dir) |
| 114 | , m_database(nullptr) |
| 115 | { |
| 116 | boost::filesystem::path identityDir; |
| 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) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 133 | BOOST_THROW_EXCEPTION(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); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 231 | BOOST_THROW_EXCEPTION(SecPublicInfo::Error("TPM info does not exist")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 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()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 324 | BOOST_THROW_EXCEPTION(Error("Incorrect key name " + keyName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 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()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 390 | BOOST_THROW_EXCEPTION(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); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 414 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getPublicKey public key does not exist")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 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); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 548 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate cannot be " |
| 549 | "decoded")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 550 | } |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 551 | |
| 552 | sqlite3_finalize(statement); |
| 553 | return certificate; |
| 554 | } |
| 555 | else { |
| 556 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 557 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getCertificate certificate does not " |
| 558 | "exist")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 559 | } |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 560 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 561 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 562 | |
| 563 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 564 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 565 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 566 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 567 | sqlite3_prepare_v2(m_database, |
| 568 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 569 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 570 | |
| 571 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 572 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 573 | if (res == SQLITE_ROW) { |
| 574 | Name identity(sqlite3_column_string(statement, 0)); |
| 575 | sqlite3_finalize(statement); |
| 576 | return identity; |
| 577 | } |
| 578 | else { |
| 579 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 580 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 581 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 584 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 585 | SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 586 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 587 | addIdentity(identityName); |
| 588 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 589 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 590 | |
| 591 | //Reset previous default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 592 | sqlite3_prepare_v2(m_database, |
| 593 | "UPDATE Identity SET default_identity=0 WHERE default_identity=1", |
| 594 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 595 | |
| 596 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 597 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 598 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 599 | sqlite3_finalize(statement); |
| 600 | |
| 601 | //Set current default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 602 | sqlite3_prepare_v2(m_database, |
| 603 | "UPDATE Identity SET default_identity=1 WHERE identity_name=?", |
| 604 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 605 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 606 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 607 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 608 | sqlite3_step(statement); |
| 609 | |
| 610 | sqlite3_finalize(statement); |
| 611 | } |
| 612 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 613 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 614 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 615 | { |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 616 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 617 | sqlite3_prepare_v2(m_database, |
| 618 | "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", |
| 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); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 622 | |
| 623 | int res = sqlite3_step(statement); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 624 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 625 | if (res == SQLITE_ROW) { |
| 626 | Name keyName = identityName; |
| 627 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)), |
| 628 | sqlite3_column_bytes(statement, 0))); |
| 629 | sqlite3_finalize(statement); |
| 630 | return keyName; |
| 631 | } |
| 632 | else { |
| 633 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 634 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not " |
| 635 | "found")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 636 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 639 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 640 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 641 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 642 | if (!doesPublicKeyExist(keyName)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 643 | BOOST_THROW_EXCEPTION(Error("Key does not exist:" + keyName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 644 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 645 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 646 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 647 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 648 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 649 | |
| 650 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 651 | sqlite3_prepare_v2(m_database, |
| 652 | "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", |
| 653 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 654 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 655 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 656 | |
| 657 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 658 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 659 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 660 | sqlite3_finalize(statement); |
| 661 | |
| 662 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 663 | sqlite3_prepare_v2(m_database, |
| 664 | "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", |
| 665 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 666 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 667 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 668 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 669 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 670 | sqlite3_step(statement); |
| 671 | |
| 672 | sqlite3_finalize(statement); |
| 673 | } |
| 674 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 675 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 676 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 677 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 678 | if (keyName.empty()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 679 | BOOST_THROW_EXCEPTION(Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key")); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 680 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 681 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 682 | Name identityName = keyName.getPrefix(-1); |
| 683 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 684 | sqlite3_stmt* statement = nullptr; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 685 | sqlite3_prepare_v2(m_database, |
| 686 | "SELECT cert_name FROM Certificate \ |
| 687 | WHERE identity_name=? AND key_identifier=? AND default_cert=1", |
| 688 | -1, &statement, 0); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 689 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 690 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 691 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 692 | |
| 693 | int res = sqlite3_step(statement); |
| 694 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 695 | if (res == SQLITE_ROW) { |
| 696 | Name certName(string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)), |
| 697 | sqlite3_column_bytes(statement, 0))); |
| 698 | sqlite3_finalize(statement); |
| 699 | return certName; |
| 700 | } |
| 701 | else { |
| 702 | sqlite3_finalize(statement); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 703 | BOOST_THROW_EXCEPTION(Error("certificate not found")); |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 704 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 707 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 708 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 709 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 710 | if (!doesCertificateExist(certificateName)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame^] | 711 | BOOST_THROW_EXCEPTION(Error("certificate does not exist:" + certificateName.toUri())); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 712 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 713 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 714 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 715 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 716 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 717 | sqlite3_stmt* statement = nullptr; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 718 | |
| 719 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 720 | sqlite3_prepare_v2(m_database, |
| 721 | "UPDATE Certificate SET default_cert=0 \ |
| 722 | WHERE default_cert=1 AND identity_name=? AND key_identifier=?", |
| 723 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 724 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 725 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 726 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 727 | |
| 728 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 729 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 730 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 731 | sqlite3_finalize(statement); |
| 732 | |
| 733 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 734 | sqlite3_prepare_v2(m_database, |
| 735 | "UPDATE Certificate SET default_cert=1 \ |
| 736 | WHERE identity_name=? AND key_identifier=? AND cert_name=?", |
| 737 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 738 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 739 | sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 740 | sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT); |
| 741 | sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 742 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 743 | sqlite3_step(statement); |
| 744 | |
| 745 | sqlite3_finalize(statement); |
| 746 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 747 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 748 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 749 | SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 750 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 751 | sqlite3_stmt* stmt; |
| 752 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 753 | sqlite3_prepare_v2(m_database, |
| 754 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 755 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 756 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 757 | sqlite3_prepare_v2(m_database, |
| 758 | "SELECT identity_name FROM Identity WHERE default_identity=0", |
| 759 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 760 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 761 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 762 | 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] | 763 | sqlite3_column_bytes(stmt, 0)))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 764 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 765 | sqlite3_finalize(stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 768 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 769 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 770 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 771 | sqlite3_stmt* stmt; |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 772 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 773 | if (isDefault) |
| 774 | sqlite3_prepare_v2(m_database, |
| 775 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", |
| 776 | -1, &stmt, 0); |
| 777 | else |
| 778 | sqlite3_prepare_v2(m_database, |
| 779 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", |
| 780 | -1, &stmt, 0); |
| 781 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 782 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 783 | Name keyName(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 784 | sqlite3_column_bytes(stmt, 0))); |
| 785 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)), |
| 786 | sqlite3_column_bytes(stmt, 1))); |
| 787 | nameList.push_back(keyName); |
| 788 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 789 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 792 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 793 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, |
| 794 | vector<Name>& nameList, |
| 795 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 796 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 797 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 798 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 799 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 800 | sqlite3_prepare_v2(m_database, |
| 801 | "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", |
| 802 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 803 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 804 | sqlite3_prepare_v2(m_database, |
| 805 | "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", |
| 806 | -1, &stmt, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 807 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 808 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 809 | |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 810 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 811 | Name keyName(identity); |
| 812 | keyName.append(string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 813 | sqlite3_column_bytes(stmt, 0))); |
| 814 | nameList.push_back(keyName); |
| 815 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 816 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 817 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 818 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 819 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 820 | SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 821 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 822 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 823 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 824 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 825 | sqlite3_prepare_v2(m_database, |
| 826 | "SELECT cert_name FROM Certificate WHERE default_cert=1", |
| 827 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 828 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 829 | sqlite3_prepare_v2(m_database, |
| 830 | "SELECT cert_name FROM Certificate WHERE default_cert=0", |
| 831 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 832 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 833 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 834 | 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] | 835 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 836 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 837 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 840 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 841 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, |
| 842 | vector<Name>& nameList, |
| 843 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 844 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 845 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 846 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 847 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 848 | sqlite3_stmt* stmt; |
| 849 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 850 | sqlite3_prepare_v2(m_database, |
| 851 | "SELECT cert_name FROM Certificate \ |
| 852 | WHERE default_cert=1 and identity_name=? and key_identifier=?", |
| 853 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 854 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 855 | sqlite3_prepare_v2(m_database, |
| 856 | "SELECT cert_name FROM Certificate \ |
| 857 | WHERE default_cert=0 and identity_name=? and key_identifier=?", |
| 858 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 859 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 860 | Name identity = keyName.getPrefix(-1); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 861 | sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 862 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 863 | std::string baseKeyName = keyName.get(-1).toUri(); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 864 | sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 865 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 866 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Yingdi Yu | a90ba48 | 2014-12-09 10:54:43 -0800 | [diff] [blame] | 867 | 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] | 868 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 869 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 870 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 874 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 875 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 876 | if (certName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 877 | return; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 878 | |
| 879 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 880 | 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] | 881 | sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 882 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 883 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 887 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 888 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 889 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 890 | return; |
| 891 | |
| 892 | string identity = keyName.getPrefix(-1).toUri(); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 893 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 894 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 895 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 896 | sqlite3_prepare_v2(m_database, |
| 897 | "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", |
| 898 | -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 899 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 900 | sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 901 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 902 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 903 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 904 | sqlite3_prepare_v2(m_database, |
| 905 | "DELETE FROM Key WHERE identity_name=? and key_identifier=?", |
| 906 | -1, &stmt, 0); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 907 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
| 908 | sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 909 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 910 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 914 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 915 | { |
| 916 | string identity = identityName.toUri(); |
| 917 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 918 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 919 | 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] | 920 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 921 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 922 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 923 | |
| 924 | 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] | 925 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 926 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 927 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 928 | |
| 929 | 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] | 930 | sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 931 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 932 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 935 | std::string |
| 936 | SecPublicInfoSqlite3::getScheme() |
| 937 | { |
| 938 | return SCHEME; |
| 939 | } |
| 940 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 941 | } // namespace ndn |