blob: 388ad8543e81f82383e0f837b6ce442e8c52511f [file] [log] [blame]
Zhiyi Zhang23564c82017-03-01 10:22:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#include "crypto-support/crypto-helper.hpp"
Zhiyi Zhang23564c82017-03-01 10:22:22 -080026#include "certificate-request.hpp"
27
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 virtual
51 ~ClientModule();
52
Zhiyi Zhang23564c82017-03-01 10:22:22 -080053 ClientConfig&
54 getClientConf()
55 {
56 return m_config;
57 }
58
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 int
60 getApplicationStatus() const
61 {
62 return m_status;
63 }
64
65 std::string
66 getChallengeStatus() const
67 {
68 return m_challengeStatus;
69 }
70
71 shared_ptr<Interest>
72 generateProbeInfoInterest(const Name& caName);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080073
74 /**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075 * @brief Process the replied PROBE INFO Data packet
76 * Warning: this function will add a new trust anchor into the application.
77 * Please invoke this function only when reply can be fully trusted or the CA
78 * can be verified in later challenge phase.
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080079 */
80 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081 onProbeInfoResponse(const Data& reply);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080082
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083 shared_ptr<Interest>
84 generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080085
86 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087 onProbeResponse(const Data& reply);
88
89 shared_ptr<Interest>
90 generateNewInterest(const time::system_clock::TimePoint& notBefore,
91 const time::system_clock::TimePoint& notAfter,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070092 const Name& identityName = Name(), const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070093
94 std::list<std::string>
95 onNewResponse(const Data& reply);
96
97 shared_ptr<Interest>
98 generateChallengeInterest(const JsonSection& paramJson);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080099
100 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101 onChallengeResponse(const Data& reply);
102
103 shared_ptr<Interest>
104 generateDownloadInterest();
105
106 shared_ptr<Interest>
107 generateCertFetchInterest();
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800108
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800109 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110 onDownloadResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800111
112 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113 onCertFetchResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800114
115 // helper functions
116 static JsonSection
117 getJsonFromData(const Data& data);
118
119 static Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120 paramFromJson(const JsonSection& json);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800121
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122PUBLIC_WITH_TESTS_ELSE_PRIVATE:
123 const JsonSection
Yufeng Zhang424d0362019-06-12 16:48:27 -0700124 genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800125
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700126 const JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700127 genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
128 const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800129
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700130PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800131 ClientConfig m_config;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800132 security::v2::KeyChain& m_keyChain;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700133
134 ClientCaItem m_ca;
135 security::Key m_key;
136 Name m_identityName;
137
138 std::string m_requestId = "";
139 int m_status = STATUS_NOT_STARTED;
140 std::string m_challengeStatus = "";
141 std::string m_challengeType = "";
142 std::string m_certId = "";
143 std::list<std::string> m_challengeList;
144 bool m_isCertInstalled = false;
145
146 int m_remainingTries = 0;
147 time::system_clock::TimePoint m_freshBefore;
148
149 ECDHState m_ecdh;
150 uint8_t m_aesKey[32] = {0};
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800151};
152
153} // namespace ndncert
154} // namespace ndn
155
156#endif // NDNCERT_CLIENT_MODULE_HPP