blob: b3655efc9ed7de388ae625fd5609fe6f00143039 [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
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070074 bool
75 verifyProbeInfoResponse(const Data& reply);
76
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080077 /**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 * @brief Process the replied PROBE INFO Data packet
79 * Warning: this function will add a new trust anchor into the application.
80 * Please invoke this function only when reply can be fully trusted or the CA
81 * can be verified in later challenge phase.
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080082 */
83 void
Zhiyi Zhangcaab5462019-10-18 13:41:02 -070084 addCaFromProbeInfoResponse(const Data& reply);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080085
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 shared_ptr<Interest>
87 generateProbeInterest(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +080088
89 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090 onProbeResponse(const Data& reply);
91
92 shared_ptr<Interest>
93 generateNewInterest(const time::system_clock::TimePoint& notBefore,
94 const time::system_clock::TimePoint& notAfter,
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070095 const Name& identityName = Name(), const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096
97 std::list<std::string>
98 onNewResponse(const Data& reply);
99
100 shared_ptr<Interest>
101 generateChallengeInterest(const JsonSection& paramJson);
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800102
103 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700104 onChallengeResponse(const Data& reply);
105
106 shared_ptr<Interest>
107 generateDownloadInterest();
108
109 shared_ptr<Interest>
110 generateCertFetchInterest();
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800111
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800112 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113 onDownloadResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800114
115 void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116 onCertFetchResponse(const Data& reply);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800117
118 // helper functions
119 static JsonSection
120 getJsonFromData(const Data& data);
121
122 static Block
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 paramFromJson(const JsonSection& json);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800124
Zhiyi Zhang547c8512019-06-18 23:46:14 -0700125 static std::vector<std::string>
126 parseProbeComponents(const std::string& probe);
127
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128PUBLIC_WITH_TESTS_ELSE_PRIVATE:
129 const JsonSection
Yufeng Zhang424d0362019-06-12 16:48:27 -0700130 genProbeRequestJson(const ClientCaItem& ca, const std::string& probeInfo);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800131
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700132 const JsonSection
Zhiyi Zhang5f749a22019-06-12 17:02:33 -0700133 genNewRequestJson(const std::string& ecdhPub, const security::v2::Certificate& certRequest,
134 const shared_ptr<Data>& probeToken = nullptr);
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800135
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800137 ClientConfig m_config;
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800138 security::v2::KeyChain& m_keyChain;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700139
140 ClientCaItem m_ca;
141 security::Key m_key;
142 Name m_identityName;
143
144 std::string m_requestId = "";
145 int m_status = STATUS_NOT_STARTED;
146 std::string m_challengeStatus = "";
147 std::string m_challengeType = "";
148 std::string m_certId = "";
149 std::list<std::string> m_challengeList;
150 bool m_isCertInstalled = false;
151
152 int m_remainingTries = 0;
153 time::system_clock::TimePoint m_freshBefore;
154
155 ECDHState m_ecdh;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800156 uint8_t m_aesKey[16] = {0};
Zhiyi Zhang23564c82017-03-01 10:22:22 -0800157};
158
159} // namespace ndncert
160} // namespace ndn
161
162#endif // NDNCERT_CLIENT_MODULE_HPP