Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -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 <ndn-cxx/face.hpp> |
| 21 | #include <ndn-cxx/security/key-chain.hpp> |
Davide Pesavento | c264949 | 2020-12-22 21:43:35 -0500 | [diff] [blame] | 22 | #include <ndn-cxx/security/signing-helpers.hpp> |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/security/validator-config.hpp> |
| 24 | |
| 25 | #include "encryptor.hpp" |
| 26 | #include "access-manager.hpp" |
| 27 | |
| 28 | #include <iostream> |
| 29 | |
| 30 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 31 | namespace ndn { |
| 32 | namespace nac { |
| 33 | // Additional nested namespaces can be used to prevent/limit name conflicts |
| 34 | namespace examples { |
| 35 | |
| 36 | class Producer : noncopyable |
| 37 | { |
| 38 | public: |
| 39 | Producer() |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 40 | : m_accessManager(m_keyChain.createIdentity("/nac/example", RsaKeyParams()), "test", |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 41 | m_keyChain, m_face) |
| 42 | , m_encryptor("/nac/example/NAC/test", |
| 43 | "/nac/example/CK", signingWithSha256(), |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 44 | [] (auto&&...) { std::cerr << "Failed to publish CK"; }, |
| 45 | m_validator, m_keyChain, m_face) |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 46 | { |
| 47 | m_validator.load(R"CONF( |
| 48 | trust-anchor |
| 49 | { |
| 50 | type any |
| 51 | } |
| 52 | )CONF", "fake-config"); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | run() |
| 57 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 58 | // Give access to default identity. If consumer uses the same default identity, it will be able to decrypt |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 59 | m_accessManager.addMember(m_keyChain.getPib().getDefaultIdentity().getDefaultKey().getDefaultCertificate()); |
| 60 | |
| 61 | m_face.setInterestFilter("/example/testApp", |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 62 | std::bind(&Producer::onInterest, this, _1, _2), |
| 63 | nullptr, // RegisterPrefixSuccessCallback is optional |
| 64 | std::bind(&Producer::onRegisterFailed, this, _1, _2)); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 65 | m_face.processEvents(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 70 | onInterest(const InterestFilter&, const Interest& interest) |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 71 | { |
| 72 | std::cout << "<< I: " << interest << std::endl; |
| 73 | |
| 74 | // Create new name, based on Interest's name |
| 75 | Name dataName(interest.getName()); |
| 76 | dataName |
| 77 | .append("testApp") // add "testApp" component to Interest name |
| 78 | .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds) |
| 79 | |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 80 | // Create Data packet |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 81 | auto data = std::make_shared<Data>(); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 82 | data->setName(dataName); |
| 83 | data->setFreshnessPeriod(10_s); // 10 seconds |
| 84 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 85 | static const std::string content = "HELLO KITTY"; |
| 86 | auto blob = m_encryptor.encrypt({reinterpret_cast<const uint8_t*>(content.data()), content.size()}); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 87 | data->setContent(blob.wireEncode()); |
| 88 | |
| 89 | // Sign Data packet with default identity |
| 90 | m_keyChain.sign(*data); |
| 91 | // m_keyChain.sign(data, <identityName>); |
| 92 | // m_keyChain.sign(data, <certificate>); |
| 93 | |
| 94 | // Return Data packet to the requester |
| 95 | std::cout << ">> D: " << *data << std::endl; |
| 96 | m_face.put(*data); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void |
| 101 | onRegisterFailed(const Name& prefix, const std::string& reason) |
| 102 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 103 | std::cerr << "ERROR: Failed to register prefix '" << prefix |
| 104 | << "' with the local forwarder (" << reason << ")" << std::endl; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 105 | m_face.shutdown(); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | KeyChain m_keyChain; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 110 | Face m_face{nullptr, m_keyChain}; |
| 111 | ValidatorConfig m_validator{m_face}; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 112 | AccessManager m_accessManager; |
| 113 | Encryptor m_encryptor; |
| 114 | }; |
| 115 | |
| 116 | } // namespace examples |
| 117 | } // namespace nac |
| 118 | } // namespace ndn |
| 119 | |
| 120 | int |
| 121 | main(int argc, char** argv) |
| 122 | { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 123 | try { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 124 | ndn::nac::examples::Producer producer; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 125 | producer.run(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 126 | return 0; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 127 | } |
| 128 | catch (const std::exception& e) { |
| 129 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 130 | return 1; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 131 | } |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 132 | } |