blob: 8f28a3ef9529747296c76f2a49ea555b32f4a1e8 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 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
125/// @brief EcdsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for ECDSA key.
126class EcdsaKeyParamsInfo
127{
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
202/// @brief EcdsaKeyParams carries parameters for ECDSA key.
203typedef SimplePublicKeyParams<EcdsaKeyParamsInfo> EcdsaKeyParams;
204
205
206/// @brief AesKeyParamsInfo is used to initialize a SimpleSymmetricKeyParams template for AES key.
207class AesKeyParamsInfo
208{
209public:
210 static KeyType
211 getType()
212 {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700213 return KeyType::AES;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700214 }
215
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700216 /**
217 * @brief check if @p size is qualified.
218 *
219 * @return KeyParams::Error if the key size is not supported.
220 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700221 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.
230template<typename KeyParamsInfo>
231class SimpleSymmetricKeyParams : public KeyParams
232{
233public:
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700234 /// @brief Create key parameter with user specified @p keyId.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700235 explicit
Yingdi Yuc08d7d62015-07-16 21:05:11 -0700236 SimpleSymmetricKeyParams(const name::Component& keyId,
237 uint32_t size = KeyParamsInfo::getDefaultSize())
238 : KeyParams(KeyParamsInfo::getType(), keyId)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700239 {
240 setKeySize(size);
241 }
242
Yingdi Yu7036ce22014-06-19 18:53:37 -0700243 uint32_t
244 getKeySize() const
245 {
246 return m_size;
247 }
248
249private:
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
262private:
263 uint32_t m_size;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700264};
265
266typedef SimpleSymmetricKeyParams<AesKeyParamsInfo> AesKeyParams;
267
268} // namespace ndn
269
270#endif // NDN_SECURITY_KEY_PARAMS_HPP