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