blob: 9b344cb08ac62fad04a0a0361d25d1411c8cdfe7 [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/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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:
Junxiao Shi68b53852018-07-25 13:56:38 -060041 using std::runtime_error::runtime_error;
Yingdi Yu7036ce22014-06-19 18:53:37 -070042 };
43
44 virtual
Yingdi Yuc08d7d62015-07-16 21:05:11 -070045 ~KeyParams();
Yingdi Yu7036ce22014-06-19 18:53:37 -070046
47 KeyType
48 getKeyType() const
49 {
50 return m_keyType;
51 }
52
Yingdi Yuc08d7d62015-07-16 21:05:11 -070053 KeyIdType
54 getKeyIdType() const
Yingdi Yu7036ce22014-06-19 18:53:37 -070055 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -070056 return m_keyIdType;
Yingdi Yu7036ce22014-06-19 18:53:37 -070057 }
58
Yingdi Yuc08d7d62015-07-16 21:05:11 -070059 const name::Component&
60 getKeyId() const
61 {
62 return m_keyId;
63 }
64
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040065 void
66 setKeyId(const name::Component& keyId)
67 {
68 m_keyId = keyId;
69 }
70
Yingdi Yuc08d7d62015-07-16 21:05:11 -070071protected:
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 Pesaventoc3dfc242017-09-14 20:18:48 -040077 * KeyIdType::USER_SPECIFIED
Yingdi Yuc08d7d62015-07-16 21:05:11 -070078 */
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 Yu7036ce22014-06-19 18:53:37 -070091private:
92 KeyType m_keyType;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070093 KeyIdType m_keyIdType;
94 name::Component m_keyId;
Yingdi Yu7036ce22014-06-19 18:53:37 -070095};
96
97
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040098namespace detail {
99
100/// @brief RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700101class RsaKeyParamsInfo
102{
103public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400104 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700105 getType()
106 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700107 return KeyType::RSA;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700108 }
109
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700110 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400111 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700112 *
113 * @throw KeyParams::Error if the key size is not supported.
114 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700115 static uint32_t
116 checkKeySize(uint32_t size);
117
118 static uint32_t
119 getDefaultSize();
120};
121
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400122/// @brief EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700123class EcKeyParamsInfo
Yingdi Yu7036ce22014-06-19 18:53:37 -0700124{
125public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400126 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700127 getType()
128 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700129 return KeyType::EC;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700130 }
131
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700132 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400133 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700134 *
135 * @throw KeyParams::Error if the key size is not supported.
136 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700137 static uint32_t
138 checkKeySize(uint32_t size);
139
140 static uint32_t
141 getDefaultSize();
142};
143
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400144} // namespace detail
145
Yingdi Yu7036ce22014-06-19 18:53:37 -0700146
147/// @brief SimplePublicKeyParams is a template for public keys with only one parameter: size.
148template<typename KeyParamsInfo>
149class SimplePublicKeyParams : public KeyParams
150{
151public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700152 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700153 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700154 SimplePublicKeyParams(const name::Component& keyId,
155 uint32_t size = KeyParamsInfo::getDefaultSize())
156 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700157 {
158 setKeySize(size);
159 }
160
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700161 /**
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 Yu7036ce22014-06-19 18:53:37 -0700167 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700168 SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
169 KeyIdType keyIdType = KeyIdType::RANDOM)
170 : KeyParams(KeyParamsInfo::getType(), keyIdType)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700171 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700172 setKeySize(size);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700173 }
174
175 uint32_t
176 getKeySize() const
177 {
178 return m_size;
179 }
180
181private:
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
194private:
195 uint32_t m_size;
196};
197
198/// @brief RsaKeyParams carries parameters for RSA key.
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400199typedef SimplePublicKeyParams<detail::RsaKeyParamsInfo> RsaKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700200
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700201/// @brief EcKeyParams carries parameters for EC key.
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400202typedef SimplePublicKeyParams<detail::EcKeyParamsInfo> EcKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700203
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400204
205namespace detail {
206
207/// @brief AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700208class AesKeyParamsInfo
209{
210public:
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400211 static constexpr KeyType
Yingdi Yu7036ce22014-06-19 18:53:37 -0700212 getType()
213 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700214 return KeyType::AES;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700215 }
216
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700217 /**
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400218 * @brief check if @p size is valid and supported for this key type.
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700219 *
220 * @return KeyParams::Error if the key size is not supported.
221 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700222 static uint32_t
223 checkKeySize(uint32_t size);
224
225 static uint32_t
226 getDefaultSize();
227};
228
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400229} // namespace detail
230
231
Yingdi Yu7036ce22014-06-19 18:53:37 -0700232/// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
233template<typename KeyParamsInfo>
234class SimpleSymmetricKeyParams : public KeyParams
235{
236public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700237 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700238 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700239 SimpleSymmetricKeyParams(const name::Component& keyId,
240 uint32_t size = KeyParamsInfo::getDefaultSize())
241 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700242 {
243 setKeySize(size);
244 }
245
Alexander Afanasyev1709aa72017-03-08 10:16:40 -0800246 /**
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 Yu7036ce22014-06-19 18:53:37 -0700260 uint32_t
261 getKeySize() const
262 {
263 return m_size;
264 }
265
266private:
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
279private:
280 uint32_t m_size;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700281};
282
Davide Pesaventoc3dfc242017-09-14 20:18:48 -0400283/// @brief AesKeyParams carries parameters for AES key.
284typedef SimpleSymmetricKeyParams<detail::AesKeyParamsInfo> AesKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700285
286} // namespace ndn
287
288#endif // NDN_SECURITY_KEY_PARAMS_HPP