blob: fddd35bfb09452a800429baa15f5d9d2c76106b6 [file] [log] [blame]
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07001/* -*- 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
tylerliu8704d032020-06-23 10:18:15 -070021#ifndef NDNCERT_REQUESTER_HPP
22#define NDNCERT_REQUESTER_HPP
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070023
Zhiyi Zhangf2306f72020-10-09 11:26:05 -070024#include "requester-state.hpp"
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070025
26namespace ndn {
27namespace ndncert {
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070028namespace requester {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070029
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070030class Requester : noncopyable
31{
32public:
tylerliudf6e5cc2020-10-05 18:52:13 -070033 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070034 * @brief Generates a CA profile discovery Interest following RDR protocol.
35 *
36 * @param caName The name prefix of the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070037 * @return A shared pointer to an Interest ready to be sent.
tylerliudf6e5cc2020-10-05 18:52:13 -070038 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070039 static shared_ptr<Interest>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070040 genCaProfileDiscoveryInterest(const Name& caName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070041
42 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070043 * @brief Generates a CA profile fetching Interest following RDR protocol.
44 *
45 * @param reply The Data packet replied from discovery Interest.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070046 * @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 Zhang6d9eda62020-10-16 17:37:02 -070052 * @brief Decodes the CA profile from the replied CA profile Data packet.
53 *
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070054 * 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 Zhang6d9eda62020-10-16 17:37:02 -070056 *
57 * @param reply The Data packet replied from CA profile fetching Interest.
tylerliudf6e5cc2020-10-05 18:52:13 -070058 * @return the CaProfile if decoding is successful
59 * @throw std::runtime_error if the decoding fails or receiving an error packet.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070060 */
Zhiyi Zhang997669a2020-10-28 21:15:40 -070061 static optional<CaProfile>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070062 onCaProfileResponse(const Data& reply);
63
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070064 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070065 * @brief Decodes the CA profile from the replied CA profile Data packet after the redirection.
66 *
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070067 * 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 Zhang6d9eda62020-10-16 17:37:02 -070070 *
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 Zhangfbcab842020-10-07 15:17:13 -070073 * @return the CaProfile if decoding is successful
74 * @throw std::runtime_error if the decoding fails or receiving an error packet.
75 */
Zhiyi Zhang997669a2020-10-28 21:15:40 -070076 static optional<CaProfile>
Zhiyi Zhang837406d2020-10-05 22:01:31 -070077 onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName);
78
tylerliudf6e5cc2020-10-05 18:52:13 -070079 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070080 * @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
tylerliudf6e5cc2020-10-05 18:52:13 -070084 * @return A shared pointer of to the encoded interest, ready to be sent.
85 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070086 static shared_ptr<Interest>
87 genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo);
88
tylerliudf6e5cc2020-10-05 18:52:13 -070089 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070090 * @brief Decodes the replied data for PROBE process from the CA.
91 *
tylerliudf6e5cc2020-10-05 18:52:13 -070092 * Will first verify the signature of the packet using the key provided inside the profile.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070093 *
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.
tylerliudf6e5cc2020-10-05 18:52:13 -070098 * @throw std::runtime_error if the decoding fails or receiving an error packet.
99 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700100 static void
101 onProbeResponse(const Data& reply, const CaProfile& ca,
tylerliub47dad72020-10-08 21:36:55 -0700102 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700103
104 // NEW/REVOKE/RENEW related helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700105 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700106 * @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)
tylerliudf6e5cc2020-10-05 18:52:13 -0700112 * @return The shared pointer to the encoded interest.
113 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700114 static shared_ptr<Interest>
115 genNewInterest(RequesterState& state, const Name& identityName,
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700116 const time::system_clock::TimePoint& notBefore,
117 const time::system_clock::TimePoint& notAfter);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700118
tylerliudf6e5cc2020-10-05 18:52:13 -0700119 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700120 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700124 * @return The shared pointer to the encoded interest.
125 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700126 static shared_ptr<Interest>
tylerliua7bea662020-10-08 18:51:02 -0700127 genRevokeInterest(RequesterState& state, const security::Certificate& certificate);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700128
tylerliudf6e5cc2020-10-05 18:52:13 -0700129 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700130 * @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
tylerliudf6e5cc2020-10-05 18:52:13 -0700134 * @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 Zhang1d3dcd22020-10-01 22:25:43 -0700137 static std::list<std::string>
138 onNewRenewRevokeResponse(RequesterState& state, const Data& reply);
139
140 // CHALLENGE helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700141 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700142 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700146 * Can use state.m_challengeType to continue.
147 * @return The requirement list for the current stage of the challenge, in name, prompt mapping.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700148 * @throw std::runtime_error if the challenge is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700149 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700150 static std::vector<std::tuple<std::string, std::string>>
151 selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected);
152
tylerliudf6e5cc2020-10-05 18:52:13 -0700153 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700154 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700158 * @return The shared pointer to the encoded interest
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700159 * @throw std::runtime_error if the challenge is not selected or is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700160 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700161 static shared_ptr<Interest>
Zhiyi Zhang222810b2020-10-16 21:50:35 -0700162 genChallengeInterest(RequesterState& state,
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700163 std::vector<std::tuple<std::string, std::string>>&& parameters);
164
tylerliudf6e5cc2020-10-05 18:52:13 -0700165 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700166 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700170 * @throw std::runtime_error if the decoding fails or receiving an error packet.
171 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700172 static void
173 onChallengeResponse(RequesterState& state, const Data& reply);
174
tylerliudf6e5cc2020-10-05 18:52:13 -0700175 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700176 * @brief Generate the interest to fetch the issued certificate
177 *
178 * @param state, the state of the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700179 * @return The shared pointer to the encoded interest
180 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700181 static shared_ptr<Interest>
182 genCertFetchInterest(const RequesterState& state);
183
tylerliudf6e5cc2020-10-05 18:52:13 -0700184 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700185 * @brief Decoded and installs the response certificate from the certificate fetch.
186 *
187 * @param reply, the data replied from the certificate fetch interest.
tylerliudf6e5cc2020-10-05 18:52:13 -0700188 * @return The shared pointer to the certificate being fetched.
189 */
tylerliua7bea662020-10-08 18:51:02 -0700190 static shared_ptr<security::Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700191 onCertFetchResponse(const Data& reply);
192
tylerliudf6e5cc2020-10-05 18:52:13 -0700193 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700194 * @brief End the current request session and performs cleanup if necessary.
195 *
196 * @param state, the requester state for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700197 */
tylerliufeabfdc2020-10-03 15:09:58 -0700198 static void
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700199 endSession(RequesterState& state);
200
201private:
202 static void
203 processIfError(const Data& data);
204};
205
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700206} // namespace requester
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700207} // namespace ndncert
208} // namespace ndn
209
tylerliu8704d032020-06-23 10:18:15 -0700210#endif // NDNCERT_REQUESTER_HPP