blob: bea8d7f340874ed925c6c9557fcbdf228cab02ab [file] [log] [blame]
Yingdi Yu0269c872013-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 Yubf7517c2013-10-15 20:31:37 -070011#include "addcontactpanel.h"
12#include "ui_addcontactpanel.h"
13
Yingdi Yud95c5642013-10-20 19:43:10 -070014#ifndef Q_MOC_RUN
Yingdi Yub2e747d2013-11-05 23:06:43 -080015#include <cryptopp/base64.h>
16// #include <ndn.cxx/helpers/der/der.h>
17#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
Yingdi Yud95c5642013-10-20 19:43:10 -070018#include "logging.h"
19#endif
20
Yingdi Yu4685b1b2013-10-18 17:05:02 -070021using namespace ndn;
22using namespace std;
23
Yingdi Yud95c5642013-10-20 19:43:10 -070024INIT_LOGGER("AddContactPanel");
25
Yingdi Yu4685b1b2013-10-18 17:05:02 -070026AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager,
Yingdi Yu9236c432013-10-18 11:29:25 -070027 QWidget *parent)
Yingdi Yu0269c872013-10-16 13:16:49 -070028 : QDialog(parent)
29 , ui(new Ui::AddContactPanel)
Yingdi Yu9236c432013-10-18 11:29:25 -070030 , m_contactManager(contactManager)
Yingdi Yud95c5642013-10-20 19:43:10 -070031 , m_warningDialog(new WarningDialog())
Yingdi Yubf7517c2013-10-15 20:31:37 -070032{
Yingdi Yu0269c872013-10-16 13:16:49 -070033 ui->setupUi(this);
Yingdi Yud95c5642013-10-20 19:43:10 -070034
35 qRegisterMetaType<ndn::Name>("NdnName");
36 qRegisterMetaType<EndorseCertificate>("EndorseCertificate");
Yingdi Yub2e747d2013-11-05 23:06:43 -080037 qRegisterMetaType<ndn::Data>("NdnData");
Yingdi Yu0269c872013-10-16 13:16:49 -070038
39 connect(ui->cancelButton, SIGNAL(clicked()),
40 this, SLOT(onCancelClicked()));
41 connect(ui->searchButton, SIGNAL(clicked()),
42 this, SLOT(onSearchClicked()));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070043 connect(ui->addButton, SIGNAL(clicked()),
44 this, SLOT(onAddClicked()));
45 connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)),
46 this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&)));
Yingdi Yud95c5642013-10-20 19:43:10 -070047 connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)),
48 this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&)));
Yingdi Yub2e747d2013-11-05 23:06:43 -080049 connect(&*m_contactManager, SIGNAL(collectEndorseFetched(const ndn::Data&)),
50 this, SLOT(onCollectEndorseFetched(const ndn::Data&)));
51 connect(&*m_contactManager, SIGNAL(collectEndorseFetchFailed(const ndn::Name&)),
52 this, SLOT(onCollectEndorseFetchFailed(const ndn::Name&)));
53 connect(&*m_contactManager, SIGNAL(contactKeyFetched(const EndorseCertificate&)),
54 this, SLOT(onContactKeyFetched(const EndorseCertificate&)));
55 connect(&*m_contactManager, SIGNAL(contactKeyFetchFailed(const ndn::Name&)),
56 this, SLOT(onContactKeyFetchFailed(const ndn::Name&)));
57
58
Yingdi Yubf7517c2013-10-15 20:31:37 -070059}
60
61AddContactPanel::~AddContactPanel()
62{
63 delete ui;
Yingdi Yud95c5642013-10-20 19:43:10 -070064 delete m_warningDialog;
Yingdi Yubf7517c2013-10-15 20:31:37 -070065}
Yingdi Yu0269c872013-10-16 13:16:49 -070066
67void
68AddContactPanel::onCancelClicked()
69{ this->close(); }
70
71void
72AddContactPanel::onSearchClicked()
73{
Yingdi Yub2e747d2013-11-05 23:06:43 -080074 m_currentEndorseCertificateReady = false;
75 m_currentCollectEndorseReady = false;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070076 ui->infoView->clear();
77 for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--)
78 ui->infoView->removeRow(i);
Yingdi Yu4685b1b2013-10-18 17:05:02 -070079 QString inputIdentity = ui->contactInput->text();
80 m_searchIdentity = Name(inputIdentity.toUtf8().constData());
Yingdi Yud95c5642013-10-20 19:43:10 -070081
Yingdi Yub2e747d2013-11-05 23:06:43 -080082 if(Qt::Checked == ui->fetchBox->checkState())
83 {
84 m_contactManager->fetchSelfEndorseCertificate(m_searchIdentity);
85 m_contactManager->fetchCollectEndorse(m_searchIdentity);
86 }
87 else
88 {
89 if(isCorrectName(m_searchIdentity))
90 m_contactManager->fetchKey(m_searchIdentity);
91 else
92 {
93 m_warningDialog->setMsg("Wrong key certificate name!");
94 m_warningDialog->show();
95 }
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 Yu0269c872013-10-16 13:16:49 -0700122}
123
124void
125AddContactPanel::onAddClicked()
126{
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700127 ContactItem contactItem(*m_currentEndorseCertificate);
Yingdi Yu71c01872013-11-03 16:22:05 -0800128 m_contactManager->getContactStorage()->addContact(contactItem);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700129 emit newContactAdded();
130 this->close();
Yingdi Yu0269c872013-10-16 13:16:49 -0700131}
132
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700133void
Yingdi Yud95c5642013-10-20 19:43:10 -0700134AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate)
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700135{
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700136 m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800137 m_currentEndorseCertificateReady = true;
138
139 if(m_currentCollectEndorseReady == true)
140 displayContactInfo();
Yingdi Yud95c5642013-10-20 19:43:10 -0700141}
142
143void
144AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity)
145{
146 m_warningDialog->setMsg("Cannot fetch contact profile");
147 m_warningDialog->show();
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700148}
149
Yingdi Yub2e747d2013-11-05 23:06:43 -0800150void
151AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate)
152{
153 m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate));
154
Yingdi Yu2e3199c2013-11-06 18:42:34 -0800155 m_currentCollectEndorseReady = NULL;
156
157 displayContactInfo();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800158}
159
160void
161AddContactPanel::onContactKeyFetchFailed(const Name& identity)
162{
163 m_warningDialog->setMsg("Cannot fetch contact ksk certificate");
164 m_warningDialog->show();
165}
166
167void
168AddContactPanel::onCollectEndorseFetched(const Data& data)
169{
170 m_currentCollectEndorse = Ptr<Data>(new Data(data));
171 m_currentCollectEndorseReady = true;
172
173 if(m_currentEndorseCertificateReady == true)
174 displayContactInfo();
175}
176
177void
178AddContactPanel::onCollectEndorseFetchFailed(const Name& identity)
179{
180 m_currentCollectEndorse = NULL;
181 m_currentCollectEndorseReady = true;
182
183 if(m_currentEndorseCertificateReady == true)
184 displayContactInfo();
185}
186
187void
188AddContactPanel::displayContactInfo()
189{
190 _LOG_TRACE("displayContactInfo");
191 const Profile& profile = m_currentEndorseCertificate->getProfileData()->getProfile();
192 const Blob& profileBlob = m_currentEndorseCertificate->getProfileData()->content();
193
194 map<string, int> endorseCount;
195
196 if(m_currentCollectEndorse != NULL)
197 {
198 _LOG_DEBUG("CollectEndorse fetched");
199 boost::iostreams::stream
200 <boost::iostreams::array_source> is (m_currentCollectEndorse->content().buf(), m_currentCollectEndorse->content().size());
201
202 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
203 const der::DerNodePtrList & children = root->getChildren();
204 der::SimpleVisitor simpleVisitor;
205
206 for(int i = 0; i < children.size(); i++)
207 {
208 Ptr<Blob> dataBlob = boost::any_cast<Ptr<Blob> >(children[i]->accept(simpleVisitor));
209 Ptr<Data> data = Data::decodeFromWire(dataBlob);
210 Ptr<EndorseCertificate> endorseCert = Ptr<EndorseCertificate>(new EndorseCertificate(*data));
211 _LOG_DEBUG("endorseCert name: " << endorseCert->getName().toUri());
212
213 Name signerKeyName = endorseCert->getSigner();
214 Name signerName = signerKeyName.getPrefix(signerKeyName.size()-1);
215
216 Ptr<ContactItem> contact = m_contactManager->getContact(signerName);
217 if(contact == NULL)
218 continue;
219 _LOG_DEBUG("get contact: " << signerName.toUri());
220
221 if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData()->getIdentityName()))
222 continue;
223 _LOG_DEBUG("contact can be trusted");
224
225 if(!security::PolicyManager::verifySignature(*data, contact->getSelfEndorseCertificate().getPublicKeyInfo()))
226 continue;
227
228 const Blob& tmpProfileBlob = endorseCert->getProfileData()->content();
229 if(profileBlob != tmpProfileBlob)
230 continue;
231
232 _LOG_DEBUG("Profile equal");
233
234 const vector<string>& endorseList = endorseCert->getEndorseList();
235 vector<string>::const_iterator it = endorseList.begin();
236 for(; it != endorseList.end(); it++)
237 {
238 _LOG_DEBUG("Entry: " << *it);
239 endorseCount[*it] += 1;
240 }
241 }
242 }
243
244 // map<string, int>::iterator tmp_it = endorseCount.begin();
245 // for(; tmp_it != endorseCount.end(); tmp_it++)
246 // {
247 // _LOG_DEBUG("Entry: " << tmp_it->first << " " << tmp_it->second);
248 // }
249
250 ui->infoView->setColumnCount(3);
251 Profile::const_iterator it = profile.begin();
252 int rowCount = 0;
253
254 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
255 ui->infoView->setHorizontalHeaderItem(0, typeHeader);
256 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
257 ui->infoView->setHorizontalHeaderItem(1, valueHeader);
258 QTableWidgetItem *endorseHeader = new QTableWidgetItem(QString::fromUtf8("Endorse"));
259 ui->infoView->setHorizontalHeaderItem(2, endorseHeader);
260
261 for(; it != profile.end(); it++)
262 {
263 ui->infoView->insertRow(rowCount);
264 QTableWidgetItem *type = new QTableWidgetItem(QString::fromUtf8(it->first.c_str()));
265 ui->infoView->setItem(rowCount, 0, type);
266
267 string valueString(it->second.buf(), it->second.size());
268 QTableWidgetItem *value = new QTableWidgetItem(QString::fromUtf8(valueString.c_str()));
269 ui->infoView->setItem(rowCount, 1, value);
270
271 map<string, int>::iterator map_it = endorseCount.find(it->first);
272 QTableWidgetItem *endorse = NULL;
273 if(map_it == endorseCount.end())
274 endorse = new QTableWidgetItem(QString::number(0));
275 else
276 endorse = new QTableWidgetItem(QString::number(map_it->second));
277 ui->infoView->setItem(rowCount, 2, endorse);
278
279 rowCount++;
280 }
281}
282
Yingdi Yu0269c872013-10-16 13:16:49 -0700283#if WAF
284#include "addcontactpanel.moc"
285#include "addcontactpanel.cpp.moc"
286#endif