Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | #include "common.hpp" |
| 26 | |
| 27 | #include "sec-public-info-sqlite3.hpp" |
| 28 | #include "identity-certificate.hpp" |
| 29 | #include "signature-sha256-with-rsa.hpp" |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 30 | #include "signature-sha256-with-ecdsa.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 31 | #include "../data.hpp" |
| 32 | |
Yingdi Yu | 874678f | 2014-01-22 19:30:34 -0800 | [diff] [blame] | 33 | #include <sqlite3.h> |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <sstream> |
| 37 | #include <fstream> |
Yingdi Yu | 0b9c315 | 2014-01-26 16:31:18 -0800 | [diff] [blame] | 38 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 39 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 40 | namespace ndn { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 45 | static const string INIT_ID_TABLE = "\ |
| 46 | CREATE TABLE IF NOT EXISTS \n \ |
| 47 | Identity( \n \ |
| 48 | identity_name BLOB NOT NULL, \n \ |
| 49 | default_identity INTEGER DEFAULT 0, \n \ |
| 50 | \ |
| 51 | PRIMARY KEY (identity_name) \n \ |
| 52 | ); \n \ |
| 53 | \ |
| 54 | CREATE INDEX identity_index ON Identity(identity_name); \n \ |
| 55 | "; |
| 56 | |
| 57 | static const string INIT_KEY_TABLE = "\ |
| 58 | CREATE TABLE IF NOT EXISTS \n \ |
| 59 | Key( \n \ |
| 60 | identity_name BLOB NOT NULL, \n \ |
| 61 | key_identifier BLOB NOT NULL, \n \ |
| 62 | key_type INTEGER, \n \ |
| 63 | public_key BLOB, \n \ |
| 64 | default_key INTEGER DEFAULT 0, \n \ |
| 65 | active INTEGER DEFAULT 0, \n \ |
| 66 | \ |
| 67 | PRIMARY KEY (identity_name, key_identifier) \n \ |
| 68 | ); \n \ |
| 69 | \ |
| 70 | CREATE INDEX key_index ON Key(identity_name); \n \ |
| 71 | "; |
| 72 | |
| 73 | static const string INIT_CERT_TABLE = "\ |
| 74 | CREATE TABLE IF NOT EXISTS \n \ |
| 75 | Certificate( \n \ |
| 76 | cert_name BLOB NOT NULL, \n \ |
| 77 | cert_issuer BLOB NOT NULL, \n \ |
| 78 | identity_name BLOB NOT NULL, \n \ |
| 79 | key_identifier BLOB NOT NULL, \n \ |
| 80 | not_before TIMESTAMP, \n \ |
| 81 | not_after TIMESTAMP, \n \ |
| 82 | certificate_data BLOB NOT NULL, \n \ |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 83 | valid_flag INTEGER DEFAULT 1, \n \ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 84 | default_cert INTEGER DEFAULT 0, \n \ |
| 85 | \ |
| 86 | PRIMARY KEY (cert_name) \n \ |
| 87 | ); \n \ |
| 88 | \ |
| 89 | CREATE INDEX cert_index ON Certificate(cert_name); \n \ |
| 90 | CREATE INDEX subject ON Certificate(identity_name); \n \ |
| 91 | "; |
| 92 | |
| 93 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 94 | * A utility function to call the normal sqlite3_bind_text where the value and length are |
| 95 | * value.c_str() and value.size(). |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 96 | */ |
Alexander Afanasyev | db4affc | 2014-07-15 13:37:16 -0700 | [diff] [blame^] | 97 | static int |
| 98 | sqlite3_bind_text(sqlite3_stmt* statement, |
| 99 | int index, |
| 100 | const string& value, |
| 101 | void(*destructor)(void*)) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 102 | { |
| 103 | return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor); |
| 104 | } |
| 105 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 106 | SecPublicInfoSqlite3::SecPublicInfoSqlite3() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 107 | { |
Yingdi Yu | 37e317f | 2014-03-19 12:16:23 -0700 | [diff] [blame] | 108 | boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn"; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 109 | boost::filesystem::create_directories(identityDir); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 5b60f70 | 2014-02-07 12:55:24 -0800 | [diff] [blame] | 111 | /// @todo Add define for windows/unix in wscript. The following may completely fail on windows |
| 112 | int res = sqlite3_open_v2((identityDir / "ndnsec-public-info.db").c_str(), &m_database, |
| 113 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 114 | #ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING |
Alexander Afanasyev | 4447146 | 2014-02-12 11:21:51 -0800 | [diff] [blame] | 115 | "unix-dotfile" |
Alexander Afanasyev | 5b60f70 | 2014-02-07 12:55:24 -0800 | [diff] [blame] | 116 | #else |
| 117 | 0 |
| 118 | #endif |
| 119 | ); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 120 | if (res != SQLITE_OK) |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 121 | throw Error("identity DB cannot be opened/created"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 122 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 123 | //Check if Key table exists; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 125 | sqlite3_prepare_v2(m_database, |
| 126 | "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'", |
| 127 | -1, &statement, 0); |
Jeff Thompson | 351ac30 | 2013-10-19 18:45:00 -0700 | [diff] [blame] | 128 | res = sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 129 | |
| 130 | bool idTableExists = false; |
| 131 | if (res == SQLITE_ROW) |
| 132 | idTableExists = true; |
| 133 | |
| 134 | sqlite3_finalize(statement); |
| 135 | |
| 136 | if (!idTableExists) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | char* errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 138 | res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 139 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 140 | if (res != SQLITE_OK && errorMessage != 0) { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 141 | sqlite3_free(errorMessage); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //Check if Key table exists; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 146 | sqlite3_prepare_v2(m_database, |
| 147 | "SELECT name FROM sqlite_master WHERE type='table' And name='Key'", |
| 148 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 149 | res = sqlite3_step(statement); |
| 150 | |
| 151 | bool keyTableExists = false; |
| 152 | if (res == SQLITE_ROW) |
| 153 | keyTableExists = true; |
| 154 | |
| 155 | sqlite3_finalize(statement); |
| 156 | |
| 157 | if (!keyTableExists) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 158 | char* errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 159 | res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 160 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 161 | if (res != SQLITE_OK && errorMessage != 0) { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 162 | sqlite3_free(errorMessage); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | //Check if Certificate table exists; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 167 | sqlite3_prepare_v2(m_database, |
| 168 | "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'", |
| 169 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 170 | res = sqlite3_step(statement); |
| 171 | |
| 172 | bool idCertificateTableExists = false; |
| 173 | if (res == SQLITE_ROW) |
| 174 | idCertificateTableExists = true; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 175 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 176 | sqlite3_finalize(statement); |
| 177 | |
| 178 | if (!idCertificateTableExists) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 179 | char* errorMessage = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 180 | res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 182 | if (res != SQLITE_OK && errorMessage != 0) { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 183 | sqlite3_free(errorMessage); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 188 | SecPublicInfoSqlite3::~SecPublicInfoSqlite3() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 189 | { |
| 190 | } |
| 191 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 192 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 193 | SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 194 | { |
| 195 | bool result = false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 196 | |
| 197 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 198 | sqlite3_prepare_v2(m_database, |
| 199 | "SELECT count(*) FROM Identity WHERE identity_name=?", |
| 200 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 201 | |
| 202 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 203 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 204 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 205 | if (res == SQLITE_ROW) { |
| 206 | int countAll = sqlite3_column_int(statement, 0); |
| 207 | if (countAll > 0) |
| 208 | result = true; |
| 209 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 210 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 211 | sqlite3_finalize(statement); |
| 212 | |
| 213 | return result; |
| 214 | } |
| 215 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 216 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 217 | SecPublicInfoSqlite3::addIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 218 | { |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 219 | if (doesIdentityExist(identityName)) |
| 220 | return; |
| 221 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 222 | sqlite3_stmt* statement; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 223 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 224 | sqlite3_prepare_v2(m_database, |
| 225 | "INSERT OR REPLACE INTO Identity (identity_name) values (?)", |
| 226 | -1, &statement, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 227 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 228 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 230 | sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 231 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 232 | sqlite3_finalize(statement); |
| 233 | } |
| 234 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 235 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 236 | SecPublicInfoSqlite3::revokeIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 237 | { |
| 238 | //TODO: |
| 239 | return false; |
| 240 | } |
| 241 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 242 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 243 | SecPublicInfoSqlite3::doesPublicKeyExist(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 244 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 245 | if (keyName.empty()) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 246 | throw Error("Incorrect key name " + keyName.toUri()); |
| 247 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 248 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 249 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 250 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 251 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 252 | sqlite3_prepare_v2(m_database, |
| 253 | "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?", |
| 254 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 255 | |
| 256 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 257 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 258 | |
| 259 | int res = sqlite3_step(statement); |
| 260 | |
| 261 | bool keyIdExist = false; |
| 262 | if (res == SQLITE_ROW) { |
| 263 | int countAll = sqlite3_column_int(statement, 0); |
| 264 | if (countAll > 0) |
| 265 | keyIdExist = true; |
| 266 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 267 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 268 | sqlite3_finalize(statement); |
| 269 | |
| 270 | return keyIdExist; |
| 271 | } |
| 272 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 273 | void |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 274 | SecPublicInfoSqlite3::addKey(const Name& keyName, |
| 275 | const PublicKey& publicKeyDer) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 276 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 277 | if (keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 278 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 279 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 280 | if (doesPublicKeyExist(keyName)) |
| 281 | return; |
| 282 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 283 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 284 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 285 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 286 | addIdentity(identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 287 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 288 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 289 | sqlite3_prepare_v2(m_database, |
| 290 | "INSERT OR REPLACE INTO Key \ |
| 291 | (identity_name, key_identifier, key_type, public_key) \ |
| 292 | values (?, ?, ?, ?)", |
| 293 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 294 | |
| 295 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 296 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 297 | sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType()); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 298 | sqlite3_bind_blob(statement, 4, |
| 299 | publicKeyDer.get().buf(), |
| 300 | publicKeyDer.get().size(), |
| 301 | SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 302 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 303 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 304 | |
| 305 | sqlite3_finalize(statement); |
| 306 | } |
| 307 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 308 | shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 309 | SecPublicInfoSqlite3::getPublicKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 310 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 311 | if (keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 312 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 313 | throw Error("SecPublicInfoSqlite3::getPublicKey Empty keyName"); |
| 314 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 315 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 316 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 317 | Name identityName = keyName.getPrefix(-1); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 318 | |
| 319 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 320 | sqlite3_prepare_v2(m_database, |
| 321 | "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?", |
| 322 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 323 | |
| 324 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 325 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 326 | |
| 327 | int res = sqlite3_step(statement); |
| 328 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 329 | shared_ptr<PublicKey> result; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 330 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 331 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 332 | result = |
| 333 | make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), |
| 334 | sqlite3_column_bytes(statement, 0)); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 335 | sqlite3_finalize(statement); |
| 336 | return result; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | sqlite3_finalize(statement); |
| 341 | throw Error("SecPublicInfoSqlite3::getPublicKey public key does not exist"); |
| 342 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 345 | KeyType |
| 346 | SecPublicInfoSqlite3::getPublicKeyType(const Name& keyName) |
| 347 | { |
| 348 | if (keyName.empty()) |
| 349 | return KEY_TYPE_NULL; |
| 350 | |
| 351 | string keyId = keyName.get(-1).toUri(); |
| 352 | Name identityName = keyName.getPrefix(-1); |
| 353 | |
| 354 | sqlite3_stmt* statement; |
| 355 | sqlite3_prepare_v2(m_database, |
| 356 | "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?", |
| 357 | -1, &statement, 0); |
| 358 | |
| 359 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 360 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 361 | |
| 362 | int res = sqlite3_step(statement); |
| 363 | |
| 364 | if (res == SQLITE_ROW) |
| 365 | { |
| 366 | int typeValue = sqlite3_column_int(statement, 0); |
| 367 | sqlite3_finalize(statement); |
| 368 | return static_cast<KeyType>(typeValue); |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | sqlite3_finalize(statement); |
| 373 | return KEY_TYPE_NULL; |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 378 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 379 | SecPublicInfoSqlite3::doesCertificateExist(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 380 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 381 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 382 | sqlite3_prepare_v2(m_database, |
| 383 | "SELECT count(*) FROM Certificate WHERE cert_name=?", |
| 384 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 385 | |
| 386 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 387 | |
| 388 | int res = sqlite3_step(statement); |
| 389 | |
| 390 | bool certExist = false; |
| 391 | if (res == SQLITE_ROW) { |
| 392 | int countAll = sqlite3_column_int(statement, 0); |
| 393 | if (countAll > 0) |
| 394 | certExist = true; |
| 395 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 396 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 397 | sqlite3_finalize(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 398 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 399 | return certExist; |
| 400 | } |
| 401 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 402 | // void |
| 403 | // SecPublicInfoSqlite3::addAnyCertificate(const IdentityCertificate& certificate) |
| 404 | // { |
| 405 | // std::string certificateName = certificate.getName().toUri(); |
| 406 | // Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 407 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 408 | // if (keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 409 | // return; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 410 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 411 | // std::string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 412 | // std::string identityName = keyName.getPrefix(-1).toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 413 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 414 | // sqlite3_stmt* statement; |
| 415 | // sqlite3_prepare_v2(m_database, |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 416 | // "INSERT OR REPLACE INTO Certificate (cert_name, cert_issuer, identity_name, key_identifier, not_before, not_after, certificate_data) " |
| 417 | // "VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 418 | // -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 419 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 420 | // sqlite3_bind_text(statement, 1, certificateName, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 421 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 422 | // try |
| 423 | // { |
| 424 | // SignatureSha256WithRsa signature(certificate.getSignature()); |
| 425 | // std::string signerName = signature.getKeyLocator().getName().toUri(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 426 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 427 | // sqlite3_bind_text(statement, 2, signerName, SQLITE_STATIC); |
| 428 | // } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 429 | // catch (KeyLocator::Error& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 430 | // { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 431 | // return; |
| 432 | // } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 433 | // catch (SignatureSha256WithRsa::Error& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 434 | // { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 435 | // return; |
| 436 | // } |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 437 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 438 | // sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC); |
| 439 | // sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 440 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 441 | // // Convert from time::milliseconds to time::seconds since 1/1/1970. |
| 442 | // sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>( |
| 443 | // time::toUnixTimestamp(certificate.getNotBefore()).count())); |
| 444 | // sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>( |
| 445 | // time::toUnixTimestamp(certificate.getNotAfter()).count())); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 446 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 447 | // 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] | 448 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 449 | // sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 450 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 451 | // sqlite3_finalize(statement); |
| 452 | // } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 453 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 454 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 455 | SecPublicInfoSqlite3::addCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 456 | { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 457 | const Name& certificateName = certificate.getName(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 458 | // KeyName is from IdentityCertificate name, so should be qualified. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 459 | Name keyName = |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 460 | IdentityCertificate::certificateNameToPublicKeyName(certificate.getName()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 461 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 462 | addKey(keyName, certificate.getPublicKeyInfo()); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 463 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 464 | if (doesCertificateExist(certificateName)) |
| 465 | return; |
| 466 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 467 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 468 | Name identity = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 469 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 470 | // Insert the certificate |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 471 | sqlite3_stmt* statement; |
| 472 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 473 | "INSERT OR REPLACE INTO Certificate \ |
| 474 | (cert_name, cert_issuer, identity_name, key_identifier, \ |
| 475 | not_before, not_after, certificate_data) \ |
| 476 | values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 477 | -1, &statement, 0); |
| 478 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 479 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
| 480 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 481 | try |
| 482 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 483 | // this will throw an exception if the signature is not the standard one |
| 484 | // or there is no key locator present |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 485 | std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri(); |
Alexander Afanasyev | db4affc | 2014-07-15 13:37:16 -0700 | [diff] [blame^] | 486 | sqlite3_bind_text(statement, 2, signerName, SQLITE_TRANSIENT); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 487 | } |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 488 | catch (Tlv::Error& e) |
| 489 | { |
| 490 | return; |
| 491 | } |
| 492 | catch (KeyLocator::Error& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 493 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 494 | return; |
| 495 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 496 | |
| 497 | sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fab95ed | 2014-01-05 23:26:30 -0800 | [diff] [blame] | 498 | sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 499 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 500 | sqlite3_bind_int64(statement, 5, |
| 501 | static_cast<sqlite3_int64>( |
| 502 | time::toUnixTimestamp(certificate.getNotBefore()).count())); |
| 503 | sqlite3_bind_int64(statement, 6, |
| 504 | static_cast<sqlite3_int64>( |
| 505 | time::toUnixTimestamp(certificate.getNotAfter()).count())); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 506 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 507 | sqlite3_bind_blob(statement, 7, |
| 508 | certificate.wireEncode().wire(), |
| 509 | certificate.wireEncode().size(), |
| 510 | SQLITE_TRANSIENT); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 511 | |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 512 | sqlite3_step(statement); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 513 | |
| 514 | sqlite3_finalize(statement); |
| 515 | } |
| 516 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 517 | shared_ptr<IdentityCertificate> |
| 518 | SecPublicInfoSqlite3::getCertificate(const Name& certificateName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 519 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 520 | sqlite3_stmt* statement; |
| 521 | |
| 522 | sqlite3_prepare_v2(m_database, |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 523 | "SELECT certificate_data FROM Certificate WHERE cert_name=?", |
| 524 | -1, &statement, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 525 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 526 | sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 527 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 528 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 529 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 530 | if (res == SQLITE_ROW) |
| 531 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 532 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 533 | certificate->wireDecode(Block(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)), |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 534 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 535 | sqlite3_finalize(statement); |
| 536 | return certificate; |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 537 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 538 | else |
| 539 | { |
| 540 | sqlite3_finalize(statement); |
| 541 | throw Error("SecPublicInfoSqlite3::getCertificate certificate does not exist"); |
| 542 | } |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 543 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 544 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 545 | |
| 546 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 547 | SecPublicInfoSqlite3::getDefaultIdentity() |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 548 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 549 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 550 | sqlite3_prepare_v2(m_database, |
| 551 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 552 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 553 | |
| 554 | int res = sqlite3_step(statement); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 555 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 556 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 557 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 558 | Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), |
| 559 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 560 | sqlite3_finalize(statement); |
| 561 | return identity; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | sqlite3_finalize(statement); |
| 566 | throw Error("SecPublicInfoSqlite3::getDefaultIdentity no default identity"); |
| 567 | } |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 570 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 571 | SecPublicInfoSqlite3::setDefaultIdentityInternal(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 572 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 573 | addIdentity(identityName); |
| 574 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 575 | sqlite3_stmt* statement; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 576 | |
| 577 | //Reset previous default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 578 | sqlite3_prepare_v2(m_database, |
| 579 | "UPDATE Identity SET default_identity=0 WHERE default_identity=1", |
| 580 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 581 | |
| 582 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 583 | { |
| 584 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 585 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 586 | sqlite3_finalize(statement); |
| 587 | |
| 588 | //Set current default identity |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 589 | sqlite3_prepare_v2(m_database, |
| 590 | "UPDATE Identity SET default_identity=1 WHERE identity_name=?", |
| 591 | -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); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 594 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 595 | sqlite3_step(statement); |
| 596 | |
| 597 | sqlite3_finalize(statement); |
| 598 | } |
| 599 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 600 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 601 | SecPublicInfoSqlite3::getDefaultKeyNameForIdentity(const Name& identityName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 602 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 603 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 604 | sqlite3_prepare_v2(m_database, |
| 605 | "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1", |
| 606 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 607 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 608 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 609 | |
| 610 | int res = sqlite3_step(statement); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 611 | |
| 612 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 613 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 614 | Name keyName = identityName; |
| 615 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), |
| 616 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 617 | sqlite3_finalize(statement); |
| 618 | return keyName; |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | sqlite3_finalize(statement); |
| 623 | throw Error("SecPublicInfoSqlite3::getDefaultKeyNameForIdentity key not found"); |
| 624 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 627 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 628 | SecPublicInfoSqlite3::setDefaultKeyNameForIdentityInternal(const Name& keyName) |
| 629 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 630 | if (!doesPublicKeyExist(keyName)) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 631 | throw Error("Key does not exist:" + keyName.toUri()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 632 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 633 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 634 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 635 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 636 | sqlite3_stmt* statement; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 637 | |
| 638 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 639 | sqlite3_prepare_v2(m_database, |
| 640 | "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?", |
| 641 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 642 | |
| 643 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 644 | |
| 645 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 646 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 647 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 648 | sqlite3_finalize(statement); |
| 649 | |
| 650 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 651 | sqlite3_prepare_v2(m_database, |
| 652 | "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?", |
| 653 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 654 | |
| 655 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 656 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 657 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 658 | sqlite3_step(statement); |
| 659 | |
| 660 | sqlite3_finalize(statement); |
| 661 | } |
| 662 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 663 | Name |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 664 | SecPublicInfoSqlite3::getDefaultCertificateNameForKey(const Name& keyName) |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 665 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 666 | if (keyName.empty()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 667 | throw Error("SecPublicInfoSqlite3::getDefaultCertificateNameForKey wrong key"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 668 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 669 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 670 | Name identityName = keyName.getPrefix(-1); |
| 671 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 672 | sqlite3_stmt* statement; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 673 | sqlite3_prepare_v2(m_database, |
| 674 | "SELECT cert_name FROM Certificate \ |
| 675 | WHERE identity_name=? AND key_identifier=? AND default_cert=1", |
| 676 | -1, &statement, 0); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 677 | |
| 678 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 679 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 680 | |
| 681 | int res = sqlite3_step(statement); |
| 682 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 683 | if (res == SQLITE_ROW) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 684 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 685 | Name certName(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)), |
| 686 | sqlite3_column_bytes(statement, 0))); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 687 | sqlite3_finalize(statement); |
| 688 | return certName; |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | sqlite3_finalize(statement); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 693 | throw Error("certificate not found"); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 694 | } |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 697 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 698 | SecPublicInfoSqlite3::setDefaultCertificateNameForKeyInternal(const Name& certificateName) |
| 699 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 700 | if (!doesCertificateExist(certificateName)) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 701 | throw Error("certificate does not exist:" + certificateName.toUri()); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 702 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 703 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 704 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 705 | Name identityName = keyName.getPrefix(-1); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 706 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 707 | sqlite3_stmt* statement; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 708 | |
| 709 | //Reset previous default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 710 | sqlite3_prepare_v2(m_database, |
| 711 | "UPDATE Certificate SET default_cert=0 \ |
| 712 | WHERE default_cert=1 AND identity_name=? AND key_identifier=?", |
| 713 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 714 | |
| 715 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 716 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 717 | |
| 718 | while (sqlite3_step(statement) == SQLITE_ROW) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 719 | ; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 720 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 721 | sqlite3_finalize(statement); |
| 722 | |
| 723 | //Set current default Key |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 724 | sqlite3_prepare_v2(m_database, |
| 725 | "UPDATE Certificate SET default_cert=1 \ |
| 726 | WHERE identity_name=? AND key_identifier=? AND cert_name=?", |
| 727 | -1, &statement, 0); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 728 | |
| 729 | sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT); |
| 730 | sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT); |
| 731 | sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 732 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 733 | sqlite3_step(statement); |
| 734 | |
| 735 | sqlite3_finalize(statement); |
| 736 | } |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 737 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 738 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 739 | SecPublicInfoSqlite3::getAllIdentities(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 740 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 741 | sqlite3_stmt* stmt; |
| 742 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 743 | sqlite3_prepare_v2(m_database, |
| 744 | "SELECT identity_name FROM Identity WHERE default_identity=1", |
| 745 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 746 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 747 | sqlite3_prepare_v2(m_database, |
| 748 | "SELECT identity_name FROM Identity WHERE default_identity=0", |
| 749 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 750 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 751 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 752 | nameList.push_back(Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), |
| 753 | sqlite3_column_bytes(stmt, 0)))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 754 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 755 | sqlite3_finalize(stmt); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 758 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 759 | SecPublicInfoSqlite3::getAllKeyNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 760 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 761 | sqlite3_stmt* stmt; |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 762 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 763 | if (isDefault) |
| 764 | sqlite3_prepare_v2(m_database, |
| 765 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=1", |
| 766 | -1, &stmt, 0); |
| 767 | else |
| 768 | sqlite3_prepare_v2(m_database, |
| 769 | "SELECT identity_name, key_identifier FROM Key WHERE default_key=0", |
| 770 | -1, &stmt, 0); |
| 771 | |
| 772 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 773 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 774 | Name keyName(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), |
| 775 | sqlite3_column_bytes(stmt, 0))); |
| 776 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), |
| 777 | sqlite3_column_bytes(stmt, 1))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 778 | nameList.push_back(keyName); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 779 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 780 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 783 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 784 | SecPublicInfoSqlite3::getAllKeyNamesOfIdentity(const Name& identity, |
| 785 | vector<Name>& nameList, |
| 786 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 787 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 788 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 789 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 790 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 791 | sqlite3_prepare_v2(m_database, |
| 792 | "SELECT key_identifier FROM Key WHERE default_key=1 and identity_name=?", |
| 793 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 794 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 795 | sqlite3_prepare_v2(m_database, |
| 796 | "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?", |
| 797 | -1, &stmt, 0); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 798 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 799 | sqlite3_bind_text(stmt, 1, |
| 800 | identity.toUri().c_str(), |
| 801 | identity.toUri().size(), |
| 802 | SQLITE_TRANSIENT); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 803 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 804 | while (sqlite3_step(stmt) == SQLITE_ROW) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 805 | { |
| 806 | Name keyName(identity); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 807 | keyName.append(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), |
| 808 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 809 | nameList.push_back(keyName); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 810 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 811 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 812 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 813 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 814 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 815 | SecPublicInfoSqlite3::getAllCertificateNames(vector<Name>& nameList, bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 816 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 817 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 818 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 819 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 820 | sqlite3_prepare_v2(m_database, |
| 821 | "SELECT cert_name FROM Certificate WHERE default_cert=1", |
| 822 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 823 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 824 | sqlite3_prepare_v2(m_database, |
| 825 | "SELECT cert_name FROM Certificate WHERE default_cert=0", |
| 826 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 827 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 828 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 829 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), |
| 830 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 831 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 832 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 835 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 836 | SecPublicInfoSqlite3::getAllCertificateNamesOfKey(const Name& keyName, |
| 837 | vector<Name>& nameList, |
| 838 | bool isDefault) |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 839 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 840 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 841 | return; |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 842 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 843 | sqlite3_stmt* stmt; |
| 844 | if (isDefault) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 845 | sqlite3_prepare_v2(m_database, |
| 846 | "SELECT cert_name FROM Certificate \ |
| 847 | WHERE default_cert=1 and identity_name=? and key_identifier=?", |
| 848 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 849 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 850 | sqlite3_prepare_v2(m_database, |
| 851 | "SELECT cert_name FROM Certificate \ |
| 852 | WHERE default_cert=0 and identity_name=? and key_identifier=?", |
| 853 | -1, &stmt, 0); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 854 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 855 | Name identity = keyName.getPrefix(-1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 856 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 857 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 858 | std::string baseKeyName = keyName.get(-1).toUri(); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 859 | sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT); |
| 860 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 861 | while (sqlite3_step(stmt) == SQLITE_ROW) |
| 862 | nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), |
| 863 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 864 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 865 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 869 | SecPublicInfoSqlite3::deleteCertificateInfo(const Name& certName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 870 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 871 | if (certName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 872 | return; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 873 | |
| 874 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 875 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 876 | sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 877 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 878 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 882 | SecPublicInfoSqlite3::deletePublicKeyInfo(const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 883 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 884 | if (keyName.empty()) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 885 | return; |
| 886 | |
| 887 | string identity = keyName.getPrefix(-1).toUri(); |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 888 | string keyId = keyName.get(-1).toUri(); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 889 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 890 | sqlite3_stmt* stmt; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 891 | sqlite3_prepare_v2(m_database, |
| 892 | "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?", |
| 893 | -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 894 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 895 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 896 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 897 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 898 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 899 | sqlite3_prepare_v2(m_database, |
| 900 | "DELETE FROM Key WHERE identity_name=? and key_identifier=?", |
| 901 | -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 902 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 903 | sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT); |
| 904 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 905 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 909 | SecPublicInfoSqlite3::deleteIdentityInfo(const Name& identityName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 910 | { |
| 911 | string identity = identityName.toUri(); |
| 912 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 913 | sqlite3_stmt* stmt; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 914 | sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 915 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 916 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 917 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 918 | |
| 919 | sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 920 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 921 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 922 | sqlite3_finalize(stmt); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 923 | |
| 924 | sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 925 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 926 | sqlite3_step(stmt); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 927 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | } // namespace ndn |