akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #ifndef NLSR_KM_HPP |
| 2 | #define NLSR_KM_HPP |
| 3 | |
| 4 | #include <ndn-cpp-dev/face.hpp> |
| 5 | #include <ndn-cpp-dev/data.hpp> |
| 6 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 7 | #include <ndn-cpp-dev/security/validator.hpp> |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 8 | #include <ndn-cpp-dev/util/random.hpp> |
| 9 | #include <ndn-cpp-dev/security/identity-certificate.hpp> |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 10 | #include <list> |
| 11 | #include "nlsr_conf_param.hpp" |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 12 | #include "nlsr_cert_store.hpp" |
| 13 | #include "utility/nlsr_tokenizer.hpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 14 | |
| 15 | namespace nlsr |
| 16 | { |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 17 | class Nlsr; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 18 | enum nlsrKeyType |
| 19 | { |
| 20 | KEY_TYPE_ROOT, |
| 21 | KEY_TYPE_SITE, |
| 22 | KEY_TYPE_OPERATOR, |
| 23 | KEY_TYPE_ROUTER, |
| 24 | KEY_TYPE_PROCESS, |
| 25 | KEY_TYPE_UNKNOWN |
| 26 | }; |
| 27 | |
| 28 | enum nlsrContentType |
| 29 | { |
| 30 | CONTENT_TYPE_DATA, |
| 31 | CONTENT_TYPE_CERT |
| 32 | }; |
| 33 | |
| 34 | class KeyManager: public ndn::KeyChain, public ndn::Validator |
| 35 | { |
| 36 | typedef SecPublicInfo::Error InfoError; |
| 37 | typedef SecTpm::Error TpmError; |
| 38 | public: |
| 39 | KeyManager() |
| 40 | : certSeqNo(1) |
| 41 | , certStore() |
| 42 | , nlsrRootKeyPrefix() |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 43 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool initKeyManager(ConfParameter &cp); |
| 47 | |
| 48 | |
| 49 | |
| 50 | void |
| 51 | checkPolicy (const ndn::Data& data, |
| 52 | int stepCount, |
| 53 | const ndn::OnDataValidated &onValidated, |
| 54 | const ndn::OnDataValidationFailed &onValidationFailed, |
| 55 | std::vector<ndn::shared_ptr<ndn::ValidationRequest> > &nextSteps) |
| 56 | {} |
| 57 | |
| 58 | void |
| 59 | checkPolicy (const ndn::Interest& interest, |
| 60 | int stepCount, |
| 61 | const ndn::OnInterestValidated &onValidated, |
| 62 | const ndn::OnInterestValidationFailed &onValidationFailed, |
| 63 | std::vector<ndn::shared_ptr<ndn::ValidationRequest> > &nextSteps) |
| 64 | {} |
| 65 | |
| 66 | void signData(ndn::Data& data) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 67 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 68 | ndn::KeyChain::signByIdentity(data,processIdentity); |
| 69 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 70 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 71 | template<typename T> |
| 72 | void signByIdentity(T& packet, ndn::Name signeeIdentity) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 73 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 74 | ndn::KeyChain::signByIdentity(packet,signeeIdentity); |
| 75 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 76 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 77 | ndn::shared_ptr<ndn::IdentityCertificate> |
| 78 | getCertificate(ndn::Name certificateName) |
| 79 | { |
| 80 | return ndn::KeyChain::getCertificate(certificateName); |
| 81 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 82 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 83 | ndn::shared_ptr<ndn::IdentityCertificate> |
| 84 | getCertificate() |
| 85 | { |
| 86 | return getCertificate(processCertName); |
| 87 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 88 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 89 | ndn::Name |
| 90 | createIdentity(const ndn::Name identityName) |
| 91 | { |
| 92 | return ndn::KeyChain::createIdentity(identityName); |
| 93 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 94 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 95 | ndn::Name |
| 96 | createIdentity(const ndn::Name identityName, const ndn::Name signee) |
| 97 | { |
| 98 | ndn::KeyChain::addIdentity(identityName); |
| 99 | ndn::Name keyName; |
| 100 | try |
| 101 | { |
| 102 | keyName = ndn::KeyChain::getDefaultKeyNameForIdentity(identityName); |
| 103 | } |
| 104 | catch(InfoError& e) |
| 105 | { |
| 106 | keyName = ndn::KeyChain::generateRSAKeyPairAsDefault(identityName, true); |
| 107 | } |
| 108 | ndn::shared_ptr<ndn::PublicKey> pubKey; |
| 109 | try |
| 110 | { |
| 111 | pubKey = ndn::KeyChain::getPublicKey(keyName); |
| 112 | } |
| 113 | catch(InfoError& e) |
| 114 | { |
| 115 | return identityName; |
| 116 | } |
| 117 | ndn::Name certName; |
| 118 | try |
| 119 | { |
| 120 | certName = ndn::KeyChain::getDefaultCertificateNameForKey(keyName); |
| 121 | } |
| 122 | catch(InfoError& e) |
| 123 | { |
| 124 | ndn::shared_ptr<ndn::IdentityCertificate> certificate = |
| 125 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 126 | ndn::Name certificateName = keyName.getPrefix(-1); |
| 127 | certificateName.append("KEY").append( |
| 128 | keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 129 | certificate->setName(certificateName); |
| 130 | certificate->setNotBefore(ndn::getNow()); |
| 131 | certificate->setNotAfter(ndn::getNow() + 31536000 /* 1 year*/); |
| 132 | certificate->setPublicKeyInfo(*pubKey); |
| 133 | certificate->addSubjectDescription( |
| 134 | ndn::CertificateSubjectDescription("2.5.4.41", |
| 135 | keyName.toUri())); |
| 136 | certificate->encode(); |
| 137 | try |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 138 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 139 | ndn::KeyChain::signByIdentity(*certificate,signee); |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 140 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 141 | catch(InfoError& e) |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 142 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 143 | try |
| 144 | { |
| 145 | ndn::KeyChain::deleteIdentity(identityName); |
| 146 | } |
| 147 | catch(InfoError& e) |
| 148 | { |
| 149 | } |
| 150 | return identityName; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 151 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 152 | certName=certificate->getName(); |
| 153 | } |
| 154 | return certName; |
| 155 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 156 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 157 | void printCertStore() |
| 158 | { |
| 159 | certStore.printCertStore(); |
| 160 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 161 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 162 | private: |
| 163 | bool |
| 164 | verifyDataPacket(ndn::Data packet) |
| 165 | { |
| 166 | std::cout<<"KeyManager::verifyDataPacket Called"<<std::endl; |
| 167 | ndn::SignatureSha256WithRsa signature(packet.getSignature()); |
| 168 | std::string signingCertName=signature.getKeyLocator().getName().toUri(); |
| 169 | std::string packetName=packet.getName().toUri(); |
| 170 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> signee= |
| 171 | certStore.getCertificateFromStore(signingCertName); |
| 172 | if( signee.second ) |
| 173 | { |
| 174 | std::string routerNameFromPacketName=getRouterName(packetName); |
| 175 | std::string routerNameFromCertName=getRouterName(signingCertName); |
| 176 | return ( (routerNameFromPacketName== routerNameFromCertName) && |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 177 | verifySignature(packet, signee.first->getPublicKeyInfo()) && |
| 178 | certStore.getCertificateIsVerified(signingCertName)); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 179 | } |
| 180 | return false; |
| 181 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 182 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 183 | bool |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 184 | verifyCertPacket(Nlsr& pnlsr, ndn::IdentityCertificate& packet); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 185 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 186 | public: |
| 187 | template<typename T> |
| 188 | bool |
| 189 | verify(T& packet ) |
| 190 | { |
| 191 | std::cout<<"KeyManager::verify Called"<<std::endl; |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 192 | |
| 193 | return verifyDataPacket(packet); |
| 194 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 195 | return false; |
| 196 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 197 | |
| 198 | bool |
| 199 | verify(Nlsr& pnlsr, ndn::IdentityCertificate& packet) |
| 200 | { |
| 201 | return verifyCertPacket(pnlsr, packet); |
| 202 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 203 | |
| 204 | ndn::Name getProcessCertName(); |
| 205 | ndn::Name getRouterCertName(); |
| 206 | ndn::Name getOperatorCertName(); |
| 207 | ndn::Name getSiteCertName(); |
| 208 | ndn::Name getRootCertName(); |
| 209 | |
| 210 | uint32_t getCertSeqNo(); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 211 | std::pair<uint32_t, bool> getCertificateSeqNum(std::string certName); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 212 | void setCerSeqNo(uint32_t csn); |
| 213 | void initCertSeqFromFile(string certSeqFileDir); |
| 214 | void writeCertSeqToFile(); |
| 215 | bool isNewCertificate(std::string certName, int checkSeqNum); |
| 216 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 217 | getCertificateFromStore(const std::string certName, int checkSeqNum); |
| 218 | std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> |
| 219 | getCertificateFromStore(const std::string certName); |
| 220 | bool addCertificate(ndn::shared_ptr<ndn::IdentityCertificate> pcert |
| 221 | , uint32_t csn, bool isv); |
| 222 | |
| 223 | |
| 224 | private: |
| 225 | bool loadAllCertificates(std::string certDirPath); |
| 226 | bool loadCertificate(std::string inputFile, nlsrKeyType keyType); |
| 227 | nlsrKeyType getKeyTypeFromName(const std::string keyName); |
| 228 | std::string getRouterName(const std::string name); |
| 229 | std::string getSiteName(const std::string name); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 230 | std::string getRootName(const std::string name); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 231 | |
| 232 | private: |
| 233 | ndn::Name processIdentity; |
| 234 | ndn::Name routerIdentity; |
| 235 | ndn::Name processCertName; |
| 236 | ndn::Name routerCertName; |
| 237 | ndn::Name opCertName; |
| 238 | ndn::Name siteCertName; |
| 239 | ndn::Name rootCertName; |
| 240 | ndn::Name processKeyName; |
| 241 | uint32_t certSeqNo; |
| 242 | string certSeqFileNameWithPath; |
| 243 | string nlsrRootKeyPrefix; |
| 244 | NlsrCertificateStore certStore; |
| 245 | |
| 246 | }; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | #endif |