blob: 51f368cfff53ce7ba0f87d12318cc09d179fdaad [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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 "key.hpp"
23#include "pib-impl.hpp"
24#include "pib.hpp"
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070025#include "../v2/certificate.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026
27namespace ndn {
28namespace security {
29
30Key::Key()
31 : m_hasDefaultCertificate(false)
32 , m_needRefreshCerts(false)
33 , m_impl(nullptr)
34{
35}
36
37Key::Key(const Name& identityName, const name::Component& keyId,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070038 const v1::PublicKey& publicKey, shared_ptr<PibImpl> impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039 : m_id(identityName)
40 , m_keyId(keyId)
41 , m_key(publicKey)
42 , m_hasDefaultCertificate(false)
43 , m_needRefreshCerts(true)
44 , m_impl(impl)
45{
46 validityCheck();
47
48 m_keyName = m_id;
49 m_keyName.append(m_keyId);
50
51 m_impl->addIdentity(m_id);
52 m_impl->addKey(m_id, m_keyId, publicKey);
53}
54
55Key::Key(const Name& identityName, const name::Component& keyId,
56 shared_ptr<PibImpl> impl)
57 : m_id(identityName)
58 , m_keyId(keyId)
59 , m_hasDefaultCertificate(false)
60 , m_needRefreshCerts(true)
61 , m_impl(impl)
62{
63 validityCheck();
64
65 m_keyName = m_id;
66 m_keyName.append(m_keyId);
67
68 m_key = m_impl->getKeyBits(m_id, m_keyId);
69}
70
71const Name&
72Key::getName() const
73{
74 validityCheck();
75
76 return m_keyName;
77}
78
79const Name&
80Key::getIdentity() const
81{
82 validityCheck();
83
84 return m_id;
85}
86
87const name::Component&
88Key::getKeyId() const
89{
90 validityCheck();
91
92 return m_keyId;
93}
94
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070095const v1::PublicKey&
Yingdi Yub8f8b342015-04-27 11:06:42 -070096Key::getPublicKey() const
97{
98 validityCheck();
99
100 return m_key;
101}
102
103void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700104Key::addCertificate(const v1::IdentityCertificate& certificate)
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105{
106 validityCheck();
107
108 if (!m_needRefreshCerts &&
109 m_certificates.find(certificate.getName()) == m_certificates.end()) {
110 // if we have already loaded all the certificate, but the new certificate is not one of them
111 // the CertificateContainer should be refreshed
112 m_needRefreshCerts = true;
113 }
114
115 m_impl->addCertificate(certificate);
116}
117
118void
119Key::removeCertificate(const Name& certName)
120{
121 validityCheck();
122
123 if (m_hasDefaultCertificate && m_defaultCertificate.getName() == certName)
124 m_hasDefaultCertificate = false;
125
126 m_impl->removeCertificate(certName);
127 m_needRefreshCerts = true;
128}
129
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700130v1::IdentityCertificate
Yingdi Yuc8209892015-06-19 17:47:56 -0700131Key::getCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700132{
133 validityCheck();
134
135 return m_impl->getCertificate(certName);
136}
137
Yingdi Yuc8209892015-06-19 17:47:56 -0700138const CertificateContainer&
139Key::getCertificates() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700140{
141 validityCheck();
142
143 if (m_needRefreshCerts) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800144 m_certificates = CertificateContainer(m_impl->getCertificatesOfKey(m_id, m_keyId), m_impl);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145 m_needRefreshCerts = false;
146 }
147
148 return m_certificates;
149}
150
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700151const v1::IdentityCertificate&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152Key::setDefaultCertificate(const Name& certName)
153{
154 validityCheck();
155
156 m_defaultCertificate = m_impl->getCertificate(certName);
157 m_impl->setDefaultCertificateOfKey(m_id, m_keyId, certName);
158 m_hasDefaultCertificate = true;
159 return m_defaultCertificate;
160}
161
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700162const v1::IdentityCertificate&
163Key::setDefaultCertificate(const v1::IdentityCertificate& certificate)
Yingdi Yub8f8b342015-04-27 11:06:42 -0700164{
165 addCertificate(certificate);
166 return setDefaultCertificate(certificate.getName());
167}
168
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700169const v1::IdentityCertificate&
Yingdi Yuc8209892015-06-19 17:47:56 -0700170Key::getDefaultCertificate() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171{
172 validityCheck();
173
174 if (!m_hasDefaultCertificate) {
175 m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_id, m_keyId);
176 m_hasDefaultCertificate = true;
177 }
178
179 return m_defaultCertificate;
180}
181
182Key::operator bool() const
183{
184 return !(this->operator!());
185}
186
187bool
188Key::operator!() const
189{
190 return (m_impl == nullptr);
191}
192
193void
194Key::validityCheck() const
195{
196 if (m_impl == nullptr)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700197 BOOST_THROW_EXCEPTION(std::domain_error("Invalid Key instance"));
Yingdi Yub8f8b342015-04-27 11:06:42 -0700198}
199
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700200namespace v2 {
201
202Name
203constructKeyName(const Name& identity, const name::Component& keyId)
204{
205 Name keyName = identity;
206 keyName
207 .append(Certificate::KEY_COMPONENT)
208 .append(keyId);
209 return keyName;
210}
211
212} // namespace v2
213
Yingdi Yub8f8b342015-04-27 11:06:42 -0700214} // namespace security
215} // namespace ndn