Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, 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 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 21 | #ifndef NDNCERT_REQUESTER_HPP |
| 22 | #define NDNCERT_REQUESTER_HPP |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 23 | |
Zhiyi Zhang | f2306f7 | 2020-10-09 11:26:05 -0700 | [diff] [blame] | 24 | #include "requester-state.hpp" |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame^] | 28 | namespace requester { |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 29 | |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 30 | class Requester : noncopyable |
| 31 | { |
| 32 | public: |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 33 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 34 | * @brief Generates a CA profile discovery Interest following RDR protocol. |
| 35 | * |
| 36 | * @param caName The name prefix of the CA. |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 37 | * @return A shared pointer to an Interest ready to be sent. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 38 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 39 | static shared_ptr<Interest> |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 40 | genCaProfileDiscoveryInterest(const Name& caName); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 43 | * @brief Generates a CA profile fetching Interest following RDR protocol. |
| 44 | * |
| 45 | * @param reply The Data packet replied from discovery Interest. |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 46 | * @return A shared pointer to an Interest ready to be sent. |
| 47 | */ |
| 48 | static shared_ptr<Interest> |
| 49 | genCaProfileInterestFromDiscoveryResponse(const Data& reply); |
| 50 | |
| 51 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 52 | * @brief Decodes the CA profile from the replied CA profile Data packet. |
| 53 | * |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 54 | * Will first verify the signature of the packet using the key provided inside the profile. |
| 55 | * The application should be cautious whether to add CaProfile into the RequesterCaCache. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 56 | * |
| 57 | * @param reply The Data packet replied from CA profile fetching Interest. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 58 | * @return the CaProfile if decoding is successful |
| 59 | * @throw std::runtime_error if the decoding fails or receiving an error packet. |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 60 | */ |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 61 | static optional<CaProfile> |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 62 | onCaProfileResponse(const Data& reply); |
| 63 | |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 64 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 65 | * @brief Decodes the CA profile from the replied CA profile Data packet after the redirection. |
| 66 | * |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 67 | * Will first verify the signature of the packet using the key provided inside the profile and |
| 68 | * verify the certificate's digest matches the one obtained from the original CA. |
| 69 | * The application should be cautious whether to add CaProfile into the RequesterCaCache. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 70 | * |
| 71 | * @param reply The Data packet replied from CA profile fetching Interest. |
| 72 | * @param caCertFullName The full name obtained from original CA's probe response. |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 73 | * @return the CaProfile if decoding is successful |
| 74 | * @throw std::runtime_error if the decoding fails or receiving an error packet. |
| 75 | */ |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 76 | static optional<CaProfile> |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 77 | onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName); |
| 78 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 79 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 80 | * @brief Generates a PROBE interest to the CA (for suggested name assignments). |
| 81 | * |
| 82 | * @param ca The CA that interest is send to |
| 83 | * @param probeInfo The requester information to carry to the CA |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 84 | * @return A shared pointer of to the encoded interest, ready to be sent. |
| 85 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 86 | static shared_ptr<Interest> |
| 87 | genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo); |
| 88 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 89 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 90 | * @brief Decodes the replied data for PROBE process from the CA. |
| 91 | * |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 92 | * Will first verify the signature of the packet using the key provided inside the profile. |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 93 | * |
| 94 | * @param reply The replied data packet |
| 95 | * @param ca the profile of the CA that replies the packet |
| 96 | * @param identityNames The vector to load the decoded identity names from the data. |
| 97 | * @param otherCas The vector to load the decoded redirection CA prefixes from the data. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 98 | * @throw std::runtime_error if the decoding fails or receiving an error packet. |
| 99 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 100 | static void |
| 101 | onProbeResponse(const Data& reply, const CaProfile& ca, |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 102 | std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 103 | |
| 104 | // NEW/REVOKE/RENEW related helpers |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 105 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 106 | * @brief Generates a NEW interest to the CA. |
| 107 | * |
| 108 | * @param state The current requester state for this request. Will be modified in the function. |
| 109 | * @param identityName The identity name to be requested. |
| 110 | * @param notBefore The expected notBefore field for the certificate (starting time) |
| 111 | * @param notAfter The expected notAfter field for the certificate (expiration time) |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 112 | * @return The shared pointer to the encoded interest. |
| 113 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 114 | static shared_ptr<Interest> |
| 115 | genNewInterest(RequesterState& state, const Name& identityName, |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 116 | const time::system_clock::TimePoint& notBefore, |
| 117 | const time::system_clock::TimePoint& notAfter); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 118 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 119 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 120 | * @brief Generates a REVOKE interest to the CA. |
| 121 | * |
| 122 | * @param state The current requester state for this request. Will be modified in the function. |
| 123 | * @param certificate The certificate to the revoked. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 124 | * @return The shared pointer to the encoded interest. |
| 125 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 126 | static shared_ptr<Interest> |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 127 | genRevokeInterest(RequesterState& state, const security::Certificate& certificate); |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 128 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 129 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 130 | * @brief Decodes the replied data of NEW, RENEW, or REVOKE interest from the CA. |
| 131 | * |
| 132 | * @param state The current requester state for the request. Will be updated in the function. |
| 133 | * @param reply The replied data from the network |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 134 | * @return the list of challenge accepted by the CA, for CHALLENGE step. |
| 135 | * @throw std::runtime_error if the decoding fails or receiving an error packet. |
| 136 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 137 | static std::list<std::string> |
| 138 | onNewRenewRevokeResponse(RequesterState& state, const Data& reply); |
| 139 | |
| 140 | // CHALLENGE helpers |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 141 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 142 | * @brief Generates the required parameter for the selected challenge for the request |
| 143 | * |
| 144 | * @param state, The requester state of the request.Will be updated in the function. |
| 145 | * @param challengeSelected, The selected challenge for the request. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 146 | * Can use state.m_challengeType to continue. |
| 147 | * @return The requirement list for the current stage of the challenge, in name, prompt mapping. |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 148 | * @throw std::runtime_error if the challenge is not supported. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 149 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 150 | static std::vector<std::tuple<std::string, std::string>> |
| 151 | selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected); |
| 152 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 153 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 154 | * @brief Generates the CHALLENGE interest for the request. |
| 155 | * |
| 156 | * @param state, The requester state of the request. |
| 157 | * @param parameters, The requirement list, in name, value mapping. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 158 | * @return The shared pointer to the encoded interest |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 159 | * @throw std::runtime_error if the challenge is not selected or is not supported. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 160 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 161 | static shared_ptr<Interest> |
Zhiyi Zhang | 222810b | 2020-10-16 21:50:35 -0700 | [diff] [blame] | 162 | genChallengeInterest(RequesterState& state, |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 163 | std::vector<std::tuple<std::string, std::string>>&& parameters); |
| 164 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 165 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 166 | * @brief Decodes the responded data from the CHALLENGE interest. |
| 167 | * |
| 168 | * @param state, the corresponding requester state of the request. Will be modified. |
| 169 | * @param reply, the response data. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 170 | * @throw std::runtime_error if the decoding fails or receiving an error packet. |
| 171 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 172 | static void |
| 173 | onChallengeResponse(RequesterState& state, const Data& reply); |
| 174 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 175 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 176 | * @brief Generate the interest to fetch the issued certificate |
| 177 | * |
| 178 | * @param state, the state of the request. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 179 | * @return The shared pointer to the encoded interest |
| 180 | */ |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 181 | static shared_ptr<Interest> |
| 182 | genCertFetchInterest(const RequesterState& state); |
| 183 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 184 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 185 | * @brief Decoded and installs the response certificate from the certificate fetch. |
| 186 | * |
| 187 | * @param reply, the data replied from the certificate fetch interest. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 188 | * @return The shared pointer to the certificate being fetched. |
| 189 | */ |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 190 | static shared_ptr<security::Certificate> |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 191 | onCertFetchResponse(const Data& reply); |
| 192 | |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 193 | /** |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 194 | * @brief End the current request session and performs cleanup if necessary. |
| 195 | * |
| 196 | * @param state, the requester state for the request. |
tylerliu | df6e5cc | 2020-10-05 18:52:13 -0700 | [diff] [blame] | 197 | */ |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 198 | static void |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 199 | endSession(RequesterState& state); |
| 200 | |
| 201 | private: |
| 202 | static void |
| 203 | processIfError(const Data& data); |
| 204 | }; |
| 205 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame^] | 206 | } // namespace requester |
Zhiyi Zhang | 1d3dcd2 | 2020-10-01 22:25:43 -0700 | [diff] [blame] | 207 | } // namespace ndncert |
| 208 | } // namespace ndn |
| 209 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 210 | #endif // NDNCERT_REQUESTER_HPP |