blob: 8dd43e039e832b77d2f92edfb74260e5a8a9393f [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"
12#include "exception.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070013#include <ndn.cxx/helpers/der/der.h>
14#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
Yingdi Yuc972c452013-10-11 11:02:09 -070015#include <ndn.cxx/security/certificate/certificate-subdescrpt.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070016#include "logging.h"
Yingdi Yuc972c452013-10-11 11:02:09 -070017
18using namespace std;
19using namespace ndn;
20using namespace ndn::security;
21
Yingdi Yu92e8e482013-10-17 21:13:03 -070022INIT_LOGGER("EndorseCertificate");
23
Yingdi Yub4be64a2013-10-13 17:24:50 -070024ProfileExtension::ProfileExtension(const ProfileData & profileData)
25 : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire())
Yingdi Yuc972c452013-10-11 11:02:09 -070026{}
27
Yingdi Yub4be64a2013-10-13 17:24:50 -070028ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
29 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.m_extnValue)
30{}
31
32ProfileExtension::ProfileExtension(const CertificateExtension& extension)
33 : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
34{
35 if(m_extnID != OID("1.3.6.1.5.32.2.1"))
36 throw LnException("Wrong ProfileExtension Number!");
37}
38
39Ptr<ProfileData>
40ProfileExtension::getProfileData()
41{
Yingdi Yu92e8e482013-10-17 21:13:03 -070042 // _LOG_DEBUG("size: " << m_extnValue.size ());
Yingdi Yub4be64a2013-10-13 17:24:50 -070043 boost::iostreams::stream
44 <boost::iostreams::array_source> is (m_extnValue.buf (), m_extnValue.size ());
45 return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(is)));
46}
47
48EndorseExtension::EndorseExtension(const vector<string>& endorsedList)
49 : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList))
50{}
51
52EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
53 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue)
54{}
55
56EndorseExtension::EndorseExtension(const CertificateExtension& extension)
57 : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
58{
59 if(m_extnID != OID("1.3.6.1.5.32.2.2"))
60 throw LnException("Wrong EndorseExtension Number!");
61}
62
63vector<string>
64EndorseExtension::getEndorsedList()
65{
66 vector<string> endorsedList;
67
68 boost::iostreams::stream
69 <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size());
70
71 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
72 const der::DerNodePtrList & children = root->getChildren();
73 der::SimpleVisitor simpleVisitor;
74
75 for(int i = 0; i < children.size(); i++)
76 endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor)));
77
78 return endorsedList;
79}
80
81Ptr<Blob>
82EndorseExtension::prepareValue(const vector<string>& endorsedList)
83{
84 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
85
86 vector<string>::const_iterator it = endorsedList.begin();
87 for(; it != endorsedList.end(); it++)
88 {
89 Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it));
90 root->addChild(entry);
91 }
92
93 blob_stream blobStream;
94 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
95 root->encode(start);
96
97 return blobStream.buf ();
98}
99
Yingdi Yuc972c452013-10-11 11:02:09 -0700100EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yub4be64a2013-10-13 17:24:50 -0700101 const Time& notBefore,
102 const Time& notAfter,
103 Ptr<ProfileData> profileData,
104 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700105 : Certificate()
106 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700107 , m_signer(kskCertificate.getPublicKeyName())
108 , m_profileData(profileData)
109 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700110{
Yingdi Yub4be64a2013-10-13 17:24:50 -0700111 Name dataName = m_keyName;
112 TimeInterval ti = time::NowUnixTimestamp();
113 ostringstream oss;
114 oss << ti.total_seconds();
115 dataName.append("PROFILE-CERT").append(m_signer).append(oss.str());
116 setName(dataName);
117
Yingdi Yuc972c452013-10-11 11:02:09 -0700118 setNotBefore(notBefore);
119 setNotAfter(notAfter);
120 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700121 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700122 addExtension(ProfileExtension(*m_profileData));
123 addExtension(EndorseExtension(m_endorseList));
124
125 encode();
126}
127
128EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
129 const Name& signer,
130 const Time& notBefore,
131 const Time& notAfter,
132 const vector<string>& endorseList)
133 : Certificate()
134 , m_keyName(endorseCertificate.m_keyName)
135 , m_signer(signer)
136 , m_profileData(endorseCertificate.m_profileData)
137 , m_endorseList(endorseList)
138{
139 Name dataName = m_keyName;
140 TimeInterval ti = time::NowUnixTimestamp();
141 ostringstream oss;
142 oss << ti.total_seconds();
143 dataName.append("PROFILE-CERT").append(m_signer).append(oss.str());
144 setName(dataName);
145
146 setNotBefore(notBefore);
147 setNotAfter(notAfter);
148 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
149 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
150 addExtension(ProfileExtension(*m_profileData));
151 addExtension(EndorseExtension(m_endorseList));
152
153 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700154}
155
156EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
157 : Certificate(endorseCertificate)
158 , m_keyName(endorseCertificate.m_keyName)
159 , m_signer(endorseCertificate.m_signer)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700160 , m_profileData(endorseCertificate.m_profileData)
161 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700162{}
163
164EndorseCertificate::EndorseCertificate(const Data& data)
165 : Certificate(data)
166{
Yingdi Yu92e8e482013-10-17 21:13:03 -0700167 // _LOG_DEBUG("0");
Yingdi Yuc972c452013-10-11 11:02:09 -0700168 const Name& dataName = data.getName();
Yingdi Yu92e8e482013-10-17 21:13:03 -0700169 // _LOG_DEBUG("1");
Yingdi Yuc972c452013-10-11 11:02:09 -0700170 name::Component certFlag(string("PROFILE-CERT"));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700171 // _LOG_DEBUG("2");
Yingdi Yuc972c452013-10-11 11:02:09 -0700172 int profileIndex = -1;
173 for(int i = 0; i < dataName.size(); i++)
174 {
175 if(0 == dataName.get(i).compare(certFlag))
176 {
177 profileIndex = i;
178 break;
179 }
180 }
Yingdi Yu92e8e482013-10-17 21:13:03 -0700181 // _LOG_DEBUG("3");
Yingdi Yuc972c452013-10-11 11:02:09 -0700182 if(profileIndex < 0)
183 throw LnException("No PROFILE-CERT component in data name!");
184
185 m_keyName = dataName.getSubName(0, profileIndex);
186 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
187
Yingdi Yu92e8e482013-10-17 21:13:03 -0700188 // _LOG_DEBUG("keyName: " << m_keyName.toUri());
189 // _LOG_DEBUG("signer: " << m_signer.toUri());
190
Yingdi Yub4be64a2013-10-13 17:24:50 -0700191 OID profileExtensionOID("1.3.6.1.5.32.2.1");
192 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
193
Yingdi Yu92e8e482013-10-17 21:13:03 -0700194 // _LOG_DEBUG("OID ready");
Yingdi Yuc972c452013-10-11 11:02:09 -0700195 ExtensionList::iterator it = m_extnList.begin();
196 for(; it != m_extnList.end(); it++)
197 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700198 // _LOG_DEBUG("entry");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700199 if(profileExtensionOID == it->getOID())
Yingdi Yuc972c452013-10-11 11:02:09 -0700200 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700201 // _LOG_DEBUG("ProfileExtn");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700202 ProfileExtension profileExtension(*it);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700203 // _LOG_DEBUG("ProfileExtn created");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700204 m_profileData = profileExtension.getProfileData();
Yingdi Yu92e8e482013-10-17 21:13:03 -0700205 // _LOG_DEBUG("get profileDate");
Yingdi Yuc972c452013-10-11 11:02:09 -0700206 }
Yingdi Yub4be64a2013-10-13 17:24:50 -0700207 if(endorseExtensionOID == it->getOID())
208 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700209 // _LOG_DEBUG("EndorseExtn");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700210 EndorseExtension endorseExtension(*it);
211 m_endorseList = endorseExtension.getEndorsedList();
212 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700213 }
214}