Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -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 | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 11 | #include "sync-intro-certificate.h" |
| 12 | #include "sync-logging.h" |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 13 | #include <ndn-cpp/security/identity/basic-identity-storage.hpp> |
| 14 | #include <ndn-cpp/security/identity/osx-private-key-storage.hpp> |
| 15 | #include <ndn-cpp/sha256-with-rsa-signature.hpp> |
| 16 | #include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp> |
| 17 | |
| 18 | #include "sync-policy-manager.h" |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace ndn; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 21 | using namespace ndn::ptr_lib; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 22 | using namespace std; |
| 23 | |
| 24 | INIT_LOGGER("SyncPolicyManager"); |
| 25 | |
| 26 | SyncPolicyManager::SyncPolicyManager(const Name& signingIdentity, |
| 27 | const Name& signingCertificateName, |
| 28 | const Name& syncPrefix, |
| 29 | int stepLimit) |
| 30 | : m_signingIdentity(signingIdentity) |
| 31 | , m_signingCertificateName(signingCertificateName.getPrefix(signingCertificateName.size()-1)) |
| 32 | , m_syncPrefix(syncPrefix) |
| 33 | , m_stepLimit(stepLimit) |
| 34 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 35 | m_transport = make_shared<TcpTransport>(); |
| 36 | m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost")); |
| 37 | |
| 38 | connectToDaemon(); |
| 39 | |
| 40 | shared_ptr<IdentityStorage> publicStorage = make_shared<BasicIdentityStorage>(); |
| 41 | shared_ptr<PrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>(); |
| 42 | m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage); |
| 43 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 44 | Name wotPrefix = syncPrefix; |
| 45 | wotPrefix.append("WOT"); |
| 46 | m_syncPrefixRegex = Regex::fromName(syncPrefix); |
| 47 | m_wotPrefixRegex = Regex::fromName(wotPrefix); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 48 | m_chatDataPolicy = make_shared<IdentityPolicyRule>("^[^<%F0.>]*<%F0.>([^<chronos>]*)<chronos><>", |
| 49 | "^([^<KEY>]*)<KEY>(<>*)[<dsk-.*><ksk-.*>]<ID-CERT>$", |
| 50 | "==", "\\1", "\\1", true); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | SyncPolicyManager::~SyncPolicyManager() |
| 54 | {} |
| 55 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 56 | void |
| 57 | SyncPolicyManager::connectToDaemon() |
| 58 | { |
| 59 | //Hack! transport does not connect to daemon unless an interest is expressed. |
| 60 | Name name("/ndn"); |
| 61 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(name); |
| 62 | m_face->expressInterest(*interest, |
| 63 | bind(&SyncPolicyManager::onConnectionData, this, _1, _2), |
| 64 | bind(&SyncPolicyManager::onConnectionDataTimeout, this, _1)); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | SyncPolicyManager::onConnectionData(const shared_ptr<const ndn::Interest>& interest, |
| 69 | const shared_ptr<Data>& data) |
| 70 | { |
| 71 | _LOG_DEBUG("onConnectionData"); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | SyncPolicyManager::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest) |
| 76 | { |
| 77 | _LOG_DEBUG("onConnectionDataTimeout"); |
| 78 | } |
| 79 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 80 | bool |
| 81 | SyncPolicyManager::skipVerifyAndTrust (const Data& data) |
| 82 | { return false; } |
| 83 | |
| 84 | bool |
| 85 | SyncPolicyManager::requireVerify (const Data& data) |
| 86 | { return true; } |
| 87 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 88 | shared_ptr<ValidationRequest> |
| 89 | SyncPolicyManager::checkVerificationPolicy(const shared_ptr<Data>& data, |
| 90 | int stepCount, |
| 91 | const OnVerified& onVerified, |
| 92 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 93 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 94 | if(stepCount > m_stepLimit) |
| 95 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 96 | onVerifyFailed(data); |
| 97 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 100 | const Sha256WithRsaSignature* sigPtr = dynamic_cast<const Sha256WithRsaSignature*> (data->getSignature()); |
| 101 | if(ndn_KeyLocatorType_KEYNAME != sigPtr->getKeyLocator().getType()) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 102 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 103 | onVerifyFailed(data); |
| 104 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 107 | const Name& keyLocatorName = sigPtr->getKeyLocator().getKeyName(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 108 | |
| 109 | // if data is intro cert |
| 110 | if(m_wotPrefixRegex->match(data->getName())) |
| 111 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 112 | // _LOG_DEBUG("Intro Cert"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 113 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 114 | map<string, PublicKey>::const_iterator it = m_trustedIntroducers.find(keyName.toUri()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 115 | if(m_trustedIntroducers.end() != it) |
| 116 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 117 | if(Sha256WithRsaHandler::verifySignature(*data, it->second)) |
| 118 | onVerified(data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 119 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 120 | onVerifyFailed(data); |
| 121 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 122 | } |
| 123 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 124 | return prepareRequest(keyName, true, data, stepCount, onVerified, onVerifyFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // if data is sync data or chat data |
| 128 | if(m_syncPrefixRegex->match(data->getName()) || m_chatDataPolicy->satisfy(*data)) |
| 129 | { |
| 130 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
| 131 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 132 | map<string, PublicKey>::const_iterator it = m_trustedIntroducers.find(keyName.toUri()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 133 | if(m_trustedIntroducers.end() != it) |
| 134 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 135 | if(Sha256WithRsaHandler::verifySignature(*data, it->second)) |
| 136 | onVerified(data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 137 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 138 | onVerifyFailed(data); |
| 139 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 142 | it = m_trustedProducers.find(keyName.toUri()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 143 | if(m_trustedProducers.end() != it) |
| 144 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 145 | if(Sha256WithRsaHandler::verifySignature(*data, it->second)) |
| 146 | onVerified(data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 147 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 148 | onVerifyFailed(data); |
| 149 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 150 | } |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 151 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 152 | return prepareRequest(keyName, false, data, stepCount, onVerified, onVerifyFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 155 | onVerifyFailed(data); |
| 156 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool |
| 160 | SyncPolicyManager::checkSigningPolicy(const Name& dataName, |
| 161 | const Name& certificateName) |
| 162 | { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 163 | return true; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | Name |
| 167 | SyncPolicyManager::inferSigningIdentity(const ndn::Name& dataName) |
| 168 | { return m_signingIdentity; } |
| 169 | |
| 170 | void |
| 171 | SyncPolicyManager::addTrustAnchor(const IdentityCertificate& identityCertificate, bool isIntroducer) |
| 172 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 173 | // _LOG_DEBUG("Add intro/producer: " << identityCertificate.getPublicKeyName()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 174 | if(isIntroducer) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 175 | m_trustedIntroducers.insert(pair <string, PublicKey > (identityCertificate.getPublicKeyName().toUri(), |
| 176 | identityCertificate.getPublicKeyInfo())); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 177 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 178 | m_trustedProducers.insert(pair <string, PublicKey > (identityCertificate.getPublicKeyName().toUri(), |
| 179 | identityCertificate.getPublicKeyInfo())); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void |
| 183 | SyncPolicyManager::addChatDataRule(const Name& prefix, |
| 184 | const IdentityCertificate& identityCertificate, |
| 185 | bool isIntroducer) |
| 186 | { |
| 187 | // Name dataPrefix = prefix; |
| 188 | // dataPrefix.append("chronos").append(m_syncPrefix.get(-1)); |
| 189 | // Ptr<Regex> dataRegex = Regex::fromName(prefix); |
| 190 | // Name certName = identityCertificate.getName(); |
| 191 | // Name signerName = certName.getPrefix(certName.size()-1); |
| 192 | // Ptr<Regex> signerRegex = Regex::fromName(signerName, true); |
| 193 | |
| 194 | // SpecificPolicyRule rule(dataRegex, signerRegex); |
| 195 | // map<Name, SpecificPolicyRule>::iterator it = m_chatDataRules.find(dataPrefix); |
| 196 | // if(it != m_chatDataRules.end()) |
| 197 | // it->second = rule; |
| 198 | // else |
| 199 | // m_chatDataRules.insert(pair <Name, SpecificPolicyRule > (dataPrefix, rule)); |
| 200 | |
| 201 | addTrustAnchor(identityCertificate, isIntroducer); |
| 202 | } |
| 203 | |
| 204 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 205 | shared_ptr<const vector<Name> > |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 206 | SyncPolicyManager::getAllIntroducerName() |
| 207 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 208 | shared_ptr<vector<Name> > nameList = make_shared<vector<Name> >(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 209 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 210 | map<string, PublicKey>::iterator it = m_trustedIntroducers.begin(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 211 | for(; it != m_trustedIntroducers.end(); it++) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 212 | nameList->push_back(Name(it->first)); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 213 | |
| 214 | return nameList; |
| 215 | } |
| 216 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 217 | shared_ptr<ValidationRequest> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 218 | SyncPolicyManager::prepareRequest(const Name& keyName, |
| 219 | bool forIntroducer, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 220 | shared_ptr<Data> data, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 221 | const int & stepCount, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 222 | const OnVerified& onVerified, |
| 223 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 224 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 225 | shared_ptr<Name> interestPrefixName = make_shared<Name>(m_syncPrefix); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 226 | interestPrefixName->append("WOT").append(keyName).append("INTRO-CERT"); |
| 227 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 228 | shared_ptr<const vector<Name> > nameList = getAllIntroducerName(); |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 229 | if(0 == nameList->size()) |
| 230 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 231 | onVerifyFailed(data); |
| 232 | return SYNC_POLICY_MANAGER_NULL_VALIDATION_REQUEST_PTR; |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 233 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 234 | |
| 235 | Name interestName = *interestPrefixName; |
| 236 | interestName.append(nameList->at(0)); |
| 237 | |
| 238 | if(forIntroducer) |
| 239 | interestName.append("INTRODUCER"); |
| 240 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 241 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName); |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 242 | // _LOG_DEBUG("send interest for intro cert: " << interest->getName()); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 243 | interest->setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 244 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 245 | OnVerified requestedCertVerifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerified, |
| 246 | this, |
| 247 | _1, |
| 248 | forIntroducer, |
| 249 | data, |
| 250 | onVerified, |
| 251 | onVerifyFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 252 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 253 | OnVerifyFailed requestedCertUnverifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerifyFailed, |
| 254 | this, |
| 255 | _1, |
| 256 | interestPrefixName, |
| 257 | forIntroducer, |
| 258 | nameList, |
| 259 | 1, |
| 260 | data, |
| 261 | onVerified, |
| 262 | onVerifyFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 263 | |
| 264 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 265 | shared_ptr<ValidationRequest> nextStep = make_shared<ValidationRequest>(interest, |
| 266 | requestedCertVerifiedCallback, |
| 267 | requestedCertUnverifiedCallback, |
| 268 | 1, |
| 269 | m_stepLimit-1); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 270 | return nextStep; |
| 271 | } |
| 272 | |
| 273 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 274 | SyncPolicyManager::OnIntroCertInterest(const shared_ptr<const Name>& prefix, |
| 275 | const shared_ptr<const ndn::Interest>& interest, |
| 276 | Transport& transport, |
| 277 | uint64_t registeredPrefixId) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 278 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 279 | map<string, Data>::const_iterator it = m_introCert.find(prefix->toUri()); |
| 280 | |
| 281 | if(m_introCert.end() != it) |
| 282 | { |
| 283 | Blob encodedData = it->second.wireEncode(); |
| 284 | transport.send(*encodedData); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | SyncPolicyManager::OnIntroCertRegisterFailed(const shared_ptr<const Name>& prefix) |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | SyncPolicyManager::onIntroCertVerified(const shared_ptr<Data>& introCertificateData, |
| 295 | bool forIntroducer, |
| 296 | shared_ptr<Data> originalData, |
| 297 | const OnVerified& onVerified, |
| 298 | const OnVerifyFailed& onVerifyFailed) |
| 299 | { |
| 300 | shared_ptr<SyncIntroCertificate> introCertificate = make_shared<SyncIntroCertificate>(*introCertificateData); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 301 | if(forIntroducer) |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 302 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 303 | m_trustedIntroducers.insert(pair <string, PublicKey > (introCertificate->getPublicKeyName().toUri(), |
| 304 | introCertificate->getPublicKeyInfo())); |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 305 | SyncIntroCertificate syncIntroCertificate(m_syncPrefix, |
| 306 | introCertificate->getPublicKeyName(), |
| 307 | m_identityManager->getDefaultKeyNameForIdentity(m_signingIdentity), |
| 308 | introCertificate->getNotBefore(), |
| 309 | introCertificate->getNotAfter(), |
| 310 | introCertificate->getPublicKeyInfo(), |
| 311 | SyncIntroCertificate::INTRODUCER); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 312 | |
| 313 | Name certName = m_identityManager->getDefaultCertificateNameForIdentity(m_signingIdentity); |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 314 | _LOG_DEBUG("Publish Intro Certificate on Verified: " << syncIntroCertificate.getName()); |
| 315 | m_identityManager->signByCertificate(syncIntroCertificate, certName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 316 | |
| 317 | m_transport->send(*syncIntroCertificate.wireEncode()); |
| 318 | |
| 319 | // Name prefix = syncIntroCertificate.getName().getPrefix(syncIntroCertificate.getName().size()-1); |
| 320 | |
| 321 | // map<string, Data>::const_iterator it = m_introCert.find(prefix.toEscapedString()); |
| 322 | // if(m_introCert.end() != it) |
| 323 | // { |
| 324 | // it->second = syncIntroCertificate; |
| 325 | // } |
| 326 | // else |
| 327 | // { |
| 328 | // m_introCert.insert(pair <string, Data> (prefix.toEscapedString(), syncIntroCertificate)); |
| 329 | // m_face->registerPrefix(prefix, |
| 330 | // boost::bind(&SyncPolicyManager::onIntroCertInterest, this, _1, _2, _3, _4), |
| 331 | // boost::bind(&SyncPolicyManager::onIntroCertRegisterFailed, this, _1)); |
| 332 | // } |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 333 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 334 | else |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 335 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 336 | m_trustedProducers.insert(pair <string, PublicKey > (introCertificate->getPublicKeyName().toUri(), |
| 337 | introCertificate->getPublicKeyInfo())); |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 338 | SyncIntroCertificate syncIntroCertificate(m_syncPrefix, |
| 339 | introCertificate->getPublicKeyName(), |
| 340 | m_identityManager->getDefaultKeyNameForIdentity(m_signingIdentity), |
| 341 | introCertificate->getNotBefore(), |
| 342 | introCertificate->getNotAfter(), |
| 343 | introCertificate->getPublicKeyInfo(), |
| 344 | SyncIntroCertificate::PRODUCER); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 345 | |
| 346 | Name certName = m_identityManager->getDefaultCertificateNameForIdentity(m_signingIdentity); |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 347 | _LOG_DEBUG("Publish Intro Certificate on Verified: " << syncIntroCertificate.getName()); |
| 348 | m_identityManager->signByCertificate(syncIntroCertificate, certName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 349 | |
| 350 | m_transport->send(*syncIntroCertificate.wireEncode()); |
| 351 | |
| 352 | // Name prefix = syncIntroCertificate.getName().getPrefix(syncIntroCertificate.getName().size()-1); |
| 353 | |
| 354 | // map<string, Data>::const_iterator it = m_introCert.find(prefix.toEscapedString()); |
| 355 | // if(m_introCert.end() != it) |
| 356 | // { |
| 357 | // it->second = syncIntroCertificate; |
| 358 | // } |
| 359 | // else |
| 360 | // { |
| 361 | // m_introCert.insert(pair <string, Data> (prefix.toEscapedString(), syncIntroCertificate)); |
| 362 | // m_face->registerPrefix(prefix, |
| 363 | // boost::bind(&SyncPolicyManager::onIntroCertInterest, this, _1, _2, _3, _4), |
| 364 | // boost::bind(&SyncPolicyManager::onIntroCertRegisterFailed, this, _1)); |
| 365 | // } |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 366 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 367 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 368 | if(Sha256WithRsaHandler::verifySignature(*originalData, introCertificate->getPublicKeyInfo())) |
| 369 | onVerified(originalData); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 370 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 371 | onVerifyFailed(originalData); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 375 | SyncPolicyManager::onIntroCertVerifyFailed(const shared_ptr<Data>& introCertificateData, |
| 376 | shared_ptr<Name> interestPrefixName, |
| 377 | bool forIntroducer, |
| 378 | shared_ptr<const vector<Name> > introNameList, |
| 379 | int nextIntroducerIndex, |
| 380 | shared_ptr<Data> originalData, |
| 381 | const OnVerified& onVerified, |
| 382 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 383 | { |
| 384 | Name interestName = *interestPrefixName; |
| 385 | if(nextIntroducerIndex < introNameList->size()) |
| 386 | interestName.append(introNameList->at(nextIntroducerIndex)); |
| 387 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 388 | onVerifyFailed(originalData); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 389 | |
| 390 | if(forIntroducer) |
| 391 | interestName.append("INTRODUCER"); |
| 392 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 393 | shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName); |
| 394 | interest->setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 395 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 396 | OnVerified onRecursiveVerified = boost::bind(&SyncPolicyManager::onIntroCertVerified, |
| 397 | this, |
| 398 | _1, |
| 399 | forIntroducer, |
| 400 | originalData, |
| 401 | onVerified, |
| 402 | onVerifyFailed); |
| 403 | |
| 404 | OnVerifyFailed onRecursiveVerifyFailed = boost::bind(&SyncPolicyManager::onIntroCertVerifyFailed, |
| 405 | this, |
| 406 | _1, |
| 407 | interestPrefixName, |
| 408 | forIntroducer, |
| 409 | introNameList, |
| 410 | nextIntroducerIndex + 1, |
| 411 | originalData, |
| 412 | onVerified, |
| 413 | onVerifyFailed); |
| 414 | |
| 415 | m_face->expressInterest(*interest, |
| 416 | boost::bind(&SyncPolicyManager::onIntroCertData, |
| 417 | this, |
| 418 | _1, |
| 419 | _2, |
| 420 | m_stepLimit-1, |
| 421 | onRecursiveVerified, |
| 422 | onRecursiveVerifyFailed, |
| 423 | originalData, |
| 424 | onVerifyFailed), |
| 425 | boost::bind(&SyncPolicyManager::onIntroCertTimeout, |
| 426 | this, |
| 427 | _1, |
| 428 | 1, |
| 429 | m_stepLimit-1, |
| 430 | onRecursiveVerified, |
| 431 | onRecursiveVerifyFailed, |
| 432 | originalData, |
| 433 | onVerifyFailed)); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 437 | SyncPolicyManager::onIntroCertData(const shared_ptr<const ndn::Interest> &interest, |
| 438 | const shared_ptr<Data>& introCertificateData, |
| 439 | int stepCount, |
| 440 | const OnVerified& onRecursiveVerified, |
| 441 | const OnVerifyFailed& onRecursiveVerifyFailed, |
| 442 | shared_ptr<Data> originalData, |
| 443 | const OnVerifyFailed& onVerifyFailed) |
| 444 | { |
| 445 | shared_ptr<ValidationRequest> nextStep = checkVerificationPolicy(introCertificateData, stepCount, onRecursiveVerified, onRecursiveVerifyFailed); |
| 446 | if (nextStep) |
| 447 | m_face->expressInterest |
| 448 | (*nextStep->interest_, |
| 449 | boost::bind(&SyncPolicyManager::onIntroCertData, |
| 450 | this, |
| 451 | _1, |
| 452 | _2, |
| 453 | nextStep->stepCount_, |
| 454 | nextStep->onVerified_, |
| 455 | nextStep->onVerifyFailed_, |
| 456 | introCertificateData, |
| 457 | onRecursiveVerifyFailed), |
| 458 | boost::bind(&SyncPolicyManager::onIntroCertTimeout, |
| 459 | this, |
| 460 | _1, |
| 461 | nextStep->retry_, |
| 462 | nextStep->stepCount_, |
| 463 | nextStep->onVerified_, |
| 464 | nextStep->onVerifyFailed_, |
| 465 | introCertificateData, |
| 466 | onRecursiveVerifyFailed)); |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | SyncPolicyManager::onIntroCertTimeout(const shared_ptr<const ndn::Interest>& interest, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 471 | int retry, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 472 | int stepCount, |
| 473 | const OnVerified& onRecursiveVerified, |
| 474 | const OnVerifyFailed& onRecursiveVerifyFailed, |
| 475 | shared_ptr<Data> originalData, |
| 476 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 477 | { |
| 478 | if(retry > 0) |
| 479 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 480 | m_face->expressInterest(*interest, |
| 481 | boost::bind(&SyncPolicyManager::onIntroCertData, |
| 482 | this, |
| 483 | _1, |
| 484 | _2, |
| 485 | stepCount, |
| 486 | onRecursiveVerified, |
| 487 | onRecursiveVerifyFailed, |
| 488 | originalData, |
| 489 | onVerifyFailed), |
| 490 | boost::bind(&SyncPolicyManager::onIntroCertTimeout, |
| 491 | this, |
| 492 | _1, |
| 493 | retry - 1, |
| 494 | stepCount, |
| 495 | onRecursiveVerified, |
| 496 | onRecursiveVerifyFailed, |
| 497 | originalData, |
| 498 | onVerifyFailed)); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 499 | } |
| 500 | else |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame^] | 501 | onVerifyFailed(originalData); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 502 | } |