blob: 796aa33fd58afee7bb2c6194aa93552330ba563b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson47c93cf2013-08-09 00:38:48 -07002/**
José Quevedo641de4c2016-01-29 00:11:24 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson47c93cf2013-08-09 00:38:48 -070022 */
23
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_KEY_CHAIN_HPP
25#define NDN_SECURITY_KEY_CHAIN_HPP
Jeff Thompson47c93cf2013-08-09 00:38:48 -070026
Yingdi Yuf56c68f2014-04-24 21:50:13 -070027#include "sec-public-info.hpp"
28#include "sec-tpm.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070029#include "key-params.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070030#include "secured-bag.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080031#include "signature-sha256-with-rsa.hpp"
Yingdi Yuc8f883c2014-06-20 23:25:22 -070032#include "signature-sha256-with-ecdsa.hpp"
Yingdi Yubf6a2812014-06-17 15:32:11 -070033#include "digest-sha256.hpp"
Yingdi Yu1b0311c2015-06-10 14:58:47 -070034#include "signing-info.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070035
Yingdi Yu4270f202014-01-28 14:19:16 -080036#include "../interest.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080037#include "../util/crypto.hpp"
Yingdi Yu0f5fb692014-06-10 12:07:28 -070038#include "../util/random.hpp"
Alexander Afanasyev07113802015-01-15 19:14:36 -080039#include <initializer_list>
Yingdi Yu31b4af22014-01-14 14:13:00 -080040
Jeff Thompson47c93cf2013-08-09 00:38:48 -070041namespace ndn {
Yingdi Yu1b0311c2015-06-10 14:58:47 -070042namespace security {
Jeff Thompson47c93cf2013-08-09 00:38:48 -070043
Yingdi Yu1b0311c2015-06-10 14:58:47 -070044/**
45 * @brief The packet signing interface.
46 */
Alexander Afanasyev45a37132014-04-28 16:54:57 -070047class KeyChain : noncopyable
Yingdi Yuf56c68f2014-04-24 21:50:13 -070048{
49public:
50 class Error : public std::runtime_error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : std::runtime_error(what)
56 {
57 }
58 };
59
Yingdi Yu41546342014-11-30 23:37:53 -080060 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -070061 * @brief Error thrown when the supplied TPM locator to KeyChain constructor does not match
62 * the locator stored in PIB
Yingdi Yu41546342014-11-30 23:37:53 -080063 */
64 class MismatchError : public Error
65 {
66 public:
67 explicit
68 MismatchError(const std::string& what)
69 : Error(what)
70 {
71 }
72 };
Yingdi Yu7036ce22014-06-19 18:53:37 -070073
Alexander Afanasyev07113802015-01-15 19:14:36 -080074 typedef function<unique_ptr<SecPublicInfo> (const std::string&)> PibCreateFunc;
75 typedef function<unique_ptr<SecTpm>(const std::string&)> TpmCreateFunc;
Yingdi Yuf56c68f2014-04-24 21:50:13 -070076
Alexander Afanasyev07113802015-01-15 19:14:36 -080077 /**
78 * @brief Register a new PIB
Alexander Afanasyev34a37632015-01-16 17:37:36 -080079 * @param aliases List of schemes with which this PIB will be associated.
80 * The first alias in the list is considered a canonical name of the PIB instance.
Alexander Afanasyev07113802015-01-15 19:14:36 -080081 */
82 template<class PibType>
83 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -080084 registerPib(std::initializer_list<std::string> aliases);
Alexander Afanasyev07113802015-01-15 19:14:36 -080085
86 /**
87 * @brief Register a new TPM
Alexander Afanasyev34a37632015-01-16 17:37:36 -080088 * @param aliases List of schemes with which this TPM will be associated
89 * The first alias in the list is considered a canonical name of the TPM instance.
Alexander Afanasyev07113802015-01-15 19:14:36 -080090 */
91 template<class TpmType>
92 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -080093 registerTpm(std::initializer_list<std::string> aliases);
Alexander Afanasyev07113802015-01-15 19:14:36 -080094
Yingdi Yu1b0311c2015-06-10 14:58:47 -070095 /**
96 * @brief Get default PIB locator
97 */
Yingdi Yu281689a2015-06-13 14:32:32 -070098 static std::string
99 getDefaultPibLocator();
100
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700101 /**
102 * @brief Create a PIB according to @p pibLocator
103 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700104 static unique_ptr<SecPublicInfo>
105 createPib(const std::string& pibLocator);
106
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700107 /**
108 * @brief Get default TPM locator
109 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700110 static std::string
111 getDefaultTpmLocator();
112
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700113 /**
114 * @brief Create a TPM according to @p tpmLocator
115 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700116 static unique_ptr<SecTpm>
117 createTpm(const std::string& tpmLocator);
118
Alexander Afanasyev07113802015-01-15 19:14:36 -0800119 /**
120 * @brief Constructor to create KeyChain with default PIB and TPM
121 *
122 * Default PIB and TPM are platform-dependent and can be overriden system-wide or on
123 * per-use basis.
124 *
125 * @todo Add detailed description about config file behavior here
126 */
127 KeyChain();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700128
Yingdi Yu41546342014-11-30 23:37:53 -0800129 /**
130 * @brief KeyChain constructor
131 *
132 * @sa http://redmine.named-data.net/issues/2260
133 *
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200134 * @param pibLocator PIB locator
135 * @param tpmLocator TPM locator
Yingdi Yu41546342014-11-30 23:37:53 -0800136 * @param allowReset if true, the PIB will be reset when the supplied tpmLocator
137 * mismatches the one in PIB
138 */
139 KeyChain(const std::string& pibLocator,
140 const std::string& tpmLocator,
141 bool allowReset = false);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700142
143 virtual
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700144 ~KeyChain();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700145
Yingdi Yube4150e2014-02-18 13:02:46 -0800146 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700147 * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a
148 * self-signed certificate of the KSK.
Yingdi Yube4150e2014-02-18 13:02:46 -0800149 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800150 * @param identityName The name of the identity.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700151 * @param params The key parameter if a key needs to be generated for the identity.
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800152 * @return The name of the default certificate of the identity.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800153 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700154 Name
155 createIdentity(const Name& identityName, const KeyParams& params = DEFAULT_KEY_PARAMS);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700156
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800157 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800158 * @brief Generate a pair of RSA keys for the specified identity.
159 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800160 * @param identityName The name of the identity.
161 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
162 * @param keySize The size of the key.
163 * @return The generated key name.
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700164 * @see generateEcdsaKeyPair
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800165 */
Yingdi Yu41546342014-11-30 23:37:53 -0800166 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700167 generateRsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700168
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700169 /**
170 * @brief Generate a pair of ECDSA keys for the specified identity.
171 *
172 * @param identityName The name of the identity.
173 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
174 * @param keySize The size of the key.
175 * @return The generated key name.
176 * @see generateRsaKeyPair
177 */
Yingdi Yu41546342014-11-30 23:37:53 -0800178 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700179 generateEcdsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700180
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800181 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700182 * @brief Generate a pair of RSA keys for the specified identity and set it as default key for
183 * the identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800184 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800185 * @param identityName The name of the identity.
186 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
187 * @param keySize The size of the key.
188 * @return The generated key name.
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700189 * @see generateRsaKeyPair, generateEcdsaKeyPair, generateEcdsaKeyPairAsDefault
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800190 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700191 Name
José Quevedo699d2ea2016-01-29 00:13:00 +0000192 generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700193
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700194 /**
195 * @brief Generate a pair of ECDSA keys for the specified identity and set it as default key for
196 * the identity.
197 *
198 * @param identityName The name of the identity.
199 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
200 * @param keySize The size of the key.
201 * @return The generated key name.
202 * @see generateRsaKeyPair, generateEcdsaKeyPair, generateRsaKeyPairAsDefault
203 */
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700204 Name
José Quevedo699d2ea2016-01-29 00:13:00 +0000205 generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk = false, uint32_t keySize = 256);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700206
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800207 /**
Yingdi Yuc55680b2014-02-26 12:31:35 -0800208 * @brief prepare an unsigned identity certificate
209 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700210 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
Yingdi Yuc55680b2014-02-26 12:31:35 -0800211 * @param signingIdentity The signing identity.
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700212 * @param notBefore Refer to v1::IdentityCertificate.
213 * @param notAfter Refer to v1::IdentityCertificate.
214 * @param subjectDescription Refer to v1::IdentityCertificate.
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700215 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
216 * certificate name according to the relation between the signingIdentity and
217 * the subject identity. If signingIdentity is a prefix of the subject identity,
218 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
219 * after subject identity (i.e., before `ksk-....`).
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700220 * @return v1::IdentityCertificate.
Yingdi Yuc55680b2014-02-26 12:31:35 -0800221 */
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700222 shared_ptr<v1::IdentityCertificate>
Yingdi Yuc55680b2014-02-26 12:31:35 -0800223 prepareUnsignedIdentityCertificate(const Name& keyName,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700224 const Name& signingIdentity,
225 const time::system_clock::TimePoint& notBefore,
226 const time::system_clock::TimePoint& notAfter,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700227 const std::vector<security::v1::CertificateSubjectDescription>& subjectDescription,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700228 const Name& certPrefix = DEFAULT_PREFIX);
229
230 /**
231 * @brief prepare an unsigned identity certificate
232 *
233 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
234 * @param publicKey Public key to sign.
235 * @param signingIdentity The signing identity.
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700236 * @param notBefore Refer to v1::IdentityCertificate.
237 * @param notAfter Refer to v1::IdentityCertificate.
238 * @param subjectDescription Refer to v1::IdentityCertificate.
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700239 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
240 * certificate name according to the relation between the signingIdentity and
241 * the subject identity. If signingIdentity is a prefix of the subject identity,
242 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
243 * after subject identity (i.e., before `ksk-....`).
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700244 * @return v1::IdentityCertificate.
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700245 */
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700246 shared_ptr<v1::IdentityCertificate>
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700247 prepareUnsignedIdentityCertificate(const Name& keyName,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700248 const v1::PublicKey& publicKey,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700249 const Name& signingIdentity,
250 const time::system_clock::TimePoint& notBefore,
251 const time::system_clock::TimePoint& notAfter,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700252 const std::vector<security::v1::CertificateSubjectDescription>& subjectDescription,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700253 const Name& certPrefix = DEFAULT_PREFIX);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800254
255 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700256 * @brief Sign data according to the supplied signing information
Yingdi Yu2e57a582014-02-20 23:34:43 -0800257 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700258 * This method uses the supplied signing information @p params to create the SignatureInfo block:
259 * - it selects a private key and its certificate to sign the packet
260 * - sets the KeyLocator field with the certificate name, and
261 * - adds other requested information to the SignatureInfo block).
Yingdi Yu2e57a582014-02-20 23:34:43 -0800262 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700263 * After that, the method assigns the created SignatureInfo to the data packets, generate a
264 * signature and sets as part of the SignatureValue block.
265 *
266 * @param data The data to sign
267 * @param params The signing parameters.
268 * @throws Error if signing fails.
269 * @see SigningInfo
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800270 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800271 void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700272 sign(Data& data, const SigningInfo& params = DEFAULT_SIGNING_INFO);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700273
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700274 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700275 * @brief Sign interest according to the supplied signing information
276 *
277 * This method uses the supplied signing information @p params to create the SignatureInfo block:
278 * - it selects a private key and its certificate to sign the packet
279 * - sets the KeyLocator field with the certificate name, and
280 * - adds other requested information to the SignatureInfo block).
281 *
282 * After that, the method appends the created SignatureInfo to the interest name, generate a
283 * signature and appends it as part of the SignatureValue block to the interest name.
284 *
285 * @param interest The interest to sign
286 * @param params The signing parameters.
287 * @throws Error if signing fails.
288 * @see SigningInfo
289 */
290 void
291 sign(Interest& interest, const SigningInfo& params = DEFAULT_SIGNING_INFO);
292
293 /**
294 * @brief Sign buffer according to the supplied signing information
295 *
296 * @param buffer The buffer to sign
297 * @param bufferLength The buffer size
298 * @param params The signing parameters.
299 * @return a SignatureValue TLV block
300 * @throws Error if signing fails.
301 * @see SigningInfo
302 */
303 Block
304 sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params);
305
306 /**
307 * @deprecated use sign sign(T&, const SigningInfo&)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800308 * @brief Sign packet with a particular certificate.
309 *
310 * @param packet The packet to be signed.
311 * @param certificateName The certificate name of the key to use for signing.
312 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompson3c73da42013-08-12 11:19:05 -0700313 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800314 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700315 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700316 sign(T& packet, const Name& certificateName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700317
Jeff Thompson29ce3102013-09-27 11:47:48 -0700318 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700319 * @deprecated Use sign(const uint8_t*, size_t, const SigningInfo&) instead
Yingdi Yu2e57a582014-02-20 23:34:43 -0800320 * @brief Sign the byte array using a particular certificate.
321 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700322 * @param buffer The byte array to be signed.
323 * @param bufferLength the length of buffer.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800324 * @param certificateName The certificate name of the signing key.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700325 * @return The Signature.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700327 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800328 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700329 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700330
331 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700332 * @deprecated use sign sign(T&, const SigningInfo&)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 * @brief Sign packet using the default certificate of a particular identity.
334 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700335 * If there is no default certificate of that identity, this method will create a self-signed
336 * certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800337 *
338 * @param packet The packet to be signed.
339 * @param identityName The signing identity name.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700340 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800341 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700342 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700343 signByIdentity(T& packet, const Name& identityName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700344
Jeff Thompson3c73da42013-08-12 11:19:05 -0700345 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700346 * @deprecated use sign(const uint8_t*, size_t, const SigningInfo&) instead
Yingdi Yu2e57a582014-02-20 23:34:43 -0800347 * @brief Sign the byte array using the default certificate of a particular identity.
348 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700349 * @param buffer The byte array to be signed.
350 * @param bufferLength the length of buffer.
351 * @param identityName The identity name.
352 * @return The Signature.
353 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700354 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700355 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700356
357 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700358 * @deprecated use sign(Data&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256))
Yingdi Yu6ab67812014-11-27 15:00:34 -0800359 * @brief Set Sha256 weak signature for @p data
Yingdi Yu21157162014-02-28 13:02:34 -0800360 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700361 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700362 signWithSha256(Data& data);
Yingdi Yu21157162014-02-28 13:02:34 -0800363
364 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700365 * @deprecated use sign(Interest&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256))
Yingdi Yu6ab67812014-11-27 15:00:34 -0800366 * @brief Set Sha256 weak signature for @p interest
367 */
368 void
369 signWithSha256(Interest& interest);
370
371 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800372 * @brief Generate a self-signed certificate for a public key.
373 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700374 * @param keyName The name of the public key
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700375 * @return The generated certificate, shared_ptr<v1::IdentityCertificate>() if selfSign fails
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800376 */
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700377 shared_ptr<v1::IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700378 selfSign(const Name& keyName);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800379
380 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800381 * @brief Self-sign the supplied identity certificate.
382 *
383 * @param cert The supplied cert.
384 * @throws SecTpm::Error if the private key does not exist.
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700385 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700386 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700387 selfSign(v1::IdentityCertificate& cert);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800388
Yingdi Yu2e57a582014-02-20 23:34:43 -0800389 /**
390 * @brief delete a certificate.
391 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800392 * @param certificateName The certificate to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800393 * @throws KeyChain::Error if certificate cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800394 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700395 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700396 deleteCertificate(const Name& certificateName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800397
Yingdi Yu2e57a582014-02-20 23:34:43 -0800398 /**
399 * @brief delete a key.
400 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800401 * @param keyName The key to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800402 * @throws KeyChain::Error if key cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800403 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700404 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700405 deleteKey(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800406
Yingdi Yu2e57a582014-02-20 23:34:43 -0800407 /**
408 * @brief delete an identity.
409 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800410 * @param identity The identity to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800411 * @throws KeyChain::Error if identity cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700413 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700414 deleteIdentity(const Name& identity);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800415
Yingdi Yu2e57a582014-02-20 23:34:43 -0800416 /**
417 * @brief export an identity.
418 *
419 * @param identity The identity to export.
420 * @param passwordStr The password to secure the private key.
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800421 * @return The encoded export data.
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700422 * @throws SecPublicInfo::Error if anything goes wrong in exporting.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800423 */
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800424 shared_ptr<SecuredBag>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700425 exportIdentity(const Name& identity, const std::string& passwordStr);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800426
Yingdi Yu2e57a582014-02-20 23:34:43 -0800427 /**
428 * @brief import an identity.
429 *
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800430 * @param securedBag The encoded import data.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800431 * @param passwordStr The password to secure the private key.
432 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800433 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700434 importIdentity(const SecuredBag& securedBag, const std::string& passwordStr);
435
436 SecPublicInfo&
437 getPib()
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800438 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700439 return *m_pib;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800440 }
441
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700442 const SecPublicInfo&
443 getPib() const
444 {
445 return *m_pib;
446 }
447
448 SecTpm&
449 getTpm()
450 {
451 return *m_tpm;
452 }
453
454 const SecTpm&
455 getTpm() const
456 {
457 return *m_tpm;
458 }
459
460 /*******************************
461 * Wrapper of SecPublicInfo *
462 *******************************/
463 bool
464 doesIdentityExist(const Name& identityName) const
465 {
466 return m_pib->doesIdentityExist(identityName);
467 }
468
469 void
470 addIdentity(const Name& identityName)
471 {
472 return m_pib->addIdentity(identityName);
473 }
474
475 bool
476 doesPublicKeyExist(const Name& keyName) const
477 {
478 return m_pib->doesPublicKeyExist(keyName);
479 }
480
481 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700482 addPublicKey(const Name& keyName, KeyType keyType, const v1::PublicKey& publicKeyDer)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700483 {
Yingdi Yu41546342014-11-30 23:37:53 -0800484 return m_pib->addKey(keyName, publicKeyDer);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700485 }
486
Yingdi Yu7036ce22014-06-19 18:53:37 -0700487 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700488 addKey(const Name& keyName, const v1::PublicKey& publicKeyDer)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700489 {
490 return m_pib->addKey(keyName, publicKeyDer);
491 }
492
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700493 shared_ptr<v1::PublicKey>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700494 getPublicKey(const Name& keyName) const
495 {
496 return m_pib->getPublicKey(keyName);
497 }
498
499 bool
500 doesCertificateExist(const Name& certificateName) const
501 {
502 return m_pib->doesCertificateExist(certificateName);
503 }
504
505 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700506 addCertificate(const v1::IdentityCertificate& certificate)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700507 {
508 return m_pib->addCertificate(certificate);
509 }
510
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700511 shared_ptr<v1::IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700512 getCertificate(const Name& certificateName) const
513 {
514 return m_pib->getCertificate(certificateName);
515 }
516
517 Name
518 getDefaultIdentity() const
519 {
520 return m_pib->getDefaultIdentity();
521 }
522
523 Name
524 getDefaultKeyNameForIdentity(const Name& identityName) const
525 {
526 return m_pib->getDefaultKeyNameForIdentity(identityName);
527 }
528
José Quevedo641de4c2016-01-29 00:11:24 +0000529 /**
530 * @brief Get default key parameters for the specified identity
531 *
532 * If identity has a previously generated key, the returned parameters
533 * will include the same type of the key. If there are no existing
534 * keys, DEFAULT_KEY_PARAMS is used.
535 */
536 const KeyParams&
537 getDefaultKeyParamsForIdentity(const Name& identityName) const;
538
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700539 Name
540 getDefaultCertificateNameForKey(const Name& keyName) const
541 {
542 return m_pib->getDefaultCertificateNameForKey(keyName);
543 }
544
545 void
546 getAllIdentities(std::vector<Name>& nameList, bool isDefault) const
547 {
548 return m_pib->getAllIdentities(nameList, isDefault);
549 }
550
551 void
552 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const
553 {
554 return m_pib->getAllKeyNames(nameList, isDefault);
555 }
556
557 void
558 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const
559 {
560 return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault);
561 }
562
563 void
564 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const
565 {
566 return m_pib->getAllCertificateNames(nameList, isDefault);
567 }
568
569 void
570 getAllCertificateNamesOfKey(const Name& keyName,
571 std::vector<Name>& nameList,
572 bool isDefault) const
573 {
574 return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault);
575 }
576
577 void
578 deleteCertificateInfo(const Name& certificateName)
579 {
580 return m_pib->deleteCertificateInfo(certificateName);
581 }
582
583 void
584 deletePublicKeyInfo(const Name& keyName)
585 {
586 return m_pib->deletePublicKeyInfo(keyName);
587 }
588
589 void
590 deleteIdentityInfo(const Name& identity)
591 {
592 return m_pib->deleteIdentityInfo(identity);
593 }
594
595 void
596 setDefaultIdentity(const Name& identityName)
597 {
598 return m_pib->setDefaultIdentity(identityName);
599 }
600
601 void
602 setDefaultKeyNameForIdentity(const Name& keyName)
603 {
604 return m_pib->setDefaultKeyNameForIdentity(keyName);
605 }
606
607 void
608 setDefaultCertificateNameForKey(const Name& certificateName)
609 {
610 return m_pib->setDefaultCertificateNameForKey(certificateName);
611 }
612
613 Name
614 getNewKeyName(const Name& identityName, bool useKsk)
615 {
616 return m_pib->getNewKeyName(identityName, useKsk);
617 }
618
619 Name
620 getDefaultCertificateNameForIdentity(const Name& identityName) const
621 {
622 return m_pib->getDefaultCertificateNameForIdentity(identityName);
623 }
624
625 Name
626 getDefaultCertificateName() const
627 {
628 return m_pib->getDefaultCertificateName();
629 }
630
631 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700632 addCertificateAsKeyDefault(const v1::IdentityCertificate& certificate)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700633 {
634 return m_pib->addCertificateAsKeyDefault(certificate);
635 }
636
637 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700638 addCertificateAsIdentityDefault(const v1::IdentityCertificate& certificate)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700639 {
640 return m_pib->addCertificateAsIdentityDefault(certificate);
641 }
642
643 void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700644 addCertificateAsSystemDefault(const v1::IdentityCertificate& certificate)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700645 {
646 return m_pib->addCertificateAsSystemDefault(certificate);
647 }
648
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700649 shared_ptr<v1::IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700650 getDefaultCertificate() const
651 {
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700652 if (!static_cast<bool>(m_pib->getDefaultCertificate()))
653 const_cast<KeyChain*>(this)->setDefaultCertificateInternal();
654
655 return m_pib->getDefaultCertificate();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700656 }
657
658 void
659 refreshDefaultCertificate()
660 {
661 return m_pib->refreshDefaultCertificate();
662 }
663
664 /*******************************
665 * Wrapper of SecTpm *
666 *******************************/
667
668 void
669 setTpmPassword(const uint8_t* password, size_t passwordLength)
670 {
671 return m_tpm->setTpmPassword(password, passwordLength);
672 }
673
674 void
675 resetTpmPassword()
676 {
677 return m_tpm->resetTpmPassword();
678 }
679
680 void
681 setInTerminal(bool inTerminal)
682 {
683 return m_tpm->setInTerminal(inTerminal);
684 }
685
686 bool
687 getInTerminal() const
688 {
689 return m_tpm->getInTerminal();
690 }
691
692 bool
693 isLocked() const
694 {
695 return m_tpm->isLocked();
696 }
697
698 bool
699 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
700 {
701 return m_tpm->unlockTpm(password, passwordLength, usePassword);
702 }
703
704 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700705 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700706 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700707 return m_tpm->generateKeyPairInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700708 }
709
710 void
711 deleteKeyPairInTpm(const Name& keyName)
712 {
713 return m_tpm->deleteKeyPairInTpm(keyName);
714 }
715
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700716 shared_ptr<v1::PublicKey>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700717 getPublicKeyFromTpm(const Name& keyName) const
718 {
719 return m_tpm->getPublicKeyFromTpm(keyName);
720 }
721
722 Block
723 signInTpm(const uint8_t* data, size_t dataLength,
724 const Name& keyName,
725 DigestAlgorithm digestAlgorithm)
726 {
727 return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm);
728 }
729
730 ConstBufferPtr
731 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
732 {
733 return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric);
734 }
735
736 ConstBufferPtr
737 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
738 {
739 return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric);
740 }
741
742 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700743 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700744 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700745 return m_tpm->generateSymmetricKeyInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700746 }
747
748 bool
749 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const
750 {
751 return m_tpm->doesKeyExistInTpm(keyName, keyClass);
752 }
753
754 bool
755 generateRandomBlock(uint8_t* res, size_t size) const
756 {
757 return m_tpm->generateRandomBlock(res, size);
758 }
759
760 void
761 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
762 {
763 return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl);
764 }
765
766 ConstBufferPtr
767 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password)
768 {
769 return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password);
770 }
771
772 bool
773 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
774 const uint8_t* buf, size_t size,
775 const std::string& password)
776 {
777 return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password);
778 }
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700779
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800780private:
Yingdi Yu41546342014-11-30 23:37:53 -0800781 void
Alexander Afanasyev07113802015-01-15 19:14:36 -0800782 initialize(const std::string& pibLocatorUri,
783 const std::string& tpmLocatorUri,
Yingdi Yu41546342014-11-30 23:37:53 -0800784 bool needReset);
785
Yingdi Yu2e57a582014-02-20 23:34:43 -0800786 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700787 * @brief Prepare a SignatureInfo TLV according to signing information and return the signing key name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700788 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700789 * @param params The signing parameters.
790 * @return The signing key name and prepared SignatureInfo.
791 * @throw Error when the requested signing method cannot be satisfied.
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700792 */
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700793 std::tuple<Name, SignatureInfo>
794 prepareSignatureInfo(const SigningInfo& params);
795
796 /**
797 * @brief Internal abstraction of packet signing.
798 *
799 * @param packet The packet to sign
800 * @param params The signing parameters.
801 * @throw Error when the signing fails.
802 */
803 template<typename T>
804 void
805 signImpl(T& packet, const SigningInfo& params);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700806
807 /**
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700808 * @brief Set default certificate if it is not initialized
809 */
810 void
811 setDefaultCertificateInternal();
812
813 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800814 * @brief Generate a key pair for the specified identity.
815 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800816 * @param identityName The name of the specified identity.
817 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
Yingdi Yu7036ce22014-06-19 18:53:37 -0700818 * @param params The parameter of the key.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800819 * @return The name of the generated key.
820 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700821 Name
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700822 generateKeyPair(const Name& identityName, bool isKsk = false,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700823 const KeyParams& params = DEFAULT_KEY_PARAMS);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800824
Yingdi Yu8726f652014-01-23 10:35:12 -0800825 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800826 * @brief Sign the data using a particular key.
827 *
828 * @param data Reference to the data packet.
829 * @param signature Signature to be added.
Yingdi Yu8726f652014-01-23 10:35:12 -0800830 * @param keyName The name of the signing key.
831 * @param digestAlgorithm the digest algorithm.
832 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700833 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700834 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700835 signPacketWrapper(Data& data, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700836 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu8726f652014-01-23 10:35:12 -0800837
Yingdi Yu2e57a582014-02-20 23:34:43 -0800838 /**
839 * @brief Sign the interest using a particular key.
840 *
841 * @param interest Reference to the interest packet.
842 * @param signature Signature to be added.
843 * @param keyName The name of the signing key.
844 * @param digestAlgorithm the digest algorithm.
845 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700846 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700847 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700848 signPacketWrapper(Interest& interest, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700849 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800850
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700851 /**
852 * @brief Generate a SignatureValue block for a buffer @p buf with size @p size using
853 * a key with name @p keyName and digest algorithm @p digestAlgorithm.
854 */
855 Block
856 pureSign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
857
Alexander Afanasyev07113802015-01-15 19:14:36 -0800858 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800859 registerPibImpl(const std::string& canonicalName,
860 std::initializer_list<std::string> aliases, PibCreateFunc createFunc);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800861
862 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800863 registerTpmImpl(const std::string& canonicalName,
864 std::initializer_list<std::string> aliases, TpmCreateFunc createFunc);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800865
Yingdi Yu41546342014-11-30 23:37:53 -0800866public:
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700867 static tlv::SignatureTypeValue
868 getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm);
869
870public:
Yingdi Yu41546342014-11-30 23:37:53 -0800871 static const Name DEFAULT_PREFIX;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700872 static const SigningInfo DEFAULT_SIGNING_INFO;
873
874 /**
875 * @brief A localhost identity which indicates that signature is generated using SHA-256.
876 * @todo Passing this as identity is not implemented.
877 */
878 static const Name DIGEST_SHA256_IDENTITY;
879
Yingdi Yu41546342014-11-30 23:37:53 -0800880 // RsaKeyParams is set to be default for backward compatibility.
881 static const RsaKeyParams DEFAULT_KEY_PARAMS;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700882
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700883 typedef std::map<std::string, Block> SignParams;
884
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700885private:
Alexander Afanasyev07113802015-01-15 19:14:36 -0800886 std::unique_ptr<SecPublicInfo> m_pib;
887 std::unique_ptr<SecTpm> m_tpm;
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700888 time::milliseconds m_lastTimestamp;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700889};
890
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700891template<typename T>
892void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700893KeyChain::signImpl(T& packet, const SigningInfo& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700894{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700895 Name keyName;
896 SignatureInfo sigInfo;
897 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700898
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700899 signPacketWrapper(packet, Signature(sigInfo),
900 keyName, params.getDigestAlgorithm());
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700901}
902
903template<typename T>
904void
905KeyChain::sign(T& packet, const Name& certificateName)
906{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700907 signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certificateName));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700908}
909
910template<typename T>
911void
912KeyChain::signByIdentity(T& packet, const Name& identityName)
913{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700914 signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_ID, identityName));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700915}
916
Alexander Afanasyev07113802015-01-15 19:14:36 -0800917template<class PibType>
918inline void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800919KeyChain::registerPib(std::initializer_list<std::string> aliases)
Alexander Afanasyev07113802015-01-15 19:14:36 -0800920{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800921 registerPibImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
Davide Pesavento409cc202015-09-19 14:13:16 +0200922 return make_unique<PibType>(locator);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800923 });
924}
925
926template<class TpmType>
927inline void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800928KeyChain::registerTpm(std::initializer_list<std::string> aliases)
Alexander Afanasyev07113802015-01-15 19:14:36 -0800929{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800930 registerTpmImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
Davide Pesavento409cc202015-09-19 14:13:16 +0200931 return make_unique<TpmType>(locator);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800932 });
933}
934
935/**
936 * \brief Register SecPib class in ndn-cxx KeyChain
937 *
938 * This macro should be placed once in the implementation file of the
939 * SecPib type within the namespace where the type is declared.
940 */
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800941#define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...) \
Alexander Afanasyev07113802015-01-15 19:14:36 -0800942static class NdnCxxAuto ## PibType ## PibRegistrationClass \
943{ \
944public: \
945 NdnCxxAuto ## PibType ## PibRegistrationClass() \
946 { \
947 ::ndn::KeyChain::registerPib<PibType>({__VA_ARGS__}); \
948 } \
949} ndnCxxAuto ## PibType ## PibRegistrationVariable
950
951/**
952 * \brief Register SecTpm class in ndn-cxx KeyChain
953 *
954 * This macro should be placed once in the implementation file of the
955 * SecTpm type within the namespace where the type is declared.
956 */
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800957#define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...) \
Alexander Afanasyev07113802015-01-15 19:14:36 -0800958static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \
959{ \
960public: \
961 NdnCxxAuto ## TpmType ## TpmRegistrationClass() \
962 { \
963 ::ndn::KeyChain::registerTpm<TpmType>({__VA_ARGS__}); \
964 } \
965} ndnCxxAuto ## TpmType ## TpmRegistrationVariable
966
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700967} // namespace security
968
969using security::KeyChain;
970
Yingdi Yu6ab67812014-11-27 15:00:34 -0800971} // namespace ndn
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700972
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700973#endif // NDN_SECURITY_KEY_CHAIN_HPP