blob: 8101eb39ea3fd48a6151703afe33d4631e522956 [file] [log] [blame]
Yingdi Yu3c2a9762013-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"
12#include "exception.h"
Yingdi Yu64206112013-12-24 11:16:32 +080013#include "endorse-extension.pb.h"
14#include <boost/date_time/posix_time/posix_time.hpp>
15#include <boost/iostreams/stream.hpp>
Yingdi Yu68aced92013-10-17 21:13:03 -070016#include "logging.h"
Yingdi Yu3c2a9762013-10-11 11:02:09 -070017
18using namespace std;
19using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080020using namespace ndn::ptr_lib;
21using namespace boost::posix_time;
Yingdi Yu3c2a9762013-10-11 11:02:09 -070022
Yingdi Yu68aced92013-10-17 21:13:03 -070023INIT_LOGGER("EndorseCertificate");
24
Yingdi Yu5ff62102013-10-13 17:24:50 -070025ProfileExtension::ProfileExtension(const ProfileData & profileData)
Yingdi Yu64206112013-12-24 11:16:32 +080026 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileData.wireEncode())
Yingdi Yu3c2a9762013-10-11 11:02:09 -070027{}
28
Yingdi Yu5ff62102013-10-13 17:24:50 -070029ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
Yingdi Yu64206112013-12-24 11:16:32 +080030 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_)
Yingdi Yu5ff62102013-10-13 17:24:50 -070031{}
32
33ProfileExtension::ProfileExtension(const CertificateExtension& extension)
Yingdi Yu64206112013-12-24 11:16:32 +080034 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yu5ff62102013-10-13 17:24:50 -070035{
Yingdi Yu64206112013-12-24 11:16:32 +080036 if(extensionId_ != OID("1.3.6.1.5.32.2.1"))
Yingdi Yu5ff62102013-10-13 17:24:50 -070037 throw LnException("Wrong ProfileExtension Number!");
38}
39
Yingdi Yu64206112013-12-24 11:16:32 +080040shared_ptr<ProfileData>
Yingdi Yu5ff62102013-10-13 17:24:50 -070041ProfileExtension::getProfileData()
42{
Yingdi Yu64206112013-12-24 11:16:32 +080043 Data data;
44 data.wireDecode(extensionValue_.buf(), extensionValue_.size());
45 return make_shared<ProfileData>(data);
Yingdi Yu5ff62102013-10-13 17:24:50 -070046}
47
Yingdi Yu64206112013-12-24 11:16:32 +080048EndorseExtension::EndorseExtension(const vector<string>& endorseList)
49 : CertificateExtension("1.3.6.1.5.32.2.2", true, EndorseExtension::encodeEndorseList(endorseList))
Yingdi Yu5ff62102013-10-13 17:24:50 -070050{}
51
52EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
Yingdi Yu64206112013-12-24 11:16:32 +080053 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_)
Yingdi Yu5ff62102013-10-13 17:24:50 -070054{}
55
56EndorseExtension::EndorseExtension(const CertificateExtension& extension)
Yingdi Yu64206112013-12-24 11:16:32 +080057 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yu5ff62102013-10-13 17:24:50 -070058{
Yingdi Yu64206112013-12-24 11:16:32 +080059 if(extensionId_ != OID("1.3.6.1.5.32.2.2"))
Yingdi Yu5ff62102013-10-13 17:24:50 -070060 throw LnException("Wrong EndorseExtension Number!");
61}
62
63vector<string>
Yingdi Yu64206112013-12-24 11:16:32 +080064EndorseExtension::getEndorseList()
Yingdi Yu5ff62102013-10-13 17:24:50 -070065{
Yingdi Yu64206112013-12-24 11:16:32 +080066 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yu5ff62102013-10-13 17:24:50 -070067
68 boost::iostreams::stream
Yingdi Yu64206112013-12-24 11:16:32 +080069 <boost::iostreams::array_source> is ((const char*)extensionValue_.buf(), extensionValue_.size());
70
71 endorseExtension.ParseFromIstream(&is);
72
73 vector<string> endorseList;
74
75 for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
76 endorseList.push_back(endorseExtension.endorseentry(i).name());
Yingdi Yu5ff62102013-10-13 17:24:50 -070077
Yingdi Yu64206112013-12-24 11:16:32 +080078 return endorseList;
Yingdi Yu5ff62102013-10-13 17:24:50 -070079}
80
Yingdi Yu64206112013-12-24 11:16:32 +080081Blob
82EndorseExtension::encodeEndorseList(const vector<string>& endorseList)
Yingdi Yu5ff62102013-10-13 17:24:50 -070083{
Yingdi Yu64206112013-12-24 11:16:32 +080084 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yu5ff62102013-10-13 17:24:50 -070085
Yingdi Yu64206112013-12-24 11:16:32 +080086 vector<string>::const_iterator it = endorseList.begin();
87 for(; it != endorseList.end(); it++)
88 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yu5ff62102013-10-13 17:24:50 -070089
Yingdi Yu64206112013-12-24 11:16:32 +080090 string encoded;
91 endorseExtension.SerializeToString(&encoded);
92
93 return Blob((const uint8_t*)encoded.c_str(), encoded.size());
Yingdi Yu5ff62102013-10-13 17:24:50 -070094}
95
Yingdi Yu3c2a9762013-10-11 11:02:09 -070096EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu64206112013-12-24 11:16:32 +080097 const ProfileData& profileData,
Yingdi Yu5ff62102013-10-13 17:24:50 -070098 const vector<string>& endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -070099 : Certificate()
100 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yu5ff62102013-10-13 17:24:50 -0700101 , m_signer(kskCertificate.getPublicKeyName())
102 , m_profileData(profileData)
103 , m_endorseList(endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700104{
Yingdi Yu64206112013-12-24 11:16:32 +0800105 time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
106 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
107
Yingdi Yu5ff62102013-10-13 17:24:50 -0700108 Name dataName = m_keyName;
Yingdi Yu64206112013-12-24 11:16:32 +0800109 dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700110 setName(dataName);
111
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800112 setNotBefore(kskCertificate.getNotBefore());
113 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu64206112013-12-24 11:16:32 +0800114 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu68aced92013-10-17 21:13:03 -0700115 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu64206112013-12-24 11:16:32 +0800116 addExtension(ProfileExtension(m_profileData));
Yingdi Yu5ff62102013-10-13 17:24:50 -0700117 addExtension(EndorseExtension(m_endorseList));
118
119 encode();
120}
121
122EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
123 const Name& signer,
Yingdi Yu5ff62102013-10-13 17:24:50 -0700124 const vector<string>& endorseList)
125 : Certificate()
126 , m_keyName(endorseCertificate.m_keyName)
127 , m_signer(signer)
128 , m_profileData(endorseCertificate.m_profileData)
129 , m_endorseList(endorseList)
130{
Yingdi Yu64206112013-12-24 11:16:32 +0800131 time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1));
132 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
133
Yingdi Yu5ff62102013-10-13 17:24:50 -0700134 Name dataName = m_keyName;
Yingdi Yu64206112013-12-24 11:16:32 +0800135 dataName.append("PROFILE-CERT").append(m_signer).appendVersion(version);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700136 setName(dataName);
137
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800138 setNotBefore(endorseCertificate.getNotBefore());
139 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu64206112013-12-24 11:16:32 +0800140 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu5ff62102013-10-13 17:24:50 -0700141 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yu64206112013-12-24 11:16:32 +0800142 addExtension(ProfileExtension(m_profileData));
Yingdi Yu5ff62102013-10-13 17:24:50 -0700143 addExtension(EndorseExtension(m_endorseList));
144
145 encode();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700146}
147
148EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
149 : Certificate(endorseCertificate)
150 , m_keyName(endorseCertificate.m_keyName)
151 , m_signer(endorseCertificate.m_signer)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700152 , m_profileData(endorseCertificate.m_profileData)
153 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700154{}
155
156EndorseCertificate::EndorseCertificate(const Data& data)
157 : Certificate(data)
158{
159 const Name& dataName = data.getName();
Yingdi Yu64206112013-12-24 11:16:32 +0800160 Name::Component certFlag(Name::fromEscapedString("PROFILE-CERT"));
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700161 int profileIndex = -1;
162 for(int i = 0; i < dataName.size(); i++)
163 {
164 if(0 == dataName.get(i).compare(certFlag))
165 {
166 profileIndex = i;
167 break;
168 }
169 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700170 if(profileIndex < 0)
171 throw LnException("No PROFILE-CERT component in data name!");
172
173 m_keyName = dataName.getSubName(0, profileIndex);
174 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
175
Yingdi Yu5ff62102013-10-13 17:24:50 -0700176 OID profileExtensionOID("1.3.6.1.5.32.2.1");
177 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
178
Yingdi Yu64206112013-12-24 11:16:32 +0800179 ExtensionList::iterator it = extensionList_.begin();
180 for(; it != extensionList_.end(); it++)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700181 {
Yingdi Yu64206112013-12-24 11:16:32 +0800182 if(profileExtensionOID == it->getOid())
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700183 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700184 ProfileExtension profileExtension(*it);
Yingdi Yu64206112013-12-24 11:16:32 +0800185 m_profileData = *profileExtension.getProfileData();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700186 }
Yingdi Yu64206112013-12-24 11:16:32 +0800187 if(endorseExtensionOID == it->getOid())
Yingdi Yu5ff62102013-10-13 17:24:50 -0700188 {
189 EndorseExtension endorseExtension(*it);
Yingdi Yu64206112013-12-24 11:16:32 +0800190 m_endorseList = endorseExtension.getEndorseList();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700191 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700192 }
193}