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