blob: 1973c777732afc16819f711a1e2d44fee1d68f45 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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 Yuf56c68f2014-04-24 21:50:13 -070034
Yingdi Yu4270f202014-01-28 14:19:16 -080035#include "../interest.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080036#include "../util/crypto.hpp"
Yingdi Yu0f5fb692014-06-10 12:07:28 -070037#include "../util/random.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080038
Jeff Thompson47c93cf2013-08-09 00:38:48 -070039
40namespace ndn {
41
Yingdi Yuf56c68f2014-04-24 21:50:13 -070042template<class TypePib, class TypeTpm>
43class KeyChainTraits
Yingdi Yu31b4af22014-01-14 14:13:00 -080044{
Jeff Thompson47c93cf2013-08-09 00:38:48 -070045public:
Yingdi Yuf56c68f2014-04-24 21:50:13 -070046 typedef TypePib Pib;
47 typedef TypeTpm Tpm;
48};
49
Alexander Afanasyev45a37132014-04-28 16:54:57 -070050class KeyChain : noncopyable
Yingdi Yuf56c68f2014-04-24 21:50:13 -070051{
52public:
53 class Error : public std::runtime_error
54 {
55 public:
56 explicit
57 Error(const std::string& what)
58 : std::runtime_error(what)
59 {
60 }
61 };
62
Yingdi Yu41546342014-11-30 23:37:53 -080063 /**
64 * This error is thrown when the TPM locator retrieved from PIB is
65 * different from what is supplied to the KeyChain constructor.
66 */
67 class MismatchError : public Error
68 {
69 public:
70 explicit
71 MismatchError(const std::string& what)
72 : Error(what)
73 {
74 }
75 };
Yingdi Yu7036ce22014-06-19 18:53:37 -070076
Yingdi Yuf56c68f2014-04-24 21:50:13 -070077 KeyChain();
78
79 template<class KeyChainTraits>
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070080 explicit
Yingdi Yuf56c68f2014-04-24 21:50:13 -070081 KeyChain(KeyChainTraits traits);
82
Yingdi Yu41546342014-11-30 23:37:53 -080083 /**
84 * @brief KeyChain constructor
85 *
86 * @sa http://redmine.named-data.net/issues/2260
87 *
88 * @param pibLocator
89 * @param tpmLocator
90 * @param allowReset if true, the PIB will be reset when the supplied tpmLocator
91 * mismatches the one in PIB
92 */
93 KeyChain(const std::string& pibLocator,
94 const std::string& tpmLocator,
95 bool allowReset = false);
Yingdi Yuf56c68f2014-04-24 21:50:13 -070096
97 virtual
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070098 ~KeyChain();
Yingdi Yuf56c68f2014-04-24 21:50:13 -070099
Yingdi Yube4150e2014-02-18 13:02:46 -0800100 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 * @brief Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a
102 * self-signed certificate of the KSK.
Yingdi Yube4150e2014-02-18 13:02:46 -0800103 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800104 * @param identityName The name of the identity.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700105 * @param params The key parameter if a key needs to be generated for the identity.
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800106 * @return The name of the default certificate of the identity.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800107 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700108 Name
109 createIdentity(const Name& identityName, const KeyParams& params = DEFAULT_KEY_PARAMS);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700110
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800111 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800112 * @brief Generate a pair of RSA keys for the specified identity.
113 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800114 * @param identityName The name of the identity.
115 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
116 * @param keySize The size of the key.
117 * @return The generated key name.
118 */
Yingdi Yu41546342014-11-30 23:37:53 -0800119 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700120 generateRsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 2048);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700121
Yingdi Yu41546342014-11-30 23:37:53 -0800122 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700123 generateEcdsaKeyPair(const Name& identityName, bool isKsk = false, uint32_t keySize = 256);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800124 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 * @brief Generate a pair of RSA keys for the specified identity and set it as default key for
126 * the identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800127 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800128 * @param identityName The name of the identity.
129 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
130 * @param keySize The size of the key.
131 * @return The generated key name.
132 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700133 Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700134 generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk = false,
135 uint32_t keySize = 2048);
136
137 Name
138 generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize = 256);
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700139
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800140 /**
Yingdi Yuc55680b2014-02-26 12:31:35 -0800141 * @brief prepare an unsigned identity certificate
142 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700143 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
Yingdi Yuc55680b2014-02-26 12:31:35 -0800144 * @param signingIdentity The signing identity.
145 * @param notBefore Refer to IdentityCertificate.
146 * @param notAfter Refer to IdentityCertificate.
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700147 * @param subjectDescription Refer to IdentityCertificate.
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700148 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
149 * certificate name according to the relation between the signingIdentity and
150 * the subject identity. If signingIdentity is a prefix of the subject identity,
151 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
152 * after subject identity (i.e., before `ksk-....`).
Yingdi Yuc55680b2014-02-26 12:31:35 -0800153 * @return IdentityCertificate.
154 */
155 shared_ptr<IdentityCertificate>
156 prepareUnsignedIdentityCertificate(const Name& keyName,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700157 const Name& signingIdentity,
158 const time::system_clock::TimePoint& notBefore,
159 const time::system_clock::TimePoint& notAfter,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700160 const std::vector<CertificateSubjectDescription>& subjectDescription,
161 const Name& certPrefix = DEFAULT_PREFIX);
162
163 /**
164 * @brief prepare an unsigned identity certificate
165 *
166 * @param keyName Key name, e.g., `/<identity_name>/ksk-123456`.
167 * @param publicKey Public key to sign.
168 * @param signingIdentity The signing identity.
169 * @param notBefore Refer to IdentityCertificate.
170 * @param notAfter Refer to IdentityCertificate.
171 * @param subjectDescription Refer to IdentityCertificate.
172 * @param certPrefix Prefix before `KEY` component. By default, KeyChain will infer the
173 * certificate name according to the relation between the signingIdentity and
174 * the subject identity. If signingIdentity is a prefix of the subject identity,
175 * `KEY` will be inserted after the signingIdentity, otherwise `KEY` is inserted
176 * after subject identity (i.e., before `ksk-....`).
177 * @return IdentityCertificate.
178 */
179 shared_ptr<IdentityCertificate>
180 prepareUnsignedIdentityCertificate(const Name& keyName,
181 const PublicKey& publicKey,
182 const Name& signingIdentity,
183 const time::system_clock::TimePoint& notBefore,
184 const time::system_clock::TimePoint& notAfter,
185 const std::vector<CertificateSubjectDescription>& subjectDescription,
186 const Name& certPrefix = DEFAULT_PREFIX);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800187
188 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189 * @brief Sign packet with default identity
190 *
Yingdi Yu60bd7082014-03-25 18:18:54 -0700191 * On return, signatureInfo and signatureValue in the packet are set.
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700192 * If default identity does not exist,
Yingdi Yu60bd7082014-03-25 18:18:54 -0700193 * a temporary identity will be created and set as default.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800194 *
195 * @param packet The packet to be signed
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800196 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800197 template<typename T>
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800198 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700199 sign(T& packet);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700200
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700201 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800202 * @brief Sign packet with a particular certificate.
203 *
204 * @param packet The packet to be signed.
205 * @param certificateName The certificate name of the key to use for signing.
206 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompson3c73da42013-08-12 11:19:05 -0700207 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800208 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700209 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700210 sign(T& packet, const Name& certificateName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700211
Jeff Thompson29ce3102013-09-27 11:47:48 -0700212 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800213 * @brief Sign the byte array using a particular certificate.
214 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700215 * @param buffer The byte array to be signed.
216 * @param bufferLength the length of buffer.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800217 * @param certificateName The certificate name of the signing key.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700218 * @return The Signature.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800219 * @throws SecPublicInfo::Error if certificate does not exist.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700220 */
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800221 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700222 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700223
224 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800225 * @brief Sign packet using the default certificate of a particular identity.
226 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700227 * If there is no default certificate of that identity, this method will create a self-signed
228 * certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800229 *
230 * @param packet The packet to be signed.
231 * @param identityName The signing identity name.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700232 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800233 template<typename T>
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700234 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700235 signByIdentity(T& packet, const Name& identityName);
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700236
Jeff Thompson3c73da42013-08-12 11:19:05 -0700237 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800238 * @brief Sign the byte array using the default certificate of a particular identity.
239 *
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700240 * @param buffer The byte array to be signed.
241 * @param bufferLength the length of buffer.
242 * @param identityName The identity name.
243 * @return The Signature.
244 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700245 Signature
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700246 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700247
248 /**
Yingdi Yu6ab67812014-11-27 15:00:34 -0800249 * @brief Set Sha256 weak signature for @p data
Yingdi Yu21157162014-02-28 13:02:34 -0800250 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700251 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700252 signWithSha256(Data& data);
Yingdi Yu21157162014-02-28 13:02:34 -0800253
254 /**
Yingdi Yu6ab67812014-11-27 15:00:34 -0800255 * @brief Set Sha256 weak signature for @p interest
256 */
257 void
258 signWithSha256(Interest& interest);
259
260 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800261 * @brief Generate a self-signed certificate for a public key.
262 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700263 * @param keyName The name of the public key
264 * @return The generated certificate, shared_ptr<IdentityCertificate>() if selfSign fails
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800265 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800266 shared_ptr<IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700267 selfSign(const Name& keyName);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800268
269 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800270 * @brief Self-sign the supplied identity certificate.
271 *
272 * @param cert The supplied cert.
273 * @throws SecTpm::Error if the private key does not exist.
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700274 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700275 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700276 selfSign(IdentityCertificate& cert);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800277
Yingdi Yu2e57a582014-02-20 23:34:43 -0800278 /**
279 * @brief delete a certificate.
280 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800281 * @param certificateName The certificate to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800282 * @throws KeyChain::Error if certificate cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800283 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700284 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700285 deleteCertificate(const Name& certificateName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800286
Yingdi Yu2e57a582014-02-20 23:34:43 -0800287 /**
288 * @brief delete a key.
289 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 * @param keyName The key to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800291 * @throws KeyChain::Error if key cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800292 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700293 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700294 deleteKey(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800295
Yingdi Yu2e57a582014-02-20 23:34:43 -0800296 /**
297 * @brief delete an identity.
298 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800299 * @param identity The identity to be deleted.
Yingdi Yu6147ef42014-12-08 17:48:32 -0800300 * @throws KeyChain::Error if identity cannot be deleted.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800301 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700302 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700303 deleteIdentity(const Name& identity);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800304
Yingdi Yu2e57a582014-02-20 23:34:43 -0800305 /**
306 * @brief export an identity.
307 *
308 * @param identity The identity to export.
309 * @param passwordStr The password to secure the private key.
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800310 * @return The encoded export data.
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700311 * @throws SecPublicInfo::Error if anything goes wrong in exporting.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800312 */
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800313 shared_ptr<SecuredBag>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700314 exportIdentity(const Name& identity, const std::string& passwordStr);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800315
Yingdi Yu2e57a582014-02-20 23:34:43 -0800316 /**
317 * @brief import an identity.
318 *
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800319 * @param securedBag The encoded import data.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800320 * @param passwordStr The password to secure the private key.
321 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800322 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700323 importIdentity(const SecuredBag& securedBag, const std::string& passwordStr);
324
325 SecPublicInfo&
326 getPib()
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800327 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700328 return *m_pib;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800329 }
330
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700331 const SecPublicInfo&
332 getPib() const
333 {
334 return *m_pib;
335 }
336
337 SecTpm&
338 getTpm()
339 {
340 return *m_tpm;
341 }
342
343 const SecTpm&
344 getTpm() const
345 {
346 return *m_tpm;
347 }
348
349 /*******************************
350 * Wrapper of SecPublicInfo *
351 *******************************/
352 bool
353 doesIdentityExist(const Name& identityName) const
354 {
355 return m_pib->doesIdentityExist(identityName);
356 }
357
358 void
359 addIdentity(const Name& identityName)
360 {
361 return m_pib->addIdentity(identityName);
362 }
363
364 bool
365 doesPublicKeyExist(const Name& keyName) const
366 {
367 return m_pib->doesPublicKeyExist(keyName);
368 }
369
370 void
371 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer)
372 {
Yingdi Yu41546342014-11-30 23:37:53 -0800373 return m_pib->addKey(keyName, publicKeyDer);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700374 }
375
Yingdi Yu7036ce22014-06-19 18:53:37 -0700376 void
377 addKey(const Name& keyName, const PublicKey& publicKeyDer)
378 {
379 return m_pib->addKey(keyName, publicKeyDer);
380 }
381
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700382 shared_ptr<PublicKey>
383 getPublicKey(const Name& keyName) const
384 {
385 return m_pib->getPublicKey(keyName);
386 }
387
388 bool
389 doesCertificateExist(const Name& certificateName) const
390 {
391 return m_pib->doesCertificateExist(certificateName);
392 }
393
394 void
395 addCertificate(const IdentityCertificate& certificate)
396 {
397 return m_pib->addCertificate(certificate);
398 }
399
400 shared_ptr<IdentityCertificate>
401 getCertificate(const Name& certificateName) const
402 {
403 return m_pib->getCertificate(certificateName);
404 }
405
406 Name
407 getDefaultIdentity() const
408 {
409 return m_pib->getDefaultIdentity();
410 }
411
412 Name
413 getDefaultKeyNameForIdentity(const Name& identityName) const
414 {
415 return m_pib->getDefaultKeyNameForIdentity(identityName);
416 }
417
418 Name
419 getDefaultCertificateNameForKey(const Name& keyName) const
420 {
421 return m_pib->getDefaultCertificateNameForKey(keyName);
422 }
423
424 void
425 getAllIdentities(std::vector<Name>& nameList, bool isDefault) const
426 {
427 return m_pib->getAllIdentities(nameList, isDefault);
428 }
429
430 void
431 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) const
432 {
433 return m_pib->getAllKeyNames(nameList, isDefault);
434 }
435
436 void
437 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) const
438 {
439 return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault);
440 }
441
442 void
443 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) const
444 {
445 return m_pib->getAllCertificateNames(nameList, isDefault);
446 }
447
448 void
449 getAllCertificateNamesOfKey(const Name& keyName,
450 std::vector<Name>& nameList,
451 bool isDefault) const
452 {
453 return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault);
454 }
455
456 void
457 deleteCertificateInfo(const Name& certificateName)
458 {
459 return m_pib->deleteCertificateInfo(certificateName);
460 }
461
462 void
463 deletePublicKeyInfo(const Name& keyName)
464 {
465 return m_pib->deletePublicKeyInfo(keyName);
466 }
467
468 void
469 deleteIdentityInfo(const Name& identity)
470 {
471 return m_pib->deleteIdentityInfo(identity);
472 }
473
474 void
475 setDefaultIdentity(const Name& identityName)
476 {
477 return m_pib->setDefaultIdentity(identityName);
478 }
479
480 void
481 setDefaultKeyNameForIdentity(const Name& keyName)
482 {
483 return m_pib->setDefaultKeyNameForIdentity(keyName);
484 }
485
486 void
487 setDefaultCertificateNameForKey(const Name& certificateName)
488 {
489 return m_pib->setDefaultCertificateNameForKey(certificateName);
490 }
491
492 Name
493 getNewKeyName(const Name& identityName, bool useKsk)
494 {
495 return m_pib->getNewKeyName(identityName, useKsk);
496 }
497
498 Name
499 getDefaultCertificateNameForIdentity(const Name& identityName) const
500 {
501 return m_pib->getDefaultCertificateNameForIdentity(identityName);
502 }
503
504 Name
505 getDefaultCertificateName() const
506 {
507 return m_pib->getDefaultCertificateName();
508 }
509
510 void
511 addCertificateAsKeyDefault(const IdentityCertificate& certificate)
512 {
513 return m_pib->addCertificateAsKeyDefault(certificate);
514 }
515
516 void
517 addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
518 {
519 return m_pib->addCertificateAsIdentityDefault(certificate);
520 }
521
522 void
523 addCertificateAsSystemDefault(const IdentityCertificate& certificate)
524 {
525 return m_pib->addCertificateAsSystemDefault(certificate);
526 }
527
528 shared_ptr<IdentityCertificate>
529 getDefaultCertificate() const
530 {
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700531 if (!static_cast<bool>(m_pib->getDefaultCertificate()))
532 const_cast<KeyChain*>(this)->setDefaultCertificateInternal();
533
534 return m_pib->getDefaultCertificate();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700535 }
536
537 void
538 refreshDefaultCertificate()
539 {
540 return m_pib->refreshDefaultCertificate();
541 }
542
543 /*******************************
544 * Wrapper of SecTpm *
545 *******************************/
546
547 void
548 setTpmPassword(const uint8_t* password, size_t passwordLength)
549 {
550 return m_tpm->setTpmPassword(password, passwordLength);
551 }
552
553 void
554 resetTpmPassword()
555 {
556 return m_tpm->resetTpmPassword();
557 }
558
559 void
560 setInTerminal(bool inTerminal)
561 {
562 return m_tpm->setInTerminal(inTerminal);
563 }
564
565 bool
566 getInTerminal() const
567 {
568 return m_tpm->getInTerminal();
569 }
570
571 bool
572 isLocked() const
573 {
574 return m_tpm->isLocked();
575 }
576
577 bool
578 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
579 {
580 return m_tpm->unlockTpm(password, passwordLength, usePassword);
581 }
582
583 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700584 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700585 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700586 return m_tpm->generateKeyPairInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700587 }
588
589 void
590 deleteKeyPairInTpm(const Name& keyName)
591 {
592 return m_tpm->deleteKeyPairInTpm(keyName);
593 }
594
595 shared_ptr<PublicKey>
596 getPublicKeyFromTpm(const Name& keyName) const
597 {
598 return m_tpm->getPublicKeyFromTpm(keyName);
599 }
600
601 Block
602 signInTpm(const uint8_t* data, size_t dataLength,
603 const Name& keyName,
604 DigestAlgorithm digestAlgorithm)
605 {
606 return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm);
607 }
608
609 ConstBufferPtr
610 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
611 {
612 return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric);
613 }
614
615 ConstBufferPtr
616 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
617 {
618 return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric);
619 }
620
621 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700622 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700623 {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700624 return m_tpm->generateSymmetricKeyInTpm(keyName, params);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700625 }
626
627 bool
628 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) const
629 {
630 return m_tpm->doesKeyExistInTpm(keyName, keyClass);
631 }
632
633 bool
634 generateRandomBlock(uint8_t* res, size_t size) const
635 {
636 return m_tpm->generateRandomBlock(res, size);
637 }
638
639 void
640 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
641 {
642 return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl);
643 }
644
645 ConstBufferPtr
646 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password)
647 {
648 return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password);
649 }
650
651 bool
652 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
653 const uint8_t* buf, size_t size,
654 const std::string& password)
655 {
656 return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password);
657 }
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700658
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800659private:
Yingdi Yu41546342014-11-30 23:37:53 -0800660 void
661 initialize(const std::string& pibLocator,
662 const std::string& tpmLocator,
663 bool needReset);
664
665 void
666 initializeTpm(const std::string& locator);
667
668 void
669 initializePib(const std::string& locator);
670
Yingdi Yu2e57a582014-02-20 23:34:43 -0800671 /**
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700672 * @brief Determine signature type
673 *
674 * An empty pointer will be returned if there is no valid signature.
675 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700676 shared_ptr<Signature>
677 determineSignatureWithPublicKey(const KeyLocator& keyLocator,
678 KeyType keyType,
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700679 DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256);
680
681 /**
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700682 * @brief Set default certificate if it is not initialized
683 */
684 void
685 setDefaultCertificateInternal();
686
687 /**
688 * @brief Sign a packet using a pariticular certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800689 *
690 * @param packet The packet to be signed.
691 * @param certificate The signing certificate.
692 */
693 template<typename T>
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800694 void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700695 sign(T& packet, const IdentityCertificate& certificate);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800696
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800697 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800698 * @brief Generate a key pair for the specified identity.
699 *
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800700 * @param identityName The name of the specified identity.
701 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
Yingdi Yu7036ce22014-06-19 18:53:37 -0700702 * @param params The parameter of the key.
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800703 * @return The name of the generated key.
704 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700705 Name
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700706 generateKeyPair(const Name& identityName, bool isKsk = false,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700707 const KeyParams& params = DEFAULT_KEY_PARAMS);
Yingdi Yu2abd73f2014-01-08 23:34:11 -0800708
Yingdi Yu8726f652014-01-23 10:35:12 -0800709 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800710 * @brief Sign the data using a particular key.
711 *
712 * @param data Reference to the data packet.
713 * @param signature Signature to be added.
Yingdi Yu8726f652014-01-23 10:35:12 -0800714 * @param keyName The name of the signing key.
715 * @param digestAlgorithm the digest algorithm.
716 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700717 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700718 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700719 signPacketWrapper(Data& data, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700720 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu8726f652014-01-23 10:35:12 -0800721
Yingdi Yu2e57a582014-02-20 23:34:43 -0800722 /**
723 * @brief Sign the interest using a particular key.
724 *
725 * @param interest Reference to the interest packet.
726 * @param signature Signature to be added.
727 * @param keyName The name of the signing key.
728 * @param digestAlgorithm the digest algorithm.
729 * @throws Tpm::Error
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700730 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700731 void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700732 signPacketWrapper(Interest& interest, const Signature& signature,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700733 const Name& keyName, DigestAlgorithm digestAlgorithm);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800734
Yingdi Yu41546342014-11-30 23:37:53 -0800735public:
736 static const Name DEFAULT_PREFIX;
737 // RsaKeyParams is set to be default for backward compatibility.
738 static const RsaKeyParams DEFAULT_KEY_PARAMS;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700739
740private:
741 SecPublicInfo* m_pib;
742 SecTpm* m_tpm;
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700743 time::milliseconds m_lastTimestamp;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700744};
745
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700746template<typename T>
747void
748KeyChain::sign(T& packet)
749{
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700750 if (!static_cast<bool>(m_pib->getDefaultCertificate()))
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700751 setDefaultCertificateInternal();
752
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700753 sign(packet, *m_pib->getDefaultCertificate());
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700754}
755
756template<typename T>
757void
758KeyChain::sign(T& packet, const Name& certificateName)
759{
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700760 shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName);
761 sign(packet, *certificate);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700762}
763
764template<typename T>
765void
766KeyChain::signByIdentity(T& packet, const Name& identityName)
767{
768 Name signingCertificateName;
769 try
770 {
771 signingCertificateName = m_pib->getDefaultCertificateNameForIdentity(identityName);
772 }
773 catch (SecPublicInfo::Error& e)
774 {
775 signingCertificateName = createIdentity(identityName);
776 // Ideally, no exception will be thrown out, unless something goes wrong in the TPM, which
777 // is a fatal error.
778 }
779
780 // We either get or create the signing certificate, sign packet! (no exception unless fatal
781 // error in TPM)
782 sign(packet, signingCertificateName);
783}
784
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700785template<typename T>
786void
787KeyChain::sign(T& packet, const IdentityCertificate& certificate)
788{
Yingdi Yu4a557052014-07-09 16:40:37 -0700789 KeyLocator keyLocator(certificate.getName().getPrefix(-1));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700790
Yingdi Yu4a557052014-07-09 16:40:37 -0700791 shared_ptr<Signature> signature =
792 determineSignatureWithPublicKey(keyLocator, certificate.getPublicKeyInfo().getKeyType());
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700793
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700794 if (!static_cast<bool>(signature))
795 throw SecPublicInfo::Error("unknown key type!");
796
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700797 signPacketWrapper(packet, *signature,
798 certificate.getPublicKeyName(),
799 DIGEST_ALGORITHM_SHA256);
800
801 return;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700802}
803
Yingdi Yu6ab67812014-11-27 15:00:34 -0800804} // namespace ndn
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700805
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700806#endif // NDN_SECURITY_KEY_CHAIN_HPP