Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 11 | #ifndef CHRONOCHAT_CONTACT_HPP |
| 12 | #define CHRONOCHAT_CONTACT_HPP |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 13 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 14 | #include "common.hpp" |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 15 | #include "endorse-certificate.hpp" |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 16 | #include "profile.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 17 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 18 | #include <ndn-cxx/security/certificate.hpp> |
| 19 | #include <ndn-cxx/util/regex.hpp> |
| 20 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 21 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 22 | |
| 23 | class Contact |
| 24 | { |
| 25 | public: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 26 | typedef std::map<Name, shared_ptr<ndn::Regex> >::const_iterator const_iterator; |
| 27 | typedef std::map<Name, shared_ptr<ndn::Regex> >::iterator iterator; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 28 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 29 | Contact(const ndn::security::Certificate& identityCertificate, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 30 | bool isIntroducer = false, |
| 31 | const std::string& alias = "") |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 32 | : m_notBefore(time::system_clock::now()) |
| 33 | , m_notAfter(time::system_clock::now() + time::days(3650)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 34 | , m_isIntroducer(isIntroducer) |
| 35 | , m_profile(identityCertificate) |
| 36 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 37 | m_keyName = identityCertificate.getKeyName(); |
| 38 | m_namespace = m_keyName.getPrefix(-2); |
| 39 | m_publicKey = identityCertificate.getPublicKey(); |
| 40 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 41 | m_name = m_profile.get("name"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 42 | m_name = m_name.empty() ? m_namespace.toUri() : m_name; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 43 | m_alias = alias.empty() ? m_name : alias; |
| 44 | m_institution = m_profile.get("institution"); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 45 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 46 | try { |
| 47 | m_notBefore = identityCertificate.getValidityPeriod().getPeriod().first; |
| 48 | m_notAfter = identityCertificate.getValidityPeriod().getPeriod().second; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 49 | } catch (const tlv::Error&) {} |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Contact(const EndorseCertificate& endorseCertificate, |
| 53 | bool isIntroducer = false, |
| 54 | const std::string& alias = "") |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 55 | : m_notBefore(time::system_clock::now()) |
| 56 | , m_notAfter(time::system_clock::now() + time::days(3650)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 57 | , m_isIntroducer(isIntroducer) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 58 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 59 | m_profile = endorseCertificate.getProfile(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 60 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 61 | m_keyName = endorseCertificate.getKeyName().getPrefix(-1).append("KEY"); |
| 62 | m_namespace = m_keyName.getPrefix(-3); |
| 63 | m_publicKey = endorseCertificate.getPublicKey(); |
| 64 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 65 | m_name = m_profile.get("name"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 66 | m_name = m_name.empty() ? m_namespace.toUri() : m_name; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | m_alias = alias.empty() ? m_name : alias; |
| 68 | m_institution = m_profile.get("institution"); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 69 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 70 | try { |
| 71 | m_notBefore = endorseCertificate.getValidityPeriod().getPeriod().first; |
| 72 | m_notAfter = endorseCertificate.getValidityPeriod().getPeriod().second; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 73 | } catch (const tlv::Error&) {} |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 76 | Contact(const Name& identity, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 77 | const std::string& alias, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 78 | const Name& keyName, |
| 79 | const time::system_clock::TimePoint& notBefore, |
| 80 | const time::system_clock::TimePoint& notAfter, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 81 | const ndn::Buffer& key, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 82 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 92 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 93 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 105 | { |
| 106 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 108 | virtual |
| 109 | ~Contact() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 110 | { |
| 111 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 112 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 113 | const Name& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 114 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 137 | const Name& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 138 | getPublicKeyName() const |
| 139 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 140 | return m_keyName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 143 | const ndn::Buffer& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 144 | getPublicKey() const |
| 145 | { |
| 146 | return m_publicKey; |
| 147 | } |
| 148 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 149 | const time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 150 | getNotBefore() const |
| 151 | { |
| 152 | return m_notBefore; |
| 153 | } |
| 154 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 155 | const time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 156 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 182 | setIsIntroducer(bool isIntroducer) |
| 183 | { |
| 184 | m_isIntroducer = isIntroducer; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 188 | addTrustScope(const Name& nameSpace) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 189 | { |
| 190 | m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 194 | deleteTrustScope(const Name& nameSpace) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 195 | { |
| 196 | m_trustScope.erase(nameSpace); |
| 197 | } |
| 198 | |
| 199 | bool |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 200 | canBeTrustedFor(const Name& name) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 201 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 202 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 205 | 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 | |
| 233 | protected: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 234 | typedef std::map<Name, shared_ptr<ndn::Regex> > TrustScopes; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 235 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 236 | Name m_namespace; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 237 | std::string m_alias; |
| 238 | std::string m_name; |
| 239 | std::string m_institution; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 240 | Name m_keyName; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 241 | ndn::Buffer m_publicKey; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 242 | time::system_clock::TimePoint m_notBefore; |
| 243 | time::system_clock::TimePoint m_notAfter; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 244 | |
| 245 | bool m_isIntroducer; |
| 246 | Profile m_profile; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 247 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 248 | TrustScopes m_trustScope; |
| 249 | }; |
| 250 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 251 | } // namespace chronochat |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 252 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 253 | #endif // CHRONOCHAT_CONTACT_HPP |