blob: 28c026e90dd842608d3acd61ea145f3d7fe9d35e [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 -070041
42namespace ndn {
Yingdi Yu1b0311c2015-06-10 14:58:47 -070043namespace security {
Jeff Thompson47c93cf2013-08-09 00:38:48 -070044
Yingdi Yu1b0311c2015-06-10 14:58:47 -070045/**
46 * @brief The packet signing interface.
47 */
Alexander Afanasyev45a37132014-04-28 16:54:57 -070048class KeyChain : noncopyable
Yingdi Yuf56c68f2014-04-24 21:50:13 -070049{
50public:
51 class Error : public std::runtime_error
52 {
53 public:
54 explicit
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58 }
59 };
60
Yingdi Yu41546342014-11-30 23:37:53 -080061 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -070062 * @brief Error thrown when the supplied TPM locator to KeyChain constructor does not match
63 * the locator stored in PIB
Yingdi Yu41546342014-11-30 23:37:53 -080064 */
65 class MismatchError : public Error
66 {
67 public:
68 explicit
69 MismatchError(const std::string& what)
70 : Error(what)
71 {
72 }
73 };
Yingdi Yu7036ce22014-06-19 18:53:37 -070074
Alexander Afanasyev07113802015-01-15 19:14:36 -080075 typedef function<unique_ptr<SecPublicInfo> (const std::string&)> PibCreateFunc;
76 typedef function<unique_ptr<SecTpm>(const std::string&)> TpmCreateFunc;
Yingdi Yuf56c68f2014-04-24 21:50:13 -070077
Alexander Afanasyev07113802015-01-15 19:14:36 -080078 /**
79 * @brief Register a new PIB
Alexander Afanasyev34a37632015-01-16 17:37:36 -080080 * @param aliases List of schemes with which this PIB will be associated.
81 * The first alias in the list is considered a canonical name of the PIB instance.
Alexander Afanasyev07113802015-01-15 19:14:36 -080082 */
83 template<class PibType>
84 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -080085 registerPib(std::initializer_list<std::string> aliases);
Alexander Afanasyev07113802015-01-15 19:14:36 -080086
87 /**
88 * @brief Register a new TPM
Alexander Afanasyev34a37632015-01-16 17:37:36 -080089 * @param aliases List of schemes with which this TPM will be associated
90 * The first alias in the list is considered a canonical name of the TPM instance.
Alexander Afanasyev07113802015-01-15 19:14:36 -080091 */
92 template<class TpmType>
93 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -080094 registerTpm(std::initializer_list<std::string> aliases);
Alexander Afanasyev07113802015-01-15 19:14:36 -080095
Yingdi Yu1b0311c2015-06-10 14:58:47 -070096 /**
97 * @brief Get default PIB locator
98 */
Yingdi Yu281689a2015-06-13 14:32:32 -070099 static std::string
100 getDefaultPibLocator();
101
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700102 /**
103 * @brief Create a PIB according to @p pibLocator
104 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700105 static unique_ptr<SecPublicInfo>
106 createPib(const std::string& pibLocator);
107
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700108 /**
109 * @brief Get default TPM locator
110 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700111 static std::string
112 getDefaultTpmLocator();
113
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700114 /**
115 * @brief Create a TPM according to @p tpmLocator
116 */
Yingdi Yu281689a2015-06-13 14:32:32 -0700117 static unique_ptr<SecTpm>
118 createTpm(const std::string& tpmLocator);
119
Alexander Afanasyev07113802015-01-15 19:14:36 -0800120 /**
121 * @brief Constructor to create KeyChain with default PIB and TPM
122 *
123 * Default PIB and TPM are platform-dependent and can be overriden system-wide or on
124 * per-use basis.
125 *
126 * @todo Add detailed description about config file behavior here
127 */
128 KeyChain();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700129
Yingdi Yu41546342014-11-30 23:37:53 -0800130 /**
131 * @brief KeyChain constructor
132 *
133 * @sa http://redmine.named-data.net/issues/2260
134 *
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200135 * @param pibLocator PIB locator
136 * @param tpmLocator TPM locator
Yingdi Yu41546342014-11-30 23:37:53 -0800137 * @param allowReset if true, the PIB will be reset when the supplied tpmLocator
138 * mismatches the one in PIB
139 */
140 KeyChain(const std::string& pibLocator,
141 const std::string& tpmLocator,
142 bool allowReset = false);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700143
144 virtual
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700145 ~KeyChain();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700146
Yingdi Yube4150e2014-02-18 13:02:46 -0800147 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700148 * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a
149 * self-signed certificate of the KSK.
Yingdi Yube4150e2014-02-18 13:02:46 -0800150 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800151 * @param identityName The name of the identity.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700152 * @param params The key parameter if a key needs to be generated for the identity.
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800153 * @return The name of the default certificate of the identity.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800154 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700155 Name
156 createIdentity(const Name& identityName, const KeyParams& params = DEFAULT_KEY_PARAMS);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700157
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800158 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800159 * @brief Generate a pair of RSA keys for the specified identity.
160 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800161 * @param identityName The name of the identity.
162 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
163 * @param keySize The size of the key.
164 * @return The generated key name.
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700165 * @see generateEcdsaKeyPair
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800166 */
Yingdi Yu41546342014-11-30 23:37:53 -0800167 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700168 generateRsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700169
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700170 /**
171 * @brief Generate a pair of ECDSA keys for the specified identity.
172 *
173 * @param identityName The name of the identity.
174 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
175 * @param keySize The size of the key.
176 * @return The generated key name.
177 * @see generateRsaKeyPair
178 */
Yingdi Yu41546342014-11-30 23:37:53 -0800179 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700180 generateEcdsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700181
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800182 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700183 * @brief Generate a pair of RSA keys for the specified identity and set it as default key for
184 * the identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800185 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800186 * @param identityName The name of the identity.
187 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
188 * @param keySize The size of the key.
189 * @return The generated key name.
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700190 * @see generateRsaKeyPair, generateEcdsaKeyPair, generateEcdsaKeyPairAsDefault
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800191 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700192 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700193 generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false,
194 uint32_t keySize = 2048);
195
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700196 /**
197 * @brief Generate a pair of ECDSA keys for the specified identity and set it as default key for
198 * the identity.
199 *
200 * @param identityName The name of the identity.
201 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
202 * @param keySize The size of the key.
203 * @return The generated key name.
204 * @see generateRsaKeyPair, generateEcdsaKeyPair, generateRsaKeyPairAsDefault
205 */
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700206 Name
207 generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize = 256);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700208
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800209 /**
Yingdi Yuc55680b2014-02-26 12:31:35 -0800210 * @brief prepare an unsigned identity certificate
211 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700212 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
Yingdi Yuc55680b2014-02-26 12:31:35 -0800213 * @param signingIdentity The signing identity.
214 * @param notBefore Refer to IdentityCertificate.
215 * @param notAfter Refer to IdentityCertificate.
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700216 * @param subjectDescription Refer to IdentityCertificate.
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700217 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
218 * certificate name according to the relation between the signingIdentity and
219 * the subject identity. If signingIdentity is a prefix of the subject identity,
220 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
221 * after subject identity (i.e., before `ksk-....`).
Yingdi Yuc55680b2014-02-26 12:31:35 -0800222 * @return IdentityCertificate.
223 */
224 shared_ptr<IdentityCertificate>
225 prepareUnsignedIdentityCertificate(const Name& keyName,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700226 const Name& signingIdentity,
227 const time::system_clock::TimePoint& notBefore,
228 const time::system_clock::TimePoint& notAfter,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700229 const std::vector<CertificateSubjectDescription>& subjectDescription,
230 const Name& certPrefix = DEFAULT_PREFIX);
231
232 /**
233 * @brief prepare an unsigned identity certificate
234 *
235 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
236 * @param publicKey Public key to sign.
237 * @param signingIdentity The signing identity.
238 * @param notBefore Refer to IdentityCertificate.
239 * @param notAfter Refer to IdentityCertificate.
240 * @param subjectDescription Refer to IdentityCertificate.
241 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
242 * certificate name according to the relation between the signingIdentity and
243 * the subject identity. If signingIdentity is a prefix of the subject identity,
244 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
245 * after subject identity (i.e., before `ksk-....`).
246 * @return IdentityCertificate.
247 */
248 shared_ptr<IdentityCertificate>
249 prepareUnsignedIdentityCertificate(const Name& keyName,
250 const PublicKey& publicKey,
251 const Name& signingIdentity,
252 const time::system_clock::TimePoint& notBefore,
253 const time::system_clock::TimePoint& notAfter,
254 const std::vector<CertificateSubjectDescription>& subjectDescription,
255 const Name& certPrefix = DEFAULT_PREFIX);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800256
257 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700258 * @brief Sign data according to the supplied signing information
Yingdi Yu2e57a582014-02-20 23:34:43 -0800259 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700260 * This method uses the supplied signing information @p params to create the SignatureInfo block:
261 * - it selects a private key and its certificate to sign the packet
262 * - sets the KeyLocator field with the certificate name, and
263 * - adds other requested information to the SignatureInfo block).
Yingdi Yu2e57a582014-02-20 23:34:43 -0800264 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700265 * After that, the method assigns the created SignatureInfo to the data packets, generate a
266 * signature and sets as part of the SignatureValue block.
267 *
268 * @param data The data to sign
269 * @param params The signing parameters.
270 * @throws Error if signing fails.
271 * @see SigningInfo
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800272 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800273 void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700274 sign(Data& data, const SigningInfo& params = DEFAULT_SIGNING_INFO);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700275
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700276 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700277 * @brief Sign interest according to the supplied signing information
278 *
279 * This method uses the supplied signing information @p params to create the SignatureInfo block:
280 * - it selects a private key and its certificate to sign the packet
281 * - sets the KeyLocator field with the certificate name, and
282 * - adds other requested information to the SignatureInfo block).
283 *
284 * After that, the method appends the created SignatureInfo to the interest name, generate a
285 * signature and appends it as part of the SignatureValue block to the interest name.
286 *
287 * @param interest The interest to sign
288 * @param params The signing parameters.
289 * @throws Error if signing fails.
290 * @see SigningInfo
291 */
292 void
293 sign(Interest& interest, const SigningInfo& params = DEFAULT_SIGNING_INFO);
294
295 /**
296 * @brief Sign buffer according to the supplied signing information
297 *
298 * @param buffer The buffer to sign
299 * @param bufferLength The buffer size
300 * @param params The signing parameters.
301 * @return a SignatureValue TLV block
302 * @throws Error if signing fails.
303 * @see SigningInfo
304 */
305 Block
306 sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params);
307
308 /**
309 * @deprecated use sign sign(T&, const SigningInfo&)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800310 * @brief Sign packet with a particular certificate.
311 *
312 * @param packet The packet to be signed.
313 * @param certificateName The certificate name of the key to use for signing.
314 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompson3c73da42013-08-12 11:19:05 -0700315 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800316 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700317 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700318 sign(T& packet, const Name& certificateName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700319
Jeff Thompson29ce3102013-09-27 11:47:48 -0700320 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700321 * @deprecated Use sign(const uint8_t*, size_t, const SigningInfo&) instead
Yingdi Yu2e57a582014-02-20 23:34:43 -0800322 * @brief Sign the byte array using a particular certificate.
323 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700324 * @param buffer The byte array to be signed.
325 * @param bufferLength the length of buffer.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326 * @param certificateName The certificate name of the signing key.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700327 * @return The Signature.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700329 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800330 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700331 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700332
333 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700334 * @deprecated use sign sign(T&, const SigningInfo&)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335 * @brief Sign packet using the default certificate of a particular identity.
336 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700337 * If there is no default certificate of that identity, this method will create a self-signed
338 * certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800339 *
340 * @param packet The packet to be signed.
341 * @param identityName The signing identity name.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700342 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800343 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700344 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700345 signByIdentity(T& packet, const Name& identityName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700346
Jeff Thompson3c73da42013-08-12 11:19:05 -0700347 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700348 * @deprecated use sign(const uint8_t*, size_t, const SigningInfo&) instead
Yingdi Yu2e57a582014-02-20 23:34:43 -0800349 * @brief Sign the byte array using the default certificate of a particular identity.
350 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700351 * @param buffer The byte array to be signed.
352 * @param bufferLength the length of buffer.
353 * @param identityName The identity name.
354 * @return The Signature.
355 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700356 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700357 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700358
359 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700360 * @deprecated use sign(Data&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256))
Yingdi Yu6ab67812014-11-27 15:00:34 -0800361 * @brief Set Sha256 weak signature for @p data
Yingdi Yu21157162014-02-28 13:02:34 -0800362 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700363 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700364 signWithSha256(Data& data);
Yingdi Yu21157162014-02-28 13:02:34 -0800365
366 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700367 * @deprecated use sign(Interest&, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256))
Yingdi Yu6ab67812014-11-27 15:00:34 -0800368 * @brief Set Sha256 weak signature for @p interest
369 */
370 void
371 signWithSha256(Interest& interest);
372
373 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374 * @brief Generate a self-signed certificate for a public key.
375 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700376 * @param keyName The name of the public key
377 * @return The generated certificate, shared_ptr<IdentityCertificate>() if selfSign fails
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800378 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800379 shared_ptr<IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700380 selfSign(const Name& keyName);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800381
382 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383 * @brief Self-sign the supplied identity certificate.
384 *
385 * @param cert The supplied cert.
386 * @throws SecTpm::Error if the private key does not exist.
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700387 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700388 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700389 selfSign(IdentityCertificate& cert);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800390
Yingdi Yu2e57a582014-02-20 23:34:43 -0800391 /**
392 * @brief delete a certificate.
393 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800394 * @param certificateName The certificate to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800395 * @throws KeyChain::Error if certificate cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800396 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700397 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700398 deleteCertificate(const Name& certificateName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800399
Yingdi Yu2e57a582014-02-20 23:34:43 -0800400 /**
401 * @brief delete a key.
402 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800403 * @param keyName The key to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800404 * @throws KeyChain::Error if key cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700406 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700407 deleteKey(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800408
Yingdi Yu2e57a582014-02-20 23:34:43 -0800409 /**
410 * @brief delete an identity.
411 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800412 * @param identity The identity to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800413 * @throws KeyChain::Error if identity cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800414 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700415 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700416 deleteIdentity(const Name& identity);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800417
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418 /**
419 * @brief export an identity.
420 *
421 * @param identity The identity to export.
422 * @param passwordStr The password to secure the private key.
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800423 * @return The encoded export data.
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700424 * @throws SecPublicInfo::Error if anything goes wrong in exporting.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425 */
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800426 shared_ptr<SecuredBag>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700427 exportIdentity(const Name& identity, const std::string& passwordStr);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800428
Yingdi Yu2e57a582014-02-20 23:34:43 -0800429 /**
430 * @brief import an identity.
431 *
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800432 * @param securedBag The encoded import data.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433 * @param passwordStr The password to secure the private key.
434 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800435 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700436 importIdentity(const SecuredBag& securedBag, const std::string& passwordStr);
437
438 SecPublicInfo&
439 getPib()
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800440 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700441 return *m_pib;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800442 }
443
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700444 const SecPublicInfo&
445 getPib() const
446 {
447 return *m_pib;
448 }
449
450 SecTpm&
451 getTpm()
452 {
453 return *m_tpm;
454 }
455
456 const SecTpm&
457 getTpm() const
458 {
459 return *m_tpm;
460 }
461
462 /*******************************
463 * Wrapper of SecPublicInfo *
464 *******************************/
465 bool
466 doesIdentityExist(const Name& identityName) const
467 {
468 return m_pib->doesIdentityExist(identityName);
469 }
470
471 void
472 addIdentity(const Name& identityName)
473 {
474 return m_pib->addIdentity(identityName);
475 }
476
477 bool
478 doesPublicKeyExist(const Name& keyName) const
479 {
480 return m_pib->doesPublicKeyExist(keyName);
481 }
482
483 void
484 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
485 {
Yingdi Yu41546342014-11-30 23:37:53 -0800486 return m_pib->addKey(keyName, publicKeyDer);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700487 }
488
Yingdi Yu7036ce22014-06-19 18:53:37 -0700489 void
490 addKey(const Name& keyName, const PublicKey& publicKeyDer)
491 {
492 return m_pib->addKey(keyName, publicKeyDer);
493 }
494
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700495 shared_ptr<PublicKey>
496 getPublicKey(const Name& keyName) const
497 {
498 return m_pib->getPublicKey(keyName);
499 }
500
501 bool
502 doesCertificateExist(const Name& certificateName) const
503 {
504 return m_pib->doesCertificateExist(certificateName);
505 }
506
507 void
508 addCertificate(const IdentityCertificate& certificate)
509 {
510 return m_pib->addCertificate(certificate);
511 }
512
513 shared_ptr<IdentityCertificate>
514 getCertificate(const Name& certificateName) const
515 {
516 return m_pib->getCertificate(certificateName);
517 }
518
519 Name
520 getDefaultIdentity() const
521 {
522 return m_pib->getDefaultIdentity();
523 }
524
525 Name
526 getDefaultKeyNameForIdentity(const Name& identityName) const
527 {
528 return m_pib->getDefaultKeyNameForIdentity(identityName);
529 }
530
José Quevedo641de4c2016-01-29 00:11:24 +0000531 /**
532 * @brief Get default key parameters for the specified identity
533 *
534 * If identity has a previously generated key, the returned parameters
535 * will include the same type of the key. If there are no existing
536 * keys, DEFAULT_KEY_PARAMS is used.
537 */
538 const KeyParams&
539 getDefaultKeyParamsForIdentity(const Name& identityName) const;
540
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700541 Name
542 getDefaultCertificateNameForKey(const Name& keyName) const
543 {
544 return m_pib->getDefaultCertificateNameForKey(keyName);
545 }
546
547 void
548 getAllIdentities(std::vector<Name>& nameList, bool isDefault) const
549 {
550 return m_pib->getAllIdentities(nameList, isDefault);
551 }
552
553 void
554 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const
555 {
556 return m_pib->getAllKeyNames(nameList, isDefault);
557 }
558
559 void
560 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const
561 {
562 return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault);
563 }
564
565 void
566 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const
567 {
568 return m_pib->getAllCertificateNames(nameList, isDefault);
569 }
570
571 void
572 getAllCertificateNamesOfKey(const Name& keyName,
573 std::vector<Name>& nameList,
574 bool isDefault) const
575 {
576 return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault);
577 }
578
579 void
580 deleteCertificateInfo(const Name& certificateName)
581 {
582 return m_pib->deleteCertificateInfo(certificateName);
583 }
584
585 void
586 deletePublicKeyInfo(const Name& keyName)
587 {
588 return m_pib->deletePublicKeyInfo(keyName);
589 }
590
591 void
592 deleteIdentityInfo(const Name& identity)
593 {
594 return m_pib->deleteIdentityInfo(identity);
595 }
596
597 void
598 setDefaultIdentity(const Name& identityName)
599 {
600 return m_pib->setDefaultIdentity(identityName);
601 }
602
603 void
604 setDefaultKeyNameForIdentity(const Name& keyName)
605 {
606 return m_pib->setDefaultKeyNameForIdentity(keyName);
607 }
608
609 void
610 setDefaultCertificateNameForKey(const Name& certificateName)
611 {
612 return m_pib->setDefaultCertificateNameForKey(certificateName);
613 }
614
615 Name
616 getNewKeyName(const Name& identityName, bool useKsk)
617 {
618 return m_pib->getNewKeyName(identityName, useKsk);
619 }
620
621 Name
622 getDefaultCertificateNameForIdentity(const Name& identityName) const
623 {
624 return m_pib->getDefaultCertificateNameForIdentity(identityName);
625 }
626
627 Name
628 getDefaultCertificateName() const
629 {
630 return m_pib->getDefaultCertificateName();
631 }
632
633 void
634 addCertificateAsKeyDefault(const IdentityCertificate& certificate)
635 {
636 return m_pib->addCertificateAsKeyDefault(certificate);
637 }
638
639 void
640 addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
641 {
642 return m_pib->addCertificateAsIdentityDefault(certificate);
643 }
644
645 void
646 addCertificateAsSystemDefault(const IdentityCertificate& certificate)
647 {
648 return m_pib->addCertificateAsSystemDefault(certificate);
649 }
650
651 shared_ptr<IdentityCertificate>
652 getDefaultCertificate() const
653 {
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700654 if (!static_cast<bool>(m_pib->getDefaultCertificate()))
655 const_cast<KeyChain*>(this)->setDefaultCertificateInternal();
656
657 return m_pib->getDefaultCertificate();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700658 }
659
660 void
661 refreshDefaultCertificate()
662 {
663 return m_pib->refreshDefaultCertificate();
664 }
665
666 /*******************************
667 * Wrapper of SecTpm *
668 *******************************/
669
670 void
671 setTpmPassword(const uint8_t* password, size_t passwordLength)
672 {
673 return m_tpm->setTpmPassword(password, passwordLength);
674 }
675
676 void
677 resetTpmPassword()
678 {
679 return m_tpm->resetTpmPassword();
680 }
681
682 void
683 setInTerminal(bool inTerminal)
684 {
685 return m_tpm->setInTerminal(inTerminal);
686 }
687
688 bool
689 getInTerminal() const
690 {
691 return m_tpm->getInTerminal();
692 }
693
694 bool
695 isLocked() const
696 {
697 return m_tpm->isLocked();
698 }
699
700 bool
701 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
702 {
703 return m_tpm->unlockTpm(password, passwordLength, usePassword);
704 }
705
706 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700707 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700708 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700709 return m_tpm->generateKeyPairInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700710 }
711
712 void
713 deleteKeyPairInTpm(const Name& keyName)
714 {
715 return m_tpm->deleteKeyPairInTpm(keyName);
716 }
717
718 shared_ptr<PublicKey>
719 getPublicKeyFromTpm(const Name& keyName) const
720 {
721 return m_tpm->getPublicKeyFromTpm(keyName);
722 }
723
724 Block
725 signInTpm(const uint8_t* data, size_t dataLength,
726 const Name& keyName,
727 DigestAlgorithm digestAlgorithm)
728 {
729 return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm);
730 }
731
732 ConstBufferPtr
733 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
734 {
735 return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric);
736 }
737
738 ConstBufferPtr
739 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
740 {
741 return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric);
742 }
743
744 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700745 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700746 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700747 return m_tpm->generateSymmetricKeyInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700748 }
749
750 bool
751 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const
752 {
753 return m_tpm->doesKeyExistInTpm(keyName, keyClass);
754 }
755
756 bool
757 generateRandomBlock(uint8_t* res, size_t size) const
758 {
759 return m_tpm->generateRandomBlock(res, size);
760 }
761
762 void
763 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
764 {
765 return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl);
766 }
767
768 ConstBufferPtr
769 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password)
770 {
771 return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password);
772 }
773
774 bool
775 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
776 const uint8_t* buf, size_t size,
777 const std::string& password)
778 {
779 return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password);
780 }
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700781
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800782private:
Yingdi Yu41546342014-11-30 23:37:53 -0800783 void
Alexander Afanasyev07113802015-01-15 19:14:36 -0800784 initialize(const std::string& pibLocatorUri,
785 const std::string& tpmLocatorUri,
Yingdi Yu41546342014-11-30 23:37:53 -0800786 bool needReset);
787
Yingdi Yu2e57a582014-02-20 23:34:43 -0800788 /**
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700789 * @brief Prepare a SignatureInfo TLV according to signing information and return the signing key name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700790 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700791 * @param params The signing parameters.
792 * @return The signing key name and prepared SignatureInfo.
793 * @throw Error when the requested signing method cannot be satisfied.
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700794 */
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700795 std::tuple<Name, SignatureInfo>
796 prepareSignatureInfo(const SigningInfo& params);
797
798 /**
799 * @brief Internal abstraction of packet signing.
800 *
801 * @param packet The packet to sign
802 * @param params The signing parameters.
803 * @throw Error when the signing fails.
804 */
805 template<typename T>
806 void
807 signImpl(T& packet, const SigningInfo& params);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700808
809 /**
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700810 * @brief Set default certificate if it is not initialized
811 */
812 void
813 setDefaultCertificateInternal();
814
815 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800816 * @brief Generate a key pair for the specified identity.
817 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800818 * @param identityName The name of the specified identity.
819 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
Yingdi Yu7036ce22014-06-19 18:53:37 -0700820 * @param params The parameter of the key.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800821 * @return The name of the generated key.
822 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700823 Name
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700824 generateKeyPair(const Name& identityName, bool isKsk = false,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700825 const KeyParams& params = DEFAULT_KEY_PARAMS);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800826
Yingdi Yu8726f652014-01-23 10:35:12 -0800827 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800828 * @brief Sign the data using a particular key.
829 *
830 * @param data Reference to the data packet.
831 * @param signature Signature to be added.
Yingdi Yu8726f652014-01-23 10:35:12 -0800832 * @param keyName The name of the signing key.
833 * @param digestAlgorithm the digest algorithm.
834 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700835 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700836 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700837 signPacketWrapper(Data& data, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700838 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu8726f652014-01-23 10:35:12 -0800839
Yingdi Yu2e57a582014-02-20 23:34:43 -0800840 /**
841 * @brief Sign the interest using a particular key.
842 *
843 * @param interest Reference to the interest packet.
844 * @param signature Signature to be added.
845 * @param keyName The name of the signing key.
846 * @param digestAlgorithm the digest algorithm.
847 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700848 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700849 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700850 signPacketWrapper(Interest& interest, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700851 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800852
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700853 /**
854 * @brief Generate a SignatureValue block for a buffer @p buf with size @p size using
855 * a key with name @p keyName and digest algorithm @p digestAlgorithm.
856 */
857 Block
858 pureSign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
859
Alexander Afanasyev07113802015-01-15 19:14:36 -0800860 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800861 registerPibImpl(const std::string& canonicalName,
862 std::initializer_list<std::string> aliases, PibCreateFunc createFunc);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800863
864 static void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800865 registerTpmImpl(const std::string& canonicalName,
866 std::initializer_list<std::string> aliases, TpmCreateFunc createFunc);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800867
Yingdi Yu41546342014-11-30 23:37:53 -0800868public:
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700869 static tlv::SignatureTypeValue
870 getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm);
871
872public:
Yingdi Yu41546342014-11-30 23:37:53 -0800873 static const Name DEFAULT_PREFIX;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700874 static const SigningInfo DEFAULT_SIGNING_INFO;
875
876 /**
877 * @brief A localhost identity which indicates that signature is generated using SHA-256.
878 * @todo Passing this as identity is not implemented.
879 */
880 static const Name DIGEST_SHA256_IDENTITY;
881
Yingdi Yu41546342014-11-30 23:37:53 -0800882 // RsaKeyParams is set to be default for backward compatibility.
883 static const RsaKeyParams DEFAULT_KEY_PARAMS;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700884
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700885 typedef std::map<std::string, Block> SignParams;
886
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700887private:
Alexander Afanasyev07113802015-01-15 19:14:36 -0800888 std::unique_ptr<SecPublicInfo> m_pib;
889 std::unique_ptr<SecTpm> m_tpm;
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700890 time::milliseconds m_lastTimestamp;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700891};
892
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700893template<typename T>
894void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700895KeyChain::signImpl(T& packet, const SigningInfo& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700896{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700897 Name keyName;
898 SignatureInfo sigInfo;
899 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700900
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700901 signPacketWrapper(packet, Signature(sigInfo),
902 keyName, params.getDigestAlgorithm());
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700903}
904
905template<typename T>
906void
907KeyChain::sign(T& packet, const Name& certificateName)
908{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700909 signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certificateName));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700910}
911
912template<typename T>
913void
914KeyChain::signByIdentity(T& packet, const Name& identityName)
915{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700916 signImpl(packet, SigningInfo(SigningInfo::SIGNER_TYPE_ID, identityName));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700917}
918
Alexander Afanasyev07113802015-01-15 19:14:36 -0800919template<class PibType>
920inline void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800921KeyChain::registerPib(std::initializer_list<std::string> aliases)
Alexander Afanasyev07113802015-01-15 19:14:36 -0800922{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800923 registerPibImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
Davide Pesavento409cc202015-09-19 14:13:16 +0200924 return make_unique<PibType>(locator);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800925 });
926}
927
928template<class TpmType>
929inline void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800930KeyChain::registerTpm(std::initializer_list<std::string> aliases)
Alexander Afanasyev07113802015-01-15 19:14:36 -0800931{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800932 registerTpmImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
Davide Pesavento409cc202015-09-19 14:13:16 +0200933 return make_unique<TpmType>(locator);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800934 });
935}
936
937/**
938 * \brief Register SecPib class in ndn-cxx KeyChain
939 *
940 * This macro should be placed once in the implementation file of the
941 * SecPib type within the namespace where the type is declared.
942 */
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800943#define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...) \
Alexander Afanasyev07113802015-01-15 19:14:36 -0800944static class NdnCxxAuto ## PibType ## PibRegistrationClass \
945{ \
946public: \
947 NdnCxxAuto ## PibType ## PibRegistrationClass() \
948 { \
949 ::ndn::KeyChain::registerPib<PibType>({__VA_ARGS__}); \
950 } \
951} ndnCxxAuto ## PibType ## PibRegistrationVariable
952
953/**
954 * \brief Register SecTpm class in ndn-cxx KeyChain
955 *
956 * This macro should be placed once in the implementation file of the
957 * SecTpm type within the namespace where the type is declared.
958 */
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800959#define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...) \
Alexander Afanasyev07113802015-01-15 19:14:36 -0800960static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \
961{ \
962public: \
963 NdnCxxAuto ## TpmType ## TpmRegistrationClass() \
964 { \
965 ::ndn::KeyChain::registerTpm<TpmType>({__VA_ARGS__}); \
966 } \
967} ndnCxxAuto ## TpmType ## TpmRegistrationVariable
968
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700969} // namespace security
970
971using security::KeyChain;
972
Yingdi Yu6ab67812014-11-27 15:00:34 -0800973} // namespace ndn
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700974
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700975#endif // NDN_SECURITY_KEY_CHAIN_HPP