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" |
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 | |
| 21 | using ndn::IdentityCertificate; |
| 22 | using ndn::CertificateSubjectDescription; |
| 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 | |
| 31 | Profile::Profile(const IdentityCertificate& identityCertificate) |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 32 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 33 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName()); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 35 | m_entries[string("IDENTITY")] = keyName.getPrefix(-1).toUri(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 37 | const vector<CertificateSubjectDescription>& subList = |
| 38 | identityCertificate.getSubjectDescriptionList(); |
| 39 | |
| 40 | for (vector<CertificateSubjectDescription>::const_iterator it = subList.begin(); |
| 41 | it != subList.end(); it++) { |
| 42 | const string oidStr = it->getOidString(); |
| 43 | string valueStr = it->getValue(); |
| 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 | 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 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame^] | 80 | template<bool T> |
| 81 | size_t |
| 82 | Profile::wireEncode(ndn::EncodingImpl<T>& block) const |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 83 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame^] | 84 | size_t totalLength = 0; |
| 85 | |
| 86 | // Profile := PROFILE-TYPE TLV-LENGTH |
| 87 | // ProfileEntry+ |
| 88 | // |
| 89 | // ProfileEntry := PROFILEENTRY-TYPE TLV-LENGTH |
| 90 | // Oid |
| 91 | // EntryData |
| 92 | // |
| 93 | // Oid := OID-TYPE TLV-LENGTH |
| 94 | // String |
| 95 | // |
| 96 | // EntryData := ENTRYDATA-TYPE TLV-LENGTH |
| 97 | // String |
| 98 | |
| 99 | // Entries |
| 100 | size_t entryLength = 0; |
| 101 | for (map<string, string>::const_reverse_iterator it = m_entries.rbegin(); |
| 102 | it != m_entries.rend(); it++) { |
| 103 | // Entry Data |
| 104 | const uint8_t* dataWire = reinterpret_cast<const uint8_t*>(it->second.c_str()); |
| 105 | entryLength += block.prependByteArrayBlock(tlv::EntryData, dataWire, it->second.length()); |
| 106 | // Oid |
| 107 | const uint8_t* oidWire = reinterpret_cast<const uint8_t*>(it->first.c_str()); |
| 108 | entryLength += block.prependByteArrayBlock(tlv::Oid, oidWire, it->first.length()); |
| 109 | entryLength += block.prependVarNumber(entryLength); |
| 110 | entryLength += block.prependVarNumber(tlv::ProfileEntry); |
| 111 | totalLength += entryLength; |
| 112 | entryLength = 0; |
| 113 | } |
| 114 | |
| 115 | // Profile |
| 116 | totalLength += block.prependVarNumber(totalLength); |
| 117 | totalLength += block.prependVarNumber(tlv::Profile); |
| 118 | |
| 119 | return totalLength; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | const Block& |
| 125 | Profile::wireEncode() const |
| 126 | { |
| 127 | ndn::EncodingEstimator estimator; |
| 128 | size_t estimatedSize = wireEncode(estimator); |
| 129 | |
| 130 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 131 | wireEncode(buffer); |
| 132 | |
| 133 | m_wire = buffer.block(); |
| 134 | m_wire.parse(); |
| 135 | |
| 136 | return m_wire; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 139 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame^] | 140 | Profile::wireDecode(const Block& profileWire) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 141 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame^] | 142 | m_wire = profileWire; |
| 143 | m_wire.parse(); |
| 144 | |
| 145 | if (m_wire.type() != tlv::Profile) |
| 146 | throw Error("Unexpected TLV number when decoding profile packet"); |
| 147 | |
| 148 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 149 | if (i == m_wire.elements_end()) |
| 150 | throw Error("Missing Profile Entry"); |
| 151 | if (i->type() != tlv::ProfileEntry) |
| 152 | throw Error("Expect Profile Entry but get TLV Type " + std::to_string(i->type())); |
| 153 | |
| 154 | while (i != m_wire.elements_end() && i->type() == tlv::ProfileEntry) { |
| 155 | Block temp = *i; |
| 156 | temp.parse(); |
| 157 | Block::element_const_iterator j = temp.elements_begin(); |
| 158 | if (j == temp.elements_end()) |
| 159 | throw Error("Missing Oid"); |
| 160 | if (j->type() != tlv::Oid) |
| 161 | throw Error("Expect Oid but get TLV Type" + std::to_string(j->type())); |
| 162 | |
| 163 | string Oid = std::string(reinterpret_cast<const char* >(j->value()), |
| 164 | j->value_size()); |
| 165 | ++j; |
| 166 | if (j == temp.elements_end()) |
| 167 | throw Error("Missing EntryData"); |
| 168 | if (j->type() != tlv::EntryData) |
| 169 | throw Error("Expect EntryData but get TLV Type " + std::to_string(j->type())); |
| 170 | |
| 171 | string EntryData = std::string(reinterpret_cast<const char* >(j->value()), |
| 172 | j->value_size()); |
| 173 | ++j; |
| 174 | if (j != temp.elements_end()) { |
| 175 | throw Error("Unexpected element"); |
| 176 | } |
| 177 | m_entries[Oid] = EntryData; |
| 178 | ++i; |
| 179 | } |
| 180 | |
| 181 | if (i != m_wire.elements_end()) { |
| 182 | throw Error("Unexpected element"); |
| 183 | } |
| 184 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 185 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 186 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 187 | bool |
| 188 | Profile::operator==(const Profile& profile) const |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 189 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 190 | if (m_entries.size() != profile.m_entries.size()) |
| 191 | return false; |
| 192 | |
| 193 | for(map<string, string>::const_iterator it = m_entries.begin(); it != m_entries.end(); it++) { |
| 194 | map<string, string>::const_iterator found = profile.m_entries.find(it->first); |
| 195 | if (found == profile.m_entries.end()) |
| 196 | return false; |
| 197 | if (found->second != it->second) |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool |
| 205 | Profile::operator!=(const Profile& profile) const |
| 206 | { |
| 207 | return !(*this == profile); |
| 208 | } |
| 209 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 210 | } // namespace chronochat |