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 | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 92 | int res = sqlite3_open((identityDir / "ndnsec-public-info.db").c_str(), &m_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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 99 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 110 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 119 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 130 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 139 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 150 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 169 | 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] | 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 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 193 | sqlite3_prepare_v2(m_database, "INSERT INTO Identity (identity_name) values (?)", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 219 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 255 | sqlite3_prepare_v2(m_database, "INSERT INTO Key (identity_name, key_identifier, key_type, public_key) values (?, ?, ?, ?)", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 279 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 305 | sqlite3_prepare_v2(m_database, "UPDATE Key SET active=? WHERE identity_name=? AND key_identifier=?", -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 320 | 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] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 351 | sqlite3_prepare_v2(m_database, |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 405 | sqlite3_prepare_v2(m_database, |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 436 | sqlite3_stmt *statement; |
| 437 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 438 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 439 | "SELECT certificate_data FROM Certificate WHERE cert_name=?", |
| 440 | -1, &statement, 0); |
| 441 | |
| 442 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 443 | |
| 444 | int res = sqlite3_step(statement); |
| 445 | |
| 446 | ptr_lib::shared_ptr<IdentityCertificate> certificate; |
| 447 | if (res == SQLITE_ROW) |
| 448 | { |
| 449 | certificate = ptr_lib::make_shared<IdentityCertificate>(); |
| 450 | certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0))); |
| 451 | } |
| 452 | sqlite3_finalize(statement); |
| 453 | |
| 454 | return certificate; |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 455 | } |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 456 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 457 | |
| 458 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 459 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 460 | { |
| 461 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 462 | 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] | 463 | |
| 464 | int res = sqlite3_step(statement); |
| 465 | |
| 466 | Name identity; |
| 467 | |
| 468 | if (res == SQLITE_ROW) |
| 469 | identity = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 470 | |
| 471 | sqlite3_finalize(statement); |
| 472 | |
| 473 | return identity; |
| 474 | } |
| 475 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 476 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 477 | SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 478 | { |
| 479 | sqlite3_stmt *statement; |
| 480 | |
| 481 | //Reset previous default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 482 | 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] | 483 | |
| 484 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 485 | {} |
| 486 | |
| 487 | sqlite3_finalize(statement); |
| 488 | |
| 489 | //Set current default identity |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 490 | 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] | 491 | |
| 492 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 493 | |
| 494 | sqlite3_step(statement); |
| 495 | |
| 496 | sqlite3_finalize(statement); |
| 497 | } |
| 498 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 499 | Name |
| 500 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 501 | { |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 502 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 503 | 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] | 504 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 505 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 506 | |
| 507 | int res = sqlite3_step(statement); |
| 508 | |
| 509 | Name keyName; |
| 510 | |
| 511 | if (res == SQLITE_ROW) |
| 512 | keyName = Name(identityName).append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 513 | |
| 514 | sqlite3_finalize(statement); |
| 515 | |
| 516 | return keyName; |
| 517 | } |
| 518 | |
| 519 | void |
| 520 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 521 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 522 | if(keyName.empty()) |
| 523 | throw Error("Incorrect key name " + keyName.toUri()); |
| 524 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 525 | string keyId = keyName.get(-1).toEscapedString(); |
| 526 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 527 | |
| 528 | sqlite3_stmt *statement; |
| 529 | |
| 530 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 531 | 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] | 532 | |
| 533 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 534 | |
| 535 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 536 | {} |
| 537 | |
| 538 | sqlite3_finalize(statement); |
| 539 | |
| 540 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 541 | 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] | 542 | |
| 543 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 544 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 545 | |
| 546 | sqlite3_step(statement); |
| 547 | |
| 548 | sqlite3_finalize(statement); |
| 549 | } |
| 550 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 551 | Name |
| 552 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 553 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 554 | if(keyName.empty()) |
| 555 | return Name(); |
| 556 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 557 | string keyId = keyName.get(-1).toEscapedString(); |
| 558 | Name identityName = keyName.getPrefix(-1); |
| 559 | |
| 560 | sqlite3_stmt *statement; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 561 | 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] | 562 | |
| 563 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 564 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 565 | |
| 566 | int res = sqlite3_step(statement); |
| 567 | |
| 568 | Name certName; |
| 569 | |
| 570 | if (res == SQLITE_ROW) |
| 571 | certName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), sqlite3_column_bytes(statement, 0))); |
| 572 | |
| 573 | sqlite3_finalize(statement); |
| 574 | |
| 575 | return certName; |
| 576 | } |
| 577 | |
| 578 | void |
| 579 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 580 | { |
| 581 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 582 | if(keyName.empty()) |
| 583 | throw Error("Incorrect key name for certificate " + certificateName.toUri()); |
| 584 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 585 | string keyId = keyName.get(-1).toEscapedString(); |
| 586 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 587 | |
| 588 | sqlite3_stmt *statement; |
| 589 | |
| 590 | //Reset previous default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 591 | 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] | 592 | |
| 593 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 594 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 595 | |
| 596 | while (sqlite3_step(statement) == SQLITE_ROW) |
| 597 | {} |
| 598 | |
| 599 | sqlite3_finalize(statement); |
| 600 | |
| 601 | //Set current default Key |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 602 | sqlite3_prepare_v2(m_database, "UPDATE Certificate SET default_cert=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] | 603 | |
| 604 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 605 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 606 | sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
| 607 | |
| 608 | sqlite3_step(statement); |
| 609 | |
| 610 | sqlite3_finalize(statement); |
| 611 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 612 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 613 | void |
| 614 | SecPublicInfoSqlite3::getAllIdentities(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 615 | { |
| 616 | sqlite3_stmt *stmt; |
| 617 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 618 | 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] | 619 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 620 | 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] | 621 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 622 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 623 | 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] | 624 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 625 | sqlite3_finalize (stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 628 | void |
| 629 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 630 | { |
| 631 | sqlite3_stmt *stmt; |
| 632 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 633 | 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] | 634 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 635 | 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] | 636 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 637 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 638 | { |
| 639 | Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 640 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 641 | nameList.push_back(keyName); |
| 642 | } |
| 643 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 646 | void |
| 647 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 648 | { |
| 649 | sqlite3_stmt *stmt; |
| 650 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 651 | 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] | 652 | else |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 653 | 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] | 654 | |
| 655 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 656 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 657 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 658 | { |
| 659 | Name keyName(identity); |
| 660 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 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::getAllCertificateNames(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 cert_name FROM Certificate WHERE default_cert=1", -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 cert_name FROM Certificate WHERE default_cert=0", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 674 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 675 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 676 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 677 | |
| 678 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 681 | void |
| 682 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, vector<Name> &nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 683 | { |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 684 | if(keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 685 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 686 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 687 | sqlite3_stmt *stmt; |
| 688 | if(isDefault) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 689 | 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] | 690 | else |
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=0 and identity_name=? and key_identifier=?", -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 692 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 693 | Name identity = keyName.getPrefix(-1); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 694 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 695 | std::string baseKeyName = keyName.get(-1).toEscapedString(); |
| 696 | sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT); |
| 697 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 698 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 699 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 700 | |
| 701 | sqlite3_finalize (stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame^] | 702 | } |
| 703 | |
| 704 | void |
| 705 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name &certName) |
| 706 | { |
| 707 | if(certName.empty()) |
| 708 | return; |
| 709 | |
| 710 | sqlite3_stmt *stmt; |
| 711 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0); |
| 712 | sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size (), SQLITE_TRANSIENT); |
| 713 | sqlite3_step(stmt); |
| 714 | sqlite3_finalize (stmt); |
| 715 | } |
| 716 | |
| 717 | void |
| 718 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name &keyName) |
| 719 | { |
| 720 | if(keyName.empty()) |
| 721 | return; |
| 722 | |
| 723 | string identity = keyName.getPrefix(-1).toUri(); |
| 724 | string keyId = keyName.get(-1).toEscapedString(); |
| 725 | |
| 726 | sqlite3_stmt *stmt; |
| 727 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 728 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 729 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 730 | sqlite3_step(stmt); |
| 731 | sqlite3_finalize (stmt); |
| 732 | |
| 733 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=? and key_identifier=?", -1, &stmt, 0); |
| 734 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 735 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 736 | sqlite3_step(stmt); |
| 737 | sqlite3_finalize (stmt); |
| 738 | } |
| 739 | |
| 740 | void |
| 741 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name &identityName) |
| 742 | { |
| 743 | string identity = identityName.toUri(); |
| 744 | |
| 745 | sqlite3_stmt *stmt; |
| 746 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0); |
| 747 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 748 | sqlite3_step(stmt); |
| 749 | sqlite3_finalize (stmt); |
| 750 | |
| 751 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0); |
| 752 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 753 | sqlite3_step(stmt); |
| 754 | sqlite3_finalize (stmt); |
| 755 | |
| 756 | sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0); |
| 757 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 758 | sqlite3_step(stmt); |
| 759 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | } // namespace ndn |
| 763 | |