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" |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 12 | #include "endorse-extension.pb.h" |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 13 | #include <boost/iostreams/stream.hpp> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 18 | using namespace ndn::ptr_lib; |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 20 | INIT_LOGGER("EndorseCertificate"); |
| 21 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 22 | ProfileExtension::ProfileExtension(const ProfileData & profileData) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 23 | : CertificateExtension("1.3.6.1.5.32.2.1", true, Buffer(profileData.wireEncode().wire(), profileData.wireEncode().size())) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 24 | {} |
| 25 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 26 | ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 27 | : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 28 | {} |
| 29 | |
| 30 | ProfileExtension::ProfileExtension(const CertificateExtension& extension) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 31 | : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 32 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 33 | if(extensionId_ != OID("1.3.6.1.5.32.2.1")) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 34 | throw Error("Wrong ProfileExtension Number!"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 37 | shared_ptr<ProfileData> |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 38 | ProfileExtension::getProfileData() |
| 39 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 40 | Data data; |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 41 | data.wireDecode(Block(extensionValue_.buf(), extensionValue_.size())); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 42 | return make_shared<ProfileData>(data); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 45 | EndorseExtension::EndorseExtension(const vector<string>& endorseList) |
| 46 | : CertificateExtension("1.3.6.1.5.32.2.2", true, EndorseExtension::encodeEndorseList(endorseList)) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 47 | {} |
| 48 | |
| 49 | EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 50 | : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 51 | {} |
| 52 | |
| 53 | EndorseExtension::EndorseExtension(const CertificateExtension& extension) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 54 | : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 55 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 56 | if(extensionId_ != OID("1.3.6.1.5.32.2.2")) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 57 | throw Error("Wrong EndorseExtension Number!"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | vector<string> |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 61 | EndorseExtension::getEndorseList() |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 62 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 63 | Chronos::EndorseExtensionMsg endorseExtension; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | |
| 65 | boost::iostreams::stream |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 66 | <boost::iostreams::array_source> is ((const char*)extensionValue_.buf(), extensionValue_.size()); |
| 67 | |
| 68 | endorseExtension.ParseFromIstream(&is); |
| 69 | |
| 70 | vector<string> endorseList; |
| 71 | |
| 72 | for(int i = 0; i < endorseExtension.endorseentry_size(); i ++) |
| 73 | endorseList.push_back(endorseExtension.endorseentry(i).name()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 74 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 75 | return endorseList; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 78 | Buffer |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 79 | EndorseExtension::encodeEndorseList(const vector<string>& endorseList) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 80 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 81 | Chronos::EndorseExtensionMsg endorseExtension; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 82 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 83 | vector<string>::const_iterator it = endorseList.begin(); |
| 84 | for(; it != endorseList.end(); it++) |
| 85 | endorseExtension.add_endorseentry()->set_name(*it); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 86 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 87 | string encoded; |
| 88 | endorseExtension.SerializeToString(&encoded); |
| 89 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 90 | return Buffer(encoded.c_str(), encoded.size()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 93 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 94 | const ProfileData& profileData, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 95 | const vector<string>& endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 96 | : Certificate() |
| 97 | , m_keyName(kskCertificate.getPublicKeyName()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 98 | , m_signer(kskCertificate.getPublicKeyName()) |
| 99 | , m_profileData(profileData) |
| 100 | , m_endorseList(endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 101 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 102 | Name dataName = m_keyName; |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 103 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 104 | setName(dataName); |
| 105 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 106 | setNotBefore(kskCertificate.getNotBefore()); |
| 107 | setNotAfter(kskCertificate.getNotAfter()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 108 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 109 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 110 | addExtension(ProfileExtension(m_profileData)); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 111 | addExtension(EndorseExtension(m_endorseList)); |
| 112 | |
| 113 | encode(); |
| 114 | } |
| 115 | |
| 116 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 117 | const Name& signer, |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 118 | const vector<string>& endorseList) |
| 119 | : Certificate() |
| 120 | , m_keyName(endorseCertificate.m_keyName) |
| 121 | , m_signer(signer) |
| 122 | , m_profileData(endorseCertificate.m_profileData) |
| 123 | , m_endorseList(endorseList) |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 124 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 125 | Name dataName = m_keyName; |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 126 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 127 | setName(dataName); |
| 128 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 129 | setNotBefore(endorseCertificate.getNotBefore()); |
| 130 | setNotAfter(endorseCertificate.getNotAfter()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 131 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 132 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 133 | addExtension(ProfileExtension(m_profileData)); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 134 | addExtension(EndorseExtension(m_endorseList)); |
| 135 | |
| 136 | encode(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 140 | : Certificate(endorseCertificate) |
| 141 | , m_keyName(endorseCertificate.m_keyName) |
| 142 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 143 | , m_profileData(endorseCertificate.m_profileData) |
| 144 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 145 | {} |
| 146 | |
| 147 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 148 | : Certificate(data) |
| 149 | { |
| 150 | const Name& dataName = data.getName(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 151 | |
Yingdi Yu | 6df6125 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 152 | if(dataName.size() < 3 || !dataName.get(-3).equals("PROFILE-CERT")) |
| 153 | throw Error("No PROFILE-CERT component in data name!"); |
| 154 | |
| 155 | m_keyName = dataName.getPrefix(-3); |
| 156 | m_signer.wireDecode(Block(dataName.get(-2).getValue().buf(), |
| 157 | dataName.get(-2).getValue().size())); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 158 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 159 | OID profileExtensionOID("1.3.6.1.5.32.2.1"); |
| 160 | OID endorseExtensionOID("1.3.6.1.5.32.2.2"); |
| 161 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 162 | ExtensionList::iterator it = extensionList_.begin(); |
| 163 | for(; it != extensionList_.end(); it++) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 164 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 165 | if(profileExtensionOID == it->getOid()) |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 166 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 167 | ProfileExtension profileExtension(*it); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 168 | m_profileData = *profileExtension.getProfileData(); |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 169 | } |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 170 | if(endorseExtensionOID == it->getOid()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 171 | { |
| 172 | EndorseExtension endorseExtension(*it); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 173 | m_endorseList = endorseExtension.getEndorseList(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 174 | } |
Yingdi Yu | 3c2a976 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 175 | } |
| 176 | } |