blob: b5c94ad5c3c5188b40f4f211bc0016c62460f003 [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 Yu17032f82014-03-25 15:48:23 -070015
Yingdi Yuc972c452013-10-11 11:02:09 -070016using namespace ndn;
Yingdi Yufa4ce792014-02-06 18:09:22 -080017
Yingdi Yufa4ce792014-02-06 18:09:22 -080018namespace chronos{
Yingdi Yuc972c452013-10-11 11:02:09 -070019
Yingdi Yufa4ce792014-02-06 18:09:22 -080020const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1");
21const OID EndorseCertificate::ENDORSE_EXT_OID("1.3.6.1.5.32.2.2");
Yingdi Yub4be64a2013-10-13 17:24:50 -070022
Yingdi Yu348f5ea2014-03-01 14:47:25 -080023const std::vector<std::string> EndorseCertificate::DEFAULT_ENDORSE_LIST = std::vector<std::string>();
24
Yingdi Yufa4ce792014-02-06 18:09:22 -080025Chronos::EndorseExtensionMsg&
Yingdi Yu17032f82014-03-25 15:48:23 -070026operator << (Chronos::EndorseExtensionMsg& endorseExtension, const std::vector<std::string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080027{
Yingdi Yu17032f82014-03-25 15:48:23 -070028 std::vector<std::string>::const_iterator it = endorseList.begin();
Yingdi Yu76dd8002013-12-24 11:16:32 +080029 for(; it != endorseList.end(); it++)
30 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yub4be64a2013-10-13 17:24:50 -070031
Yingdi Yufa4ce792014-02-06 18:09:22 -080032 return endorseExtension;
33}
34
35Chronos::EndorseExtensionMsg&
Yingdi Yu17032f82014-03-25 15:48:23 -070036operator >> (Chronos::EndorseExtensionMsg& endorseExtension, std::vector<std::string>& endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080037{
38 for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
39 endorseList.push_back(endorseExtension.endorseentry(i).name());
40
41 return endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070042}
43
Yingdi Yuc972c452013-10-11 11:02:09 -070044EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yufa4ce792014-02-06 18:09:22 -080045 const Profile& profile,
Yingdi Yu17032f82014-03-25 15:48:23 -070046 const std::vector<std::string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070047 : Certificate()
Yingdi Yufa4ce792014-02-06 18:09:22 -080048 , m_profile(profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070049 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070050{
Yingdi Yufa4ce792014-02-06 18:09:22 -080051 m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName());
52 m_signer = m_keyName;
53
Yingdi Yub4be64a2013-10-13 17:24:50 -070054 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080055 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070056 setName(dataName);
57
Yingdi Yub2e747d2013-11-05 23:06:43 -080058 setNotBefore(kskCertificate.getNotBefore());
59 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +080060 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu92e8e482013-10-17 21:13:03 -070061 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080062
63 OBufferStream profileStream;
64 m_profile.encode(profileStream);
65 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
66
67 OBufferStream endorseStream;
68 Chronos::EndorseExtensionMsg endorseExtension;
69 endorseExtension << m_endorseList;
70 endorseExtension.SerializeToOstream(&endorseStream);
71 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070072
73 encode();
74}
75
76EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
77 const Name& signer,
Yingdi Yu17032f82014-03-25 15:48:23 -070078 const std::vector<std::string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070079 : Certificate()
80 , m_keyName(endorseCertificate.m_keyName)
81 , m_signer(signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -080082 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -070083 , m_endorseList(endorseList)
Yingdi Yufa4ce792014-02-06 18:09:22 -080084{
Yingdi Yub4be64a2013-10-13 17:24:50 -070085 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -080086 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -070087 setName(dataName);
88
Yingdi Yub2e747d2013-11-05 23:06:43 -080089 setNotBefore(endorseCertificate.getNotBefore());
90 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +080091 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070092 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yufa4ce792014-02-06 18:09:22 -080093
94 OBufferStream profileStream;
95 m_profile.encode(profileStream);
96 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
97
98 OBufferStream endorseStream;
99 Chronos::EndorseExtensionMsg endorseExtension;
100 endorseExtension << m_endorseList;
101 endorseExtension.SerializeToOstream(&endorseStream);
102 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700103
104 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700105}
106
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800107EndorseCertificate::EndorseCertificate(const Name& keyName,
108 const PublicKey& key,
Yingdi Yua7876722014-03-25 14:46:55 -0700109 const time::system_clock::TimePoint& notBefore,
110 const time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800111 const Name& signer,
112 const Profile& profile,
Yingdi Yu17032f82014-03-25 15:48:23 -0700113 const std::vector<std::string>& endorseList)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114 : Certificate()
115 , m_keyName(keyName)
116 , m_signer(signer)
117 , m_profile(profile)
118 , m_endorseList(endorseList)
119{
120 Name dataName = m_keyName;
121 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
122 setName(dataName);
123
124 setNotBefore(notBefore);
125 setNotAfter(notAfter);
126 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
127 setPublicKeyInfo(key);
128
129 OBufferStream profileStream;
130 m_profile.encode(profileStream);
131 addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf()));
132
133 OBufferStream endorseStream;
134 Chronos::EndorseExtensionMsg endorseExtension;
135 endorseExtension << m_endorseList;
136 endorseExtension.SerializeToOstream(&endorseStream);
137 addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf()));
138
139 encode();
140}
141
Yingdi Yuc972c452013-10-11 11:02:09 -0700142EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
143 : Certificate(endorseCertificate)
144 , m_keyName(endorseCertificate.m_keyName)
145 , m_signer(endorseCertificate.m_signer)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800146 , m_profile(endorseCertificate.m_profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700147 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700148{}
149
150EndorseCertificate::EndorseCertificate(const Data& data)
151 : Certificate(data)
152{
153 const Name& dataName = data.getName();
Yingdi Yuc972c452013-10-11 11:02:09 -0700154
Yingdi Yufa4ce792014-02-06 18:09:22 -0800155 if(dataName.size() < 3 || dataName.get(-3).toEscapedString() != "PROFILE-CERT")
Yingdi Yub6fb0302014-01-21 11:05:11 -0800156 throw Error("No PROFILE-CERT component in data name!");
157
158 m_keyName = dataName.getPrefix(-3);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800159 m_signer.wireDecode(dataName.get(-2).blockFromValue());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700160
Yingdi Yu76dd8002013-12-24 11:16:32 +0800161 ExtensionList::iterator it = extensionList_.begin();
162 for(; it != extensionList_.end(); it++)
Yingdi Yuc972c452013-10-11 11:02:09 -0700163 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800164 if(PROFILE_EXT_OID == it->getOid())
Yingdi Yuc972c452013-10-11 11:02:09 -0700165 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800166 boost::iostreams::stream<boost::iostreams::array_source> is
167 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
168 m_profile.decode(is);
Yingdi Yuc972c452013-10-11 11:02:09 -0700169 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800170 if(ENDORSE_EXT_OID == it->getOid())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700171 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800172 Chronos::EndorseExtensionMsg endorseExtension;
173
174 boost::iostreams::stream<boost::iostreams::array_source> is
175 (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size());
176 endorseExtension.ParseFromIstream(&is);
177
178 endorseExtension >> m_endorseList;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700179 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700180 }
181}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800182
183}//chronos