blob: bf9d2993af07d4f3eb3c40712e5f3b4df79a1a01 [file] [log] [blame]
Yingdi Yuadadb712015-04-15 17:23:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
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 "in-memory-pib-impl.hpp"
23#include "pib.hpp"
24
25namespace ndn {
26namespace security {
27
28InMemoryPibImpl::InMemoryPibImpl()
29 : m_hasDefaultIdentity(false)
30{
31}
32
33void
34InMemoryPibImpl::setTpmLocator(const std::string& tpmLocator)
35{
36 throw Error("InMemoryPib/Tpm does not need a locator");
37}
38
39std::string
40InMemoryPibImpl::getTpmLocator() const
41{
42 return "tpm-memory:";
43}
44
45bool
46InMemoryPibImpl::hasIdentity(const Name& identity) const
47{
48 return (m_identities.count(identity) > 0);
49}
50
51void
52InMemoryPibImpl::addIdentity(const Name& identity)
53{
54 m_identities.insert(identity);
55}
56
57void
58InMemoryPibImpl::removeIdentity(const Name& identity)
59{
60 m_identities.erase(identity);
61 if (identity == m_defaultIdentity)
62 m_hasDefaultIdentity = false;
63}
64
65std::set<Name>
66InMemoryPibImpl::getIdentities() const
67{
68 return m_identities;
69}
70
71void
72InMemoryPibImpl::setDefaultIdentity(const Name& identityName)
73{
74 addIdentity(identityName);
75 m_defaultIdentity = identityName;
76 m_hasDefaultIdentity = true;
77}
78
79Name
80InMemoryPibImpl::getDefaultIdentity() const
81{
82 if (m_hasDefaultIdentity)
83 return m_defaultIdentity;
84
85 throw Pib::Error("No default identity");
86}
87
88bool
89InMemoryPibImpl::hasKey(const Name& identity, const name::Component& keyId) const
90{
91 return (m_keys.count(getKeyName(identity, keyId)) > 0);
92}
93
94void
95InMemoryPibImpl::addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey)
96{
97 m_keys[getKeyName(identity, keyId)] = publicKey;
98}
99
100void
101InMemoryPibImpl::removeKey(const Name& identity, const name::Component& keyId)
102{
103 m_keys.erase(getKeyName(identity, keyId));
104}
105
106PublicKey
107InMemoryPibImpl::getKeyBits(const Name& identity, const name::Component& keyId) const
108{
109 auto it = m_keys.find(getKeyName(identity, keyId));
110 if (it == m_keys.end())
111 throw Pib::Error("No default certificate");
112
113 return it->second;
114}
115
116std::set<name::Component>
117InMemoryPibImpl::getKeysOfIdentity(const Name& identity) const
118{
119 std::set<name::Component> ids;
120 for (const auto& it : m_keys) {
121 if (identity == it.first.getPrefix(-1))
122 ids.insert(it.first.get(-1));
123 }
124 return ids;
125}
126
127void
128InMemoryPibImpl::setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId)
129{
130 Name keyName = getKeyName(identity, keyId);
131
132 if (!hasKey(identity, keyId))
133 throw Pib::Error("No key");
134
135 m_defaultKey[identity] = keyName;
136}
137
138name::Component
139InMemoryPibImpl::getDefaultKeyOfIdentity(const Name& identity) const
140{
141 auto it = m_defaultKey.find(identity);
142 if (it == m_defaultKey.end())
143 throw Pib::Error("No default key");
144
145 return it->second.get(-1);
146}
147
148Name
149InMemoryPibImpl::getKeyName(const Name& identity, const name::Component& keyId) const
150{
151 Name keyName = identity;
152 keyName.append(keyId);
153 return keyName;
154}
155
156bool
157InMemoryPibImpl::hasCertificate(const Name& certName) const
158{
159 return (m_certs.count(certName) > 0);
160}
161
162void
163InMemoryPibImpl::addCertificate(const IdentityCertificate& certificate)
164{
165 m_certs[certificate.getName()] = certificate;
166}
167
168void
169InMemoryPibImpl::removeCertificate(const Name& certName)
170{
171 m_certs.erase(certName);
172}
173
174IdentityCertificate
175InMemoryPibImpl::getCertificate(const Name& certName) const
176{
177 auto it = m_certs.find(certName);
178 if (it == m_certs.end())
179 throw Pib::Error("No cert");
180
181 return it->second;
182}
183
184std::set<Name>
185InMemoryPibImpl::getCertificatesOfKey(const Name& identity, const name::Component& keyId) const
186{
187 Name keyName = getKeyName(identity, keyId);
188
189 std::set<Name> certNames;
190 for (const auto& it : m_certs) {
191 if (it.second.getPublicKeyName() == keyName)
192 certNames.insert(it.first);
193 }
194 return certNames;
195}
196
197void
198InMemoryPibImpl::setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId, const Name& certName)
199{
200 if (!hasCertificate(certName))
201 throw Pib::Error("No cert");
202
203 Name keyName = getKeyName(identity, keyId);
204 m_defaultCert[keyName] = certName;
205}
206
207IdentityCertificate
208InMemoryPibImpl::getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const
209{
210 Name keyName = getKeyName(identity, keyId);
211
212 auto it = m_defaultCert.find(keyName);
213 if (it == m_defaultCert.end())
214 throw Pib::Error("No default certificate");
215
216 auto certIt = m_certs.find(it->second);
217 if (certIt == m_certs.end())
218 throw Pib::Error("No default certificate");
219 else
220 return certIt->second;
221}
222
223} // namespace security
224} // namespace ndn