blob: 57ae01f9753a3d811593393b89eea8b99a57cf07 [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang23564c82017-03-01 10:22:22 -08004 *
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 Zhangad9e04f2020-03-27 12:04:31 -070026#include "crypto-support/crypto-helper.hpp"
Zhiyi Zhang23564c82017-03-01 10:22:22 -080027
28namespace ndn {
29namespace ndncert {
30
Zhiyi Zhang23564c82017-03-01 10:22:22 -080031// 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
35class ClientModule : noncopyable
36{
37public:
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 Zhang23564c82017-03-01 10:22:22 -080047public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 ClientModule(security::v2::KeyChain& keyChain);
Zhiyi Zhang23564c82017-03-01 10:22:22 -080049
Davide Pesavento08994782018-01-22 12:13:41 -050050 ~ClientModule();
51
Zhiyi Zhang23564c82017-03-01 10:22:22 -080052 ClientConfig&
53 getClientConf()
54 {
55 return m_config;
56 }
57
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 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>
swa77020643ac2020-03-26 02:24:45 -070071 generateInfoInterest(const Name& caName);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080072
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070073 bool
74 verifyProbeInfoResponse(const Data& reply);
75
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080076 /**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077 * @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 Zhang1c0bd372017-12-18 18:32:55 +080081 */
82 void
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070083 addCaFromProbeInfoResponse(const Data& reply);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080084
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 shared_ptr<Interest>
86 generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080087
88 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089 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 Zhang5f749a22019-06-12 17:02:33 -070094 const Name& identityName = Name(), const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095
96 std::list<std::string>
97 onNewResponse(const Data& reply);
98
99 shared_ptr<Interest>
100 generateChallengeInterest(const JsonSection& paramJson);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800101
102 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103 onChallengeResponse(const Data& reply);
104
105 shared_ptr<Interest>
106 generateDownloadInterest();
107
108 shared_ptr<Interest>
109 generateCertFetchInterest();
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800110
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800111 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112 onCertFetchResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800113
114 // helper functions
115 static JsonSection
116 getJsonFromData(const Data& data);
117
118 static Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 paramFromJson(const JsonSection& json);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800120
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700121 static std::vector<std::string>
122 parseProbeComponents(const std::string& probe);
123
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700124 void
125 endSession();
126
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700127PUBLIC_WITH_TESTS_ELSE_PRIVATE:
swa77020643ac2020-03-26 02:24:45 -0700128 JsonSection
Yufeng Zhang424d0362019-06-12 16:48:27 -0700129 genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800130
swa77020643ac2020-03-26 02:24:45 -0700131 JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700132 genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
133 const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800134
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700135PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800136 ClientConfig m_config;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800137 security::v2::KeyChain& m_keyChain;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138
139 ClientCaItem m_ca;
140 security::Key m_key;
141 Name m_identityName;
142
143 std::string m_requestId = "";
144 int m_status = STATUS_NOT_STARTED;
145 std::string m_challengeStatus = "";
146 std::string m_challengeType = "";
147 std::string m_certId = "";
swa770cf1d8f72020-04-21 23:12:39 -0700148 std::string m_issuedCertName = "";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700149 std::list<std::string> m_challengeList;
150 bool m_isCertInstalled = false;
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700151 bool m_isNewlyCreatedIdentity = false;
152 bool m_isNewlyCreatedKey = false;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700153
154 int m_remainingTries = 0;
155 time::system_clock::TimePoint m_freshBefore;
156
157 ECDHState m_ecdh;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800158 uint8_t m_aesKey[16] = {0};
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800159};
160
161} // namespace ndncert
162} // namespace ndn
163
164#endif // NDNCERT_CLIENT_MODULE_HPP