Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "pib-memory.hpp" |
| 23 | #include "pib.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | |
| 28 | PibMemory::PibMemory() |
| 29 | : m_hasDefaultIdentity(false) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | PibMemory::setTpmLocator(const std::string& tpmLocator) |
| 35 | { |
| 36 | throw Error("PibMemory does not need a locator"); |
| 37 | } |
| 38 | |
| 39 | std::string |
| 40 | PibMemory::getTpmLocator() const |
| 41 | { |
| 42 | return "tpm-memory:"; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | PibMemory::hasIdentity(const Name& identity) const |
| 47 | { |
| 48 | return (m_identities.count(identity) > 0); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | PibMemory::addIdentity(const Name& identity) |
| 53 | { |
| 54 | m_identities.insert(identity); |
| 55 | |
| 56 | if (!m_hasDefaultIdentity) { |
| 57 | m_defaultIdentity = identity; |
| 58 | m_hasDefaultIdentity = true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | PibMemory::removeIdentity(const Name& identity) |
| 64 | { |
| 65 | m_identities.erase(identity); |
| 66 | if (identity == m_defaultIdentity) |
| 67 | m_hasDefaultIdentity = false; |
| 68 | |
| 69 | auto keyIds = this->getKeysOfIdentity(identity); |
| 70 | for (const name::Component& keyId : keyIds) { |
| 71 | this->removeKey(identity, keyId); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::set<Name> |
| 76 | PibMemory::getIdentities() const |
| 77 | { |
| 78 | return m_identities; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | PibMemory::setDefaultIdentity(const Name& identityName) |
| 83 | { |
| 84 | addIdentity(identityName); |
| 85 | m_defaultIdentity = identityName; |
| 86 | m_hasDefaultIdentity = true; |
| 87 | } |
| 88 | |
| 89 | Name |
| 90 | PibMemory::getDefaultIdentity() const |
| 91 | { |
| 92 | if (m_hasDefaultIdentity) |
| 93 | return m_defaultIdentity; |
| 94 | |
| 95 | throw Pib::Error("No default identity"); |
| 96 | } |
| 97 | |
| 98 | bool |
| 99 | PibMemory::hasKey(const Name& identity, const name::Component& keyId) const |
| 100 | { |
| 101 | return (m_keys.count(getKeyName(identity, keyId)) > 0); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | PibMemory::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey) |
| 106 | { |
| 107 | this->addIdentity(identity); |
| 108 | |
| 109 | Name keyName = getKeyName(identity, keyId); |
| 110 | m_keys[keyName] = publicKey; |
| 111 | |
| 112 | if (m_defaultKey.find(identity) == m_defaultKey.end()) |
| 113 | m_defaultKey[identity] = keyName; |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | PibMemory::removeKey(const Name& identity, const name::Component& keyId) |
| 118 | { |
| 119 | Name keyName = getKeyName(identity, keyId); |
| 120 | m_keys.erase(keyName); |
| 121 | m_defaultKey.erase(identity); |
| 122 | |
| 123 | |
| 124 | auto certNames = this->getCertificatesOfKey(identity, keyId); |
| 125 | for (const auto& certName : certNames) { |
| 126 | this->removeCertificate(certName); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | PublicKey |
| 131 | PibMemory::getKeyBits(const Name& identity, const name::Component& keyId) const |
| 132 | { |
| 133 | if (!hasKey(identity, keyId)) |
| 134 | throw Pib::Error("No key"); |
| 135 | |
| 136 | auto it = m_keys.find(getKeyName(identity, keyId)); |
| 137 | return it->second; |
| 138 | } |
| 139 | |
| 140 | std::set<name::Component> |
| 141 | PibMemory::getKeysOfIdentity(const Name& identity) const |
| 142 | { |
| 143 | std::set<name::Component> ids; |
| 144 | for (const auto& it : m_keys) { |
| 145 | if (identity == it.first.getPrefix(-1)) |
| 146 | ids.insert(it.first.get(-1)); |
| 147 | } |
| 148 | return ids; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | PibMemory::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId) |
| 153 | { |
| 154 | Name keyName = getKeyName(identity, keyId); |
| 155 | |
| 156 | if (!hasKey(identity, keyId)) |
| 157 | throw Pib::Error("No key"); |
| 158 | |
| 159 | m_defaultKey[identity] = keyName; |
| 160 | } |
| 161 | |
| 162 | name::Component |
| 163 | PibMemory::getDefaultKeyOfIdentity(const Name& identity) const |
| 164 | { |
| 165 | auto it = m_defaultKey.find(identity); |
| 166 | if (it == m_defaultKey.end()) |
| 167 | throw Pib::Error("No default key"); |
| 168 | |
| 169 | return it->second.get(-1); |
| 170 | } |
| 171 | |
| 172 | Name |
| 173 | PibMemory::getKeyName(const Name& identity, const name::Component& keyId) const |
| 174 | { |
| 175 | Name keyName = identity; |
| 176 | keyName.append(keyId); |
| 177 | return keyName; |
| 178 | } |
| 179 | |
| 180 | bool |
| 181 | PibMemory::hasCertificate(const Name& certName) const |
| 182 | { |
| 183 | return (m_certs.count(certName) > 0); |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | PibMemory::addCertificate(const IdentityCertificate& certificate) |
| 188 | { |
| 189 | this->addKey(certificate.getPublicKeyName().getPrefix(-1), |
| 190 | certificate.getPublicKeyName().get(-1), |
| 191 | certificate.getPublicKeyInfo()); |
| 192 | |
| 193 | m_certs[certificate.getName()] = certificate; |
| 194 | |
| 195 | const Name& keyName = certificate.getPublicKeyName(); |
| 196 | if (m_defaultCert.find(keyName) == m_defaultCert.end()) |
| 197 | m_defaultCert[keyName] = certificate.getName(); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | PibMemory::removeCertificate(const Name& certName) |
| 202 | { |
| 203 | m_certs.erase(certName); |
| 204 | m_defaultCert.erase(IdentityCertificate::certificateNameToPublicKeyName(certName)); |
| 205 | } |
| 206 | |
| 207 | IdentityCertificate |
| 208 | PibMemory::getCertificate(const Name& certName) const |
| 209 | { |
| 210 | if (!hasCertificate(certName)) |
| 211 | throw Pib::Error("No cert"); |
| 212 | |
| 213 | auto it = m_certs.find(certName); |
| 214 | return it->second; |
| 215 | } |
| 216 | |
| 217 | std::set<Name> |
| 218 | PibMemory::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const |
| 219 | { |
| 220 | Name keyName = getKeyName(identity, keyId); |
| 221 | |
| 222 | std::set<Name> certNames; |
| 223 | for (const auto& it : m_certs) { |
| 224 | if (it.second.getPublicKeyName() == keyName) |
| 225 | certNames.insert(it.first); |
| 226 | } |
| 227 | return certNames; |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | PibMemory::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName) |
| 232 | { |
| 233 | if (!hasCertificate(certName)) |
| 234 | throw Pib::Error("No cert"); |
| 235 | |
| 236 | Name keyName = getKeyName(identity, keyId); |
| 237 | m_defaultCert[keyName] = certName; |
| 238 | } |
| 239 | |
| 240 | IdentityCertificate |
| 241 | PibMemory::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const |
| 242 | { |
| 243 | Name keyName = getKeyName(identity, keyId); |
| 244 | |
| 245 | auto it = m_defaultCert.find(keyName); |
| 246 | if (it == m_defaultCert.end()) |
| 247 | throw Pib::Error("No default certificate"); |
| 248 | |
| 249 | auto certIt = m_certs.find(it->second); |
| 250 | if (certIt == m_certs.end()) |
| 251 | throw Pib::Error("No default certificate"); |
| 252 | else |
| 253 | return certIt->second; |
| 254 | } |
| 255 | |
| 256 | } // namespace security |
| 257 | } // namespace ndn |