Skip to content

Commit

Permalink
Adapting to TLV-specific ndn-cpp
Browse files Browse the repository at this point in the history
Change-Id: I5f39f38156221fc978db5048c1ff4a3bbb0c17d9
  • Loading branch information
bruinfish authored and cawka committed May 9, 2014
1 parent 6eabbd7 commit f8f572d
Show file tree
Hide file tree
Showing 31 changed files with 424 additions and 551 deletions.
48 changes: 26 additions & 22 deletions src/addcontactpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <QMessageBox>

#ifndef Q_MOC_RUN
#include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp>
#include <ndn-cpp/security/verifier.hpp>
#include <boost/iostreams/stream.hpp>
#include "null-ptrs.h"
#include "endorse-collection.pb.h"
Expand Down Expand Up @@ -207,7 +207,8 @@ AddContactPanel::displayContactInfo()
{
// _LOG_TRACE("displayContactInfo");
const Profile& profile = m_currentEndorseCertificate->getProfileData().getProfile();
const Blob& profileBlob = m_currentEndorseCertificate->getProfileData().getContent();
const Block& profileContent = m_currentEndorseCertificate->getProfileData().getContent();
Buffer profileBlock(profileContent.value(), profileContent.value_size());

map<string, int> endorseCount;

Expand All @@ -216,15 +217,17 @@ AddContactPanel::displayContactInfo()
Chronos::EndorseCollection endorseCollection;

boost::iostreams::stream
<boost::iostreams::array_source> is ((const char*)m_currentCollectEndorse->getContent().buf(), m_currentCollectEndorse->getContent().size());
<boost::iostreams::array_source> is (reinterpret_cast<const char*>(m_currentCollectEndorse->getContent().value()),
m_currentCollectEndorse->getContent().value_size());

endorseCollection.ParseFromIstream(&is);

for(int i = 0; i < endorseCollection.endorsement_size(); i ++)
{
try{
Data data;
data.wireDecode((const uint8_t*)endorseCollection.endorsement(i).blob().c_str(), endorseCollection.endorsement(i).blob().size());
data.wireDecode(Block(reinterpret_cast<const uint8_t*>(endorseCollection.endorsement(i).blob().c_str()),
endorseCollection.endorsement(i).blob().size()));
EndorseCertificate endorseCert(data);

Name signerKeyName = endorseCert.getSigner();
Expand All @@ -237,11 +240,12 @@ AddContactPanel::displayContactInfo()
if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData().getIdentityName()))
continue;

if(!Sha256WithRsaHandler::verifySignature(data, contact->getSelfEndorseCertificate().getPublicKeyInfo()))
if(!Verifier::verifySignature(data, data.getSignature(), contact->getSelfEndorseCertificate().getPublicKeyInfo()))
continue;

const Blob& tmpProfileBlob = endorseCert.getProfileData().getContent();
if(!isSameBlob(profileBlob, tmpProfileBlob))
const Block& tmpProfileContent = endorseCert.getProfileData().getContent();
Buffer tmpProfileBlock(tmpProfileContent.value(), tmpProfileContent.value_size());
if(profileBlock != tmpProfileBlock)
continue;

const vector<string>& endorseList = endorseCert.getEndorseList();
Expand Down Expand Up @@ -286,26 +290,26 @@ AddContactPanel::displayContactInfo()
}
}

bool
AddContactPanel::isSameBlob(const ndn::Blob& blobA, const ndn::Blob& blobB)
{
size_t size = blobA.size();
// bool
// AddContactPanel::isSameBlob(const ndn::Blob& blobA, const ndn::Blob& blobB)
// {
// size_t size = blobA.size();

if(size != blobB.size())
return false;
// if(size != blobB.size())
// return false;

const uint8_t* ap = blobA.buf();
const uint8_t* bp = blobB.buf();
// const uint8_t* ap = blobA.buf();
// const uint8_t* bp = blobB.buf();

for(int i = 0; i < size; i++)
{
if(ap[i] != bp[i])
return false;
}
// for(int i = 0; i < size; i++)
// {
// if(ap[i] != bp[i])
// return false;
// }

return true;
// return true;

}
// }


#if WAF
Expand Down
4 changes: 2 additions & 2 deletions src/addcontactpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class AddContactPanel : public QDialog
bool
isCorrectName(const ndn::Name& name);

static bool
isSameBlob(const ndn::Blob& blobA, const ndn::Blob& blobB);
// static bool
// isSameBlob(const ndn::Blob& blobA, const ndn::Blob& blobB);

private slots:
void
Expand Down
1 change: 0 additions & 1 deletion src/browsecontactdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <boost/asio.hpp>
#include <boost/tokenizer.hpp>
#include "logging.h"
#include "exception.h"
// #include "ndn.cxx/error.h"
#endif

Expand Down
17 changes: 8 additions & 9 deletions src/chat-policy-rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

#include "chat-policy-rule.h"
#include <ndn-cpp/sha256-with-rsa-signature.hpp>
#include <ndn-cpp/security/signature/signature-sha256-with-rsa.hpp>

