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