Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 13 | #include <ndn.cxx/helpers/der/visitor/print-visitor.h> |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 14 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 15 | #include "logging.h" |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace std; |
| 18 | using namespace ndn; |
| 19 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 20 | INIT_LOGGER("Profile"); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 21 | |
| 22 | static string nameOid("2.5.4.41"); |
| 23 | static string orgOid("2.5.4.11"); |
| 24 | static string groupOid("2.5.4.1"); |
| 25 | static string homepageOid("2.5.4.3"); |
| 26 | static string advisor("2.5.4.80"); |
| 27 | static string emailOid("1.2.840.113549.1.9.1"); |
| 28 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 29 | Profile::Profile(const security::IdentityCertificate& oldIdentityCertificate) |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 30 | { |
| 31 | using namespace ndn::security; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 32 | security::IdentityCertificate identityCertificate(oldIdentityCertificate); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 33 | |
| 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 Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | Profile::Profile(const Name& identityName) |
| 65 | : m_identityName(identityName) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 66 | { |
| 67 | const string& nameString = identityName.toUri(); |
| 68 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 69 | m_entries[string("IDENTITY")] = identityBlob; |
| 70 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 71 | |
| 72 | Profile::Profile(const Name& identityName, |
| 73 | const string& name, |
| 74 | const string& institution) |
| 75 | : m_identityName(identityName) |
| 76 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 77 | const string& nameString = identityName.toUri(); |
| 78 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 79 | m_entries[string("IDENTITY")] = identityBlob; |
| 80 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 81 | 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 | |
| 88 | Profile::Profile(const Profile& profile) |
| 89 | : m_identityName(profile.m_identityName) |
| 90 | , m_entries(profile.m_entries) |
| 91 | {} |
| 92 | |
| 93 | void |
| 94 | Profile::setProfileEntry(const string& profileType, |
| 95 | const Blob& profileValue) |
| 96 | { m_entries[profileType] = profileValue; } |
| 97 | |
| 98 | Ptr<const Blob> |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 99 | Profile::getProfileEntry(const string& profileType) const |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 100 | { |
| 101 | if(m_entries.find(profileType) != m_entries.end()) |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 102 | return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size())); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | Ptr<Blob> |
| 108 | Profile::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 | |
| 133 | Ptr<Profile> |
| 134 | Profile::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 Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 140 | 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 Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 148 | const der::DerNodePtrList & tuple = entry->getChildren(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 149 | 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 | } |