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