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" |
| 13 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 14 | #ifndef Q_MOC_RUN |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 15 | // #include <cryptopp/base64.h> |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 16 | // #include <ndn.cxx/helpers/der/der.h> |
| 17 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 18 | #include "logging.h" |
| 19 | #endif |
| 20 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 21 | using namespace ndn; |
| 22 | using namespace std; |
| 23 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 24 | INIT_LOGGER("AddContactPanel"); |
| 25 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 26 | AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager, |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 27 | QWidget *parent) |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 28 | : QDialog(parent) |
| 29 | , ui(new Ui::AddContactPanel) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 30 | , m_contactManager(contactManager) |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 31 | , m_warningDialog(new WarningDialog()) |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 32 | { |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 33 | ui->setupUi(this); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 34 | |
| 35 | qRegisterMetaType<ndn::Name>("NdnName"); |
| 36 | qRegisterMetaType<EndorseCertificate>("EndorseCertificate"); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 37 | qRegisterMetaType<ndn::Data>("NdnData"); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 38 | |
| 39 | connect(ui->cancelButton, SIGNAL(clicked()), |
| 40 | this, SLOT(onCancelClicked())); |
| 41 | connect(ui->searchButton, SIGNAL(clicked()), |
| 42 | this, SLOT(onSearchClicked())); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 43 | connect(ui->addButton, SIGNAL(clicked()), |
| 44 | this, SLOT(onAddClicked())); |
| 45 | connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)), |
| 46 | this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&))); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 47 | connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)), |
| 48 | this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&))); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 49 | 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 Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | AddContactPanel::~AddContactPanel() |
| 62 | { |
| 63 | delete ui; |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 64 | delete m_warningDialog; |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 65 | } |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 66 | |
| 67 | void |
| 68 | AddContactPanel::onCancelClicked() |
| 69 | { this->close(); } |
| 70 | |
| 71 | void |
| 72 | AddContactPanel::onSearchClicked() |
| 73 | { |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 74 | m_currentEndorseCertificateReady = false; |
| 75 | m_currentCollectEndorseReady = false; |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 76 | ui->infoView->clear(); |
| 77 | for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--) |
| 78 | ui->infoView->removeRow(i); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 79 | QString inputIdentity = ui->contactInput->text(); |
| 80 | m_searchIdentity = Name(inputIdentity.toUtf8().constData()); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 82 | 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 | |
| 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){ |
| 131 | m_warningDialog->setMsg(e.what()); |
| 132 | m_warningDialog->show(); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 133 | _LOG_ERROR("Exception: " << e.what()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 134 | return; |
| 135 | } |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 136 | emit newContactAdded(); |
| 137 | this->close(); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 140 | void |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 141 | AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 142 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 143 | try{ |
| 144 | m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate)); |
| 145 | }catch(exception& e){ |
| 146 | m_warningDialog->setMsg(e.what()); |
| 147 | m_warningDialog->show(); |
| 148 | _LOG_ERROR("Exception: " << e.what()); |
| 149 | return; |
| 150 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 151 | m_currentEndorseCertificateReady = true; |
| 152 | |
| 153 | if(m_currentCollectEndorseReady == true) |
| 154 | displayContactInfo(); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity) |
| 159 | { |
| 160 | m_warningDialog->setMsg("Cannot fetch contact profile"); |
| 161 | m_warningDialog->show(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 164 | void |
| 165 | AddContactPanel::onContactKeyFetched(const EndorseCertificate& endorseCertificate) |
| 166 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 167 | try{ |
| 168 | m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate)); |
| 169 | }catch(exception& e){ |
| 170 | m_warningDialog->setMsg(e.what()); |
| 171 | m_warningDialog->show(); |
| 172 | _LOG_ERROR("Exception: " << e.what()); |
| 173 | return; |
| 174 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | 4f04c55 | 2013-11-07 15:30:05 -0800 | [diff] [blame^] | 176 | m_currentCollectEndorseReady = false; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 177 | |
| 178 | displayContactInfo(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
| 182 | AddContactPanel::onContactKeyFetchFailed(const Name& identity) |
| 183 | { |
| 184 | m_warningDialog->setMsg("Cannot fetch contact ksk certificate"); |
| 185 | m_warningDialog->show(); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | AddContactPanel::onCollectEndorseFetched(const Data& data) |
| 190 | { |
| 191 | m_currentCollectEndorse = Ptr<Data>(new Data(data)); |
| 192 | m_currentCollectEndorseReady = true; |
| 193 | |
| 194 | if(m_currentEndorseCertificateReady == true) |
| 195 | displayContactInfo(); |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | AddContactPanel::onCollectEndorseFetchFailed(const Name& identity) |
| 200 | { |
| 201 | m_currentCollectEndorse = NULL; |
| 202 | m_currentCollectEndorseReady = true; |
| 203 | |
| 204 | if(m_currentEndorseCertificateReady == true) |
| 205 | displayContactInfo(); |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | AddContactPanel::displayContactInfo() |
| 210 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 211 | // _LOG_TRACE("displayContactInfo"); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 212 | const Profile& profile = m_currentEndorseCertificate->getProfileData()->getProfile(); |
| 213 | const Blob& profileBlob = m_currentEndorseCertificate->getProfileData()->content(); |
| 214 | |
| 215 | map<string, int> endorseCount; |
| 216 | |
| 217 | if(m_currentCollectEndorse != NULL) |
| 218 | { |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 219 | boost::iostreams::stream |
| 220 | <boost::iostreams::array_source> is (m_currentCollectEndorse->content().buf(), m_currentCollectEndorse->content().size()); |
| 221 | |
| 222 | Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is))); |
| 223 | const der::DerNodePtrList & children = root->getChildren(); |
| 224 | der::SimpleVisitor simpleVisitor; |
| 225 | |
| 226 | for(int i = 0; i < children.size(); i++) |
| 227 | { |
| 228 | Ptr<Blob> dataBlob = boost::any_cast<Ptr<Blob> >(children[i]->accept(simpleVisitor)); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 229 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 230 | Ptr<Data> data = NULL; |
| 231 | Ptr<EndorseCertificate> endorseCert = NULL; |
| 232 | try{ |
| 233 | data = Data::decodeFromWire(dataBlob); |
| 234 | endorseCert = Ptr<EndorseCertificate>(new EndorseCertificate(*data)); |
| 235 | }catch(exception& e){ |
| 236 | continue; |
| 237 | } |
| 238 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 239 | Name signerKeyName = endorseCert->getSigner(); |
| 240 | Name signerName = signerKeyName.getPrefix(signerKeyName.size()-1); |
| 241 | |
| 242 | Ptr<ContactItem> contact = m_contactManager->getContact(signerName); |
| 243 | if(contact == NULL) |
| 244 | continue; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 245 | |
| 246 | if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData()->getIdentityName())) |
| 247 | continue; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 248 | |
| 249 | if(!security::PolicyManager::verifySignature(*data, contact->getSelfEndorseCertificate().getPublicKeyInfo())) |
| 250 | continue; |
| 251 | |
| 252 | const Blob& tmpProfileBlob = endorseCert->getProfileData()->content(); |
| 253 | if(profileBlob != tmpProfileBlob) |
| 254 | continue; |
| 255 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 256 | const vector<string>& endorseList = endorseCert->getEndorseList(); |
| 257 | vector<string>::const_iterator it = endorseList.begin(); |
| 258 | for(; it != endorseList.end(); it++) |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 259 | endorseCount[*it] += 1; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 262 | |
| 263 | ui->infoView->setColumnCount(3); |
| 264 | Profile::const_iterator it = profile.begin(); |
| 265 | int rowCount = 0; |
| 266 | |
| 267 | QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type")); |
| 268 | ui->infoView->setHorizontalHeaderItem(0, typeHeader); |
| 269 | QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value")); |
| 270 | ui->infoView->setHorizontalHeaderItem(1, valueHeader); |
| 271 | QTableWidgetItem *endorseHeader = new QTableWidgetItem(QString::fromUtf8("Endorse")); |
| 272 | ui->infoView->setHorizontalHeaderItem(2, endorseHeader); |
| 273 | |
| 274 | for(; it != profile.end(); it++) |
| 275 | { |
| 276 | ui->infoView->insertRow(rowCount); |
| 277 | QTableWidgetItem *type = new QTableWidgetItem(QString::fromUtf8(it->first.c_str())); |
| 278 | ui->infoView->setItem(rowCount, 0, type); |
| 279 | |
| 280 | string valueString(it->second.buf(), it->second.size()); |
| 281 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromUtf8(valueString.c_str())); |
| 282 | ui->infoView->setItem(rowCount, 1, value); |
| 283 | |
| 284 | map<string, int>::iterator map_it = endorseCount.find(it->first); |
| 285 | QTableWidgetItem *endorse = NULL; |
| 286 | if(map_it == endorseCount.end()) |
| 287 | endorse = new QTableWidgetItem(QString::number(0)); |
| 288 | else |
| 289 | endorse = new QTableWidgetItem(QString::number(map_it->second)); |
| 290 | ui->infoView->setItem(rowCount, 2, endorse); |
| 291 | |
| 292 | rowCount++; |
| 293 | } |
| 294 | } |
| 295 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 296 | #if WAF |
| 297 | #include "addcontactpanel.moc" |
| 298 | #include "addcontactpanel.cpp.moc" |
| 299 | #endif |