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 | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 42 | // _LOG_DEBUG("size: " << m_extnValue.size ()); |
Yingdi Yu | 5ff6210 | 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 | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 100 | // EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate) |
| 101 | // : Certificate() |
| 102 | // , m_keyName(kskCertificate.getPublicKeyName()) |
| 103 | // , m_signer(kskCertificate.getPublicKeyName()) |
| 104 | // { |
| 105 | // Profile profile(m_keyName.getPrefix(m_keyName.size()-1), |
| 106 | // m_keyName.get(-2).toUri(), |
| 107 | // m_keyName.get(m_keyName.size()-2).toUri()); |
| 108 | |
| 109 | // Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(m_keyName.getPrefix(m_keyName.size()-1), |
| 110 | // profile)); |
| 111 | |
| 112 | // m_profileData = profileData; |
| 113 | |
| 114 | // Name dataName = m_keyName; |
| 115 | // dataName.append("PROFILE-CERT").append(m_signer).appendVersion(); |
| 116 | // setName(dataName); |
| 117 | |
| 118 | // setNotBefore(kskCertificate.getNotBefore()); |
| 119 | // setNotAfter(kskCertificate.getNotAfter()); |
| 120 | // addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
| 121 | // setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
| 122 | // addExtension(ProfileExtension(*m_profileData)); |
| 123 | // addExtension(EndorseExtension(m_endorseList)); |
| 124 | |
| 125 | // encode(); |
| 126 | // } |
| 127 | |
| 128 | |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 129 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 130 | Ptr<ProfileData> profileData, |
| 131 | const vector<string>& endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 132 | : Certificate() |
| 133 | , m_keyName(kskCertificate.getPublicKeyName()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 134 | , m_signer(kskCertificate.getPublicKeyName()) |
| 135 | , m_profileData(profileData) |
| 136 | , m_endorseList(endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 137 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 138 | Name dataName = m_keyName; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 139 | dataName.append("PROFILE-CERT").append(m_signer).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 140 | setName(dataName); |
| 141 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 142 | setNotBefore(kskCertificate.getNotBefore()); |
| 143 | setNotAfter(kskCertificate.getNotAfter()); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 144 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 145 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 146 | addExtension(ProfileExtension(*m_profileData)); |
| 147 | addExtension(EndorseExtension(m_endorseList)); |
| 148 | |
| 149 | encode(); |
| 150 | } |
| 151 | |
| 152 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 153 | const Name& signer, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 154 | const vector<string>& endorseList) |
| 155 | : Certificate() |
| 156 | , m_keyName(endorseCertificate.m_keyName) |
| 157 | , m_signer(signer) |
| 158 | , m_profileData(endorseCertificate.m_profileData) |
| 159 | , m_endorseList(endorseList) |
| 160 | { |
| 161 | Name dataName = m_keyName; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 162 | dataName.append("PROFILE-CERT").append(m_signer).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 163 | setName(dataName); |
| 164 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 165 | setNotBefore(endorseCertificate.getNotBefore()); |
| 166 | setNotAfter(endorseCertificate.getNotAfter()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 167 | addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri())); |
| 168 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
| 169 | addExtension(ProfileExtension(*m_profileData)); |
| 170 | addExtension(EndorseExtension(m_endorseList)); |
| 171 | |
| 172 | encode(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 176 | : Certificate(endorseCertificate) |
| 177 | , m_keyName(endorseCertificate.m_keyName) |
| 178 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 179 | , m_profileData(endorseCertificate.m_profileData) |
| 180 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 181 | {} |
| 182 | |
| 183 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 184 | : Certificate(data) |
| 185 | { |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 186 | // _LOG_DEBUG("0"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 187 | const Name& dataName = data.getName(); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 188 | // _LOG_DEBUG("1"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 189 | name::Component certFlag(string("PROFILE-CERT")); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 190 | // _LOG_DEBUG("2"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 191 | int profileIndex = -1; |
| 192 | for(int i = 0; i < dataName.size(); i++) |
| 193 | { |
| 194 | if(0 == dataName.get(i).compare(certFlag)) |
| 195 | { |
| 196 | profileIndex = i; |
| 197 | break; |
| 198 | } |
| 199 | } |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 200 | // _LOG_DEBUG("3"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 201 | if(profileIndex < 0) |
| 202 | throw LnException("No PROFILE-CERT component in data name!"); |
| 203 | |
| 204 | m_keyName = dataName.getSubName(0, profileIndex); |
| 205 | m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2); |
| 206 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 207 | // _LOG_DEBUG("keyName: " << m_keyName.toUri()); |
| 208 | // _LOG_DEBUG("signer: " << m_signer.toUri()); |
| 209 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 210 | OID profileExtensionOID("1.3.6.1.5.32.2.1"); |
| 211 | OID endorseExtensionOID("1.3.6.1.5.32.2.2"); |
| 212 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 213 | // _LOG_DEBUG("OID ready"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 214 | ExtensionList::iterator it = m_extnList.begin(); |
| 215 | for(; it != m_extnList.end(); it++) |
| 216 | { |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 217 | // _LOG_DEBUG("entry"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 218 | if(profileExtensionOID == it->getOID()) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 219 | { |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 220 | // _LOG_DEBUG("ProfileExtn"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 221 | ProfileExtension profileExtension(*it); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 222 | // _LOG_DEBUG("ProfileExtn created"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 223 | m_profileData = profileExtension.getProfileData(); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 224 | // _LOG_DEBUG("get profileDate"); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 225 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 226 | if(endorseExtensionOID == it->getOID()) |
| 227 | { |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 228 | // _LOG_DEBUG("EndorseExtn"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 229 | EndorseExtension endorseExtension(*it); |
| 230 | m_endorseList = endorseExtension.getEndorsedList(); |
| 231 | } |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 232 | } |
| 233 | } |