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