blob: 2b6c3c3d73ea10d35ade120c6b2bbf6ce6694355 [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 Zhangfbcab842020-10-07 15:17:13 -070037 * 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.
tylerliudf6e5cc2020-10-05 18:52:13 -070040 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070041 static shared_ptr<Interest>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070042 genCaProfileDiscoveryInterest(const Name& caName);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070043
44 /**
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070045 * 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 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 Zhangfbcab842020-10-07 15:17:13 -070056 * @param reply, the Data packet replied from CA profile fetching Interest.
tylerliudf6e5cc2020-10-05 18:52:13 -070057 * @return the CaProfile if decoding is successful
58 * @throw std::runtime_error if the decoding fails or receiving an error packet.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070059 */
60 static boost::optional<CaProfile>
61 onCaProfileResponse(const Data& reply);
62
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070063 /**
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 Zhang837406d2020-10-05 22:01:31 -070073 static boost::optional<CaProfile>
74 onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName);
75
tylerliudf6e5cc2020-10-05 18:52:13 -070076 /**
77 * Generates a PROBE interest to the CA (for suggested name assignments).
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070078 * @param ca, the CA that interest is send to
79 * @param probeInfo, the requester information to carry to the CA
tylerliudf6e5cc2020-10-05 18:52:13 -070080 * @return A shared pointer of to the encoded interest, ready to be sent.
81 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070082 static shared_ptr<Interest>
83 genProbeInterest(const CaProfile& ca, std::vector<std::tuple<std::string, std::string>>&& probeInfo);
84
tylerliudf6e5cc2020-10-05 18:52:13 -070085 /**
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 Zhangfbcab842020-10-07 15:17:13 -070088 * @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.
tylerliudf6e5cc2020-10-05 18:52:13 -070092 * @throw std::runtime_error if the decoding fails or receiving an error packet.
93 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070094 static void
95 onProbeResponse(const Data& reply, const CaProfile& ca,
tylerliub47dad72020-10-08 21:36:55 -070096 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070097
98 // NEW/REVOKE/RENEW related helpers
tylerliudf6e5cc2020-10-05 18:52:13 -070099 /**
100 * Generates a NEW interest to the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700101 * @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)
tylerliudf6e5cc2020-10-05 18:52:13 -0700105 * @return The shared pointer to the encoded interest.
106 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700107 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
tylerliudf6e5cc2020-10-05 18:52:13 -0700112 /**
113 * Generates a REVOKE interest to the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700114 * @param state, The current requester state for this request. Will be modified in the function.
115 * @param certificate, the certificate to the revoked.
tylerliudf6e5cc2020-10-05 18:52:13 -0700116 * @return The shared pointer to the encoded interest.
117 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700118 static shared_ptr<Interest>
tylerliua7bea662020-10-08 18:51:02 -0700119 genRevokeInterest(RequesterState& state, const security::Certificate& certificate);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700120
tylerliudf6e5cc2020-10-05 18:52:13 -0700121 /**
122 * Decodes the replied data of NEW, RENEW, or REVOKE interest from the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700123 * @param state, the current requester state for the request. Will be updated in the function.
124 * @param reply, the replied data from the network
tylerliudf6e5cc2020-10-05 18:52:13 -0700125 * @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 Zhang1d3dcd22020-10-01 22:25:43 -0700128 static std::list<std::string>
129 onNewRenewRevokeResponse(RequesterState& state, const Data& reply);
130
131 // CHALLENGE helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700132 /**
133 * Generates the required parameter for the selected challenge for the request
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700134 * @param state, The requester state of the request.Will be updated in the function.
135 * @param challengeSelected, The selected challenge for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700136 * 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 Zhang1d3dcd22020-10-01 22:25:43 -0700139 static std::vector<std::tuple<std::string, std::string>>
140 selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected);
141
tylerliudf6e5cc2020-10-05 18:52:13 -0700142 /**
143 * Generates the CHALLENGE interest for the request.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700144 * @param state, The requester state of the request.
145 * @param parameters, The requirement list, in name, value mapping.
tylerliudf6e5cc2020-10-05 18:52:13 -0700146 * @return The shared pointer to the encoded interest
147 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700148 static shared_ptr<Interest>
149 genChallengeInterest(const RequesterState& state,
150 std::vector<std::tuple<std::string, std::string>>&& parameters);
151
tylerliudf6e5cc2020-10-05 18:52:13 -0700152 /**
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700153 * 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.
tylerliudf6e5cc2020-10-05 18:52:13 -0700156 * @throw std::runtime_error if the decoding fails or receiving an error packet.
157 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700158 static void
159 onChallengeResponse(RequesterState& state, const Data& reply);
160
tylerliudf6e5cc2020-10-05 18:52:13 -0700161 /**
162 * Generate the interest to fetch the issued certificate
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700163 * @param state, the state of the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700164 * @return The shared pointer to the encoded interest
165 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700166 static shared_ptr<Interest>
167 genCertFetchInterest(const RequesterState& state);
168
tylerliudf6e5cc2020-10-05 18:52:13 -0700169 /**
170 * Decoded and installs the response certificate from the certificate fetch.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700171 * @param reply, the data replied from the certificate fetch interest.
tylerliudf6e5cc2020-10-05 18:52:13 -0700172 * @return The shared pointer to the certificate being fetched.
173 */
tylerliua7bea662020-10-08 18:51:02 -0700174 static shared_ptr<security::Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700175 onCertFetchResponse(const Data& reply);
176
tylerliudf6e5cc2020-10-05 18:52:13 -0700177 /**
178 * End the current request session and performs cleanup if necessary.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700179 * @param state, the requester state for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700180 */
tylerliufeabfdc2020-10-03 15:09:58 -0700181 static void
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700182 endSession(RequesterState& state);
183
184private:
185 static void
186 processIfError(const Data& data);
187};
188
189} // namespace ndncert
190} // namespace ndn
191
tylerliu8704d032020-06-23 10:18:15 -0700192#endif // NDNCERT_REQUESTER_HPP