blob: fb20d4bf47d848de095aff8965e2fb53129d4caf [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>
71 generateProbeInfoInterest(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 Zhangad9e04f2020-03-27 12:04:31 -0700111 shared_ptr<security::v2::Certificate>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112 onDownloadResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800113
114 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115 onCertFetchResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800116
117 // helper functions
118 static JsonSection
119 getJsonFromData(const Data& data);
120
121 static Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122 paramFromJson(const JsonSection& json);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800123
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700124 static std::vector<std::string>
125 parseProbeComponents(const std::string& probe);
126
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700127 void
128 endSession();
129
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700130PUBLIC_WITH_TESTS_ELSE_PRIVATE:
131 const JsonSection
Yufeng Zhang424d0362019-06-12 16:48:27 -0700132 genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800133
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700134 const JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700135 genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
136 const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800137
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800139 ClientConfig m_config;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800140 security::v2::KeyChain& m_keyChain;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141
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 Zhangad9e04f2020-03-27 12:04:31 -0700153 bool m_isNewlyCreatedIdentity = false;
154 bool m_isNewlyCreatedKey = false;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700155
156 int m_remainingTries = 0;
157 time::system_clock::TimePoint m_freshBefore;
158
159 ECDHState m_ecdh;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800160 uint8_t m_aesKey[16] = {0};
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800161};
162
163} // namespace ndncert
164} // namespace ndn
165
166#endif // NDNCERT_CLIENT_MODULE_HPP