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