blob: 0267803f5dcf627c868234e8bdb1880011c645be [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.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070038 * @p caName, the name prefix of the CA.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070039 * @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.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070046 * @p reply, the Data packet replied from discovery Interest.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070047 * @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 Zhang97bedb82020-10-10 11:11:35 -070056 * @p 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.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -070068 * @p reply, the Data packet replied from CA profile fetching Interest.
69 * @p caCertFullName, the full name obtained from original CA's probe response.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070070 * @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 Zhang97bedb82020-10-10 11:11:35 -070078 * @p ca, the CA that interest is send to
79 * @p 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 Zhang97bedb82020-10-10 11:11:35 -070088 * @p reply, The replied data packet
89 * @p ca, the profile of the CA that replies the packet
90 * @p identityNames, The vector to load the decoded identity names from the data.
91 * @p 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 Zhang97bedb82020-10-10 11:11:35 -0700101 * @p state, The current requester state for this request. Will be modified in the function.
102 * @p identityName, The identity name to be requested.
103 * @p notBefore, The expected notBefore field for the certificate (starting time)
104 * @p 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,
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700109 const time::system_clock::TimePoint& notBefore,
110 const time::system_clock::TimePoint& notAfter);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700111
tylerliudf6e5cc2020-10-05 18:52:13 -0700112 /**
113 * Generates a REVOKE interest to the CA.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700114 * @p state, The current requester state for this request. Will be modified in the function.
115 * @p 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 Zhang97bedb82020-10-10 11:11:35 -0700123 * @p state, the current requester state for the request. Will be updated in the function.
124 * @p 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 Zhang97bedb82020-10-10 11:11:35 -0700134 * @p state, The requester state of the request.Will be updated in the function.
135 * @p 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.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700138 * @throw std::runtime_error if the challenge is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700139 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700140 static std::vector<std::tuple<std::string, std::string>>
141 selectOrContinueChallenge(RequesterState& state, const std::string& challengeSelected);
142
tylerliudf6e5cc2020-10-05 18:52:13 -0700143 /**
144 * Generates the CHALLENGE interest for the request.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700145 * @p state, The requester state of the request.
146 * @p parameters, The requirement list, in name, value mapping.
tylerliudf6e5cc2020-10-05 18:52:13 -0700147 * @return The shared pointer to the encoded interest
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700148 * @throw std::runtime_error if the challenge is not selected or is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700149 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700150 static shared_ptr<Interest>
151 genChallengeInterest(const RequesterState& state,
152 std::vector<std::tuple<std::string, std::string>>&& parameters);
153
tylerliudf6e5cc2020-10-05 18:52:13 -0700154 /**
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700155 * Decodes the responded data from the CHALLENGE interest.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700156 * @p state, the corresponding requester state of the request. Will be modified.
157 * @p reply, the response data.
tylerliudf6e5cc2020-10-05 18:52:13 -0700158 * @throw std::runtime_error if the decoding fails or receiving an error packet.
159 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700160 static void
161 onChallengeResponse(RequesterState& state, const Data& reply);
162
tylerliudf6e5cc2020-10-05 18:52:13 -0700163 /**
164 * Generate the interest to fetch the issued certificate
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700165 * @p state, the state of the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700166 * @return The shared pointer to the encoded interest
167 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700168 static shared_ptr<Interest>
169 genCertFetchInterest(const RequesterState& state);
170
tylerliudf6e5cc2020-10-05 18:52:13 -0700171 /**
172 * Decoded and installs the response certificate from the certificate fetch.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700173 * @p reply, the data replied from the certificate fetch interest.
tylerliudf6e5cc2020-10-05 18:52:13 -0700174 * @return The shared pointer to the certificate being fetched.
175 */
tylerliua7bea662020-10-08 18:51:02 -0700176 static shared_ptr<security::Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700177 onCertFetchResponse(const Data& reply);
178
tylerliudf6e5cc2020-10-05 18:52:13 -0700179 /**
180 * End the current request session and performs cleanup if necessary.
Zhiyi Zhang97bedb82020-10-10 11:11:35 -0700181 * @p state, the requester state for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700182 */
tylerliufeabfdc2020-10-03 15:09:58 -0700183 static void
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700184 endSession(RequesterState& state);
185
186private:
187 static void
188 processIfError(const Data& data);
189};
190
191} // namespace ndncert
192} // namespace ndn
193
tylerliu8704d032020-06-23 10:18:15 -0700194#endif // NDNCERT_REQUESTER_HPP