Yingdi Yu | adadb71 | 2015-04-15 17:23:56 -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 "in-memory-pib-impl.hpp" |
| 23 | #include "pib.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | |
| 28 | InMemoryPibImpl::InMemoryPibImpl() |
| 29 | : m_hasDefaultIdentity(false) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | InMemoryPibImpl::setTpmLocator(const std::string& tpmLocator) |
| 35 | { |
| 36 | throw Error("InMemoryPib/Tpm does not need a locator"); |
| 37 | } |
| 38 | |
| 39 | std::string |
| 40 | InMemoryPibImpl::getTpmLocator() const |
| 41 | { |
| 42 | return "tpm-memory:"; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | InMemoryPibImpl::hasIdentity(const Name& identity) const |
| 47 | { |
| 48 | return (m_identities.count(identity) > 0); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | InMemoryPibImpl::addIdentity(const Name& identity) |
| 53 | { |
| 54 | m_identities.insert(identity); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | InMemoryPibImpl::removeIdentity(const Name& identity) |
| 59 | { |
| 60 | m_identities.erase(identity); |
| 61 | if (identity == m_defaultIdentity) |
| 62 | m_hasDefaultIdentity = false; |
| 63 | } |
| 64 | |
| 65 | std::set<Name> |
| 66 | InMemoryPibImpl::getIdentities() const |
| 67 | { |
| 68 | return m_identities; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | InMemoryPibImpl::setDefaultIdentity(const Name& identityName) |
| 73 | { |
| 74 | addIdentity(identityName); |
| 75 | m_defaultIdentity = identityName; |
| 76 | m_hasDefaultIdentity = true; |
| 77 | } |
| 78 | |
| 79 | Name |
| 80 | InMemoryPibImpl::getDefaultIdentity() const |
| 81 | { |
| 82 | if (m_hasDefaultIdentity) |
| 83 | return m_defaultIdentity; |
| 84 | |
| 85 | throw Pib::Error("No default identity"); |
| 86 | } |
| 87 | |
| 88 | bool |
| 89 | InMemoryPibImpl::hasKey(const Name& identity, const name::Component& keyId) const |
| 90 | { |
| 91 | return (m_keys.count(getKeyName(identity, keyId)) > 0); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | InMemoryPibImpl::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey) |
| 96 | { |
| 97 | m_keys[getKeyName(identity, keyId)] = publicKey; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | InMemoryPibImpl::removeKey(const Name& identity, const name::Component& keyId) |
| 102 | { |
| 103 | m_keys.erase(getKeyName(identity, keyId)); |
| 104 | } |
| 105 | |
| 106 | PublicKey |
| 107 | InMemoryPibImpl::getKeyBits(const Name& identity, const name::Component& keyId) const |
| 108 | { |
| 109 | auto it = m_keys.find(getKeyName(identity, keyId)); |
| 110 | if (it == m_keys.end()) |
| 111 | throw Pib::Error("No default certificate"); |
| 112 | |
| 113 | return it->second; |
| 114 | } |
| 115 | |
| 116 | std::set<name::Component> |
| 117 | InMemoryPibImpl::getKeysOfIdentity(const Name& identity) const |
| 118 | { |
| 119 | std::set<name::Component> ids; |
| 120 | for (const auto& it : m_keys) { |
| 121 | if (identity == it.first.getPrefix(-1)) |
| 122 | ids.insert(it.first.get(-1)); |
| 123 | } |
| 124 | return ids; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | InMemoryPibImpl::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId) |
| 129 | { |
| 130 | Name keyName = getKeyName(identity, keyId); |
| 131 | |
| 132 | if (!hasKey(identity, keyId)) |
| 133 | throw Pib::Error("No key"); |
| 134 | |
| 135 | m_defaultKey[identity] = keyName; |
| 136 | } |
| 137 | |
| 138 | name::Component |
| 139 | InMemoryPibImpl::getDefaultKeyOfIdentity(const Name& identity) const |
| 140 | { |
| 141 | auto it = m_defaultKey.find(identity); |
| 142 | if (it == m_defaultKey.end()) |
| 143 | throw Pib::Error("No default key"); |
| 144 | |
| 145 | return it->second.get(-1); |
| 146 | } |
| 147 | |
| 148 | Name |
| 149 | InMemoryPibImpl::getKeyName(const Name& identity, const name::Component& keyId) const |
| 150 | { |
| 151 | Name keyName = identity; |
| 152 | keyName.append(keyId); |
| 153 | return keyName; |
| 154 | } |
| 155 | |
| 156 | bool |
| 157 | InMemoryPibImpl::hasCertificate(const Name& certName) const |
| 158 | { |
| 159 | return (m_certs.count(certName) > 0); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | InMemoryPibImpl::addCertificate(const IdentityCertificate& certificate) |
| 164 | { |
| 165 | m_certs[certificate.getName()] = certificate; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | InMemoryPibImpl::removeCertificate(const Name& certName) |
| 170 | { |
| 171 | m_certs.erase(certName); |
| 172 | } |
| 173 | |
| 174 | IdentityCertificate |
| 175 | InMemoryPibImpl::getCertificate(const Name& certName) const |
| 176 | { |
| 177 | auto it = m_certs.find(certName); |
| 178 | if (it == m_certs.end()) |
| 179 | throw Pib::Error("No cert"); |
| 180 | |
| 181 | return it->second; |
| 182 | } |
| 183 | |
| 184 | std::set<Name> |
| 185 | InMemoryPibImpl::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const |
| 186 | { |
| 187 | Name keyName = getKeyName(identity, keyId); |
| 188 | |
| 189 | std::set<Name> certNames; |
| 190 | for (const auto& it : m_certs) { |
| 191 | if (it.second.getPublicKeyName() == keyName) |
| 192 | certNames.insert(it.first); |
| 193 | } |
| 194 | return certNames; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | InMemoryPibImpl::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName) |
| 199 | { |
| 200 | if (!hasCertificate(certName)) |
| 201 | throw Pib::Error("No cert"); |
| 202 | |
| 203 | Name keyName = getKeyName(identity, keyId); |
| 204 | m_defaultCert[keyName] = certName; |
| 205 | } |
| 206 | |
| 207 | IdentityCertificate |
| 208 | InMemoryPibImpl::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const |
| 209 | { |
| 210 | Name keyName = getKeyName(identity, keyId); |
| 211 | |
| 212 | auto it = m_defaultCert.find(keyName); |
| 213 | if (it == m_defaultCert.end()) |
| 214 | throw Pib::Error("No default certificate"); |
| 215 | |
| 216 | auto certIt = m_certs.find(it->second); |
| 217 | if (certIt == m_certs.end()) |
| 218 | throw Pib::Error("No default certificate"); |
| 219 | else |
| 220 | return certIt->second; |
| 221 | } |
| 222 | |
| 223 | } // namespace security |
| 224 | } // namespace ndn |