Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contact-storage.h" |
| 12 | #include "exception.h" |
| 13 | |
| 14 | #include <string> |
| 15 | #include <boost/filesystem.hpp> |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 16 | #include <ndn.cxx/fields/signature-sha256-with-rsa.h> |
| 17 | #include <ndn.cxx/security/exception.h> |
| 18 | #include <ndn.cxx/helpers/der/exception.h> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 19 | #include "logging.h" |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 20 | |
| 21 | using namespace std; |
| 22 | using namespace ndn; |
| 23 | namespace fs = boost::filesystem; |
| 24 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 25 | INIT_LOGGER ("ContactStorage"); |
| 26 | |
| 27 | const string INIT_SP_TABLE = "\ |
| 28 | CREATE TABLE IF NOT EXISTS \n \ |
| 29 | SelfProfile( \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 30 | profile_identity BLOB NOT NULL, \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 31 | profile_type BLOB NOT NULL, \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 32 | profile_value BLOB NOT NULL, \n \ |
| 33 | \ |
| 34 | PRIMARY KEY (profile_identity, profile_type) \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 35 | ); \n \ |
| 36 | \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 37 | CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 38 | "; |
| 39 | |
| 40 | const string INIT_PD_TABLE = "\ |
| 41 | CREATE TABLE IF NOT EXISTS \n \ |
| 42 | ProfileData( \n \ |
| 43 | identity BLOB NOT NULL UNIQUE, \n \ |
| 44 | profile_data BLOB NOT NULL, \n \ |
| 45 | \ |
| 46 | PRIMARY KEY (identity) \n \ |
| 47 | ); \n \ |
| 48 | CREATE INDEX pd_index ON ProfileData(identity); \n \ |
| 49 | "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 50 | |
| 51 | const string INIT_TC_TABLE = "\ |
| 52 | CREATE TABLE IF NOT EXISTS \n \ |
| 53 | TrustedContact( \n \ |
| 54 | contact_namespace BLOB NOT NULL, \n \ |
| 55 | contact_alias BLOB NOT NULL, \n \ |
| 56 | self_certificate BLOB NOT NULL, \n \ |
| 57 | trust_scope BLOB NOT NULL, \n \ |
| 58 | \ |
| 59 | PRIMARY KEY (contact_namespace) \n \ |
| 60 | ); \n \ |
| 61 | \ |
| 62 | CREATE INDEX tc_index ON TrustedContact(contact_namespace); \n \ |
| 63 | "; |
| 64 | |
| 65 | const string INIT_NC_TABLE = "\ |
| 66 | CREATE TABLE IF NOT EXISTS \n \ |
| 67 | NormalContact( \n \ |
| 68 | contact_namespace BLOB NOT NULL, \n \ |
| 69 | contact_alias BLOB NOT NULL, \n \ |
| 70 | self_certificate BLOB NOT NULL, \n \ |
| 71 | \ |
| 72 | PRIMARY KEY (contact_namespace) \n \ |
| 73 | ); \n \ |
| 74 | \ |
| 75 | CREATE INDEX nc_index ON NormalContact(contact_namespace); \n \ |
| 76 | "; |
| 77 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 78 | ContactStorage::ContactStorage(Ptr<security::IdentityManager> identityManager) |
| 79 | : m_identityManager(identityManager) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 80 | { |
| 81 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 82 | fs::create_directories (chronosDir); |
| 83 | |
| 84 | int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db); |
| 85 | if (res != SQLITE_OK) |
| 86 | throw LnException("Chronos DB cannot be open/created"); |
| 87 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 88 | // Check if SelfProfile table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 89 | sqlite3_stmt *stmt; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 90 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0); |
| 91 | res = sqlite3_step (stmt); |
| 92 | |
| 93 | bool spTableExist = false; |
| 94 | if (res == SQLITE_ROW) |
| 95 | spTableExist = true; |
| 96 | sqlite3_finalize (stmt); |
| 97 | |
| 98 | if(!spTableExist) |
| 99 | { |
| 100 | char *errmsg = 0; |
| 101 | res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 102 | if (res != SQLITE_OK && errmsg != 0) |
| 103 | throw LnException("Init \"error\" in SelfProfile"); |
| 104 | } |
| 105 | |
| 106 | // Check if ProfileData table exists |
| 107 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ProfileData'", -1, &stmt, 0); |
| 108 | res = sqlite3_step (stmt); |
| 109 | |
| 110 | bool pdTableExist = false; |
| 111 | if (res == SQLITE_ROW) |
| 112 | pdTableExist = true; |
| 113 | sqlite3_finalize (stmt); |
| 114 | |
| 115 | if(!pdTableExist) |
| 116 | { |
| 117 | char *errmsg = 0; |
| 118 | res = sqlite3_exec (m_db, INIT_PD_TABLE.c_str (), NULL, NULL, &errmsg); |
| 119 | if (res != SQLITE_OK && errmsg != 0) |
| 120 | throw LnException("Init \"error\" in ProfileData"); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | // Check if TrustedContact table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 125 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustedContact'", -1, &stmt, 0); |
| 126 | res = sqlite3_step (stmt); |
| 127 | |
| 128 | bool tcTableExist = false; |
| 129 | if (res == SQLITE_ROW) |
| 130 | tcTableExist = true; |
| 131 | sqlite3_finalize (stmt); |
| 132 | |
| 133 | if(!tcTableExist) |
| 134 | { |
| 135 | char *errmsg = 0; |
| 136 | res = sqlite3_exec (m_db, INIT_TC_TABLE.c_str (), NULL, NULL, &errmsg); |
| 137 | if (res != SQLITE_OK && errmsg != 0) |
| 138 | throw LnException("Init \"error\" in TrustedContact"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Check if NormalContact table exists |
| 142 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='NormalContact'", -1, &stmt, 0); |
| 143 | res = sqlite3_step (stmt); |
| 144 | |
| 145 | bool ncTableExist = false; |
| 146 | if (res == SQLITE_ROW) |
| 147 | ncTableExist = true; |
| 148 | sqlite3_finalize (stmt); |
| 149 | |
| 150 | if(!ncTableExist) |
| 151 | { |
| 152 | char *errmsg = 0; |
| 153 | res = sqlite3_exec (m_db, INIT_NC_TABLE.c_str (), NULL, NULL, &errmsg); |
| 154 | |
| 155 | if (res != SQLITE_OK && errmsg != 0) |
| 156 | throw LnException("Init \"error\" in NormalContact"); |
| 157 | } |
| 158 | } |
| 159 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 160 | bool |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 161 | ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 162 | { |
| 163 | bool result = false; |
| 164 | |
| 165 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 166 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 167 | sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 168 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 169 | |
| 170 | int res = sqlite3_step (stmt); |
| 171 | |
| 172 | if (res == SQLITE_ROW) |
| 173 | { |
| 174 | int countAll = sqlite3_column_int (stmt, 0); |
| 175 | if (countAll > 0) |
| 176 | result = true; |
| 177 | } |
| 178 | sqlite3_finalize (stmt); |
| 179 | return result; |
| 180 | } |
| 181 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 182 | // void |
| 183 | // ContactStorage::setSelfProfileIdentity(const Name& identity) |
| 184 | // { |
| 185 | // string profileType("IDENTITY"); |
| 186 | // Blob identityBlob(identity.toUri().c_str(), identity.toUri().size()); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 187 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 188 | // sqlite3_stmt *stmt; |
| 189 | // if(doesSelfEntryExist(profileType)) |
| 190 | // { |
| 191 | // sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=?", -1, &stmt, 0); |
| 192 | // sqlite3_bind_text(stmt, 1, identityBlob.buf(), identityBlob.size(), SQLITE_TRANSIENT); |
| 193 | // sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
| 194 | // } |
| 195 | // else |
| 196 | // { |
| 197 | // sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_type, profile_value) values (?, ?)", -1, &stmt, 0); |
| 198 | // sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
| 199 | // sqlite3_bind_text(stmt, 2, identityBlob.buf(), identityBlob.size(), SQLITE_TRANSIENT); |
| 200 | // } |
| 201 | // sqlite3_step (stmt); |
| 202 | // sqlite3_finalize (stmt); |
| 203 | // } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 204 | |
| 205 | void |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 206 | ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 207 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 208 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 209 | if(doesSelfEntryExist(identity, profileType)) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 210 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 211 | sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 212 | sqlite3_bind_text(stmt, 1, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
| 213 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 214 | sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 218 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0); |
| 219 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 220 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
| 221 | sqlite3_bind_text(stmt, 3, profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 222 | } |
| 223 | sqlite3_step (stmt); |
| 224 | sqlite3_finalize (stmt); |
| 225 | } |
| 226 | |
| 227 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 228 | ContactStorage::getSelfProfile(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 229 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 230 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 231 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 232 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 233 | sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=", -1, &stmt, 0); |
| 234 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 235 | |
| 236 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 237 | { |
| 238 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 239 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 240 | |
| 241 | profile->setProfileEntry(profileType, profileValue ); |
| 242 | } |
| 243 | |
| 244 | return profile; |
| 245 | } |
| 246 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 247 | void |
| 248 | ContactStorage::addTrustedContact(const TrustedContact& trustedContact) |
| 249 | { |
| 250 | if(doesTrustedContactExist(trustedContact.getNameSpace())) |
| 251 | throw LnException("Trusted Contact has already existed"); |
| 252 | |
| 253 | sqlite3_stmt *stmt; |
| 254 | sqlite3_prepare_v2 (m_db, |
| 255 | "INSERT INTO TrustedContact (contact_namespace, contact_alias, self_certificate, trust_scope) values (?, ?, ?, ?)", |
| 256 | -1, |
| 257 | &stmt, |
| 258 | 0); |
| 259 | |
| 260 | sqlite3_bind_text(stmt, 1, trustedContact.getNameSpace().toUri().c_str(), trustedContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 261 | sqlite3_bind_text(stmt, 2, trustedContact.getAlias().c_str(), trustedContact.getAlias().size(), SQLITE_TRANSIENT); |
| 262 | Ptr<Blob> selfCertificateBlob = trustedContact.getSelfEndorseCertificate().encodeToWire(); |
| 263 | sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT); |
| 264 | Ptr<Blob> trustScopeBlob = trustedContact.getTrustScopeBlob(); |
| 265 | sqlite3_bind_text(stmt, 4, trustScopeBlob->buf(), trustScopeBlob->size(),SQLITE_TRANSIENT); |
| 266 | |
| 267 | int res = sqlite3_step (stmt); |
| 268 | sqlite3_finalize (stmt); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | ContactStorage::addNormalContact(const ContactItem& normalContact) |
| 273 | { |
| 274 | if(doesNormalContactExist(normalContact.getNameSpace())) |
| 275 | throw LnException("Normal Contact has already existed"); |
| 276 | |
| 277 | sqlite3_stmt *stmt; |
| 278 | sqlite3_prepare_v2 (m_db, |
| 279 | "INSERT INTO NormalContact (contact_namespace, contact_alias, self_certificate) values (?, ?, ?)", |
| 280 | -1, |
| 281 | &stmt, |
| 282 | 0); |
| 283 | |
| 284 | sqlite3_bind_text(stmt, 1, normalContact.getNameSpace().toUri().c_str(), normalContact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 285 | sqlite3_bind_text(stmt, 2, normalContact.getAlias().c_str(), normalContact.getAlias().size(), SQLITE_TRANSIENT); |
| 286 | Ptr<Blob> selfCertificateBlob = normalContact.getSelfEndorseCertificate().encodeToWire(); |
| 287 | sqlite3_bind_text(stmt, 3, selfCertificateBlob->buf(), selfCertificateBlob->size(), SQLITE_TRANSIENT); |
| 288 | |
| 289 | int res = sqlite3_step (stmt); |
| 290 | sqlite3_finalize (stmt); |
| 291 | } |
| 292 | |
| 293 | bool |
| 294 | ContactStorage::doesContactExist(const Name& name, bool normal) |
| 295 | { |
| 296 | bool result = false; |
| 297 | |
| 298 | sqlite3_stmt *stmt; |
| 299 | if(normal) |
| 300 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM NormalContact WHERE contact_namespace=?", -1, &stmt, 0); |
| 301 | else |
| 302 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM TrustedContact WHERE contact_namespace=?", -1, &stmt, 0); |
| 303 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 304 | |
| 305 | int res = sqlite3_step (stmt); |
| 306 | |
| 307 | if (res == SQLITE_ROW) |
| 308 | { |
| 309 | int countAll = sqlite3_column_int (stmt, 0); |
| 310 | if (countAll > 0) |
| 311 | result = true; |
| 312 | } |
| 313 | sqlite3_finalize (stmt); |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | vector<Ptr<TrustedContact> > |
| 318 | ContactStorage::getAllTrustedContacts() const |
| 319 | { |
| 320 | vector<Ptr<TrustedContact> > trustedContacts; |
| 321 | |
| 322 | sqlite3_stmt *stmt; |
| 323 | sqlite3_prepare_v2 (m_db, |
| 324 | "SELECT contact_alias, self_certificate, trust_scope FROM TrustedContact", |
| 325 | -1, |
| 326 | &stmt, |
| 327 | 0); |
| 328 | |
| 329 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 330 | { |
| 331 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 332 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 333 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 334 | EndorseCertificate endorseCertificate(*certData); |
| 335 | string trustScope(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)), sqlite3_column_bytes (stmt, 2)); |
| 336 | |
| 337 | trustedContacts.push_back(Ptr<TrustedContact>(new TrustedContact(endorseCertificate, trustScope, alias))); |
| 338 | } |
| 339 | |
| 340 | return trustedContacts; |
| 341 | } |
| 342 | |
| 343 | vector<Ptr<ContactItem> > |
| 344 | ContactStorage::getAllNormalContacts() const |
| 345 | { |
| 346 | vector<Ptr<ContactItem> > normalContacts; |
| 347 | |
| 348 | sqlite3_stmt *stmt; |
| 349 | sqlite3_prepare_v2 (m_db, |
| 350 | "SELECT contact_alias, self_certificate FROM NormalContact", |
| 351 | -1, |
| 352 | &stmt, |
| 353 | 0); |
| 354 | |
| 355 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 356 | { |
| 357 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 358 | Ptr<Blob> certBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
| 359 | Ptr<Data> certData = Data::decodeFromWire(certBlob); |
| 360 | EndorseCertificate endorseCertificate(*certData); |
| 361 | |
| 362 | normalContacts.push_back(Ptr<ContactItem>(new ContactItem(endorseCertificate, alias))); |
| 363 | } |
| 364 | |
| 365 | return normalContacts; |
| 366 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 367 | |
| 368 | void |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 369 | ContactStorage::updateProfileData(const Name& identity) const |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 370 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 371 | // Get current profile; |
| 372 | Ptr<Profile> newProfile = getSelfProfile(identity); |
| 373 | if(NULL == newProfile) |
| 374 | return; |
| 375 | Ptr<Blob> newProfileBlob = newProfile->toDerBlob(); |
| 376 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 377 | // Check if profile exists |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 378 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 379 | sqlite3_prepare_v2 (m_db, "SELECT profile_data FROM ProfileData where identity=?", -1, &stmt, 0); |
| 380 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 381 | |
| 382 | if(sqlite3_step (stmt) != SQLITE_ROW) |
| 383 | { |
| 384 | sqlite3_finalize (stmt); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 385 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 386 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 387 | _LOG_DEBUG("Signing DONE!"); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 388 | if(NULL == newEndorseCertificate) |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 389 | return; |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 390 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 391 | |
| 392 | _LOG_DEBUG("Before Inserting!"); |
| 393 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 394 | sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileData (identity, profile_data) values (?, ?)", -1, &stmt, 0); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 395 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 396 | sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 397 | sqlite3_step(stmt); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 398 | } |
| 399 | else |
| 400 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 401 | Ptr<Blob> profileDataBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 402 | Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 403 | EndorseCertificate oldEndorseCertificate(*plainData); |
| 404 | // _LOG_DEBUG("Certificate converted!"); |
| 405 | const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content(); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 406 | sqlite3_finalize (stmt); |
| 407 | |
| 408 | if(oldProfileBlob == *newProfileBlob) |
| 409 | return; |
| 410 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 411 | Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 412 | _LOG_DEBUG("Signing DONE!"); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 413 | if(NULL == newEndorseCertificate) |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 414 | return; |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 415 | Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire(); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 416 | |
| 417 | _LOG_DEBUG("Before Updating!"); |
| 418 | |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 419 | sqlite3_prepare_v2 (m_db, "UPDATE ProfileData SET profile_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 420 | sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 421 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 422 | sqlite3_step(stmt); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | Ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 427 | ContactStorage::getSelfProfile(const Name& identity) const |
| 428 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 429 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identity)); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 430 | sqlite3_stmt *stmt; |
| 431 | sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 432 | sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 433 | |
| 434 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 435 | { |
| 436 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 437 | Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
| 438 | profile->setProfileEntry(profileType, profileValue); |
| 439 | } |
| 440 | |
| 441 | return profile; |
| 442 | } |
| 443 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 444 | Ptr<EndorseCertificate> |
| 445 | ContactStorage::getSignedSelfEndorseCertificate(const Name& identity, |
| 446 | const Profile& profile) const |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 447 | { |
| 448 | Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(identity); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 449 | if(0 == certificateName.size()) |
| 450 | return NULL; |
| 451 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 452 | Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile)); |
| 453 | m_identityManager->signByCertificate(*profileData, certificateName); |
| 454 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 455 | Ptr<security::IdentityCertificate> dskCert = m_identityManager->getCertificate(certificateName); |
| 456 | Ptr<const signature::Sha256WithRsa> dskCertSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(dskCert->getSignature()); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame^] | 457 | // HACK! KSK certificate should be retrieved from network. |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 458 | Ptr<security::IdentityCertificate> kskCert = m_identityManager->getCertificate(dskCertSig->getKeyLocator().getKeyName()); |
| 459 | |
| 460 | vector<string> endorseList; |
| 461 | Profile::const_iterator it = profile.begin(); |
| 462 | for(; it != profile.end(); it++) |
| 463 | endorseList.push_back(it->first); |
| 464 | |
| 465 | Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert, |
| 466 | kskCert->getNotBefore(), |
| 467 | kskCert->getNotAfter(), |
| 468 | profileData, |
| 469 | endorseList)); |
| 470 | m_identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName()); |
| 471 | |
| 472 | return selfEndorseCertificate; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 473 | } |
| 474 | |