Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [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 | b4be64a | 2013-10-13 17:24:50 -0700 | [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> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "profile.hpp" |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 13 | #include <ndn-cxx/security/additional-description.hpp> |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 14 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 15 | namespace chronochat { |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 16 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 17 | using std::vector; |
| 18 | using std::string; |
| 19 | using std::map; |
| 20 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 21 | using ndn::security::Certificate; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 23 | const std::string Profile::OID_NAME("2.5.4.41"); |
| 24 | const std::string Profile::OID_ORG("2.5.4.11"); |
| 25 | const std::string Profile::OID_GROUP("2.5.4.1"); |
| 26 | const std::string Profile::OID_HOMEPAGE("2.5.4.3"); |
| 27 | const std::string Profile::OID_ADVISOR("2.5.4.80"); |
| 28 | const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 29 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 30 | Profile::Profile(const Certificate& identityCertificate) |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 31 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 32 | Name keyName = identityCertificate.getKeyName(); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 33 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 34 | m_entries[string("IDENTITY")] = keyName.getPrefix(-2).toUri(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 35 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 36 | auto additionalWire = identityCertificate.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription); |
| 37 | if (additionalWire) { |
| 38 | ndn::security::AdditionalDescription additional(*additionalWire); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 39 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 40 | for (auto it = additional.begin(); it != additional.end(); it++) { |
| 41 | const string oidStr = it->first; |
| 42 | string valueStr = it->second; |
| 43 | if (oidStr == OID_NAME) |
| 44 | m_entries["name"] = valueStr; |
| 45 | else if (oidStr == OID_ORG) |
| 46 | m_entries["institution"] = valueStr; |
| 47 | else if (oidStr == OID_GROUP) |
| 48 | m_entries["group"] = valueStr; |
| 49 | else if (oidStr == OID_HOMEPAGE) |
| 50 | m_entries["homepage"] = valueStr; |
| 51 | else if (oidStr == OID_ADVISOR) |
| 52 | m_entries["advisor"] = valueStr; |
| 53 | else if (oidStr == OID_EMAIL) |
| 54 | m_entries["email"] = valueStr; |
| 55 | else |
| 56 | m_entries[oidStr] = valueStr; |
| 57 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 58 | } |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 61 | Profile::Profile(const Name& identityName) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 62 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 63 | m_entries["IDENTITY"] = identityName.toUri(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 64 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 65 | |
| 66 | Profile::Profile(const Name& identityName, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 67 | const string& name, |
| 68 | const string& institution) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 69 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 70 | m_entries["IDENTITY"] = identityName.toUri(); |
| 71 | m_entries["name"] = name; |
| 72 | m_entries["institution"] = institution; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | Profile::Profile(const Profile& profile) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 76 | : m_entries(profile.m_entries) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 77 | { |
| 78 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 79 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 80 | Profile::Profile(const Block& profileWire) |
| 81 | { |
| 82 | this->wireDecode(profileWire); |
| 83 | } |
| 84 | |
| 85 | template<ndn::encoding::Tag T> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 86 | size_t |
| 87 | Profile::wireEncode(ndn::EncodingImpl<T>& block) const |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 88 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 89 | size_t totalLength = 0; |
| 90 | |
| 91 | // Profile := PROFILE-TYPE TLV-LENGTH |
| 92 | // ProfileEntry+ |
| 93 | // |
| 94 | // ProfileEntry := PROFILEENTRY-TYPE TLV-LENGTH |
| 95 | // Oid |
| 96 | // EntryData |
| 97 | // |
| 98 | // Oid := OID-TYPE TLV-LENGTH |
| 99 | // String |
| 100 | // |
| 101 | // EntryData := ENTRYDATA-TYPE TLV-LENGTH |
| 102 | // String |
| 103 | |
| 104 | // Entries |
| 105 | size_t entryLength = 0; |
| 106 | for (map<string, string>::const_reverse_iterator it = m_entries.rbegin(); |
| 107 | it != m_entries.rend(); it++) { |
| 108 | // Entry Data |
| 109 | const uint8_t* dataWire = reinterpret_cast<const uint8_t*>(it->second.c_str()); |
| 110 | entryLength += block.prependByteArrayBlock(tlv::EntryData, dataWire, it->second.length()); |
| 111 | // Oid |
| 112 | const uint8_t* oidWire = reinterpret_cast<const uint8_t*>(it->first.c_str()); |
| 113 | entryLength += block.prependByteArrayBlock(tlv::Oid, oidWire, it->first.length()); |
| 114 | entryLength += block.prependVarNumber(entryLength); |
| 115 | entryLength += block.prependVarNumber(tlv::ProfileEntry); |
| 116 | totalLength += entryLength; |
| 117 | entryLength = 0; |
| 118 | } |
| 119 | |
| 120 | // Profile |
| 121 | totalLength += block.prependVarNumber(totalLength); |
| 122 | totalLength += block.prependVarNumber(tlv::Profile); |
| 123 | |
| 124 | return totalLength; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | const Block& |
| 130 | Profile::wireEncode() const |
| 131 | { |
| 132 | ndn::EncodingEstimator estimator; |
| 133 | size_t estimatedSize = wireEncode(estimator); |
| 134 | |
| 135 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 136 | wireEncode(buffer); |
| 137 | |
| 138 | m_wire = buffer.block(); |
| 139 | m_wire.parse(); |
| 140 | |
| 141 | return m_wire; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 144 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 145 | Profile::wireDecode(const Block& profileWire) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 146 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 147 | m_wire = profileWire; |
| 148 | m_wire.parse(); |
| 149 | |
| 150 | if (m_wire.type() != tlv::Profile) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 151 | NDN_THROW(Error("Unexpected TLV number when decoding profile packet")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 152 | |
| 153 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 154 | if (i == m_wire.elements_end()) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 155 | NDN_THROW(Error("Missing Profile Entry")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 156 | if (i->type() != tlv::ProfileEntry) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 157 | NDN_THROW(Error("Expect Profile Entry but get TLV Type " + std::to_string(i->type()))); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 158 | |
| 159 | while (i != m_wire.elements_end() && i->type() == tlv::ProfileEntry) { |
| 160 | Block temp = *i; |
| 161 | temp.parse(); |
| 162 | Block::element_const_iterator j = temp.elements_begin(); |
| 163 | if (j == temp.elements_end()) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 164 | NDN_THROW(Error("Missing Oid")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 165 | if (j->type() != tlv::Oid) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 166 | NDN_THROW(Error("Expect Oid but get TLV Type" + std::to_string(j->type()))); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 167 | |
| 168 | string Oid = std::string(reinterpret_cast<const char* >(j->value()), |
| 169 | j->value_size()); |
| 170 | ++j; |
| 171 | if (j == temp.elements_end()) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 172 | NDN_THROW(Error("Missing EntryData")); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 173 | if (j->type() != tlv::EntryData) |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 174 | NDN_THROW(Error("Expect EntryData but get TLV Type " + std::to_string(j->type()))); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 175 | |
| 176 | string EntryData = std::string(reinterpret_cast<const char* >(j->value()), |
| 177 | j->value_size()); |
| 178 | ++j; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 179 | if (j != temp.elements_end()) |
| 180 | NDN_THROW(Error("Unexpected element")); |
| 181 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 182 | m_entries[Oid] = EntryData; |
| 183 | ++i; |
| 184 | } |
| 185 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 186 | if (i != m_wire.elements_end()) |
| 187 | NDN_THROW(Error("Unexpected element")); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 188 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 189 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 190 | bool |
| 191 | Profile::operator==(const Profile& profile) const |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 192 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 193 | if (m_entries.size() != profile.m_entries.size()) |
| 194 | return false; |
| 195 | |
| 196 | for(map<string, string>::const_iterator it = m_entries.begin(); it != m_entries.end(); it++) { |
| 197 | map<string, string>::const_iterator found = profile.m_entries.find(it->first); |
| 198 | if (found == profile.m_entries.end()) |
| 199 | return false; |
| 200 | if (found->second != it->second) |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool |
| 208 | Profile::operator!=(const Profile& profile) const |
| 209 | { |
| 210 | return !(*this == profile); |
| 211 | } |
| 212 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 213 | } // namespace chronochat |