blob: 5261207177ad4b4dae0adcd04c28e3ae546a76e4 [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
29Profile::Profile(security::IdentityCertificate& identityCertificate)
30{
31 using namespace ndn::security;
32
33 Name keyName = identityCertificate.getPublicKeyName();
34 m_identityName = keyName.getPrefix(keyName.size()-1);
35
36 const string& identityString = m_identityName.toUri();
37 Blob identityBlob (identityString.c_str(), identityString.size());
38 m_entries[string("IDENTITY")] = identityBlob;
39
40 const vector<CertificateSubDescrypt>& subList = identityCertificate.getSubjectDescriptionList();
41 vector<CertificateSubDescrypt>::const_iterator it = subList.begin();
42 for(; it != subList.end(); it++)
43 {
44 string oidStr = it->getOidStr();
45 Blob blob (it->getValue().c_str(), it->getValue().size());
46 if(oidStr == nameOid)
47 m_entries[string("name")] = blob;
48 else if(oidStr == orgOid)
49 m_entries[string("institution")] = blob;
50 else if(oidStr == groupOid)
51 m_entries[string("group")] = blob;
52 else if(oidStr == homepageOid)
53 m_entries[string("homepage")] = blob;
54 else if(oidStr == advisor)
55 m_entries[string("advisor")] = blob;
56 else if(oidStr == emailOid)
57 m_entries[string("email")] = blob;
58 else
59 m_entries[oidStr] = blob;
60 }
61}
62
Yingdi Yub4be64a2013-10-13 17:24:50 -070063Profile::Profile(const Name& identityName)
64 : m_identityName(identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070065{
66 const string& nameString = identityName.toUri();
67 Blob identityBlob (nameString.c_str(), nameString.size());
68 m_entries[string("IDENTITY")] = identityBlob;
69}
Yingdi Yub4be64a2013-10-13 17:24:50 -070070
71Profile::Profile(const Name& identityName,
72 const string& name,
73 const string& institution)
74 : m_identityName(identityName)
75{
Yingdi Yu3b318c12013-10-15 17:54:00 -070076 const string& nameString = identityName.toUri();
77 Blob identityBlob (nameString.c_str(), nameString.size());
78 m_entries[string("IDENTITY")] = identityBlob;
79
Yingdi Yub4be64a2013-10-13 17:24:50 -070080 Blob nameBlob (name.c_str(), name.size());
81 Blob institutionBlob (institution.c_str(), institution.size());
82
83 m_entries[string("name")] = nameBlob;
84 m_entries[string("institution")] = institutionBlob;
85}
86
87Profile::Profile(const Profile& profile)
88 : m_identityName(profile.m_identityName)
89 , m_entries(profile.m_entries)
90{}
91
92void
93Profile::setProfileEntry(const string& profileType,
94 const Blob& profileValue)
95{ m_entries[profileType] = profileValue; }
96
97Ptr<const Blob>
Yingdi Yudbeb8e22013-10-14 09:36:31 -070098Profile::getProfileEntry(const string& profileType) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070099{
100 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yudbeb8e22013-10-14 09:36:31 -0700101 return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102
103 return NULL;
104}
105
106Ptr<Blob>
107Profile::toDerBlob() const
108{
109 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
110
111 Ptr<der::DerPrintableString> identityName = Ptr<der::DerPrintableString>(new der::DerPrintableString(m_identityName.toUri()));
112 root->addChild(identityName);
113
114 map<string, Blob>::const_iterator it = m_entries.begin();
115 for(; it != m_entries.end(); it++)
116 {
117 Ptr<der::DerSequence> entry = Ptr<der::DerSequence>::Create();
118 Ptr<der::DerPrintableString> type = Ptr<der::DerPrintableString>(new der::DerPrintableString(it->first));
119 Ptr<der::DerOctetString> value = Ptr<der::DerOctetString>(new der::DerOctetString(it->second));
120 entry->addChild(type);
121 entry->addChild(value);
122 root->addChild(entry);
123 }
124
125 blob_stream blobStream;
126 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
127 root->encode(start);
128
129 return blobStream.buf ();
130}
131
132Ptr<Profile>
133Profile::fromDerBlob(const Blob& derBlob)
134{
135 boost::iostreams::stream
136 <boost::iostreams::array_source> is (derBlob.buf(), derBlob.size());
137
138 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700139 const der::DerNodePtrList & children = root->getChildren();
140 der::SimpleVisitor simpleVisitor;
141 string identityName = boost::any_cast<string>(children[0]->accept(simpleVisitor));
142 Ptr<Profile> profile = Ptr<Profile>(new Profile(identityName));
143
144 for(int i = 1; i < children.size(); i++)
145 {
146 Ptr<der::DerSequence> entry = DynamicCast<der::DerSequence>(children[i]);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700147 const der::DerNodePtrList & tuple = entry->getChildren();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700148 string type = boost::any_cast<string>(tuple[0]->accept(simpleVisitor));
149 Ptr<Blob> value = boost::any_cast<Ptr<Blob> >(tuple[1]->accept(simpleVisitor));
150 profile->setProfileEntry(type, *value);
151 }
152
153 return profile;
154}