blob: 11cad880279d8c10d25d35e92ef884e2e7249a80 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
26namespace ndn {
27namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070028namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
30Identity::Identity()
31 : m_hasDefaultKey(false)
32 , m_needRefreshKeys(false)
33 , m_impl(nullptr)
34{
35}
36
37Identity::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 Mastorakis0d2ed2e2015-07-27 19:09:12 -070048 BOOST_THROW_EXCEPTION(Pib::Error("Identity: " + m_name.toUri() + " does not exist"));
Yingdi Yub8f8b342015-04-27 11:06:42 -070049}
50
51const Name&
52Identity::getName() const
53{
Yingdi Yub8f8b342015-04-27 11:06:42 -070054 return m_name;
55}
56
57Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070058Identity::addKey(const uint8_t* key, size_t keyLen, const Name& keyName)
Yingdi Yub8f8b342015-04-27 11:06:42 -070059{
Yingdi Yu6ee2d362015-07-16 21:48:05 -070060 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 Yub8f8b342015-04-27 11:06:42 -070063 }
64
Yingdi Yu6ee2d362015-07-16 21:48:05 -070065 // 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 Yub8f8b342015-04-27 11:06:42 -070068
Yingdi Yu6ee2d362015-07-16 21:48:05 -070069 return Key(keyName, key, keyLen, m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -070070}
71
72void
Yingdi Yu6ee2d362015-07-16 21:48:05 -070073Identity::removeKey(const Name& keyName)
Yingdi Yub8f8b342015-04-27 11:06:42 -070074{
Yingdi Yu6ee2d362015-07-16 21:48:05 -070075 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 Yub8f8b342015-04-27 11:06:42 -070080 validityCheck();
81
Yingdi Yu6ee2d362015-07-16 21:48:05 -070082 if (m_hasDefaultKey && m_defaultKey.getName() == keyName)
Yingdi Yub8f8b342015-04-27 11:06:42 -070083 m_hasDefaultKey = false;
84
Yingdi Yu6ee2d362015-07-16 21:48:05 -070085 m_impl->removeKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070086 m_needRefreshKeys = true;
87}
88
89Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070090Identity::getKey(const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070091{
Yingdi Yu6ee2d362015-07-16 21:48:05 -070092 return Key(keyName, m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -070093}
94
95const KeyContainer&
Yingdi Yuc8209892015-06-19 17:47:56 -070096Identity::getKeys() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070097{
98 validityCheck();
99
100 if (m_needRefreshKeys) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800101 m_keys = KeyContainer(m_name, m_impl->getKeysOfIdentity(m_name), m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700102 m_needRefreshKeys = false;
103 }
104
105 return m_keys;
106}
107
108Key&
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700109Identity::setDefaultKey(const Name& keyName)
Yingdi Yub8f8b342015-04-27 11:06:42 -0700110{
111 validityCheck();
112
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700113 m_defaultKey = Key(keyName, m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700114 m_hasDefaultKey = true;
115
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700116 m_impl->setDefaultKeyOfIdentity(m_name, keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700117 return m_defaultKey;
118}
119
120Key&
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700121Identity::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName)
Yingdi Yub8f8b342015-04-27 11:06:42 -0700122{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700123 validityCheck();
124
125 addKey(key, keyLen, keyName);
126 return setDefaultKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700127}
128
129Key&
Yingdi Yuc8209892015-06-19 17:47:56 -0700130Identity::getDefaultKey() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700131{
132 validityCheck();
133
134 if (!m_hasDefaultKey) {
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700135 m_defaultKey = Key(m_impl->getDefaultKeyOfIdentity(m_name), m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700136 m_hasDefaultKey = true;
137 }
138
139 return m_defaultKey;
140}
141
142Identity::operator bool() const
143{
144 return !(this->operator!());
145}
146
147bool
148Identity::operator!() const
149{
150 return (m_impl == nullptr);
151}
152
153void
154Identity::validityCheck() const
155{
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700156 if (m_impl == nullptr) {
157 BOOST_THROW_EXCEPTION(std::domain_error("Invalid identity instance"));
158 }
Yingdi Yub8f8b342015-04-27 11:06:42 -0700159}
160
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162} // namespace security
163} // namespace ndn