using namespace ndn;
using namespace std;
Expand All @@ -36,16 +36,15 @@ ChatPolicyRule::matchDataName(const Data & data)
bool
ChatPolicyRule::matchSignerName(const Data & data)
{
const Sha256WithRsaSignature* sigPtr = dynamic_cast<const Sha256WithRsaSignature*> (data.getSignature());

if(NULL == sigPtr)
try{
SignatureSha256WithRsa sig(data.getSignature());
Name signerName = sig.getKeyLocator().getName ();
return m_signerRegex->match(signerName);
}catch(SignatureSha256WithRsa::Error &e){
return false;

if(ndn_KeyLocatorType_KEYNAME != sigPtr->getKeyLocator().getType())
}catch(KeyLocator::Error &e){
return false;

Name signerName = sigPtr->getKeyLocator ().getKeyName ();
return m_signerRegex->match(signerName);
}
}

bool
Expand Down
54 changes: 22 additions & 32 deletions src/chatdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <sync-intro-certificate.h>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <ndn-cpp/sha256-with-rsa-signature.hpp>
#include <ndn-cpp/security/signature/signature-sha256-with-rsa.hpp>
#include "logging.h"
#endif

Expand All @@ -35,7 +35,6 @@ Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> )
Q_DECLARE_METATYPE(size_t)

ChatDialog::ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
ndn::ptr_lib::shared_ptr<ndn::IdentityManager> identityManager,
const ndn::Name& chatroomPrefix,
const ndn::Name& localPrefix,
const ndn::Name& defaultIdentity,
Expand All @@ -49,7 +48,7 @@ ChatDialog::ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
, m_localPrefix(localPrefix)
, m_defaultIdentity(defaultIdentity)
, m_invitationPolicyManager(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toEscapedString(), m_defaultIdentity))
, m_identityManager(identityManager)
, m_keyChain(new ndn::KeyChain())
, m_nick(nick)
, m_sock(NULL)
, m_lastMsgTime(0)
Expand Down Expand Up @@ -89,8 +88,8 @@ ChatDialog::ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,

startFace();

ndn::Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(m_defaultIdentity);
m_syncPolicyManager = ndn::ptr_lib::make_shared<SyncPolicyManager>(m_defaultIdentity, certificateName, m_chatroomPrefix, m_face, m_transport);
ndn::Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);
m_syncPolicyManager = ndn::ptr_lib::make_shared<SyncPolicyManager>(m_defaultIdentity, certificateName, m_chatroomPrefix, m_face);

connect(ui->inviteButton, SIGNAL(clicked()),
this, SLOT(openInviteListDialog()));
Expand Down Expand Up @@ -134,8 +133,7 @@ ChatDialog::~ChatDialog()
void
ChatDialog::startFace()
{
m_transport = ndn::ptr_lib::make_shared<ndn::TcpTransport>();
m_face = ndn::ptr_lib::make_shared<ndn::Face>(m_transport, ndn::ptr_lib::make_shared<ndn::TcpTransport::ConnectionInfo>("localhost"));
m_face = ndn::ptr_lib::make_shared<ndn::Face>();

connectToDaemon();

Expand Down Expand Up @@ -334,7 +332,7 @@ ChatDialog::sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool i
{
m_invitationPolicyManager->addTrustAnchor(contact->getSelfEndorseCertificate());

ndn::Name certificateName = m_identityManager->getDefaultCertificateNameForIdentity(m_defaultIdentity);
ndn::Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);

ndn::Name interestName("/ndn/broadcast/chronos/invitation");
interestName.append(contact->getNameSpace());
Expand All @@ -346,16 +344,11 @@ ChatDialog::sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool i
interestName.append(certificateName);

string signedUri = interestName.toUri();
ndn::Blob signedBlob((const uint8_t*)signedUri.c_str(), signedUri.size());

ndn::ptr_lib::shared_ptr<const ndn::Sha256WithRsaSignature> sha256sig = ndn::ptr_lib::dynamic_pointer_cast<const ndn::Sha256WithRsaSignature>(m_identityManager->signByCertificate(signedBlob.buf(), signedBlob.size(), certificateName));
const ndn::Blob& sigBits = sha256sig->getSignature();
ndn::Signature sig = m_keyChain->sign(reinterpret_cast<const uint8_t*>(signedUri.c_str()), signedUri.size(), certificateName);
const ndn::Block& sigValue = sig.getValue();

_LOG_DEBUG("size A: " << interestName.size());

interestName.append(sigBits);

_LOG_DEBUG("size B: " << interestName.size());
interestName.append(sigValue);

//TODO... remove version from invitation interest
// interestName.appendVersion();
Expand Down Expand Up @@ -385,7 +378,7 @@ ChatDialog::onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& dat
const ndn::Name& identity,
bool isIntroducer)
{
string content((const char*)data->getContent().buf(), data->getContent().size());
string content(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size());
if(content == string("nack"))
invitationRejected(identity);
else
Expand Down Expand Up @@ -422,8 +415,8 @@ void
ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::ptr_lib::shared_ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer)
{
_LOG_DEBUG(" " << identity.toUri() << " Accepted your invitation!");
const ndn::Sha256WithRsaSignature* sha256sig = dynamic_cast<const ndn::Sha256WithRsaSignature*>(data->getSignature());
const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
ndn::SignatureSha256WithRsa sig(data->getSignature());
const ndn::Name & keyLocatorName = sig.getKeyLocator().getName();
ndn::ptr_lib::shared_ptr<ndn::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName);
m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
publishIntroCert(*dskCertificate, isIntroducer);
Expand All @@ -434,15 +427,15 @@ ChatDialog::publishIntroCert(const ndn::IdentityCertificate& dskCertificate, boo
{
SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix,
dskCertificate.getPublicKeyName(),
m_identityManager->getDefaultKeyNameForIdentity(m_defaultIdentity),
m_keyChain->getDefaultKeyNameForIdentity(m_defaultIdentity),
dskCertificate.getNotBefore(),
dskCertificate.getNotAfter(),
dskCertificate.getPublicKeyInfo(),
(isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER));
ndn::Name certName = m_identityManager->getDefaultCertificateNameForIdentity(m_defaultIdentity);
ndn::Name certName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);
_LOG_DEBUG("Publish Intro Certificate: " << syncIntroCertificate.getName());
m_identityManager->signByCertificate(syncIntroCertificate, certName);
m_transport->send(*syncIntroCertificate.wireEncode());
m_keyChain->sign(syncIntroCertificate, certName);
m_face->put(syncIntroCertificate);
}

