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