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