blob: 42b1cb1d6e1ad9614168925f3c19d70ad0db5dd8 [file] [log] [blame]
Yingdi Yuc972c452013-10-11 11:02:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yuc972c452013-10-11 11:02:09 -07004 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yuc972c452013-10-11 11:02:09 -070010 */
11
Yingdi Yu0b0a7362014-08-05 16:31:30 -070012#include "endorse-certificate.hpp"
Varun Patila24bd3e2020-11-24 10:08:33 +053013#include "endorse-extension.hpp"
14
Yingdi Yu76dd8002013-12-24 11:16:32 +080015#include <boost/iostreams/stream.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070016#include <ndn-cxx/encoding/buffer-stream.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053017#include <ndn-cxx/security/additional-description.hpp>
18#include <ndn-cxx/security/validity-period.hpp>
Yingdi Yuc972c452013-10-11 11:02:09 -070019
Yingdi Yueb692ac2015-02-10 18:46:18 -080020namespace chronochat {
Yingdi Yufa4ce792014-02-06 18:09:22 -080021
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022using std::vector;
23using std::string;
24
Varun Patil3d850902020-11-23 12:19:14 +053025using ndn::security::Certificate;
Yingdi Yu0b0a7362014-08-05 16:31:30 -070026using ndn::OBufferStream;
Yingdi Yuc972c452013-10-11 11:02:09 -070027
Yingdi Yu0b0a7362014-08-05 16:31:30 -070028const vector<string> EndorseCertificate::DEFAULT_ENDORSE_LIST;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080030EndorseExtension&
31operator<<(EndorseExtension& endorseExtension, const vector<string>& endorseList)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070032{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080033 for (const auto& entry : endorseList)
34 endorseExtension.addEntry(entry);
Yingdi Yub4be64a2013-10-13 17:24:50 -070035
Yingdi Yufa4ce792014-02-06 18:09:22 -080036 return endorseExtension;
37}
38
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080039EndorseExtension&
40operator>>(EndorseExtension& endorseExtension, vector<string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080041{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080042 const std::list<string>& endorseEntries = endorseExtension.getEntries();
43 for (const auto& entry: endorseEntries)
44 endorseList.push_back(entry);
Yingdi Yufa4ce792014-02-06 18:09:22 -080045
46 return endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070047}
48
Varun Patil3d850902020-11-23 12:19:14 +053049EndorseCertificate::EndorseCertificate(const Certificate& kskCertificate,
Yingdi Yufa4ce792014-02-06 18:09:22 -080050 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070051 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070052 : Certificate()
Yingdi Yufa4ce792014-02-06 18:09:22 -080053 , m_profile(profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070054 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070055{
Varun Patil3d850902020-11-23 12:19:14 +053056 setName(kskCertificate.getKeyName().getPrefix(-2)
57 .append("PROFILE-CERT")
58 .append("KEY")
59 .append(kskCertificate.getKeyId())
60 .append("self")
61 .appendTimestamp());
Yingdi Yufa4ce792014-02-06 18:09:22 -080062
Varun Patil3d850902020-11-23 12:19:14 +053063 m_signer = kskCertificate.getKeyName();
Yingdi Yub4be64a2013-10-13 17:24:50 -070064
Varun Patil3d850902020-11-23 12:19:14 +053065 setMetaInfo(kskCertificate.getMetaInfo());
66 setContent(kskCertificate.getPublicKey().data(), kskCertificate.getPublicKey().size());
Yingdi Yufa4ce792014-02-06 18:09:22 -080067
Varun Patil3d850902020-11-23 12:19:14 +053068 ndn::security::AdditionalDescription description;
69 description.set("2.5.4.41", getKeyName().toUri());
70 description.set("signer", m_signer.toUri());
Yingdi Yufa4ce792014-02-06 18:09:22 -080071
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080072 EndorseExtension endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -080073 endorseExtension << m_endorseList;
Yingdi Yufa0b6a02014-04-30 14:26:42 -070074
Varun Patil3d850902020-11-23 12:19:14 +053075 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 Patila24bd3e2020-11-24 10:08:33 +053084 } catch (const tlv::Error&) {
Varun Patil3d850902020-11-23 12:19:14 +053085 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(
86 time::system_clock::now(), time::system_clock::now() + time::days(3650)));
87 }
88
89 setSignatureInfo(signatureInfo);
Yingdi Yub4be64a2013-10-13 17:24:50 -070090}
91
92EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
93 const Name& signer,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070094 const vector<string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070095 : Certificate()
Yingdi Yub4be64a2013-10-13 17:24:50 -070096 , m_signer(signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -080097 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070098 , m_endorseList(endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080099{
Varun Patil3d850902020-11-23 12:19:14 +0530100 setName(endorseCertificate.getName()
101 .getPrefix(-2)
102 .append(m_signer.wireEncode())
103 .appendVersion());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700104
Varun Patil3d850902020-11-23 12:19:14 +0530105 setMetaInfo(endorseCertificate.getMetaInfo());
106 setContent(endorseCertificate.getPublicKey().data(), endorseCertificate.getPublicKey().size());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800107
Varun Patil3d850902020-11-23 12:19:14 +0530108 ndn::security::AdditionalDescription description;
109 description.set("2.5.4.41", getKeyName().toUri());
110 description.set("signer", m_signer.toUri());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800111
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800112 EndorseExtension endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800113 endorseExtension << m_endorseList;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700114
Varun Patil3d850902020-11-23 12:19:14 +0530115 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 Patila24bd3e2020-11-24 10:08:33 +0530124 } catch (const tlv::Error&) {
Varun Patil3d850902020-11-23 12:19:14 +0530125 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(
126 time::system_clock::now(), time::system_clock::now() + time::days(3650)));
127 }
128
129 setSignatureInfo(signatureInfo);
Yingdi Yuc972c452013-10-11 11:02:09 -0700130}
131
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800132EndorseCertificate::EndorseCertificate(const Name& keyName,
Varun Patil3d850902020-11-23 12:19:14 +0530133 const ndn::Buffer& key,
Yingdi Yua7876722014-03-25 14:46:55 -0700134 const time::system_clock::TimePoint& notBefore,
135 const time::system_clock::TimePoint& notAfter,
Varun Patil3d850902020-11-23 12:19:14 +0530136 const Name::Component& signerKeyId,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800137 const Name& signer,
138 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700139 const vector<string>& endorseList)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800140 : Certificate()
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 , m_signer(signer)
142 , m_profile(profile)
143 , m_endorseList(endorseList)
144{
Varun Patil3d850902020-11-23 12:19:14 +0530145 setName(keyName.getPrefix(-2)
146 .append("PROFILE-CERT")
147 .append("KEY")
148 .append(signerKeyId)
149 .append(m_signer.wireEncode())
150 .appendVersion());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700151
Varun Patil3d850902020-11-23 12:19:14 +0530152 setContent(key.data(), key.size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800153
Varun Patil3d850902020-11-23 12:19:14 +0530154 ndn::security::AdditionalDescription description;
155 description.set("2.5.4.41", keyName.toUri());
156 description.set("signer", m_signer.toUri());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800157
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800158 EndorseExtension endorseExtension;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800159 endorseExtension << m_endorseList;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800160
Varun Patil3d850902020-11-23 12:19:14 +0530161 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 Yu348f5ea2014-03-01 14:47:25 -0800171}
172
Yingdi Yuc972c452013-10-11 11:02:09 -0700173EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
174 : Certificate(endorseCertificate)
Yingdi Yuc972c452013-10-11 11:02:09 -0700175 , m_signer(endorseCertificate.m_signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800176 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700177 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700178{
179}
Yingdi Yuc972c452013-10-11 11:02:09 -0700180
181EndorseCertificate::EndorseCertificate(const Data& data)
182 : Certificate(data)
183{
Yingdi Yuc972c452013-10-11 11:02:09 -0700184
Varun Patil3d850902020-11-23 12:19:14 +0530185 auto additionalWire = getSignatureInfo().getCustomTlv(tlv::AdditionalDescription);
186 if (additionalWire) {
187 ndn::security::AdditionalDescription additional(*additionalWire);
188 m_signer = additional.get("signer");
189 }
Yingdi Yub6fb0302014-01-21 11:05:11 -0800190
Varun Patil3d850902020-11-23 12:19:14 +0530191 auto profileWire = getSignatureInfo().getCustomTlv(tlv::Profile);
192 if (profileWire) {
193 m_profile = Profile(*profileWire);
194 }
Yingdi Yub4be64a2013-10-13 17:24:50 -0700195
Varun Patil3d850902020-11-23 12:19:14 +0530196 auto endorseExtensionBlock = getSignatureInfo().getCustomTlv(tlv::EndorseExtension);
197 if (endorseExtensionBlock) {
198 EndorseExtension endorseExtension(*endorseExtensionBlock);
199 endorseExtension >> m_endorseList;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700200 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700201}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800202
Yingdi Yueb692ac2015-02-10 18:46:18 -0800203} // namespace chronochat