void
Expand All @@ -464,7 +457,6 @@ ChatDialog::initializeSync()
m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
m_syncPolicyManager,
m_face,
m_transport,
boost::bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
boost::bind(&ChatDialog::processRemoveWrapper, this, _1));

Expand Down Expand Up @@ -604,8 +596,8 @@ void
ChatDialog::processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data)
{
string name = data->getName().toUri();
const char* buf = (const char*)data->getContent().buf();
size_t len = data->getContent().size();
const char* buf = reinterpret_cast<const char*>(data->getContent().value());
size_t len = data->getContent().value_size();

char *tempBuf = new char[len];
memcpy(tempBuf, buf, len);
Expand All @@ -617,8 +609,8 @@ void
ChatDialog::processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data)
{
string name = data->getName().toUri();
const char* buf = (const char*)data->getContent().buf();
size_t len = data->getContent().size();
const char* buf = reinterpret_cast<const char*>(data->getContent().value());
size_t len = data->getContent().value_size();

char *tempBuf = new char[len];
memcpy(tempBuf, buf, len);
Expand Down Expand Up @@ -899,7 +891,6 @@ ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
m_syncPolicyManager,
m_face,
m_transport,
bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
bind(&ChatDialog::processRemoveWrapper, this, _1));
usleep(100000);
Expand Down Expand Up @@ -1050,8 +1041,7 @@ ChatDialog::updateLocalPrefix()
m_newLocalPrefixReady = false;
ndn::Name interestName("/local/ndn/prefix");
ndn::Interest interest(interestName);
interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
interest.setInterestLifetimeMilliseconds(1000);
interest.setInterestLifetime(1000);

m_face->expressInterest(interest,
bind(&ChatDialog::onLocalPrefix, this, _1, _2),
Expand All @@ -1078,7 +1068,7 @@ void
ChatDialog::onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
const ndn::ptr_lib::shared_ptr<ndn::Data>& data)
{
string dataString((const char*)data->getContent().buf(), data->getContent().size());
string dataString(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size());
QString originPrefix = QString::fromStdString (dataString).trimmed ();
string trimmedString = originPrefix.toStdString();
m_newLocalPrefix = ndn::Name(trimmedString);
Expand Down
6 changes: 2 additions & 4 deletions src/chatdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef Q_MOC_RUN
#include <ndn-cpp/data.hpp>
#include <ndn-cpp/face.hpp>
#include <ndn-cpp/security/identity/identity-manager.hpp>
#include <ndn-cpp/security/key-chain.hpp>
#include "invitation-policy-manager.h"
#include "contact-item.h"

Expand Down Expand Up @@ -53,7 +53,6 @@ class ChatDialog : public QDialog

public:
explicit ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
ndn::ptr_lib::shared_ptr<ndn::IdentityManager> identityManager,
const ndn::Name& chatroomPrefix,
const ndn::Name& localPrefix,
const ndn::Name& defaultIdentity,
Expand Down Expand Up @@ -344,9 +343,8 @@ private slots:
ndn::Name m_defaultIdentity;
ndn::ptr_lib::shared_ptr<InvitationPolicyManager> m_invitationPolicyManager;
ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;

boost::recursive_mutex m_mutex;
boost::thread m_thread;
Expand Down
Loading

0 comments on commit f8f572d

Please sign in to comment.