Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | ba16b8f | 2013-12-16 13:11:47 -0800 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/security/key-chain.hpp> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 11 | #include <ndn-cpp/security/policy/policy-manager.hpp> |
| 12 | |
Alexander Afanasyev | 6be1a6a | 2014-01-06 00:08:14 -0800 | [diff] [blame] | 13 | #include <ndn-cpp/security/identity/basic-identity-storage.hpp> |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 14 | |
| 15 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 16 | using namespace std; |
Jeff Thompson | 2381da8 | 2013-11-06 14:34:09 -0800 | [diff] [blame] | 17 | using namespace ndn::func_lib; |
Alexander Afanasyev | 6be1a6a | 2014-01-06 00:08:14 -0800 | [diff] [blame] | 18 | #if NDN_CPP_HAVE_CXX11 |
Jeff Thompson | 09324ed | 2013-11-06 14:40:35 -0800 | [diff] [blame] | 19 | // In the std library, the placeholders are in a different namespace than boost. |
| 20 | using namespace ndn::func_lib::placeholders; |
Jeff Thompson | 9bdeb6d | 2013-11-06 14:30:07 -0800 | [diff] [blame] | 21 | #endif |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 22 | |
| 23 | namespace ndn { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 25 | const ptr_lib::shared_ptr<IdentityStorage> KeyChain::DefaultIdentityStorage = ptr_lib::shared_ptr<IdentityStorage>(); |
| 26 | const ptr_lib::shared_ptr<PrivateKeyStorage> KeyChain::DefaultPrivateKeyStorage = ptr_lib::shared_ptr<PrivateKeyStorage>(); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 27 | const ptr_lib::shared_ptr<PolicyManager> KeyChain::DefaultPolicyManager = ptr_lib::shared_ptr<PolicyManager>(); |
| 28 | const ptr_lib::shared_ptr<EncryptionManager> KeyChain::DefaultEncryptionManager = ptr_lib::shared_ptr<EncryptionManager>(); |
| 29 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 30 | KeyChain::KeyChain(const ptr_lib::shared_ptr<IdentityStorage> &publicInfoStorage /* = DefaultIdentityStorage */, |
| 31 | const ptr_lib::shared_ptr<PrivateKeyStorage> &privateKeyStorage /* = DefaultPrivateKeyStorage */, |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 32 | const ptr_lib::shared_ptr<PolicyManager> &policyManager /* = DefaultPolicyManager */, |
| 33 | const ptr_lib::shared_ptr<EncryptionManager> &encryptionManager /* = DefaultEncryptionManager */) |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 34 | : publicInfoStorage_(publicInfoStorage) |
| 35 | , privateKeyStorage_(privateKeyStorage) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 36 | , policyManager_(policyManager) |
| 37 | , encryptionManager_(encryptionManager) |
Alexander Afanasyev | 736708b | 2014-01-06 14:45:34 -0800 | [diff] [blame] | 38 | // , maxSteps_(100) |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 39 | { |
| 40 | if (publicInfoStorage_ == DefaultIdentityStorage) |
| 41 | { |
| 42 | publicInfoStorage_ = ptr_lib::make_shared<BasicIdentityStorage>(); |
| 43 | } |
| 44 | |
| 45 | if (privateKeyStorage_ == DefaultPrivateKeyStorage) |
| 46 | { |
| 47 | #ifdef USE_OSX_PRIVATEKEY_STORAGE |
| 48 | privateStorage_ = ptr_lib::make_shared<OSXPrivatekeyStorage>(); |
| 49 | // #else |
| 50 | // m_privateStorage = Ptr<SimpleKeyStore>::Create(); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | identityManager_ = ptr_lib::make_shared<IdentityManager>(publicInfoStorage_, privateKeyStorage_); |
| 55 | |
| 56 | if (policyManager_ == DefaultPolicyManager) |
| 57 | { |
| 58 | // #ifdef USE_SIMPLE_POLICY_MANAGER |
| 59 | // Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager()); |
| 60 | // Ptr<IdentityPolicyRule> rule1 = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
| 61 | // "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
| 62 | // ">", "\\1\\2", "\\1", true)); |
| 63 | // Ptr<IdentityPolicyRule> rule2 = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
| 64 | // "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
| 65 | // "==", "\\1", "\\1\\2", true)); |
| 66 | // Ptr<IdentityPolicyRule> rule3 = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^(<>*)$", |
| 67 | // "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
| 68 | // ">", "\\1", "\\1", true)); |
| 69 | // policyManager->addVerificationPolicyRule(rule1); |
| 70 | // policyManager->addVerificationPolicyRule(rule2); |
| 71 | // policyManager->addVerificationPolicyRule(rule3); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 73 | // policyManager->addSigningPolicyRule(rule3); |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 75 | // m_policyManager = policyManager; |
| 76 | // |
| 77 | // #else |
| 78 | // policyManager_ = new NoVerifyPolicyManager(); |
| 79 | // #endif |
| 80 | } |
| 81 | |
| 82 | if (encryptionManager_ == DefaultEncryptionManager) |
| 83 | { |
| 84 | } |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 85 | |
| 86 | // #ifdef USE_BASIC_ENCRYPTION_MANAGER |
| 87 | // encryptionManager_ = new BasicEncryptionManager(m_identityManager->getPrivateStorage(), "/tmp/encryption.db"); |
| 88 | // #endif |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 92 | void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 93 | KeyChain::signByIdentity(Data& data, const Name& identityName) |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 94 | { |
| 95 | Name signingCertificateName; |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 97 | if (identityName.getComponentCount() == 0) { |
| 98 | Name inferredIdentity = policyManager_->inferSigningIdentity(data.getName()); |
| 99 | if (inferredIdentity.getComponentCount() == 0) |
| 100 | signingCertificateName = identityManager_->getDefaultCertificateName(); |
| 101 | else |
| 102 | signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(inferredIdentity); |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 103 | } |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 104 | else |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 105 | signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName); |
| 106 | |
| 107 | if (signingCertificateName.getComponentCount() == 0) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 108 | throw Error("No qualified certificate name found!"); |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 109 | |
| 110 | if (!policyManager_->checkSigningPolicy(data.getName(), signingCertificateName)) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 111 | throw Error("Signing Cert name does not comply with signing policy"); |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 113 | identities().signByCertificate(data, signingCertificateName); |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 116 | Signature |
Jeff Thompson | e9ffe79 | 2013-10-22 10:58:48 -0700 | [diff] [blame] | 117 | KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName) |
| 118 | { |
| 119 | Name signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 120 | |
Jeff Thompson | e9ffe79 | 2013-10-22 10:58:48 -0700 | [diff] [blame] | 121 | if (signingCertificateName.size() == 0) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 122 | throw Error("No qualified certificate name found!"); |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 124 | return identities().signByCertificate(buffer, bufferLength, signingCertificateName); |
Jeff Thompson | e9ffe79 | 2013-10-22 10:58:48 -0700 | [diff] [blame] | 125 | } |
Jeff Thompson | c01e178 | 2013-10-21 14:08:42 -0700 | [diff] [blame] | 126 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 127 | void |
Jeff Thompson | 7c5d231 | 2013-09-25 16:07:15 -0700 | [diff] [blame] | 128 | KeyChain::verifyData |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 129 | (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount) |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 130 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 131 | if (policies().requireVerify(*data)) { |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 132 | ptr_lib::shared_ptr<ValidationRequest> nextStep = policyManager_->checkVerificationPolicy |
Jeff Thompson | f309aa6 | 2013-10-31 17:03:54 -0700 | [diff] [blame] | 133 | (data, stepCount, onVerified, onVerifyFailed); |
Jeff Thompson | cda349e | 2013-11-05 17:37:39 -0800 | [diff] [blame] | 134 | if (nextStep) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 135 | { |
| 136 | if (!face_) |
| 137 | throw Error("Face should be set prior to verifyData method to call"); |
| 138 | |
| 139 | face_->expressInterest |
| 140 | (*nextStep->interest_, |
| 141 | bind(&KeyChain::onCertificateData, this, _1, _2, nextStep), |
| 142 | bind(&KeyChain::onCertificateInterestTimeout, this, _1, nextStep->retry_, onVerifyFailed, data, nextStep)); |
| 143 | } |
Jeff Thompson | f309aa6 | 2013-10-31 17:03:54 -0700 | [diff] [blame] | 144 | } |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 145 | else if (policies().skipVerifyAndTrust(*data)) |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 146 | onVerified(data); |
| 147 | else |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 148 | onVerifyFailed(data); |
Jeff Thompson | 1e90d8c | 2013-08-12 16:09:25 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Thompson | cda349e | 2013-11-05 17:37:39 -0800 | [diff] [blame] | 151 | void |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 152 | KeyChain::onCertificateData(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep) |
Jeff Thompson | cda349e | 2013-11-05 17:37:39 -0800 | [diff] [blame] | 153 | { |
| 154 | // Try to verify the certificate (data) according to the parameters in nextStep. |
| 155 | verifyData(data, nextStep->onVerified_, nextStep->onVerifyFailed_, nextStep->stepCount_); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | KeyChain::onCertificateInterestTimeout |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 160 | (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed, const ptr_lib::shared_ptr<Data> &data, |
| 161 | ptr_lib::shared_ptr<ValidationRequest> nextStep) |
Jeff Thompson | cda349e | 2013-11-05 17:37:39 -0800 | [diff] [blame] | 162 | { |
| 163 | if (retry > 0) |
| 164 | // Issue the same expressInterest as in verifyData except decrement retry. |
| 165 | face_->expressInterest |
| 166 | (*interest, |
| 167 | bind(&KeyChain::onCertificateData, this, _1, _2, nextStep), |
| 168 | bind(&KeyChain::onCertificateInterestTimeout, this, _1, retry - 1, onVerifyFailed, data, nextStep)); |
| 169 | else |
| 170 | onVerifyFailed(data); |
| 171 | } |
| 172 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 173 | } |