Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include "access-manager.hpp" |
| 21 | #include "encrypted-content.hpp" |
| 22 | |
| 23 | #include <ndn-cxx/util/logger.hpp> |
| 24 | |
| 25 | NDN_LOG_INIT(nac.AccessManager); |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nac { |
| 29 | |
| 30 | AccessManager::AccessManager(const Identity& identity, const Name& dataset, |
| 31 | KeyChain& keyChain, Face& face) |
| 32 | : m_identity(identity) |
| 33 | , m_keyChain(keyChain) |
| 34 | , m_face(face) |
| 35 | { |
| 36 | // NAC Identity: <identity>/NAC/<dataset> |
| 37 | // generate NAC key |
| 38 | auto nacId = m_keyChain.createIdentity(Name(identity.getName()).append(NAC).append(dataset), RsaKeyParams()); |
| 39 | m_nacKey = nacId.getDefaultKey(); |
| 40 | if (m_nacKey.getKeyType() != KeyType::RSA) { |
| 41 | NDN_LOG_INFO("Cannot re-use existing KEK/KDK pair, as it is not an RSA key, regenerating"); |
| 42 | m_nacKey = m_keyChain.createKey(nacId, RsaKeyParams()); |
| 43 | } |
| 44 | auto nacKeyId = m_nacKey.getName().at(-1); |
| 45 | |
| 46 | auto kekPrefix = Name(m_nacKey.getIdentity()).append(KEK); |
| 47 | |
| 48 | auto kek = make_shared<Data>(m_nacKey.getDefaultCertificate()); |
| 49 | kek->setName(Name(kekPrefix).append(nacKeyId)); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 50 | kek->setFreshnessPeriod(DEFAULT_KEK_FRESHNESS_PERIOD); |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 51 | m_keyChain.sign(*kek, signingByIdentity(m_identity)); |
| 52 | // kek looks like a cert, but doesn't have ValidityPeriod |
| 53 | m_ims.insert(*kek); |
| 54 | |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame^] | 55 | auto serveFromIms = [this] (const Name&, const Interest& interest) { |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 56 | auto data = m_ims.find(interest); |
| 57 | if (data != nullptr) { |
| 58 | NDN_LOG_DEBUG("Serving " << data->getName() << " from InMemoryStorage"); |
| 59 | m_face.put(*data); |
| 60 | } |
| 61 | else { |
| 62 | NDN_LOG_DEBUG("Didn't find data for " << interest.getName()); |
| 63 | // send NACK? |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | auto handleError = [] (const Name& prefix, const std::string& msg) { |
| 68 | NDN_LOG_ERROR("Failed to register prefix " << prefix << ": " << msg); |
| 69 | }; |
| 70 | |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame^] | 71 | m_kekReg = m_face.setInterestFilter(kekPrefix, serveFromIms, handleError); |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 72 | |
| 73 | auto kdkPrefix = Name(m_nacKey.getIdentity()).append(KDK).append(nacKeyId); |
Davide Pesavento | d51d960 | 2019-07-20 23:33:06 -0400 | [diff] [blame^] | 74 | m_kdkReg = m_face.setInterestFilter(kdkPrefix, serveFromIms, handleError); |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Alexander Afanasyev | 2b57aeb | 2018-06-15 18:32:28 -0400 | [diff] [blame] | 77 | Data |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 78 | AccessManager::addMember(const Certificate& memberCert) |
| 79 | { |
| 80 | Name kdkName(m_nacKey.getIdentity()); |
| 81 | kdkName |
| 82 | .append(KDK) |
| 83 | .append(m_nacKey.getName().at(-1)) // key-id |
| 84 | .append(ENCRYPTED_BY) |
| 85 | .append(memberCert.getKeyName()); |
| 86 | |
| 87 | const size_t secretLength = 32; |
| 88 | uint8_t secret[secretLength + 1]; |
| 89 | random::generateSecureBytes(secret, secretLength); |
| 90 | // because of stupid bug in ndn-cxx, remove all \0 in generated secret, replace with 1 |
| 91 | for (size_t i = 0; i < secretLength; ++i) { |
| 92 | if (secret[i] == 0) { |
| 93 | secret[i] = 1; |
| 94 | } |
| 95 | } |
| 96 | secret[secretLength] = 0; |
| 97 | |
| 98 | auto kdkData = m_keyChain.exportSafeBag(m_nacKey.getDefaultCertificate(), |
| 99 | reinterpret_cast<const char*>(secret), secretLength); |
| 100 | |
| 101 | PublicKey memberKey; |
| 102 | memberKey.loadPkcs8(memberCert.getPublicKey().data(), memberCert.getPublicKey().size()); |
| 103 | |
| 104 | EncryptedContent content; |
| 105 | content.setPayload(kdkData->wireEncode()); |
| 106 | content.setPayloadKey(memberKey.encrypt(secret, secretLength)); |
| 107 | |
| 108 | auto kdk = make_shared<Data>(kdkName); |
| 109 | kdk->setContent(content.wireEncode()); |
Alexander Afanasyev | 1a21e10 | 2018-06-13 20:33:21 -0400 | [diff] [blame] | 110 | // FreshnessPeriod can serve as a soft access control for revoking access |
| 111 | kdk->setFreshnessPeriod(DEFAULT_KDK_FRESHNESS_PERIOD); |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 112 | m_keyChain.sign(*kdk, signingByIdentity(m_identity)); |
| 113 | |
| 114 | m_ims.insert(*kdk); |
Alexander Afanasyev | 2b57aeb | 2018-06-15 18:32:28 -0400 | [diff] [blame] | 115 | |
| 116 | return *kdk; |
Alexander Afanasyev | e96538a | 2018-06-13 20:32:53 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | AccessManager::removeMember(const Name& identity) |
| 121 | { |
| 122 | m_ims.erase(Name(m_nacKey.getName()).append(KDK).append(ENCRYPTED_BY).append(identity)); |
| 123 | } |
| 124 | |
| 125 | } // namespace nac |
| 126 | } // namespace ndn |