Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 12 | |
| 13 | #include <boost/date_time/posix_time/posix_time.hpp> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 14 | |
| 15 | using namespace ndn; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 16 | using namespace std; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 17 | using namespace boost; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 18 | |
| 19 | SyncIntroCertificate::SyncIntroCertificate () |
| 20 | : Certificate() |
| 21 | {} |
| 22 | |
| 23 | SyncIntroCertificate::SyncIntroCertificate (const Name& nameSpace, |
| 24 | const Name& keyName, |
| 25 | const Name& signerName, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 26 | const MillisecondsSince1970& notBefore, |
| 27 | const MillisecondsSince1970& notAfter, |
| 28 | const PublicKey& key, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 29 | 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 Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 44 | throw Error("Wrong Introduction Type!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 45 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 46 | |
| 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 Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 50 | |
Yingdi Yu | 8600a09 | 2013-11-01 16:12:31 -0700 | [diff] [blame] | 51 | Data::setName(certificateName); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 52 | setNotBefore(notBefore); |
| 53 | setNotAfter(notAfter); |
| 54 | setPublicKeyInfo(key); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 55 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); |
Yingdi Yu | 8600a09 | 2013-11-01 16:12:31 -0700 | [diff] [blame] | 56 | encode(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | SyncIntroCertificate::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 Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 78 | throw Error("Wrong Introduction Type!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 79 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 80 | 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 Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 83 | |
| 84 | setName(certificateName); |
| 85 | setNotBefore(identityCertificate.getNotBefore()); |
| 86 | setNotAfter(identityCertificate.getNotAfter()); |
| 87 | setPublicKeyInfo(identityCertificate.getPublicKeyInfo()); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 88 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | SyncIntroCertificate::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 Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 100 | if(certificateName.get(i).toEscapedString() == string("WOT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 101 | { |
| 102 | keyNameStart = i + 1; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if(i >= certificateName.size()) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 108 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 109 | |
| 110 | for(; i< certificateName.size(); i++) |
| 111 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 112 | if(certificateName.get(i).toEscapedString() == string("INTRO-CERT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 113 | { |
| 114 | keyNameEnd = i; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if(i >= certificateName.size()) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 120 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 121 | |
Yingdi Yu | 7bfcd65 | 2013-11-12 13:15:33 -0800 | [diff] [blame] | 122 | m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd - keyNameStart); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 123 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 124 | string typeComponent = certificateName.get(certificateName.size() - 2).toEscapedString(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 125 | if(typeComponent == string("PRODUCER")) |
| 126 | m_introType = PRODUCER; |
| 127 | else if(typeComponent == string("INTRODUCER")) |
| 128 | m_introType = INTRODUCER; |
| 129 | else |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 130 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | SyncIntroCertificate::SyncIntroCertificate (const SyncIntroCertificate& chronosIntroCertificate) |
| 134 | : Certificate(chronosIntroCertificate) |
| 135 | , m_keyName(chronosIntroCertificate.m_keyName) |
| 136 | , m_introType(chronosIntroCertificate.m_introType) |
| 137 | {} |
| 138 | |
| 139 | Data & |
| 140 | SyncIntroCertificate::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 Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 147 | if(certificateName.get(i).toEscapedString() == string("WOT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 148 | { |
| 149 | keyNameStart = i + 1; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if(i >= certificateName.size()) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 155 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 156 | |
| 157 | for(; i< certificateName.size(); i++) |
| 158 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 159 | if(certificateName.get(i).toEscapedString() == string("INTRO-CERT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 160 | { |
| 161 | keyNameEnd = i; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if(i >= certificateName.size()) |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 167 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 168 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 169 | m_keyName = certificateName.getSubName(keyNameStart, keyNameEnd - keyNameStart); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 170 | |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 171 | string typeComponent = certificateName.get(certificateName.size() - 2).toEscapedString(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 172 | if(typeComponent == string("PRODUCER")) |
| 173 | m_introType = PRODUCER; |
| 174 | else if(typeComponent == string("INTRODUCER")) |
| 175 | m_introType = INTRODUCER; |
| 176 | else |
Yingdi Yu | 0cb0f2b | 2014-01-09 13:51:16 -0800 | [diff] [blame] | 177 | throw Error("Wrong SyncIntroCertificate Name!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 178 | |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | bool |
| 183 | SyncIntroCertificate::isSyncIntroCertificate(const Certificate& certificate) |
| 184 | { |
| 185 | const Name& certificateName = certificate.getName(); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 186 | string introType = certificateName.get(certificateName.size() - 2).toEscapedString(); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 187 | 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 Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 195 | if(certificateName.get(i).toEscapedString() == string("WOT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 196 | { |
| 197 | findWot = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if(!findWot) |
| 203 | return false; |
| 204 | |
| 205 | for(; i < certificateName.size(); i++) |
| 206 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 207 | if(certificateName.get(i).toEscapedString() == string("INTRO-CERT")) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 208 | { |
| 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 | } |