Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -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 "panel-policy-manager.h" |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 12 | #include "null-ptrs.h" |
| 13 | #include <ndn-cpp/sha256-with-rsa-signature.hpp> |
| 14 | #include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp> |
| 15 | // #include <boost/bind.hpp> |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "logging.h" |
| 18 | |
| 19 | using namespace std; |
| 20 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 21 | using namespace ndn::ptr_lib; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 22 | |
| 23 | INIT_LOGGER("PanelPolicyManager"); |
| 24 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 25 | PanelPolicyManager::PanelPolicyManager(const int & stepLimit) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 26 | : m_stepLimit(stepLimit) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 27 | , m_certificateCache() |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 28 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 29 | m_localPrefixRegex = make_shared<Regex>("^<local><ndn><prefix><><>$"); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 31 | m_invitationDataSigningRule = make_shared<IdentityPolicyRule>("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", |
| 32 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT><>$", |
| 33 | "==", "\\1", "\\1\\2", true); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 34 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 35 | m_dskRule = make_shared<IdentityPolicyRule>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT><>$", |
| 36 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 37 | "==", "\\1", "\\1\\2", true); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 38 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 39 | m_endorseeRule = make_shared<IdentityPolicyRule>("^([^<DNS>]*)<DNS><>*<ENDORSEE><>$", |
| 40 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 41 | "==", "\\1", "\\1\\2", true); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 42 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 43 | m_kskRegex = make_shared<Regex>("^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT><>$", "\\1\\2"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 45 | m_keyNameRegex = make_shared<Regex>("^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", "\\1\\2"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 46 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 47 | m_signingCertificateRegex = make_shared<Regex>("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", "\\1"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool |
| 51 | PanelPolicyManager::skipVerifyAndTrust (const Data & data) |
| 52 | { |
| 53 | if(m_localPrefixRegex->match(data.getName())) |
| 54 | return true; |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | bool |
| 60 | PanelPolicyManager::requireVerify (const Data & data) |
| 61 | { |
| 62 | // if(m_invitationDataRule->matchDataName(data)) |
| 63 | // return true; |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 64 | if(m_kskRegex->match(data.getName())) |
| 65 | return true; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 66 | if(m_dskRule->matchDataName(data)) |
| 67 | return true; |
| 68 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 69 | if(m_endorseeRule->matchDataName(data)) |
| 70 | return true; |
| 71 | |
| 72 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 76 | shared_ptr<ValidationRequest> |
| 77 | PanelPolicyManager::checkVerificationPolicy(const shared_ptr<Data>& data, |
| 78 | int stepCount, |
| 79 | const OnVerified& onVerified, |
| 80 | const OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 81 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 82 | if(m_stepLimit == stepCount) |
| 83 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 84 | _LOG_ERROR("Reach the maximum steps of verification!"); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 85 | onVerifyFailed(data); |
| 86 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 89 | const Sha256WithRsaSignature* sha256sig = dynamic_cast<const Sha256WithRsaSignature*>(data->getSignature()); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 91 | if(ndn_KeyLocatorType_KEYNAME != sha256sig->getKeyLocator().getType()) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 92 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 93 | _LOG_ERROR("Keylocator is not name!"); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 94 | onVerifyFailed(data); |
| 95 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName(); |
| 99 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 100 | if(m_kskRegex->match(data->getName())) |
| 101 | { |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 102 | Name keyName = m_kskRegex->expand(); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 103 | map<Name, PublicKey>::iterator it = m_trustAnchors.find(keyName); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 104 | if(m_trustAnchors.end() != it) |
| 105 | { |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 106 | // _LOG_DEBUG("found key!"); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 107 | IdentityCertificate identityCertificate(*data); |
| 108 | if(isSameKey(it->second.getKeyDer(), identityCertificate.getPublicKeyInfo().getKeyDer())) |
| 109 | onVerified(data); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 110 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 111 | onVerifyFailed(data); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 112 | } |
| 113 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 114 | onVerifyFailed(data); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 115 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 116 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 117 | } |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 118 | |
| 119 | if(m_dskRule->satisfy(*data)) |
| 120 | { |
| 121 | m_keyNameRegex->match(keyLocatorName); |
| 122 | Name keyName = m_keyNameRegex->expand(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 123 | |
| 124 | if(m_trustAnchors.end() != m_trustAnchors.find(keyName)) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 125 | if(Sha256WithRsaHandler::verifySignature(*data, m_trustAnchors[keyName])) |
| 126 | onVerified(data); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 127 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 128 | onVerifyFailed(data); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 129 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 130 | onVerifyFailed(data); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 132 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 133 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 134 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 135 | if(m_endorseeRule->satisfy(*data)) |
| 136 | { |
| 137 | m_keyNameRegex->match(keyLocatorName); |
| 138 | Name keyName = m_keyNameRegex->expand(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 139 | if(m_trustAnchors.end() != m_trustAnchors.find(keyName)) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 140 | if(Sha256WithRsaHandler::verifySignature(*data, m_trustAnchors[keyName])) |
| 141 | onVerified(data); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 142 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 143 | onVerifyFailed(data); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 144 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 145 | onVerifyFailed(data); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 146 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 147 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 150 | _LOG_DEBUG("Unverified!"); |
| 151 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 152 | onVerifyFailed(data); |
| 153 | return CHRONOCHAT_NULL_VALIDATIONREQUEST_PTR; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 156 | bool |
| 157 | PanelPolicyManager::checkSigningPolicy(const Name & dataName, const Name & certificateName) |
| 158 | { |
| 159 | return m_invitationDataSigningRule->satisfy(dataName, certificateName); |
| 160 | } |
| 161 | |
| 162 | Name |
| 163 | PanelPolicyManager::inferSigningIdentity(const Name & dataName) |
| 164 | { |
| 165 | if(m_signingCertificateRegex->match(dataName)) |
| 166 | return m_signingCertificateRegex->expand(); |
| 167 | else |
| 168 | return Name(); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | PanelPolicyManager::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate) |
| 173 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 174 | // _LOG_DEBUG("Add Anchor: " << selfEndorseCertificate.getPublicKeyName().toUri()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 175 | m_trustAnchors.insert(pair <Name, PublicKey > (selfEndorseCertificate.getPublicKeyName(), selfEndorseCertificate.getPublicKeyInfo())); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 176 | } |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 177 | |
Yingdi Yu | 6ea54e4 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 178 | void |
| 179 | PanelPolicyManager::removeTrustAnchor(const Name& keyName) |
| 180 | { |
| 181 | m_trustAnchors.erase(keyName); |
| 182 | } |
| 183 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 184 | shared_ptr<PublicKey> |
| 185 | PanelPolicyManager::getTrustedKey(const Name& inviterCertName) |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 186 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 187 | Name keyLocatorName = inviterCertName.getPrefix(-1); |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 188 | m_keyNameRegex->match(keyLocatorName); |
| 189 | Name keyName = m_keyNameRegex->expand(); |
| 190 | |
| 191 | if(m_trustAnchors.end() != m_trustAnchors.find(keyName)) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 192 | return make_shared<PublicKey>(m_trustAnchors[keyName]); |
| 193 | return CHRONOCHAT_NULL_PUBLICKEY_PTR; |
| 194 | } |
| 195 | |
| 196 | bool |
| 197 | PanelPolicyManager::isSameKey(const Blob& keyA, const Blob& keyB) |
| 198 | { |
| 199 | size_t size = keyA.size(); |
| 200 | |
| 201 | if(size != keyB.size()) |
| 202 | return false; |
| 203 | |
| 204 | const uint8_t* ap = keyA.buf(); |
| 205 | const uint8_t* bp = keyB.buf(); |
| 206 | |
| 207 | for(int i = 0; i < size; i++) |
| 208 | { |
| 209 | if(ap[i] != bp[i]) |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return true; |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 214 | } |