blob: 65cf831dd426f123cffcfea13657ed929387c954 [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 {
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 Zhang1d3dcd22020-10-01 22:25:43 -070033class Requester : noncopyable
34{
35public:
tylerliudf6e5cc2020-10-05 18:52:13 -070036 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070037 * @brief Generates a CA profile discovery Interest following RDR protocol.
38 *
39 * @param caName The name prefix of the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070040 * @return A shared pointer to an Interest ready to be sent.
tylerliudf6e5cc2020-10-05 18:52:13 -070041 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070042 static shared_ptr<Interest>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070043 genCaProfileDiscoveryInterest(const Name& caName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070044
45 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070046 * @brief Generates a CA profile fetching Interest following RDR protocol.
47 *
48 * @param reply The Data packet replied from discovery Interest.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070049 * @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 Zhang6d9eda62020-10-16 17:37:02 -070055 * @brief Decodes the CA profile from the replied CA profile Data packet.
56 *
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070057 * 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 Zhang6d9eda62020-10-16 17:37:02 -070059 *
60 * @param reply The Data packet replied from CA profile fetching Interest.
tylerliudf6e5cc2020-10-05 18:52:13 -070061 * @return the CaProfile if decoding is successful
62 * @throw std::runtime_error if the decoding fails or receiving an error packet.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070063 */
64 static boost::optional<CaProfile>
65 onCaProfileResponse(const Data& reply);
66
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070067 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070068 * @brief Decodes the CA profile from the replied CA profile Data packet after the redirection.
69 *
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070070 * 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 Zhang6d9eda62020-10-16 17:37:02 -070073 *
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 Zhangfbcab842020-10-07 15:17:13 -070076 * @return the CaProfile if decoding is successful
77 * @throw std::runtime_error if the decoding fails or receiving an error packet.
78 */
Zhiyi Zhang837406d2020-10-05 22:01:31 -070079 static boost::optional<CaProfile>
80 onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName);
81
tylerliudf6e5cc2020-10-05 18:52:13 -070082 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070083 * @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
tylerliudf6e5cc2020-10-05 18:52:13 -070087 * @return A shared pointer of to the encoded interest, ready to be sent.
88 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070089 static shared_ptr<Interest>
90 genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo);
91
tylerliudf6e5cc2020-10-05 18:52:13 -070092 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070093 * @brief Decodes the replied data for PROBE process from the CA.
94 *
tylerliudf6e5cc2020-10-05 18:52:13 -070095 * Will first verify the signature of the packet using the key provided inside the profile.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070096 *
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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700101 * @throw std::runtime_error if the decoding fails or receiving an error packet.
102 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700103 static void
104 onProbeResponse(const Data& reply, const CaProfile& ca,
tylerliub47dad72020-10-08 21:36:55 -0700105 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700106
107 // NEW/REVOKE/RENEW related helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700108 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700109 * @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)
tylerliudf6e5cc2020-10-05 18:52:13 -0700115 * @return The shared pointer to the encoded interest.
116 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700117 static shared_ptr<Interest>
118 genNewInterest(RequesterState& state, const Name& identityName,
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700119 const time::system_clock::TimePoint& notBefore,
120 const time::system_clock::TimePoint& notAfter);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700121
tylerliudf6e5cc2020-10-05 18:52:13 -0700122 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700123 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700127 * @return The shared pointer to the encoded interest.
128 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700129 static shared_ptr<Interest>
tylerliua7bea662020-10-08 18:51:02 -0700130 genRevokeInterest(RequesterState& state, const security::Certificate& certificate);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700131
tylerliudf6e5cc2020-10-05 18:52:13 -0700132 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700133 * @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
tylerliudf6e5cc2020-10-05 18:52:13 -0700137 * @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 Zhang1d3dcd22020-10-01 22:25:43 -0700140 static std::list<std::string>
141 onNewRenewRevokeResponse(RequesterState& state, const Data& reply);
142
143 // CHALLENGE helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700144 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700145 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700149 * Can use state.m_challengeType to continue.
150 * @return The requirement list for the current stage of the challenge, in name, prompt mapping.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700151 * @throw std::runtime_error if the challenge is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700152 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700153 static std::vector<std::tuple<std::string, std::string>>
154 selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected);
155
tylerliudf6e5cc2020-10-05 18:52:13 -0700156 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700157 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700161 * @return The shared pointer to the encoded interest
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700162 * @throw std::runtime_error if the challenge is not selected or is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700163 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700164 static shared_ptr<Interest>
165 genChallengeInterest(const RequesterState& state,
166 std::vector<std::tuple<std::string, std::string>>&& parameters);
167
tylerliudf6e5cc2020-10-05 18:52:13 -0700168 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700169 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700173 * @throw std::runtime_error if the decoding fails or receiving an error packet.
174 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700175 static void
176 onChallengeResponse(RequesterState& state, const Data& reply);
177
tylerliudf6e5cc2020-10-05 18:52:13 -0700178 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700179 * @brief Generate the interest to fetch the issued certificate
180 *
181 * @param state, the state of the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700182 * @return The shared pointer to the encoded interest
183 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700184 static shared_ptr<Interest>
185 genCertFetchInterest(const RequesterState& state);
186
tylerliudf6e5cc2020-10-05 18:52:13 -0700187 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700188 * @brief Decoded and installs the response certificate from the certificate fetch.
189 *
190 * @param reply, the data replied from the certificate fetch interest.
tylerliudf6e5cc2020-10-05 18:52:13 -0700191 * @return The shared pointer to the certificate being fetched.
192 */
tylerliua7bea662020-10-08 18:51:02 -0700193 static shared_ptr<security::Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700194 onCertFetchResponse(const Data& reply);
195
tylerliudf6e5cc2020-10-05 18:52:13 -0700196 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700197 * @brief End the current request session and performs cleanup if necessary.
198 *
199 * @param state, the requester state for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700200 */
tylerliufeabfdc2020-10-03 15:09:58 -0700201 static void
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700202 endSession(RequesterState& state);
203
204private:
205 static void
206 processIfError(const Data& data);
207};
208
209} // namespace ndncert
210} // namespace ndn
211
tylerliu8704d032020-06-23 10:18:15 -0700212#endif // NDNCERT_REQUESTER_HPP