Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Yingdi Yu | 874678f | 2014-01-22 19:30:34 -0800 | [diff] [blame] | 9 | #include <sqlite3.h> |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <sstream> |
| 13 | #include <fstream> |
Yingdi Yu | 0b9c315 | 2014-01-26 16:31:18 -0800 | [diff] [blame] | 14 | #include <boost/filesystem.hpp> |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 15 | #include "../util/logging.hpp" |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 16 | #include "../util/time.hpp" |
| 17 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 18 | #include "data.hpp" |
| 19 | #include "security/identity-certificate.hpp" |
| 20 | #include "security/sec-public-info-sqlite3.hpp" |
| 21 | #include "security/signature-sha256-with-rsa.hpp" |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 22 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | INIT_LOGGER("BasicKeyMetaInfo"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn |
| 29 | { |
| 30 | |
| 31 | static const string INIT_ID_TABLE = "\ |
| 32 | CREATE TABLE IF NOT EXISTS \n \ |
| 33 | Identity( \n \ |
| 34 | identity_name BLOB NOT NULL, \n \ |
| 35 | default_identity INTEGER DEFAULT 0, \n \ |
| 36 | \ |
| 37 | PRIMARY KEY (identity_name) \n \ |
| 38 | ); \n \ |
| 39 | \ |
| 40 | CREATE INDEX identity_index ON Identity(identity_name); \n \ |
| 41 | "; |
| 42 | |
| 43 | static const string INIT_KEY_TABLE = "\ |
| 44 | CREATE TABLE IF NOT EXISTS \n \ |
| 45 | Key( \n \ |
| 46 | identity_name BLOB NOT NULL, \n \ |
| 47 | key_identifier BLOB NOT NULL, \n \ |
| 48 | key_type INTEGER, \n \ |
| 49 | public_key BLOB, \n \ |
| 50 | default_key INTEGER DEFAULT 0, \n \ |
| 51 | active INTEGER DEFAULT 0, \n \ |
| 52 | \ |
| 53 | PRIMARY KEY (identity_name, key_identifier) \n \ |
| 54 | ); \n \ |
| 55 | \ |
| 56 | CREATE INDEX key_index ON Key(identity_name); \n \ |
| 57 | "; |
| 58 | |
| 59 | static const string INIT_CERT_TABLE = "\ |
| 60 | CREATE TABLE IF NOT EXISTS \n \ |
| 61 | Certificate( \n \ |
| 62 | cert_name BLOB NOT NULL, \n \ |
| 63 | cert_issuer BLOB NOT NULL, \n \ |
| 64 | identity_name BLOB NOT NULL, \n \ |
| 65 | key_identifier BLOB NOT NULL, \n \ |
| 66 | not_before TIMESTAMP, \n \ |
| 67 | not_after TIMESTAMP, \n \ |
| 68 | certificate_data BLOB NOT NULL, \n \ |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 69 | valid_flag INTEGER DEFAULT 1, \n \ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 70 | default_cert INTEGER DEFAULT 0, \n \ |
| 71 | \ |
| 72 | PRIMARY KEY (cert_name) \n \ |
| 73 | ); \n \ |
| 74 | \ |
| 75 | CREATE INDEX cert_index ON Certificate(cert_name); \n \ |
| 76 | CREATE INDEX subject ON Certificate(identity_name); \n \ |
| 77 | "; |
| 78 | |
| 79 | /** |
| 80 | * A utility function to call the normal sqlite3_bind_text where the value and length are value.c_str() and value.size(). |
| 81 | */ |
| 82 | static int sqlite3_bind_text(sqlite3_stmt* statement, int index, const string& value, void(*destructor)(void*)) |
| 83 | { |
| 84 | return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor); |
| 85 | } |
| 86 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 87 | SecPublicInfoSqlite3::SecPublicInfoSqlite3() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 88 | { |
Yingdi Yu | 0b9c315 | 2014-01-26 16:31:18 -0800 | [diff] [blame] | 89 | boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndnx"; |
| 90 | boost::filesystem::create_directories (identityDir); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 5b60f70 | 2014-02-07 12:55:24 -0800 | [diff] [blame^] | 92 | /// @todo Add define for windows/unix in wscript. The following may completely fail on windows |
| 93 | int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database, |
| 94 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 95 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
| 96 | "unix-dotfile" |
| 97 | #else |
| 98 | 0 |
| 99 | #endif |
| 100 | ); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 101 | if (res != SQLITE_OK) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 102 | throw Error("identity DB cannot be opened/created"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 103 | |
| 104 | //Check if Key table exists; |
| 105 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 106 | sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'", -1, &statement, 0); |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 107 | res = sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 108 | |
| 109 | bool idTableExists = false; |
| 110 | if (res == SQLITE_ROW) |
| 111 | idTableExists = true; |
| 112 | |
| 113 | sqlite3_finalize(statement); |
| 114 | |
| 115 | if (!idTableExists) { |
| 116 | char *errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 117 | res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 118 | |
| 119 | if (res != SQLITE_OK && errorMessage != 0) { |
| 120 | _LOG_TRACE("Init \"error\" in Identity: " << errorMessage); |
| 121 | sqlite3_free(errorMessage); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | //Check if Key table exists; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 126 | sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 127 | res = sqlite3_step(statement); |
| 128 | |
| 129 | bool keyTableExists = false; |
| 130 | if (res == SQLITE_ROW) |
| 131 | keyTableExists = true; |
| 132 | |
| 133 | sqlite3_finalize(statement); |
| 134 | |
| 135 | if (!keyTableExists) { |
| 136 | char *errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 137 | res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 138 | |
| 139 | if (res != SQLITE_OK && errorMessage != 0) { |
| 140 | _LOG_TRACE("Init \"error\" in KEY: " << errorMessage); |
| 141 | sqlite3_free(errorMessage); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //Check if Certificate table exists; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 146 | sqlite3_prepare_v2(m_database, "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 147 | res = sqlite3_step(statement); |
| 148 | |
| 149 | bool idCertificateTableExists = false; |
| 150 | if (res == SQLITE_ROW) |
| 151 | idCertificateTableExists = true; |
| 152 | |
| 153 | sqlite3_finalize(statement); |
| 154 | |
| 155 | if (!idCertificateTableExists) { |
| 156 | char *errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 157 | res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 158 | |
| 159 | if (res != SQLITE_OK && errorMessage != 0) { |
| 160 | _LOG_TRACE("Init \"error\" in ID-CERT: " << errorMessage); |
| 161 | sqlite3_free(errorMessage); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 166 | SecPublicInfoSqlite3::~SecPublicInfoSqlite3() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 167 | { |
| 168 | } |
| 169 | |
| 170 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 171 | SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 172 | { |
| 173 | bool result = false; |
| 174 | |
| 175 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 176 | sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Identity WHERE identity_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 177 | |
| 178 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 179 | int res = sqlite3_step(statement); |
| 180 | |
| 181 | if (res == SQLITE_ROW) { |
| 182 | int countAll = sqlite3_column_int(statement, 0); |
| 183 | if (countAll > 0) |
| 184 | result = true; |
| 185 | } |
| 186 | |
| 187 | sqlite3_finalize(statement); |
| 188 | |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 193 | SecPublicInfoSqlite3::addIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 194 | { |
| 195 | if (doesIdentityExist(identityName)) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 196 | throw Error("Identity already exists"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 197 | |
| 198 | sqlite3_stmt *statement; |
| 199 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 200 | sqlite3_prepare_v2(m_database, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 201 | |
| 202 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 203 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 204 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 205 | |
| 206 | sqlite3_finalize(statement); |
| 207 | } |
| 208 | |
| 209 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 210 | SecPublicInfoSqlite3::revokeIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 211 | { |
| 212 | //TODO: |
| 213 | return false; |
| 214 | } |
| 215 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 216 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 217 | SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 218 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 219 | if(keyName.empty()) |
| 220 | throw Error("Incorrect key name " + keyName.toUri()); |
| 221 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 222 | string keyId = keyName.get(-1).toEscapedString(); |
| 223 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 224 | |
| 225 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 226 | sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 227 | |
| 228 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 229 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 230 | |
| 231 | int res = sqlite3_step(statement); |
| 232 | |
| 233 | bool keyIdExist = false; |
| 234 | if (res == SQLITE_ROW) { |
| 235 | int countAll = sqlite3_column_int(statement, 0); |
| 236 | if (countAll > 0) |
| 237 | keyIdExist = true; |
| 238 | } |
| 239 | |
| 240 | sqlite3_finalize(statement); |
| 241 | |
| 242 | return keyIdExist; |
| 243 | } |
| 244 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 245 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 246 | SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 247 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 248 | if(keyName.empty()) |
| 249 | throw Error("Incorrect key name " + keyName.toUri()); |
| 250 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 251 | string keyId = keyName.get(-1).toEscapedString(); |
| 252 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 253 | |
| 254 | |
| 255 | if (!doesIdentityExist(identityName)) |
| 256 | addIdentity(identityName); |
| 257 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 258 | if (doesPublicKeyExist(keyName)) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 259 | throw Error("a key with the same name already exists!"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 260 | |
| 261 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 262 | sqlite3_prepare_v2(m_database, "INSERT INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 263 | |
| 264 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 265 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 266 | sqlite3_bind_int(statement, 3, (int)keyType); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 267 | sqlite3_bind_blob(statement, 4, publicKeyDer.get().buf(), publicKeyDer.get().size(), SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 269 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 270 | |
| 271 | sqlite3_finalize(statement); |
| 272 | } |
| 273 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 274 | ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 275 | SecPublicInfoSqlite3::getPublicKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 276 | { |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 277 | if (!doesPublicKeyExist(keyName)) { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 278 | _LOG_DEBUG("keyName does not exist"); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 279 | return ptr_lib::shared_ptr<PublicKey>(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 282 | string keyId = keyName.get(-1).toEscapedString(); |
| 283 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 284 | |
| 285 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 286 | sqlite3_prepare_v2(m_database, "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 287 | |
| 288 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 289 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 290 | |
| 291 | int res = sqlite3_step(statement); |
| 292 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 293 | ptr_lib::shared_ptr<PublicKey> result; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 294 | if (res == SQLITE_ROW) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 295 | result = ptr_lib::make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0)); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 296 | |
| 297 | sqlite3_finalize(statement); |
| 298 | |
| 299 | return result; |
| 300 | } |
| 301 | |
| 302 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 303 | SecPublicInfoSqlite3::updateKeyStatus(const Name& keyName, bool isActive) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 304 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 305 | if(keyName.empty()) |
| 306 | throw Error("Incorrect key name " + keyName.toUri()); |
| 307 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 308 | string keyId = keyName.get(-1).toEscapedString(); |
| 309 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 310 | |
| 311 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 312 | sqlite3_prepare_v2(m_database, "UPDATE Key SET active=? WHERE identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 313 | |
| 314 | sqlite3_bind_int(statement, 1, (isActive ? 1 : 0)); |
| 315 | sqlite3_bind_text(statement, 2, identityName.toUri(), SQLITE_TRANSIENT); |
| 316 | sqlite3_bind_text(statement, 3, keyId, SQLITE_TRANSIENT); |
| 317 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 318 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 319 | |
| 320 | sqlite3_finalize(statement); |
| 321 | } |
| 322 | |
| 323 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 324 | SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 325 | { |
| 326 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 327 | sqlite3_prepare_v2(m_database, "SELECT count(*) FROM Certificate WHERE cert_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 328 | |
| 329 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 330 | |
| 331 | int res = sqlite3_step(statement); |
| 332 | |
| 333 | bool certExist = false; |
| 334 | if (res == SQLITE_ROW) { |
| 335 | int countAll = sqlite3_column_int(statement, 0); |
| 336 | if (countAll > 0) |
| 337 | certExist = true; |
| 338 | } |
| 339 | |
| 340 | sqlite3_finalize(statement); |
| 341 | |
| 342 | return certExist; |
| 343 | } |
| 344 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 345 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 346 | SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 347 | { |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 348 | std::string certificateName = certificate.getName().toUri(); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 349 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
| 350 | |
| 351 | if(keyName.empty()) |
| 352 | throw Error("Incorrect key name " + keyName.toUri()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 353 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 354 | std::string keyId = keyName.get(-1).toEscapedString(); |
| 355 | std::string identityName = keyName.getPrefix(-1).toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 356 | |
| 357 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 358 | sqlite3_prepare_v2(m_database, |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 359 | "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\ |
| 360 | values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
| 361 | -1, &statement, 0); |
| 362 | |
| 363 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 364 | _LOG_DEBUG("certName: " << certificateName); |
| 365 | sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 366 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 367 | // this will throw an exception if the signature is not the standard one or there is no key locator present |
| 368 | SignatureSha256WithRsa signature(certificate.getSignature()); |
| 369 | std::string signerName = signature.getKeyLocator().getName().toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 370 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 371 | sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC); |
| 372 | |
| 373 | sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC); |
| 374 | sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 375 | |
Jeff Thompson | 9f2b9fc | 2013-10-19 18:00:12 -0700 | [diff] [blame] | 376 | // Convert from milliseconds to seconds since 1/1/1970. |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 377 | sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000)); |
| 378 | sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000)); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 379 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 380 | sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 381 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 382 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 383 | |
| 384 | sqlite3_finalize(statement); |
| 385 | } |
| 386 | |
| 387 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 388 | SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 389 | { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 390 | const Name& certificateName = certificate.getName(); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 391 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 392 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 393 | if (!doesPublicKeyExist(keyName)) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 394 | throw Error("No corresponding Key record for certificate!" + keyName.toUri() + " " + certificateName.toUri()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 395 | |
| 396 | // Check if certificate has already existed! |
| 397 | if (doesCertificateExist(certificateName)) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 398 | throw Error("Certificate has already been installed!"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 399 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 400 | string keyId = keyName.get(-1).toEscapedString(); |
| 401 | Name identity = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 402 | |
| 403 | // Check if the public key of certificate is the same as the key record |
| 404 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 405 | ptr_lib::shared_ptr<PublicKey> pubKey = getPublicKey(keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 406 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 407 | if (!pubKey || (*pubKey) != certificate.getPublicKeyInfo()) |
| 408 | throw Error("Certificate does not match the public key!"); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 409 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 410 | // Insert the certificate |
| 411 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 412 | sqlite3_prepare_v2(m_database, |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 413 | "INSERT INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\ |
| 414 | values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
| 415 | -1, &statement, 0); |
| 416 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 417 | _LOG_DEBUG("certName: " << certificateName.toUri()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 418 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 419 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 420 | // this will throw an exception if the signature is not the standard one or there is no key locator present |
| 421 | SignatureSha256WithRsa signature(certificate.getSignature()); |
| 422 | std::string signerName = signature.getKeyLocator().getName().toUri(); |
| 423 | |
| 424 | sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 425 | |
| 426 | sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 427 | sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 428 | |
Jeff Thompson | 9f2b9fc | 2013-10-19 18:00:12 -0700 | [diff] [blame] | 429 | // Convert from milliseconds to seconds since 1/1/1970. |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 430 | sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000)); |
| 431 | sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000)); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 432 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 433 | sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 434 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 435 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 436 | |
| 437 | sqlite3_finalize(statement); |
| 438 | } |
| 439 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 440 | ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 441 | SecPublicInfoSqlite3::getCertificate(const Name &certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 442 | { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 443 | sqlite3_stmt *statement; |
| 444 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 445 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 446 | "SELECT certificate_data FROM Certificate WHERE cert_name=?", |
| 447 | -1, &statement, 0); |
| 448 | |
| 449 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 450 | |
| 451 | int res = sqlite3_step(statement); |
| 452 | |
| 453 | ptr_lib::shared_ptr<IdentityCertificate> certificate; |
| 454 | if (res == SQLITE_ROW) |
| 455 | { |
| 456 | certificate = ptr_lib::make_shared<IdentityCertificate>(); |
| 457 | certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0))); |
| 458 | } |
| 459 | sqlite3_finalize(statement); |
| 460 | |
| 461 | return certificate; |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 462 | } |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 463 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 464 | |
| 465 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 466 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 467 | { |
| 468 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 469 | sqlite3_prepare_v2(m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 470 | |
| 471 | int res = sqlite3_step(statement); |
| 472 | |
| 473 | Name identity; |
| 474 | |
| 475 | if (res == SQLITE_ROW) |
| 476 | identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 477 | |
| 478 | sqlite3_finalize(statement); |
| 479 | |
| 480 | return identity; |
| 481 | } |
| 482 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 483 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 484 | SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 485 | { |
| 486 | sqlite3_stmt *statement; |
| 487 | |
| 488 | //Reset previous default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 489 | sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=0 WHERE default_identity=1", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 490 | |
| 491 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 492 | {} |
| 493 | |
| 494 | sqlite3_finalize(statement); |
| 495 | |
| 496 | //Set current default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 497 | sqlite3_prepare_v2(m_database, "UPDATE Identity SET default_identity=1 WHERE identity_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 498 | |
| 499 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 500 | |
| 501 | sqlite3_step(statement); |
| 502 | |
| 503 | sqlite3_finalize(statement); |
| 504 | } |
| 505 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 506 | Name |
| 507 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 508 | { |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 509 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 510 | sqlite3_prepare_v2(m_database, "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 511 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 512 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 513 | |
| 514 | int res = sqlite3_step(statement); |
| 515 | |
| 516 | Name keyName; |
| 517 | |
| 518 | if (res == SQLITE_ROW) |
| 519 | keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 520 | |
| 521 | sqlite3_finalize(statement); |
| 522 | |
| 523 | return keyName; |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 528 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 529 | if(keyName.empty()) |
| 530 | throw Error("Incorrect key name " + keyName.toUri()); |
| 531 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 532 | string keyId = keyName.get(-1).toEscapedString(); |
| 533 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 534 | |
| 535 | sqlite3_stmt *statement; |
| 536 | |
| 537 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 538 | sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 539 | |
| 540 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 541 | |
| 542 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 543 | {} |
| 544 | |
| 545 | sqlite3_finalize(statement); |
| 546 | |
| 547 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 548 | sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 549 | |
| 550 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 551 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 552 | |
| 553 | sqlite3_step(statement); |
| 554 | |
| 555 | sqlite3_finalize(statement); |
| 556 | } |
| 557 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 558 | Name |
| 559 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 560 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 561 | if(keyName.empty()) |
| 562 | return Name(); |
| 563 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 564 | string keyId = keyName.get(-1).toEscapedString(); |
| 565 | Name identityName = keyName.getPrefix(-1); |
| 566 | |
| 567 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 568 | sqlite3_prepare_v2(m_database, "SELECT cert_name FROM Certificate WHERE identity_name=? AND key_identifier=? AND default_cert=1", -1, &statement, 0); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 569 | |
| 570 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 571 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 572 | |
| 573 | int res = sqlite3_step(statement); |
| 574 | |
| 575 | Name certName; |
| 576 | |
| 577 | if (res == SQLITE_ROW) |
| 578 | certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 579 | |
| 580 | sqlite3_finalize(statement); |
| 581 | |
| 582 | return certName; |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 587 | { |
| 588 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 589 | if(keyName.empty()) |
| 590 | throw Error("Incorrect key name for certificate " + certificateName.toUri()); |
| 591 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 592 | string keyId = keyName.get(-1).toEscapedString(); |
| 593 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 594 | |
| 595 | sqlite3_stmt *statement; |
| 596 | |
| 597 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 598 | sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=0 WHERE default_cert=1 AND identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 599 | |
| 600 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 601 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 602 | |
| 603 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 604 | {} |
| 605 | |
| 606 | sqlite3_finalize(statement); |
| 607 | |
| 608 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 609 | sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=1 WHERE identity_name=? AND key_identifier=? AND cert_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 610 | |
| 611 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 612 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 613 | sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
| 614 | |
| 615 | sqlite3_step(statement); |
| 616 | |
| 617 | sqlite3_finalize(statement); |
| 618 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 619 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 620 | void |
| 621 | SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 622 | { |
| 623 | sqlite3_stmt *stmt; |
| 624 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 625 | sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=1", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 626 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 627 | sqlite3_prepare_v2 (m_database, "SELECT identity_name FROM Identity WHERE default_identity=0", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 628 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 629 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 630 | nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)))); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 631 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 632 | sqlite3_finalize (stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 635 | void |
| 636 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 637 | { |
| 638 | sqlite3_stmt *stmt; |
| 639 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 640 | sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 641 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 642 | sqlite3_prepare_v2 (m_database, "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 643 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 644 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 645 | { |
| 646 | Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 647 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 648 | nameList.push_back(keyName); |
| 649 | } |
| 650 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 653 | void |
| 654 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 655 | { |
| 656 | sqlite3_stmt *stmt; |
| 657 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 658 | sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 659 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 660 | sqlite3_prepare_v2 (m_database, "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 661 | |
| 662 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 663 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 664 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 665 | { |
| 666 | Name keyName(identity); |
| 667 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 668 | nameList.push_back(keyName); |
| 669 | } |
| 670 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 673 | void |
| 674 | SecPublicInfoSqlite3::getAllCertificateNames(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 675 | { |
| 676 | sqlite3_stmt *stmt; |
| 677 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 678 | sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 679 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 680 | sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 681 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 682 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 683 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 684 | |
| 685 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 688 | void |
| 689 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 690 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 691 | if(keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 692 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 693 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 694 | sqlite3_stmt *stmt; |
| 695 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 696 | sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=1 and identity_name=? and key_identifier=?", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 697 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 698 | sqlite3_prepare_v2 (m_database, "SELECT cert_name FROM Certificate WHERE default_cert=0 and identity_name=? and key_identifier=?", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 699 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 700 | Name identity = keyName.getPrefix(-1); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 701 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 702 | std::string baseKeyName = keyName.get(-1).toEscapedString(); |
| 703 | sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT); |
| 704 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 705 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 706 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 707 | |
| 708 | sqlite3_finalize (stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void |
| 712 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName) |
| 713 | { |
| 714 | if(certName.empty()) |
| 715 | return; |
| 716 | |
| 717 | sqlite3_stmt *stmt; |
| 718 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0); |
| 719 | sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT); |
| 720 | sqlite3_step(stmt); |
| 721 | sqlite3_finalize (stmt); |
| 722 | } |
| 723 | |
| 724 | void |
| 725 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName) |
| 726 | { |
| 727 | if(keyName.empty()) |
| 728 | return; |
| 729 | |
| 730 | string identity = keyName.getPrefix(-1).toUri(); |
| 731 | string keyId = keyName.get(-1).toEscapedString(); |
| 732 | |
| 733 | sqlite3_stmt *stmt; |
| 734 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 735 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 736 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 737 | sqlite3_step(stmt); |
| 738 | sqlite3_finalize (stmt); |
| 739 | |
| 740 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 741 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 742 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 743 | sqlite3_step(stmt); |
| 744 | sqlite3_finalize (stmt); |
| 745 | } |
| 746 | |
| 747 | void |
| 748 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName) |
| 749 | { |
| 750 | string identity = identityName.toUri(); |
| 751 | |
| 752 | sqlite3_stmt *stmt; |
| 753 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0); |
| 754 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 755 | sqlite3_step(stmt); |
| 756 | sqlite3_finalize (stmt); |
| 757 | |
| 758 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0); |
| 759 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 760 | sqlite3_step(stmt); |
| 761 | sqlite3_finalize (stmt); |
| 762 | |
| 763 | sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0); |
| 764 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 765 | sqlite3_step(stmt); |
| 766 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | } // namespace ndn |
| 770 | |