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