blob: 9d77ccada5ec0393a1f46d821dc62dd32ef9b907 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -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 "sync-intro-certificate.h"
12#include <ndn.cxx/security/exception.h>
13
14using namespace ndn;
15using namespace ndn::security;
16using namespace std;
17
18SyncIntroCertificate::SyncIntroCertificate ()
19 : Certificate()
20{}
21
22SyncIntroCertificate::SyncIntroCertificate (const Name& nameSpace,
23 const Name& keyName,
24 const Name& signerName,
25 const Time& notBefore,
26 const Time& notAfter,
27 const Publickey& key,
28 const IntroType& introType)
29 : m_keyName(keyName)
30 , m_introType(introType)
31{
32 Name certificateName = nameSpace;
33 certificateName.append("WOT").append(keyName).append("INTRO-CERT").append(signerName);
34 switch(introType)
35 {
36 case PRODUCER:
37 certificateName.append("PRODUCER");
38 break;
39 case INTRODUCER:
40 certificateName.append("INTRODUCER");
41 break;
42 default:
43 throw SecException("Wrong Introduction Type!");
44 }
45 certificateName.appendVersion();
46
47 setName(certificateName);
48 setNotBefore(notBefore);
49 setNotAfter(notAfter);
50 setPublicKeyInfo(key);
51 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", keyName.toUri()));
52}
53
54SyncIntroCertificate::SyncIntroCertificate (const Name& nameSpace,
55 const IdentityCertificate& identityCertificate,
56 const Name& signerName,
57 const IntroType& introType)
58 : m_introType(introType)
59{
60 m_keyName = identityCertificate.getPublicKeyName();
61
62 Name certificateName = nameSpace;
63 certificateName.append("WOT").append(m_keyName).append("INTRO-CERT").append(signerName);
64 switch(introType)
65 {
66 case PRODUCER:
67 certificateName.append("PRODUCER");
68 break;
69 case INTRODUCER:
70 certificateName.append("INTRODUCER");
71 break;
72 default:
73 throw SecException("Wrong Introduction Type!");
74 }
75 certificateName.appendVersion();
76
77 setName(certificateName);
78 setNotBefore(identityCertificate.getNotBefore());
79 setNotAfter(identityCertificate.getNotAfter());
80 setPublicKeyInfo(identityCertificate.getPublicKeyInfo());
81 addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
82}
83
84SyncIntroCertificate::SyncIntroCertificate (const Data& data)
85 : Certificate(data)
86{
87 Name certificateName = getName();
88 int i = 0;
89 int keyNameStart = 0;
90 int keyNameEnd = 0;
91 for(; i < certificateName.size(); i++)
92 {
93 if(certificateName.get(i).toUri() == string("WOT"))
94 {
95 keyNameStart = i + 1;
96 break;
97 }
98 }
99
100 if(i >= certificateName.size())
101 throw SecException("Wrong SyncIntroCertificate Name!");
102
103 for(; i< certificateName.size(); i++)
104 {
105 if(certificateName.get(i).toUri() == string("INTRO-CERT"))
106 {
107 keyNameEnd = i;
108 break;
109 }
110 }
111
112 if(i >= certificateName.size())
113 throw SecException("Wrong SyncIntroCertificate Name!");
114
115 m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd);
116
117 string typeComponent = certificateName.get(certificateName.size() - 2).toUri();
118 if(typeComponent == string("PRODUCER"))
119 m_introType = PRODUCER;
120 else if(typeComponent == string("INTRODUCER"))
121 m_introType = INTRODUCER;
122 else
123 throw SecException("Wrong SyncIntroCertificate Name!");
124}
125
126SyncIntroCertificate::SyncIntroCertificate (const SyncIntroCertificate& chronosIntroCertificate)
127 : Certificate(chronosIntroCertificate)
128 , m_keyName(chronosIntroCertificate.m_keyName)
129 , m_introType(chronosIntroCertificate.m_introType)
130{}
131
132Data &
133SyncIntroCertificate::setName (const Name& certificateName)
134{
135 int i = 0;
136 int keyNameStart = 0;
137 int keyNameEnd = 0;
138 for(; i < certificateName.size(); i++)
139 {
140 if(certificateName.get(i).toUri() == string("WOT"))
141 {
142 keyNameStart = i + 1;
143 break;
144 }
145 }
146
147 if(i >= certificateName.size())
148 throw SecException("Wrong SyncIntroCertificate Name!");
149
150 for(; i< certificateName.size(); i++)
151 {
152 if(certificateName.get(i).toUri() == string("INTRO-CERT"))
153 {
154 keyNameEnd = i;
155 break;
156 }
157 }
158
159 if(i >= certificateName.size())
160 throw SecException("Wrong SyncIntroCertificate Name!");
161
162 m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd);
163
164 string typeComponent = certificateName.get(certificateName.size() - 2).toUri();
165 if(typeComponent == string("PRODUCER"))
166 m_introType = PRODUCER;
167 else if(typeComponent == string("INTRODUCER"))
168 m_introType = INTRODUCER;
169 else
170 throw SecException("Wrong SyncIntroCertificate Name!");
171
172 return *this;
173}
174
175bool
176SyncIntroCertificate::isSyncIntroCertificate(const Certificate& certificate)
177{
178 const Name& certificateName = certificate.getName();
179 string introType = certificateName.get(certificateName.size() - 2).toUri();
180 if(introType != string("PRODUCER") && introType != string("INTRODUCER"))
181 return false;
182
183 int i = 0;
184 bool findWot = false;
185 bool findIntroCert = false;
186 for(; i < certificateName.size(); i++)
187 {
188 if(certificateName.get(i).toUri() == string("WOT"))
189 {
190 findWot = true;
191 break;
192 }
193 }
194
195 if(!findWot)
196 return false;
197
198 for(; i < certificateName.size(); i++)
199 {
200 if(certificateName.get(i).toUri() == string("INTRO-CERT"))
201 {
202 findIntroCert = true;
203 break;
204 }
205 }
206 if(!findIntroCert)
207 return false;
208
209 if(i < certificateName.size() - 2)
210 return true;
211
212 return false;
213}