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 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 11 | #include "validator-invitation.hpp" |
| 12 | #include "invitation.hpp" |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 13 | |
| 14 | #include "logging.h" |
| 15 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 16 | using namespace ndn; |
| 17 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 18 | namespace chronos { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 19 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 20 | using std::vector; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 21 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 22 | using ndn::CertificateCache; |
| 23 | using ndn::SecRuleRelative; |
| 24 | using ndn::OnDataValidated; |
| 25 | using ndn::OnDataValidationFailed; |
| 26 | using ndn::OnInterestValidated; |
| 27 | using ndn::OnInterestValidationFailed; |
| 28 | using ndn::ValidationRequest; |
| 29 | using ndn::IdentityCertificate; |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 31 | const shared_ptr<CertificateCache> ValidatorInvitation::DefaultCertificateCache = |
| 32 | shared_ptr<CertificateCache>(); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 33 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 34 | ValidatorInvitation::ValidatorInvitation() |
| 35 | : Validator() |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 36 | , m_invitationReplyRule("^([^<CHRONOCHAT-INVITATION>]*)<CHRONOCHAT-INVITATION>", |
| 37 | "^([^<KEY>]*)<KEY>(<>*)[<dsk-.*><ksk-.*>]<ID-CERT>$", |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 38 | "==", "\\1", "\\1\\2", true) |
| 39 | , m_invitationInterestRule("^[^<CHRONOCHAT-INVITATION>]*<CHRONOCHAT-INVITATION><>{6}$") |
| 40 | , m_innerKeyRegex("^([^<KEY>]*)<KEY>(<>*)[<dsk-.*><ksk-.*>]<ID-CERT><>$", "\\1\\2") |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 41 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 42 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 43 | |
| 44 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 45 | ValidatorInvitation::addTrustAnchor(const Name& keyName, const ndn::PublicKey& key) |
| 46 | { |
| 47 | m_trustAnchors[keyName] = key; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | ValidatorInvitation::removeTrustAnchor(const Name& keyName) |
| 52 | { |
| 53 | m_trustAnchors.erase(keyName); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | ValidatorInvitation::cleanTrustAnchor() |
| 58 | { |
| 59 | m_trustAnchors.clear(); |
| 60 | } |
| 61 | |
| 62 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 63 | ValidatorInvitation::checkPolicy (const Data& data, |
| 64 | int stepCount, |
| 65 | const OnDataValidated& onValidated, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 66 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 67 | vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 69 | const Signature& signature = data.getSignature(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 71 | if (signature.getKeyLocator().getType() != KeyLocator::KeyLocator_Name) |
| 72 | return onValidationFailed(data.shared_from_this(), |
| 73 | "Key Locator is not a name: " + data.getName().toUri()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 74 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 75 | const Name & keyLocatorName = signature.getKeyLocator().getName(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 76 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 77 | if (!m_invitationReplyRule.satisfy(data.getName(), keyLocatorName)) |
| 78 | return onValidationFailed(data.shared_from_this(), |
| 79 | "Does not comply with the invitation rule: " + |
| 80 | data.getName().toUri() + " signed by: " + |
| 81 | keyLocatorName.toUri()); |
| 82 | |
| 83 | Data innerData; |
| 84 | innerData.wireDecode(data.getContent().blockFromValue()); |
| 85 | |
| 86 | return internalCheck(data.wireEncode().value(), |
| 87 | data.wireEncode().value_size() - data.getSignature().getValue().size(), |
| 88 | signature, |
| 89 | keyLocatorName, |
| 90 | innerData, |
| 91 | bind(onValidated, data.shared_from_this()), |
| 92 | bind(onValidationFailed, data.shared_from_this(), _1)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 96 | ValidatorInvitation::checkPolicy (const Interest& interest, |
| 97 | int stepCount, |
| 98 | const OnInterestValidated& onValidated, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 99 | const OnInterestValidationFailed& onValidationFailed, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 100 | vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 101 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 102 | const Name& interestName = interest.getName(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 103 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 104 | if (!m_invitationInterestRule.match(interestName)) |
| 105 | return onValidationFailed(interest.shared_from_this(), |
| 106 | "Invalid interest name: " + interest.getName().toUri()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 107 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 108 | Name signedName = interestName.getPrefix(-1); |
| 109 | Buffer signedBlob = Buffer(signedName.wireEncode().value(), |
| 110 | signedName.wireEncode().value_size()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 111 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 112 | Block signatureBlock = interestName.get(Invitation::SIGNATURE).blockFromValue(); |
| 113 | Block signatureInfo = interestName.get(Invitation::KEY_LOCATOR).blockFromValue(); |
| 114 | Signature signature(signatureInfo, signatureBlock); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 115 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 116 | if (signature.getKeyLocator().getType() != KeyLocator::KeyLocator_Name) |
| 117 | return onValidationFailed(interest.shared_from_this(), |
| 118 | "KeyLocator is not a name: " + interest.getName().toUri()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 119 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 120 | const Name & keyLocatorName = signature.getKeyLocator().getName(); |
| 121 | |
| 122 | Data innerData; |
| 123 | innerData.wireDecode(interestName.get(Invitation::INVITER_CERT).blockFromValue()); |
| 124 | |
| 125 | return internalCheck(signedBlob.buf(), signedBlob.size(), |
| 126 | signature, |
| 127 | keyLocatorName, |
| 128 | innerData, |
| 129 | bind(onValidated, interest.shared_from_this()), |
| 130 | bind(onValidationFailed, interest.shared_from_this(), _1)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | ValidatorInvitation::internalCheck(const uint8_t* buf, size_t size, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 135 | const Signature& signature, |
| 136 | const Name& keyLocatorName, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 137 | const Data& innerData, |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 138 | const OnValidated& onValidated, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 139 | const OnValidationFailed& onValidationFailed) |
| 140 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 141 | Name signingKeyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 142 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 143 | TrustAnchors::const_iterator keyIt = m_trustAnchors.find(signingKeyName); |
| 144 | if (keyIt == m_trustAnchors.end()) |
| 145 | return onValidationFailed("Cannot reach any trust anchor"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 146 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 147 | if (!Validator::verifySignature(buf, size, signature, keyIt->second)) |
| 148 | return onValidationFailed("Cannot verify outer signature"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 149 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 150 | // Temporarily disabled, we should get it back when we create a specific key for the chatroom. |
| 151 | // if(!Validator::verifySignature(innerData, m_trustAnchors[signingKeyName])) |
| 152 | // return onValidationFailed("Cannot verify inner signature"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 153 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 154 | if (!m_innerKeyRegex.match(innerData.getName()) || |
| 155 | m_innerKeyRegex.expand() != signingKeyName.getPrefix(-1)) |
| 156 | return onValidationFailed("Inner certificate does not comply with the rule"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 157 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 158 | return onValidated(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 161 | } // namespace chronos |