blob: 06cb1e52b9a191633d347f5e49fc92e243ec22fe [file] [log] [blame]
Yingdi Yub4be64a2013-10-13 17:24:50 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yub4be64a2013-10-13 17:24:50 -07004 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yub4be64a2013-10-13 17:24:50 -070010 */
11
Yingdi Yu0b0a7362014-08-05 16:31:30 -070012#include "profile.hpp"
Varun Patil3d850902020-11-23 12:19:14 +053013#include <ndn-cxx/security/additional-description.hpp>
Yingdi Yub4be64a2013-10-13 17:24:50 -070014
Yingdi Yueb692ac2015-02-10 18:46:18 -080015namespace chronochat {
Yingdi Yu2e3199c2013-11-06 18:42:34 -080016
Yingdi Yu0b0a7362014-08-05 16:31:30 -070017using std::vector;
18using std::string;
19using std::map;
20
Varun Patil3d850902020-11-23 12:19:14 +053021using ndn::security::Certificate;
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022
Yingdi Yu17032f82014-03-25 15:48:23 -070023const std::string Profile::OID_NAME("2.5.4.41");
24const std::string Profile::OID_ORG("2.5.4.11");
25const std::string Profile::OID_GROUP("2.5.4.1");
26const std::string Profile::OID_HOMEPAGE("2.5.4.3");
27const std::string Profile::OID_ADVISOR("2.5.4.80");
28const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
Yingdi Yufa4ce792014-02-06 18:09:22 -080029
Varun Patil3d850902020-11-23 12:19:14 +053030Profile::Profile(const Certificate& identityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080031{
Varun Patil3d850902020-11-23 12:19:14 +053032 Name keyName = identityCertificate.getKeyName();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080033
Varun Patil3d850902020-11-23 12:19:14 +053034 m_entries[string("IDENTITY")] = keyName.getPrefix(-2).toUri();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070035
Varun Patil3d850902020-11-23 12:19:14 +053036 auto additionalWire = identityCertificate.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription);
37 if (additionalWire) {
38 ndn::security::AdditionalDescription additional(*additionalWire);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070039
Varun Patil3d850902020-11-23 12:19:14 +053040 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 Yu0b0a7362014-08-05 16:31:30 -070058 }
Yingdi Yu2e3199c2013-11-06 18:42:34 -080059}
60
Yingdi Yub4be64a2013-10-13 17:24:50 -070061Profile::Profile(const Name& identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070062{
Yingdi Yufa4ce792014-02-06 18:09:22 -080063 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu3b318c12013-10-15 17:54:00 -070064}
Yingdi Yub4be64a2013-10-13 17:24:50 -070065
66Profile::Profile(const Name& identityName,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070067 const string& name,
68 const string& institution)
Yingdi Yub4be64a2013-10-13 17:24:50 -070069{
Yingdi Yufa4ce792014-02-06 18:09:22 -080070 m_entries["IDENTITY"] = identityName.toUri();
71 m_entries["name"] = name;
72 m_entries["institution"] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070073}
74
75Profile::Profile(const Profile& profile)
Yingdi Yufa4ce792014-02-06 18:09:22 -080076 : m_entries(profile.m_entries)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070077{
78}
Yingdi Yub4be64a2013-10-13 17:24:50 -070079
Varun Patil3d850902020-11-23 12:19:14 +053080Profile::Profile(const Block& profileWire)
81{
82 this->wireDecode(profileWire);
83}
84
85template<ndn::encoding::Tag T>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080086size_t
87Profile::wireEncode(ndn::EncodingImpl<T>& block) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070088{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080089 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
129const Block&
130Profile::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 Yub4be64a2013-10-13 17:24:50 -0700142}
143
Yingdi Yu76dd8002013-12-24 11:16:32 +0800144void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800145Profile::wireDecode(const Block& profileWire)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700146{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800147 m_wire = profileWire;
148 m_wire.parse();
149
150 if (m_wire.type() != tlv::Profile)
Varun Patila24bd3e2020-11-24 10:08:33 +0530151 NDN_THROW(Error("Unexpected TLV number when decoding profile packet"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800152
153 Block::element_const_iterator i = m_wire.elements_begin();
154 if (i == m_wire.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530155 NDN_THROW(Error("Missing Profile Entry"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800156 if (i->type() != tlv::ProfileEntry)
Varun Patila24bd3e2020-11-24 10:08:33 +0530157 NDN_THROW(Error("Expect Profile Entry but get TLV Type " + std::to_string(i->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800158
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 Patila24bd3e2020-11-24 10:08:33 +0530164 NDN_THROW(Error("Missing Oid"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800165 if (j->type() != tlv::Oid)
Varun Patila24bd3e2020-11-24 10:08:33 +0530166 NDN_THROW(Error("Expect Oid but get TLV Type" + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800167
168 string Oid = std::string(reinterpret_cast<const char* >(j->value()),
169 j->value_size());
170 ++j;
171 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530172 NDN_THROW(Error("Missing EntryData"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800173 if (j->type() != tlv::EntryData)
Varun Patila24bd3e2020-11-24 10:08:33 +0530174 NDN_THROW(Error("Expect EntryData but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800175
176 string EntryData = std::string(reinterpret_cast<const char* >(j->value()),
177 j->value_size());
178 ++j;
Varun Patila24bd3e2020-11-24 10:08:33 +0530179 if (j != temp.elements_end())
180 NDN_THROW(Error("Unexpected element"));
181
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800182 m_entries[Oid] = EntryData;
183 ++i;
184 }
185
Varun Patila24bd3e2020-11-24 10:08:33 +0530186 if (i != m_wire.elements_end())
187 NDN_THROW(Error("Unexpected element"));
Yingdi Yufa4ce792014-02-06 18:09:22 -0800188}
Yingdi Yub4be64a2013-10-13 17:24:50 -0700189
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700190bool
191Profile::operator==(const Profile& profile) const
Yingdi Yufa4ce792014-02-06 18:09:22 -0800192{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700193 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
207bool
208Profile::operator!=(const Profile& profile) const
209{
210 return !(*this == profile);
211}
212
Yingdi Yueb692ac2015-02-10 18:46:18 -0800213} // namespace chronochat