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 | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 29 | INIT_LOGGER("ndn.SecPublicInfoSqlite3"); |
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 | { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 199 | sqlite3_stmt *statement; |
| 200 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 201 | sqlite3_prepare_v2(m_database, "INSERT OR REPLACE INTO Identity (identity_name) values (?)", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 202 | |
| 203 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 204 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 205 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 206 | |
| 207 | sqlite3_finalize(statement); |
| 208 | } |
| 209 | |
| 210 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 211 | SecPublicInfoSqlite3::revokeIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 212 | { |
| 213 | //TODO: |
| 214 | return false; |
| 215 | } |
| 216 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 217 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 218 | SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 219 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 220 | if(keyName.empty()) |
| 221 | throw Error("Incorrect key name " + keyName.toUri()); |
| 222 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 223 | string keyId = keyName.get(-1).toEscapedString(); |
| 224 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 225 | |
| 226 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 227 | 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] | 228 | |
| 229 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 230 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 231 | |
| 232 | int res = sqlite3_step(statement); |
| 233 | |
| 234 | bool keyIdExist = false; |
| 235 | if (res == SQLITE_ROW) { |
| 236 | int countAll = sqlite3_column_int(statement, 0); |
| 237 | if (countAll > 0) |
| 238 | keyIdExist = true; |
| 239 | } |
| 240 | |
| 241 | sqlite3_finalize(statement); |
| 242 | |
| 243 | return keyIdExist; |
| 244 | } |
| 245 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 246 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 247 | SecPublicInfoSqlite3::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 248 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 249 | if(keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 250 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 251 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 252 | string keyId = keyName.get(-1).toEscapedString(); |
| 253 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 254 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 255 | addIdentity(identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 256 | |
| 257 | sqlite3_stmt *statement; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 258 | sqlite3_prepare_v2(m_database, "INSERT OR REPLACE 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] | 259 | |
| 260 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 261 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 262 | sqlite3_bind_int(statement, 3, (int)keyType); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 263 | 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] | 264 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 265 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 266 | |
| 267 | sqlite3_finalize(statement); |
| 268 | } |
| 269 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 270 | shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 271 | SecPublicInfoSqlite3::getPublicKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 272 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 273 | if (keyName.empty()) |
| 274 | { |
| 275 | _LOG_DEBUG("SecPublicInfoSqlite3::getPublicKey Empty keyName"); |
| 276 | throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName"); |
| 277 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 278 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 279 | string keyId = keyName.get(-1).toEscapedString(); |
| 280 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 281 | |
| 282 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 283 | 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] | 284 | |
| 285 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 286 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 287 | |
| 288 | int res = sqlite3_step(statement); |
| 289 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 290 | shared_ptr<PublicKey> result; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 291 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 292 | { |
| 293 | result = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), sqlite3_column_bytes(statement, 0)); |
| 294 | sqlite3_finalize(statement); |
| 295 | return result; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | sqlite3_finalize(statement); |
| 300 | throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist"); |
| 301 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 305 | SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 306 | { |
| 307 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 308 | 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] | 309 | |
| 310 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 311 | |
| 312 | int res = sqlite3_step(statement); |
| 313 | |
| 314 | bool certExist = false; |
| 315 | if (res == SQLITE_ROW) { |
| 316 | int countAll = sqlite3_column_int(statement, 0); |
| 317 | if (countAll > 0) |
| 318 | certExist = true; |
| 319 | } |
| 320 | |
| 321 | sqlite3_finalize(statement); |
| 322 | |
| 323 | return certExist; |
| 324 | } |
| 325 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 326 | // void |
| 327 | // SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate) |
| 328 | // { |
| 329 | // std::string certificateName = certificate.getName().toUri(); |
| 330 | // Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 331 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 332 | // if(keyName.empty()) |
| 333 | // return; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 334 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 335 | // std::string keyId = keyName.get(-1).toEscapedString(); |
| 336 | // std::string identityName = keyName.getPrefix(-1).toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 337 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 338 | // sqlite3_stmt *statement; |
| 339 | // sqlite3_prepare_v2(m_database, |
| 340 | // "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\ |
| 341 | // values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
| 342 | // -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 343 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 344 | // _LOG_DEBUG("certName: " << certificateName); |
| 345 | // sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 346 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 347 | // try |
| 348 | // { |
| 349 | // SignatureSha256WithRsa signature(certificate.getSignature()); |
| 350 | // std::string signerName = signature.getKeyLocator().getName().toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 351 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 352 | // sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC); |
| 353 | // } |
| 354 | // catch(KeyLocator::Error& e) |
| 355 | // { |
| 356 | // _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type"); |
| 357 | // return; |
| 358 | // } |
| 359 | // catch(SignatureSha256WithRsa::Error& e) |
| 360 | // { |
| 361 | // _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type"); |
| 362 | // return; |
| 363 | // } |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 364 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 365 | // sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC); |
| 366 | // sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 367 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 368 | // // Convert from time::milliseconds to time::seconds since 1/1/1970. |
| 369 | // sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>( |
| 370 | // time::toUnixTimestamp(certificate.getNotBefore()).count())); |
| 371 | // sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>( |
| 372 | // time::toUnixTimestamp(certificate.getNotAfter()).count())); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 373 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 374 | // 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] | 375 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 376 | // sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 377 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 378 | // sqlite3_finalize(statement); |
| 379 | // } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 380 | |
| 381 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 382 | SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 383 | { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 384 | const Name& certificateName = certificate.getName(); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 385 | Name keyName = |
| 386 | IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); // KeyName is from IdentityCertificate name, so should be qualified. |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 387 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 388 | addPublicKey(keyName, KEY_TYPE_RSA, certificate.getPublicKeyInfo()); //HACK!!! Assume the key type is RSA, we should check more. |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 389 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 390 | string keyId = keyName.get(-1).toEscapedString(); |
| 391 | Name identity = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 392 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 393 | // Insert the certificate |
| 394 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 395 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 396 | "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data)\ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 397 | values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
| 398 | -1, &statement, 0); |
| 399 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 400 | _LOG_DEBUG("certName: " << certificateName.toUri()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 401 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 402 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 403 | try |
| 404 | { |
| 405 | // this will throw an exception if the signature is not the standard one or there is no key locator present |
| 406 | SignatureSha256WithRsa signature(certificate.getSignature()); |
| 407 | std::string signerName = signature.getKeyLocator().getName().toUri(); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 408 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 409 | sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC); |
| 410 | } |
| 411 | catch(KeyLocator::Error& e) |
| 412 | { |
| 413 | _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported keylocator type"); |
| 414 | return; |
| 415 | } |
| 416 | catch(SignatureSha256WithRsa::Error& e) |
| 417 | { |
| 418 | _LOG_DEBUG("SecPublicInfoSqlite3::addAnyCertificate unsupported signature type"); |
| 419 | return; |
| 420 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 421 | |
| 422 | sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 423 | sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 424 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 425 | sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>( |
| 426 | time::toUnixTimestamp(certificate.getNotBefore()).count())); |
| 427 | sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>( |
| 428 | time::toUnixTimestamp(certificate.getNotAfter()).count())); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 429 | |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 430 | 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] | 431 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 432 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 433 | |
| 434 | sqlite3_finalize(statement); |
| 435 | } |
| 436 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 437 | shared_ptr<IdentityCertificate> |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 438 | SecPublicInfoSqlite3::getCertificate(const Name &certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 439 | { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 440 | sqlite3_stmt *statement; |
| 441 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 442 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 443 | "SELECT certificate_data FROM Certificate WHERE cert_name=?", |
| 444 | -1, &statement, 0); |
| 445 | |
| 446 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 447 | |
| 448 | int res = sqlite3_step(statement); |
| 449 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 450 | if (res == SQLITE_ROW) |
| 451 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 452 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 453 | certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), |
| 454 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 455 | sqlite3_finalize(statement); |
| 456 | return certificate; |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 457 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 458 | else |
| 459 | { |
| 460 | sqlite3_finalize(statement); |
| 461 | throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist"); |
| 462 | } |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 463 | } |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 464 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 465 | |
| 466 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 467 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 468 | { |
| 469 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 470 | 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] | 471 | |
| 472 | int res = sqlite3_step(statement); |
| 473 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 474 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 475 | { |
| 476 | Name identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 477 | sqlite3_finalize(statement); |
| 478 | return identity; |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | sqlite3_finalize(statement); |
| 483 | throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity"); |
| 484 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 490 | addIdentity(identityName); |
| 491 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 492 | sqlite3_stmt *statement; |
| 493 | |
| 494 | //Reset previous default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 495 | 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] | 496 | |
| 497 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 498 | {} |
| 499 | |
| 500 | sqlite3_finalize(statement); |
| 501 | |
| 502 | //Set current default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 503 | 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] | 504 | |
| 505 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 506 | |
| 507 | sqlite3_step(statement); |
| 508 | |
| 509 | sqlite3_finalize(statement); |
| 510 | } |
| 511 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 512 | Name |
| 513 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 514 | { |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 515 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 516 | 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] | 517 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 518 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 519 | |
| 520 | int res = sqlite3_step(statement); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 521 | |
| 522 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 523 | { |
| 524 | Name keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), |
| 525 | sqlite3_column_bytes(statement, 0))); |
| 526 | sqlite3_finalize(statement); |
| 527 | return keyName; |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | sqlite3_finalize(statement); |
| 532 | throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found"); |
| 533 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void |
| 537 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 538 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 539 | if(!doesPublicKeyExist(keyName)) |
| 540 | throw Error("SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal Key does not exist:" + keyName.toUri()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 541 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 542 | string keyId = keyName.get(-1).toEscapedString(); |
| 543 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 544 | |
| 545 | sqlite3_stmt *statement; |
| 546 | |
| 547 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 548 | sqlite3_prepare_v2(m_database, "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 549 | |
| 550 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 551 | |
| 552 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 553 | {} |
| 554 | |
| 555 | sqlite3_finalize(statement); |
| 556 | |
| 557 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 558 | 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] | 559 | |
| 560 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 561 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 562 | |
| 563 | sqlite3_step(statement); |
| 564 | |
| 565 | sqlite3_finalize(statement); |
| 566 | } |
| 567 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 568 | Name |
| 569 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 570 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 571 | if(keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 572 | throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 573 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 574 | string keyId = keyName.get(-1).toEscapedString(); |
| 575 | Name identityName = keyName.getPrefix(-1); |
| 576 | |
| 577 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 578 | 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] | 579 | |
| 580 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 581 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 582 | |
| 583 | int res = sqlite3_step(statement); |
| 584 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 585 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 586 | { |
| 587 | Name certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 588 | sqlite3_finalize(statement); |
| 589 | return certName; |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | sqlite3_finalize(statement); |
| 594 | throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey certificate not found"); |
| 595 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void |
| 599 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 600 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 601 | if(!doesCertificateExist(certificateName)) |
| 602 | throw Error("SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal certificate does not exist:" + certificateName.toUri()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 603 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 604 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 605 | string keyId = keyName.get(-1).toEscapedString(); |
| 606 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 607 | |
| 608 | sqlite3_stmt *statement; |
| 609 | |
| 610 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 611 | 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] | 612 | |
| 613 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 614 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 615 | |
| 616 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 617 | {} |
| 618 | |
| 619 | sqlite3_finalize(statement); |
| 620 | |
| 621 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 622 | 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] | 623 | |
| 624 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 625 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 626 | sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
| 627 | |
| 628 | sqlite3_step(statement); |
| 629 | |
| 630 | sqlite3_finalize(statement); |
| 631 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 632 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 633 | void |
| 634 | SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 635 | { |
| 636 | sqlite3_stmt *stmt; |
| 637 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 638 | 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] | 639 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 640 | 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] | 641 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 642 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 643 | 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] | 644 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 645 | sqlite3_finalize (stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 648 | void |
| 649 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 650 | { |
| 651 | sqlite3_stmt *stmt; |
| 652 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 653 | 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] | 654 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 655 | 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] | 656 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 657 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 658 | { |
| 659 | Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 660 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 661 | nameList.push_back(keyName); |
| 662 | } |
| 663 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 666 | void |
| 667 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 668 | { |
| 669 | sqlite3_stmt *stmt; |
| 670 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 671 | 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] | 672 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 673 | 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] | 674 | |
| 675 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 676 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 677 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 678 | { |
| 679 | Name keyName(identity); |
| 680 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 681 | nameList.push_back(keyName); |
| 682 | } |
| 683 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 686 | void |
| 687 | SecPublicInfoSqlite3::getAllCertificateNames(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 688 | { |
| 689 | sqlite3_stmt *stmt; |
| 690 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 691 | 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] | 692 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 693 | 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] | 694 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 695 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 696 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 697 | |
| 698 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 701 | void |
| 702 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 703 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 704 | if(keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 705 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 706 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 707 | sqlite3_stmt *stmt; |
| 708 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 709 | 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] | 710 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 711 | 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] | 712 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 713 | Name identity = keyName.getPrefix(-1); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 714 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 715 | std::string baseKeyName = keyName.get(-1).toEscapedString(); |
| 716 | sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT); |
| 717 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 718 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 719 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 720 | |
| 721 | sqlite3_finalize (stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void |
| 725 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName) |
| 726 | { |
| 727 | if(certName.empty()) |
| 728 | return; |
| 729 | |
| 730 | sqlite3_stmt *stmt; |
| 731 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0); |
| 732 | sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT); |
| 733 | sqlite3_step(stmt); |
| 734 | sqlite3_finalize (stmt); |
| 735 | } |
| 736 | |
| 737 | void |
| 738 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName) |
| 739 | { |
| 740 | if(keyName.empty()) |
| 741 | return; |
| 742 | |
| 743 | string identity = keyName.getPrefix(-1).toUri(); |
| 744 | string keyId = keyName.get(-1).toEscapedString(); |
| 745 | |
| 746 | sqlite3_stmt *stmt; |
| 747 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 748 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 749 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 750 | sqlite3_step(stmt); |
| 751 | sqlite3_finalize (stmt); |
| 752 | |
| 753 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 754 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 755 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 756 | sqlite3_step(stmt); |
| 757 | sqlite3_finalize (stmt); |
| 758 | } |
| 759 | |
| 760 | void |
| 761 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName) |
| 762 | { |
| 763 | string identity = identityName.toUri(); |
| 764 | |
| 765 | sqlite3_stmt *stmt; |
| 766 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0); |
| 767 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 768 | sqlite3_step(stmt); |
| 769 | sqlite3_finalize (stmt); |
| 770 | |
| 771 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0); |
| 772 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 773 | sqlite3_step(stmt); |
| 774 | sqlite3_finalize (stmt); |
| 775 | |
| 776 | sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0); |
| 777 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 778 | sqlite3_step(stmt); |
| 779 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | } // namespace ndn |
| 783 | |