Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | |
| 125 | /// @brief EcdsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for ECDSA key. |
| 126 | class EcdsaKeyParamsInfo |
| 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 | |
| 202 | /// @brief EcdsaKeyParams carries parameters for ECDSA key. |
| 203 | typedef SimplePublicKeyParams<EcdsaKeyParamsInfo> EcdsaKeyParams; |
| 204 | |
| 205 | |
| 206 | /// @brief AesKeyParamsInfo is used to initialize a SimpleSymmetricKeyParams template for AES key. |
| 207 | class AesKeyParamsInfo |
| 208 | { |
| 209 | public: |
| 210 | static KeyType |
| 211 | getType() |
| 212 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 213 | return KeyType::AES; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame^] | 216 | /** |
| 217 | * @brief check if @p size is qualified. |
| 218 | * |
| 219 | * @return KeyParams::Error if the key size is not supported. |
| 220 | */ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 221 | static uint32_t |
| 222 | checkKeySize(uint32_t size); |
| 223 | |
| 224 | static uint32_t |
| 225 | getDefaultSize(); |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | /// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size. |
| 230 | template<typename KeyParamsInfo> |
| 231 | class SimpleSymmetricKeyParams : public KeyParams |
| 232 | { |
| 233 | public: |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame^] | 234 | /// @brief Create key parameter with user specified @p keyId. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 235 | explicit |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame^] | 236 | SimpleSymmetricKeyParams(const name::Component& keyId, |
| 237 | uint32_t size = KeyParamsInfo::getDefaultSize()) |
| 238 | : KeyParams(KeyParamsInfo::getType(), keyId) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 239 | { |
| 240 | setKeySize(size); |
| 241 | } |
| 242 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 243 | uint32_t |
| 244 | getKeySize() const |
| 245 | { |
| 246 | return m_size; |
| 247 | } |
| 248 | |
| 249 | private: |
| 250 | void |
| 251 | setKeySize(uint32_t size) |
| 252 | { |
| 253 | m_size = KeyParamsInfo::checkKeySize(size); |
| 254 | } |
| 255 | |
| 256 | uint32_t |
| 257 | getDefaultKeySize() const |
| 258 | { |
| 259 | return KeyParamsInfo::getDefaultSize(); |
| 260 | } |
| 261 | |
| 262 | private: |
| 263 | uint32_t m_size; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | typedef SimpleSymmetricKeyParams<AesKeyParamsInfo> AesKeyParams; |
| 267 | |
| 268 | } // namespace ndn |
| 269 | |
| 270 | #endif // NDN_SECURITY_KEY_PARAMS_HPP |