security: Correct KeyChain::addCertificate semantics
The documentation of KeyChain::addCertificate was ambiguous. On the one
hand, it stated that the certificate with the same name will be
overwritten. On the other hand, it stated that it will thrown an
exception if certificate with the same name exists. This commit ensures
the former behavior, which is consistent with the old KeyChain.
Change-Id: I911f7c86c634caf260ecb9a5dbdf13b066f8711f
diff --git a/src/security/pib/detail/identity-impl.cpp b/src/security/pib/detail/identity-impl.cpp
index af01dc5..284f14c 100644
--- a/src/security/pib/detail/identity-impl.cpp
+++ b/src/security/pib/detail/identity-impl.cpp
@@ -49,10 +49,6 @@
{
BOOST_ASSERT(m_keys.isConsistent());
- if (m_keys.find(keyName) != m_keys.end()) {
- BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing key " + keyName.toUri()));
- }
-
return m_keys.add(key, keyLen, keyName);
}
diff --git a/src/security/pib/detail/identity-impl.hpp b/src/security/pib/detail/identity-impl.hpp
index b654348..ceff37d 100644
--- a/src/security/pib/detail/identity-impl.hpp
+++ b/src/security/pib/detail/identity-impl.hpp
@@ -64,10 +64,10 @@
* @brief Add a @p key of @p keyLen bytes with @p keyName (in PKCS#8 format).
*
* If no default key is set before, the new key will be set as the default key of the identity.
+ * If a key with the same name already exists, overwrite the key.
*
* @return the added key.
* @throw std::invalid_argument key name does not match identity
- * @throw Pib::Error a key with the same name already exists
*/
Key
addKey(const uint8_t* key, size_t keyLen, const Name& keyName);
diff --git a/src/security/pib/detail/key-impl.cpp b/src/security/pib/detail/key-impl.cpp
index 35a0de6..c0cd839 100644
--- a/src/security/pib/detail/key-impl.cpp
+++ b/src/security/pib/detail/key-impl.cpp
@@ -39,15 +39,11 @@
{
BOOST_ASSERT(impl != nullptr);
- if (m_impl->hasKey(m_keyName)) {
- BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing key " + m_keyName.toUri()));
- }
-
transform::PublicKey publicKey;
try {
publicKey.loadPkcs8(key, keyLen);
}
- catch (transform::PublicKey::Error&) {
+ catch (const transform::PublicKey::Error&) {
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid key bits"));
}
m_keyType = publicKey.getKeyType();
@@ -75,11 +71,6 @@
KeyImpl::addCertificate(const v2::Certificate& certificate)
{
BOOST_ASSERT(m_certificates.isConsistent());
-
- if (m_certificates.find(certificate.getName()) != m_certificates.end()) {
- BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing certificate " + certificate.getName().toUri()));
- }
-
m_certificates.add(certificate);
}
diff --git a/src/security/pib/detail/key-impl.hpp b/src/security/pib/detail/key-impl.hpp
index c05e77e..a7e683f 100644
--- a/src/security/pib/detail/key-impl.hpp
+++ b/src/security/pib/detail/key-impl.hpp
@@ -107,8 +107,10 @@
* If no default certificate is set before, the new certificate will be set as the default
* certificate of the key.
*
+ * If a certificate with the same name (without implicit digest) already exists, overwrite
+ * the certificate.
+ *
* @throw std::invalid_argument certificate name does not match key name
- * @throw Pib::Error a certificate with the same name already exists
*/
void
addCertificate(const v2::Certificate& certificate);