blob: faa0972237bee968b388e7c936b4f3be50d85073 [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
11#include "endorse-certificate.h"
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 Yuc972c452013-10-11 11:02:09 -070014
Yingdi Yuc972c452013-10-11 11:02:09 -070015using namespace ndn;
Yingdi Yufa4ce792014-02-06 18:09:22 -080016
Yingdi Yufa4ce792014-02-06 18:09:22 -080017namespace chronos{
Yingdi Yuc972c452013-10-11 11:02:09 -070018
Yingdi Yufa4ce792014-02-06 18:09:22 -080019const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1");
20const OID EndorseCertificate::ENDORSE_EXT_OID("1.3.6.1.5.32.2.2");
Yingdi Yub4be64a2013-10-13 17:24:50 -070021
Yingdi Yu348f5ea2014-03-01 14:47:25 -080022const std::vector<std::string> EndorseCertificate::DEFAULT_ENDORSE_LIST = std::vector<std::string>();
23
Yingdi Yufa4ce792014-02-06 18:09:22 -080024Chronos::EndorseExtensionMsg&
Yingdi Yu17032f82014-03-25 15:48:23 -070025operator << (Chronos::EndorseExtensionMsg& endorseExtension, const std::vector<std::string>& endorseList)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070026{
Yingdi Yu17032f82014-03-25 15:48:23 -070027 std::vector<std::string>::const_iterator it = endorseList.begin();
Yingdi Yu76dd8002013-12-24 11:16:32 +080028 for(; it != endorseList.end(); it++)
29 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yub4be64a2013-10-13 17:24:50 -070030
Yingdi Yufa4ce792014-02-06 18:09:22 -080031 return endorseExtension;
32}
33
34Chronos::EndorseExtensionMsg&
Yingdi Yu17032f82014-03-25 15:48:23 -070035operator >> (Chronos::EndorseExtensionMsg& endorseExtension, std::vector<std::string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080036{
37 for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
38 endorseList.push_back(endorseExtension.endorseentry(i).name());
39
40 return endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070041}
42
Yingdi Yuc972c452013-10-11 11:02:09 -070043EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yufa4ce792014-02-06 18:09:22 -080044 const Profile& profile,
Yingdi Yu17032f82014-03-25 15:48:23 -070045 const std::vector<std::string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070046 : Certificate()
Yingdi Yufa4ce792014-02-06 18:09:22 -080047 , m_profile(profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070048 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070049{
Yingdi Yufa4ce792014-02-06 18:09:22 -080050 m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName());
51 m_signer = m_keyName;
52
Yingdi Yub4be64a2013-10-13 17:24:50 -070053 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080054 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070055 setName(dataName);
56
Yingdi Yub2e747d2013-11-05 23:06:43 -080057 setNotBefore(kskCertificate.getNotBefore());
58 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +080059 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070060 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080061
62 OBufferStream profileStream;
63 m_profile.encode(profileStream);
64 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
65
66 OBufferStream endorseStream;
67 Chronos::EndorseExtensionMsg endorseExtension;
68 endorseExtension << m_endorseList;
69 endorseExtension.SerializeToOstream(&endorseStream);
70 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yufa0b6a02014-04-30 14:26:42 -070071
Yingdi Yub4be64a2013-10-13 17:24:50 -070072 encode();
73}
74
75EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
76 const Name& signer,
Yingdi Yu17032f82014-03-25 15:48:23 -070077 const std::vector<std::string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070078 : Certificate()
79 , m_keyName(endorseCertificate.m_keyName)
80 , m_signer(signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -080081 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070082 , m_endorseList(endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080083{
Yingdi Yub4be64a2013-10-13 17:24:50 -070084 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080085 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070086 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070087
Yingdi Yub2e747d2013-11-05 23:06:43 -080088 setNotBefore(endorseCertificate.getNotBefore());
89 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +080090 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070091 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080092
93 OBufferStream profileStream;
94 m_profile.encode(profileStream);
95 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
96
97 OBufferStream endorseStream;
98 Chronos::EndorseExtensionMsg endorseExtension;
99 endorseExtension << m_endorseList;
100 endorseExtension.SerializeToOstream(&endorseStream);
101 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102
103 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700104}
105
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800106EndorseCertificate::EndorseCertificate(const Name& keyName,
107 const PublicKey& key,
Yingdi Yua7876722014-03-25 14:46:55 -0700108 const time::system_clock::TimePoint& notBefore,
109 const time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800110 const Name& signer,
111 const Profile& profile,
Yingdi Yu17032f82014-03-25 15:48:23 -0700112 const std::vector<std::string>& endorseList)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800113 : Certificate()
114 , m_keyName(keyName)
115 , m_signer(signer)
116 , m_profile(profile)
117 , m_endorseList(endorseList)
118{
119 Name dataName = m_keyName;
120 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
121 setName(dataName);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700122
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800123 setNotBefore(notBefore);
124 setNotAfter(notAfter);
125 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
126 setPublicKeyInfo(key);
127
128 OBufferStream profileStream;
129 m_profile.encode(profileStream);
130 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
131
132 OBufferStream endorseStream;
133 Chronos::EndorseExtensionMsg endorseExtension;
134 endorseExtension << m_endorseList;
135 endorseExtension.SerializeToOstream(&endorseStream);
136 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
137
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700138 encode();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800139}
140
Yingdi Yuc972c452013-10-11 11:02:09 -0700141EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
142 : Certificate(endorseCertificate)
143 , m_keyName(endorseCertificate.m_keyName)
144 , m_signer(endorseCertificate.m_signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800145 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700146 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700147{}
148
149EndorseCertificate::EndorseCertificate(const Data& data)
150 : Certificate(data)
151{
152 const Name& dataName = data.getName();
Yingdi Yuc972c452013-10-11 11:02:09 -0700153
Yingdi Yufa4ce792014-02-06 18:09:22 -0800154 if(dataName.size() < 3 || dataName.get(-3).toEscapedString() != "PROFILE-CERT")
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700155 throw Error("No PROFILE-CERT component in data name!");
Yingdi Yub6fb0302014-01-21 11:05:11 -0800156
157 m_keyName = dataName.getPrefix(-3);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800158 m_signer.wireDecode(dataName.get(-2).blockFromValue());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700159
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700160 ExtensionList::iterator it = m_extensionList.begin();
161 for(; it != m_extensionList.end(); it++)
Yingdi Yuc972c452013-10-11 11:02:09 -0700162 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800163 if(PROFILE_EXT_OID == it->getOid())
Yingdi Yuc972c452013-10-11 11:02:09 -0700164 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700165 boost::iostreams::stream<boost::iostreams::array_source> is
Yingdi Yufa4ce792014-02-06 18:09:22 -0800166 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
167 m_profile.decode(is);
Yingdi Yuc972c452013-10-11 11:02:09 -0700168 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800169 if(ENDORSE_EXT_OID == it->getOid())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700170 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800171 Chronos::EndorseExtensionMsg endorseExtension;
172
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700173 boost::iostreams::stream<boost::iostreams::array_source> is
174 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800175 endorseExtension.ParseFromIstream(&is);
176
177 endorseExtension >> m_endorseList;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700178 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700179 }
180}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800181
182}//chronos