blob: 0fd6ba073a8c35f38f90a0e36e0d275464c67c91 [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"
Yingdi Yu64206112013-12-24 11:16:32 +080012#include "endorse-extension.pb.h"
Yingdi Yu64206112013-12-24 11:16:32 +080013#include <boost/iostreams/stream.hpp>
Yingdi Yu68aced92013-10-17 21:13:03 -070014#include "logging.h"
Yingdi Yu3c2a9762013-10-11 11:02:09 -070015
16using namespace std;
17using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080018using namespace ndn::ptr_lib;
Yingdi Yu3c2a9762013-10-11 11:02:09 -070019
Yingdi Yu68aced92013-10-17 21:13:03 -070020INIT_LOGGER("EndorseCertificate");
21
Yingdi Yu5ff62102013-10-13 17:24:50 -070022ProfileExtension::ProfileExtension(const ProfileData & profileData)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080023 : CertificateExtension("1.3.6.1.5.32.2.1", true, Buffer(profileData.wireEncode().wire(), profileData.wireEncode().size()))
Yingdi Yu3c2a9762013-10-11 11:02:09 -070024{}
25
Yingdi Yu5ff62102013-10-13 17:24:50 -070026ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
Yingdi Yu64206112013-12-24 11:16:32 +080027 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.extensionValue_)
Yingdi Yu5ff62102013-10-13 17:24:50 -070028{}
29
30ProfileExtension::ProfileExtension(const CertificateExtension& extension)
Yingdi Yu64206112013-12-24 11:16:32 +080031 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yu5ff62102013-10-13 17:24:50 -070032{
Yingdi Yu64206112013-12-24 11:16:32 +080033 if(extensionId_ != OID("1.3.6.1.5.32.2.1"))
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080034 throw Error("Wrong ProfileExtension Number!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070035}
36
Yingdi Yu64206112013-12-24 11:16:32 +080037shared_ptr<ProfileData>
Yingdi Yu5ff62102013-10-13 17:24:50 -070038ProfileExtension::getProfileData()
39{
Yingdi Yu64206112013-12-24 11:16:32 +080040 Data data;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080041 data.wireDecode(Block(extensionValue_.buf(), extensionValue_.size()));
Yingdi Yu64206112013-12-24 11:16:32 +080042 return make_shared<ProfileData>(data);
Yingdi Yu5ff62102013-10-13 17:24:50 -070043}
44
Yingdi Yu64206112013-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 Yu5ff62102013-10-13 17:24:50 -070047{}
48
49EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
Yingdi Yu64206112013-12-24 11:16:32 +080050 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.extensionValue_)
Yingdi Yu5ff62102013-10-13 17:24:50 -070051{}
52
53EndorseExtension::EndorseExtension(const CertificateExtension& extension)
Yingdi Yu64206112013-12-24 11:16:32 +080054 : CertificateExtension(extension.getOid(), extension.getIsCritical(), extension.getValue())
Yingdi Yu5ff62102013-10-13 17:24:50 -070055{
Yingdi Yu64206112013-12-24 11:16:32 +080056 if(extensionId_ != OID("1.3.6.1.5.32.2.2"))
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080057 throw Error("Wrong EndorseExtension Number!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070058}
59
60vector<string>
Yingdi Yu64206112013-12-24 11:16:32 +080061EndorseExtension::getEndorseList()
Yingdi Yu5ff62102013-10-13 17:24:50 -070062{
Yingdi Yu64206112013-12-24 11:16:32 +080063 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yu5ff62102013-10-13 17:24:50 -070064
65 boost::iostreams::stream
Yingdi Yu64206112013-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 Yu5ff62102013-10-13 17:24:50 -070074
Yingdi Yu64206112013-12-24 11:16:32 +080075 return endorseList;
Yingdi Yu5ff62102013-10-13 17:24:50 -070076}
77
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080078Buffer
Yingdi Yu64206112013-12-24 11:16:32 +080079EndorseExtension::encodeEndorseList(const vector<string>& endorseList)
Yingdi Yu5ff62102013-10-13 17:24:50 -070080{
Yingdi Yu64206112013-12-24 11:16:32 +080081 Chronos::EndorseExtensionMsg endorseExtension;
Yingdi Yu5ff62102013-10-13 17:24:50 -070082
Yingdi Yu64206112013-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 Yu5ff62102013-10-13 17:24:50 -070086
Yingdi Yu64206112013-12-24 11:16:32 +080087 string encoded;
88 endorseExtension.SerializeToString(&encoded);
89
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080090 return Buffer(encoded.c_str(), encoded.size());
Yingdi Yu5ff62102013-10-13 17:24:50 -070091}
92
Yingdi Yu3c2a9762013-10-11 11:02:09 -070093EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu64206112013-12-24 11:16:32 +080094 const ProfileData& profileData,
Yingdi Yu5ff62102013-10-13 17:24:50 -070095 const vector<string>& endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -070096 : Certificate()
97 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yu5ff62102013-10-13 17:24:50 -070098 , m_signer(kskCertificate.getPublicKeyName())
99 , m_profileData(profileData)
100 , m_endorseList(endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700101{
Yingdi Yu5ff62102013-10-13 17:24:50 -0700102 Name dataName = m_keyName;
Yingdi Yu6df61252014-01-21 11:05:11 -0800103 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700104 setName(dataName);
105
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800106 setNotBefore(kskCertificate.getNotBefore());
107 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu64206112013-12-24 11:16:32 +0800108 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu68aced92013-10-17 21:13:03 -0700109 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu64206112013-12-24 11:16:32 +0800110 addExtension(ProfileExtension(m_profileData));
Yingdi Yu5ff62102013-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 Yu5ff62102013-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 Yu6df61252014-01-21 11:05:11 -0800124{
Yingdi Yu5ff62102013-10-13 17:24:50 -0700125 Name dataName = m_keyName;
Yingdi Yu6df61252014-01-21 11:05:11 -0800126 dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700127 setName(dataName);
128
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800129 setNotBefore(endorseCertificate.getNotBefore());
130 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu64206112013-12-24 11:16:32 +0800131 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu5ff62102013-10-13 17:24:50 -0700132 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
Yingdi Yu64206112013-12-24 11:16:32 +0800133 addExtension(ProfileExtension(m_profileData));
Yingdi Yu5ff62102013-10-13 17:24:50 -0700134 addExtension(EndorseExtension(m_endorseList));
135
136 encode();
Yingdi Yu3c2a9762013-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 Yu5ff62102013-10-13 17:24:50 -0700143 , m_profileData(endorseCertificate.m_profileData)
144 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700145{}
146
147EndorseCertificate::EndorseCertificate(const Data& data)
148 : Certificate(data)
149{
150 const Name& dataName = data.getName();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700151
Yingdi Yu6df61252014-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 Yu3c2a9762013-10-11 11:02:09 -0700158
Yingdi Yu5ff62102013-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 Yu64206112013-12-24 11:16:32 +0800162 ExtensionList::iterator it = extensionList_.begin();
163 for(; it != extensionList_.end(); it++)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700164 {
Yingdi Yu64206112013-12-24 11:16:32 +0800165 if(profileExtensionOID == it->getOid())
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700166 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700167 ProfileExtension profileExtension(*it);
Yingdi Yu64206112013-12-24 11:16:32 +0800168 m_profileData = *profileExtension.getProfileData();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700169 }
Yingdi Yu64206112013-12-24 11:16:32 +0800170 if(endorseExtensionOID == it->getOid())
Yingdi Yu5ff62102013-10-13 17:24:50 -0700171 {
172 EndorseExtension endorseExtension(*it);
Yingdi Yu64206112013-12-24 11:16:32 +0800173 m_endorseList = endorseExtension.getEndorseList();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700174 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700175 }
176}