blob: 20e2aff4bd0425250c1fa91bda4dde8c9899e7f4 [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 Yub2e747d2013-11-05 23:06:43 -0800100// EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate)
101// : Certificate()
102// , m_keyName(kskCertificate.getPublicKeyName())
103// , m_signer(kskCertificate.getPublicKeyName())
104// {
105// Profile profile(m_keyName.getPrefix(m_keyName.size()-1),
106// m_keyName.get(-2).toUri(),
107// m_keyName.get(m_keyName.size()-2).toUri());
108
109// Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(m_keyName.getPrefix(m_keyName.size()-1),
110// profile));
111
112// m_profileData = profileData;
113
114// Name dataName = m_keyName;
115// dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
116// setName(dataName);
117
118// setNotBefore(kskCertificate.getNotBefore());
119// setNotAfter(kskCertificate.getNotAfter());
120// addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
121// setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
122// addExtension(ProfileExtension(*m_profileData));
123// addExtension(EndorseExtension(m_endorseList));
124
125// encode();
126// }
127
128
Yingdi Yuc972c452013-10-11 11:02:09 -0700129EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yub4be64a2013-10-13 17:24:50 -0700130 Ptr<ProfileData> profileData,
131 const vector<string>& endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700132 : Certificate()
133 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yub4be64a2013-10-13 17:24:50 -0700134 , m_signer(kskCertificate.getPublicKeyName())
135 , m_profileData(profileData)
136 , m_endorseList(endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700137{
Yingdi Yub4be64a2013-10-13 17:24:50 -0700138 Name dataName = m_keyName;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800139 dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700140 setName(dataName);
141
Yingdi Yub2e747d2013-11-05 23:06:43 -0800142 setNotBefore(kskCertificate.getNotBefore());
143 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yuc972c452013-10-11 11:02:09 -0700144 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700145 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700146 addExtension(ProfileExtension(*m_profileData));
147 addExtension(EndorseExtension(m_endorseList));
148
149 encode();
150}
151
152EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
153 const Name& signer,
Yingdi Yub4be64a2013-10-13 17:24:50 -0700154 const vector<string>& endorseList)
155 : Certificate()
156 , m_keyName(endorseCertificate.m_keyName)
157 , m_signer(signer)
158 , m_profileData(endorseCertificate.m_profileData)
159 , m_endorseList(endorseList)
160{
161 Name dataName = m_keyName;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800162 dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700163 setName(dataName);
164
Yingdi Yub2e747d2013-11-05 23:06:43 -0800165 setNotBefore(endorseCertificate.getNotBefore());
166 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700167 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
168 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
169 addExtension(ProfileExtension(*m_profileData));
170 addExtension(EndorseExtension(m_endorseList));
171
172 encode();
Yingdi Yuc972c452013-10-11 11:02:09 -0700173}
174
175EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
176 : Certificate(endorseCertificate)
177 , m_keyName(endorseCertificate.m_keyName)
178 , m_signer(endorseCertificate.m_signer)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700179 , m_profileData(endorseCertificate.m_profileData)
180 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yuc972c452013-10-11 11:02:09 -0700181{}
182
183EndorseCertificate::EndorseCertificate(const Data& data)
184 : Certificate(data)
185{
Yingdi Yu92e8e482013-10-17 21:13:03 -0700186 // _LOG_DEBUG("0");
Yingdi Yuc972c452013-10-11 11:02:09 -0700187 const Name& dataName = data.getName();
Yingdi Yu92e8e482013-10-17 21:13:03 -0700188 // _LOG_DEBUG("1");
Yingdi Yuc972c452013-10-11 11:02:09 -0700189 name::Component certFlag(string("PROFILE-CERT"));
Yingdi Yu92e8e482013-10-17 21:13:03 -0700190 // _LOG_DEBUG("2");
Yingdi Yuc972c452013-10-11 11:02:09 -0700191 int profileIndex = -1;
192 for(int i = 0; i < dataName.size(); i++)
193 {
194 if(0 == dataName.get(i).compare(certFlag))
195 {
196 profileIndex = i;
197 break;
198 }
199 }
Yingdi Yu92e8e482013-10-17 21:13:03 -0700200 // _LOG_DEBUG("3");
Yingdi Yuc972c452013-10-11 11:02:09 -0700201 if(profileIndex < 0)
202 throw LnException("No PROFILE-CERT component in data name!");
203
204 m_keyName = dataName.getSubName(0, profileIndex);
205 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
206
Yingdi Yu92e8e482013-10-17 21:13:03 -0700207 // _LOG_DEBUG("keyName: " << m_keyName.toUri());
208 // _LOG_DEBUG("signer: " << m_signer.toUri());
209
Yingdi Yub4be64a2013-10-13 17:24:50 -0700210 OID profileExtensionOID("1.3.6.1.5.32.2.1");
211 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
212
Yingdi Yu92e8e482013-10-17 21:13:03 -0700213 // _LOG_DEBUG("OID ready");
Yingdi Yuc972c452013-10-11 11:02:09 -0700214 ExtensionList::iterator it = m_extnList.begin();
215 for(; it != m_extnList.end(); it++)
216 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700217 // _LOG_DEBUG("entry");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700218 if(profileExtensionOID == it->getOID())
Yingdi Yuc972c452013-10-11 11:02:09 -0700219 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700220 // _LOG_DEBUG("ProfileExtn");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700221 ProfileExtension profileExtension(*it);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700222 // _LOG_DEBUG("ProfileExtn created");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700223 m_profileData = profileExtension.getProfileData();
Yingdi Yu92e8e482013-10-17 21:13:03 -0700224 // _LOG_DEBUG("get profileDate");
Yingdi Yuc972c452013-10-11 11:02:09 -0700225 }
Yingdi Yub4be64a2013-10-13 17:24:50 -0700226 if(endorseExtensionOID == it->getOID())
227 {
Yingdi Yu92e8e482013-10-17 21:13:03 -0700228 // _LOG_DEBUG("EndorseExtn");
Yingdi Yub4be64a2013-10-13 17:24:50 -0700229 EndorseExtension endorseExtension(*it);
230 m_endorseList = endorseExtension.getEndorsedList();
231 }
Yingdi Yuc972c452013-10-11 11:02:09 -0700232 }
233}