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