Yingdi Yu | c972c45 | 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 | b4be64a | 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 | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 15 | #include <ndn.cxx/security/certificate/certificate-subdescrpt.h> |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 16 | #include "logging.h" |
Yingdi Yu | c972c45 | 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 | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 22 | INIT_LOGGER("EndorseCertificate"); |
| 23 | |
Yingdi Yu | b4be64a | 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 | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 26 | {} |
| 27 | |
Yingdi Yu | b4be64a | 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 | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 42 | // _LOG_DEBUG("size: " << m_extnValue.size ()); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 43 | boost::iostreams::stream |
| 44 | <boost::iostreams::array_source> is (m_extnValue.buf (), m_extnValue.size ()); |
| 45 | return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(is))); |
| 46 | } |
| 47 | |
| 48 | EndorseExtension::EndorseExtension(const vector<string>& endorsedList) |
| 49 | : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList)) |
| 50 | {} |
| 51 | |
| 52 | EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension) |
| 53 | : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue) |
| 54 | {} |
| 55 | |
| 56 | EndorseExtension::EndorseExtension(const CertificateExtension& extension) |
| 57 | : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue()) |
| 58 | { |
| 59 | if(m_extnID != OID("1.3.6.1.5.32.2.2")) |
| 60 | throw LnException("Wrong EndorseExtension Number!"); |
| 61 | } |
| 62 | |
| 63 | vector<string> |
| 64 | EndorseExtension::getEndorsedList() |
| 65 | { |
| 66 | vector<string> endorsedList; |
| 67 | |
| 68 | boost::iostreams::stream |
| 69 | <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size()); |
| 70 | |
| 71 | Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is))); |
| 72 | const der::DerNodePtrList & children = root->getChildren(); |
| 73 | der::SimpleVisitor simpleVisitor; |
| 74 | |
| 75 | for(int i = 0; i < children.size(); i++) |
| 76 | endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor))); |
| 77 | |
| 78 | return endorsedList; |
| 79 | } |
| 80 | |
| 81 | Ptr<Blob> |
| 82 | EndorseExtension::prepareValue(const vector<string>& endorsedList) |
| 83 | { |
| 84 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 85 | |
| 86 | vector<string>::const_iterator it = endorsedList.begin(); |
| 87 | for(; it != endorsedList.end(); it++) |
| 88 | { |
| 89 | Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it)); |
| 90 | root->addChild(entry); |
| 91 | } |
| 92 | |
| 93 | blob_stream blobStream; |
| 94 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 95 | root->encode(start); |
| 96 | |
| 97 | return blobStream.buf (); |
| 98 | } |
| 99 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 100 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 101 | const Time& notBefore, |
| 102 | const Time& notAfter, |
| 103 | Ptr<ProfileData> profileData, |
| 104 | const vector<string>& endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 105 | : Certificate() |
| 106 | , m_keyName(kskCertificate.getPublicKeyName()) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 107 | , m_signer(kskCertificate.getPublicKeyName()) |
| 108 | , m_profileData(profileData) |
| 109 | , m_endorseList(endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 110 | { |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 111 | Name dataName = m_keyName; |
| 112 | TimeInterval ti = time::NowUnixTimestamp(); |
| 113 | ostringstream oss; |
| 114 | oss << ti.total_seconds(); |
| 115 | dataName.append("PROFILE-CERT").append(m_signer).append(oss.str()); |
| 116 | setName(dataName); |
| 117 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 118 | setNotBefore(notBefore); |
| 119 | setNotAfter(notAfter); |
| 120 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 121 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 122 | addExtension(ProfileExtension(*m_profileData)); |
| 123 | addExtension(EndorseExtension(m_endorseList)); |
| 124 | |
| 125 | encode(); |
| 126 | } |
| 127 | |
| 128 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 129 | const Name& signer, |
| 130 | const Time& notBefore, |
| 131 | const Time& notAfter, |
| 132 | const vector<string>& endorseList) |
| 133 | : Certificate() |
| 134 | , m_keyName(endorseCertificate.m_keyName) |
| 135 | , m_signer(signer) |
| 136 | , m_profileData(endorseCertificate.m_profileData) |
| 137 | , m_endorseList(endorseList) |
| 138 | { |
| 139 | Name dataName = m_keyName; |
| 140 | TimeInterval ti = time::NowUnixTimestamp(); |
| 141 | ostringstream oss; |
| 142 | oss << ti.total_seconds(); |
| 143 | dataName.append("PROFILE-CERT").append(m_signer).append(oss.str()); |
| 144 | setName(dataName); |
| 145 | |
| 146 | setNotBefore(notBefore); |
| 147 | setNotAfter(notAfter); |
| 148 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
| 149 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
| 150 | addExtension(ProfileExtension(*m_profileData)); |
| 151 | addExtension(EndorseExtension(m_endorseList)); |
| 152 | |
| 153 | encode(); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 157 | : Certificate(endorseCertificate) |
| 158 | , m_keyName(endorseCertificate.m_keyName) |
| 159 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 160 | , m_profileData(endorseCertificate.m_profileData) |
| 161 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 162 | {} |
| 163 | |
| 164 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 165 | : Certificate(data) |
| 166 | { |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 167 | // _LOG_DEBUG("0"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 168 | const Name& dataName = data.getName(); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 169 | // _LOG_DEBUG("1"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 170 | name::Component certFlag(string("PROFILE-CERT")); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 171 | // _LOG_DEBUG("2"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 172 | int profileIndex = -1; |
| 173 | for(int i = 0; i < dataName.size(); i++) |
| 174 | { |
| 175 | if(0 == dataName.get(i).compare(certFlag)) |
| 176 | { |
| 177 | profileIndex = i; |
| 178 | break; |
| 179 | } |
| 180 | } |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 181 | // _LOG_DEBUG("3"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 182 | if(profileIndex < 0) |
| 183 | throw LnException("No PROFILE-CERT component in data name!"); |
| 184 | |
| 185 | m_keyName = dataName.getSubName(0, profileIndex); |
| 186 | m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2); |
| 187 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 188 | // _LOG_DEBUG("keyName: " << m_keyName.toUri()); |
| 189 | // _LOG_DEBUG("signer: " << m_signer.toUri()); |
| 190 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 191 | OID profileExtensionOID("1.3.6.1.5.32.2.1"); |
| 192 | OID endorseExtensionOID("1.3.6.1.5.32.2.2"); |
| 193 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 194 | // _LOG_DEBUG("OID ready"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 195 | ExtensionList::iterator it = m_extnList.begin(); |
| 196 | for(; it != m_extnList.end(); it++) |
| 197 | { |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 198 | // _LOG_DEBUG("entry"); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 199 | if(profileExtensionOID == it->getOID()) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 200 | { |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 201 | // _LOG_DEBUG("ProfileExtn"); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 202 | ProfileExtension profileExtension(*it); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 203 | // _LOG_DEBUG("ProfileExtn created"); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 204 | m_profileData = profileExtension.getProfileData(); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 205 | // _LOG_DEBUG("get profileDate"); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 206 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 207 | if(endorseExtensionOID == it->getOID()) |
| 208 | { |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 209 | // _LOG_DEBUG("EndorseExtn"); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 210 | EndorseExtension endorseExtension(*it); |
| 211 | m_endorseList = endorseExtension.getEndorsedList(); |
| 212 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 213 | } |
| 214 | } |