blob: 4c7d11809dabfa2356829b2a32f643708953e547 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Yufe4a9182014-06-26 12:56:11 -070025#include "../common.hpp"
Yingdi Yuc08d7d62015-07-16 21:05:11 -070026#include "../name-component.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070027#include "security-common.hpp"
28
29namespace ndn {
30
31/**
32 * @brief Base class of key parameters.
33 *
34 * Its subclasses are used to store parameters for key generation.
35 */
36class KeyParams
37{
38public:
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 Yuc08d7d62015-07-16 21:05:11 -070050 ~KeyParams();
Yingdi Yu7036ce22014-06-19 18:53:37 -070051
52 KeyType
53 getKeyType() const
54 {
55 return m_keyType;
56 }
57
Yingdi Yuc08d7d62015-07-16 21:05:11 -070058 KeyIdType
59 getKeyIdType() const
Yingdi Yu7036ce22014-06-19 18:53:37 -070060 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -070061 return m_keyIdType;
Yingdi Yu7036ce22014-06-19 18:53:37 -070062 }
63
Yingdi Yuc08d7d62015-07-16 21:05:11 -070064 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
76protected:
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 Yu7036ce22014-06-19 18:53:37 -070096private:
97 KeyType m_keyType;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070098 KeyIdType m_keyIdType;
99 name::Component m_keyId;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700100};
101
102
103/// @brief RsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for RSA key.
104class RsaKeyParamsInfo
105{
106public:
107 static KeyType
108 getType()
109 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700110 return KeyType::RSA;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700111 }
112
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700113 /**
114 * @brief check if @p size is qualified.
115 *
116 * @throw KeyParams::Error if the key size is not supported.
117 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700118 static uint32_t
119 checkKeySize(uint32_t size);
120
121 static uint32_t
122 getDefaultSize();
123};
124
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700125/// @brief EcKeyParamInfo is used to initialize a SimplePublicKeyParams template for elliptic curve key.
126class EcKeyParamsInfo
Yingdi Yu7036ce22014-06-19 18:53:37 -0700127{
128public:
129 static KeyType
130 getType()
131 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700132 return KeyType::EC;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700133 }
134
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700135 /**
136 * @brief check if @p size is qualified.
137 *
138 * @throw KeyParams::Error if the key size is not supported.
139 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700140 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.
149template<typename KeyParamsInfo>
150class SimplePublicKeyParams : public KeyParams
151{
152public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700153 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700154 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700155 SimplePublicKeyParams(const name::Component& keyId,
156 uint32_t size = KeyParamsInfo::getDefaultSize())
157 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700158 {
159 setKeySize(size);
160 }
161
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700162 /**
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 Yu7036ce22014-06-19 18:53:37 -0700168 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700169 SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
170 KeyIdType keyIdType = KeyIdType::RANDOM)
171 : KeyParams(KeyParamsInfo::getType(), keyIdType)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700172 {
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700173 setKeySize(size);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700174 }
175
176 uint32_t
177 getKeySize() const
178 {
179 return m_size;
180 }
181
182private:
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
195private:
196 uint32_t m_size;
197};
198
199/// @brief RsaKeyParams carries parameters for RSA key.
200typedef SimplePublicKeyParams<RsaKeyParamsInfo> RsaKeyParams;
201
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700202/// @brief EcKeyParams carries parameters for EC key.
203typedef SimplePublicKeyParams<EcKeyParamsInfo> EcKeyParams;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700204
205/// @brief AesKeyParamsInfo is used to initialize a SimpleSymmetricKeyParams template for AES key.
206class AesKeyParamsInfo
207{
208public:
209 static KeyType
210 getType()
211 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700212 return KeyType::AES;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700213 }
214
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700215 /**
216 * @brief check if @p size is qualified.
217 *
218 * @return KeyParams::Error if the key size is not supported.
219 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700220 static uint32_t
221 checkKeySize(uint32_t size);
222
223 static uint32_t
224 getDefaultSize();
225};
226
Yingdi Yu7036ce22014-06-19 18:53:37 -0700227/// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
228template<typename KeyParamsInfo>
229class SimpleSymmetricKeyParams : public KeyParams
230{
231public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700232 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700233 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700234 SimpleSymmetricKeyParams(const name::Component& keyId,
235 uint32_t size = KeyParamsInfo::getDefaultSize())
236 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700237 {
238 setKeySize(size);
239 }
240
Alexander Afanasyev1709aa72017-03-08 10:16:40 -0800241 /**
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 Yu7036ce22014-06-19 18:53:37 -0700255 uint32_t
256 getKeySize() const
257 {
258 return m_size;
259 }
260
261private:
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
274private:
275 uint32_t m_size;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700276};
277
278typedef SimpleSymmetricKeyParams<AesKeyParamsInfo> AesKeyParams;
279
280} // namespace ndn
281
282#endif // NDN_SECURITY_KEY_PARAMS_HPP