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