Yingdi Yu | 5ff6210 | 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 | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 13 | #include <ndn.cxx/helpers/der/visitor/print-visitor.h> |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 14 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 15 | #include "logging.h" |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace std; |
| 18 | using namespace ndn; |
| 19 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 20 | INIT_LOGGER("Profile"); |
Yingdi Yu | e9ea5c9 | 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 | |
| 29 | Profile::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 Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 63 | Profile::Profile(const Name& identityName) |
| 64 | : m_identityName(identityName) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 65 | { |
| 66 | const string& nameString = identityName.toUri(); |
| 67 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 68 | m_entries[string("IDENTITY")] = identityBlob; |
| 69 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 70 | |
| 71 | Profile::Profile(const Name& identityName, |
| 72 | const string& name, |
| 73 | const string& institution) |
| 74 | : m_identityName(identityName) |
| 75 | { |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 76 | const string& nameString = identityName.toUri(); |
| 77 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 78 | m_entries[string("IDENTITY")] = identityBlob; |
| 79 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 80 | 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 | |
| 87 | Profile::Profile(const Profile& profile) |
| 88 | : m_identityName(profile.m_identityName) |
| 89 | , m_entries(profile.m_entries) |
| 90 | {} |
| 91 | |
| 92 | void |
| 93 | Profile::setProfileEntry(const string& profileType, |
| 94 | const Blob& profileValue) |
| 95 | { m_entries[profileType] = profileValue; } |
| 96 | |
| 97 | Ptr<const Blob> |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 98 | Profile::getProfileEntry(const string& profileType) const |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 99 | { |
| 100 | if(m_entries.find(profileType) != m_entries.end()) |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 101 | return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size())); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | Ptr<Blob> |
| 107 | Profile::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 | |
| 132 | Ptr<Profile> |
| 133 | Profile::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 Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 139 | 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 Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 147 | const der::DerNodePtrList & tuple = entry->getChildren(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 148 | 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 | } |