blob: dce4908ae05b8e20a2cfe76c2e5a8124eec59f01 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc3dfc242017-09-14 20:18:48 -04002/*
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu7036ce22014-06-19 18:53:37 -07004 *
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 Yu7036ce22014-06-19 18:53:37 -070025#include "security-common.hpp"
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040026#include "../name-component.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070027
28namespace ndn {
29
30/**
31 * @brief Base class of key parameters.
32 *
33 * Its subclasses are used to store parameters for key generation.
34 */
35class KeyParams
36{
37public:
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 Yuc08d7d62015-07-16 21:05:11 -070049 ~KeyParams();
Yingdi Yu7036ce22014-06-19 18:53:37 -070050
51 KeyType
52 getKeyType() const
53 {
54 return m_keyType;
55 }
56
Yingdi Yuc08d7d62015-07-16 21:05:11 -070057 KeyIdType
58 getKeyIdType() const
Yingdi Yu7036ce22014-06-19 18:53:37 -070059 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -070060 return m_keyIdType;
Yingdi Yu7036ce22014-06-19 18:53:37 -070061 }
62
Yingdi Yuc08d7d62015-07-16 21:05:11 -070063 const name::Component&
64 getKeyId() const
65 {
66 return m_keyId;
67 }
68
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040069 void
70 setKeyId(const name::Component& keyId)
71 {
72 m_keyId = keyId;
73 }
74
Yingdi Yuc08d7d62015-07-16 21:05:11 -070075protected:
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 Pesaventoc3dfc242017-09-14 20:18:48 -040081 * KeyIdType::USER_SPECIFIED
Yingdi Yuc08d7d62015-07-16 21:05:11 -070082 */
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 Yu7036ce22014-06-19 18:53:37 -070095private:
96 KeyType m_keyType;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070097 KeyIdType m_keyIdType;
98 name::Component m_keyId;
Yingdi Yu7036ce22014-06-19 18:53:37 -070099};
100
101
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400102namespace detail {
103
104/// @brief RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700105class RsaKeyParamsInfo
106{
107public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400108 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700109 getType()
110 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700111 return KeyType::RSA;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700112 }
113
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700114 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400115 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700116 *
117 * @throw KeyParams::Error if the key size is not supported.
118 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700119 static uint32_t
120 checkKeySize(uint32_t size);
121
122 static uint32_t
123 getDefaultSize();
124};
125
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400126/// @brief EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700127class EcKeyParamsInfo
Yingdi Yu7036ce22014-06-19 18:53:37 -0700128{
129public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400130 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700131 getType()
132 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700133 return KeyType::EC;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700134 }
135
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700136 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400137 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700138 *
139 * @throw KeyParams::Error if the key size is not supported.
140 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700141 static uint32_t
142 checkKeySize(uint32_t size);
143
144 static uint32_t
145 getDefaultSize();
146};
147
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400148} // namespace detail
149
Yingdi Yu7036ce22014-06-19 18:53:37 -0700150
151/// @brief SimplePublicKeyParams is a template for public keys with only one parameter: size.
152template<typename KeyParamsInfo>
153class SimplePublicKeyParams : public KeyParams
154{
155public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700156 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700157 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700158 SimplePublicKeyParams(const name::Component& keyId,
159 uint32_t size = KeyParamsInfo::getDefaultSize())
160 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700161 {
162 setKeySize(size);
163 }
164
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700165 /**
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 Yu7036ce22014-06-19 18:53:37 -0700171 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700172 SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
173 KeyIdType keyIdType = KeyIdType::RANDOM)
174 : KeyParams(KeyParamsInfo::getType(), keyIdType)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700175 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700176 setKeySize(size);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700177 }
178
179 uint32_t
180 getKeySize() const
181 {
182 return m_size;
183 }
184
185private:
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
198private:
199 uint32_t m_size;
200};
201
202/// @brief RsaKeyParams carries parameters for RSA key.
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400203typedef SimplePublicKeyParams<detail::RsaKeyParamsInfo> RsaKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700204
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700205/// @brief EcKeyParams carries parameters for EC key.
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400206typedef SimplePublicKeyParams<detail::EcKeyParamsInfo> EcKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700207
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400208
209namespace detail {
210
211/// @brief AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700212class AesKeyParamsInfo
213{
214public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400215 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700216 getType()
217 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700218 return KeyType::AES;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700219 }
220
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700221 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400222 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700223 *
224 * @return KeyParams::Error if the key size is not supported.
225 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700226 static uint32_t
227 checkKeySize(uint32_t size);
228
229 static uint32_t
230 getDefaultSize();
231};
232
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400233} // namespace detail
234
235
Yingdi Yu7036ce22014-06-19 18:53:37 -0700236/// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
237template<typename KeyParamsInfo>
238class SimpleSymmetricKeyParams : public KeyParams
239{
240public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700241 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700242 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700243 SimpleSymmetricKeyParams(const name::Component& keyId,
244 uint32_t size = KeyParamsInfo::getDefaultSize())
245 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700246 {
247 setKeySize(size);
248 }
249
Alexander Afanasyev1709aa72017-03-08 10:16:40 -0800250 /**
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 Yu7036ce22014-06-19 18:53:37 -0700264 uint32_t
265 getKeySize() const
266 {
267 return m_size;
268 }
269
270private:
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
283private:
284 uint32_t m_size;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700285};
286
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400287/// @brief AesKeyParams carries parameters for AES key.
288typedef SimpleSymmetricKeyParams<detail::AesKeyParamsInfo> AesKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700289
290} // namespace ndn
291
292#endif // NDN_SECURITY_KEY_PARAMS_HPP