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