Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
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 | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 15 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 16 | #include <ndn-cxx/util/regex.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 17 | #include "endorse-certificate.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame^] | 19 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 20 | |
| 21 | class Contact |
| 22 | { |
| 23 | public: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 24 | typedef std::map<Name, shared_ptr<ndn::Regex> >::const_iterator const_iterator; |
| 25 | typedef std::map<Name, shared_ptr<ndn::Regex> >::iterator iterator; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 39 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 50 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 51 | m_profile = endorseCertificate.getProfile(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 53 | m_name = m_profile.get("name"); |
| 54 | m_alias = alias.empty() ? m_name : alias; |
| 55 | m_institution = m_profile.get("institution"); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 57 | m_keyName = endorseCertificate.getPublicKeyName();; |
| 58 | m_namespace = m_keyName.getPrefix(-1); |
| 59 | m_publicKey = endorseCertificate.getPublicKeyInfo(); |
| 60 | } |
| 61 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 62 | Contact(const Name& identity, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 63 | const std::string& alias, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 64 | const Name& keyName, |
| 65 | const time::system_clock::TimePoint& notBefore, |
| 66 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 79 | 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) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 91 | { |
| 92 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 94 | virtual |
| 95 | ~Contact() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 96 | { |
| 97 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 98 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 99 | const Name& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 100 | getNameSpace() const |
| 101 | { |
| 102 | return m_namespace; |
| 103 | } |
| 104 | |
| 105 | const std::string& |
| 106 | getAlias() const |
| 107 | { |
| 108 | return m_alias; |
| 109 | } |
| 110 | |
| 111 | const std::string& |
| 112 | getName() const |
| 113 | { |
| 114 | return m_name; |
| 115 | } |
| 116 | |
| 117 | const std::string& |
| 118 | getInstitution() const |
| 119 | { |
| 120 | return m_institution; |
| 121 | } |
| 122 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 123 | const Name& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 124 | getPublicKeyName() const |
| 125 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 126 | return m_keyName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | const ndn::PublicKey& |
| 130 | getPublicKey() const |
| 131 | { |
| 132 | return m_publicKey; |
| 133 | } |
| 134 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 135 | const time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 136 | getNotBefore() const |
| 137 | { |
| 138 | return m_notBefore; |
| 139 | } |
| 140 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 141 | const time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 142 | getNotAfter() const |
| 143 | { |
| 144 | return m_notAfter; |
| 145 | } |
| 146 | |
| 147 | const Profile& |
| 148 | getProfile() const |
| 149 | { |
| 150 | return m_profile; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | setProfile(const Profile& profile) |
| 155 | { |
| 156 | m_name = profile.get("name"); |
| 157 | m_institution = profile.get("institution"); |
| 158 | m_profile = profile; |
| 159 | } |
| 160 | |
| 161 | bool |
| 162 | isIntroducer() const |
| 163 | { |
| 164 | return m_isIntroducer; |
| 165 | } |
| 166 | |
| 167 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 168 | setIsIntroducer(bool isIntroducer) |
| 169 | { |
| 170 | m_isIntroducer = isIntroducer; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 174 | addTrustScope(const Name& nameSpace) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 175 | { |
| 176 | m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 180 | deleteTrustScope(const Name& nameSpace) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 181 | { |
| 182 | m_trustScope.erase(nameSpace); |
| 183 | } |
| 184 | |
| 185 | bool |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 186 | canBeTrustedFor(const Name& name) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 187 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 188 | for (std::map<Name, shared_ptr<ndn::Regex> >::iterator it = m_trustScope.begin(); |
| 189 | it != m_trustScope.end(); it++) |
| 190 | if (it->second->match(name)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 191 | return true; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | const_iterator |
| 196 | trustScopeBegin() const |
| 197 | { |
| 198 | return m_trustScope.begin(); |
| 199 | } |
| 200 | |
| 201 | const_iterator |
| 202 | trustScopeEnd() const |
| 203 | { |
| 204 | return m_trustScope.end(); |
| 205 | } |
| 206 | |
| 207 | iterator |
| 208 | trustScopeBegin() |
| 209 | { |
| 210 | return m_trustScope.begin(); |
| 211 | } |
| 212 | |
| 213 | iterator |
| 214 | trustScopeEnd() |
| 215 | { |
| 216 | return m_trustScope.end(); |
| 217 | } |
| 218 | |
| 219 | protected: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 220 | typedef std::map<Name, shared_ptr<ndn::Regex> > TrustScopes; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 221 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 222 | Name m_namespace; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 223 | std::string m_alias; |
| 224 | std::string m_name; |
| 225 | std::string m_institution; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 226 | Name m_keyName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 227 | ndn::PublicKey m_publicKey; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 228 | time::system_clock::TimePoint m_notBefore; |
| 229 | time::system_clock::TimePoint m_notAfter; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 230 | |
| 231 | bool m_isIntroducer; |
| 232 | Profile m_profile; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 233 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 234 | TrustScopes m_trustScope; |
| 235 | }; |
| 236 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame^] | 237 | } // namespace chronochat |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 238 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame^] | 239 | #endif // CHRONOCHAT_CONTACT_HPP |