Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 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 | |
| 21 | #ifndef NDNCERT_CLIENT_MODULE_HPP |
| 22 | #define NDNCERT_CLIENT_MODULE_HPP |
| 23 | |
| 24 | #include "client-config.hpp" |
| 25 | #include "certificate-request.hpp" |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame^] | 26 | #include "crypto-support/crypto-helper.hpp" |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 31 | // TODO |
| 32 | // For each CA item in Client.Conf, create a validator instance and initialize it with CA's cert |
| 33 | // The validator instance should be in ClientCaItem |
| 34 | |
| 35 | class ClientModule : noncopyable |
| 36 | { |
| 37 | public: |
| 38 | /** |
| 39 | * @brief Error that can be thrown from ClientModule |
| 40 | */ |
| 41 | class Error : public std::runtime_error |
| 42 | { |
| 43 | public: |
| 44 | using std::runtime_error::runtime_error; |
| 45 | }; |
| 46 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 47 | public: |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | ClientModule(security::v2::KeyChain& keyChain); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 49 | |
Davide Pesavento | 0899478 | 2018-01-22 12:13:41 -0500 | [diff] [blame] | 50 | ~ClientModule(); |
| 51 | |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 52 | ClientConfig& |
| 53 | getClientConf() |
| 54 | { |
| 55 | return m_config; |
| 56 | } |
| 57 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | int |
| 59 | getApplicationStatus() const |
| 60 | { |
| 61 | return m_status; |
| 62 | } |
| 63 | |
| 64 | std::string |
| 65 | getChallengeStatus() const |
| 66 | { |
| 67 | return m_challengeStatus; |
| 68 | } |
| 69 | |
| 70 | shared_ptr<Interest> |
| 71 | generateProbeInfoInterest(const Name& caName); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 72 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 73 | bool |
| 74 | verifyProbeInfoResponse(const Data& reply); |
| 75 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 76 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 77 | * @brief Process the replied PROBE INFO Data packet |
| 78 | * Warning: this function will add a new trust anchor into the application. |
| 79 | * Please invoke this function only when reply can be fully trusted or the CA |
| 80 | * can be verified in later challenge phase. |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 81 | */ |
| 82 | void |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 83 | addCaFromProbeInfoResponse(const Data& reply); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 84 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | shared_ptr<Interest> |
| 86 | generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 87 | |
| 88 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | onProbeResponse(const Data& reply); |
| 90 | |
| 91 | shared_ptr<Interest> |
| 92 | generateNewInterest(const time::system_clock::TimePoint& notBefore, |
| 93 | const time::system_clock::TimePoint& notAfter, |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 94 | const Name& identityName = Name(), const shared_ptr<Data>& probeToken = nullptr); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 95 | |
| 96 | std::list<std::string> |
| 97 | onNewResponse(const Data& reply); |
| 98 | |
| 99 | shared_ptr<Interest> |
| 100 | generateChallengeInterest(const JsonSection& paramJson); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 101 | |
| 102 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 103 | onChallengeResponse(const Data& reply); |
| 104 | |
| 105 | shared_ptr<Interest> |
| 106 | generateDownloadInterest(); |
| 107 | |
| 108 | shared_ptr<Interest> |
| 109 | generateCertFetchInterest(); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 110 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame^] | 111 | shared_ptr<security::v2::Certificate> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 112 | onDownloadResponse(const Data& reply); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 113 | |
| 114 | void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 115 | onCertFetchResponse(const Data& reply); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 116 | |
| 117 | // helper functions |
| 118 | static JsonSection |
| 119 | getJsonFromData(const Data& data); |
| 120 | |
| 121 | static Block |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 122 | paramFromJson(const JsonSection& json); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 123 | |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 124 | static std::vector<std::string> |
| 125 | parseProbeComponents(const std::string& probe); |
| 126 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame^] | 127 | void |
| 128 | endSession(); |
| 129 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 130 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 131 | const JsonSection |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 132 | genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 133 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 134 | const JsonSection |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 135 | genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest, |
| 136 | const shared_ptr<Data>& probeToken = nullptr); |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 137 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 138 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 139 | ClientConfig m_config; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 140 | security::v2::KeyChain& m_keyChain; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | |
| 142 | ClientCaItem m_ca; |
| 143 | security::Key m_key; |
| 144 | Name m_identityName; |
| 145 | |
| 146 | std::string m_requestId = ""; |
| 147 | int m_status = STATUS_NOT_STARTED; |
| 148 | std::string m_challengeStatus = ""; |
| 149 | std::string m_challengeType = ""; |
| 150 | std::string m_certId = ""; |
| 151 | std::list<std::string> m_challengeList; |
| 152 | bool m_isCertInstalled = false; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame^] | 153 | bool m_isNewlyCreatedIdentity = false; |
| 154 | bool m_isNewlyCreatedKey = false; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 155 | |
| 156 | int m_remainingTries = 0; |
| 157 | time::system_clock::TimePoint m_freshBefore; |
| 158 | |
| 159 | ECDHState m_ecdh; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 160 | uint8_t m_aesKey[16] = {0}; |
Zhiyi Zhang | 23564c8 | 2017-03-01 10:22:22 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // namespace ndncert |
| 164 | } // namespace ndn |
| 165 | |
| 166 | #endif // NDNCERT_CLIENT_MODULE_HPP |