Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 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 | #ifndef NDN_SECURITY_KEY_PARAMS_HPP |
| 23 | #define NDN_SECURITY_KEY_PARAMS_HPP |
| 24 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 25 | #include "security-common.hpp" |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 26 | #include "../name-component.hpp" |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
| 30 | /** |
| 31 | * @brief Base class of key parameters. |
| 32 | * |
| 33 | * Its subclasses are used to store parameters for key generation. |
| 34 | */ |
| 35 | class KeyParams |
| 36 | { |
| 37 | public: |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 41 | using std::runtime_error::runtime_error; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | virtual |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 45 | ~KeyParams(); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 46 | |
| 47 | KeyType |
| 48 | getKeyType() const |
| 49 | { |
| 50 | return m_keyType; |
| 51 | } |
| 52 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 53 | KeyIdType |
| 54 | getKeyIdType() const |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 55 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 56 | return m_keyIdType; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 59 | const name::Component& |
| 60 | getKeyId() const |
| 61 | { |
| 62 | return m_keyId; |
| 63 | } |
| 64 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 65 | void |
| 66 | setKeyId(const name::Component& keyId) |
| 67 | { |
| 68 | m_keyId = keyId; |
| 69 | } |
| 70 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 71 | protected: |
| 72 | /** |
| 73 | * @brief Create a key generation parameter |
| 74 | * |
| 75 | * @param keyType Type of the created key |
| 76 | * @param keyIdType The method how the key id should be generated; must not be |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 77 | * KeyIdType::USER_SPECIFIED |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 78 | */ |
| 79 | KeyParams(KeyType keyType, KeyIdType keyIdType); |
| 80 | |
| 81 | /** |
| 82 | * @brief Create a key generation parameter |
| 83 | * |
| 84 | * @param keyType Type of the created key |
| 85 | * @param keyId The user-specified key id. The keyIdType will be set to KeyIdType::USER_SPECIFIED. |
| 86 | * keyId MUST NOT be the empty component. |
| 87 | * @post getKeyIdType() == KeyIdType::USER_SPECIFIED |
| 88 | */ |
| 89 | KeyParams(KeyType keyType, const name::Component& keyId); |
| 90 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 91 | private: |
| 92 | KeyType m_keyType; |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 93 | KeyIdType m_keyIdType; |
| 94 | name::Component m_keyId; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 98 | namespace detail { |
| 99 | |
| 100 | /// @brief RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 101 | class RsaKeyParamsInfo |
| 102 | { |
| 103 | public: |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 104 | static constexpr KeyType |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 105 | getType() |
| 106 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 107 | return KeyType::RSA; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 110 | /** |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 111 | * @brief check if @p size is valid and supported for this key type. |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 112 | * |
| 113 | * @throw KeyParams::Error if the key size is not supported. |
| 114 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 115 | static uint32_t |
| 116 | checkKeySize(uint32_t size); |
| 117 | |
| 118 | static uint32_t |
| 119 | getDefaultSize(); |
| 120 | }; |
| 121 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 122 | /// @brief EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys. |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 123 | class EcKeyParamsInfo |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 124 | { |
| 125 | public: |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 126 | static constexpr KeyType |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 127 | getType() |
| 128 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 129 | return KeyType::EC; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 132 | /** |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 133 | * @brief check if @p size is valid and supported for this key type. |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 134 | * |
| 135 | * @throw KeyParams::Error if the key size is not supported. |
| 136 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 137 | static uint32_t |
| 138 | checkKeySize(uint32_t size); |
| 139 | |
| 140 | static uint32_t |
| 141 | getDefaultSize(); |
| 142 | }; |
| 143 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 144 | } // namespace detail |
| 145 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 146 | |
| 147 | /// @brief SimplePublicKeyParams is a template for public keys with only one parameter: size. |
| 148 | template<typename KeyParamsInfo> |
| 149 | class SimplePublicKeyParams : public KeyParams |
| 150 | { |
| 151 | public: |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 152 | /// @brief Create key parameter with user specified @p keyId. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 153 | explicit |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 154 | SimplePublicKeyParams(const name::Component& keyId, |
| 155 | uint32_t size = KeyParamsInfo::getDefaultSize()) |
| 156 | : KeyParams(KeyParamsInfo::getType(), keyId) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 157 | { |
| 158 | setKeySize(size); |
| 159 | } |
| 160 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 161 | /** |
| 162 | * @brief Create key parameter with auto-created keyId. |
| 163 | * |
| 164 | * This method is used only if user does not want to maintain the uniqueness of key name. |
| 165 | * By default, an 8-byte random number will be used as the key Id. |
| 166 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 167 | explicit |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 168 | SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(), |
| 169 | KeyIdType keyIdType = KeyIdType::RANDOM) |
| 170 | : KeyParams(KeyParamsInfo::getType(), keyIdType) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 171 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 172 | setKeySize(size); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | uint32_t |
| 176 | getKeySize() const |
| 177 | { |
| 178 | return m_size; |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | void |
| 183 | setKeySize(uint32_t size) |
| 184 | { |
| 185 | m_size = KeyParamsInfo::checkKeySize(size); |
| 186 | } |
| 187 | |
| 188 | uint32_t |
| 189 | getDefaultKeySize() const |
| 190 | { |
| 191 | return KeyParamsInfo::getDefaultSize(); |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | uint32_t m_size; |
| 196 | }; |
| 197 | |
| 198 | /// @brief RsaKeyParams carries parameters for RSA key. |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 199 | typedef SimplePublicKeyParams<detail::RsaKeyParamsInfo> RsaKeyParams; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 200 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 201 | /// @brief EcKeyParams carries parameters for EC key. |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 202 | typedef SimplePublicKeyParams<detail::EcKeyParamsInfo> EcKeyParams; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 203 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 204 | |
| 205 | namespace detail { |
| 206 | |
| 207 | /// @brief AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 208 | class AesKeyParamsInfo |
| 209 | { |
| 210 | public: |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 211 | static constexpr KeyType |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 212 | getType() |
| 213 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 214 | return KeyType::AES; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 217 | /** |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 218 | * @brief check if @p size is valid and supported for this key type. |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 219 | * |
| 220 | * @return KeyParams::Error if the key size is not supported. |
| 221 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 222 | static uint32_t |
| 223 | checkKeySize(uint32_t size); |
| 224 | |
| 225 | static uint32_t |
| 226 | getDefaultSize(); |
| 227 | }; |
| 228 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 229 | } // namespace detail |
| 230 | |
| 231 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 232 | /// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size. |
| 233 | template<typename KeyParamsInfo> |
| 234 | class SimpleSymmetricKeyParams : public KeyParams |
| 235 | { |
| 236 | public: |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 237 | /// @brief Create key parameter with user specified @p keyId. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 238 | explicit |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 239 | SimpleSymmetricKeyParams(const name::Component& keyId, |
| 240 | uint32_t size = KeyParamsInfo::getDefaultSize()) |
| 241 | : KeyParams(KeyParamsInfo::getType(), keyId) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 242 | { |
| 243 | setKeySize(size); |
| 244 | } |
| 245 | |
Alexander Afanasyev | 1709aa7 | 2017-03-08 10:16:40 -0800 | [diff] [blame] | 246 | /** |
| 247 | * @brief Create key parameter with auto-created keyId. |
| 248 | * |
| 249 | * This method is used only if user does not want to maintain the uniqueness of key name. |
| 250 | * By default, an 8-byte random number will be used as the key Id. |
| 251 | */ |
| 252 | explicit |
| 253 | SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(), |
| 254 | KeyIdType keyIdType = KeyIdType::RANDOM) |
| 255 | : KeyParams(KeyParamsInfo::getType(), keyIdType) |
| 256 | { |
| 257 | setKeySize(size); |
| 258 | } |
| 259 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 260 | uint32_t |
| 261 | getKeySize() const |
| 262 | { |
| 263 | return m_size; |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | void |
| 268 | setKeySize(uint32_t size) |
| 269 | { |
| 270 | m_size = KeyParamsInfo::checkKeySize(size); |
| 271 | } |
| 272 | |
| 273 | uint32_t |
| 274 | getDefaultKeySize() const |
| 275 | { |
| 276 | return KeyParamsInfo::getDefaultSize(); |
| 277 | } |
| 278 | |
| 279 | private: |
| 280 | uint32_t m_size; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame] | 283 | /// @brief AesKeyParams carries parameters for AES key. |
| 284 | typedef SimpleSymmetricKeyParams<detail::AesKeyParamsInfo> AesKeyParams; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 285 | |
| 286 | } // namespace ndn |
| 287 | |
| 288 | #endif // NDN_SECURITY_KEY_PARAMS_HPP |