security: Add interface to request different key id generation
The option for key id include timestamp, random, and user-specified
value.
This commit only adds the interface, the support to honor the interface
will be implemented later.
Change-Id: I476381ff0fc56cd7906392cb7482b7458e386781
Refs: #2948
diff --git a/src/security/key-params.hpp b/src/security/key-params.hpp
index 9769270..8f28a3e 100644
--- a/src/security/key-params.hpp
+++ b/src/security/key-params.hpp
@@ -23,6 +23,7 @@
#define NDN_SECURITY_KEY_PARAMS_HPP
#include "../common.hpp"
+#include "../name-component.hpp"
#include "security-common.hpp"
namespace ndn {
@@ -46,9 +47,7 @@
};
virtual
- ~KeyParams()
- {
- }
+ ~KeyParams();
KeyType
getKeyType() const
@@ -56,15 +55,48 @@
return m_keyType;
}
-protected:
- explicit
- KeyParams(KeyType keyType)
- : m_keyType(keyType)
+ KeyIdType
+ getKeyIdType() const
{
+ return m_keyIdType;
}
+ void
+ setKeyId(const name::Component& keyId)
+ {
+ m_keyId = keyId;
+ }
+
+ const name::Component&
+ getKeyId() const
+ {
+ return m_keyId;
+ }
+
+protected:
+ /**
+ * @brief Create a key generation parameter
+ *
+ * @param keyType Type of the created key
+ * @param keyIdType The method how the key id should be generated; must not be
+ KeyIdType::USER_SPECIFIED
+ */
+ KeyParams(KeyType keyType, KeyIdType keyIdType);
+
+ /**
+ * @brief Create a key generation parameter
+ *
+ * @param keyType Type of the created key
+ * @param keyId The user-specified key id. The keyIdType will be set to KeyIdType::USER_SPECIFIED.
+ * keyId MUST NOT be the empty component.
+ * @post getKeyIdType() == KeyIdType::USER_SPECIFIED
+ */
+ KeyParams(KeyType keyType, const name::Component& keyId);
+
private:
KeyType m_keyType;
+ KeyIdType m_keyIdType;
+ name::Component m_keyId;
};
@@ -78,7 +110,11 @@
return KeyType::RSA;
}
- /// @brief check if size is qualified, otherwise return the default key size.
+ /**
+ * @brief check if @p size is qualified.
+ *
+ * @throw KeyParams::Error if the key size is not supported.
+ */
static uint32_t
checkKeySize(uint32_t size);
@@ -96,7 +132,11 @@
return KeyType::EC;
}
- /// @brief check if size is qualified, otherwise return the default key size.
+ /**
+ * @brief check if @p size is qualified.
+ *
+ * @throw KeyParams::Error if the key size is not supported.
+ */
static uint32_t
checkKeySize(uint32_t size);
@@ -110,25 +150,27 @@
class SimplePublicKeyParams : public KeyParams
{
public:
+ /// @brief Create key parameter with user specified @p keyId.
explicit
- SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize())
- : KeyParams(KeyParamsInfo::getType())
+ SimplePublicKeyParams(const name::Component& keyId,
+ uint32_t size = KeyParamsInfo::getDefaultSize())
+ : KeyParams(KeyParamsInfo::getType(), keyId)
{
setKeySize(size);
}
+ /**
+ * @brief Create key parameter with auto-created keyId.
+ *
+ * This method is used only if user does not want to maintain the uniqueness of key name.
+ * By default, an 8-byte random number will be used as the key Id.
+ */
explicit
- SimplePublicKeyParams(const SimplePublicKeyParams& params)
- : KeyParams(params)
- , m_size(params.m_size)
+ SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
+ KeyIdType keyIdType = KeyIdType::RANDOM)
+ : KeyParams(KeyParamsInfo::getType(), keyIdType)
{
- }
-
- explicit
- SimplePublicKeyParams(const KeyParams& params)
- : KeyParams(params.getKeyType())
- {
- BOOST_THROW_EXCEPTION(KeyParams::Error("Incorrect key parameters (incompatible key type)"));
+ setKeySize(size);
}
uint32_t
@@ -171,7 +213,11 @@
return KeyType::AES;
}
- /// @brief check if size is qualified, otherwise return the default key size.
+ /**
+ * @brief check if @p size is qualified.
+ *
+ * @return KeyParams::Error if the key size is not supported.
+ */
static uint32_t
checkKeySize(uint32_t size);
@@ -185,26 +231,15 @@
class SimpleSymmetricKeyParams : public KeyParams
{
public:
+ /// @brief Create key parameter with user specified @p keyId.
explicit
- SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize())
- : KeyParams(KeyParamsInfo::getType())
+ SimpleSymmetricKeyParams(const name::Component& keyId,
+ uint32_t size = KeyParamsInfo::getDefaultSize())
+ : KeyParams(KeyParamsInfo::getType(), keyId)
{
setKeySize(size);
}
- explicit
- SimpleSymmetricKeyParams(const SimpleSymmetricKeyParams& params)
- : KeyParams(params)
- , m_size(params.m_size)
- {
- }
-
- explicit
- SimpleSymmetricKeyParams(const KeyParams& params)
- {
- BOOST_THROW_EXCEPTION(KeyParams::Error("Incorrect key parameters (incompatible key type)"));
- }
-
uint32_t
getKeySize() const
{
@@ -226,7 +261,6 @@
private:
uint32_t m_size;
-
};
typedef SimpleSymmetricKeyParams<AesKeyParamsInfo> AesKeyParams;