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