key-impl.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "key-impl.hpp"
23 #include "../pib-impl.hpp"
24 #include "../pib.hpp"
25 #include "../../transform/public-key.hpp"
26 
27 namespace ndn {
28 namespace security {
29 namespace pib {
30 namespace detail {
31 
32 KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl)
33  : m_identity(v2::extractIdentityFromKeyName(keyName))
34  , m_keyName(keyName)
35  , m_key(key, keyLen)
36  , m_isDefaultCertificateLoaded(false)
37  , m_certificates(keyName, impl)
38  , m_impl(impl)
39 {
40  BOOST_ASSERT(impl != nullptr);
41 
42  if (m_impl->hasKey(m_keyName)) {
43  BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing key " + m_keyName.toUri()));
44  }
45 
46  transform::PublicKey publicKey;
47  try {
48  publicKey.loadPkcs8(key, keyLen);
49  }
51  BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid key bits"));
52  }
53  m_keyType = publicKey.getKeyType();
54 
55  m_impl->addKey(m_identity, m_keyName, key, keyLen);
56 }
57 
58 KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl)
59  : m_identity(v2::extractIdentityFromKeyName(keyName))
60  , m_keyName(keyName)
61  , m_isDefaultCertificateLoaded(false)
62  , m_certificates(keyName, impl)
63  , m_impl(impl)
64 {
65  BOOST_ASSERT(impl != nullptr);
66 
67  m_key = m_impl->getKeyBits(m_keyName);
68 
70  key.loadPkcs8(m_key.buf(), m_key.size());
71  m_keyType = key.getKeyType();
72 }
73 
74 void
76 {
77  BOOST_ASSERT(m_certificates.isConsistent());
78 
79  if (m_certificates.find(certificate.getName()) != m_certificates.end()) {
80  BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing certificate " + certificate.getName().toUri()));
81  }
82 
83  m_certificates.add(certificate);
84 }
85 
86 void
88 {
89  BOOST_ASSERT(m_certificates.isConsistent());
90 
91  if (m_isDefaultCertificateLoaded && m_defaultCertificate.getName() == certName)
92  m_isDefaultCertificateLoaded = false;
93 
94  m_certificates.remove(certName);
95 }
96 
98 KeyImpl::getCertificate(const Name& certName) const
99 {
100  BOOST_ASSERT(m_certificates.isConsistent());
101 
102  return m_certificates.get(certName);
103 }
104 
107 {
108  BOOST_ASSERT(m_certificates.isConsistent());
109 
110  return m_certificates;
111 }
112 
113 const v2::Certificate&
115 {
116  BOOST_ASSERT(m_certificates.isConsistent());
117 
118  m_defaultCertificate = m_certificates.get(certName);
119  m_impl->setDefaultCertificateOfKey(m_keyName, certName);
120  m_isDefaultCertificateLoaded = true;
121  return m_defaultCertificate;
122 }
123 
124 const v2::Certificate&
126 {
127  addCertificate(certificate);
128  return setDefaultCertificate(certificate.getName());
129 }
130 
131 const v2::Certificate&
133 {
134  BOOST_ASSERT(m_certificates.isConsistent());
135 
136  if (!m_isDefaultCertificateLoaded) {
137  m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_keyName);
138  m_isDefaultCertificateLoaded = true;
139  }
140 
141  BOOST_ASSERT(m_impl->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
142 
143  return m_defaultCertificate;
144 }
145 
146 } // namespace detail
147 } // namespace pib
148 } // namespace security
149 } // namespace ndn
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
KeyImpl(const Name &keyName, const uint8_t *key, size_t keyLen, shared_ptr< PibImpl > impl)
Create a KeyImpl with keyName.
Definition: key-impl.cpp:32
The certificate following the certificate format naming convention.
represents a semantic error
Definition: pib.hpp:56
void remove(const Name &certName)
Remove a certificate with certName from the container.
Abstraction of public key in crypto transformation.
const v2::Certificate & setDefaultCertificate(const Name &certName)
Set an existing one with certName as the default certificate.
Definition: key-impl.cpp:114
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:171
const CertificateContainer & getCertificates() const
Get all the certificates for this key.
Definition: key-impl.cpp:106
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
Container of certificates of a key.
uint8_t * buf()
Definition: buffer.hpp:87
const_iterator find(const Name &certName) const
bool isConsistent() const
Check if the container is consistent with the backend storage.
void loadPkcs8(const uint8_t *buf, size_t size)
Load the public key in PKCS#8 format from a buffer buf.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
void removeCertificate(const Name &certName)
Remove a certificate with certName.
Definition: key-impl.cpp:87
void add(const v2::Certificate &certificate)
Add certificate into the container.
v2::Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key-impl.cpp:98
v2::Certificate get(const Name &certName) const
Get a certificate with certName from the container.
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:148
void addCertificate(const v2::Certificate &certificate)
Add certificate.
Definition: key-impl.cpp:75
const v2::Certificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key-impl.cpp:132
KeyType getKeyType() const
Get the type of the public key.