blob: 9c9d95b81bc290850ede63931437c5a2890fd912 [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 Pesavento714dba02022-03-17 20:46:28 -04003 * Copyright (c) 2014-2022, 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 {
33// Additional nested namespaces can be used to prevent/limit name conflicts
34namespace examples {
35
36class Producer : noncopyable
37{
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 Pesavento714dba02022-03-17 20:46:28 -040062 std::bind(&Producer::onInterest, this, _1, _2),
63 nullptr, // RegisterPrefixSuccessCallback is optional
64 std::bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040065 m_face.processEvents();
66 }
67
68private:
69 void
Davide Pesavento714dba02022-03-17 20:46:28 -040070 onInterest(const InterestFilter&, const Interest& interest)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040071 {
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 Afanasyevc3d29902018-06-29 18:20:55 -040080 // Create Data packet
Davide Pesavento714dba02022-03-17 20:46:28 -040081 auto data = std::make_shared<Data>();
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040082 data->setName(dataName);
83 data->setFreshnessPeriod(10_s); // 10 seconds
84
Davide Pesavento714dba02022-03-17 20:46:28 -040085 static const std::string content = "HELLO KITTY";
86 auto blob = m_encryptor.encrypt({reinterpret_cast<const uint8_t*>(content.data()), content.size()});
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040087 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 Pesavento714dba02022-03-17 20:46:28 -0400103 std::cerr << "ERROR: Failed to register prefix '" << prefix
104 << "' with the local forwarder (" << reason << ")" << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400105 m_face.shutdown();
106 }
107
108private:
109 KeyChain m_keyChain;
Davide Pesavento714dba02022-03-17 20:46:28 -0400110 Face m_face{nullptr, m_keyChain};
111 ValidatorConfig m_validator{m_face};
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400112 AccessManager m_accessManager;
113 Encryptor m_encryptor;
114};
115
116} // namespace examples
117} // namespace nac
118} // namespace ndn
119
120int
121main(int argc, char** argv)
122{
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400123 try {
Davide Pesavento714dba02022-03-17 20:46:28 -0400124 ndn::nac::examples::Producer producer;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400125 producer.run();
Davide Pesavento714dba02022-03-17 20:46:28 -0400126 return 0;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400127 }
128 catch (const std::exception& e) {
129 std::cerr << "ERROR: " << e.what() << std::endl;
Davide Pesavento714dba02022-03-17 20:46:28 -0400130 return 1;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400131 }
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400132}