blob: dc89fa9191a0dc90c8698ec5248a82fcb605a31d [file] [log] [blame]
Yingdi Yub4be64a2013-10-13 17:24:50 -07001/* -*- 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 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"
Yingdi Yu92e8e482013-10-17 21:13:03 -070013#include "logging.h"
Varun Patil3d850902020-11-23 12:19:14 +053014#include <ndn-cxx/security/additional-description.hpp>
Yingdi Yub4be64a2013-10-13 17:24:50 -070015
Yingdi Yueb692ac2015-02-10 18:46:18 -080016namespace chronochat {
Yingdi Yu2e3199c2013-11-06 18:42:34 -080017
Yingdi Yu0b0a7362014-08-05 16:31:30 -070018using std::vector;
19using std::string;
20using std::map;
21
Varun Patil3d850902020-11-23 12:19:14 +053022using ndn::security::Certificate;
Yingdi Yu0b0a7362014-08-05 16:31:30 -070023
Yingdi Yu17032f82014-03-25 15:48:23 -070024const std::string Profile::OID_NAME("2.5.4.41");
25const std::string Profile::OID_ORG("2.5.4.11");
26const std::string Profile::OID_GROUP("2.5.4.1");
27const std::string Profile::OID_HOMEPAGE("2.5.4.3");
28const std::string Profile::OID_ADVISOR("2.5.4.80");
29const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
Yingdi Yufa4ce792014-02-06 18:09:22 -080030
Varun Patil3d850902020-11-23 12:19:14 +053031Profile::Profile(const Certificate& identityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080032{
Varun Patil3d850902020-11-23 12:19:14 +053033 Name keyName = identityCertificate.getKeyName();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080034
Varun Patil3d850902020-11-23 12:19:14 +053035 m_entries[string("IDENTITY")] = keyName.getPrefix(-2).toUri();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070036
Varun Patil3d850902020-11-23 12:19:14 +053037 auto additionalWire = identityCertificate.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription);
38 if (additionalWire) {
39 ndn::security::AdditionalDescription additional(*additionalWire);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070040
Varun Patil3d850902020-11-23 12:19:14 +053041 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 Yu0b0a7362014-08-05 16:31:30 -070059 }
Yingdi Yu2e3199c2013-11-06 18:42:34 -080060}
61
Yingdi Yub4be64a2013-10-13 17:24:50 -070062Profile::Profile(const Name& identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070063{
Yingdi Yufa4ce792014-02-06 18:09:22 -080064 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu3b318c12013-10-15 17:54:00 -070065}
Yingdi Yub4be64a2013-10-13 17:24:50 -070066
67Profile::Profile(const Name& identityName,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070068 const string& name,
69 const string& institution)
Yingdi Yub4be64a2013-10-13 17:24:50 -070070{
Yingdi Yufa4ce792014-02-06 18:09:22 -080071 m_entries["IDENTITY"] = identityName.toUri();
72 m_entries["name"] = name;
73 m_entries["institution"] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070074}
75
76Profile::Profile(const Profile& profile)
Yingdi Yufa4ce792014-02-06 18:09:22 -080077 : m_entries(profile.m_entries)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070078{
79}
Yingdi Yub4be64a2013-10-13 17:24:50 -070080
Varun Patil3d850902020-11-23 12:19:14 +053081Profile::Profile(const Block& profileWire)
82{
83 this->wireDecode(profileWire);
84}
85
86template<ndn::encoding::Tag T>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080087size_t
88Profile::wireEncode(ndn::EncodingImpl<T>& block) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070089{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080090 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
130const Block&
131Profile::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 Yub4be64a2013-10-13 17:24:50 -0700143}
144
Yingdi Yu76dd8002013-12-24 11:16:32 +0800145void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800146Profile::wireDecode(const Block& profileWire)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700147{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800148 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 Yufa4ce792014-02-06 18:09:22 -0800191}
Yingdi Yub4be64a2013-10-13 17:24:50 -0700192
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700193bool
194Profile::operator==(const Profile& profile) const
Yingdi Yufa4ce792014-02-06 18:09:22 -0800195{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700196 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
210bool
211Profile::operator!=(const Profile& profile) const
212{
213 return !(*this == profile);
214}
215
Yingdi Yueb692ac2015-02-10 18:46:18 -0800216} // namespace chronochat