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); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 68 | if (identity == m_defaultIdentity) { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 69 | m_hasDefaultIdentity = false; |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 70 | m_defaultIdentity.clear(); |
| 71 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 73 | auto keyNames = getKeysOfIdentity(identity); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 74 | for (const Name& keyName : keyNames) { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 75 | removeKey(keyName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 79 | void |
| 80 | PibMemory::clearIdentities() |
| 81 | { |
| 82 | m_hasDefaultIdentity = false; |
| 83 | m_defaultIdentity.clear(); |
| 84 | m_identities.clear(); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 85 | m_defaultKeys.clear(); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 86 | m_keys.clear(); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 87 | m_defaultCerts.clear(); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 88 | m_certs.clear(); |
| 89 | } |
| 90 | |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 91 | std::set<Name> |
| 92 | PibMemory::getIdentities() const |
| 93 | { |
| 94 | return m_identities; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | PibMemory::setDefaultIdentity(const Name& identityName) |
| 99 | { |
| 100 | addIdentity(identityName); |
| 101 | m_defaultIdentity = identityName; |
| 102 | m_hasDefaultIdentity = true; |
| 103 | } |
| 104 | |
| 105 | Name |
| 106 | PibMemory::getDefaultIdentity() const |
| 107 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 108 | if (m_hasDefaultIdentity) { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 109 | return m_defaultIdentity; |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 110 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 111 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 112 | BOOST_THROW_EXCEPTION(Pib::Error("No default identity")); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 116 | PibMemory::hasKey(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 117 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 118 | return (m_keys.count(keyName) > 0); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 122 | PibMemory::addKey(const Name& identity, const Name& keyName, |
| 123 | const uint8_t* key, size_t keyLen) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 124 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 125 | addIdentity(identity); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 127 | m_keys[keyName] = Buffer(key, keyLen); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 128 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 129 | if (m_defaultKeys.count(identity) == 0) { |
| 130 | m_defaultKeys[identity] = keyName; |
| 131 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 135 | PibMemory::removeKey(const Name& keyName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 136 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 137 | Name identity = v2::extractIdentityFromKeyName(keyName); |
| 138 | |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 139 | m_keys.erase(keyName); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 140 | m_defaultKeys.erase(identity); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 141 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 142 | auto certNames = getCertificatesOfKey(keyName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 143 | for (const auto& certName : certNames) { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 144 | removeCertificate(certName); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 148 | Buffer |
| 149 | PibMemory::getKeyBits(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 150 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 151 | if (!hasKey(keyName)) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 152 | BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 153 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 155 | auto key = m_keys.find(keyName); |
| 156 | BOOST_ASSERT(key != m_keys.end()); |
| 157 | return key->second; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 160 | std::set<Name> |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 161 | PibMemory::getKeysOfIdentity(const Name& identity) const |
| 162 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 163 | std::set<Name> ids; |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 164 | for (const auto& key : m_keys) { |
| 165 | if (identity == v2::extractIdentityFromKeyName(key.first)) { |
| 166 | ids.insert(key.first); |
| 167 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 168 | } |
| 169 | return ids; |
| 170 | } |
| 171 | |
| 172 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 173 | PibMemory::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 174 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 175 | if (!hasKey(keyName)) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 176 | BOOST_THROW_EXCEPTION(Pib::Error("Key `" + keyName.toUri() + "` not found")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 177 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 178 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 179 | m_defaultKeys[identity] = keyName; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 182 | Name |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 183 | PibMemory::getDefaultKeyOfIdentity(const Name& identity) const |
| 184 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 185 | auto defaultKey = m_defaultKeys.find(identity); |
| 186 | if (defaultKey == m_defaultKeys.end()) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 187 | BOOST_THROW_EXCEPTION(Pib::Error("No default key for identity `" + identity.toUri() + "`")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 188 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 189 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 190 | return defaultKey->second; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool |
| 194 | PibMemory::hasCertificate(const Name& certName) const |
| 195 | { |
| 196 | return (m_certs.count(certName) > 0); |
| 197 | } |
| 198 | |
| 199 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 200 | PibMemory::addCertificate(const v2::Certificate& certificate) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 201 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 202 | Name certName = certificate.getName(); |
| 203 | Name keyName = certificate.getKeyName(); |
| 204 | Name identity = certificate.getIdentity(); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 205 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 206 | addKey(identity, keyName, certificate.getContent().value(), certificate.getContent().value_size()); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 207 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 208 | m_certs[certName] = certificate; |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 209 | if (m_defaultCerts.count(keyName) == 0) { |
| 210 | m_defaultCerts[keyName] = certName; |
| 211 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | PibMemory::removeCertificate(const Name& certName) |
| 216 | { |
| 217 | m_certs.erase(certName); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 218 | auto defaultCert = m_defaultCerts.find(v2::extractKeyNameFromCertName(certName)); |
| 219 | if (defaultCert != m_defaultCerts.end() && defaultCert->second == certName) { |
| 220 | m_defaultCerts.erase(defaultCert); |
| 221 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 224 | v2::Certificate |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 225 | PibMemory::getCertificate(const Name& certName) const |
| 226 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 227 | if (!hasCertificate(certName)) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 228 | BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 229 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 230 | |
| 231 | auto it = m_certs.find(certName); |
| 232 | return it->second; |
| 233 | } |
| 234 | |
| 235 | std::set<Name> |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 236 | PibMemory::getCertificatesOfKey(const Name& keyName) const |
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 | std::set<Name> certNames; |
| 239 | for (const auto& it : m_certs) { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 240 | if (v2::extractKeyNameFromCertName(it.second.getName()) == keyName) { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 241 | certNames.insert(it.first); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 242 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 243 | } |
| 244 | return certNames; |
| 245 | } |
| 246 | |
| 247 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 248 | PibMemory::setDefaultCertificateOfKey(const Name& keyName, const Name& certName) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 249 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 250 | if (!hasCertificate(certName)) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 251 | BOOST_THROW_EXCEPTION(Pib::Error("Certificate `" + certName.toUri() + "` does not exist")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 252 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 253 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 254 | m_defaultCerts[keyName] = certName; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 257 | v2::Certificate |
| 258 | PibMemory::getDefaultCertificateOfKey(const Name& keyName) const |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 259 | { |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 260 | auto it = m_defaultCerts.find(keyName); |
| 261 | if (it == m_defaultCerts.end()) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 262 | BOOST_THROW_EXCEPTION(Pib::Error("No default certificate for key `" + keyName.toUri() + "`")); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 263 | } |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 264 | |
| 265 | auto certIt = m_certs.find(it->second); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame^] | 266 | BOOST_ASSERT(certIt != m_certs.end()); |
| 267 | return certIt->second; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 270 | } // namespace pib |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 271 | } // namespace security |
| 272 | } // namespace ndn |