blob: a1f342277bb6c9fb3ab8bc0c433065eaf766cccd [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/*
3 * Copyright (c) 2014-2020, 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()
40 : m_face(nullptr, m_keyChain)
41 , m_validator(m_face)
42 , m_accessManager(m_keyChain.createIdentity("/nac/example", RsaKeyParams()), "test",
43 m_keyChain, m_face)
44 , m_encryptor("/nac/example/NAC/test",
45 "/nac/example/CK", signingWithSha256(),
46 [] (auto...) {
47 std::cerr << "Failed to publish CK";
48 }, m_validator, m_keyChain, m_face)\
49 {
50 m_validator.load(R"CONF(
51 trust-anchor
52 {
53 type any
54 }
55 )CONF", "fake-config");
56 }
57
58 void
59 run()
60 {
61 // give access to default identity. If consumer uses the same default identity, he will be able to decrypt
62 m_accessManager.addMember(m_keyChain.getPib().getDefaultIdentity().getDefaultKey().getDefaultCertificate());
63
64 m_face.setInterestFilter("/example/testApp",
65 bind(&Producer::onInterest, this, _1, _2),
66 RegisterPrefixSuccessCallback(),
67 bind(&Producer::onRegisterFailed, this, _1, _2));
68 m_face.processEvents();
69 }
70
71private:
72 void
73 onInterest(const InterestFilter& filter, const Interest& interest)
74 {
75 std::cout << "<< I: " << interest << std::endl;
76
77 // Create new name, based on Interest's name
78 Name dataName(interest.getName());
79 dataName
80 .append("testApp") // add "testApp" component to Interest name
81 .appendVersion(); // add "version" component (current UNIX timestamp in milliseconds)
82
83 static const std::string content = "HELLO KITTY";
84
85 // Create Data packet
86 shared_ptr<Data> data = make_shared<Data>();
87 data->setName(dataName);
88 data->setFreshnessPeriod(10_s); // 10 seconds
89
90 auto blob = m_encryptor.encrypt(reinterpret_cast<const uint8_t*>(content.data()), content.size());
91 data->setContent(blob.wireEncode());
92
93 // Sign Data packet with default identity
94 m_keyChain.sign(*data);
95 // m_keyChain.sign(data, <identityName>);
96 // m_keyChain.sign(data, <certificate>);
97
98 // Return Data packet to the requester
99 std::cout << ">> D: " << *data << std::endl;
100 m_face.put(*data);
101 }
102
103
104 void
105 onRegisterFailed(const Name& prefix, const std::string& reason)
106 {
107 std::cerr << "ERROR: Failed to register prefix \""
108 << prefix << "\" in local hub's daemon (" << reason << ")"
109 << std::endl;
110 m_face.shutdown();
111 }
112
113private:
114 KeyChain m_keyChain;
115 Face m_face;
116 ValidatorConfig m_validator;
117 AccessManager m_accessManager;
118 Encryptor m_encryptor;
119};
120
121} // namespace examples
122} // namespace nac
123} // namespace ndn
124
125int
126main(int argc, char** argv)
127{
128 ndn::nac::examples::Producer producer;
129 try {
130 producer.run();
131 }
132 catch (const std::exception& e) {
133 std::cerr << "ERROR: " << e.what() << std::endl;
134 }
135 return 0;
136}