blob: df2a43a7a2c0612aa3d10a1547bb886f640ef8ce [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento08994782018-01-22 12:13:41 -05003 * Copyright (c) 2017-2018, Regents of the University of California.
Zhiyi Zhang23564c82017-03-01 10:22:22 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert 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 General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#ifndef NDNCERT_CLIENT_MODULE_HPP
22#define NDNCERT_CLIENT_MODULE_HPP
23
24#include "client-config.hpp"
25#include "certificate-request.hpp"
26
27namespace ndn {
28namespace ndncert {
29
30class RequestState
31{
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080032
Zhiyi Zhang23564c82017-03-01 10:22:22 -080033public:
34 ClientCaItem m_ca;
35 security::Key m_key;
36
37 std::string m_requestId;
38 std::string m_status;
39 std::string m_challengeType;
40 std::list<std::string> m_challengeList;
41
42 bool m_isInstalled = false;
43};
44
45// TODO
46// For each CA item in Client.Conf, create a validator instance and initialize it with CA's cert
47// The validator instance should be in ClientCaItem
48
49class ClientModule : noncopyable
50{
51public:
52 /**
53 * @brief Error that can be thrown from ClientModule
54 */
55 class Error : public std::runtime_error
56 {
57 public:
58 using std::runtime_error::runtime_error;
59 };
60
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080061 using LocalhostListCallback = function<void (const ClientConfig&)>;
62 using ListCallback = function<void (const std::list<Name>&, const Name&, const Name&)>;
Zhiyi Zhang23564c82017-03-01 10:22:22 -080063 using RequestCallback = function<void (const shared_ptr<RequestState>&)>;
64 using ErrorCallback = function<void (const std::string&)>;
65
66public:
Zhiyi Zhang23564c82017-03-01 10:22:22 -080067 ClientModule(Face& face, security::v2::KeyChain& keyChain, size_t retryTimes = 2);
68
Davide Pesavento08994782018-01-22 12:13:41 -050069 virtual
70 ~ClientModule();
71
Zhiyi Zhang23564c82017-03-01 10:22:22 -080072 ClientConfig&
73 getClientConf()
74 {
75 return m_config;
76 }
77
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080078 /**
79 * @brief Send /CA-prefix/CA/_DOWNLOAD/ANCHOR to get CA's latest anchor with the config
80 */
81 void
82 requestCaTrustAnchor(const Name& caName, const DataCallback& trustAnchorCallback,
83 const ErrorCallback& errorCallback);
84
85 /**
86 * @brief Send /localhost/CA/List to query local available CAs
87 *
88 * For more information:
89 * https://github.com/named-data/ndncert/wiki/Intra-Node-Design
90 */
91 void
92 requestLocalhostList(const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback);
93
94 /**
95 * @brief Handle the list request response
96 */
97 void
98 handleLocalhostListResponse(const Interest& request, const Data& reply,
99 const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback);
100
101 void
102 requestList(const ClientCaItem& ca, const std::string& additionalInfo,
103 const ListCallback& listCallback, const ErrorCallback& errorCallback);
104
105 void
106 handleListResponse(const Interest& request, const Data& reply, const ClientCaItem& ca,
107 const ListCallback& listCallback, const ErrorCallback& errorCallback);
108
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800109 void
110 sendProbe(const ClientCaItem& ca, const std::string& probeInfo,
111 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
112
113 void
114 handleProbeResponse(const Interest& request, const Data& reply, const ClientCaItem& ca,
115 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
116
117 void
118 sendNew(const ClientCaItem& ca, const Name& identityName,
119 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
120
121 void
122 handleNewResponse(const Interest& request, const Data& reply,
123 const shared_ptr<RequestState>& state,
124 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
125
126 void
127 sendSelect(const shared_ptr<RequestState>& state, const std::string& challengeType,
128 const JsonSection& selectParams,
129 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
130
131 void
132 handleSelectResponse(const Interest& request, const Data& reply,
133 const shared_ptr<RequestState>& state,
134 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
135
136 void
137 sendValidate(const shared_ptr<RequestState>& state, const JsonSection& validateParams,
138 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
139
140 void
141 handleValidateResponse(const Interest& request, const Data& reply,
142 const shared_ptr<RequestState>& state,
143 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
144
145 void
146 requestStatus(const shared_ptr<RequestState>& state,
147 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
148
149 void
150 handleStatusResponse(const Interest& request, const Data& reply,
151 const shared_ptr<RequestState>& state,
152 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
153
154 void
155 requestDownload(const shared_ptr<RequestState>& state, const RequestCallback& requestCallback,
156 const ErrorCallback& errorCallback);
157
158 void
159 handleDownloadResponse(const Interest& request, const Data& reply,
160 const shared_ptr<RequestState>& state,
161 const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
162
163 // helper functions
164 static JsonSection
165 getJsonFromData(const Data& data);
166
167 static Block
168 nameBlockFromJson(const JsonSection& json);
169
170 static bool
171 checkStatus(const RequestState& state, const JsonSection& json, const ErrorCallback& errorCallback);
172
173protected:
174 virtual void
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800175 onTimeout(const Interest& interest, int nRetriesLeft,
176 const DataCallback& dataCallback, const ErrorCallback& errorCallback);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800177
178 virtual void
179 onNack(const Interest& interest, const lp::Nack& nack, const ErrorCallback& errorCallback);
180
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800181protected:
182 ClientConfig m_config;
183 Face& m_face;
184 security::v2::KeyChain& m_keyChain;
185 size_t m_retryTimes;
186};
187
188} // namespace ndncert
189} // namespace ndn
190
191#endif // NDNCERT_CLIENT_MODULE_HPP