blob: 0fd6ba073a8c35f38f90a0e36e0d275464c67c91 [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 Yu92e8e482013-10-17 21:13:03 -070014#include "logging.h"
Yingdi Yuc972c452013-10-11 11:02:09 -070015
16using namespace std;
17using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080018using namespace ndn::ptr_lib;
Yingdi Yuc972c452013-10-11 11:02:09 -070019
Yingdi Yu92e8e482013-10-17 21:13:03 -070020INIT_LOGGER("EndorseCertificate");
21
Yingdi Yub4be64a2013-10-13 17:24:50 -070022ProfileExtension::ProfileExtension(const ProfileData & profileData)
Yingdi Yuf8f572d2014-01-13 11:19:47 -080023 : CertificateExtension("1.3.6.1.5.32.2.1", true, Buffer(profileData.wireEncode().wire(), profileData.wireEncode().size()))
Yingdi Yuc972c452013-10-11 11:02:09 -070024{}
25
Yingdi Yub4be64a2013-10-13 17:24:50 -070026ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080027 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_)
Yingdi Yub4be64a2013-10-13 17:24:50 -070028{}
29
30ProfileExtension::ProfileExtension(const CertificateExtension& extension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080031 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yub4be64a2013-10-13 17:24:50 -070032{
Yingdi Yu76dd8002013-12-24 11:16:32 +080033 if(extensionId_ != OID("1.3.6.1.5.32.2.1"))
Yingdi Yuf8f572d2014-01-13 11:19:47 -080034 throw Error("Wrong ProfileExtension Number!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070035}
36
Yingdi Yu76dd8002013-12-24 11:16:32 +080037shared_ptr<ProfileData>
Yingdi Yub4be64a2013-10-13 17:24:50 -070038ProfileExtension::getProfileData()
39{
Yingdi Yu76dd8002013-12-24 11:16:32 +080040 Data data;
Yingdi Yuf8f572d2014-01-13 11:19:47 -080041 data.wireDecode(Block(extensionValue_.buf(), extensionValue_.size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +080042 return make_shared<ProfileData>(data);
Yingdi Yub4be64a2013-10-13 17:24:50 -070043}
44
Yingdi Yu76dd8002013-12-24 11:16:32 +080045EndorseExtension::EndorseExtension(const vector<string>& endorseList)
46 : CertificateExtension("1.3.6.1.5.32.2.2", true, EndorseExtension::encodeEndorseList(endorseList))
Yingdi Yub4be64a2013-10-13 17:24:50 -070047{}
48
49EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080050 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_)
Yingdi Yub4be64a2013-10-13 17:24:50 -070051{}
52
53EndorseExtension::EndorseExtension(const CertificateExtension& extension)
Yingdi Yu76dd8002013-12-24 11:16:32 +080054 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yub4be64a2013-10-13 17:24:50 -070055{
Yingdi Yu76dd8002013-12-24 11:16:32 +080056 if(extensionId_ != OID("1.3.6.1.5.32.2.2"))
Yingdi Yuf8f572d2014-01-13 11:19:47 -080057 throw Error("Wrong EndorseExtension Number!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070058}
59
60vector<string>
Yingdi Yu76dd8002013-12-24 11:16:32 +080061EndorseExtension::getEndorseList()
Yingdi Yub4be64a2013-10-13 17:24:50 -070062{
Yingdi Yu76dd8002013-12-24 11:16:32 +080063 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070064
65 boost::iostreams::stream
Yingdi Yu76dd8002013-12-24 11:16:32 +080066 <boost::iostreams::array_source> is ((const char*)extensionValue_.buf(), extensionValue_.size());
67
68 endorseExtension.ParseFromIstream(&is);
69
70 vector<string> endorseList;
71
72 for(int i = 0; i < endorseExtension.endorseentry_size(); i ++)
73 endorseList.push_back(endorseExtension.endorseentry(i).name());
Yingdi Yub4be64a2013-10-13 17:24:50 -070074
Yingdi Yu76dd8002013-12-24 11:16:32 +080075 return endorseList;
Yingdi Yub4be64a2013-10-13 17:24:50 -070076}
77
Yingdi Yuf8f572d2014-01-13 11:19:47 -080078Buffer
Yingdi Yu76dd8002013-12-24 11:16:32 +080079EndorseExtension::encodeEndorseList(const vector<string>& endorseList)
Yingdi Yub4be64a2013-10-13 17:24:50 -070080{
Yingdi Yu76dd8002013-12-24 11:16:32 +080081 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yub4be64a2013-10-13 17:24:50 -070082
Yingdi Yu76dd8002013-12-24 11:16:32 +080083 vector<string>::const_iterator it = endorseList.begin();
84 for(; it != endorseList.end(); it++)
85 endorseExtension.add_endorseentry()->set_name(*it);
Yingdi Yub4be64a2013-10-13 17:24:50 -070086
Yingdi Yu76dd8002013-12-24 11:16:32 +080087 string encoded;
88 endorseExtension.SerializeToString(&encoded);
89
Yingdi Yuf8f572d2014-01-13 11:19:47 -080090 return Buffer(encoded.c_str(), encoded.size());
Yingdi Yub4be64a2013-10-13 17:24:50 -070091}
92
Yingdi Yuc972c452013-10-11 11:02:09 -070093EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu76dd8002013-12-24 11:16:32 +080094 const ProfileData& profileData,
Yingdi Yub4be64a2013-10-13 17:24:50 -070095 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -070096 : Certificate()
97 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yub4be64a2013-10-13 17:24:50 -070098 , m_signer(kskCertificate.getPublicKeyName())
99 , m_profileData(profileData)
100 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700101{
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -0800103 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700104 setName(dataName);
105
Yingdi Yub2e747d2013-11-05 23:06:43 -0800106 setNotBefore(kskCertificate.getNotBefore());
107 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800108 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700109 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800110 addExtension(ProfileExtension(m_profileData));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700111 addExtension(EndorseExtension(m_endorseList));
112
113 encode();
114}
115
116EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
117 const Name& signer,
Yingdi Yub4be64a2013-10-13 17:24:50 -0700118 const vector<string>& endorseList)
119 : Certificate()
120 , m_keyName(endorseCertificate.m_keyName)
121 , m_signer(signer)
122 , m_profileData(endorseCertificate.m_profileData)
123 , m_endorseList(endorseList)
Yingdi Yub6fb0302014-01-21 11:05:11 -0800124{
Yingdi Yub4be64a2013-10-13 17:24:50 -0700125 Name dataName = m_keyName;
Yingdi Yub6fb0302014-01-21 11:05:11 -0800126 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700127 setName(dataName);
128
Yingdi Yub2e747d2013-11-05 23:06:43 -0800129 setNotBefore(endorseCertificate.getNotBefore());
130 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800131 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700132 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800133 addExtension(ProfileExtension(m_profileData));
Yingdi Yub4be64a2013-10-13 17:24:50 -0700134 addExtension(EndorseExtension(m_endorseList));
135
136 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700137}
138
139EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
140 : Certificate(endorseCertificate)
141 , m_keyName(endorseCertificate.m_keyName)
142 , m_signer(endorseCertificate.m_signer)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700143 , m_profileData(endorseCertificate.m_profileData)
144 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700145{}
146
147EndorseCertificate::EndorseCertificate(const Data& data)
148 : Certificate(data)
149{
150 const Name& dataName = data.getName();
Yingdi Yuc972c452013-10-11 11:02:09 -0700151
Yingdi Yub6fb0302014-01-21 11:05:11 -0800152 if(dataName.size() < 3 || !dataName.get(-3).equals("PROFILE-CERT"))
153 throw Error("No PROFILE-CERT component in data name!");
154
155 m_keyName = dataName.getPrefix(-3);
156 m_signer.wireDecode(Block(dataName.get(-2).getValue().buf(),
157 dataName.get(-2).getValue().size()));
Yingdi Yuc972c452013-10-11 11:02:09 -0700158
Yingdi Yub4be64a2013-10-13 17:24:50 -0700159 OID profileExtensionOID("1.3.6.1.5.32.2.1");
160 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
161
Yingdi Yu76dd8002013-12-24 11:16:32 +0800162 ExtensionList::iterator it = extensionList_.begin();
163 for(; it != extensionList_.end(); it++)
Yingdi Yuc972c452013-10-11 11:02:09 -0700164 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800165 if(profileExtensionOID == it->getOid())
Yingdi Yuc972c452013-10-11 11:02:09 -0700166 {
Yingdi Yub4be64a2013-10-13 17:24:50 -0700167 ProfileExtension profileExtension(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800168 m_profileData = *profileExtension.getProfileData();
Yingdi Yuc972c452013-10-11 11:02:09 -0700169 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800170 if(endorseExtensionOID == it->getOid())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700171 {
172 EndorseExtension endorseExtension(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800173 m_endorseList = endorseExtension.getEndorseList();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700174 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700175 }
176}