Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -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 "endorse-certificate.h" |
| 12 | #include "exception.h" |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 13 | #include <ndn.cxx/helpers/der/der.h> |
| 14 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 15 | #include <ndn.cxx/security/certificate/certificate-subdescrpt.h> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 17 | |
| 18 | using namespace std; |
| 19 | using namespace ndn; |
| 20 | using namespace ndn::security; |
| 21 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 22 | INIT_LOGGER("EndorseCertificate"); |
| 23 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 24 | ProfileExtension::ProfileExtension(const ProfileData & profileData) |
| 25 | : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire()) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 26 | {} |
| 27 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 28 | ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension) |
| 29 | : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.m_extnValue) |
| 30 | {} |
| 31 | |
| 32 | ProfileExtension::ProfileExtension(const CertificateExtension& extension) |
| 33 | : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue()) |
| 34 | { |
| 35 | if(m_extnID != OID("1.3.6.1.5.32.2.1")) |
| 36 | throw LnException("Wrong ProfileExtension Number!"); |
| 37 | } |
| 38 | |
| 39 | Ptr<ProfileData> |
| 40 | ProfileExtension::getProfileData() |
| 41 | { |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 42 | Ptr<Blob> dataBlob = Ptr<Blob>(new Blob(m_extnValue.buf (), m_extnValue.size ())); |
| 43 | return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(dataBlob))); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | EndorseExtension::EndorseExtension(const vector<string>& endorsedList) |
| 47 | : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList)) |
| 48 | {} |
| 49 | |
| 50 | EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension) |
| 51 | : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue) |
| 52 | {} |
| 53 | |
| 54 | EndorseExtension::EndorseExtension(const CertificateExtension& extension) |
| 55 | : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue()) |
| 56 | { |
| 57 | if(m_extnID != OID("1.3.6.1.5.32.2.2")) |
| 58 | throw LnException("Wrong EndorseExtension Number!"); |
| 59 | } |
| 60 | |
| 61 | vector<string> |
| 62 | EndorseExtension::getEndorsedList() |
| 63 | { |
| 64 | vector<string> endorsedList; |
| 65 | |
| 66 | boost::iostreams::stream |
| 67 | <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size()); |
| 68 | |
| 69 | Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is))); |
| 70 | const der::DerNodePtrList & children = root->getChildren(); |
| 71 | der::SimpleVisitor simpleVisitor; |
| 72 | |
| 73 | for(int i = 0; i < children.size(); i++) |
| 74 | endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor))); |
| 75 | |
| 76 | return endorsedList; |
| 77 | } |
| 78 | |
| 79 | Ptr<Blob> |
| 80 | EndorseExtension::prepareValue(const vector<string>& endorsedList) |
| 81 | { |
| 82 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 83 | |
| 84 | vector<string>::const_iterator it = endorsedList.begin(); |
| 85 | for(; it != endorsedList.end(); it++) |
| 86 | { |
| 87 | Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it)); |
| 88 | root->addChild(entry); |
| 89 | } |
| 90 | |
| 91 | blob_stream blobStream; |
| 92 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 93 | root->encode(start); |
| 94 | |
| 95 | return blobStream.buf (); |
| 96 | } |
| 97 | |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 98 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 99 | Ptr<ProfileData> profileData, |
| 100 | const vector<string>& endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 101 | : Certificate() |
| 102 | , m_keyName(kskCertificate.getPublicKeyName()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 103 | , m_signer(kskCertificate.getPublicKeyName()) |
| 104 | , m_profileData(profileData) |
| 105 | , m_endorseList(endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 106 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 107 | Name dataName = m_keyName; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 108 | dataName.append("PROFILE-CERT").append(m_signer).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 109 | setName(dataName); |
| 110 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 111 | setNotBefore(kskCertificate.getNotBefore()); |
| 112 | setNotAfter(kskCertificate.getNotAfter()); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 113 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 114 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 115 | addExtension(ProfileExtension(*m_profileData)); |
| 116 | addExtension(EndorseExtension(m_endorseList)); |
| 117 | |
| 118 | encode(); |
| 119 | } |
| 120 | |
| 121 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 122 | const Name& signer, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 123 | const vector<string>& endorseList) |
| 124 | : Certificate() |
| 125 | , m_keyName(endorseCertificate.m_keyName) |
| 126 | , m_signer(signer) |
| 127 | , m_profileData(endorseCertificate.m_profileData) |
| 128 | , m_endorseList(endorseList) |
| 129 | { |
| 130 | Name dataName = m_keyName; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 131 | dataName.append("PROFILE-CERT").append(m_signer).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 132 | setName(dataName); |
| 133 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 134 | setNotBefore(endorseCertificate.getNotBefore()); |
| 135 | setNotAfter(endorseCertificate.getNotAfter()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 136 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
| 137 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
| 138 | addExtension(ProfileExtension(*m_profileData)); |
| 139 | addExtension(EndorseExtension(m_endorseList)); |
| 140 | |
| 141 | encode(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 145 | : Certificate(endorseCertificate) |
| 146 | , m_keyName(endorseCertificate.m_keyName) |
| 147 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 148 | , m_profileData(endorseCertificate.m_profileData) |
| 149 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 150 | {} |
| 151 | |
| 152 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 153 | : Certificate(data) |
| 154 | { |
| 155 | const Name& dataName = data.getName(); |
| 156 | name::Component certFlag(string("PROFILE-CERT")); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 157 | int profileIndex = -1; |
| 158 | for(int i = 0; i < dataName.size(); i++) |
| 159 | { |
| 160 | if(0 == dataName.get(i).compare(certFlag)) |
| 161 | { |
| 162 | profileIndex = i; |
| 163 | break; |
| 164 | } |
| 165 | } |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 166 | if(profileIndex < 0) |
| 167 | throw LnException("No PROFILE-CERT component in data name!"); |
| 168 | |
| 169 | m_keyName = dataName.getSubName(0, profileIndex); |
| 170 | m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2); |
| 171 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 172 | OID profileExtensionOID("1.3.6.1.5.32.2.1"); |
| 173 | OID endorseExtensionOID("1.3.6.1.5.32.2.2"); |
| 174 | |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 175 | ExtensionList::iterator it = m_extnList.begin(); |
| 176 | for(; it != m_extnList.end(); it++) |
| 177 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 178 | if(profileExtensionOID == it->getOID()) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 179 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 180 | ProfileExtension profileExtension(*it); |
| 181 | m_profileData = profileExtension.getProfileData(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 182 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 183 | if(endorseExtensionOID == it->getOID()) |
| 184 | { |
| 185 | EndorseExtension endorseExtension(*it); |
| 186 | m_endorseList = endorseExtension.getEndorsedList(); |
| 187 | } |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 188 | } |
| 189 | } |