blob: e7a25bb99e04b4ace30eba1cde0254239dcbd5d3 [file] [log] [blame]
Alexander Afanasyevc3d29902018-06-29 18:20:55 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc2649492020-12-22 21:43:35 -05002/*
Davide Pesavento8d60e642023-04-17 02:36:03 -04003 * Copyright (c) 2014-2023, Regents of the University of California
Alexander Afanasyevc3d29902018-06-29 18:20:55 -04004 *
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 Pesaventoc2649492020-12-22 21:43:35 -050022#include <ndn-cxx/security/signing-helpers.hpp>
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040023#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`)
31namespace ndn {
32namespace nac {
Davide Pesavento8d60e642023-04-17 02:36:03 -040033// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040034namespace examples {
35
Davide Pesavento8d60e642023-04-17 02:36:03 -040036class Producer
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040037{
38public:
39 Producer()
Davide Pesavento714dba02022-03-17 20:46:28 -040040 : m_accessManager(m_keyChain.createIdentity("/nac/example", RsaKeyParams()), "test",
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040041 m_keyChain, m_face)
42 , m_encryptor("/nac/example/NAC/test",
43 "/nac/example/CK", signingWithSha256(),
Davide Pesavento714dba02022-03-17 20:46:28 -040044 [] (auto&&...) { std::cerr << "Failed to publish CK"; },
45 m_validator, m_keyChain, m_face)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040046 {
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 Pesavento714dba02022-03-17 20:46:28 -040058 // Give access to default identity. If consumer uses the same default identity, it will be able to decrypt
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040059 m_accessManager.addMember(m_keyChain.getPib().getDefaultIdentity().getDefaultKey().getDefaultCertificate());
60
61 m_face.setInterestFilter("/example/testApp",
Davide Pesavento8d60e642023-04-17 02:36:03 -040062 std::bind(&Producer::onInterest, this, std::placeholders::_2),
Davide Pesavento714dba02022-03-17 20:46:28 -040063 nullptr, // RegisterPrefixSuccessCallback is optional
Davide Pesavento8d60e642023-04-17 02:36:03 -040064 [this] (const Name& prefix, const std::string& reason) {
65 std::cerr << "ERROR: Failed to register prefix '" << prefix
66 << "' with the local forwarder (" << reason << ")\n";
67 m_face.shutdown();
68 });
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040069 m_face.processEvents();
70 }
71
72private:
73 void
Davide Pesavento8d60e642023-04-17 02:36:03 -040074 onInterest(const Interest& interest)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040075 {
Davide Pesavento8d60e642023-04-17 02:36:03 -040076 std::cout << ">> I: " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040077
Davide Pesavento8d60e642023-04-17 02:36:03 -040078 // Create a new name for the Data packet, based on the Interest's name
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040079 Name dataName(interest.getName());
80 dataName
81 .append("testApp") // add "testApp" component to Interest name
Davide Pesavento8d60e642023-04-17 02:36:03 -040082 .appendVersion(); // add version component (current UNIX timestamp in milliseconds)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040083
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040084 // Create Data packet
Davide Pesavento714dba02022-03-17 20:46:28 -040085 auto data = std::make_shared<Data>();
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040086 data->setName(dataName);
87 data->setFreshnessPeriod(10_s); // 10 seconds
88
Davide Pesavento8d60e642023-04-17 02:36:03 -040089 constexpr std::string_view content{"Hello, world!"};
Davide Pesavento714dba02022-03-17 20:46:28 -040090 auto blob = m_encryptor.encrypt({reinterpret_cast<const uint8_t*>(content.data()), content.size()});
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040091 data->setContent(blob.wireEncode());
92
93 // Sign Data packet with default identity
94 m_keyChain.sign(*data);
Davide Pesavento8d60e642023-04-17 02:36:03 -040095 // m_keyChain.sign(*data, signingByIdentity(<identityName>));
96 // m_keyChain.sign(*data, signingByKey(<keyName>));
97 // m_keyChain.sign(*data, signingByCertificate(<certName>));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040098
99 // Return Data packet to the requester
Davide Pesavento8d60e642023-04-17 02:36:03 -0400100 std::cout << "<< D: " << *data << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400101 m_face.put(*data);
102 }
103
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400104private:
105 KeyChain m_keyChain;
Davide Pesavento714dba02022-03-17 20:46:28 -0400106 Face m_face{nullptr, m_keyChain};
107 ValidatorConfig m_validator{m_face};
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400108 AccessManager m_accessManager;
109 Encryptor m_encryptor;
110};
111
112} // namespace examples
113} // namespace nac
114} // namespace ndn
115
116int
117main(int argc, char** argv)
118{
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400119 try {
Davide Pesavento714dba02022-03-17 20:46:28 -0400120 ndn::nac::examples::Producer producer;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400121 producer.run();
Davide Pesavento714dba02022-03-17 20:46:28 -0400122 return 0;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400123 }
124 catch (const std::exception& e) {
125 std::cerr << "ERROR: " << e.what() << std::endl;
Davide Pesavento714dba02022-03-17 20:46:28 -0400126 return 1;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400127 }
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400128}