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