Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -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 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 9 | #include "common.hpp" |
| 10 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 11 | #include "validator.hpp" |
| 12 | #include "../util/logging.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 13 | #include "../util/crypto.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 14 | |
| 15 | #include <cryptopp/rsa.h> |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 16 | #include <cryptopp/files.h> |
| 17 | #include <cryptopp/hex.h> |
| 18 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace std; |
| 21 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 22 | INIT_LOGGER("ndn.Validator"); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | const shared_ptr<Face> Validator::DefaultFace = shared_ptr<Face>(); |
| 27 | |
| 28 | Validator::Validator(shared_ptr<Face> face /* = DefaultFace */) |
| 29 | : m_face(face) |
| 30 | {} |
| 31 | |
| 32 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 33 | Validator::validate(const Interest& interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 34 | const OnInterestValidated &onValidated, |
| 35 | const OnInterestValidationFailed &onValidationFailed, |
| 36 | int stepCount) |
| 37 | { |
| 38 | vector<shared_ptr<ValidationRequest> > nextSteps; |
| 39 | checkPolicy(interest, stepCount, onValidated, onValidationFailed, nextSteps); |
| 40 | |
| 41 | if (!nextSteps.empty()) |
| 42 | { |
| 43 | if(!static_cast<bool>(m_face)) |
| 44 | throw Error("Face should be set prior to verify method to call"); |
| 45 | |
| 46 | vector<shared_ptr<ValidationRequest> >::const_iterator it = nextSteps.begin(); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 47 | OnFailure onFailure = bind(onValidationFailed, interest.shared_from_this(), _1); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 48 | for(; it != nextSteps.end(); it++) |
| 49 | m_face->expressInterest((*it)->m_interest, |
| 50 | bind(&Validator::onData, this, _1, _2, *it), |
| 51 | bind(&Validator::onTimeout, |
| 52 | this, _1, (*it)->m_retry, |
| 53 | onFailure, |
| 54 | *it)); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | //If there is no nextStep, that means InterestPolicy has already been able to verify the Interest. |
| 59 | //No more further processes. |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 64 | Validator::validate(const Data& data, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 65 | const OnDataValidated &onValidated, |
| 66 | const OnDataValidationFailed &onValidationFailed, |
| 67 | int stepCount) |
| 68 | { |
| 69 | vector<shared_ptr<ValidationRequest> > nextSteps; |
| 70 | checkPolicy(data, stepCount, onValidated, onValidationFailed, nextSteps); |
| 71 | |
| 72 | if (!nextSteps.empty()) |
| 73 | { |
| 74 | if(!static_cast<bool>(m_face)) |
| 75 | throw Error("Face should be set prior to verify method to call"); |
| 76 | |
| 77 | vector<shared_ptr<ValidationRequest> >::const_iterator it = nextSteps.begin(); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 78 | OnFailure onFailure = bind(onValidationFailed, data.shared_from_this(), _1); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 79 | for(; it != nextSteps.end(); it++) |
| 80 | m_face->expressInterest((*it)->m_interest, |
| 81 | bind(&Validator::onData, this, _1, _2, *it), |
| 82 | bind(&Validator::onTimeout, |
| 83 | this, _1, (*it)->m_retry, |
| 84 | onFailure, |
| 85 | *it)); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | //If there is no nextStep, that means InterestPolicy has already been able to verify the Interest. |
| 90 | //No more further processes. |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 95 | Validator::onData(const Interest& interest, |
| 96 | Data& data, |
| 97 | const shared_ptr<ValidationRequest>& nextStep) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 98 | { |
| 99 | validate(data, nextStep->m_onValidated, nextStep->m_onDataValidated, nextStep->m_stepCount); |
| 100 | } |
| 101 | |
| 102 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 103 | Validator::onTimeout(const Interest& interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 104 | int retry, |
| 105 | const OnFailure &onFailure, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 106 | const shared_ptr<ValidationRequest>& nextStep) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 107 | { |
| 108 | if (retry > 0) |
| 109 | // Issue the same expressInterest except decrement retry. |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 110 | m_face->expressInterest(interest, |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 111 | bind(&Validator::onData, this, _1, _2, nextStep), |
| 112 | bind(&Validator::onTimeout, this, _1, retry - 1, onFailure, nextStep)); |
| 113 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 114 | onFailure("Cannot fetch cert: " + interest.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool |
| 118 | Validator::verifySignature(const Data& data, const PublicKey& key) |
| 119 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 120 | try |
| 121 | { |
| 122 | switch(data.getSignature().getType()){ |
| 123 | case Signature::Sha256WithRsa: |
| 124 | { |
| 125 | SignatureSha256WithRsa sigSha256Rsa(data.getSignature()); |
| 126 | return verifySignature(data, sigSha256Rsa, key); |
| 127 | } |
| 128 | default: |
| 129 | { |
| 130 | _LOG_DEBUG("verifySignature: Unknown signature type: " << data.getSignature().getType()); |
| 131 | return false; |
| 132 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 135 | catch(Signature::Error &e) |
| 136 | { |
| 137 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 138 | return false; |
| 139 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
| 143 | bool |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 144 | Validator::verifySignature(const Interest& interest, const PublicKey& key) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 145 | { |
| 146 | const Name &interestName = interest.getName(); |
| 147 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 148 | if(interestName.size() < 2) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 149 | return false; |
| 150 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 151 | try |
| 152 | { |
| 153 | const Block& nameBlock = interestName.wireEncode(); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 154 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 155 | Signature sig(interestName[-2].blockFromValue(), |
| 156 | interestName[-1].blockFromValue()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 157 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 158 | switch(sig.getType()){ |
| 159 | case Signature::Sha256WithRsa: |
| 160 | { |
| 161 | SignatureSha256WithRsa sigSha256Rsa(sig); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 162 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 163 | return verifySignature(nameBlock.value(), |
| 164 | nameBlock.value_size() - interestName[-1].size(), |
| 165 | sigSha256Rsa, key); |
| 166 | } |
| 167 | default: |
| 168 | { |
| 169 | _LOG_DEBUG("verifySignature: Unknown signature type: " << sig.getType()); |
| 170 | return false; |
| 171 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 174 | catch(Signature::Error &e) |
| 175 | { |
| 176 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 177 | return false; |
| 178 | } |
| 179 | catch(Block::Error &e) |
| 180 | { |
| 181 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 182 | return false; |
| 183 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 188 | Validator::verifySignature(const Buffer& data, const Signature& sig, const PublicKey& key) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 189 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 190 | try |
| 191 | { |
| 192 | switch(sig.getType()){ |
| 193 | case Signature::Sha256WithRsa: |
| 194 | { |
| 195 | SignatureSha256WithRsa sigSha256Rsa(sig); |
| 196 | return verifySignature(data, sigSha256Rsa, key); |
| 197 | } |
| 198 | default: |
| 199 | { |
| 200 | _LOG_DEBUG("verifySignature: Unknown signature type: " << sig.getType()); |
| 201 | return false; |
| 202 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 205 | catch(Signature::Error &e) |
| 206 | { |
| 207 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 208 | return false; |
| 209 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
| 213 | bool |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 214 | Validator::verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256WithRsa& sig, const PublicKey& key) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 215 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 216 | try |
| 217 | { |
| 218 | using namespace CryptoPP; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 219 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 220 | RSA::PublicKey publicKey; |
| 221 | ByteQueue queue; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 222 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 223 | queue.Put(reinterpret_cast<const byte*>(key.get().buf()), key.get().size()); |
| 224 | publicKey.Load(queue); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 225 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 226 | RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey); |
| 227 | return verifier.VerifyMessage(buf, size, sig.getValue().value(), sig.getValue().value_size()); |
| 228 | } |
| 229 | catch(CryptoPP::Exception& e) |
| 230 | { |
| 231 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 232 | return false; |
| 233 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 236 | bool |
| 237 | Validator::verifySignature(const uint8_t* buf, const size_t size, const SignatureSha256& sig) |
| 238 | { |
| 239 | try |
| 240 | { |
| 241 | ConstBufferPtr buffer = crypto::sha256(buf, size); |
| 242 | const Block& sigValue = sig.getValue(); |
| 243 | |
| 244 | if(static_cast<bool>(buffer) |
| 245 | && buffer->size() == sigValue.value_size() |
Yingdi Yu | 110881d | 2014-03-04 18:27:29 -0800 | [diff] [blame] | 246 | && buffer->size() == crypto::SHA256_DIGEST_SIZE) |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 247 | { |
| 248 | |
| 249 | const uint8_t* p1 = buffer->buf(); |
| 250 | const uint8_t* p2 = sigValue.value(); |
| 251 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame^] | 252 | for(size_t i = 0; i < crypto::SHA256_DIGEST_SIZE; i++) |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 253 | if(p1[i] != p2[i]) |
| 254 | return false; |
| 255 | return true; |
| 256 | } |
| 257 | else |
| 258 | return false; |
| 259 | } |
| 260 | catch(CryptoPP::Exception& e) |
| 261 | { |
| 262 | _LOG_DEBUG("verifySignature: " << e.what()); |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 267 | } // namespace ndn |