blob: 8026644a01df5dd91ef76de3b05c574d5fd2efe5 [file] [log] [blame]
Yingdi Yuede8eaf2013-10-14 14:07:03 -07001/* -*- 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 Yu0a6b6c52013-10-15 17:54:00 -070016#include "logging.h"
Yingdi Yuede8eaf2013-10-14 14:07:03 -070017
18using namespace std;
19using namespace ndn;
20namespace fs = boost::filesystem;
21
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070022INIT_LOGGER ("ContactStorage");
23
24const string INIT_SP_TABLE = "\
25CREATE TABLE IF NOT EXISTS \n \
26 SelfProfile( \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070027 profile_identity BLOB NOT NULL, \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070028 profile_type BLOB NOT NULL, \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070029 profile_value BLOB NOT NULL, \n \
30 \
31 PRIMARY KEY (profile_identity, profile_type) \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070032 ); \n \
33 \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070034CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070035";
36
37const string INIT_PD_TABLE = "\
38CREATE 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 \
45CREATE INDEX pd_index ON ProfileData(identity); \n \
46";
Yingdi Yuede8eaf2013-10-14 14:07:03 -070047
48const string INIT_TC_TABLE = "\
49CREATE 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 \
59CREATE INDEX tc_index ON TrustedContact(contact_namespace); \n \
60";
61
62const string INIT_NC_TABLE = "\
63CREATE 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 \
72CREATE INDEX nc_index ON NormalContact(contact_namespace); \n \
73";
74
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070075ContactStorage::ContactStorage(Ptr<security::IdentityManager> identityManager)
76 : m_identityManager(identityManager)
Yingdi Yuede8eaf2013-10-14 14:07:03 -070077{
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 Yu0a6b6c52013-10-15 17:54:00 -070085 // Check if SelfProfile table exists
Yingdi Yuede8eaf2013-10-14 14:07:03 -070086 sqlite3_stmt *stmt;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070087 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 Yuede8eaf2013-10-14 14:07:03 -0700122 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 Yuede8eaf2013-10-14 14:07:03 -0700136 }
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 Yu0a6b6c52013-10-15 17:54:00 -0700157bool
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700158ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700159{
160 bool result = false;
161
162 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700163 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700164 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700165 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700166
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 Yu49eea9f2013-10-17 15:07:17 -0700179// void
180// ContactStorage::setSelfProfileIdentity(const Name& identity)
181// {
182// string profileType("IDENTITY");
183// Blob identityBlob(identity.toUri().c_str(), identity.toUri().size());
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700184
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700185// 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 Yu0a6b6c52013-10-15 17:54:00 -0700201
202void
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700203ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700204{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700205 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700206 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700207 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700208 sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700209 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 Yu49eea9f2013-10-17 15:07:17 -0700211 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700212 }
213 else
214 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700215 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 Yu0a6b6c52013-10-15 17:54:00 -0700219 }
220 sqlite3_step (stmt);
221 sqlite3_finalize (stmt);
222}
223
224Ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700225ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700226{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700227 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700228 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700229
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700230 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 Yu0a6b6c52013-10-15 17:54:00 -0700232
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 Yuede8eaf2013-10-14 14:07:03 -0700244void
245ContactStorage::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
268void
269ContactStorage::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
290bool
291ContactStorage::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
314vector<Ptr<TrustedContact> >
315ContactStorage::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
340vector<Ptr<ContactItem> >
341ContactStorage::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 Yu0a6b6c52013-10-15 17:54:00 -0700364
365void
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700366ContactStorage::updateProfileData(const Name& identity) const
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700367{
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700368 _LOG_DEBUG("Enter updateProfileData!");
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700369 // Get current profile;
370 Ptr<Profile> newProfile = getSelfProfile(identity);
371 if(NULL == newProfile)
372 return;
373 Ptr<Blob> newProfileBlob = newProfile->toDerBlob();
374
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700375 // Check if profile exists
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700376 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700377 sqlite3_prepare_v2 (m_db, "SELECT profile_data FROM ProfileData where identity=?", -1, &stmt, 0);
378 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700379
380 if(sqlite3_step (stmt) != SQLITE_ROW)
381 {
382 sqlite3_finalize (stmt);
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700383
384 Ptr<ProfileData> newProfileData = getSignedSelfProfileData(identity, *newProfile);
385 _LOG_DEBUG("Signing DONE!");
386 if(NULL == newProfileData)
387 return;
388 Ptr<Blob> newProfileDataBlob = newProfileData->encodeToWire();
389
390 _LOG_DEBUG("Before Inserting!");
391
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700392 sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileData (identity, profile_data) values (?, ?)", -1, &stmt, 0);
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700393 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
394 sqlite3_bind_text(stmt, 2, newProfileDataBlob->buf(), newProfileDataBlob->size(), SQLITE_TRANSIENT);
395 sqlite3_step(stmt);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700396 }
397 else
398 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700399 Ptr<Blob> profileDataBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
400 Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
401 const Blob& oldProfileBlob = plainData->content();
402 sqlite3_finalize (stmt);
403
404 if(oldProfileBlob == *newProfileBlob)
405 return;
406
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700407 Ptr<ProfileData> newProfileData = getSignedSelfProfileData(identity, *newProfile);
408 _LOG_DEBUG("Signing DONE!");
409 if(NULL == newProfileData)
410 return;
411 Ptr<Blob> newProfileDataBlob = newProfileData->encodeToWire();
412
413 _LOG_DEBUG("Before Updating!");
414
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700415 sqlite3_prepare_v2 (m_db, "UPDATE ProfileData SET profile_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700416 sqlite3_bind_text(stmt, 1, newProfileDataBlob->buf(), newProfileDataBlob->size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700417 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700418 sqlite3_step(stmt);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700419 }
420}
421
422Ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700423ContactStorage::getSelfProfile(const Name& identity) const
424{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700425 Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700426 sqlite3_stmt *stmt;
427 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
428 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700429
430 while( sqlite3_step (stmt) == SQLITE_ROW)
431 {
432 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
433 Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
434 profile->setProfileEntry(profileType, profileValue);
435 }
436
437 return profile;
438}
439
440Ptr<ProfileData>
441ContactStorage::getSignedSelfProfileData(const Name& identity,
442 const Profile& profile) const
443{
444 Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(identity);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700445 if(0 == certificateName.size())
446 return NULL;
447
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700448 Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile));
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700449 _LOG_DEBUG("Get ProfileData, size: " << profileData->content().size());
450 _LOG_DEBUG("Get SigningCert, name: " << certificateName.toUri());
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700451 m_identityManager->signByCertificate(*profileData, certificateName);
452
453 return profileData;
454}
455