NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2017 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 "key.hpp"
23 #include "detail/key-impl.hpp"
24 #include "../v2/certificate.hpp"
25 
26 namespace ndn {
27 namespace security {
28 namespace pib {
29 
30 Key::Key() = default;
31 
32 Key::Key(weak_ptr<detail::KeyImpl> impl)
33  : m_impl(impl)
34 {
35 }
36 
37 const Name&
38 Key::getName() const
39 {
40  return lock()->getName();
41 }
42 
43 const Name&
45 {
46  return lock()->getIdentity();
47 }
48 
49 KeyType
51 {
52  return lock()->getKeyType();
53 }
54 
55 const Buffer&
57 {
58  return lock()->getPublicKey();
59 }
60 
61 void
62 Key::addCertificate(const v2::Certificate& certificate) const
63 {
64  return lock()->addCertificate(certificate);
65 }
66 
67 void
68 Key::removeCertificate(const Name& certName) const
69 {
70  return lock()->removeCertificate(certName);
71 }
72 
74 Key::getCertificate(const Name& certName) const
75 {
76  return lock()->getCertificate(certName);
77 }
78 
81 {
82  return lock()->getCertificates();
83 }
84 
85 const v2::Certificate&
86 Key::setDefaultCertificate(const Name& certName) const
87 {
88  return lock()->setDefaultCertificate(certName);
89 }
90 
91 const v2::Certificate&
93 {
94  return lock()->setDefaultCertificate(certificate);
95 }
96 
97 const v2::Certificate&
99 {
100  return lock()->getDefaultCertificate();
101 }
102 
103 Key::operator bool() const
104 {
105  return !m_impl.expired();
106 }
107 
108 shared_ptr<detail::KeyImpl>
109 Key::lock() const
110 {
111  auto impl = m_impl.lock();
112 
113  if (impl == nullptr) {
114  BOOST_THROW_EXCEPTION(std::domain_error("Invalid key instance"));
115  }
116 
117  return impl;
118 }
119 
120 bool
121 operator!=(const Key& lhs, const Key& rhs)
122 {
123  return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
124 }
125 
126 std::ostream&
127 operator<<(std::ostream& os, const Key& key)
128 {
129  if (key) {
130  os << key.getName();
131  }
132  else {
133  os << "(empty)";
134  }
135  return os;
136 }
137 
138 } // namespace pib
139 
140 namespace v2 {
141 
142 Name
143 constructKeyName(const Name& identity, const name::Component& keyId)
144 {
145  Name keyName = identity;
146  keyName
148  .append(keyId);
149  return keyName;
150 }
151 
152 bool
153 isValidKeyName(const Name& keyName)
154 {
155  return (keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH &&
157 }
158 
159 Name
161 {
162  if (!isValidKeyName(keyName)) {
163  BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` "
164  "does not follow the naming conventions"));
165  }
166 
167  return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
168 }
169 
170 } // namespace v2
171 
172 } // namespace security
173 } // namespace ndn
std::ostream & operator<<(std::ostream &os, const Identity &id)
Definition: identity.cpp:107
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:210
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:117
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
static const size_t MIN_KEY_NAME_LENGTH
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:164
KeyType getKeyType() const
Get key type.
Definition: key.cpp:50
Name & append(const Component &component)
Append a component.
Definition: name.hpp:256
bool isValidKeyName(const Name &keyName)
Check if keyName follow the naming conventions for the key name.
Definition: key.cpp:153
KeyType
The type of a cryptographic key.
Container of certificates of a key.
A frontend handle of a key instance.
Definition: key.hpp:49
Represents an absolute name.
Definition: name.hpp:42
const Name & getName() const
Get key name.
Definition: key.cpp:38
size_t size() const
Get number of components.
Definition: name.hpp:154
const v2::Certificate & setDefaultCertificate(const Name &certName) const
Set an existing certificate with certName as the default certificate.
Definition: key.cpp:86
Represents a name component.
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:143
const v2::Certificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key.cpp:98
bool operator!=(const Identity &lhs, const Identity &rhs)
Definition: identity.cpp:101
static const name::Component KEY_COMPONENT
const Buffer & getPublicKey() const
Get public key bits.
Definition: key.cpp:56
v2::Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key.cpp:74
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
void removeCertificate(const Name &certName) const
Remove a certificate with certName.
Definition: key.cpp:68
Key()
Default Constructor.
const Name & getIdentity() const
Get the name of the belonging identity.
Definition: key.cpp:44
const CertificateContainer & getCertificates() const
Get all certificates for this key.
Definition: key.cpp:80