-
Notifications
You must be signed in to change notification settings - Fork 9
/
contact.hpp
253 lines (213 loc) · 5.53 KB
/
contact.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2020, Regents of the University of California
* Yingdi Yu
*
* BSD license, See the LICENSE file for more information
*
* Author: Yingdi Yu <[email protected]>
*/
#ifndef CHRONOCHAT_CONTACT_HPP
#define CHRONOCHAT_CONTACT_HPP
#include "common.hpp"
#include "endorse-certificate.hpp"
#include "profile.hpp"
#include <ndn-cxx/security/certificate.hpp>
#include <ndn-cxx/util/regex.hpp>
namespace chronochat {
class Contact
{
public:
typedef std::map<Name, shared_ptr<ndn::Regex> >::const_iterator const_iterator;
typedef std::map<Name, shared_ptr<ndn::Regex> >::iterator iterator;
Contact(const ndn::security::Certificate& identityCertificate,
bool isIntroducer = false,
const std::string& alias = "")
: m_notBefore(time::system_clock::now())
, m_notAfter(time::system_clock::now() + time::days(3650))
, m_isIntroducer(isIntroducer)
, m_profile(identityCertificate)
{
m_keyName = identityCertificate.getKeyName();
m_namespace = m_keyName.getPrefix(-2);
m_publicKey = identityCertificate.getPublicKey();
m_name = m_profile.get("name");
m_name = m_name.empty() ? m_namespace.toUri() : m_name;
m_alias = alias.empty() ? m_name : alias;
m_institution = m_profile.get("institution");
try {
m_notBefore = identityCertificate.getValidityPeriod().getPeriod().first;
m_notAfter = identityCertificate.getValidityPeriod().getPeriod().second;
} catch (const tlv::Error&) {}
}
Contact(const EndorseCertificate& endorseCertificate,
bool isIntroducer = false,
const std::string& alias = "")
: m_notBefore(time::system_clock::now())
, m_notAfter(time::system_clock::now() + time::days(3650))
, m_isIntroducer(isIntroducer)
{
m_profile = endorseCertificate.getProfile();
m_keyName = endorseCertificate.getKeyName().getPrefix(-1).append("KEY");
m_namespace = m_keyName.getPrefix(-3);
m_publicKey = endorseCertificate.getPublicKey();
m_name = m_profile.get("name");
m_name = m_name.empty() ? m_namespace.toUri() : m_name;
m_alias = alias.empty() ? m_name : alias;
m_institution = m_profile.get("institution");
try {
m_notBefore = endorseCertificate.getValidityPeriod().getPeriod().first;
m_notAfter = endorseCertificate.getValidityPeriod().getPeriod().second;
} catch (const tlv::Error&) {}
}
Contact(const Name& identity,
const std::string& alias,
const Name& keyName,
const time::system_clock::TimePoint& notBefore,
const time::system_clock::TimePoint& notAfter,
const ndn::Buffer& key,
bool isIntroducer)
: m_namespace(identity)
, m_alias(alias)
, m_keyName(keyName)
, m_publicKey(key)
, m_notBefore(notBefore)
, m_notAfter(notAfter)
, m_isIntroducer(isIntroducer)
{
}
Contact(const Contact& contact)
: m_namespace(contact.m_namespace)
, m_alias(contact.m_alias)
, m_name(contact.m_name)
, m_institution(contact.m_institution)
, m_keyName(contact.m_keyName)
, m_publicKey(contact.m_publicKey)
, m_notBefore(contact.m_notBefore)
, m_notAfter(contact.m_notAfter)
, m_isIntroducer(contact.m_isIntroducer)
, m_profile(contact.m_profile)
, m_trustScope(contact.m_trustScope)
{
}
virtual
~Contact()
{
}
const Name&
getNameSpace() const
{
return m_namespace;
}
const std::string&
getAlias() const
{
return m_alias;
}
const std::string&
getName() const
{
return m_name;
}
const std::string&
getInstitution() const
{
return m_institution;
}
const Name&
getPublicKeyName() const
{
return m_keyName;
}
const ndn::Buffer&
getPublicKey() const
{
return m_publicKey;
}
const time::system_clock::TimePoint&
getNotBefore() const
{
return m_notBefore;
}
const time::system_clock::TimePoint&
getNotAfter() const
{
return m_notAfter;
}
const Profile&
getProfile() const
{
return m_profile;
}
void
setProfile(const Profile& profile)
{
m_name = profile.get("name");
m_institution = profile.get("institution");
m_profile = profile;
}
bool
isIntroducer() const
{
return m_isIntroducer;
}
void
setIsIntroducer(bool isIntroducer)
{
m_isIntroducer = isIntroducer;
}
void
addTrustScope(const Name& nameSpace)
{
m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace);
}
void
deleteTrustScope(const Name& nameSpace)
{
m_trustScope.erase(nameSpace);
}
bool
canBeTrustedFor(const Name& name)
{
for (std::map<Name, shared_ptr<ndn::Regex> >::iterator it = m_trustScope.begin();
it != m_trustScope.end(); it++)
if (it->second->match(name))
return true;
return false;
}
const_iterator
trustScopeBegin() const
{
return m_trustScope.begin();
}
const_iterator
trustScopeEnd() const
{
return m_trustScope.end();
}
iterator
trustScopeBegin()
{
return m_trustScope.begin();
}
iterator
trustScopeEnd()
{
return m_trustScope.end();
}
protected:
typedef std::map<Name, shared_ptr<ndn::Regex> > TrustScopes;
Name m_namespace;
std::string m_alias;
std::string m_name;
std::string m_institution;
Name m_keyName;
ndn::Buffer m_publicKey;
time::system_clock::TimePoint m_notBefore;
time::system_clock::TimePoint m_notAfter;
bool m_isIntroducer;
Profile m_profile;
TrustScopes m_trustScope;
};
} // namespace chronochat
#endif // CHRONOCHAT_CONTACT_HPP