blob: 38bc6e99be8fce494c0b2b37dbf80986ebb208e4 [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 Yu6a5b9f62013-11-06 23:00:21 -080016// #include <cryptopp/base64.h>
Yingdi Yu8dacdf22013-11-05 23:06:43 -080017// #include <ndn.cxx/helpers/der/der.h>
18#include <ndn.cxx/helpers/der/visitor/simple-visitor.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;
23using namespace std;
24
Yingdi Yuc29fb982013-10-20 19:43:10 -070025INIT_LOGGER("AddContactPanel");
26
Yingdi Yuaa8d7692013-10-18 17:05:02 -070027AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager,
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070028 QWidget *parent)
Yingdi Yud59b5932013-10-16 13:16:49 -070029 : QDialog(parent)
30 , ui(new Ui::AddContactPanel)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070031 , m_contactManager(contactManager)
Yingdi Yuc29fb982013-10-20 19:43:10 -070032 , m_warningDialog(new WarningDialog())
Yingdi Yua4898752013-10-15 20:31:37 -070033{
Yingdi Yud59b5932013-10-16 13:16:49 -070034 ui->setupUi(this);
Yingdi Yuc29fb982013-10-20 19:43:10 -070035
36 qRegisterMetaType<ndn::Name>("NdnName");
37 qRegisterMetaType<EndorseCertificate>("EndorseCertificate");
Yingdi Yu8dacdf22013-11-05 23:06:43 -080038 qRegisterMetaType<ndn::Data>("NdnData");
Yingdi Yud59b5932013-10-16 13:16:49 -070039
40 connect(ui->cancelButton, SIGNAL(clicked()),
41 this, SLOT(onCancelClicked()));
42 connect(ui->searchButton, SIGNAL(clicked()),
43 this, SLOT(onSearchClicked()));
Yingdi Yu79c25a22013-10-21 13:38:38 -070044 connect(ui->addButton, SIGNAL(clicked()),
45 this, SLOT(onAddClicked()));
46 connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)),
47 this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&)));
Yingdi Yuc29fb982013-10-20 19:43:10 -070048 connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)),
49 this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -080050 connect(&*m_contactManager, SIGNAL(collectEndorseFetched(const ndn::Data&)),
51 this, SLOT(onCollectEndorseFetched(const ndn::Data&)));
52 connect(&*m_contactManager, SIGNAL(collectEndorseFetchFailed(const ndn::Name&)),
53 this, SLOT(onCollectEndorseFetchFailed(const ndn::Name&)));
54 connect(&*m_contactManager, SIGNAL(contactKeyFetched(const EndorseCertificate&)),
55 this, SLOT(onContactKeyFetched(const EndorseCertificate&)));
56 connect(&*m_contactManager, SIGNAL(contactKeyFetchFailed(const ndn::Name&)),
57 this, SLOT(onContactKeyFetchFailed(const ndn::Name&)));
58
59
Yingdi Yua4898752013-10-15 20:31:37 -070060}
61
62AddContactPanel::~AddContactPanel()
63{
64 delete ui;
Yingdi Yuc29fb982013-10-20 19:43:10 -070065 delete m_warningDialog;
Yingdi Yua4898752013-10-15 20:31:37 -070066}
Yingdi Yud59b5932013-10-16 13:16:49 -070067
68void
69AddContactPanel::onCancelClicked()
70{ this->close(); }
71
72void
73AddContactPanel::onSearchClicked()
74{
Yingdi Yu8dacdf22013-11-05 23:06:43 -080075 m_currentEndorseCertificateReady = false;
76 m_currentCollectEndorseReady = false;
Yingdi Yu79c25a22013-10-21 13:38:38 -070077 ui->infoView->clear();
78 for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--)
79 ui->infoView->removeRow(i);
Yingdi Yuaa8d7692013-10-18 17:05:02 -070080 QString inputIdentity = ui->contactInput->text();
81 m_searchIdentity = Name(inputIdentity.toUtf8().constData());
Yingdi Yuc29fb982013-10-20 19:43:10 -070082
Yingdi Yu8dacdf22013-11-05 23:06:43 -080083 if(Qt::Checked == ui->fetchBox->checkState())
84 {
85 m_contactManager->fetchSelfEndorseCertificate(m_searchIdentity);
86 m_contactManager->fetchCollectEndorse(m_searchIdentity);
87 }
88 else
89 {
90 if(isCorrectName(m_searchIdentity))
91 m_contactManager->fetchKey(m_searchIdentity);
92 else
93 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -080094 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Wrong key certificate name!"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -080095 }
96 }
97}
98
99bool
100AddContactPanel::isCorrectName(const Name& name)
101{
102 string key("KEY");
103 string idCert("ID-CERT");
104
105 if(name.get(-1).toUri() != idCert)
106 return false;
107
108 int keyIndex = -1;
109 for(int i = 0; i < name.size(); i++)
110 {
111 if(name.get(i).toUri() == key)
112 {
113 keyIndex = i;
114 break;
115 }
116 }
117
118 if(keyIndex < 0)
119 return false;
120 else
121 return true;
Yingdi Yud59b5932013-10-16 13:16:49 -0700122}
123
124void
125AddContactPanel::onAddClicked()
126{
Yingdi Yu79c25a22013-10-21 13:38:38 -0700127 ContactItem contactItem(*m_currentEndorseCertificate);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800128 try{
129 m_contactManager->getContactStorage()->addContact(contactItem);
130 }catch(exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800131 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800132 _LOG_ERROR("Exception: " << e.what());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800133 return;
134 }
Yingdi Yu79c25a22013-10-21 13:38:38 -0700135 emit newContactAdded();
136 this->close();
Yingdi Yud59b5932013-10-16 13:16:49 -0700137}
138
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700139void
Yingdi Yuc29fb982013-10-20 19:43:10 -0700140AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700141{
Yingdi Yub35b8652013-11-07 11:32:40 -0800142 try{
143 m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate));
144 }catch(exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800145 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800146 _LOG_ERROR("Exception: " << e.what());
147 return;
148 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800149 m_currentEndorseCertificateReady = true;
150
151 if(m_currentCollectEndorseReady == true)
152 displayContactInfo();
Yingdi Yuc29fb982013-10-20 19:43:10 -0700153}
154
155void
156AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity)
157{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800158 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact profile"));
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700159}
160
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800161void
162AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate)
163{
Yingdi Yub35b8652013-11-07 11:32:40 -0800164 try{
165 m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate));
166 }catch(exception& e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800167 QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what()));
Yingdi Yub35b8652013-11-07 11:32:40 -0800168 _LOG_ERROR("Exception: " << e.what());
169 return;
170 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800171
Alexander Afanasyev4f04c552013-11-07 15:30:05 -0800172 m_currentCollectEndorseReady = false;
Yingdi Yue9ea5c92013-11-06 18:42:34 -0800173
174 displayContactInfo();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800175}
176
177void
178AddContactPanel::onContactKeyFetchFailed(const Name& identity)
179{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800180 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact ksk certificate"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800181}
182
183void
184AddContactPanel::onCollectEndorseFetched(const Data& data)
185{
186 m_currentCollectEndorse = Ptr<Data>(new Data(data));
187 m_currentCollectEndorseReady = true;
188
189 if(m_currentEndorseCertificateReady == true)
190 displayContactInfo();
191}
192
193void
194AddContactPanel::onCollectEndorseFetchFailed(const Name& identity)
195{
196 m_currentCollectEndorse = NULL;
197 m_currentCollectEndorseReady = true;
198
199 if(m_currentEndorseCertificateReady == true)
200 displayContactInfo();
201}
202
203void
204AddContactPanel::displayContactInfo()
205{
Yingdi Yub35b8652013-11-07 11:32:40 -0800206 // _LOG_TRACE("displayContactInfo");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800207 const Profile& profile = m_currentEndorseCertificate->getProfileData()->getProfile();
208 const Blob& profileBlob = m_currentEndorseCertificate->getProfileData()->content();
209
210 map<string, int> endorseCount;
211
212 if(m_currentCollectEndorse != NULL)
213 {
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800214 boost::iostreams::stream
215 <boost::iostreams::array_source> is (m_currentCollectEndorse->content().buf(), m_currentCollectEndorse->content().size());
216
217 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
218 const der::DerNodePtrList & children = root->getChildren();
219 der::SimpleVisitor simpleVisitor;
220
221 for(int i = 0; i < children.size(); i++)
222 {
223 Ptr<Blob> dataBlob = boost::any_cast<Ptr<Blob> >(children[i]->accept(simpleVisitor));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800224
Yingdi Yub35b8652013-11-07 11:32:40 -0800225 Ptr<Data> data = NULL;
226 Ptr<EndorseCertificate> endorseCert = NULL;
227 try{
228 data = Data::decodeFromWire(dataBlob);
229 endorseCert = Ptr<EndorseCertificate>(new EndorseCertificate(*data));
230 }catch(exception& e){
231 continue;
232 }
233
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800234 Name signerKeyName = endorseCert->getSigner();
235 Name signerName = signerKeyName.getPrefix(signerKeyName.size()-1);
236
237 Ptr<ContactItem> contact = m_contactManager->getContact(signerName);
238 if(contact == NULL)
239 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800240
241 if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData()->getIdentityName()))
242 continue;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800243
244 if(!security::PolicyManager::verifySignature(*data, contact->getSelfEndorseCertificate().getPublicKeyInfo()))
245 continue;
246
247 const Blob& tmpProfileBlob = endorseCert->getProfileData()->content();
248 if(profileBlob != tmpProfileBlob)
249 continue;
250
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800251 const vector<string>& endorseList = endorseCert->getEndorseList();
252 vector<string>::const_iterator it = endorseList.begin();
253 for(; it != endorseList.end(); it++)
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800254 endorseCount[*it] += 1;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800255 }
256 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800257
258 ui->infoView->setColumnCount(3);
259 Profile::const_iterator it = profile.begin();
260 int rowCount = 0;
261
262 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
263 ui->infoView->setHorizontalHeaderItem(0, typeHeader);
264 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
265 ui->infoView->setHorizontalHeaderItem(1, valueHeader);
266 QTableWidgetItem *endorseHeader = new QTableWidgetItem(QString::fromUtf8("Endorse"));
267 ui->infoView->setHorizontalHeaderItem(2, endorseHeader);
268
269 for(; it != profile.end(); it++)
270 {
271 ui->infoView->insertRow(rowCount);
272 QTableWidgetItem *type = new QTableWidgetItem(QString::fromUtf8(it->first.c_str()));
273 ui->infoView->setItem(rowCount, 0, type);
274
275 string valueString(it->second.buf(), it->second.size());
276 QTableWidgetItem *value = new QTableWidgetItem(QString::fromUtf8(valueString.c_str()));
277 ui->infoView->setItem(rowCount, 1, value);
278
279 map<string, int>::iterator map_it = endorseCount.find(it->first);
280 QTableWidgetItem *endorse = NULL;
281 if(map_it == endorseCount.end())
282 endorse = new QTableWidgetItem(QString::number(0));
283 else
284 endorse = new QTableWidgetItem(QString::number(map_it->second));
285 ui->infoView->setItem(rowCount, 2, endorse);
286
287 rowCount++;
288 }
289}
290
Yingdi Yud59b5932013-10-16 13:16:49 -0700291#if WAF
292#include "addcontactpanel.moc"
293#include "addcontactpanel.cpp.moc"
294#endif