blob: e6044fac63b590861c85806683eb47026f8f102f [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>
9 */
10
11#include "profile.h"
Yingdi Yu92e8e482013-10-17 21:13:03 -070012#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070013
Yingdi Yub4be64a2013-10-13 17:24:50 -070014using namespace ndn;
15
Yingdi Yu92e8e482013-10-17 21:13:03 -070016INIT_LOGGER("Profile");
Yingdi Yu2e3199c2013-11-06 18:42:34 -080017
Yingdi Yufa4ce792014-02-06 18:09:22 -080018namespace chronos{
Yingdi Yu2e3199c2013-11-06 18:42:34 -080019
Yingdi Yu17032f82014-03-25 15:48:23 -070020const std::string Profile::OID_NAME("2.5.4.41");
21const std::string Profile::OID_ORG("2.5.4.11");
22const std::string Profile::OID_GROUP("2.5.4.1");
23const std::string Profile::OID_HOMEPAGE("2.5.4.3");
24const std::string Profile::OID_ADVISOR("2.5.4.80");
25const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
Yingdi Yufa4ce792014-02-06 18:09:22 -080026
27Profile::Profile(const IdentityCertificate& identityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080028{
Yingdi Yufa4ce792014-02-06 18:09:22 -080029 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName());
Yingdi Yu2e3199c2013-11-06 18:42:34 -080030
Yingdi Yu17032f82014-03-25 15:48:23 -070031 m_entries[std::string("IDENTITY")] = keyName.getPrefix(-1).toUri();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070032
Yingdi Yu17032f82014-03-25 15:48:23 -070033 const std::vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList();
34 std::vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080035 for(; it != subList.end(); it++)
36 {
Yingdi Yu17032f82014-03-25 15:48:23 -070037 const std::string oidStr = it->getOidString();
38 std::string valueStr = it->getValue();
Yingdi Yufa4ce792014-02-06 18:09:22 -080039 if(oidStr == OID_NAME)
40 m_entries["name"] = valueStr;
41 else if(oidStr == OID_ORG)
42 m_entries["institution"] = valueStr;
43 else if(oidStr == OID_GROUP)
44 m_entries["group"] = valueStr;
45 else if(oidStr == OID_HOMEPAGE)
46 m_entries["homepage"] = valueStr;
47 else if(oidStr == OID_ADVISOR)
48 m_entries["advisor"] = valueStr;
49 else if(oidStr == OID_EMAIL)
50 m_entries["email"] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080051 else
Yingdi Yu76dd8002013-12-24 11:16:32 +080052 m_entries[oidStr] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080053 }
54}
55
Yingdi Yub4be64a2013-10-13 17:24:50 -070056Profile::Profile(const Name& identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070057{
Yingdi Yufa4ce792014-02-06 18:09:22 -080058 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu3b318c12013-10-15 17:54:00 -070059}
Yingdi Yub4be64a2013-10-13 17:24:50 -070060
61Profile::Profile(const Name& identityName,
Yingdi Yufa0b6a02014-04-30 14:26:42 -070062 const std::string& name,
63 const std::string& institution)
Yingdi Yub4be64a2013-10-13 17:24:50 -070064{
Yingdi Yufa4ce792014-02-06 18:09:22 -080065 m_entries["IDENTITY"] = identityName.toUri();
66 m_entries["name"] = name;
67 m_entries["institution"] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070068}
69
70Profile::Profile(const Profile& profile)
Yingdi Yufa4ce792014-02-06 18:09:22 -080071 : m_entries(profile.m_entries)
Yingdi Yub4be64a2013-10-13 17:24:50 -070072{}
73
74void
Yingdi Yu17032f82014-03-25 15:48:23 -070075Profile::encode(std::ostream& os) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070076{
Yingdi Yufa4ce792014-02-06 18:09:22 -080077 Chronos::ProfileMsg profileMsg;
78 profileMsg << (*this);
79 profileMsg.SerializeToOstream(&os);
Yingdi Yub4be64a2013-10-13 17:24:50 -070080}
81
Yingdi Yu76dd8002013-12-24 11:16:32 +080082void
Yingdi Yu17032f82014-03-25 15:48:23 -070083Profile::decode(std::istream& is)
Yingdi Yub4be64a2013-10-13 17:24:50 -070084{
Yingdi Yufa0b6a02014-04-30 14:26:42 -070085 Chronos::ProfileMsg profileMsg;
Yingdi Yufa4ce792014-02-06 18:09:22 -080086 profileMsg.ParseFromIstream(&is);
87 profileMsg >> (*this);
88}
Yingdi Yub4be64a2013-10-13 17:24:50 -070089
Yingdi Yufa4ce792014-02-06 18:09:22 -080090Chronos::ProfileMsg&
91operator << (Chronos::ProfileMsg& profileMsg, const Profile& profile)
92{
Yingdi Yu17032f82014-03-25 15:48:23 -070093 std::map<std::string, std::string>::const_iterator it = profile.begin();
Yingdi Yufa4ce792014-02-06 18:09:22 -080094 for(; it != profile.end(); it++)
Yingdi Yub4be64a2013-10-13 17:24:50 -070095 {
Yingdi Yu76dd8002013-12-24 11:16:32 +080096 Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
97 profileEntry->set_oid(it->first);
98 profileEntry->set_data(it->second);
Yingdi Yub4be64a2013-10-13 17:24:50 -070099 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800100 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700101}
102
Yingdi Yufa4ce792014-02-06 18:09:22 -0800103Chronos::ProfileMsg&
104operator >> (Chronos::ProfileMsg& profileMsg, Profile& profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700105{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800106 for(int i = 0; i < profileMsg.entry_size(); i++)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700107 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800108 const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800109 profile[profileEntry.oid()] = profileEntry.data();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700110 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700111
Yingdi Yufa4ce792014-02-06 18:09:22 -0800112 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700113}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800114
115}//chronos