blob: 24372a4e7e7b746325ea0660c23beb9f6b86fdd4 [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"
12#include <ndn.cxx/helpers/der/der.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070013#include <ndn.cxx/helpers/der/visitor/print-visitor.h>
Yingdi Yub4be64a2013-10-13 17:24:50 -070014#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070015#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070016
17using namespace std;
18using namespace ndn;
19
Yingdi Yu92e8e482013-10-17 21:13:03 -070020INIT_LOGGER("Profile");
Yingdi Yu2e3199c2013-11-06 18:42:34 -080021
22static string nameOid("2.5.4.41");
23static string orgOid("2.5.4.11");
24static string groupOid("2.5.4.1");
25static string homepageOid("2.5.4.3");
26static string advisor("2.5.4.80");
27static string emailOid("1.2.840.113549.1.9.1");
28
Yingdi Yuae8217c2013-11-09 00:03:26 -080029Profile::Profile(const security::IdentityCertificate& oldIdentityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080030{
31 using namespace ndn::security;
Yingdi Yuae8217c2013-11-09 00:03:26 -080032 security::IdentityCertificate identityCertificate(oldIdentityCertificate);
Yingdi Yu2e3199c2013-11-06 18:42:34 -080033
34 Name keyName = identityCertificate.getPublicKeyName();
35 m_identityName = keyName.getPrefix(keyName.size()-1);
36
37 const string& identityString = m_identityName.toUri();
38 Blob identityBlob (identityString.c_str(), identityString.size());
39 m_entries[string("IDENTITY")] = identityBlob;
40
41 const vector<CertificateSubDescrypt>& subList = identityCertificate.getSubjectDescriptionList();
42 vector<CertificateSubDescrypt>::const_iterator it = subList.begin();
43 for(; it != subList.end(); it++)
44 {
45 string oidStr = it->getOidStr();
46 Blob blob (it->getValue().c_str(), it->getValue().size());
47 if(oidStr == nameOid)
48 m_entries[string("name")] = blob;
49 else if(oidStr == orgOid)
50 m_entries[string("institution")] = blob;
51 else if(oidStr == groupOid)
52 m_entries[string("group")] = blob;
53 else if(oidStr == homepageOid)
54 m_entries[string("homepage")] = blob;
55 else if(oidStr == advisor)
56 m_entries[string("advisor")] = blob;
57 else if(oidStr == emailOid)
58 m_entries[string("email")] = blob;
59 else
60 m_entries[oidStr] = blob;
61 }
62}
63
Yingdi Yub4be64a2013-10-13 17:24:50 -070064Profile::Profile(const Name& identityName)
65 : m_identityName(identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070066{
67 const string& nameString = identityName.toUri();
68 Blob identityBlob (nameString.c_str(), nameString.size());
69 m_entries[string("IDENTITY")] = identityBlob;
70}
Yingdi Yub4be64a2013-10-13 17:24:50 -070071
72Profile::Profile(const Name& identityName,
73 const string& name,
74 const string& institution)
75 : m_identityName(identityName)
76{
Yingdi Yu3b318c12013-10-15 17:54:00 -070077 const string& nameString = identityName.toUri();
78 Blob identityBlob (nameString.c_str(), nameString.size());
79 m_entries[string("IDENTITY")] = identityBlob;
80
Yingdi Yub4be64a2013-10-13 17:24:50 -070081 Blob nameBlob (name.c_str(), name.size());
82 Blob institutionBlob (institution.c_str(), institution.size());
83
84 m_entries[string("name")] = nameBlob;
85 m_entries[string("institution")] = institutionBlob;
86}
87
88Profile::Profile(const Profile& profile)
89 : m_identityName(profile.m_identityName)
90 , m_entries(profile.m_entries)
91{}
92
93void
94Profile::setProfileEntry(const string& profileType,
95 const Blob& profileValue)
96{ m_entries[profileType] = profileValue; }
97
98Ptr<const Blob>
Yingdi Yudbeb8e22013-10-14 09:36:31 -070099Profile::getProfileEntry(const string& profileType) const
Yingdi Yub4be64a2013-10-13 17:24:50 -0700100{
101 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yudbeb8e22013-10-14 09:36:31 -0700102 return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700103
104 return NULL;
105}
106
107Ptr<Blob>
108Profile::toDerBlob() const
109{
110 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
111
112 Ptr<der::DerPrintableString> identityName = Ptr<der::DerPrintableString>(new der::DerPrintableString(m_identityName.toUri()));
113 root->addChild(identityName);
114
115 map<string, Blob>::const_iterator it = m_entries.begin();
116 for(; it != m_entries.end(); it++)
117 {
118 Ptr<der::DerSequence> entry = Ptr<der::DerSequence>::Create();
119 Ptr<der::DerPrintableString> type = Ptr<der::DerPrintableString>(new der::DerPrintableString(it->first));
120 Ptr<der::DerOctetString> value = Ptr<der::DerOctetString>(new der::DerOctetString(it->second));
121 entry->addChild(type);
122 entry->addChild(value);
123 root->addChild(entry);
124 }
125
126 blob_stream blobStream;
127 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
128 root->encode(start);
129
130 return blobStream.buf ();
131}
132
133Ptr<Profile>
134Profile::fromDerBlob(const Blob& derBlob)
135{
136 boost::iostreams::stream
137 <boost::iostreams::array_source> is (derBlob.buf(), derBlob.size());
138
139 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700140 const der::DerNodePtrList & children = root->getChildren();
141 der::SimpleVisitor simpleVisitor;
142 string identityName = boost::any_cast<string>(children[0]->accept(simpleVisitor));
143 Ptr<Profile> profile = Ptr<Profile>(new Profile(identityName));
144
145 for(int i = 1; i < children.size(); i++)
146 {
147 Ptr<der::DerSequence> entry = DynamicCast<der::DerSequence>(children[i]);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700148 const der::DerNodePtrList & tuple = entry->getChildren();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700149 string type = boost::any_cast<string>(tuple[0]->accept(simpleVisitor));
150 Ptr<Blob> value = boost::any_cast<Ptr<Blob> >(tuple[1]->accept(simpleVisitor));
151 profile->setProfileEntry(type, *value);
152 }
153
154 return profile;
155}