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