blob: 356a2a3e1347ef8007f8fa2d770a5714bb35c9c5 [file] [log] [blame]
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_SECURITY_V2_TRUST_ANCHOR_GROUP_HPP
23#define NDN_SECURITY_V2_TRUST_ANCHOR_GROUP_HPP
24
25#include "../../data.hpp"
26#include "certificate.hpp"
27
28#include <set>
29#include <boost/filesystem/path.hpp>
30
31namespace ndn {
32namespace security {
33namespace v2 {
34
35class CertContainerInterface
36{
37public:
38 virtual void
39 add(Certificate&& cert) = 0;
40
41 virtual void
42 remove(const Name& certName) = 0;
43};
44
45/**
46 * @brief A group of trust anchors
47 */
48class TrustAnchorGroup : noncopyable
49{
50public:
51 /**
52 * @brief Create an anchor group
53 */
54 explicit
55 TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id);
56
57 /**
58 * @return group id
59 */
60 const std::string&
61 getId() const
62 {
63 return m_id;
64 }
65
66 /**
67 * @return number of certificates in the group
68 */
69 size_t
70 size() const;
71
72 /**
73 * @brief Request certificate refresh
74 */
75 virtual void
76 refresh();
77
78protected:
79 CertContainerInterface& m_certs;
80 std::set<Name> m_anchorNames;
81
82private:
83 std::string m_id;
84};
85
86/**
87 * @brief Static trust anchor group
88 */
89class StaticTrustAnchorGroup : public TrustAnchorGroup
90{
91public:
92 /**
93 * @brief Create a static trust anchor group
94 * @param certContainer Reference to CertContainerInterface instance
95 * @param id Group id
96 */
97 StaticTrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id);
98
99 /**
100 * @brief Load static anchor @p cert
101 */
102 void
103 add(Certificate&& cert);
104
105 /**
106 * @brief Remove static anchor @p certName
107 */
108 void
109 remove(const Name& certName);
110};
111
112/**
113 * @brief Dynamic trust anchor group
114 */
115class DynamicTrustAnchorGroup : public TrustAnchorGroup
116{
117public:
118 /**
119 * @brief Create a dynamic trust anchor group
120 *
121 * This contructor would load all the certificates from @p path and will be refreshing
122 * certificates every @p refreshPeriod time period.
123 *
124 * Note that refresh is not scheduled, but is performed upon "find" operations.
125 *
126 * When @p isDir is false and @p path doesn't point to a valid certificate (file doesn't
127 * exist or content is not a valid certificate), the dynamic anchor group will be empty until
128 * file gets created. If file disappears or gets corrupted, the anchor group becomes empty.
129 *
130 * When @p idDir is true and @p path does't point to a valid folder, folder is empty, or
131 * doesn't contain valid certificates, the group will be empty until certificate files are
132 * placed in the folder. If folder is removed, becomes empty, or no longer contains valid
133 * certificates, the anchor group becomes empty.
134 *
135 * Upon refresh, the existing certificates are not changed.
136 *
137 * @param certContainer A certificate container into which trust anchors from the group will
138 * be added
139 * @param id Group id
140 * @param path File path for trust anchor(s), could be directory or file. If it is a
141 * directory, all the certificates in the directory will be loaded.
142 * @param refreshPeriod Refresh time for the anchors under @p path, must be positive.
143 * @param isDir Tells whether the path is a directory or a single file.
144 *
145 * @throw std::invalid_argument @p refreshPeriod is negative
146 */
147 DynamicTrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id,
148 const boost::filesystem::path& path, time::nanoseconds refreshPeriod, bool isDir = false);
149
150 void
151 refresh() override;
152
153private:
154 bool m_isDir;
155 boost::filesystem::path m_path;
156 time::nanoseconds m_refreshPeriod;
157 time::steady_clock::TimePoint m_expireTime;
158};
159
160} // namespace v2
161} // namespace security
162} // namespace ndn
163
164#endif // NDN_SECURITY_V2_TRUST_ANCHOR_GROUP_HPP