blob: ae07796c896280414167229b5a5e1747b2bd2401 [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"
13#include <boost/date_time/posix_time/posix_time.hpp>
14#include <boost/iostreams/stream.hpp>
Yingdi Yu92e8e482013-10-17 21:13:03 -070015#include "logging.h"
Yingdi Yuc972c452013-10-11 11:02:09 -070016
17using namespace std;
18using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080019using namespace ndn::ptr_lib;
20using namespace boost::posix_time;
Yingdi Yuc972c452013-10-11 11:02:09 -070021
Yingdi Yu92e8e482013-10-17 21:13:03 -070022INIT_LOGGER("EndorseCertificate");
23
Yingdi Yub4be64a2013-10-13 17:24:50 -070024ProfileExtension::ProfileExtension(const ProfileData & profileData)
Yingdi Yuf8f572d2014-01-13 11:19:47 -080025 : CertificateExtension("1.3.6.1.5.32.2.1", true, Buffer(profileData.wireEncode().wire(), profileData.wireEncode().size()))
Yingdi Yuc972c452013-10-11 11:02:09 -070026{}
27
Yingdi Yub4be64a2013-10-13 17:24:50 -070028ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080029 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_)
Yingdi Yub4be64a2013-10-13 17:24:50 -070030{}
31
32ProfileExtension::ProfileExtension(const CertificateExtension& extension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080033 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yub4be64a2013-10-13 17:24:50 -070034{
Yingdi Yu76dd8002013-12-24 11:16:32 +080035 if(extensionId_ != OID("1.3.6.1.5.32.2.1"))
Yingdi Yuf8f572d2014-01-13 11:19:47 -080036 throw Error("Wrong ProfileExtension Number!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070037}
38
Yingdi Yu76dd8002013-12-24 11:16:32 +080039shared_ptr<ProfileData>
Yingdi Yub4be64a2013-10-13 17:24:50 -070040ProfileExtension::getProfileData()
41{
Yingdi Yu76dd8002013-12-24 11:16:32 +080042 Data data;
Yingdi Yuf8f572d2014-01-13 11:19:47 -080043 data.wireDecode(Block(extensionValue_.buf(), extensionValue_.size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +080044 return make_shared<ProfileData>(data);
Yingdi Yub4be64a2013-10-13 17:24:50 -070045}
46
Yingdi Yu76dd8002013-12-24 11:16:32 +080047EndorseExtension::EndorseExtension(const vector<string>& endorseList)
48 : CertificateExtension("1.3.6.1.5.32.2.2", true, EndorseExtension::encodeEndorseList(endorseList))
Yingdi Yub4be64a2013-10-13 17:24:50 -070049{}
50
51EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080052 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_)
Yingdi Yub4be64a2013-10-13 17:24:50 -070053{}
54
55EndorseExtension::EndorseExtension(const CertificateExtension& extension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080056 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yub4be64a2013-10-13 17:24:50 -070057{
Yingdi Yu76dd8002013-12-24 11:16:32 +080058 if(extensionId_ != OID("1.3.6.1.5.32.2.2"))
Yingdi Yuf8f572d2014-01-13 11:19:47 -080059 throw Error("Wrong EndorseExtension Number!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070060}
61
62vector<string>
Yingdi Yu76dd8002013-12-24 11:16:32 +080063EndorseExtension::getEndorseList()
Yingdi Yub4be64a2013-10-13 17:24:50 -070064{
Yingdi Yu76dd8002013-12-24 11:16:32 +080065 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070066
67 boost::iostreams::stream
Yingdi Yu76dd8002013-12-24 11:16:32 +080068 <boost::iostreams::array_source> is ((const char*)extensionValue_.buf(), extensionValue_.size());
69
70 endorseExtension.ParseFromIstream(&is);
71
72 vector<string> endorseList;
73
74 for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
75 endorseList.push_back(endorseExtension.endorseentry(i).name());
Yingdi Yub4be64a2013-10-13 17:24:50 -070076
Yingdi Yu76dd8002013-12-24 11:16:32 +080077 return endorseList;
Yingdi Yub4be64a2013-10-13 17:24:50 -070078}
79
Yingdi Yuf8f572d2014-01-13 11:19:47 -080080Buffer
Yingdi Yu76dd8002013-12-24 11:16:32 +080081EndorseExtension::encodeEndorseList(const vector<string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070082{
Yingdi Yu76dd8002013-12-24 11:16:32 +080083 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070084
Yingdi Yu76dd8002013-12-24 11:16:32 +080085 vector<string>::const_iterator it = endorseList.begin();
86 for(; it != endorseList.end(); it++)
87 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yub4be64a2013-10-13 17:24:50 -070088
Yingdi Yu76dd8002013-12-24 11:16:32 +080089 string encoded;
90 endorseExtension.SerializeToString(&encoded);
91
Yingdi Yuf8f572d2014-01-13 11:19:47 -080092 return Buffer(encoded.c_str(), encoded.size());
Yingdi Yub4be64a2013-10-13 17:24:50 -070093}
94
Yingdi Yuc972c452013-10-11 11:02:09 -070095EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu76dd8002013-12-24 11:16:32 +080096 const ProfileData& profileData,
Yingdi Yub4be64a2013-10-13 17:24:50 -070097 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070098 : Certificate()
99 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700100 , m_signer(kskCertificate.getPublicKeyName())
101 , m_profileData(profileData)
102 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700103{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800104 time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
105 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
106
Yingdi Yub4be64a2013-10-13 17:24:50 -0700107 Name dataName = m_keyName;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800108 dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700109 setName(dataName);
110
Yingdi Yub2e747d2013-11-05 23:06:43 -0800111 setNotBefore(kskCertificate.getNotBefore());
112 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800113 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700114 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800115 addExtension(ProfileExtension(m_profileData));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700116 addExtension(EndorseExtension(m_endorseList));
117
118 encode();
119}
120
121EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
122 const Name& signer,
Yingdi Yub4be64a2013-10-13 17:24:50 -0700123 const vector<string>& endorseList)
124 : Certificate()
125 , m_keyName(endorseCertificate.m_keyName)
126 , m_signer(signer)
127 , m_profileData(endorseCertificate.m_profileData)
128 , m_endorseList(endorseList)
129{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800130 time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
131 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
132
Yingdi Yub4be64a2013-10-13 17:24:50 -0700133 Name dataName = m_keyName;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800134 dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700135 setName(dataName);
136
Yingdi Yub2e747d2013-11-05 23:06:43 -0800137 setNotBefore(endorseCertificate.getNotBefore());
138 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800139 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700140 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800141 addExtension(ProfileExtension(m_profileData));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700142 addExtension(EndorseExtension(m_endorseList));
143
144 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700145}
146
147EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
148 : Certificate(endorseCertificate)
149 , m_keyName(endorseCertificate.m_keyName)
150 , m_signer(endorseCertificate.m_signer)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700151 , m_profileData(endorseCertificate.m_profileData)
152 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700153{}
154
155EndorseCertificate::EndorseCertificate(const Data& data)
156 : Certificate(data)
157{
158 const Name& dataName = data.getName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800159 Name::Component certFlag(Name::fromEscapedString("PROFILE-CERT"));
Yingdi Yuc972c452013-10-11 11:02:09 -0700160 int profileIndex = -1;
161 for(int i = 0; i < dataName.size(); i++)
162 {
163 if(0 == dataName.get(i).compare(certFlag))
164 {
165 profileIndex = i;
166 break;
167 }
168 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700169 if(profileIndex < 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800170 throw Error("No PROFILE-CERT component in data name!");
Yingdi Yuc972c452013-10-11 11:02:09 -0700171
172 m_keyName = dataName.getSubName(0, profileIndex);
173 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
174
Yingdi Yub4be64a2013-10-13 17:24:50 -0700175 OID profileExtensionOID("1.3.6.1.5.32.2.1");
176 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
177
Yingdi Yu76dd8002013-12-24 11:16:32 +0800178 ExtensionList::iterator it = extensionList_.begin();
179 for(; it != extensionList_.end(); it++)
Yingdi Yuc972c452013-10-11 11:02:09 -0700180 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800181 if(profileExtensionOID == it->getOid())
Yingdi Yuc972c452013-10-11 11:02:09 -0700182 {
Yingdi Yub4be64a2013-10-13 17:24:50 -0700183 ProfileExtension profileExtension(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800184 m_profileData = *profileExtension.getProfileData();
Yingdi Yuc972c452013-10-11 11:02:09 -0700185 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800186 if(endorseExtensionOID == it->getOid())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700187 {
188 EndorseExtension endorseExtension(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800189 m_endorseList = endorseExtension.getEndorseList();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700190 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700191 }
192}