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 | |
| 11 | #ifndef CHRONOS_CONTACT_H |
| 12 | #define CHRONOS_CONTACT_H |
| 13 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame^] | 14 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 15 | #include <ndn-cxx/util/regex.hpp> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 16 | #include "endorse-certificate.h" |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace chronos{ |
| 20 | |
| 21 | class Contact |
| 22 | { |
| 23 | public: |
| 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 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 | |
| 62 | Contact(const ndn::Name& identity, |
| 63 | const std::string& alias, |
| 64 | const ndn::Name& keyName, |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 65 | const ndn::time::system_clock::TimePoint& notBefore, |
| 66 | const ndn::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) |
| 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 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame^] | 124 | return m_keyName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | const ndn::PublicKey& |
| 128 | getPublicKey() const |
| 129 | { |
| 130 | return m_publicKey; |
| 131 | } |
| 132 | |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 133 | const ndn::time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | getNotBefore() const |
| 135 | { |
| 136 | return m_notBefore; |
| 137 | } |
| 138 | |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 139 | const ndn::time::system_clock::TimePoint& |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 140 | 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 Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame^] | 166 | setIsIntroducer(bool isIntroducer) |
| 167 | { |
| 168 | m_isIntroducer = isIntroducer; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | addTrustScope(const ndn::Name& nameSpace) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame^] | 173 | { |
| 174 | m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 175 | } |
| 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 | |
| 218 | protected: |
| 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 Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 227 | ndn::time::system_clock::TimePoint m_notBefore; |
| 228 | ndn::time::system_clock::TimePoint m_notAfter; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 229 | |
| 230 | bool m_isIntroducer; |
| 231 | Profile m_profile; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame^] | 232 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 233 | TrustScopes m_trustScope; |
| 234 | }; |
| 235 | |
| 236 | } // namespace chronos |
| 237 | |
| 238 | #endif // CHRONOS_CONTACT_H |