blob: 52f5483f0018ad2e7abd8bf6df81c0e039a7421b [file] [log] [blame]
Yingdi Yud59b5932013-10-16 13:16:49 -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
Yingdi Yua4898752013-10-15 20:31:37 -070011#include "addcontactpanel.h"
12#include "ui_addcontactpanel.h"
Yingdi Yueb98f7d2013-11-10 01:34:57 -080013#include <QMessageBox>
Yingdi Yua4898752013-10-15 20:31:37 -070014
Yingdi Yuc29fb982013-10-20 19:43:10 -070015#ifndef Q_MOC_RUN
Yingdi Yu6df61252014-01-21 11:05:11 -080016#include <ndn-cpp-dev/security/verifier.hpp>
Yingdi Yu64206112013-12-24 11:16:32 +080017#include <boost/iostreams/stream.hpp>
Yingdi Yu64206112013-12-24 11:16:32 +080018#include "endorse-collection.pb.h"
Yingdi Yuc29fb982013-10-20 19:43:10 -070019#include "logging.h"
20#endif
21
Yingdi Yuaa8d7692013-10-18 17:05:02 -070022using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080023using namespace ndn::ptr_lib;
Yingdi Yuaa8d7692013-10-18 17:05:02 -070024using namespace std;
25
Yingdi Yuc29fb982013-10-20 19:43:10 -070026INIT_LOGGER("AddContactPanel");
27
Yingdi Yu64206112013-12-24 11:16:32 +080028AddContactPanel::AddContactPanel(shared_ptr<ContactManager> contactManager,
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070029 QWidget *parent)
Yingdi Yud59b5932013-10-16 13:16:49 -070030 : QDialog(parent)
31 , ui(new Ui::AddContactPanel)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070032 , m_contactManager(contactManager)
Yingdi Yuc29fb982013-10-20 19:43:10 -070033 , m_warningDialog(new WarningDialog())
Yingdi Yua4898752013-10-15 20:31:37 -070034{
Yingdi Yud59b5932013-10-16 13:16:49 -070035 ui->setupUi(this);
Yingdi Yuc29fb982013-10-20 19:43:10 -070036
37 qRegisterMetaType<ndn::Name>("NdnName");
38 qRegisterMetaType<EndorseCertificate>("EndorseCertificate");
Yingdi Yu8dacdf22013-11-05 23:06:43 -080039 qRegisterMetaType<ndn::Data>("NdnData");
Yingdi Yud59b5932013-10-16 13:16:49 -070040
41 connect(ui->cancelButton, SIGNAL(clicked()),
42 this, SLOT(onCancelClicked()));
43 connect(ui->searchButton, SIGNAL(clicked()),
44 this, SLOT(onSearchClicked()));
Yingdi Yu79c25a22013-10-21 13:38:38 -070045 connect(ui->addButton, SIGNAL(clicked()),
46 this, SLOT(onAddClicked()));
47 connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)),
48 this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&)));
Yingdi Yuc29fb982013-10-20 19:43:10 -070049 connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)),
50 this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -080051 connect(&*m_contactManager, SIGNAL(collectEndorseFetched(const ndn::Data&)),
52 this, SLOT(onCollectEndorseFetched(const ndn::Data&)));
53 connect(&*m_contactManager, SIGNAL(collectEndorseFetchFailed(const ndn::Name&)),
54 this, SLOT(onCollectEndorseFetchFailed(const ndn::Name&)));
55 connect(&*m_contactManager, SIGNAL(contactKeyFetched(const EndorseCertificate&)),
56 this, SLOT(onContactKeyFetched(const EndorseCertificate&)));
57 connect(&*m_contactManager, SIGNAL(contactKeyFetchFailed(const ndn::Name&)),
58 this, SLOT(onContactKeyFetchFailed(const ndn::Name&)));
59
60
Yingdi Yua4898752013-10-15 20:31:37 -070061}
62
63AddContactPanel::~AddContactPanel()
64{
65 delete ui;
Yingdi Yuc29fb982013-10-20 19:43:10 -070066 delete m_warningDialog;
Yingdi Yua4898752013-10-15 20:31:37 -070067}
Yingdi Yud59b5932013-10-16 13:16:49 -070068
69void
70AddContactPanel::onCancelClicked()
71{ this->close(); }
72
73void
74AddContactPanel::onSearchClicked()
75{
Yingdi Yu8dacdf22013-11-05 23:06:43 -080076 m_currentEndorseCertificateReady = false;
77 m_currentCollectEndorseReady = false;
Yingdi Yu79c25a22013-10-21 13:38:38 -070078 ui->infoView->clear();
79 for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--)
80 ui->infoView->removeRow(i);
Yingdi Yuaa8d7692013-10-18 17:05:02 -070081 QString inputIdentity = ui->contactInput->text();
Yingdi Yu64206112013-12-24 11:16:32 +080082 m_searchIdentity = Name(inputIdentity.toStdString());
Yingdi Yuc29fb982013-10-20 19:43:10 -070083
Yingdi Yu8dacdf22013-11-05 23:06:43 -080084 if(Qt::Checked == ui->fetchBox->checkState())
85 {
86 m_contactManager->fetchSelfEndorseCertificate(m_searchIdentity);
87 m_contactManager->fetchCollectEndorse(m_searchIdentity);
88 }
89 else
90 {
91 if(isCorrectName(m_searchIdentity))
92 m_contactManager->fetchKey(m_searchIdentity);
93 else
94 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -080095 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Wrong key certificate name!"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -080096 }
97 }
98}
99
100bool
101AddContactPanel::isCorrectName(const Name& name)
102{
103 string key("KEY");
104 string idCert("ID-CERT");
105
Yingdi Yu64206112013-12-24 11:16:32 +0800106 if(name.get(-1).toEscapedString() != idCert)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800107 return false;
108
109 int keyIndex = -1;
110 for(int i = 0; i < name.size(); i++)
111 {
Yingdi Yu64206112013-12-24 11:16:32 +0800112 if(name.get(i).toEscapedString() == key)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800113 {
114 keyIndex = i;
115 break;
116 }
117 }
118
119 if(keyIndex < 0)
120 return false;
121 else
122 return true;
Yingdi Yud59b5932013-10-16 13:16:49 -0700123}
124
125void
126AddContactPanel::onAddClicked()
127{
Yingdi Yu79c25a22013-10-21 13:38:38 -0700128 ContactItem contactItem(*m_currentEndorseCertificate);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800129 try{
130 m_contactManager->getContactStorage()->addContact(contactItem);
Yingdi Yu64206112013-12-24 11:16:32 +0800131 }catch(std::exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800132 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800133 _LOG_ERROR("Exception: " << e.what());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800134 return;
135 }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700136 emit newContactAdded();
137 this->close();
Yingdi Yud59b5932013-10-16 13:16:49 -0700138}
139
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700140void
Yingdi Yuc29fb982013-10-20 19:43:10 -0700141AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700142{
Yingdi Yub35b8652013-11-07 11:32:40 -0800143 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800144 m_currentEndorseCertificate = make_shared<EndorseCertificate>(endorseCertificate);
145 }catch(std::exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800146 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800147 _LOG_ERROR("Exception: " << e.what());
148 return;
149 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800150 m_currentEndorseCertificateReady = true;
151
152 if(m_currentCollectEndorseReady == true)
153 displayContactInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700154}
155
156void
157AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity)
158{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800159 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact profile"));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700160}
161
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800162void
163AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate)
164{
Yingdi Yub35b8652013-11-07 11:32:40 -0800165 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800166 m_currentEndorseCertificate = make_shared<EndorseCertificate>(endorseCertificate);
167 }catch(std::exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800168 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800169 _LOG_ERROR("Exception: " << e.what());
170 return;
171 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800172
Alexander Afanasyev4f04c552013-11-07 15:30:05 -0800173 m_currentCollectEndorseReady = false;
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800174
175 displayContactInfo();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800176}
177
178void
179AddContactPanel::onContactKeyFetchFailed(const Name& identity)
180{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800181 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact ksk certificate"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800182}
183
184void
185AddContactPanel::onCollectEndorseFetched(const Data& data)
186{
Yingdi Yu64206112013-12-24 11:16:32 +0800187 m_currentCollectEndorse = make_shared<Data>(data);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800188 m_currentCollectEndorseReady = true;
189
190 if(m_currentEndorseCertificateReady == true)
191 displayContactInfo();
192}
193
194void
195AddContactPanel::onCollectEndorseFetchFailed(const Name& identity)
196{
Yingdi Yueaa84e22014-01-16 10:30:26 -0800197 m_currentCollectEndorse = shared_ptr<Data>();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800198 m_currentCollectEndorseReady = true;
199
200 if(m_currentEndorseCertificateReady == true)
201 displayContactInfo();
202}
203
204void
205AddContactPanel::displayContactInfo()
206{
Yingdi Yub35b8652013-11-07 11:32:40 -0800207 // _LOG_TRACE("displayContactInfo");
Yingdi Yu64206112013-12-24 11:16:32 +0800208 const Profile& profile = m_currentEndorseCertificate->getProfileData().getProfile();
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800209 const Block& profileContent = m_currentEndorseCertificate->getProfileData().getContent();
210 Buffer profileBlock(profileContent.value(), profileContent.value_size());
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800211
212 map<string, int> endorseCount;
213
Yingdi Yueaa84e22014-01-16 10:30:26 -0800214 if(static_cast<bool>(m_currentCollectEndorse))
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800215 {
Yingdi Yu64206112013-12-24 11:16:32 +0800216 Chronos::EndorseCollection endorseCollection;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800217
Yingdi Yu64206112013-12-24 11:16:32 +0800218 boost::iostreams::stream
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800219 <boost::iostreams::array_source> is (reinterpret_cast<const char*>(m_currentCollectEndorse->getContent().value()),
220 m_currentCollectEndorse->getContent().value_size());
Yingdi Yu64206112013-12-24 11:16:32 +0800221
222 endorseCollection.ParseFromIstream(&is);
223
224 for(int i = 0; i < endorseCollection.endorsement_size(); i ++)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800225 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800226 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800227 Data data;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800228 data.wireDecode(Block(reinterpret_cast<const uint8_t*>(endorseCollection.endorsement(i).blob().c_str()),
229 endorseCollection.endorsement(i).blob().size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800230 EndorseCertificate endorseCert(data);
231
232 Name signerKeyName = endorseCert.getSigner();
233 Name signerName = signerKeyName.getPrefix(-1);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800234
Yingdi Yu64206112013-12-24 11:16:32 +0800235 shared_ptr<ContactItem> contact = m_contactManager->getContact(signerName);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800236 if(static_cast<bool>(contact))
Yingdi Yu64206112013-12-24 11:16:32 +0800237 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800238
Yingdi Yu64206112013-12-24 11:16:32 +0800239 if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData().getIdentityName()))
240 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800241
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800242 if(!Verifier::verifySignature(data, data.getSignature(), contact->getSelfEndorseCertificate().getPublicKeyInfo()))
Yingdi Yu64206112013-12-24 11:16:32 +0800243 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800244
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800245 const Block& tmpProfileContent = endorseCert.getProfileData().getContent();
246 Buffer tmpProfileBlock(tmpProfileContent.value(), tmpProfileContent.value_size());
247 if(profileBlock != tmpProfileBlock)
Yingdi Yu64206112013-12-24 11:16:32 +0800248 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800249
Yingdi Yu64206112013-12-24 11:16:32 +0800250 const vector<string>& endorseList = endorseCert.getEndorseList();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800251 vector<string>::const_iterator it = endorseList.begin();
252 for(; it != endorseList.end(); it++)
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800253 endorseCount[*it] += 1;
Yingdi Yu64206112013-12-24 11:16:32 +0800254 }catch(std::exception& e){
255 continue;
256 }
257 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800258 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800259
260 ui->infoView->setColumnCount(3);
261 Profile::const_iterator it = profile.begin();
262 int rowCount = 0;
263
264 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
265 ui->infoView->setHorizontalHeaderItem(0, typeHeader);
266 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
267 ui->infoView->setHorizontalHeaderItem(1, valueHeader);
268 QTableWidgetItem *endorseHeader = new QTableWidgetItem(QString::fromUtf8("Endorse"));
269 ui->infoView->setHorizontalHeaderItem(2, endorseHeader);
270
271 for(; it != profile.end(); it++)
272 {
273 ui->infoView->insertRow(rowCount);
Yingdi Yu64206112013-12-24 11:16:32 +0800274 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(it->first));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800275 ui->infoView->setItem(rowCount, 0, type);
276
Yingdi Yu64206112013-12-24 11:16:32 +0800277 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(it->second));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800278 ui->infoView->setItem(rowCount, 1, value);
279
280 map<string, int>::iterator map_it = endorseCount.find(it->first);
281 QTableWidgetItem *endorse = NULL;
282 if(map_it == endorseCount.end())
283 endorse = new QTableWidgetItem(QString::number(0));
284 else
285 endorse = new QTableWidgetItem(QString::number(map_it->second));
286 ui->infoView->setItem(rowCount, 2, endorse);
287
288 rowCount++;
289 }
290}
291
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800292// bool
293// AddContactPanel::isSameBlob(const ndn::Blob& blobA, const ndn::Blob& blobB)
294// {
295// size_t size = blobA.size();
Yingdi Yu64206112013-12-24 11:16:32 +0800296
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800297// if(size != blobB.size())
298// return false;
Yingdi Yu64206112013-12-24 11:16:32 +0800299
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800300// const uint8_t* ap = blobA.buf();
301// const uint8_t* bp = blobB.buf();
Yingdi Yu64206112013-12-24 11:16:32 +0800302
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800303// for(int i = 0; i < size; i++)
304// {
305// if(ap[i] != bp[i])
306// return false;
307// }
Yingdi Yu64206112013-12-24 11:16:32 +0800308
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800309// return true;
Yingdi Yu64206112013-12-24 11:16:32 +0800310
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800311// }
Yingdi Yu64206112013-12-24 11:16:32 +0800312
313
Yingdi Yud59b5932013-10-16 13:16:49 -0700314#if WAF
315#include "addcontactpanel.moc"
316#include "addcontactpanel.cpp.moc"
317#endif