Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [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 "validator-panel.h" |
| 12 | |
| 13 | #include "logging.h" |
| 14 | |
| 15 | using namespace std; |
| 16 | using namespace ndn; |
| 17 | using namespace ndn::ptr_lib; |
| 18 | |
| 19 | INIT_LOGGER("ValidatorPanel"); |
| 20 | |
| 21 | namespace chronos{ |
| 22 | |
| 23 | const shared_ptr<CertificateCache> ValidatorPanel::DEFAULT_CERT_CACHE = shared_ptr<CertificateCache>(); |
| 24 | |
| 25 | ValidatorPanel::ValidatorPanel(int stepLimit /* = 10 */, |
| 26 | const shared_ptr<CertificateCache> certificateCache/* = DEFAULT_CERT_CACHE */) |
| 27 | : m_stepLimit(stepLimit) |
| 28 | , m_certificateCache(certificateCache) |
| 29 | { |
| 30 | m_endorseeRule = make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><>*<ENDORSEE><>$", |
| 31 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 32 | "==", "\\1", "\\1\\2", true); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | void |
| 38 | ValidatorPanel::checkPolicy (const Data& data, |
| 39 | int stepCount, |
| 40 | const OnDataValidated& onValidated, |
| 41 | const OnDataValidationFailed& onValidationFailed, |
| 42 | vector<shared_ptr<ValidationRequest> >& nextSteps) |
| 43 | { |
| 44 | if(m_stepLimit == stepCount) |
| 45 | { |
| 46 | _LOG_ERROR("Reach the maximum steps of verification!"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 47 | onValidationFailed(data.shared_from_this(), |
| 48 | "Reach maximum validation steps: " + data.getName().toUri()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | try |
| 53 | { |
| 54 | SignatureSha256WithRsa sig(data.getSignature()); |
| 55 | const Name& keyLocatorName = sig.getKeyLocator().getName(); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 56 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 57 | if(m_endorseeRule->satisfy(data.getName(), keyLocatorName)) |
| 58 | { |
| 59 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 60 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | if(m_trustAnchors.end() != m_trustAnchors.find(keyName) && Validator::verifySignature(data, sig, m_trustAnchors[keyName])) |
| 62 | onValidated(data.shared_from_this()); |
| 63 | else |
| 64 | onValidationFailed(data.shared_from_this(), "Cannot verify signature:" + data.getName().toUri()); |
| 65 | } |
| 66 | else |
| 67 | onValidationFailed(data.shared_from_this(), "Does not satisfy rule: " + data.getName().toUri()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 68 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | catch(SignatureSha256WithRsa::Error &e) |
| 72 | { |
| 73 | return onValidationFailed(data.shared_from_this(), |
| 74 | "Not SignatureSha256WithRsa signature: " + data.getName().toUri()); |
| 75 | } |
| 76 | catch(KeyLocator::Error &e) |
| 77 | { |
| 78 | return onValidationFailed(data.shared_from_this(), |
| 79 | "Key Locator is not a name: " + data.getName().toUri()); |
| 80 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | }//chronos |