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