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 | |
| 11 | #include "sync-policy-manager.h" |
| 12 | |
| 13 | #include "sync-intro-certificate.h" |
| 14 | #include "sync-logging.h" |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame^] | 15 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace ndn; |
| 18 | using namespace ndn::security; |
| 19 | using namespace std; |
| 20 | |
| 21 | INIT_LOGGER("SyncPolicyManager"); |
| 22 | |
| 23 | SyncPolicyManager::SyncPolicyManager(const Name& signingIdentity, |
| 24 | const Name& signingCertificateName, |
| 25 | const Name& syncPrefix, |
| 26 | int stepLimit) |
| 27 | : m_signingIdentity(signingIdentity) |
| 28 | , m_signingCertificateName(signingCertificateName.getPrefix(signingCertificateName.size()-1)) |
| 29 | , m_syncPrefix(syncPrefix) |
| 30 | , m_stepLimit(stepLimit) |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame^] | 31 | , m_identityManager(Ptr<security::IdentityManager>::Create()) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 32 | { |
| 33 | Name wotPrefix = syncPrefix; |
| 34 | wotPrefix.append("WOT"); |
| 35 | m_syncPrefixRegex = Regex::fromName(syncPrefix); |
| 36 | m_wotPrefixRegex = Regex::fromName(wotPrefix); |
Yingdi Yu | 4526a2e | 2013-11-06 16:36:01 -0800 | [diff] [blame] | 37 | m_chatDataPolicy = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^[^<%F0.>]*<%F0.>([^<chronos>]*)<chronos><>", |
Yingdi Yu | 9529bc3 | 2013-11-01 16:19:25 -0700 | [diff] [blame] | 38 | "^([^<KEY>]*)<KEY>(<>*)[<dsk-.*><ksk-.*>]<ID-CERT>$", |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 39 | "==", "\\1", "\\1", true)); |
| 40 | } |
| 41 | |
| 42 | SyncPolicyManager::~SyncPolicyManager() |
| 43 | {} |
| 44 | |
| 45 | bool |
| 46 | SyncPolicyManager::skipVerifyAndTrust (const Data& data) |
| 47 | { return false; } |
| 48 | |
| 49 | bool |
| 50 | SyncPolicyManager::requireVerify (const Data& data) |
| 51 | { return true; } |
| 52 | |
| 53 | Ptr<ValidationRequest> |
| 54 | SyncPolicyManager::checkVerificationPolicy(Ptr<Data> data, |
| 55 | const int& stepCount, |
| 56 | const DataCallback& verifiedCallback, |
| 57 | const UnverifiedCallback& unverifiedCallback) |
| 58 | { |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 59 | // #ifdef _DEBUG |
| 60 | // _LOG_DEBUG("checkVerificationPolicy"); |
| 61 | // verifiedCallback(data); |
| 62 | // return NULL; |
| 63 | // #else |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 64 | //TODO: |
| 65 | if(stepCount > m_stepLimit) |
| 66 | { |
| 67 | unverifiedCallback(data); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | Ptr<const signature::Sha256WithRsa> sha256sig = DynamicCast<const signature::Sha256WithRsa> (data->getSignature()); |
| 72 | if(KeyLocator::KEYNAME != sha256sig->getKeyLocator().getType()) |
| 73 | { |
| 74 | unverifiedCallback(data); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | const Name& keyLocatorName = sha256sig->getKeyLocator().getKeyName(); |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 79 | // _LOG_DEBUG("data name: " << data->getName()); |
| 80 | // _LOG_DEBUG("signer name: " << keyLocatorName); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 81 | |
| 82 | // if data is intro cert |
| 83 | if(m_wotPrefixRegex->match(data->getName())) |
| 84 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 85 | // _LOG_DEBUG("Intro Cert"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 86 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
| 87 | map<Name, Publickey>::const_iterator it = m_trustedIntroducers.find(keyName); |
| 88 | if(m_trustedIntroducers.end() != it) |
| 89 | { |
| 90 | if(verifySignature(*data, it->second)) |
| 91 | verifiedCallback(data); |
| 92 | else |
| 93 | unverifiedCallback(data); |
| 94 | return NULL; |
| 95 | } |
| 96 | else |
| 97 | return prepareRequest(keyName, true, data, stepCount, verifiedCallback, unverifiedCallback); |
| 98 | } |
| 99 | |
| 100 | // if data is sync data or chat data |
| 101 | if(m_syncPrefixRegex->match(data->getName()) || m_chatDataPolicy->satisfy(*data)) |
| 102 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 103 | // _LOG_DEBUG("Sync/Chat Data"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 104 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 105 | // _LOG_DEBUG("keyName: " << keyName.toUri()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 106 | |
| 107 | map<Name, Publickey>::const_iterator it = m_trustedIntroducers.find(keyName); |
| 108 | if(m_trustedIntroducers.end() != it) |
| 109 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 110 | // _LOG_DEBUG("Find trusted introducer!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 111 | if(verifySignature(*data, it->second)) |
| 112 | verifiedCallback(data); |
| 113 | else |
| 114 | unverifiedCallback(data); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | it = m_trustedProducers.find(keyName); |
| 119 | if(m_trustedProducers.end() != it) |
| 120 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 121 | // _LOG_DEBUG("Find trusted producer!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 122 | if(verifySignature(*data, it->second)) |
| 123 | verifiedCallback(data); |
| 124 | else |
| 125 | unverifiedCallback(data); |
| 126 | return NULL; |
| 127 | } |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 128 | |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 129 | // _LOG_DEBUG("Did not find any trusted one!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 130 | return prepareRequest(keyName, false, data, stepCount, verifiedCallback, unverifiedCallback); |
| 131 | } |
| 132 | |
| 133 | unverifiedCallback(data); |
| 134 | return NULL; |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 135 | // #endif |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool |
| 139 | SyncPolicyManager::checkSigningPolicy(const Name& dataName, |
| 140 | const Name& certificateName) |
| 141 | { |
| 142 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 143 | return true; |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 144 | |
| 145 | // #ifdef _DEBUG |
| 146 | // _LOG_DEBUG("checkSigningPolicy"); |
| 147 | // return true; |
| 148 | // #else |
| 149 | // return (m_syncPrefixRegex->match(dataName) && certificateName.getPrefix(certificateName.size()-1) == m_signingCertificateName) ? true : false; |
| 150 | // #endif |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | Name |
| 154 | SyncPolicyManager::inferSigningIdentity(const ndn::Name& dataName) |
| 155 | { return m_signingIdentity; } |
| 156 | |
| 157 | void |
| 158 | SyncPolicyManager::addTrustAnchor(const IdentityCertificate& identityCertificate, bool isIntroducer) |
| 159 | { |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 160 | // _LOG_DEBUG("Add intro/producer: " << identityCertificate.getPublicKeyName()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 161 | if(isIntroducer) |
| 162 | m_trustedIntroducers.insert(pair <Name, Publickey > (identityCertificate.getPublicKeyName(), identityCertificate.getPublicKeyInfo())); |
| 163 | else |
| 164 | m_trustedProducers.insert(pair <Name, Publickey > (identityCertificate.getPublicKeyName(), identityCertificate.getPublicKeyInfo())); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | SyncPolicyManager::addChatDataRule(const Name& prefix, |
| 169 | const IdentityCertificate& identityCertificate, |
| 170 | bool isIntroducer) |
| 171 | { |
| 172 | // Name dataPrefix = prefix; |
| 173 | // dataPrefix.append("chronos").append(m_syncPrefix.get(-1)); |
| 174 | // Ptr<Regex> dataRegex = Regex::fromName(prefix); |
| 175 | // Name certName = identityCertificate.getName(); |
| 176 | // Name signerName = certName.getPrefix(certName.size()-1); |
| 177 | // Ptr<Regex> signerRegex = Regex::fromName(signerName, true); |
| 178 | |
| 179 | // SpecificPolicyRule rule(dataRegex, signerRegex); |
| 180 | // map<Name, SpecificPolicyRule>::iterator it = m_chatDataRules.find(dataPrefix); |
| 181 | // if(it != m_chatDataRules.end()) |
| 182 | // it->second = rule; |
| 183 | // else |
| 184 | // m_chatDataRules.insert(pair <Name, SpecificPolicyRule > (dataPrefix, rule)); |
| 185 | |
| 186 | addTrustAnchor(identityCertificate, isIntroducer); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | Ptr<const vector<Name> > |
| 191 | SyncPolicyManager::getAllIntroducerName() |
| 192 | { |
| 193 | Ptr<vector<Name> > nameList = Ptr<vector<Name> >(new vector<Name>); |
| 194 | |
| 195 | map<Name, Publickey>::iterator it = m_trustedIntroducers.begin(); |
| 196 | for(; it != m_trustedIntroducers.end(); it++) |
| 197 | nameList->push_back(it->first); |
| 198 | |
| 199 | return nameList; |
| 200 | } |
| 201 | |
| 202 | Ptr<ValidationRequest> |
| 203 | SyncPolicyManager::prepareRequest(const Name& keyName, |
| 204 | bool forIntroducer, |
| 205 | Ptr<Data> data, |
| 206 | const int & stepCount, |
| 207 | const DataCallback& verifiedCallback, |
| 208 | const UnverifiedCallback& unverifiedCallback) |
| 209 | { |
| 210 | Ptr<Name> interestPrefixName = Ptr<Name>(new Name(m_syncPrefix)); |
| 211 | interestPrefixName->append("WOT").append(keyName).append("INTRO-CERT"); |
| 212 | |
| 213 | Ptr<const std::vector<ndn::Name> > nameList = getAllIntroducerName(); |
Yingdi Yu | 0b3bd48 | 2013-11-01 16:11:20 -0700 | [diff] [blame] | 214 | if(0 == nameList->size()) |
| 215 | { |
| 216 | unverifiedCallback(data); |
| 217 | return NULL; |
| 218 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 219 | |
| 220 | Name interestName = *interestPrefixName; |
| 221 | interestName.append(nameList->at(0)); |
| 222 | |
| 223 | if(forIntroducer) |
| 224 | interestName.append("INTRODUCER"); |
| 225 | |
| 226 | Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName)); |
Yingdi Yu | 1baf6e0 | 2013-11-07 11:35:32 -0800 | [diff] [blame] | 227 | // _LOG_DEBUG("send interest for intro cert: " << interest->getName()); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 228 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 229 | |
| 230 | DataCallback requestedCertVerifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerified, |
| 231 | this, |
| 232 | _1, |
| 233 | forIntroducer, |
| 234 | data, |
| 235 | verifiedCallback, |
| 236 | unverifiedCallback); |
| 237 | |
| 238 | UnverifiedCallback requestedCertUnverifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertUnverified, |
| 239 | this, |
| 240 | _1, |
| 241 | interestPrefixName, |
| 242 | forIntroducer, |
| 243 | nameList, |
| 244 | 1, |
| 245 | data, |
| 246 | verifiedCallback, |
| 247 | unverifiedCallback); |
| 248 | |
| 249 | |
| 250 | Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, |
| 251 | requestedCertVerifiedCallback, |
| 252 | requestedCertUnverifiedCallback, |
| 253 | 1, |
| 254 | m_stepLimit-1) |
| 255 | ); |
| 256 | return nextStep; |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | SyncPolicyManager::onIntroCertVerified(Ptr<Data> introCertificateData, |
| 261 | bool forIntroducer, |
| 262 | Ptr<Data> originalData, |
| 263 | const DataCallback& verifiedCallback, |
| 264 | const UnverifiedCallback& unverifiedCallback) |
| 265 | { |
| 266 | Ptr<SyncIntroCertificate> introCertificate = Ptr<SyncIntroCertificate>(new SyncIntroCertificate(*introCertificateData)); |
| 267 | if(forIntroducer) |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame^] | 268 | { |
| 269 | m_trustedIntroducers.insert(pair <Name, Publickey > (introCertificate->getPublicKeyName(), introCertificate->getPublicKeyInfo())); |
| 270 | SyncIntroCertificate syncIntroCertificate(m_syncPrefix, |
| 271 | introCertificate->getPublicKeyName(), |
| 272 | m_identityManager->getDefaultKeyNameForIdentity(m_signingIdentity), |
| 273 | introCertificate->getNotBefore(), |
| 274 | introCertificate->getNotAfter(), |
| 275 | introCertificate->getPublicKeyInfo(), |
| 276 | SyncIntroCertificate::INTRODUCER); |
| 277 | // : SyncIntroCertificate::PRODUCER) |
| 278 | ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_signingIdentity); |
| 279 | _LOG_DEBUG("Publish Intro Certificate on Verified: " << syncIntroCertificate.getName()); |
| 280 | m_identityManager->signByCertificate(syncIntroCertificate, certName); |
| 281 | m_handler->putToNdnd(*syncIntroCertificate.encodeToWire()); |
| 282 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 283 | else |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame^] | 284 | { |
| 285 | m_trustedProducers.insert(pair <Name, Publickey > (introCertificate->getPublicKeyName(), introCertificate->getPublicKeyInfo())); |
| 286 | SyncIntroCertificate syncIntroCertificate(m_syncPrefix, |
| 287 | introCertificate->getPublicKeyName(), |
| 288 | m_identityManager->getDefaultKeyNameForIdentity(m_signingIdentity), |
| 289 | introCertificate->getNotBefore(), |
| 290 | introCertificate->getNotAfter(), |
| 291 | introCertificate->getPublicKeyInfo(), |
| 292 | SyncIntroCertificate::PRODUCER); |
| 293 | // : SyncIntroCertificate::PRODUCER) |
| 294 | ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_signingIdentity); |
| 295 | _LOG_DEBUG("Publish Intro Certificate on Verified: " << syncIntroCertificate.getName()); |
| 296 | m_identityManager->signByCertificate(syncIntroCertificate, certName); |
| 297 | m_handler->putToNdnd(*syncIntroCertificate.encodeToWire()); |
| 298 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 299 | |
| 300 | if(verifySignature(*originalData, introCertificate->getPublicKeyInfo())) |
| 301 | verifiedCallback(originalData); |
| 302 | else |
| 303 | unverifiedCallback(originalData); |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | SyncPolicyManager::onIntroCertUnverified(Ptr<Data> introCertificateData, |
| 308 | Ptr<Name> interestPrefixName, |
| 309 | bool forIntroducer, |
| 310 | Ptr<const std::vector<ndn::Name> > introNameList, |
| 311 | const int& nextIntroducerIndex, |
| 312 | Ptr<Data> originalData, |
| 313 | const DataCallback& verifiedCallback, |
| 314 | const UnverifiedCallback& unverifiedCallback) |
| 315 | { |
| 316 | Name interestName = *interestPrefixName; |
| 317 | if(nextIntroducerIndex < introNameList->size()) |
| 318 | interestName.append(introNameList->at(nextIntroducerIndex)); |
| 319 | else |
| 320 | unverifiedCallback(originalData); |
| 321 | |
| 322 | if(forIntroducer) |
| 323 | interestName.append("INTRODUCER"); |
| 324 | |
| 325 | Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName)); |
| 326 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 327 | |
| 328 | DataCallback requestedCertVerifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerified, |
| 329 | this, |
| 330 | _1, |
| 331 | forIntroducer, |
| 332 | originalData, |
| 333 | verifiedCallback, |
| 334 | unverifiedCallback); |
| 335 | |
| 336 | UnverifiedCallback requestedCertUnverifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertUnverified, |
| 337 | this, |
| 338 | _1, |
| 339 | interestPrefixName, |
| 340 | forIntroducer, |
| 341 | introNameList, |
| 342 | nextIntroducerIndex + 1, |
| 343 | originalData, |
| 344 | verifiedCallback, |
| 345 | unverifiedCallback); |
| 346 | |
| 347 | TimeoutCallback requestedCertTimeoutCallback = boost::bind(&SyncPolicyManager::onIntroCertTimeOut, |
| 348 | this, |
| 349 | _1, |
| 350 | _2, |
| 351 | 1, |
| 352 | requestedCertUnverifiedCallback, |
| 353 | originalData); |
| 354 | |
| 355 | Ptr<Closure> closure = Ptr<Closure> (new Closure(requestedCertVerifiedCallback, |
| 356 | requestedCertTimeoutCallback, |
| 357 | requestedCertUnverifiedCallback, |
| 358 | m_stepLimit-1) |
| 359 | ); |
| 360 | |
| 361 | m_handler->sendInterest(interest, closure); |
| 362 | } |
| 363 | |
| 364 | void |
| 365 | SyncPolicyManager::onIntroCertTimeOut(Ptr<Closure> closure, |
| 366 | Ptr<Interest> interest, |
| 367 | int retry, |
| 368 | const UnverifiedCallback& unverifiedCallback, |
| 369 | Ptr<Data> data) |
| 370 | { |
| 371 | if(retry > 0) |
| 372 | { |
| 373 | Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback, |
| 374 | boost::bind(&SyncPolicyManager::onIntroCertTimeOut, |
| 375 | this, |
| 376 | _1, |
| 377 | _2, |
| 378 | retry - 1, |
| 379 | unverifiedCallback, |
| 380 | data), |
| 381 | closure->m_unverifiedCallback, |
| 382 | closure->m_stepCount) |
| 383 | ); |
| 384 | m_handler->sendInterest(interest, newClosure); |
| 385 | } |
| 386 | else |
| 387 | unverifiedCallback(data); |
| 388 | } |