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