Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -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 | b8f8b34 | 2015-04-27 11:06:42 -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 "identity.hpp" |
| 23 | #include "pib-impl.hpp" |
| 24 | #include "pib.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 28 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 29 | |
| 30 | Identity::Identity() |
| 31 | : m_hasDefaultKey(false) |
| 32 | , m_needRefreshKeys(false) |
| 33 | , m_impl(nullptr) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Identity::Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit) |
| 38 | : m_name(identityName) |
| 39 | , m_hasDefaultKey(false) |
| 40 | , m_needRefreshKeys(true) |
| 41 | , m_impl(impl) |
| 42 | { |
| 43 | validityCheck(); |
| 44 | |
| 45 | if (needInit) |
| 46 | m_impl->addIdentity(m_name); |
| 47 | else if (!m_impl->hasIdentity(m_name)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 48 | BOOST_THROW_EXCEPTION(Pib::Error("Identity: " + m_name.toUri() + " does not exist")); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | const Name& |
| 52 | Identity::getName() const |
| 53 | { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 54 | return m_name; |
| 55 | } |
| 56 | |
| 57 | Key |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 58 | Identity::addKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 59 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 60 | if (m_name != v2::extractIdentityFromKeyName(keyName)) { |
| 61 | BOOST_THROW_EXCEPTION(Pib::Error("Key name `" + keyName.toUri() + "` does not match identity " |
| 62 | "`" + m_name.toUri() + "`")); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 65 | // if we have already loaded all the keys, but the new key is not one of them the |
| 66 | // KeyContainer should be refreshed |
| 67 | m_needRefreshKeys = m_needRefreshKeys || m_keys.find(keyName) == m_keys.end(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 69 | return Key(keyName, key, keyLen, m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 73 | Identity::removeKey(const Name& keyName) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 74 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 75 | if (m_name != v2::extractIdentityFromKeyName(keyName)) { |
| 76 | BOOST_THROW_EXCEPTION(Pib::Error("Key name `" + keyName.toUri() + "` does not match identity " |
| 77 | "`" + m_name.toUri() + "`")); |
| 78 | } |
| 79 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 80 | validityCheck(); |
| 81 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 82 | if (m_hasDefaultKey && m_defaultKey.getName() == keyName) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 83 | m_hasDefaultKey = false; |
| 84 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 85 | m_impl->removeKey(keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 86 | m_needRefreshKeys = true; |
| 87 | } |
| 88 | |
| 89 | Key |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 90 | Identity::getKey(const Name& keyName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 91 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 92 | return Key(keyName, m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | const KeyContainer& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 96 | Identity::getKeys() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 97 | { |
| 98 | validityCheck(); |
| 99 | |
| 100 | if (m_needRefreshKeys) { |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 101 | m_keys = KeyContainer(m_name, m_impl->getKeysOfIdentity(m_name), m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 102 | m_needRefreshKeys = false; |
| 103 | } |
| 104 | |
| 105 | return m_keys; |
| 106 | } |
| 107 | |
| 108 | Key& |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 109 | Identity::setDefaultKey(const Name& keyName) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 110 | { |
| 111 | validityCheck(); |
| 112 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 113 | m_defaultKey = Key(keyName, m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 114 | m_hasDefaultKey = true; |
| 115 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 116 | m_impl->setDefaultKeyOfIdentity(m_name, keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 117 | return m_defaultKey; |
| 118 | } |
| 119 | |
| 120 | Key& |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 121 | Identity::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 122 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 123 | validityCheck(); |
| 124 | |
| 125 | addKey(key, keyLen, keyName); |
| 126 | return setDefaultKey(keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Key& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 130 | Identity::getDefaultKey() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 131 | { |
| 132 | validityCheck(); |
| 133 | |
| 134 | if (!m_hasDefaultKey) { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 135 | m_defaultKey = Key(m_impl->getDefaultKeyOfIdentity(m_name), m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 136 | m_hasDefaultKey = true; |
| 137 | } |
| 138 | |
| 139 | return m_defaultKey; |
| 140 | } |
| 141 | |
| 142 | Identity::operator bool() const |
| 143 | { |
| 144 | return !(this->operator!()); |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | Identity::operator!() const |
| 149 | { |
| 150 | return (m_impl == nullptr); |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | Identity::validityCheck() const |
| 155 | { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 156 | if (m_impl == nullptr) { |
| 157 | BOOST_THROW_EXCEPTION(std::domain_error("Invalid identity instance")); |
| 158 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 161 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 162 | } // namespace security |
| 163 | } // namespace ndn |