blob: a95ad421839152aa7810269f94d35c55c16aa365 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#ifndef CHRONOS_CONTACT_H
12#define CHRONOS_CONTACT_H
13
Yingdi Yufa0b6a02014-04-30 14:26:42 -070014#include <ndn-cxx/security/identity-certificate.hpp>
15#include <ndn-cxx/util/regex.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080016#include "endorse-certificate.h"
17#include <vector>
18
19namespace chronos{
20
21class Contact
22{
23public:
24 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::Regex> >::const_iterator const_iterator;
25 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::Regex> >::iterator iterator;
26
27 Contact(const ndn::IdentityCertificate& identityCertificate,
28 bool isIntroducer = false,
29 const std::string& alias = "")
30 : m_notBefore(identityCertificate.getNotBefore())
31 , m_notAfter(identityCertificate.getNotAfter())
32 , m_isIntroducer(isIntroducer)
33 , m_profile(identityCertificate)
34 {
35 m_name = m_profile.get("name");
36 m_alias = alias.empty() ? m_name : alias;
37 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070038
Yingdi Yu348f5ea2014-03-01 14:47:25 -080039 m_keyName = identityCertificate.getPublicKeyName();
40 m_namespace = m_keyName.getPrefix(-1);
41 m_publicKey = identityCertificate.getPublicKeyInfo();
42 }
43
44 Contact(const EndorseCertificate& endorseCertificate,
45 bool isIntroducer = false,
46 const std::string& alias = "")
47 : m_notBefore(endorseCertificate.getNotBefore())
48 , m_notAfter(endorseCertificate.getNotAfter())
49 , m_isIntroducer(isIntroducer)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070050 {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080051 m_profile = endorseCertificate.getProfile();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070052
Yingdi Yu348f5ea2014-03-01 14:47:25 -080053 m_name = m_profile.get("name");
54 m_alias = alias.empty() ? m_name : alias;
55 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070056
Yingdi Yu348f5ea2014-03-01 14:47:25 -080057 m_keyName = endorseCertificate.getPublicKeyName();;
58 m_namespace = m_keyName.getPrefix(-1);
59 m_publicKey = endorseCertificate.getPublicKeyInfo();
60 }
61
62 Contact(const ndn::Name& identity,
63 const std::string& alias,
64 const ndn::Name& keyName,
Yingdi Yua7876722014-03-25 14:46:55 -070065 const ndn::time::system_clock::TimePoint& notBefore,
66 const ndn::time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 const ndn::PublicKey& key,
68 bool isIntroducer)
69 : m_namespace(identity)
70 , m_alias(alias)
71 , m_keyName(keyName)
72 , m_publicKey(key)
73 , m_notBefore(notBefore)
74 , m_notAfter(notAfter)
75 , m_isIntroducer(isIntroducer)
76 {
77 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -070078
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 Contact(const Contact& contact)
80 : m_namespace(contact.m_namespace)
81 , m_alias(contact.m_alias)
82 , m_name(contact.m_name)
83 , m_institution(contact.m_institution)
84 , m_keyName(contact.m_keyName)
85 , m_publicKey(contact.m_publicKey)
86 , m_notBefore(contact.m_notBefore)
87 , m_notAfter(contact.m_notAfter)
88 , m_isIntroducer(contact.m_isIntroducer)
89 , m_profile(contact.m_profile)
90 , m_trustScope(contact.m_trustScope)
91 {}
Yingdi Yufa0b6a02014-04-30 14:26:42 -070092
Yingdi Yu348f5ea2014-03-01 14:47:25 -080093 virtual
94 ~Contact()
95 {}
96
97 const ndn::Name&
98 getNameSpace() const
99 {
100 return m_namespace;
101 }
102
103 const std::string&
104 getAlias() const
105 {
106 return m_alias;
107 }
108
109 const std::string&
110 getName() const
111 {
112 return m_name;
113 }
114
115 const std::string&
116 getInstitution() const
117 {
118 return m_institution;
119 }
120
121 const ndn::Name&
122 getPublicKeyName() const
123 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700124 return m_keyName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800125 }
126
127 const ndn::PublicKey&
128 getPublicKey() const
129 {
130 return m_publicKey;
131 }
132
Yingdi Yua7876722014-03-25 14:46:55 -0700133 const ndn::time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 getNotBefore() const
135 {
136 return m_notBefore;
137 }
138
Yingdi Yua7876722014-03-25 14:46:55 -0700139 const ndn::time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800140 getNotAfter() const
141 {
142 return m_notAfter;
143 }
144
145 const Profile&
146 getProfile() const
147 {
148 return m_profile;
149 }
150
151 void
152 setProfile(const Profile& profile)
153 {
154 m_name = profile.get("name");
155 m_institution = profile.get("institution");
156 m_profile = profile;
157 }
158
159 bool
160 isIntroducer() const
161 {
162 return m_isIntroducer;
163 }
164
165 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700166 setIsIntroducer(bool isIntroducer)
167 {
168 m_isIntroducer = isIntroducer;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800169 }
170
171 void
172 addTrustScope(const ndn::Name& nameSpace)
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700173 {
174 m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800175 }
176
177 void
178 deleteTrustScope(const ndn::Name& nameSpace)
179 {
180 m_trustScope.erase(nameSpace);
181 }
182
183 bool
184 canBeTrustedFor(const ndn::Name& name)
185 {
186 std::map<ndn::Name, ndn::shared_ptr<ndn::Regex> >::iterator it = m_trustScope.begin();
187
188 for(; it != m_trustScope.end(); it++)
189 if(it->second->match(name))
190 return true;
191 return false;
192 }
193
194 const_iterator
195 trustScopeBegin() const
196 {
197 return m_trustScope.begin();
198 }
199
200 const_iterator
201 trustScopeEnd() const
202 {
203 return m_trustScope.end();
204 }
205
206 iterator
207 trustScopeBegin()
208 {
209 return m_trustScope.begin();
210 }
211
212 iterator
213 trustScopeEnd()
214 {
215 return m_trustScope.end();
216 }
217
218protected:
219 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::Regex> > TrustScopes;
220
221 ndn::Name m_namespace;
222 std::string m_alias;
223 std::string m_name;
224 std::string m_institution;
225 ndn::Name m_keyName;
226 ndn::PublicKey m_publicKey;
Yingdi Yua7876722014-03-25 14:46:55 -0700227 ndn::time::system_clock::TimePoint m_notBefore;
228 ndn::time::system_clock::TimePoint m_notAfter;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800229
230 bool m_isIntroducer;
231 Profile m_profile;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700232
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800233 TrustScopes m_trustScope;
234};
235
236} // namespace chronos
237
238#endif // CHRONOS_CONTACT_H