blob: 80ff1657c1bfc4468bd56f0eedfaee0af9b9a360 [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 Yu5ff62102013-10-13 17:24:50 -070013#include <ndn.cxx/helpers/der/der.h>
14#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
Yingdi Yu3c2a9762013-10-11 11:02:09 -070015#include <ndn.cxx/security/certificate/certificate-subdescrpt.h>
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;
20using namespace ndn::security;
21
Yingdi Yu68aced92013-10-17 21:13:03 -070022INIT_LOGGER("EndorseCertificate");
23
Yingdi Yu5ff62102013-10-13 17:24:50 -070024ProfileExtension::ProfileExtension(const ProfileData & profileData)
25 : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire())
Yingdi Yu3c2a9762013-10-11 11:02:09 -070026{}
27
Yingdi Yu5ff62102013-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 Yub35b8652013-11-07 11:32:40 -080042 Ptr<Blob> dataBlob = Ptr<Blob>(new Blob(m_extnValue.buf (), m_extnValue.size ()));
43 return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(dataBlob)));
Yingdi Yu5ff62102013-10-13 17:24:50 -070044}
45
46EndorseExtension::EndorseExtension(const vector<string>& endorsedList)
47 : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList))
48{}
49
50EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
51 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue)
52{}
53
54EndorseExtension::EndorseExtension(const CertificateExtension& extension)
55 : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
56{
57 if(m_extnID != OID("1.3.6.1.5.32.2.2"))
58 throw LnException("Wrong EndorseExtension Number!");
59}
60
61vector<string>
62EndorseExtension::getEndorsedList()
63{
64 vector<string> endorsedList;
65
66 boost::iostreams::stream
67 <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size());
68
69 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
70 const der::DerNodePtrList & children = root->getChildren();
71 der::SimpleVisitor simpleVisitor;
72
73 for(int i = 0; i < children.size(); i++)
74 endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor)));
75
76 return endorsedList;
77}
78
79Ptr<Blob>
80EndorseExtension::prepareValue(const vector<string>& endorsedList)
81{
82 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
83
84 vector<string>::const_iterator it = endorsedList.begin();
85 for(; it != endorsedList.end(); it++)
86 {
87 Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it));
88 root->addChild(entry);
89 }
90
91 blob_stream blobStream;
92 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
93 root->encode(start);
94
95 return blobStream.buf ();
96}
97
Yingdi Yu3c2a9762013-10-11 11:02:09 -070098EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu5ff62102013-10-13 17:24:50 -070099 Ptr<ProfileData> profileData,
100 const vector<string>& endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700101 : Certificate()
102 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yu5ff62102013-10-13 17:24:50 -0700103 , m_signer(kskCertificate.getPublicKeyName())
104 , m_profileData(profileData)
105 , m_endorseList(endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700106{
Yingdi Yu5ff62102013-10-13 17:24:50 -0700107 Name dataName = m_keyName;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800108 dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700109 setName(dataName);
110
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800111 setNotBefore(kskCertificate.getNotBefore());
112 setNotAfter(kskCertificate.getNotAfter());
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700113 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
Yingdi Yu68aced92013-10-17 21:13:03 -0700114 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu5ff62102013-10-13 17:24:50 -0700115 addExtension(ProfileExtension(*m_profileData));
116 addExtension(EndorseExtension(m_endorseList));
117
118 encode();
119}
120
121EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
122 const Name& signer,
Yingdi Yu5ff62102013-10-13 17:24:50 -0700123 const vector<string>& endorseList)
124 : Certificate()
125 , m_keyName(endorseCertificate.m_keyName)
126 , m_signer(signer)
127 , m_profileData(endorseCertificate.m_profileData)
128 , m_endorseList(endorseList)
129{
130 Name dataName = m_keyName;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800131 dataName.append("PROFILE-CERT").append(m_signer).appendVersion();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700132 setName(dataName);
133
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800134 setNotBefore(endorseCertificate.getNotBefore());
135 setNotAfter(endorseCertificate.getNotAfter());
Yingdi Yu5ff62102013-10-13 17:24:50 -0700136 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
137 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
138 addExtension(ProfileExtension(*m_profileData));
139 addExtension(EndorseExtension(m_endorseList));
140
141 encode();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700142}
143
144EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
145 : Certificate(endorseCertificate)
146 , m_keyName(endorseCertificate.m_keyName)
147 , m_signer(endorseCertificate.m_signer)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700148 , m_profileData(endorseCertificate.m_profileData)
149 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700150{}
151
152EndorseCertificate::EndorseCertificate(const Data& data)
153 : Certificate(data)
154{
155 const Name& dataName = data.getName();
156 name::Component certFlag(string("PROFILE-CERT"));
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700157 int profileIndex = -1;
158 for(int i = 0; i < dataName.size(); i++)
159 {
160 if(0 == dataName.get(i).compare(certFlag))
161 {
162 profileIndex = i;
163 break;
164 }
165 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700166 if(profileIndex < 0)
167 throw LnException("No PROFILE-CERT component in data name!");
168
169 m_keyName = dataName.getSubName(0, profileIndex);
170 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
171
Yingdi Yu5ff62102013-10-13 17:24:50 -0700172 OID profileExtensionOID("1.3.6.1.5.32.2.1");
173 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
174
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700175 ExtensionList::iterator it = m_extnList.begin();
176 for(; it != m_extnList.end(); it++)
177 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700178 if(profileExtensionOID == it->getOID())
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700179 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700180 ProfileExtension profileExtension(*it);
181 m_profileData = profileExtension.getProfileData();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700182 }
Yingdi Yu5ff62102013-10-13 17:24:50 -0700183 if(endorseExtensionOID == it->getOID())
184 {
185 EndorseExtension endorseExtension(*it);
186 m_endorseList = endorseExtension.getEndorsedList();
187 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700188 }
189}