blob: f70bf05b5eb4b84c18ac190023def806ae80e18d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yuf56c68f2014-04-24 21:50:13 -07002/**
Yingdi Yufe4733a2015-10-22 14:24:12 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuf56c68f2014-04-24 21:50:13 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Yingdi Yuf56c68f2014-04-24 21:50:13 -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.
Yingdi Yuf56c68f2014-04-24 21:50:13 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 */
23
24#include "key-chain.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080025#include "../signing-helpers.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070026
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080027#include "../../util/random.hpp"
28#include "../../util/config-file.hpp"
Alexander Afanasyev07113802015-01-15 19:14:36 -080029
Yingdi Yuf56c68f2014-04-24 21:50:13 -070030#include "sec-public-info-sqlite3.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070031
32#ifdef NDN_CXX_HAVE_OSX_SECURITY
33#include "sec-tpm-osx.hpp"
Alexander Afanasyev07113802015-01-15 19:14:36 -080034#endif // NDN_CXX_HAVE_OSX_SECURITY
Yingdi Yuf56c68f2014-04-24 21:50:13 -070035
Alexander Afanasyev07113802015-01-15 19:14:36 -080036#include "sec-tpm-file.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070037
38namespace ndn {
Yingdi Yu1b0311c2015-06-10 14:58:47 -070039namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080040namespace v1 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070041
Yingdi Yu0eb5d722014-06-10 15:06:25 -070042// Use a GUID as a magic number of KeyChain::DEFAULT_PREFIX identifier
43const Name KeyChain::DEFAULT_PREFIX("/723821fd-f534-44b3-80d9-44bf5f58bbbb");
Yingdi Yu1b0311c2015-06-10 14:58:47 -070044
45// Note: cannot use default constructor, as it depends on static variables which may or may not be
46// initialized at this point
47const SigningInfo KeyChain::DEFAULT_SIGNING_INFO(SigningInfo::SIGNER_TYPE_NULL, Name(), SignatureInfo());
Yingdi Yu0eb5d722014-06-10 15:06:25 -070048
Yingdi Yu7036ce22014-06-19 18:53:37 -070049const RsaKeyParams KeyChain::DEFAULT_KEY_PARAMS;
50
Alexander Afanasyev07113802015-01-15 19:14:36 -080051const std::string DEFAULT_PIB_SCHEME = "pib-sqlite3";
52
53#if defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN)
54const std::string DEFAULT_TPM_SCHEME = "tpm-osxkeychain";
55#else
56const std::string DEFAULT_TPM_SCHEME = "tpm-file";
57#endif // defined(NDN_CXX_HAVE_OSX_SECURITY) and defined(NDN_CXX_WITH_OSX_KEYCHAIN)
58
59// When static library is used, not everything is compiled into the resulting binary.
60// Therefore, the following standard PIB and TPMs need to be registered here.
61// http://stackoverflow.com/q/9459980/2150331
62//
63// Also, cannot use Type::SCHEME, as its value may be uninitialized
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080064NDN_CXX_V1_KEYCHAIN_REGISTER_PIB(SecPublicInfoSqlite3, "pib-sqlite3", "sqlite3");
Alexander Afanasyev07113802015-01-15 19:14:36 -080065
66#ifdef NDN_CXX_HAVE_OSX_SECURITY
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080067NDN_CXX_V1_KEYCHAIN_REGISTER_TPM(SecTpmOsx, "tpm-osxkeychain", "osx-keychain");
Alexander Afanasyev07113802015-01-15 19:14:36 -080068#endif // NDN_CXX_HAVE_OSX_SECURITY
69
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080070NDN_CXX_V1_KEYCHAIN_REGISTER_TPM(SecTpmFile, "tpm-file", "file");
Alexander Afanasyev07113802015-01-15 19:14:36 -080071
Alexander Afanasyev34a37632015-01-16 17:37:36 -080072template<class T>
73struct Factory
74{
75 Factory(const std::string& canonicalName, const T& create)
76 : canonicalName(canonicalName)
77 , create(create)
78 {
79 }
80
81 std::string canonicalName;
82 T create;
83};
84typedef Factory<KeyChain::PibCreateFunc> PibFactory;
85typedef Factory<KeyChain::TpmCreateFunc> TpmFactory;
86
87static std::map<std::string, PibFactory>&
Alexander Afanasyev07113802015-01-15 19:14:36 -080088getPibFactories()
89{
Alexander Afanasyev34a37632015-01-16 17:37:36 -080090 static std::map<std::string, PibFactory> pibFactories;
Alexander Afanasyev07113802015-01-15 19:14:36 -080091 return pibFactories;
92}
93
Alexander Afanasyev34a37632015-01-16 17:37:36 -080094static std::map<std::string, TpmFactory>&
Alexander Afanasyev07113802015-01-15 19:14:36 -080095getTpmFactories()
96{
Alexander Afanasyev34a37632015-01-16 17:37:36 -080097 static std::map<std::string, TpmFactory> tpmFactories;
Alexander Afanasyev07113802015-01-15 19:14:36 -080098 return tpmFactories;
99}
100
101void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800102KeyChain::registerPibImpl(const std::string& canonicalName,
103 std::initializer_list<std::string> aliases,
Alexander Afanasyev07113802015-01-15 19:14:36 -0800104 KeyChain::PibCreateFunc createFunc)
105{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800106 for (const std::string& alias : aliases) {
107 getPibFactories().insert(make_pair(alias, PibFactory(canonicalName, createFunc)));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800108 }
109}
110
111void
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800112KeyChain::registerTpmImpl(const std::string& canonicalName,
113 std::initializer_list<std::string> aliases,
Alexander Afanasyev07113802015-01-15 19:14:36 -0800114 KeyChain::TpmCreateFunc createFunc)
115{
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800116 for (const std::string& alias : aliases) {
117 getTpmFactories().insert(make_pair(alias, TpmFactory(canonicalName, createFunc)));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800118 }
119}
120
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700121KeyChain::KeyChain()
Yingdi Yu41546342014-11-30 23:37:53 -0800122 : m_pib(nullptr)
123 , m_tpm(nullptr)
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700124 , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now()))
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700125{
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700126 std::string pibLocator;
127 std::string tpmLocator;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700128
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700129 if (getenv("NDN_CLIENT_PIB") != nullptr) {
130 pibLocator = getenv("NDN_CLIENT_PIB");
131 }
132
133 if (getenv("NDN_CLIENT_TPM") != nullptr) {
134 tpmLocator = getenv("NDN_CLIENT_TPM");
135 }
136
137 if (pibLocator.empty() || tpmLocator.empty()) {
138 ConfigFile config;
139 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
140
141 if (pibLocator.empty()) {
142 pibLocator = parsed.get<std::string>("pib", "");
143 }
144
145 if (tpmLocator.empty()) {
146 tpmLocator = parsed.get<std::string>("tpm", "");
147 }
148 }
Alexander Afanasyev07113802015-01-15 19:14:36 -0800149
150 initialize(pibLocator, tpmLocator, false);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700151}
152
153KeyChain::KeyChain(const std::string& pibName,
Yingdi Yu41546342014-11-30 23:37:53 -0800154 const std::string& tpmName,
155 bool allowReset)
156 : m_pib(nullptr)
157 , m_tpm(nullptr)
Yingdi Yu0f5fb692014-06-10 12:07:28 -0700158 , m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now()))
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700159{
Alexander Afanasyev07113802015-01-15 19:14:36 -0800160 initialize(pibName, tpmName, allowReset);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700161}
162
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700163KeyChain::~KeyChain()
164{
Alexander Afanasyev07113802015-01-15 19:14:36 -0800165}
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700166
Alexander Afanasyev07113802015-01-15 19:14:36 -0800167static inline std::tuple<std::string/*type*/, std::string/*location*/>
168parseUri(const std::string& uri)
169{
170 size_t pos = uri.find(':');
171 if (pos != std::string::npos) {
172 return std::make_tuple(uri.substr(0, pos),
173 uri.substr(pos + 1));
174 }
175 else {
176 return std::make_tuple(uri, "");
177 }
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700178}
179
Yingdi Yu281689a2015-06-13 14:32:32 -0700180std::string
181KeyChain::getDefaultPibLocator()
Yingdi Yu41546342014-11-30 23:37:53 -0800182{
Yingdi Yu281689a2015-06-13 14:32:32 -0700183 std::string defaultPibLocator = DEFAULT_PIB_SCHEME + ":";
184 return defaultPibLocator;
185}
Yingdi Yu41546342014-11-30 23:37:53 -0800186
Yingdi Yu281689a2015-06-13 14:32:32 -0700187static inline std::tuple<std::string/*type*/, std::string/*location*/>
188getCanonicalPibLocator(const std::string& pibLocator)
189{
Alexander Afanasyev07113802015-01-15 19:14:36 -0800190 std::string pibScheme, pibLocation;
Yingdi Yu281689a2015-06-13 14:32:32 -0700191 std::tie(pibScheme, pibLocation) = parseUri(pibLocator);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800192
Alexander Afanasyev07113802015-01-15 19:14:36 -0800193 if (pibScheme.empty()) {
194 pibScheme = DEFAULT_PIB_SCHEME;
195 }
Yingdi Yu281689a2015-06-13 14:32:32 -0700196
Alexander Afanasyev07113802015-01-15 19:14:36 -0800197 auto pibFactory = getPibFactories().find(pibScheme);
198 if (pibFactory == getPibFactories().end()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700199 BOOST_THROW_EXCEPTION(KeyChain::Error("PIB scheme '" + pibScheme + "' is not supported"));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800200 }
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800201 pibScheme = pibFactory->second.canonicalName;
Alexander Afanasyev07113802015-01-15 19:14:36 -0800202
Yingdi Yu281689a2015-06-13 14:32:32 -0700203 return std::make_tuple(pibScheme, pibLocation);
204}
205
206unique_ptr<SecPublicInfo>
207KeyChain::createPib(const std::string& pibLocator)
208{
209 BOOST_ASSERT(!getPibFactories().empty());
210
211 std::string pibScheme, pibLocation;
212 std::tie(pibScheme, pibLocation) = getCanonicalPibLocator(pibLocator);
213 auto pibFactory = getPibFactories().find(pibScheme);
214 BOOST_ASSERT(pibFactory != getPibFactories().end());
215 return pibFactory->second.create(pibLocation);
216}
217
218std::string
219KeyChain::getDefaultTpmLocator()
220{
221 std::string defaultTpmLocator = DEFAULT_TPM_SCHEME + ":";
222 return defaultTpmLocator;
223}
224
225static inline std::tuple<std::string/*type*/, std::string/*location*/>
226getCanonicalTpmLocator(const std::string& tpmLocator)
227{
228 std::string tpmScheme, tpmLocation;
229 std::tie(tpmScheme, tpmLocation) = parseUri(tpmLocator);
230
Alexander Afanasyev07113802015-01-15 19:14:36 -0800231 if (tpmScheme.empty()) {
232 tpmScheme = DEFAULT_TPM_SCHEME;
233 }
234 auto tpmFactory = getTpmFactories().find(tpmScheme);
235 if (tpmFactory == getTpmFactories().end()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700236 BOOST_THROW_EXCEPTION(KeyChain::Error("TPM scheme '" + tpmScheme + "' is not supported"));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800237 }
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800238 tpmScheme = tpmFactory->second.canonicalName;
Alexander Afanasyev07113802015-01-15 19:14:36 -0800239
Yingdi Yu281689a2015-06-13 14:32:32 -0700240 return std::make_tuple(tpmScheme, tpmLocation);
241}
Alexander Afanasyev07113802015-01-15 19:14:36 -0800242
Yingdi Yu281689a2015-06-13 14:32:32 -0700243unique_ptr<SecTpm>
244KeyChain::createTpm(const std::string& tpmLocator)
245{
246 BOOST_ASSERT(!getTpmFactories().empty());
247
248 std::string tpmScheme, tpmLocation;
249 std::tie(tpmScheme, tpmLocation) = getCanonicalTpmLocator(tpmLocator);
250 auto tpmFactory = getTpmFactories().find(tpmScheme);
251 BOOST_ASSERT(tpmFactory != getTpmFactories().end());
252 return tpmFactory->second.create(tpmLocation);
253}
254
255void
256KeyChain::initialize(const std::string& pibLocator,
257 const std::string& tpmLocator,
258 bool allowReset)
259{
260 // PIB Locator
261 std::string pibScheme, pibLocation;
262 std::tie(pibScheme, pibLocation) = getCanonicalPibLocator(pibLocator);
263 std::string canonicalPibLocator = pibScheme + ":" + pibLocation;
264
265 // Create PIB
266 m_pib = createPib(canonicalPibLocator);
267
268 // TPM Locator
269 std::string tpmScheme, tpmLocation;
270 std::tie(tpmScheme, tpmLocation) = getCanonicalTpmLocator(tpmLocator);
271 std::string canonicalTpmLocator = tpmScheme + ":" + tpmLocation;
Alexander Afanasyev07113802015-01-15 19:14:36 -0800272
273 // Create TPM, checking that it matches to the previously associated one
Yingdi Yu41546342014-11-30 23:37:53 -0800274 try {
Alexander Afanasyev07113802015-01-15 19:14:36 -0800275 if (!allowReset &&
Yingdi Yu281689a2015-06-13 14:32:32 -0700276 !m_pib->getTpmLocator().empty() && m_pib->getTpmLocator() != canonicalTpmLocator)
Alexander Afanasyev07113802015-01-15 19:14:36 -0800277 // Tpm mismatch, but we do not want to reset PIB
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700278 BOOST_THROW_EXCEPTION(MismatchError("TPM locator supplied does not match TPM locator in PIB: "
279 + m_pib->getTpmLocator() + " != " + canonicalTpmLocator));
Yingdi Yu41546342014-11-30 23:37:53 -0800280 }
José Quevedo641de4c2016-01-29 00:11:24 +0000281 catch (const SecPublicInfo::Error&) {
Yingdi Yu41546342014-11-30 23:37:53 -0800282 // TPM locator is not set in PIB yet.
283 }
284
Alexander Afanasyev07113802015-01-15 19:14:36 -0800285 // note that key mismatch may still happen if the TPM locator is initially set to a
286 // wrong one or if the PIB was shared by more than one TPMs before. This is due to the
287 // old PIB does not have TPM info, new pib should not have this problem.
Yingdi Yu281689a2015-06-13 14:32:32 -0700288 m_tpm = createTpm(canonicalTpmLocator);
289 m_pib->setTpmLocator(canonicalTpmLocator);
Yingdi Yu41546342014-11-30 23:37:53 -0800290}
291
Yingdi Yu7036ce22014-06-19 18:53:37 -0700292Name
293KeyChain::createIdentity(const Name& identityName, const KeyParams& params)
294{
295 m_pib->addIdentity(identityName);
296
297 Name keyName;
José Quevedo641de4c2016-01-29 00:11:24 +0000298 try {
299 keyName = m_pib->getDefaultKeyNameForIdentity(identityName);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700300
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800301 shared_ptr<PublicKey> key = m_pib->getPublicKey(keyName);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700302
José Quevedo641de4c2016-01-29 00:11:24 +0000303 if (key->getKeyType() != params.getKeyType()) {
Yingdi Yu7036ce22014-06-19 18:53:37 -0700304 keyName = generateKeyPair(identityName, true, params);
305 m_pib->setDefaultKeyNameForIdentity(keyName);
306 }
José Quevedo641de4c2016-01-29 00:11:24 +0000307 }
308 catch (const SecPublicInfo::Error& e) {
309 keyName = generateKeyPair(identityName, true, params);
310 m_pib->setDefaultKeyNameForIdentity(keyName);
311 }
Yingdi Yu7036ce22014-06-19 18:53:37 -0700312
313 Name certName;
José Quevedo641de4c2016-01-29 00:11:24 +0000314 try {
315 certName = m_pib->getDefaultCertificateNameForKey(keyName);
316 }
317 catch (const SecPublicInfo::Error& e) {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800318 shared_ptr<IdentityCertificate> selfCert = selfSign(keyName);
José Quevedo641de4c2016-01-29 00:11:24 +0000319 m_pib->addCertificateAsIdentityDefault(*selfCert);
320 certName = selfCert->getName();
321 }
Yingdi Yu7036ce22014-06-19 18:53:37 -0700322
323 return certName;
324}
325
326Name
Yingdi Yu41546342014-11-30 23:37:53 -0800327KeyChain::generateRsaKeyPair(const Name& identityName, bool isKsk, uint32_t keySize)
328{
329 RsaKeyParams params(keySize);
330 return generateKeyPair(identityName, isKsk, params);
331}
332
333Name
334KeyChain::generateEcdsaKeyPair(const Name& identityName, bool isKsk, uint32_t keySize)
335{
336 EcdsaKeyParams params(keySize);
337 return generateKeyPair(identityName, isKsk, params);
338}
339
340Name
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700341KeyChain::generateRsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize)
Yingdi Yu7036ce22014-06-19 18:53:37 -0700342{
343 RsaKeyParams params(keySize);
344
345 Name keyName = generateKeyPair(identityName, isKsk, params);
346
347 m_pib->setDefaultKeyNameForIdentity(keyName);
348
349 return keyName;
350}
351
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700352Name
353KeyChain::generateEcdsaKeyPairAsDefault(const Name& identityName, bool isKsk, uint32_t keySize)
354{
355 EcdsaKeyParams params(keySize);
356
357 Name keyName = generateKeyPair(identityName, isKsk, params);
358
359 m_pib->setDefaultKeyNameForIdentity(keyName);
360
361 return keyName;
362}
363
364
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800365shared_ptr<IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700366KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName,
367 const Name& signingIdentity,
368 const time::system_clock::TimePoint& notBefore,
369 const time::system_clock::TimePoint& notAfter,
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800370 const std::vector<CertificateSubjectDescription>& subjectDescription,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700371 const Name& certPrefix)
372{
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800373 shared_ptr<PublicKey> publicKey;
José Quevedo641de4c2016-01-29 00:11:24 +0000374 try {
375 publicKey = m_pib->getPublicKey(keyName);
376 }
377 catch (const SecPublicInfo::Error& e) {
378 return nullptr;
379 }
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700380
381 return prepareUnsignedIdentityCertificate(keyName, *publicKey, signingIdentity,
382 notBefore, notAfter,
383 subjectDescription, certPrefix);
384}
385
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800386shared_ptr<IdentityCertificate>
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700387KeyChain::prepareUnsignedIdentityCertificate(const Name& keyName,
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800388 const PublicKey& publicKey,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700389 const Name& signingIdentity,
390 const time::system_clock::TimePoint& notBefore,
391 const time::system_clock::TimePoint& notAfter,
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800392 const std::vector<CertificateSubjectDescription>& subjectDescription,
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700393 const Name& certPrefix)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700394{
395 if (keyName.size() < 1)
José Quevedo641de4c2016-01-29 00:11:24 +0000396 return nullptr;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700397
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700398 std::string keyIdPrefix = keyName.get(-1).toUri().substr(0, 4);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700399 if (keyIdPrefix != "ksk-" && keyIdPrefix != "dsk-")
José Quevedo641de4c2016-01-29 00:11:24 +0000400 return nullptr;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700401
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700402 Name certName;
403
José Quevedo641de4c2016-01-29 00:11:24 +0000404 if (certPrefix == KeyChain::DEFAULT_PREFIX) {
405 // No certificate prefix hint, infer the prefix
406 if (signingIdentity.isPrefixOf(keyName))
407 certName.append(signingIdentity)
408 .append("KEY")
409 .append(keyName.getSubName(signingIdentity.size()))
410 .append("ID-CERT")
411 .appendVersion();
412 else
413 certName.append(keyName.getPrefix(-1))
414 .append("KEY")
415 .append(keyName.get(-1))
416 .append("ID-CERT")
417 .appendVersion();
418 }
419 else {
420 // cert prefix hint is supplied, determine the cert name.
421 if (certPrefix.isPrefixOf(keyName) && certPrefix != keyName)
422 certName.append(certPrefix)
423 .append("KEY")
424 .append(keyName.getSubName(certPrefix.size()))
425 .append("ID-CERT")
426 .appendVersion();
427 else
428 return nullptr;
429 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700430
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800431 auto certificate = make_shared<IdentityCertificate>();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700432 certificate->setName(certName);
433 certificate->setNotBefore(notBefore);
434 certificate->setNotAfter(notAfter);
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700435 certificate->setPublicKeyInfo(publicKey);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700436
José Quevedo641de4c2016-01-29 00:11:24 +0000437 if (subjectDescription.empty()) {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800438 CertificateSubjectDescription subjectName(oid::ATTRIBUTE_NAME, keyName.getPrefix(-1).toUri());
José Quevedo641de4c2016-01-29 00:11:24 +0000439 certificate->addSubjectDescription(subjectName);
440 }
441 else {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800442 std::vector<CertificateSubjectDescription>::const_iterator sdIt = subjectDescription.begin();
443 std::vector<CertificateSubjectDescription>::const_iterator sdEnd = subjectDescription.end();
José Quevedo641de4c2016-01-29 00:11:24 +0000444 for(; sdIt != sdEnd; sdIt++)
445 certificate->addSubjectDescription(*sdIt);
446 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700447
448 certificate->encode();
449
450 return certificate;
451}
452
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700453std::tuple<Name, SignatureInfo>
454KeyChain::prepareSignatureInfo(const SigningInfo& params)
455{
456 SignatureInfo sigInfo = params.getSignatureInfo();
457
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800458 shared_ptr<IdentityCertificate> signingCert;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700459
460 switch (params.getSignerType()) {
José Quevedo641de4c2016-01-29 00:11:24 +0000461 case SigningInfo::SIGNER_TYPE_NULL: {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700462 if (m_pib->getDefaultCertificate() == nullptr)
463 setDefaultCertificateInternal();
464
465 signingCert = m_pib->getDefaultCertificate();
466 break;
467 }
José Quevedo641de4c2016-01-29 00:11:24 +0000468 case SigningInfo::SIGNER_TYPE_ID: {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700469 Name signingCertName;
470 try {
471 signingCertName = m_pib->getDefaultCertificateNameForIdentity(params.getSignerName());
472 }
José Quevedo641de4c2016-01-29 00:11:24 +0000473 catch (const SecPublicInfo::Error&) {
474 signingCertName = createIdentity(params.getSignerName(), getDefaultKeyParamsForIdentity(params.getSignerName()));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700475 }
476
477 signingCert = m_pib->getCertificate(signingCertName);
478
479 break;
480 }
José Quevedo641de4c2016-01-29 00:11:24 +0000481 case SigningInfo::SIGNER_TYPE_KEY: {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700482 Name signingCertName;
483 try {
484 signingCertName = m_pib->getDefaultCertificateNameForKey(params.getSignerName());
485 }
José Quevedo641de4c2016-01-29 00:11:24 +0000486 catch (const SecPublicInfo::Error&) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700487 BOOST_THROW_EXCEPTION(Error("signing certificate does not exist"));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700488 }
489
490 signingCert = m_pib->getCertificate(signingCertName);
491
492 break;
493 }
José Quevedo641de4c2016-01-29 00:11:24 +0000494 case SigningInfo::SIGNER_TYPE_CERT: {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700495 signingCert = m_pib->getCertificate(params.getSignerName());
496 if (signingCert == nullptr)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700497 BOOST_THROW_EXCEPTION(Error("signing certificate does not exist"));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700498
499 break;
500 }
José Quevedo641de4c2016-01-29 00:11:24 +0000501 case SigningInfo::SIGNER_TYPE_SHA256: {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700502 sigInfo.setSignatureType(tlv::DigestSha256);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700503 return std::make_tuple(SigningInfo::getDigestSha256Identity(), sigInfo);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700504 }
José Quevedo641de4c2016-01-29 00:11:24 +0000505 default:
506 BOOST_THROW_EXCEPTION(Error("Unrecognized signer type"));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700507 }
508
509 sigInfo.setSignatureType(getSignatureType(signingCert->getPublicKeyInfo().getKeyType(),
510 params.getDigestAlgorithm()));
511 sigInfo.setKeyLocator(KeyLocator(signingCert->getName().getPrefix(-1)));
512
513 return std::make_tuple(signingCert->getPublicKeyName(), sigInfo);
514}
515
516void
517KeyChain::sign(Data& data, const SigningInfo& params)
518{
519 signImpl(data, params);
520}
521
522void
523KeyChain::sign(Interest& interest, const SigningInfo& params)
524{
525 signImpl(interest, params);
526}
527
528Block
529KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params)
530{
531 Name keyName;
532 SignatureInfo sigInfo;
533 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
Yingdi Yu99b2a002015-08-12 12:47:44 -0700534 return pureSign(buffer, bufferLength, keyName, DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700535}
536
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700537Signature
538KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName)
539{
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800540 shared_ptr<IdentityCertificate> certificate = m_pib->getCertificate(certificateName);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700541
José Quevedo641de4c2016-01-29 00:11:24 +0000542 if (certificate == nullptr) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700543 BOOST_THROW_EXCEPTION(SecPublicInfo::Error("certificate does not exist"));
José Quevedo641de4c2016-01-29 00:11:24 +0000544 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700545
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700546 Signature sig;
Yingdi Yu4a557052014-07-09 16:40:37 -0700547
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700548 // For temporary usage, we support SHA256 only, but will support more.
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700549 sig.setValue(m_tpm->signInTpm(buffer, bufferLength,
550 certificate->getPublicKeyName(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700551 DigestAlgorithm::SHA256));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700552
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700553 return sig;
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700554}
555
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800556shared_ptr<IdentityCertificate>
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700557KeyChain::selfSign(const Name& keyName)
558{
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800559 shared_ptr<PublicKey> pubKey;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700560 try {
561 pubKey = m_pib->getPublicKey(keyName); // may throw an exception.
562 }
José Quevedo641de4c2016-01-29 00:11:24 +0000563 catch (const SecPublicInfo::Error&) {
564 return nullptr;
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700565 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700566
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800567 auto certificate = make_shared<IdentityCertificate>();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700568
569 Name certificateName = keyName.getPrefix(-1);
570 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
571
572 certificate->setName(certificateName);
573 certificate->setNotBefore(time::system_clock::now());
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700574 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700575 certificate->setPublicKeyInfo(*pubKey);
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800576 certificate->addSubjectDescription(CertificateSubjectDescription(oid::ATTRIBUTE_NAME,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700577 keyName.toUri()));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700578 certificate->encode();
579
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700580 certificate->setSignature(Signature(SignatureInfo()));
581
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700582 selfSign(*certificate);
583 return certificate;
584}
585
586void
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800587KeyChain::selfSign(IdentityCertificate& cert)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700588{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700589 Name keyName = cert.getPublicKeyName();
Yingdi Yu99b2a002015-08-12 12:47:44 -0700590
591 if (!m_tpm->doesKeyExistInTpm(keyName, KeyClass::PRIVATE))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700592 BOOST_THROW_EXCEPTION(SecTpm::Error("Private key does not exist"));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700593
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700594 SignatureInfo sigInfo(cert.getSignature().getInfo());
595 sigInfo.setKeyLocator(KeyLocator(cert.getName().getPrefix(-1)));
596 sigInfo.setSignatureType(getSignatureType(cert.getPublicKeyInfo().getKeyType(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700597 DigestAlgorithm::SHA256));
Yingdi Yu4a557052014-07-09 16:40:37 -0700598
Yingdi Yu99b2a002015-08-12 12:47:44 -0700599 signPacketWrapper(cert, Signature(sigInfo), keyName, DigestAlgorithm::SHA256);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700600}
601
602shared_ptr<SecuredBag>
603KeyChain::exportIdentity(const Name& identity, const std::string& passwordStr)
604{
605 if (!m_pib->doesIdentityExist(identity))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700606 BOOST_THROW_EXCEPTION(SecPublicInfo::Error("Identity does not exist"));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700607
608 Name keyName = m_pib->getDefaultKeyNameForIdentity(identity);
609
610 ConstBufferPtr pkcs5;
José Quevedo641de4c2016-01-29 00:11:24 +0000611 try {
612 pkcs5 = m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, passwordStr);
613 }
614 catch (const SecTpm::Error& e) {
615 BOOST_THROW_EXCEPTION(SecPublicInfo::Error("Fail to export PKCS5 of private key"));
616 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700617
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800618 shared_ptr<IdentityCertificate> cert;
José Quevedo641de4c2016-01-29 00:11:24 +0000619 try {
620 cert = m_pib->getCertificate(m_pib->getDefaultCertificateNameForKey(keyName));
621 }
622 catch (const SecPublicInfo::Error& e) {
623 cert = selfSign(keyName);
624 m_pib->addCertificateAsIdentityDefault(*cert);
625 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700626
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700627 // make_shared on OSX 10.9 has some strange problem here
José Quevedo641de4c2016-01-29 00:11:24 +0000628 return shared_ptr<SecuredBag>(new SecuredBag(*cert, pkcs5));
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700629}
630
631void
632KeyChain::importIdentity(const SecuredBag& securedBag, const std::string& passwordStr)
633{
634 Name certificateName = securedBag.getCertificate().getName();
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800635 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700636 Name identity = keyName.getPrefix(-1);
637
638 // Add identity
639 m_pib->addIdentity(identity);
640
641 // Add key
642 m_tpm->importPrivateKeyPkcs5IntoTpm(keyName,
643 securedBag.getKey()->buf(),
644 securedBag.getKey()->size(),
645 passwordStr);
646
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800647 shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri());
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700648 // HACK! We should set key type according to the pkcs8 info.
Yingdi Yu41546342014-11-30 23:37:53 -0800649 m_pib->addKey(keyName, *pubKey);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700650 m_pib->setDefaultKeyNameForIdentity(keyName);
651
652 // Add cert
653 m_pib->addCertificateAsIdentityDefault(securedBag.getCertificate());
654}
655
José Quevedo641de4c2016-01-29 00:11:24 +0000656const KeyParams&
657KeyChain::getDefaultKeyParamsForIdentity(const Name &identityName) const
658{
Yingdi Yu99b2a002015-08-12 12:47:44 -0700659 KeyType keyType = KeyType::NONE;
José Quevedo641de4c2016-01-29 00:11:24 +0000660 try {
661 keyType = m_pib->getPublicKeyType(m_pib->getDefaultKeyNameForIdentity(identityName));
662 }
663 catch (const SecPublicInfo::Error& e) { // @TODO Switch to Pib::Error
664 return DEFAULT_KEY_PARAMS;
665 }
666
667 switch (keyType) {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700668 case KeyType::RSA: {
José Quevedo641de4c2016-01-29 00:11:24 +0000669 static RsaKeyParams defaultRsaParams;
670 return defaultRsaParams;
671 }
Yingdi Yu99b2a002015-08-12 12:47:44 -0700672 case KeyType::EC: {
José Quevedo641de4c2016-01-29 00:11:24 +0000673 static EcdsaKeyParams defaultEcdsaParams;
674 return defaultEcdsaParams;
675 }
Yingdi Yu99b2a002015-08-12 12:47:44 -0700676 case KeyType::NONE: {
José Quevedo641de4c2016-01-29 00:11:24 +0000677 return DEFAULT_KEY_PARAMS;
678 }
679 default:
680 BOOST_THROW_EXCEPTION(Error("Unsupported key type"));
681 }
682}
683
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700684void
685KeyChain::setDefaultCertificateInternal()
686{
687 m_pib->refreshDefaultCertificate();
688
José Quevedo641de4c2016-01-29 00:11:24 +0000689 if (m_pib->getDefaultCertificate() == nullptr) {
690 Name defaultIdentity;
691 try {
692 defaultIdentity = m_pib->getDefaultIdentity();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700693 }
José Quevedo641de4c2016-01-29 00:11:24 +0000694 catch (const SecPublicInfo::Error& e) {
695 uint32_t random = random::generateWord32();
696 defaultIdentity.append("tmp-identity")
697 .append(reinterpret_cast<uint8_t*>(&random), 4);
698 }
699 createIdentity(defaultIdentity);
700 m_pib->setDefaultIdentity(defaultIdentity);
701 m_pib->refreshDefaultCertificate();
702 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700703}
704
Yingdi Yu7036ce22014-06-19 18:53:37 -0700705Name
706KeyChain::generateKeyPair(const Name& identityName, bool isKsk, const KeyParams& params)
707{
708 Name keyName = m_pib->getNewKeyName(identityName, isKsk);
709
710 m_tpm->generateKeyPairInTpm(keyName.toUri(), params);
711
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800712 shared_ptr<PublicKey> pubKey = m_tpm->getPublicKeyFromTpm(keyName.toUri());
Yingdi Yu7036ce22014-06-19 18:53:37 -0700713 m_pib->addKey(keyName, *pubKey);
714
715 return keyName;
716}
717
718void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700719KeyChain::signPacketWrapper(Data& data, const Signature& signature,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700720 const Name& keyName, DigestAlgorithm digestAlgorithm)
721{
722 data.setSignature(signature);
723
724 EncodingBuffer encoder;
725 data.wireEncode(encoder, true);
726
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700727 Block sigValue = pureSign(encoder.buf(), encoder.size(), keyName, digestAlgorithm);
728
729 data.wireEncode(encoder, sigValue);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700730}
731
732void
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700733KeyChain::signPacketWrapper(Interest& interest, const Signature& signature,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700734 const Name& keyName, DigestAlgorithm digestAlgorithm)
735{
736 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
José Quevedo641de4c2016-01-29 00:11:24 +0000737 if (timestamp <= m_lastTimestamp) {
738 timestamp = m_lastTimestamp + time::milliseconds(1);
739 }
Yingdi Yu7036ce22014-06-19 18:53:37 -0700740
741 Name signedName = interest.getName();
742 signedName
743 .append(name::Component::fromNumber(timestamp.count())) // timestamp
744 .append(name::Component::fromNumber(random::generateWord64())) // nonce
745 .append(signature.getInfo()); // signatureInfo
746
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700747 Block sigValue = pureSign(signedName.wireEncode().value(),
748 signedName.wireEncode().value_size(),
749 keyName,
750 digestAlgorithm);
751
Yingdi Yu7036ce22014-06-19 18:53:37 -0700752 sigValue.encode();
753 signedName.append(sigValue); // signatureValue
754 interest.setName(signedName);
755}
756
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700757Block
758KeyChain::pureSign(const uint8_t* buf, size_t size,
759 const Name& keyName, DigestAlgorithm digestAlgorithm) const
760{
Yingdi Yufe4733a2015-10-22 14:24:12 -0700761 if (keyName == SigningInfo::getDigestSha256Identity())
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700762 return Block(tlv::SignatureValue, crypto::computeSha256Digest(buf, size));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700763
764 return m_tpm->signInTpm(buf, size, keyName, digestAlgorithm);
765}
766
Yingdi Yu7036ce22014-06-19 18:53:37 -0700767Signature
768KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName)
769{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700770 Signature sig;
José Quevedo641de4c2016-01-29 00:11:24 +0000771 sig.setValue(sign(buffer, bufferLength, signingByIdentity(identityName)));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700772 return sig;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700773}
774
775void
776KeyChain::signWithSha256(Data& data)
777{
José Quevedo641de4c2016-01-29 00:11:24 +0000778 return sign(data, signingWithSha256());
Yingdi Yu7036ce22014-06-19 18:53:37 -0700779}
780
781void
Yingdi Yu6ab67812014-11-27 15:00:34 -0800782KeyChain::signWithSha256(Interest& interest)
783{
784 DigestSha256 sig;
785
786 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
787 if (timestamp <= m_lastTimestamp)
788 timestamp = m_lastTimestamp + time::milliseconds(1);
789
790 Name signedName = interest.getName();
791 signedName
792 .append(name::Component::fromNumber(timestamp.count())) // timestamp
793 .append(name::Component::fromNumber(random::generateWord64())) // nonce
794 .append(sig.getInfo()); // signatureInfo
795
796 Block sigValue(tlv::SignatureValue,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700797 crypto::computeSha256Digest(signedName.wireEncode().value(),
798 signedName.wireEncode().value_size()));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800799
800 sigValue.encode();
801 signedName.append(sigValue); // signatureValue
802 interest.setName(signedName);
803}
804
805void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700806KeyChain::deleteCertificate(const Name& certificateName)
807{
Yingdi Yu7036ce22014-06-19 18:53:37 -0700808 m_pib->deleteCertificateInfo(certificateName);
809}
810
811void
812KeyChain::deleteKey(const Name& keyName)
813{
Yingdi Yu7036ce22014-06-19 18:53:37 -0700814 m_pib->deletePublicKeyInfo(keyName);
815 m_tpm->deleteKeyPairInTpm(keyName);
816}
817
818void
819KeyChain::deleteIdentity(const Name& identity)
820{
Yingdi Yu6147ef42014-12-08 17:48:32 -0800821 std::vector<Name> keyNames;
822 m_pib->getAllKeyNamesOfIdentity(identity, keyNames, true);
823 m_pib->getAllKeyNamesOfIdentity(identity, keyNames, false);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700824
825 m_pib->deleteIdentityInfo(identity);
826
Yingdi Yu6147ef42014-12-08 17:48:32 -0800827 for (const auto& keyName : keyNames)
828 m_tpm->deleteKeyPairInTpm(keyName);
Yingdi Yu7036ce22014-06-19 18:53:37 -0700829}
830
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700831tlv::SignatureTypeValue
832KeyChain::getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm)
833{
834 switch (keyType) {
Yingdi Yu99b2a002015-08-12 12:47:44 -0700835 case KeyType::RSA:
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700836 return tlv::SignatureSha256WithRsa;
Yingdi Yu99b2a002015-08-12 12:47:44 -0700837 case KeyType::EC:
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700838 return tlv::SignatureSha256WithEcdsa;
839 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700840 BOOST_THROW_EXCEPTION(Error("Unsupported key types"));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700841 }
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700842}
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700843
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800844} // namespace v1
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700845} // namespace security
846} // namespace ndn