blob: 729d106dbe81d922c9b1bf860cdfc0056aeed0f3 [file] [log] [blame]
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu60775552022-03-07 17:10:10 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -07004 *
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
tylerliu4140fe82021-01-27 15:45:44 -080021#ifndef NDNCERT_REQUESTER_REQUEST_HPP
22#define NDNCERT_REQUESTER_REQUEST_HPP
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070023
tylerliu4140fe82021-01-27 15:45:44 -080024#include "detail/ca-request-state.hpp"
25#include "detail/crypto-helpers.hpp"
26#include "detail/profile-storage.hpp"
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070027
Davide Pesavento0dc02012021-11-23 22:55:03 -050028#include <ndn-cxx/security/key-chain.hpp>
29
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040030namespace ndncert::requester {
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070031
Davide Pesavento0dc02012021-11-23 22:55:03 -050032class Request : boost::noncopyable
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070033{
34public:
tylerliudf6e5cc2020-10-05 18:52:13 -070035 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070036 * @brief Generates a CA profile discovery Interest following RDR protocol.
37 *
38 * @param 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 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050041 static std::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 Zhang6d9eda62020-10-16 17:37:02 -070045 * @brief Generates a CA profile fetching Interest following RDR protocol.
46 *
47 * @param reply The Data packet replied from discovery Interest.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070048 * @return A shared pointer to an Interest ready to be sent.
49 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050050 static std::shared_ptr<Interest>
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070051 genCaProfileInterestFromDiscoveryResponse(const Data& reply);
52
53 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070054 * @brief Decodes the CA profile from the replied CA profile Data packet.
55 *
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070056 * Will first verify the signature of the packet using the key provided inside the profile.
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070057 * The application should be cautious whether to add CaProfile into the ProfileStorage.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070058 *
59 * @param reply The Data packet replied from CA profile fetching Interest.
tylerliudf6e5cc2020-10-05 18:52:13 -070060 * @return the CaProfile if decoding is successful
61 * @throw std::runtime_error if the decoding fails or receiving an error packet.
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070062 */
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040063 static std::optional<CaProfile>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070064 onCaProfileResponse(const Data& reply);
65
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070066 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070067 * @brief Decodes the CA profile from the replied CA profile Data packet after the redirection.
68 *
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070069 * Will first verify the signature of the packet using the key provided inside the profile and
70 * verify the certificate's digest matches the one obtained from the original CA.
Zhiyi Zhanga16b7582020-10-29 18:59:46 -070071 * The application should be cautious whether to add CaProfile into the ProfileStorage.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070072 *
73 * @param reply The Data packet replied from CA profile fetching Interest.
74 * @param caCertFullName The full name obtained from original CA's probe response.
Zhiyi Zhangfbcab842020-10-07 15:17:13 -070075 * @return the CaProfile if decoding is successful
76 * @throw std::runtime_error if the decoding fails or receiving an error packet.
77 */
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040078 static std::optional<CaProfile>
Zhiyi Zhang837406d2020-10-05 22:01:31 -070079 onCaProfileResponseAfterRedirection(const Data& reply, const Name& caCertFullName);
80
tylerliudf6e5cc2020-10-05 18:52:13 -070081 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070082 * @brief Generates a PROBE interest to the CA (for suggested name assignments).
83 *
84 * @param ca The CA that interest is send to
85 * @param probeInfo The requester information to carry to the CA
tylerliudf6e5cc2020-10-05 18:52:13 -070086 * @return A shared pointer of to the encoded interest, ready to be sent.
87 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050088 static std::shared_ptr<Interest>
tylerliu40226332020-11-11 15:37:16 -080089 genProbeInterest(const CaProfile& ca, std::multimap<std::string, std::string>&& probeInfo);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -070090
tylerliudf6e5cc2020-10-05 18:52:13 -070091 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070092 * @brief Decodes the replied data for PROBE process from the CA.
93 *
tylerliudf6e5cc2020-10-05 18:52:13 -070094 * Will first verify the signature of the packet using the key provided inside the profile.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070095 *
96 * @param reply The replied data packet
97 * @param ca the profile of the CA that replies the packet
98 * @param identityNames The vector to load the decoded identity names from the data.
99 * @param otherCas The vector to load the decoded redirection CA prefixes from the data.
tylerliudf6e5cc2020-10-05 18:52:13 -0700100 * @throw std::runtime_error if the decoding fails or receiving an error packet.
101 */
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700102 static void
103 onProbeResponse(const Data& reply, const CaProfile& ca,
tylerliub47dad72020-10-08 21:36:55 -0700104 std::vector<std::pair<Name, int>>& identityNames, std::vector<Name>& otherCas);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700105
tylerliu4140fe82021-01-27 15:45:44 -0800106 explicit
Davide Pesavento0dc02012021-11-23 22:55:03 -0500107 Request(ndn::KeyChain& keyChain, const CaProfile& profile, RequestType requestType);
tylerliu4140fe82021-01-27 15:45:44 -0800108
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700109 // NEW/REVOKE/RENEW related helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700110 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700111 * @brief Generates a NEW interest to the CA.
112 *
113 * @param state The current requester state for this request. Will be modified in the function.
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800114 * @param keyName The key name to be requested.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700115 * @param notBefore The expected notBefore field for the certificate (starting time)
116 * @param notAfter The expected notAfter field for the certificate (expiration time)
tylerliudf6e5cc2020-10-05 18:52:13 -0700117 * @return The shared pointer to the encoded interest.
118 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500119 std::shared_ptr<Interest>
Tianyuan Yuca23bb02022-03-09 14:09:14 -0800120 genNewInterest(const Name& keyName,
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700121 const time::system_clock::TimePoint& notBefore,
122 const time::system_clock::TimePoint& notAfter);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700123
tylerliudf6e5cc2020-10-05 18:52:13 -0700124 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700125 * @brief Generates a REVOKE interest to the CA.
126 *
127 * @param state The current requester state for this request. Will be modified in the function.
128 * @param certificate The certificate to the revoked.
tylerliudf6e5cc2020-10-05 18:52:13 -0700129 * @return The shared pointer to the encoded interest.
130 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500131 std::shared_ptr<Interest>
132 genRevokeInterest(const Certificate& certificate);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700133
tylerliudf6e5cc2020-10-05 18:52:13 -0700134 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700135 * @brief Decodes the replied data of NEW, RENEW, or REVOKE interest from the CA.
136 *
137 * @param state The current requester state for the request. Will be updated in the function.
138 * @param reply The replied data from the network
tylerliudf6e5cc2020-10-05 18:52:13 -0700139 * @return the list of challenge accepted by the CA, for CHALLENGE step.
140 * @throw std::runtime_error if the decoding fails or receiving an error packet.
141 */
tylerliu4140fe82021-01-27 15:45:44 -0800142 std::list<std::string>
143 onNewRenewRevokeResponse(const Data& reply);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700144
145 // CHALLENGE helpers
tylerliudf6e5cc2020-10-05 18:52:13 -0700146 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700147 * @brief Generates the required parameter for the selected challenge for the request
148 *
149 * @param state, The requester state of the request.Will be updated in the function.
150 * @param challengeSelected, The selected challenge for the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700151 * Can use state.m_challengeType to continue.
152 * @return The requirement list for the current stage of the challenge, in name, prompt mapping.
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700153 * @throw std::runtime_error if the challenge is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700154 */
tylerliu4140fe82021-01-27 15:45:44 -0800155 std::multimap<std::string, std::string>
156 selectOrContinueChallenge(const std::string& challengeSelected);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700157
tylerliudf6e5cc2020-10-05 18:52:13 -0700158 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700159 * @brief Generates the CHALLENGE interest for the request.
160 *
161 * @param state, The requester state of the request.
162 * @param parameters, The requirement list, in name, value mapping.
tylerliudf6e5cc2020-10-05 18:52:13 -0700163 * @return The shared pointer to the encoded interest
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700164 * @throw std::runtime_error if the challenge is not selected or is not supported.
tylerliudf6e5cc2020-10-05 18:52:13 -0700165 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500166 std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800167 genChallengeInterest(std::multimap<std::string, std::string>&& parameters);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700168
tylerliudf6e5cc2020-10-05 18:52:13 -0700169 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700170 * @brief Decodes the responded data from the CHALLENGE interest.
171 *
172 * @param state, the corresponding requester state of the request. Will be modified.
173 * @param reply, the response data.
tylerliudf6e5cc2020-10-05 18:52:13 -0700174 * @throw std::runtime_error if the decoding fails or receiving an error packet.
175 */
tylerliu4140fe82021-01-27 15:45:44 -0800176 void
177 onChallengeResponse(const Data& reply);
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700178
tylerliudf6e5cc2020-10-05 18:52:13 -0700179 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700180 * @brief Generate the interest to fetch the issued certificate
181 *
182 * @param state, the state of the request.
tylerliudf6e5cc2020-10-05 18:52:13 -0700183 * @return The shared pointer to the encoded interest
184 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500185 std::shared_ptr<Interest>
tylerliu4140fe82021-01-27 15:45:44 -0800186 genCertFetchInterest() const;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700187
tylerliudf6e5cc2020-10-05 18:52:13 -0700188 /**
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -0700189 * @brief Decoded and installs the response certificate from the certificate fetch.
190 *
191 * @param reply, the data replied from the certificate fetch interest.
tylerliudf6e5cc2020-10-05 18:52:13 -0700192 * @return The shared pointer to the certificate being fetched.
193 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500194 static std::shared_ptr<Certificate>
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700195 onCertFetchResponse(const Data& reply);
196
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700197private:
198 static void
199 processIfError(const Data& data);
tylerliu4140fe82021-01-27 15:45:44 -0800200
201public:
202 /**
203 * @brief The CA profile for this request.
204 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800205 CaProfile m_caProfile;
tylerliu4140fe82021-01-27 15:45:44 -0800206 /**
207 * @brief The type of request. Either NEW, RENEW, or REVOKE.
208 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800209 RequestType m_type;
tylerliu4140fe82021-01-27 15:45:44 -0800210 /**
211 * @brief The identity name for the requesting certificate.
212 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800213 Name m_identityName;
tylerliu4140fe82021-01-27 15:45:44 -0800214 /**
215 * @brief The CA-generated request ID for the request.
216 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800217 RequestId m_requestId;
tylerliu4140fe82021-01-27 15:45:44 -0800218 /**
219 * @brief The current status of the request.
220 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800221 Status m_status = Status::BEFORE_CHALLENGE;
tylerliu4140fe82021-01-27 15:45:44 -0800222 /**
223 * @brief The type of challenge chosen.
224 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800225 std::string m_challengeType;
tylerliu4140fe82021-01-27 15:45:44 -0800226 /**
227 * @brief The status of the current challenge.
228 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800229 std::string m_challengeStatus;
tylerliu4140fe82021-01-27 15:45:44 -0800230 /**
231 * @brief The remaining number of tries left for the challenge
232 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800233 int m_remainingTries = 0;
tylerliu4140fe82021-01-27 15:45:44 -0800234 /**
235 * @brief The time this challenge will remain fresh
236 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800237 time::system_clock::TimePoint m_freshBefore;
tylerliu4140fe82021-01-27 15:45:44 -0800238 /**
239 * @brief the name of the certificate being issued.
240 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800241 Name m_issuedCertName;
tylerliu4140fe82021-01-27 15:45:44 -0800242 /**
Tianyuan Yu60775552022-03-07 17:10:10 -0800243 * @brief The optional forwarding hint.
244 */
245 Name m_forwardingHint;
246 /**
tylerliu4140fe82021-01-27 15:45:44 -0800247 * @brief ecdh state.
248 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800249 ECDHState m_ecdh;
tylerliu4140fe82021-01-27 15:45:44 -0800250 /**
251 * @brief AES key derived from the ecdh shared secret.
252 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800253 std::array<uint8_t, 16> m_aesKey = {};
tylerliu4140fe82021-01-27 15:45:44 -0800254 /**
255 * @brief The last Initialization Vector used by the AES encryption.
256 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800257 std::vector<uint8_t> m_encryptionIv;
tylerliu4140fe82021-01-27 15:45:44 -0800258 /**
259 * @brief The last Initialization Vector used by the other side's AES encryption.
260 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800261 std::vector<uint8_t> m_decryptionIv;
tylerliu4140fe82021-01-27 15:45:44 -0800262 /**
263 * @brief Store Nonce for signature
264 */
Zhiyi Zhang6499edd2021-02-17 22:37:21 -0800265 std::array<uint8_t, 16> m_nonce = {};
Zhiyi Zhang3f1c7cf2021-02-17 14:08:14 -0800266
tylerliu4140fe82021-01-27 15:45:44 -0800267private:
268 /**
269 * @brief The local keychain to generate and install identities, keys and certificates
270 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500271 ndn::KeyChain& m_keyChain;
tylerliu4140fe82021-01-27 15:45:44 -0800272 /**
tylerliu4140fe82021-01-27 15:45:44 -0800273 * @brief The keypair for the request.
274 */
Davide Pesavento0dc02012021-11-23 22:55:03 -0500275 ndn::security::Key m_keyPair;
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700276};
277
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400278} // namespace ndncert::requester
Zhiyi Zhang1d3dcd22020-10-01 22:25:43 -0700279
tylerliu4140fe82021-01-27 15:45:44 -0800280#endif // NDNCERT_REQUESTER_REQUEST_HPP