blob: 13f4cadd26848a73bae5d505d51f43eba3e99b76 [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"
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080012
13#include <boost/date_time/posix_time/posix_time.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070014
15using namespace ndn;
Yingdi Yu43e71612013-10-30 22:19:31 -070016using namespace std;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080017using namespace boost;
Yingdi Yu43e71612013-10-30 22:19:31 -070018
19SyncIntroCertificate::SyncIntroCertificate ()
20 : Certificate()
21{}
22
23SyncIntroCertificate::SyncIntroCertificate (const Name& nameSpace,
24 const Name& keyName,
25 const Name& signerName,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080026 const MillisecondsSince1970& notBefore,
27 const MillisecondsSince1970& notAfter,
28 const PublicKey& key,
Yingdi Yu43e71612013-10-30 22:19:31 -070029 const IntroType& introType)
30 : m_keyName(keyName)
31 , m_introType(introType)
32{
33 Name certificateName = nameSpace;
34 certificateName.append("WOT").append(keyName).append("INTRO-CERT").append(signerName);
35 switch(introType)
36 {
37 case PRODUCER:
38 certificateName.append("PRODUCER");
39 break;
40 case INTRODUCER:
41 certificateName.append("INTRODUCER");
42 break;
43 default:
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080044 throw Error("Wrong Introduction Type!");
Yingdi Yu43e71612013-10-30 22:19:31 -070045 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080046
47 posix_time::time_duration now = posix_time::microsec_clock::universal_time () - posix_time::ptime(gregorian::date (1970, boost::gregorian::Jan, 1));
48 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
49 certificateName.appendVersion(version);
Yingdi Yu43e71612013-10-30 22:19:31 -070050
Yingdi Yu8600a092013-11-01 16:12:31 -070051 Data::setName(certificateName);
Yingdi Yu43e71612013-10-30 22:19:31 -070052 setNotBefore(notBefore);
53 setNotAfter(notAfter);
54 setPublicKeyInfo(key);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080055 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri()));
Yingdi Yu8600a092013-11-01 16:12:31 -070056 encode();
Yingdi Yu43e71612013-10-30 22:19:31 -070057}
58
59SyncIntroCertificate::SyncIntroCertificate (const Name& nameSpace,
60 const IdentityCertificate& identityCertificate,
61 const Name& signerName,
62 const IntroType& introType)
63 : m_introType(introType)
64{
65 m_keyName = identityCertificate.getPublicKeyName();
66
67 Name certificateName = nameSpace;
68 certificateName.append("WOT").append(m_keyName).append("INTRO-CERT").append(signerName);
69 switch(introType)
70 {
71 case PRODUCER:
72 certificateName.append("PRODUCER");
73 break;
74 case INTRODUCER:
75 certificateName.append("INTRODUCER");
76 break;
77 default:
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080078 throw Error("Wrong Introduction Type!");
Yingdi Yu43e71612013-10-30 22:19:31 -070079 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080080 posix_time::time_duration now = posix_time::microsec_clock::universal_time () - posix_time::ptime(gregorian::date (1970, boost::gregorian::Jan, 1));
81 uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244));
82 certificateName.appendVersion(version);
Yingdi Yu43e71612013-10-30 22:19:31 -070083
84 setName(certificateName);
85 setNotBefore(identityCertificate.getNotBefore());
86 setNotAfter(identityCertificate.getNotAfter());
87 setPublicKeyInfo(identityCertificate.getPublicKeyInfo());
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080088 addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri()));
Yingdi Yu43e71612013-10-30 22:19:31 -070089}
90
91SyncIntroCertificate::SyncIntroCertificate (const Data& data)
92 : Certificate(data)
93{
94 Name certificateName = getName();
95 int i = 0;
96 int keyNameStart = 0;
97 int keyNameEnd = 0;
98 for(; i < certificateName.size(); i++)
99 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800100 if(certificateName.get(i).toEscapedString() == string("WOT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700101 {
102 keyNameStart = i + 1;
103 break;
104 }
105 }
106
107 if(i >= certificateName.size())
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800108 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700109
110 for(; i< certificateName.size(); i++)
111 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800112 if(certificateName.get(i).toEscapedString() == string("INTRO-CERT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700113 {
114 keyNameEnd = i;
115 break;
116 }
117 }
118
119 if(i >= certificateName.size())
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800120 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700121
Yingdi Yu7bfcd652013-11-12 13:15:33 -0800122 m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd - keyNameStart);
Yingdi Yu43e71612013-10-30 22:19:31 -0700123
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800124 string typeComponent = certificateName.get(certificateName.size() - 2).toEscapedString();
Yingdi Yu43e71612013-10-30 22:19:31 -0700125 if(typeComponent == string("PRODUCER"))
126 m_introType = PRODUCER;
127 else if(typeComponent == string("INTRODUCER"))
128 m_introType = INTRODUCER;
129 else
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800130 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700131}
132
133SyncIntroCertificate::SyncIntroCertificate (const SyncIntroCertificate& chronosIntroCertificate)
134 : Certificate(chronosIntroCertificate)
135 , m_keyName(chronosIntroCertificate.m_keyName)
136 , m_introType(chronosIntroCertificate.m_introType)
137{}
138
139Data &
140SyncIntroCertificate::setName (const Name& certificateName)
141{
142 int i = 0;
143 int keyNameStart = 0;
144 int keyNameEnd = 0;
145 for(; i < certificateName.size(); i++)
146 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800147 if(certificateName.get(i).toEscapedString() == string("WOT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700148 {
149 keyNameStart = i + 1;
150 break;
151 }
152 }
153
154 if(i >= certificateName.size())
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800155 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700156
157 for(; i< certificateName.size(); i++)
158 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800159 if(certificateName.get(i).toEscapedString() == string("INTRO-CERT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700160 {
161 keyNameEnd = i;
162 break;
163 }
164 }
165
166 if(i >= certificateName.size())
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800167 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700168
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800169 m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd - keyNameStart);
Yingdi Yu43e71612013-10-30 22:19:31 -0700170
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800171 string typeComponent = certificateName.get(certificateName.size() - 2).toEscapedString();
Yingdi Yu43e71612013-10-30 22:19:31 -0700172 if(typeComponent == string("PRODUCER"))
173 m_introType = PRODUCER;
174 else if(typeComponent == string("INTRODUCER"))
175 m_introType = INTRODUCER;
176 else
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800177 throw Error("Wrong SyncIntroCertificate Name!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700178
179 return *this;
180}
181
182bool
183SyncIntroCertificate::isSyncIntroCertificate(const Certificate& certificate)
184{
185 const Name& certificateName = certificate.getName();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800186 string introType = certificateName.get(certificateName.size() - 2).toEscapedString();
Yingdi Yu43e71612013-10-30 22:19:31 -0700187 if(introType != string("PRODUCER") && introType != string("INTRODUCER"))
188 return false;
189
190 int i = 0;
191 bool findWot = false;
192 bool findIntroCert = false;
193 for(; i < certificateName.size(); i++)
194 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800195 if(certificateName.get(i).toEscapedString() == string("WOT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700196 {
197 findWot = true;
198 break;
199 }
200 }
201
202 if(!findWot)
203 return false;
204
205 for(; i < certificateName.size(); i++)
206 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800207 if(certificateName.get(i).toEscapedString() == string("INTRO-CERT"))
Yingdi Yu43e71612013-10-30 22:19:31 -0700208 {
209 findIntroCert = true;
210 break;
211 }
212 }
213 if(!findIntroCert)
214 return false;
215
216 if(i < certificateName.size() - 2)
217 return true;
218
219 return false;
220}