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