Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "endorse-certificate.hpp" |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 13 | #include "endorse-extension.hpp" |
| 14 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 15 | #include <boost/iostreams/stream.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 16 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 17 | #include <ndn-cxx/security/additional-description.hpp> |
| 18 | #include <ndn-cxx/security/validity-period.hpp> |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 20 | namespace chronochat { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 21 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 22 | using std::vector; |
| 23 | using std::string; |
| 24 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 25 | using ndn::security::Certificate; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 26 | using ndn::OBufferStream; |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 27 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 28 | const vector<string> EndorseCertificate::DEFAULT_ENDORSE_LIST; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 29 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 30 | EndorseExtension& |
| 31 | operator<<(EndorseExtension& endorseExtension, const vector<string>& endorseList) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 32 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 33 | for (const auto& entry : endorseList) |
| 34 | endorseExtension.addEntry(entry); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 35 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 36 | return endorseExtension; |
| 37 | } |
| 38 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 39 | EndorseExtension& |
| 40 | operator>>(EndorseExtension& endorseExtension, vector<string>& endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 41 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 42 | const std::list<string>& endorseEntries = endorseExtension.getEntries(); |
| 43 | for (const auto& entry: endorseEntries) |
| 44 | endorseList.push_back(entry); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 45 | |
| 46 | return endorseExtension; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 49 | EndorseCertificate::EndorseCertificate(const Certificate& kskCertificate, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 50 | const Profile& profile, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 51 | const vector<string>& endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 52 | : Certificate() |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 53 | , m_profile(profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 54 | , m_endorseList(endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 55 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 56 | setName(kskCertificate.getKeyName().getPrefix(-2) |
| 57 | .append("PROFILE-CERT") |
| 58 | .append("KEY") |
| 59 | .append(kskCertificate.getKeyId()) |
| 60 | .append("self") |
| 61 | .appendTimestamp()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 62 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 63 | m_signer = kskCertificate.getKeyName(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 65 | setMetaInfo(kskCertificate.getMetaInfo()); |
| 66 | setContent(kskCertificate.getPublicKey().data(), kskCertificate.getPublicKey().size()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 67 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 68 | ndn::security::AdditionalDescription description; |
| 69 | description.set("2.5.4.41", getKeyName().toUri()); |
| 70 | description.set("signer", m_signer.toUri()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 71 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 72 | EndorseExtension endorseExtension; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 73 | endorseExtension << m_endorseList; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 74 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 75 | ndn::SignatureInfo signatureInfo; |
| 76 | signatureInfo.addCustomTlv(description.wireEncode()); |
| 77 | signatureInfo.addCustomTlv(m_profile.wireEncode()); |
| 78 | |
| 79 | if (m_endorseList.size() > 0) |
| 80 | signatureInfo.addCustomTlv(endorseExtension.wireEncode()); |
| 81 | |
| 82 | try { |
| 83 | signatureInfo.setValidityPeriod(kskCertificate.getValidityPeriod()); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 84 | } catch (const tlv::Error&) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 85 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod( |
| 86 | time::system_clock::now(), time::system_clock::now() + time::days(3650))); |
| 87 | } |
| 88 | |
| 89 | setSignatureInfo(signatureInfo); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 93 | const Name& signer, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 94 | const vector<string>& endorseList) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 95 | : Certificate() |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 96 | , m_signer(signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 97 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 98 | , m_endorseList(endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 99 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 100 | setName(endorseCertificate.getName() |
| 101 | .getPrefix(-2) |
| 102 | .append(m_signer.wireEncode()) |
| 103 | .appendVersion()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 104 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 105 | setMetaInfo(endorseCertificate.getMetaInfo()); |
| 106 | setContent(endorseCertificate.getPublicKey().data(), endorseCertificate.getPublicKey().size()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 107 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 108 | ndn::security::AdditionalDescription description; |
| 109 | description.set("2.5.4.41", getKeyName().toUri()); |
| 110 | description.set("signer", m_signer.toUri()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 111 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 112 | EndorseExtension endorseExtension; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 113 | endorseExtension << m_endorseList; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 114 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 115 | ndn::SignatureInfo signatureInfo; |
| 116 | signatureInfo.addCustomTlv(description.wireEncode()); |
| 117 | signatureInfo.addCustomTlv(m_profile.wireEncode()); |
| 118 | |
| 119 | if (m_endorseList.size() > 0) |
| 120 | signatureInfo.addCustomTlv(endorseExtension.wireEncode()); |
| 121 | |
| 122 | try { |
| 123 | signatureInfo.setValidityPeriod(endorseCertificate.getValidityPeriod()); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 124 | } catch (const tlv::Error&) { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 125 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod( |
| 126 | time::system_clock::now(), time::system_clock::now() + time::days(3650))); |
| 127 | } |
| 128 | |
| 129 | setSignatureInfo(signatureInfo); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 132 | EndorseCertificate::EndorseCertificate(const Name& keyName, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 133 | const ndn::Buffer& key, |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 134 | const time::system_clock::TimePoint& notBefore, |
| 135 | const time::system_clock::TimePoint& notAfter, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 136 | const Name::Component& signerKeyId, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 137 | const Name& signer, |
| 138 | const Profile& profile, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 139 | const vector<string>& endorseList) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 140 | : Certificate() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | , m_signer(signer) |
| 142 | , m_profile(profile) |
| 143 | , m_endorseList(endorseList) |
| 144 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 145 | setName(keyName.getPrefix(-2) |
| 146 | .append("PROFILE-CERT") |
| 147 | .append("KEY") |
| 148 | .append(signerKeyId) |
| 149 | .append(m_signer.wireEncode()) |
| 150 | .appendVersion()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 151 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 152 | setContent(key.data(), key.size()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 153 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 154 | ndn::security::AdditionalDescription description; |
| 155 | description.set("2.5.4.41", keyName.toUri()); |
| 156 | description.set("signer", m_signer.toUri()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 157 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 158 | EndorseExtension endorseExtension; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 159 | endorseExtension << m_endorseList; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 160 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 161 | ndn::SignatureInfo signatureInfo; |
| 162 | signatureInfo.addCustomTlv(description.wireEncode()); |
| 163 | signatureInfo.addCustomTlv(m_profile.wireEncode()); |
| 164 | |
| 165 | if (m_endorseList.size() > 0) |
| 166 | signatureInfo.addCustomTlv(endorseExtension.wireEncode()); |
| 167 | |
| 168 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(notBefore, notAfter)); |
| 169 | |
| 170 | setSignatureInfo(signatureInfo); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 173 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 174 | : Certificate(endorseCertificate) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 175 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 176 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 177 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 178 | { |
| 179 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 180 | |
| 181 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 182 | : Certificate(data) |
| 183 | { |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 184 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 185 | auto additionalWire = getSignatureInfo().getCustomTlv(tlv::AdditionalDescription); |
| 186 | if (additionalWire) { |
| 187 | ndn::security::AdditionalDescription additional(*additionalWire); |
| 188 | m_signer = additional.get("signer"); |
| 189 | } |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 190 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 191 | auto profileWire = getSignatureInfo().getCustomTlv(tlv::Profile); |
| 192 | if (profileWire) { |
| 193 | m_profile = Profile(*profileWire); |
| 194 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 195 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 196 | auto endorseExtensionBlock = getSignatureInfo().getCustomTlv(tlv::EndorseExtension); |
| 197 | if (endorseExtensionBlock) { |
| 198 | EndorseExtension endorseExtension(*endorseExtensionBlock); |
| 199 | endorseExtension >> m_endorseList; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 200 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 201 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 202 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 203 | } // namespace chronochat |