blob: 4b04b476157014060cd1b3855aa13639b7886305 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yu348f5ea2014-03-01 14:47:25 -08004 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
Yingdi Yueb692ac2015-02-10 18:46:18 -080011#ifndef CHRONOCHAT_CONTACT_HPP
12#define CHRONOCHAT_CONTACT_HPP
Yingdi Yu348f5ea2014-03-01 14:47:25 -080013
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014#include "common.hpp"
Yingdi Yu0b0a7362014-08-05 16:31:30 -070015#include "endorse-certificate.hpp"
Varun Patil3d850902020-11-23 12:19:14 +053016#include "profile.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080017
Varun Patila24bd3e2020-11-24 10:08:33 +053018#include <ndn-cxx/security/certificate.hpp>
19#include <ndn-cxx/util/regex.hpp>
20
Yingdi Yueb692ac2015-02-10 18:46:18 -080021namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080022
23class Contact
24{
25public:
Yingdi Yu0b0a7362014-08-05 16:31:30 -070026 typedef std::map<Name, shared_ptr<ndn::Regex> >::const_iterator const_iterator;
27 typedef std::map<Name, shared_ptr<ndn::Regex> >::iterator iterator;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028
Varun Patil3d850902020-11-23 12:19:14 +053029 Contact(const ndn::security::Certificate& identityCertificate,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080030 bool isIntroducer = false,
31 const std::string& alias = "")
Varun Patil3d850902020-11-23 12:19:14 +053032 : m_notBefore(time::system_clock::now())
33 , m_notAfter(time::system_clock::now() + time::days(3650))
Yingdi Yu348f5ea2014-03-01 14:47:25 -080034 , m_isIntroducer(isIntroducer)
35 , m_profile(identityCertificate)
36 {
Varun Patil3d850902020-11-23 12:19:14 +053037 m_keyName = identityCertificate.getKeyName();
38 m_namespace = m_keyName.getPrefix(-2);
39 m_publicKey = identityCertificate.getPublicKey();
40
Yingdi Yu348f5ea2014-03-01 14:47:25 -080041 m_name = m_profile.get("name");
Varun Patil3d850902020-11-23 12:19:14 +053042 m_name = m_name.empty() ? m_namespace.toUri() : m_name;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080043 m_alias = alias.empty() ? m_name : alias;
44 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070045
Varun Patil3d850902020-11-23 12:19:14 +053046 try {
47 m_notBefore = identityCertificate.getValidityPeriod().getPeriod().first;
48 m_notAfter = identityCertificate.getValidityPeriod().getPeriod().second;
Varun Patila24bd3e2020-11-24 10:08:33 +053049 } catch (const tlv::Error&) {}
Yingdi Yu348f5ea2014-03-01 14:47:25 -080050 }
51
52 Contact(const EndorseCertificate& endorseCertificate,
53 bool isIntroducer = false,
54 const std::string& alias = "")
Varun Patil3d850902020-11-23 12:19:14 +053055 : m_notBefore(time::system_clock::now())
56 , m_notAfter(time::system_clock::now() + time::days(3650))
Yingdi Yu348f5ea2014-03-01 14:47:25 -080057 , m_isIntroducer(isIntroducer)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070058 {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080059 m_profile = endorseCertificate.getProfile();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070060
Varun Patil3d850902020-11-23 12:19:14 +053061 m_keyName = endorseCertificate.getKeyName().getPrefix(-1).append("KEY");
62 m_namespace = m_keyName.getPrefix(-3);
63 m_publicKey = endorseCertificate.getPublicKey();
64
Yingdi Yu348f5ea2014-03-01 14:47:25 -080065 m_name = m_profile.get("name");
Varun Patil3d850902020-11-23 12:19:14 +053066 m_name = m_name.empty() ? m_namespace.toUri() : m_name;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 m_alias = alias.empty() ? m_name : alias;
68 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070069
Varun Patil3d850902020-11-23 12:19:14 +053070 try {
71 m_notBefore = endorseCertificate.getValidityPeriod().getPeriod().first;
72 m_notAfter = endorseCertificate.getValidityPeriod().getPeriod().second;
Varun Patila24bd3e2020-11-24 10:08:33 +053073 } catch (const tlv::Error&) {}
Yingdi Yu348f5ea2014-03-01 14:47:25 -080074 }
75
Yingdi Yu0b0a7362014-08-05 16:31:30 -070076 Contact(const Name& identity,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080077 const std::string& alias,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070078 const Name& keyName,
79 const time::system_clock::TimePoint& notBefore,
80 const time::system_clock::TimePoint& notAfter,
Varun Patil3d850902020-11-23 12:19:14 +053081 const ndn::Buffer& key,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080082 bool isIntroducer)
83 : m_namespace(identity)
84 , m_alias(alias)
85 , m_keyName(keyName)
86 , m_publicKey(key)
87 , m_notBefore(notBefore)
88 , m_notAfter(notAfter)
89 , m_isIntroducer(isIntroducer)
90 {
91 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -070092
Yingdi Yu348f5ea2014-03-01 14:47:25 -080093 Contact(const Contact& contact)
94 : m_namespace(contact.m_namespace)
95 , m_alias(contact.m_alias)
96 , m_name(contact.m_name)
97 , m_institution(contact.m_institution)
98 , m_keyName(contact.m_keyName)
99 , m_publicKey(contact.m_publicKey)
100 , m_notBefore(contact.m_notBefore)
101 , m_notAfter(contact.m_notAfter)
102 , m_isIntroducer(contact.m_isIntroducer)
103 , m_profile(contact.m_profile)
104 , m_trustScope(contact.m_trustScope)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700105 {
106 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700107
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800108 virtual
109 ~Contact()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700110 {
111 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800112
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700113 const Name&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114 getNameSpace() const
115 {
116 return m_namespace;
117 }
118
119 const std::string&
120 getAlias() const
121 {
122 return m_alias;
123 }
124
125 const std::string&
126 getName() const
127 {
128 return m_name;
129 }
130
131 const std::string&
132 getInstitution() const
133 {
134 return m_institution;
135 }
136
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700137 const Name&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800138 getPublicKeyName() const
139 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700140 return m_keyName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 }
142
Varun Patil3d850902020-11-23 12:19:14 +0530143 const ndn::Buffer&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800144 getPublicKey() const
145 {
146 return m_publicKey;
147 }
148
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700149 const time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800150 getNotBefore() const
151 {
152 return m_notBefore;
153 }
154
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700155 const time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800156 getNotAfter() const
157 {
158 return m_notAfter;
159 }
160
161 const Profile&
162 getProfile() const
163 {
164 return m_profile;
165 }
166
167 void
168 setProfile(const Profile& profile)
169 {
170 m_name = profile.get("name");
171 m_institution = profile.get("institution");
172 m_profile = profile;
173 }
174
175 bool
176 isIntroducer() const
177 {
178 return m_isIntroducer;
179 }
180
181 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700182 setIsIntroducer(bool isIntroducer)
183 {
184 m_isIntroducer = isIntroducer;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800185 }
186
187 void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700188 addTrustScope(const Name& nameSpace)
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700189 {
190 m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800191 }
192
193 void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700194 deleteTrustScope(const Name& nameSpace)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800195 {
196 m_trustScope.erase(nameSpace);
197 }
198
199 bool
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700200 canBeTrustedFor(const Name& name)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800201 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700202 for (std::map<Name, shared_ptr<ndn::Regex> >::iterator it = m_trustScope.begin();
203 it != m_trustScope.end(); it++)
204 if (it->second->match(name))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800205 return true;
206 return false;
207 }
208
209 const_iterator
210 trustScopeBegin() const
211 {
212 return m_trustScope.begin();
213 }
214
215 const_iterator
216 trustScopeEnd() const
217 {
218 return m_trustScope.end();
219 }
220
221 iterator
222 trustScopeBegin()
223 {
224 return m_trustScope.begin();
225 }
226
227 iterator
228 trustScopeEnd()
229 {
230 return m_trustScope.end();
231 }
232
233protected:
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700234 typedef std::map<Name, shared_ptr<ndn::Regex> > TrustScopes;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800235
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700236 Name m_namespace;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800237 std::string m_alias;
238 std::string m_name;
239 std::string m_institution;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700240 Name m_keyName;
Varun Patil3d850902020-11-23 12:19:14 +0530241 ndn::Buffer m_publicKey;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700242 time::system_clock::TimePoint m_notBefore;
243 time::system_clock::TimePoint m_notAfter;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800244
245 bool m_isIntroducer;
246 Profile m_profile;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700247
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800248 TrustScopes m_trustScope;
249};
250
Yingdi Yueb692ac2015-02-10 18:46:18 -0800251} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800252
Yingdi Yueb692ac2015-02-10 18:46:18 -0800253#endif // CHRONOCHAT_CONTACT_HPP