Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 11 | #include "addcontactpanel.h" |
| 12 | #include "ui_addcontactpanel.h" |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 13 | #include <QMessageBox> |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 14 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 15 | #ifndef Q_MOC_RUN |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 16 | // #include <cryptopp/base64.h> |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 17 | // #include <ndn.cxx/helpers/der/der.h> |
| 18 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 19 | #include "logging.h" |
| 20 | #endif |
| 21 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 22 | using namespace ndn; |
| 23 | using namespace std; |
| 24 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 25 | INIT_LOGGER("AddContactPanel"); |
| 26 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 27 | AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager, |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 28 | QWidget *parent) |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 29 | : QDialog(parent) |
| 30 | , ui(new Ui::AddContactPanel) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 31 | , m_contactManager(contactManager) |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 32 | , m_warningDialog(new WarningDialog()) |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 33 | { |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 34 | ui->setupUi(this); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 35 | |
| 36 | qRegisterMetaType<ndn::Name>("NdnName"); |
| 37 | qRegisterMetaType<EndorseCertificate>("EndorseCertificate"); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 38 | qRegisterMetaType<ndn::Data>("NdnData"); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 39 | |
| 40 | connect(ui->cancelButton, SIGNAL(clicked()), |
| 41 | this, SLOT(onCancelClicked())); |
| 42 | connect(ui->searchButton, SIGNAL(clicked()), |
| 43 | this, SLOT(onSearchClicked())); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 44 | connect(ui->addButton, SIGNAL(clicked()), |
| 45 | this, SLOT(onAddClicked())); |
| 46 | connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)), |
| 47 | this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&))); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 48 | connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)), |
| 49 | this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&))); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 50 | 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 Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | AddContactPanel::~AddContactPanel() |
| 63 | { |
| 64 | delete ui; |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 65 | delete m_warningDialog; |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 66 | } |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 67 | |
| 68 | void |
| 69 | AddContactPanel::onCancelClicked() |
| 70 | { this->close(); } |
| 71 | |
| 72 | void |
| 73 | AddContactPanel::onSearchClicked() |
| 74 | { |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 75 | m_currentEndorseCertificateReady = false; |
| 76 | m_currentCollectEndorseReady = false; |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 77 | ui->infoView->clear(); |
| 78 | for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--) |
| 79 | ui->infoView->removeRow(i); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 80 | QString inputIdentity = ui->contactInput->text(); |
| 81 | m_searchIdentity = Name(inputIdentity.toUtf8().constData()); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 82 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 83 | 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 Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 94 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Wrong key certificate name!")); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | AddContactPanel::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 Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
| 125 | AddContactPanel::onAddClicked() |
| 126 | { |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 127 | ContactItem contactItem(*m_currentEndorseCertificate); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 128 | try{ |
| 129 | m_contactManager->getContactStorage()->addContact(contactItem); |
| 130 | }catch(exception& e){ |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 131 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what())); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 132 | _LOG_ERROR("Exception: " << e.what()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 133 | return; |
| 134 | } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 135 | emit newContactAdded(); |
| 136 | this->close(); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 139 | void |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 140 | AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 141 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 142 | try{ |
| 143 | m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate)); |
| 144 | }catch(exception& e){ |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 145 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what())); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 146 | _LOG_ERROR("Exception: " << e.what()); |
| 147 | return; |
| 148 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 149 | m_currentEndorseCertificateReady = true; |
| 150 | |
| 151 | if(m_currentCollectEndorseReady == true) |
| 152 | displayContactInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity) |
| 157 | { |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 158 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact profile")); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 161 | void |
| 162 | AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate) |
| 163 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 164 | try{ |
| 165 | m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate)); |
| 166 | }catch(exception& e){ |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 167 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString(e.what())); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 168 | _LOG_ERROR("Exception: " << e.what()); |
| 169 | return; |
| 170 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 4f04c55 | 2013-11-07 15:30:05 -0800 | [diff] [blame] | 172 | m_currentCollectEndorseReady = false; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 173 | |
| 174 | displayContactInfo(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void |
| 178 | AddContactPanel::onContactKeyFetchFailed(const Name& identity) |
| 179 | { |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame^] | 180 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Cannot fetch contact ksk certificate")); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | AddContactPanel::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 | |
| 193 | void |
| 194 | AddContactPanel::onCollectEndorseFetchFailed(const Name& identity) |
| 195 | { |
| 196 | m_currentCollectEndorse = NULL; |
| 197 | m_currentCollectEndorseReady = true; |
| 198 | |
| 199 | if(m_currentEndorseCertificateReady == true) |
| 200 | displayContactInfo(); |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | AddContactPanel::displayContactInfo() |
| 205 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 206 | // _LOG_TRACE("displayContactInfo"); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 207 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 214 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 224 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 225 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 234 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 240 | |
| 241 | if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData()->getIdentityName())) |
| 242 | continue; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 243 | |
| 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 251 | const vector<string>& endorseList = endorseCert->getEndorseList(); |
| 252 | vector<string>::const_iterator it = endorseList.begin(); |
| 253 | for(; it != endorseList.end(); it++) |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 254 | endorseCount[*it] += 1; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 257 | |
| 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 Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 291 | #if WAF |
| 292 | #include "addcontactpanel.moc" |
| 293 | #include "addcontactpanel.cpp.moc" |
| 294 | #endif |