blob: add341adf6c1f708bdac923391603b8cab5e3183 [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 Yua1a688f2014-02-06 18:09:22 -080016#include <ndn-cpp-dev/security/validator.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 Yua1a688f2014-02-06 18:09:22 -080023using namespace chronos;
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");
Yingdi Yua1a688f2014-02-06 18:09:22 -080038 qRegisterMetaType<chronos::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()));
Yingdi Yua1a688f2014-02-06 18:09:22 -080047 connect(&*m_contactManager, SIGNAL(contactFetched(const chronos::EndorseCertificate&)),
48 this, SLOT(selfEndorseCertificateFetched(const chronos::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&)));
Yingdi Yua1a688f2014-02-06 18:09:22 -080055 connect(&*m_contactManager, SIGNAL(contactKeyFetched(const chronos::EndorseCertificate&)),
56 this, SLOT(onContactKeyFetched(const chronos::EndorseCertificate&)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -080057 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{
Yingdi Yua1a688f2014-02-06 18:09:22 -0800103 if(name.get(-1).toEscapedString() != "ID-CERT")
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800104 return false;
105
106 int keyIndex = -1;
107 for(int i = 0; i < name.size(); i++)
108 {
Yingdi Yua1a688f2014-02-06 18:09:22 -0800109 if(name.get(i).toEscapedString() == "KEY")
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800110 {
111 keyIndex = i;
112 break;
113 }
114 }
115
116 if(keyIndex < 0)
117 return false;
118 else
119 return true;
Yingdi Yud59b5932013-10-16 13:16:49 -0700120}
121
122void
123AddContactPanel::onAddClicked()
124{
Yingdi Yu79c25a22013-10-21 13:38:38 -0700125 ContactItem contactItem(*m_currentEndorseCertificate);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800126 try{
127 m_contactManager->getContactStorage()->addContact(contactItem);
Yingdi Yua1a688f2014-02-06 18:09:22 -0800128 }catch(ContactStorage::Error& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800129 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800130 _LOG_ERROR("Exception: " << e.what());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800131 return;
132 }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700133 emit newContactAdded();
134 this->close();
Yingdi Yud59b5932013-10-16 13:16:49 -0700135}
136
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700137void
Yingdi Yuc29fb982013-10-20 19:43:10 -0700138AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700139{
Yingdi Yua1a688f2014-02-06 18:09:22 -0800140
141 m_currentEndorseCertificate = make_shared<EndorseCertificate>(endorseCertificate);
142
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800143 m_currentEndorseCertificateReady = true;
144
145 if(m_currentCollectEndorseReady == true)
146 displayContactInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700147}
148
149void
150AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity)
151{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800152 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact profile"));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700153}
154
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800155void
156AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate)
157{
Yingdi Yua1a688f2014-02-06 18:09:22 -0800158 m_currentEndorseCertificate = make_shared<EndorseCertificate>(endorseCertificate);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800159
Alexander Afanasyev4f04c552013-11-07 15:30:05 -0800160 m_currentCollectEndorseReady = false;
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800161
162 displayContactInfo();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800163}
164
165void
166AddContactPanel::onContactKeyFetchFailed(const Name& identity)
167{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800168 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact ksk certificate"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800169}
170
171void
172AddContactPanel::onCollectEndorseFetched(const Data& data)
173{
Yingdi Yu64206112013-12-24 11:16:32 +0800174 m_currentCollectEndorse = make_shared<Data>(data);
Yingdi Yua1a688f2014-02-06 18:09:22 -0800175
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800176 m_currentCollectEndorseReady = true;
177
178 if(m_currentEndorseCertificateReady == true)
179 displayContactInfo();
180}
181
182void
183AddContactPanel::onCollectEndorseFetchFailed(const Name& identity)
184{
Yingdi Yueaa84e22014-01-16 10:30:26 -0800185 m_currentCollectEndorse = shared_ptr<Data>();
Yingdi Yua1a688f2014-02-06 18:09:22 -0800186
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800187 m_currentCollectEndorseReady = true;
188
189 if(m_currentEndorseCertificateReady == true)
190 displayContactInfo();
191}
192
193void
194AddContactPanel::displayContactInfo()
195{
Yingdi Yub35b8652013-11-07 11:32:40 -0800196 // _LOG_TRACE("displayContactInfo");
Yingdi Yua1a688f2014-02-06 18:09:22 -0800197 const Profile& profile = m_currentEndorseCertificate->getProfile();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800198
199 map<string, int> endorseCount;
200
Yingdi Yua1a688f2014-02-06 18:09:22 -0800201 if(!static_cast<bool>(m_currentCollectEndorse))
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800202 {
Yingdi Yu64206112013-12-24 11:16:32 +0800203 Chronos::EndorseCollection endorseCollection;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800204
Yingdi Yu64206112013-12-24 11:16:32 +0800205 boost::iostreams::stream
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800206 <boost::iostreams::array_source> is (reinterpret_cast<const char*>(m_currentCollectEndorse->getContent().value()),
207 m_currentCollectEndorse->getContent().value_size());
Yingdi Yu64206112013-12-24 11:16:32 +0800208
209 endorseCollection.ParseFromIstream(&is);
210
211 for(int i = 0; i < endorseCollection.endorsement_size(); i ++)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800212 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800213 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800214 Data data;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800215 data.wireDecode(Block(reinterpret_cast<const uint8_t*>(endorseCollection.endorsement(i).blob().c_str()),
216 endorseCollection.endorsement(i).blob().size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800217 EndorseCertificate endorseCert(data);
218
Yingdi Yua1a688f2014-02-06 18:09:22 -0800219 Name signerName = endorseCert.getSigner().getPrefix(-1);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800220
Yingdi Yu64206112013-12-24 11:16:32 +0800221 shared_ptr<ContactItem> contact = m_contactManager->getContact(signerName);
Yingdi Yua1a688f2014-02-06 18:09:22 -0800222 if(!static_cast<bool>(contact))
Yingdi Yu64206112013-12-24 11:16:32 +0800223 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800224
Yingdi Yua1a688f2014-02-06 18:09:22 -0800225 if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfile().getIdentityName()))
Yingdi Yu64206112013-12-24 11:16:32 +0800226 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800227
Yingdi Yua1a688f2014-02-06 18:09:22 -0800228 if(!Validator::verifySignature(data, data.getSignature(), contact->getSelfEndorseCertificate().getPublicKeyInfo()))
Yingdi Yu64206112013-12-24 11:16:32 +0800229 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800230
Yingdi Yua1a688f2014-02-06 18:09:22 -0800231 const Profile& tmpProfile = endorseCert.getProfile();
232 if(!(profile == tmpProfile))
Yingdi Yu64206112013-12-24 11:16:32 +0800233 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800234
Yingdi Yu64206112013-12-24 11:16:32 +0800235 const vector<string>& endorseList = endorseCert.getEndorseList();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800236 vector<string>::const_iterator it = endorseList.begin();
237 for(; it != endorseList.end(); it++)
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800238 endorseCount[*it] += 1;
Yingdi Yua1a688f2014-02-06 18:09:22 -0800239 }catch(std::runtime_error& e){
Yingdi Yu64206112013-12-24 11:16:32 +0800240 continue;
241 }
242 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800243 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800244
245 ui->infoView->setColumnCount(3);
246 Profile::const_iterator it = profile.begin();
247 int rowCount = 0;
248
249 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
250 ui->infoView->setHorizontalHeaderItem(0, typeHeader);
251 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
252 ui->infoView->setHorizontalHeaderItem(1, valueHeader);
253 QTableWidgetItem *endorseHeader = new QTableWidgetItem(QString::fromUtf8("Endorse"));
254 ui->infoView->setHorizontalHeaderItem(2, endorseHeader);
255
256 for(; it != profile.end(); it++)
257 {
258 ui->infoView->insertRow(rowCount);
Yingdi Yu64206112013-12-24 11:16:32 +0800259 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(it->first));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800260 ui->infoView->setItem(rowCount, 0, type);
261
Yingdi Yu64206112013-12-24 11:16:32 +0800262 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(it->second));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800263 ui->infoView->setItem(rowCount, 1, value);
264
265 map<string, int>::iterator map_it = endorseCount.find(it->first);
266 QTableWidgetItem *endorse = NULL;
267 if(map_it == endorseCount.end())
268 endorse = new QTableWidgetItem(QString::number(0));
269 else
270 endorse = new QTableWidgetItem(QString::number(map_it->second));
271 ui->infoView->setItem(rowCount, 2, endorse);
272
273 rowCount++;
274 }
275}
276
Yingdi Yu64206112013-12-24 11:16:32 +0800277
Yingdi Yud59b5932013-10-16 13:16:49 -0700278#if WAF
279#include "addcontactpanel.moc"
280#include "addcontactpanel.cpp.moc"
281#endif