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