blob: b72d57ed5a1db1892f4ce605db855329f249b666 [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>
9 */
10
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "endorse-certificate.hpp"
Yingdi Yu76dd8002013-12-24 11:16:32 +080012#include "endorse-extension.pb.h"
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>
Yingdi Yuc972c452013-10-11 11:02:09 -070015
Yingdi Yueb692ac2015-02-10 18:46:18 -080016namespace chronochat {
Yingdi Yufa4ce792014-02-06 18:09:22 -080017
Yingdi Yu0b0a7362014-08-05 16:31:30 -070018using std::vector;
19using std::string;
20
21using ndn::PublicKey;
22using ndn::IdentityCertificate;
23using ndn::CertificateSubjectDescription;
24using ndn::CertificateExtension;
25using ndn::OID;
26using ndn::OBufferStream;
Yingdi Yuc972c452013-10-11 11:02:09 -070027
Yingdi Yufa4ce792014-02-06 18:09:22 -080028const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1");
29const OID EndorseCertificate::ENDORSE_EXT_OID("1.3.6.1.5.32.2.2");
Yingdi Yub4be64a2013-10-13 17:24:50 -070030
Yingdi Yu0b0a7362014-08-05 16:31:30 -070031const vector<string> EndorseCertificate::DEFAULT_ENDORSE_LIST;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080032
Yingdi Yueb692ac2015-02-10 18:46:18 -080033chronochat::EndorseExtensionMsg&
34operator<<(chronochat::EndorseExtensionMsg& endorseExtension, const vector<string>& endorseList)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070035{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070036 for (vector<string>::const_iterator it = endorseList.begin(); it != endorseList.end(); it++)
Yingdi Yu76dd8002013-12-24 11:16:32 +080037 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yub4be64a2013-10-13 17:24:50 -070038
Yingdi Yufa4ce792014-02-06 18:09:22 -080039 return endorseExtension;
40}
41
Yingdi Yueb692ac2015-02-10 18:46:18 -080042chronochat::EndorseExtensionMsg&
43operator>>(chronochat::EndorseExtensionMsg& endorseExtension, vector<string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080044{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070045 for (int i = 0; i < endorseExtension.endorseentry_size(); i ++)
Yingdi Yufa4ce792014-02-06 18:09:22 -080046 endorseList.push_back(endorseExtension.endorseentry(i).name());
47
48 return endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070049}
50
Yingdi Yuc972c452013-10-11 11:02:09 -070051EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yufa4ce792014-02-06 18:09:22 -080052 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070053 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070054 : Certificate()
Yingdi Yufa4ce792014-02-06 18:09:22 -080055 , m_profile(profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070056 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070057{
Yingdi Yufa4ce792014-02-06 18:09:22 -080058 m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName());
59 m_signer = m_keyName;
60
Yingdi Yub4be64a2013-10-13 17:24:50 -070061 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080062 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070063 setName(dataName);
64
Yingdi Yub2e747d2013-11-05 23:06:43 -080065 setNotBefore(kskCertificate.getNotBefore());
66 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu0b0a7362014-08-05 16:31:30 -070067 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070068 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080069
70 OBufferStream profileStream;
71 m_profile.encode(profileStream);
72 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
73
74 OBufferStream endorseStream;
Yingdi Yueb692ac2015-02-10 18:46:18 -080075 chronochat::EndorseExtensionMsg endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -080076 endorseExtension << m_endorseList;
77 endorseExtension.SerializeToOstream(&endorseStream);
78 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070079
Yingdi Yub4be64a2013-10-13 17:24:50 -070080 encode();
81}
82
83EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
84 const Name& signer,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070085 const vector<string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070086 : Certificate()
87 , m_keyName(endorseCertificate.m_keyName)
88 , m_signer(signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -080089 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070090 , m_endorseList(endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080091{
Yingdi Yub4be64a2013-10-13 17:24:50 -070092 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080093 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070094 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070095
Yingdi Yub2e747d2013-11-05 23:06:43 -080096 setNotBefore(endorseCertificate.getNotBefore());
97 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu0b0a7362014-08-05 16:31:30 -070098 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070099 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800100
101 OBufferStream profileStream;
102 m_profile.encode(profileStream);
103 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
104
105 OBufferStream endorseStream;
Yingdi Yueb692ac2015-02-10 18:46:18 -0800106 chronochat::EndorseExtensionMsg endorseExtension;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800107 endorseExtension << m_endorseList;
108 endorseExtension.SerializeToOstream(&endorseStream);
109 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700110
111 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700112}
113
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114EndorseCertificate::EndorseCertificate(const Name& keyName,
115 const PublicKey& key,
Yingdi Yua7876722014-03-25 14:46:55 -0700116 const time::system_clock::TimePoint& notBefore,
117 const time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800118 const Name& signer,
119 const Profile& profile,
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700120 const vector<string>& endorseList)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800121 : Certificate()
122 , m_keyName(keyName)
123 , m_signer(signer)
124 , m_profile(profile)
125 , m_endorseList(endorseList)
126{
127 Name dataName = m_keyName;
128 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
129 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700130
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800131 setNotBefore(notBefore);
132 setNotAfter(notAfter);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700133 addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 setPublicKeyInfo(key);
135
136 OBufferStream profileStream;
137 m_profile.encode(profileStream);
138 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
139
140 OBufferStream endorseStream;
Yingdi Yueb692ac2015-02-10 18:46:18 -0800141 chronochat::EndorseExtensionMsg endorseExtension;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142 endorseExtension << m_endorseList;
143 endorseExtension.SerializeToOstream(&endorseStream);
144 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
145
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700146 encode();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800147}
148
Yingdi Yuc972c452013-10-11 11:02:09 -0700149EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
150 : Certificate(endorseCertificate)
151 , m_keyName(endorseCertificate.m_keyName)
152 , m_signer(endorseCertificate.m_signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800153 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700154 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700155{
156}
Yingdi Yuc972c452013-10-11 11:02:09 -0700157
158EndorseCertificate::EndorseCertificate(const Data& data)
159 : Certificate(data)
160{
161 const Name& dataName = data.getName();
Yingdi Yuc972c452013-10-11 11:02:09 -0700162
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700163 if(dataName.size() < 3 || dataName.get(-3).toUri() != "PROFILE-CERT")
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700164 throw Error("No PROFILE-CERT component in data name!");
Yingdi Yub6fb0302014-01-21 11:05:11 -0800165
166 m_keyName = dataName.getPrefix(-3);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800167 m_signer.wireDecode(dataName.get(-2).blockFromValue());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700168
Yingdi Yufa4ce792014-02-06 18:09:22 -0800169
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700170 for (ExtensionList::iterator it = m_extensionList.begin() ; it != m_extensionList.end(); it++) {
171 if (PROFILE_EXT_OID == it->getOid()) {
172 boost::iostreams::stream<boost::iostreams::array_source> is
173 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
174 m_profile.decode(is);
Yingdi Yuc972c452013-10-11 11:02:09 -0700175 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700176 if (ENDORSE_EXT_OID == it->getOid()) {
Yingdi Yueb692ac2015-02-10 18:46:18 -0800177 chronochat::EndorseExtensionMsg endorseExtension;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700178
179 boost::iostreams::stream<boost::iostreams::array_source> is
180 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
181 endorseExtension.ParseFromIstream(&is);
182
183 endorseExtension >> m_endorseList;
184 }
185 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700186}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800187
Yingdi Yueb692ac2015-02-10 18:46:18 -0800188} // namespace chronochat