Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 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" |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 24 | #include "../security-common.hpp" |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 28 | namespace pib { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 29 | |
| 30 | PibMemory::PibMemory() |
| 31 | : m_hasDefaultIdentity(false) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | PibMemory::setTpmLocator(const std::string& tpmLocator) |
| 37 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 38 | m_tpmLocator = tpmLocator; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | std::string |
| 42 | PibMemory::getTpmLocator() const |
| 43 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 44 | return m_tpmLocator; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool |
| 48 | PibMemory::hasIdentity(const Name& identity) const |
| 49 | { |
| 50 | return (m_identities.count(identity) > 0); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | PibMemory::addIdentity(const Name& identity) |
| 55 | { |
| 56 | m_identities.insert(identity); |
| 57 | |
| 58 | if (!m_hasDefaultIdentity) { |
| 59 | m_defaultIdentity = identity; |
| 60 | m_hasDefaultIdentity = true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | PibMemory::removeIdentity(const Name& identity) |
| 66 | { |
| 67 | m_identities.erase(identity); |
| 68 | if (identity == m_defaultIdentity) |
| 69 | m_hasDefaultIdentity = false; |
| 70 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 71 | auto keyNames = this->getKeysOfIdentity(identity); |
| 72 | for (const Name& keyName : keyNames) { |
| 73 | this->removeKey(keyName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 77 | void |
| 78 | PibMemory::clearIdentities() |
| 79 | { |
| 80 | m_hasDefaultIdentity = false; |
| 81 | m_defaultIdentity.clear(); |
| 82 | m_identities.clear(); |
| 83 | m_defaultKey.clear(); |
| 84 | m_keys.clear(); |
| 85 | m_defaultCert.clear(); |
| 86 | m_certs.clear(); |
| 87 | } |
| 88 | |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 89 | std::set<Name> |
| 90 | PibMemory::getIdentities() const |
| 91 | { |
| 92 | return m_identities; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | PibMemory::setDefaultIdentity(const Name& identityName) |
| 97 | { |
| 98 | addIdentity(identityName); |
| 99 | m_defaultIdentity = identityName; |
| 100 | m_hasDefaultIdentity = true; |
| 101 | } |
| 102 | |
| 103 | Name |
| 104 | PibMemory::getDefaultIdentity() const |
| 105 | { |
| 106 | if (m_hasDefaultIdentity) |
| 107 | return m_defaultIdentity; |
| 108 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 109 | BOOST_THROW_EXCEPTION(Pib::Error("No default identity")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 113 | PibMemory::hasKey(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 114 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 115 | return (m_keys.count(keyName) > 0); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 119 | PibMemory::addKey(const Name& identity, const Name& keyName, |
| 120 | const uint8_t* key, size_t keyLen) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 121 | { |
| 122 | this->addIdentity(identity); |
| 123 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 124 | m_keys[keyName] = Buffer(key, keyLen); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 125 | |
| 126 | if (m_defaultKey.find(identity) == m_defaultKey.end()) |
| 127 | m_defaultKey[identity] = keyName; |
| 128 | } |
| 129 | |
| 130 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 131 | PibMemory::removeKey(const Name& keyName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 132 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 133 | Name identity = v2::extractIdentityFromKeyName(keyName); |
| 134 | |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 135 | m_keys.erase(keyName); |
| 136 | m_defaultKey.erase(identity); |
| 137 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 138 | auto certNames = this->getCertificatesOfKey(keyName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 139 | for (const auto& certName : certNames) { |
| 140 | this->removeCertificate(certName); |
| 141 | } |
| 142 | } |
| 143 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 144 | Buffer |
| 145 | PibMemory::getKeyBits(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 146 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 147 | if (!hasKey(keyName)) |
| 148 | BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 149 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 150 | auto it = m_keys.find(keyName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 151 | return it->second; |
| 152 | } |
| 153 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 154 | std::set<Name> |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 155 | PibMemory::getKeysOfIdentity(const Name& identity) const |
| 156 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 157 | std::set<Name> ids; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 158 | for (const auto& it : m_keys) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 159 | if (identity == v2::extractIdentityFromKeyName(it.first)) |
| 160 | ids.insert(it.first); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 161 | } |
| 162 | return ids; |
| 163 | } |
| 164 | |
| 165 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 166 | PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 167 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 168 | if (!hasKey(keyName)) |
| 169 | BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 170 | |
| 171 | m_defaultKey[identity] = keyName; |
| 172 | } |
| 173 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 174 | Name |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 175 | PibMemory::getDefaultKeyOfIdentity(const Name& identity) const |
| 176 | { |
| 177 | auto it = m_defaultKey.find(identity); |
| 178 | if (it == m_defaultKey.end()) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 179 | BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 180 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 181 | return it->second; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool |
| 185 | PibMemory::hasCertificate(const Name& certName) const |
| 186 | { |
| 187 | return (m_certs.count(certName) > 0); |
| 188 | } |
| 189 | |
| 190 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 191 | PibMemory::addCertificate(const v2::Certificate& certificate) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 192 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 193 | Name certName = certificate.getName(); |
| 194 | Name keyName = certificate.getKeyName(); |
| 195 | Name identity = certificate.getIdentity(); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 196 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 197 | this->addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size()); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 198 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 199 | m_certs[certName] = certificate; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 200 | if (m_defaultCert.find(keyName) == m_defaultCert.end()) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 201 | m_defaultCert[keyName] = certName; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void |
| 205 | PibMemory::removeCertificate(const Name& certName) |
| 206 | { |
| 207 | m_certs.erase(certName); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 208 | m_defaultCert.erase(v2::extractKeyNameFromCertName(certName)); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 211 | v2::Certificate |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 212 | PibMemory::getCertificate(const Name& certName) const |
| 213 | { |
| 214 | if (!hasCertificate(certName)) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 215 | BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 216 | |
| 217 | auto it = m_certs.find(certName); |
| 218 | return it->second; |
| 219 | } |
| 220 | |
| 221 | std::set<Name> |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 222 | PibMemory::getCertificatesOfKey(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 223 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 224 | std::set<Name> certNames; |
| 225 | for (const auto& it : m_certs) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 226 | if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 227 | certNames.insert(it.first); |
| 228 | } |
| 229 | return certNames; |
| 230 | } |
| 231 | |
| 232 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 233 | PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 234 | { |
| 235 | if (!hasCertificate(certName)) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 236 | BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 237 | |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 238 | m_defaultCert[keyName] = certName; |
| 239 | } |
| 240 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 241 | v2::Certificate |
| 242 | PibMemory::getDefaultCertificateOfKey(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 243 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 244 | auto it = m_defaultCert.find(keyName); |
| 245 | if (it == m_defaultCert.end()) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 246 | BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 247 | |
| 248 | auto certIt = m_certs.find(it->second); |
| 249 | if (certIt == m_certs.end()) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 250 | BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 251 | else |
| 252 | return certIt->second; |
| 253 | } |
| 254 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 255 | } // namespace pib |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 256 | } // namespace security |
| 257 | } // namespace ndn |