blob: c6d338acd54af412b0e53c9affb60551bcb82a89 [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>
16
17
18using namespace std;
19using namespace ndn;
20using namespace ndn::security;
21
Yingdi Yu5ff62102013-10-13 17:24:50 -070022ProfileExtension::ProfileExtension(const ProfileData & profileData)
23 : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire())
Yingdi Yu3c2a9762013-10-11 11:02:09 -070024{}
25
Yingdi Yu5ff62102013-10-13 17:24:50 -070026ProfileExtension::ProfileExtension(const ProfileExtension& profileExtension)
27 : CertificateExtension("1.3.6.1.5.32.2.1", true, profileExtension.m_extnValue)
28{}
29
30ProfileExtension::ProfileExtension(const CertificateExtension& extension)
31 : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
32{
33 if(m_extnID != OID("1.3.6.1.5.32.2.1"))
34 throw LnException("Wrong ProfileExtension Number!");
35}
36
37Ptr<ProfileData>
38ProfileExtension::getProfileData()
39{
40 boost::iostreams::stream
41 <boost::iostreams::array_source> is (m_extnValue.buf (), m_extnValue.size ());
42 return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(is)));
43}
44
45EndorseExtension::EndorseExtension(const vector<string>& endorsedList)
46 : CertificateExtension("1.3.6.1.5.32.2.2", true, *EndorseExtension::prepareValue(endorsedList))
47{}
48
49EndorseExtension::EndorseExtension(const EndorseExtension& endorseExtension)
50 : CertificateExtension("1.3.6.1.5.32.2.2", true, endorseExtension.m_extnValue)
51{}
52
53EndorseExtension::EndorseExtension(const CertificateExtension& extension)
54 : CertificateExtension(extension.getOID(), extension.getCritical(), extension.getValue())
55{
56 if(m_extnID != OID("1.3.6.1.5.32.2.2"))
57 throw LnException("Wrong EndorseExtension Number!");
58}
59
60vector<string>
61EndorseExtension::getEndorsedList()
62{
63 vector<string> endorsedList;
64
65 boost::iostreams::stream
66 <boost::iostreams::array_source> is (m_extnValue.buf(), m_extnValue.size());
67
68 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
69 const der::DerNodePtrList & children = root->getChildren();
70 der::SimpleVisitor simpleVisitor;
71
72 for(int i = 0; i < children.size(); i++)
73 endorsedList.push_back(boost::any_cast<string>(children[i]->accept(simpleVisitor)));
74
75 return endorsedList;
76}
77
78Ptr<Blob>
79EndorseExtension::prepareValue(const vector<string>& endorsedList)
80{
81 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
82
83 vector<string>::const_iterator it = endorsedList.begin();
84 for(; it != endorsedList.end(); it++)
85 {
86 Ptr<der::DerPrintableString> entry = Ptr<der::DerPrintableString>(new der::DerPrintableString(*it));
87 root->addChild(entry);
88 }
89
90 blob_stream blobStream;
91 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
92 root->encode(start);
93
94 return blobStream.buf ();
95}
96
Yingdi Yu3c2a9762013-10-11 11:02:09 -070097EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate,
Yingdi Yu5ff62102013-10-13 17:24:50 -070098 const Time& notBefore,
99 const Time& notAfter,
100 Ptr<ProfileData> profileData,
101 const vector<string>& endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700102 : Certificate()
103 , m_keyName(kskCertificate.getPublicKeyName())
Yingdi Yu5ff62102013-10-13 17:24:50 -0700104 , m_signer(kskCertificate.getPublicKeyName())
105 , m_profileData(profileData)
106 , m_endorseList(endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700107{
Yingdi Yu5ff62102013-10-13 17:24:50 -0700108 Name dataName = m_keyName;
109 TimeInterval ti = time::NowUnixTimestamp();
110 ostringstream oss;
111 oss << ti.total_seconds();
112 dataName.append("PROFILE-CERT").append(m_signer).append(oss.str());
113 setName(dataName);
114
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700115 setNotBefore(notBefore);
116 setNotAfter(notAfter);
117 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
118 setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
Yingdi Yu5ff62102013-10-13 17:24:50 -0700119 addExtension(ProfileExtension(*m_profileData));
120 addExtension(EndorseExtension(m_endorseList));
121
122 encode();
123}
124
125EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate,
126 const Name& signer,
127 const Time& notBefore,
128 const Time& notAfter,
129 const vector<string>& endorseList)
130 : Certificate()
131 , m_keyName(endorseCertificate.m_keyName)
132 , m_signer(signer)
133 , m_profileData(endorseCertificate.m_profileData)
134 , m_endorseList(endorseList)
135{
136 Name dataName = m_keyName;
137 TimeInterval ti = time::NowUnixTimestamp();
138 ostringstream oss;
139 oss << ti.total_seconds();
140 dataName.append("PROFILE-CERT").append(m_signer).append(oss.str());
141 setName(dataName);
142
143 setNotBefore(notBefore);
144 setNotAfter(notAfter);
145 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
146 setPublicKeyInfo(endorseCertificate.getPublicKeyInfo());
147 addExtension(ProfileExtension(*m_profileData));
148 addExtension(EndorseExtension(m_endorseList));
149
150 encode();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700151}
152
153EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate)
154 : Certificate(endorseCertificate)
155 , m_keyName(endorseCertificate.m_keyName)
156 , m_signer(endorseCertificate.m_signer)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700157 , m_profileData(endorseCertificate.m_profileData)
158 , m_endorseList(endorseCertificate.m_endorseList)
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700159{}
160
161EndorseCertificate::EndorseCertificate(const Data& data)
162 : Certificate(data)
163{
164 const Name& dataName = data.getName();
165 name::Component certFlag(string("PROFILE-CERT"));
166
167 int profileIndex = -1;
168 for(int i = 0; i < dataName.size(); i++)
169 {
170 if(0 == dataName.get(i).compare(certFlag))
171 {
172 profileIndex = i;
173 break;
174 }
175 }
176
177 if(profileIndex < 0)
178 throw LnException("No PROFILE-CERT component in data name!");
179
180 m_keyName = dataName.getSubName(0, profileIndex);
181 m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
182
Yingdi Yu5ff62102013-10-13 17:24:50 -0700183 OID profileExtensionOID("1.3.6.1.5.32.2.1");
184 OID endorseExtensionOID("1.3.6.1.5.32.2.2");
185
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700186 ExtensionList::iterator it = m_extnList.begin();
187 for(; it != m_extnList.end(); it++)
188 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700189 if(profileExtensionOID == it->getOID())
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700190 {
Yingdi Yu5ff62102013-10-13 17:24:50 -0700191 ProfileExtension profileExtension(*it);
192 m_profileData = profileExtension.getProfileData();
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700193 }
Yingdi Yu5ff62102013-10-13 17:24:50 -0700194 if(endorseExtensionOID == it->getOID())
195 {
196 EndorseExtension endorseExtension(*it);
197 m_endorseList = endorseExtension.getEndorsedList();
198 }
Yingdi Yu3c2a9762013-10-11 11:02:09 -0700199 }
200}