NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
key.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "key.hpp"
23 #include "pib-impl.hpp"
24 #include "pib.hpp"
25 
26 namespace ndn {
27 namespace security {
28 
30  : m_hasDefaultCertificate(false)
31  , m_needRefreshCerts(false)
32  , m_impl(nullptr)
33 {
34 }
35 
36 Key::Key(const Name& identityName, const name::Component& keyId,
37  const PublicKey& publicKey, shared_ptr<PibImpl> impl)
38  : m_id(identityName)
39  , m_keyId(keyId)
40  , m_key(publicKey)
41  , m_hasDefaultCertificate(false)
42  , m_needRefreshCerts(true)
43  , m_impl(impl)
44 {
45  validityCheck();
46 
47  m_keyName = m_id;
48  m_keyName.append(m_keyId);
49 
50  m_impl->addIdentity(m_id);
51  m_impl->addKey(m_id, m_keyId, publicKey);
52 }
53 
54 Key::Key(const Name& identityName, const name::Component& keyId,
55  shared_ptr<PibImpl> impl)
56  : m_id(identityName)
57  , m_keyId(keyId)
58  , m_hasDefaultCertificate(false)
59  , m_needRefreshCerts(true)
60  , m_impl(impl)
61 {
62  validityCheck();
63 
64  m_keyName = m_id;
65  m_keyName.append(m_keyId);
66 
67  m_key = m_impl->getKeyBits(m_id, m_keyId);
68 }
69 
70 const Name&
71 Key::getName() const
72 {
73  validityCheck();
74 
75  return m_keyName;
76 }
77 
78 const Name&
80 {
81  validityCheck();
82 
83  return m_id;
84 }
85 
86 const name::Component&
88 {
89  validityCheck();
90 
91  return m_keyId;
92 }
93 
94 const PublicKey&
96 {
97  validityCheck();
98 
99  return m_key;
100 }
101 
102 void
103 Key::addCertificate(const IdentityCertificate& certificate)
104 {
105  validityCheck();
106 
107  if (!m_needRefreshCerts &&
108  m_certificates.find(certificate.getName()) == m_certificates.end()) {
109  // if we have already loaded all the certificate, but the new certificate is not one of them
110  // the CertificateContainer should be refreshed
111  m_needRefreshCerts = true;
112  }
113 
114  m_impl->addCertificate(certificate);
115 }
116 
117 void
118 Key::removeCertificate(const Name& certName)
119 {
120  validityCheck();
121 
122  if (m_hasDefaultCertificate && m_defaultCertificate.getName() == certName)
123  m_hasDefaultCertificate = false;
124 
125  m_impl->removeCertificate(certName);
126  m_needRefreshCerts = true;
127 }
128 
130 Key::getCertificate(const Name& certName) const
131 {
132  validityCheck();
133 
134  return m_impl->getCertificate(certName);
135 }
136 
139 {
140  validityCheck();
141 
142  if (m_needRefreshCerts) {
143  m_certificates = std::move(CertificateContainer(m_impl->getCertificatesOfKey(m_id, m_keyId),
144  m_impl));
145  m_needRefreshCerts = false;
146  }
147 
148  return m_certificates;
149 }
150 
151 const IdentityCertificate&
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 
162 const IdentityCertificate&
164 {
165  addCertificate(certificate);
166  return setDefaultCertificate(certificate.getName());
167 }
168 
169 const IdentityCertificate&
171 {
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 
182 Key::operator bool() const
183 {
184  return !(this->operator!());
185 }
186 
187 bool
189 {
190  return (m_impl == nullptr);
191 }
192 
193 void
195 {
196  if (m_impl == nullptr)
197  BOOST_THROW_EXCEPTION(std::domain_error("Invalid Key instance"));
198 }
199 
200 } // namespace security
201 } // namespace ndn
void validityCheck() const
Check the validity of this instance.
Definition: key.cpp:194
const Name & getIdentity() const
Get the name of the belonging identity.
Definition: key.cpp:79
Copyright (c) 2011-2015 Regents of the University of California.
IdentityCertificate getCertificate(const Name &certName) const
Get a certificate.
Definition: key.cpp:130
const IdentityCertificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key.cpp:170
A handler to search or enumerate certificates of a key.
Key()
Default Constructor.
Definition: key.cpp:29
const Name & getName() const
Get the name of the key.
Definition: key.cpp:71
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE const name::Component const PublicKey shared_ptr< PibImpl > impl
Definition: key.hpp:168
bool operator!() const
Check if the Key instance is invalid.
Definition: key.cpp:188
const CertificateContainer & getCertificates() const
Get all the certificates for this key.
Definition: key.cpp:138
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const IdentityCertificate & setDefaultCertificate(const Name &certName)
Set the default certificate.
Definition: key.cpp:152
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE const name::Component const PublicKey & publicKey
Definition: key.hpp:168
Component holds a read-only name component value.
const_iterator find(const Name &certName) const
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE const name::Component & keyId
Definition: key.hpp:168
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
const name::Component & getKeyId() const
Get the key id of the key.
Definition: key.cpp:87
void removeCertificate(const Name &certName)
Remove a certificate.
Definition: key.cpp:118
const PublicKey & getPublicKey() const
Get public key.
Definition: key.cpp:95