blob: cbeb18cbecedc36da7863e8e1888925b0762d46f [file] [log] [blame]
Yingdi Yuc972c452013-10-11 11:02:09 -07001/* -*- 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>
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"
Yingdi Yu76dd8002013-12-24 11:16:32 +080013#include <boost/iostreams/stream.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014#include <ndn-cxx/encoding/buffer-stream.hpp>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080015#include "endorse-extension.hpp"
16#include <list>
Yingdi Yuc972c452013-10-11 11:02:09 -070017
Yingdi Yueb692ac2015-02-10 18:46:18 -080018namespace chronochat {
Yingdi Yufa4ce792014-02-06 18:09:22 -080019
Yingdi Yu0b0a7362014-08-05 16:31:30 -070020using std::vector;
21using std::string;
22
23using ndn::PublicKey;
24using ndn::IdentityCertificate;
25using ndn::CertificateSubjectDescription;
26using ndn::CertificateExtension;
27using ndn::OID;
28using ndn::OBufferStream;
Yingdi Yuc972c452013-10-11 11:02:09 -070029
Yingdi Yufa4ce792014-02-06 18:09:22 -080030const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1");
31const OID EndorseCertificate::ENDORSE_EXT_OID("1.3.6.1.5.32.2.2");
Yingdi Yub4be64a2013-10-13 17:24:50 -070032
Yingdi Yu0b0a7362014-08-05 16:31:30 -070033const vector<string> EndorseCertificate::DEFAULT_ENDORSE_LIST;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080034
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080035EndorseExtension&
36operator<<(EndorseExtension& endorseExtension, const vector<string>& endorseList)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070037{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080038 for (const auto& entry : endorseList)
39 endorseExtension.addEntry(entry);
Yingdi Yub4be64a2013-10-13 17:24:50 -070040
Yingdi Yufa4ce792014-02-06 18:09:22 -080041 return endorseExtension;
42}
43
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080044EndorseExtension&
45operator>>(EndorseExtension& endorseExtension, vector<string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080046{
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080047 const std::list<string>& endorseEntries = endorseExtension.getEntries();
48 for (const auto& entry: endorseEntries)
49 endorseList.push_back(entry);
Yingdi Yufa4ce792014-02-06 18:09:22 -080050
51 return endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070052}
53
Yingdi Yuc972c452013-10-11 11:02:09 -070054EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yufa4ce792014-02-06 18:09:22 -080055 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070056 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070057 : Certificate()
Yingdi Yufa4ce792014-02-06 18:09:22 -080058 , m_profile(profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070059 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070060{
Yingdi Yufa4ce792014-02-06 18:09:22 -080061 m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName());
62 m_signer = m_keyName;
63
Yingdi Yub4be64a2013-10-13 17:24:50 -070064 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080065 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070066 setName(dataName);
67
Yingdi Yub2e747d2013-11-05 23:06:43 -080068 setNotBefore(kskCertificate.getNotBefore());
69 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu0b0a7362014-08-05 16:31:30 -070070 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070071 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080072
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080073 Block profileWire = m_profile.wireEncode();
74 addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(),
75 profileWire.size())));
Yingdi Yufa4ce792014-02-06 18:09:22 -080076
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080077 EndorseExtension endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -080078 endorseExtension << m_endorseList;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080079 Block endorseWire = endorseExtension.wireEncode();
80 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(),
81 endorseWire.size())));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070082
Yingdi Yub4be64a2013-10-13 17:24:50 -070083 encode();
84}
85
86EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
87 const Name& signer,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070088 const vector<string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070089 : Certificate()
90 , m_keyName(endorseCertificate.m_keyName)
91 , m_signer(signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -080092 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070093 , m_endorseList(endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080094{
Yingdi Yub4be64a2013-10-13 17:24:50 -070095 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080096 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070097 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070098
Yingdi Yub2e747d2013-11-05 23:06:43 -080099 setNotBefore(endorseCertificate.getNotBefore());
100 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700101 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800103
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800104 Block profileWire = m_profile.wireEncode();
105 addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(),
106 profileWire.size())));
Yingdi Yufa4ce792014-02-06 18:09:22 -0800107
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800108 EndorseExtension endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800109 endorseExtension << m_endorseList;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800110 Block endorseWire = endorseExtension.wireEncode();
111 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(),
112 endorseWire.size())));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700113
114 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700115}
116
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800117EndorseCertificate::EndorseCertificate(const Name& keyName,
118 const PublicKey& key,
Yingdi Yua7876722014-03-25 14:46:55 -0700119 const time::system_clock::TimePoint& notBefore,
120 const time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800121 const Name& signer,
122 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700123 const vector<string>& endorseList)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800124 : Certificate()
125 , m_keyName(keyName)
126 , m_signer(signer)
127 , m_profile(profile)
128 , m_endorseList(endorseList)
129{
130 Name dataName = m_keyName;
131 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
132 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700133
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 setNotBefore(notBefore);
135 setNotAfter(notAfter);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700136 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800137 setPublicKeyInfo(key);
138
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800139 Block profileWire = m_profile.wireEncode();
140 addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(),
141 profileWire.size())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800143 EndorseExtension endorseExtension;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800144 endorseExtension << m_endorseList;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800145 Block endorseWire = endorseExtension.wireEncode();
146 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(),
147 endorseWire.size())));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800148
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700149 encode();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800150}
151
Yingdi Yuc972c452013-10-11 11:02:09 -0700152EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
153 : Certificate(endorseCertificate)
154 , m_keyName(endorseCertificate.m_keyName)
155 , m_signer(endorseCertificate.m_signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800156 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700157 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700158{
159}
Yingdi Yuc972c452013-10-11 11:02:09 -0700160
161EndorseCertificate::EndorseCertificate(const Data& data)
162 : Certificate(data)
163{
164 const Name& dataName = data.getName();
Yingdi Yuc972c452013-10-11 11:02:09 -0700165
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700166 if(dataName.size() < 3 || dataName.get(-3).toUri() != "PROFILE-CERT")
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700167 throw Error("No PROFILE-CERT component in data name!");
Yingdi Yub6fb0302014-01-21 11:05:11 -0800168
169 m_keyName = dataName.getPrefix(-3);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800170 m_signer.wireDecode(dataName.get(-2).blockFromValue());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700171
Yingdi Yufa4ce792014-02-06 18:09:22 -0800172
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800173 for (const auto& entry : m_extensionList) {
174 if (PROFILE_EXT_OID == entry.getOid()) {
175 m_profile.wireDecode(Block(entry.getValue().buf(), entry.getValue().size()));
Yingdi Yuc972c452013-10-11 11:02:09 -0700176 }
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800177 if (ENDORSE_EXT_OID == entry.getOid()) {
178 EndorseExtension endorseExtension;
179 endorseExtension.wireDecode(Block(entry.getValue().buf(), entry.getValue().size()));
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700180
181 endorseExtension >> m_endorseList;
182 }
183 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700184}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800185
Yingdi Yueb692ac2015-02-10 18:46:18 -0800186} // namespace chronochat