Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 4 | * |
| 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 | |
| 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | |
| 30 | class RequestState |
| 31 | { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame^] | 32 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 33 | public: |
| 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 | |
| 49 | class ClientModule : noncopyable |
| 50 | { |
| 51 | public: |
| 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 Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame^] | 61 | using LocalhostListCallback = function<void (const ClientConfig&)>; |
| 62 | using ListCallback = function<void (const std::list<Name>&, const Name&, const Name&)>; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 63 | using RequestCallback = function<void (const shared_ptr<RequestState>&)>; |
| 64 | using ErrorCallback = function<void (const std::string&)>; |
| 65 | |
| 66 | public: |
| 67 | explicit |
| 68 | ClientModule(Face& face, security::v2::KeyChain& keyChain, size_t retryTimes = 2); |
| 69 | |
| 70 | ClientConfig& |
| 71 | getClientConf() |
| 72 | { |
| 73 | return m_config; |
| 74 | } |
| 75 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame^] | 76 | /** |
| 77 | * @brief Send /CA-prefix/CA/_DOWNLOAD/ANCHOR to get CA's latest anchor with the config |
| 78 | */ |
| 79 | void |
| 80 | requestCaTrustAnchor(const Name& caName, const DataCallback& trustAnchorCallback, |
| 81 | const ErrorCallback& errorCallback); |
| 82 | |
| 83 | /** |
| 84 | * @brief Send /localhost/CA/List to query local available CAs |
| 85 | * |
| 86 | * For more information: |
| 87 | * https://github.com/named-data/ndncert/wiki/Intra-Node-Design |
| 88 | */ |
| 89 | void |
| 90 | requestLocalhostList(const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback); |
| 91 | |
| 92 | /** |
| 93 | * @brief Handle the list request response |
| 94 | */ |
| 95 | void |
| 96 | handleLocalhostListResponse(const Interest& request, const Data& reply, |
| 97 | const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback); |
| 98 | |
| 99 | void |
| 100 | requestList(const ClientCaItem& ca, const std::string& additionalInfo, |
| 101 | const ListCallback& listCallback, const ErrorCallback& errorCallback); |
| 102 | |
| 103 | void |
| 104 | handleListResponse(const Interest& request, const Data& reply, const ClientCaItem& ca, |
| 105 | const ListCallback& listCallback, const ErrorCallback& errorCallback); |
| 106 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 107 | void |
| 108 | sendProbe(const ClientCaItem& ca, const std::string& probeInfo, |
| 109 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 110 | |
| 111 | void |
| 112 | handleProbeResponse(const Interest& request, const Data& reply, const ClientCaItem& ca, |
| 113 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 114 | |
| 115 | void |
| 116 | sendNew(const ClientCaItem& ca, const Name& identityName, |
| 117 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 118 | |
| 119 | void |
| 120 | handleNewResponse(const Interest& request, const Data& reply, |
| 121 | const shared_ptr<RequestState>& state, |
| 122 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 123 | |
| 124 | void |
| 125 | sendSelect(const shared_ptr<RequestState>& state, const std::string& challengeType, |
| 126 | const JsonSection& selectParams, |
| 127 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 128 | |
| 129 | void |
| 130 | handleSelectResponse(const Interest& request, const Data& reply, |
| 131 | const shared_ptr<RequestState>& state, |
| 132 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 133 | |
| 134 | void |
| 135 | sendValidate(const shared_ptr<RequestState>& state, const JsonSection& validateParams, |
| 136 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 137 | |
| 138 | void |
| 139 | handleValidateResponse(const Interest& request, const Data& reply, |
| 140 | const shared_ptr<RequestState>& state, |
| 141 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 142 | |
| 143 | void |
| 144 | requestStatus(const shared_ptr<RequestState>& state, |
| 145 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 146 | |
| 147 | void |
| 148 | handleStatusResponse(const Interest& request, const Data& reply, |
| 149 | const shared_ptr<RequestState>& state, |
| 150 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 151 | |
| 152 | void |
| 153 | requestDownload(const shared_ptr<RequestState>& state, const RequestCallback& requestCallback, |
| 154 | const ErrorCallback& errorCallback); |
| 155 | |
| 156 | void |
| 157 | handleDownloadResponse(const Interest& request, const Data& reply, |
| 158 | const shared_ptr<RequestState>& state, |
| 159 | const RequestCallback& requestCallback, const ErrorCallback& errorCallback); |
| 160 | |
| 161 | // helper functions |
| 162 | static JsonSection |
| 163 | getJsonFromData(const Data& data); |
| 164 | |
| 165 | static Block |
| 166 | nameBlockFromJson(const JsonSection& json); |
| 167 | |
| 168 | static bool |
| 169 | checkStatus(const RequestState& state, const JsonSection& json, const ErrorCallback& errorCallback); |
| 170 | |
| 171 | protected: |
| 172 | virtual void |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame^] | 173 | onTimeout(const Interest& interest, int nRetriesLeft, |
| 174 | const DataCallback& dataCallback, const ErrorCallback& errorCallback); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 175 | |
| 176 | virtual void |
| 177 | onNack(const Interest& interest, const lp::Nack& nack, const ErrorCallback& errorCallback); |
| 178 | |
| 179 | |
| 180 | protected: |
| 181 | ClientConfig m_config; |
| 182 | Face& m_face; |
| 183 | security::v2::KeyChain& m_keyChain; |
| 184 | size_t m_retryTimes; |
| 185 | }; |
| 186 | |
| 187 | } // namespace ndncert |
| 188 | } // namespace ndn |
| 189 | |
| 190 | #endif // NDNCERT_CLIENT_MODULE_HPP |