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 "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 
73 v2::Certificate
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&
92 Key::setDefaultCertificate(const v2::Certificate& certificate) const
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 !(this->operator!());
106 }
107 
108 bool
110 {
111  return m_impl.expired();
112 }
113 
114 shared_ptr<detail::KeyImpl>
115 Key::lock() const
116 {
117  auto impl = m_impl.lock();
118 
119  if (impl == nullptr) {
120  BOOST_THROW_EXCEPTION(std::domain_error("Invalid key instance"));
121  }
122 
123  return impl;
124 }
125 
126 } // namespace pib
127 
128 namespace v2 {
129 
130 Name
131 constructKeyName(const Name& identity, const name::Component& keyId)
132 {
133  Name keyName = identity;
134  keyName
136  .append(keyId);
137  return keyName;
138 }
139 
140 bool
141 isValidKeyName(const Name& keyName)
142 {
143  return (keyName.size() > Certificate::MIN_KEY_NAME_LENGTH &&
145 }
146 
147 Name
149 {
150  if (!isValidKeyName(keyName)) {
151  BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` "
152  "does not follow the naming conventions"));
153  }
154 
155  return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
156 }
157 
158 } // namespace v2
159 
160 } // namespace security
161 } // namespace ndn
const Name & getName() const
Definition: key.cpp:38
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
The certificate following the certificate format naming convention.
static const size_t MIN_KEY_NAME_LENGTH
bool isValidKeyName(const Name &keyName)
Check if keyName follow the naming conventions for the key name.
Definition: key.cpp:141
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:171
Container of certificates of a key.
const CertificateContainer & getCertificates() const
Get all certificates for this key.
Definition: key.cpp:80
KeyType getKeyType() const
Get key type.
Definition: key.cpp:50
size_t size() const
Get the number of components.
Definition: name.hpp:400
v2::Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key.cpp:74
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const Buffer & getPublicKey() const
Get public key bits.
Definition: key.cpp:56
const v2::Certificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key.cpp:98
Component holds a read-only name component value.
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:131
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:140
static const name::Component KEY_COMPONENT
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
const Name & getIdentity() const
Get the name of the belonging identity.
Definition: key.cpp:44
bool operator!() const
Check if the Key instance is invalid.
Definition: key.cpp:109
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:148
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:44
Key()
Default Constructor.
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:411