v2/key-chain.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "key-chain.hpp"
23 
24 #include "../../util/config-file.hpp"
25 #include "../../util/logger.hpp"
26 
27 #include "../pib/pib-sqlite3.hpp"
28 #include "../pib/pib-memory.hpp"
29 
30 #ifdef NDN_CXX_HAVE_OSX_SECURITY
31 #include "../tpm/back-end-osx.hpp"
32 #endif // NDN_CXX_HAVE_OSX_SECURITY
33 
34 #include "../tpm/back-end-file.hpp"
35 #include "../tpm/back-end-mem.hpp"
36 
37 #include "../transform/bool-sink.hpp"
38 #include "../transform/buffer-source.hpp"
39 #include "../transform/private-key.hpp"
40 #include "../transform/verifier-filter.hpp"
41 #include "../../encoding/buffer-stream.hpp"
42 #include "../../util/crypto.hpp"
43 
44 #include <boost/lexical_cast.hpp>
45 
46 namespace ndn {
47 namespace security {
48 
49 // When static library is used, not everything is compiled into the resulting binary.
50 // Therefore, the following standard PIB and TPMs need to be registered here.
51 // http://stackoverflow.com/q/9459980/2150331
52 
54 // PIB //
56 namespace pib {
59 } // namespace pib
60 
62 // TPM //
64 namespace tpm {
65 #if defined(NDN_CXX_HAVE_OSX_SECURITY) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
67 #endif // defined(NDN_CXX_HAVE_OSX_SECURITY) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
68 
71 } // namespace tpm
72 
73 namespace v2 {
74 
76 
77 std::string KeyChain::s_defaultPibLocator;
78 std::string KeyChain::s_defaultTpmLocator;
79 
80 KeyChain::PibFactories&
81 KeyChain::getPibFactories()
82 {
83  static PibFactories pibFactories;
84  return pibFactories;
85 }
86 
87 KeyChain::TpmFactories&
88 KeyChain::getTpmFactories()
89 {
90  static TpmFactories tpmFactories;
91  return tpmFactories;
92 }
93 
94 const std::string&
95 KeyChain::getDefaultPibScheme()
96 {
98 }
99 
100 const std::string&
101 KeyChain::getDefaultTpmScheme()
102 {
103 #if defined(NDN_CXX_HAVE_OSX_SECURITY) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
104  return tpm::BackEndOsx::getScheme();;
105 #else
107 #endif // defined(NDN_CXX_HAVE_OSX_SECURITY) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
108 }
109 
110 const std::string&
111 KeyChain::getDefaultPibLocator()
112 {
113  if (!s_defaultPibLocator.empty())
114  return s_defaultPibLocator;
115 
116  if (getenv("NDN_CLIENT_PIB") != nullptr) {
117  s_defaultPibLocator = getenv("NDN_CLIENT_PIB");
118  }
119  else {
120  ConfigFile config;
121  s_defaultPibLocator = config.getParsedConfiguration().get<std::string>("pib", getDefaultPibScheme() + ":");
122  }
123 
124  return s_defaultPibLocator;
125 }
126 
127 const std::string&
128 KeyChain::getDefaultTpmLocator()
129 {
130  if (!s_defaultTpmLocator.empty())
131  return s_defaultTpmLocator;
132 
133  if (getenv("NDN_CLIENT_TPM") != nullptr) {
134  s_defaultTpmLocator = getenv("NDN_CLIENT_TPM");
135  }
136  else {
137  ConfigFile config;
138  s_defaultTpmLocator = config.getParsedConfiguration().get<std::string>("tpm", getDefaultTpmScheme() + ":");
139  }
140 
141  return s_defaultTpmLocator;
142 }
143 
144 
145 // Other defaults
146 
147 const SigningInfo&
149 {
150  static SigningInfo signingInfo;
151  return signingInfo;
152 }
153 
154 const KeyParams&
156 {
157  static EcKeyParams keyParams;
158  return keyParams;
159 }
160 
161 //
162 
164  : KeyChain(getDefaultPibLocator(), getDefaultTpmLocator(), true)
165 {
166 }
167 
168 KeyChain::KeyChain(const std::string& pibLocator, const std::string& tpmLocator, bool allowReset)
169 {
170  // PIB Locator
171  std::string pibScheme, pibLocation;
172  std::tie(pibScheme, pibLocation) = parseAndCheckPibLocator(pibLocator);
173  std::string canonicalPibLocator = pibScheme + ":" + pibLocation;
174 
175  // Create PIB
176  m_pib = createPib(canonicalPibLocator);
177  std::string oldTpmLocator;
178  try {
179  oldTpmLocator = m_pib->getTpmLocator();
180  }
181  catch (const Pib::Error&) {
182  // TPM locator is not set in PIB yet.
183  }
184 
185  // TPM Locator
186  std::string tpmScheme, tpmLocation;
187  std::tie(tpmScheme, tpmLocation) = parseAndCheckTpmLocator(tpmLocator);
188  std::string canonicalTpmLocator = tpmScheme + ":" + tpmLocation;
189 
190  if (canonicalPibLocator == getDefaultPibLocator()) {
191  // Default PIB must use default TPM
192  if (!oldTpmLocator.empty() && oldTpmLocator != getDefaultTpmLocator()) {
193  m_pib->reset();
194  canonicalTpmLocator = getDefaultTpmLocator();
195  }
196  }
197  else {
198  // non-default PIB check consistency
199  if (!oldTpmLocator.empty() && oldTpmLocator != canonicalTpmLocator) {
200  if (allowReset)
201  m_pib->reset();
202  else
203  BOOST_THROW_EXCEPTION(LocatorMismatchError("TPM locator supplied does not match TPM locator in PIB: " +
204  oldTpmLocator + " != " + canonicalTpmLocator));
205  }
206  }
207 
208  // note that key mismatch may still happen if the TPM locator is initially set to a
209  // wrong one or if the PIB was shared by more than one TPMs before. This is due to the
210  // old PIB does not have TPM info, new pib should not have this problem.
211  m_tpm = createTpm(canonicalTpmLocator);
212  m_pib->setTpmLocator(canonicalTpmLocator);
213 }
214 
215 KeyChain::~KeyChain() = default;
216 
217 // public: management
218 
219 Identity
220 KeyChain::createIdentity(const Name& identityName, const KeyParams& params)
221 {
222  Identity id = m_pib->addIdentity(identityName);
223 
224  Key key;
225  try {
226  key = id.getDefaultKey();
227  }
228  catch (const Pib::Error&) {
229  key = createKey(id, params);
230  }
231 
232  try {
233  key.getDefaultCertificate();
234  }
235  catch (const Pib::Error&) {
236  NDN_LOG_DEBUG("No default cert for " << key.getName() << ", requesting self-signing");
237  selfSign(key);
238  }
239 
240  return id;
241 }
242 
243 void
244 KeyChain::deleteIdentity(const Identity& identity)
245 {
246  BOOST_ASSERT(static_cast<bool>(identity));
247 
248  Name identityName = identity.getName();
249 
250  for (const auto& key : identity.getKeys()) {
251  m_tpm->deleteKey(key.getName());
252  }
253 
254  m_pib->removeIdentity(identityName);
255 }
256 
257 void
258 KeyChain::setDefaultIdentity(const Identity& identity)
259 {
260  BOOST_ASSERT(static_cast<bool>(identity));
261 
262  m_pib->setDefaultIdentity(identity.getName());
263 }
264 
265 Key
266 KeyChain::createKey(const Identity& identity, const KeyParams& params)
267 {
268  BOOST_ASSERT(static_cast<bool>(identity));
269 
270  // create key in TPM
271  Name keyName = m_tpm->createKey(identity.getName(), params);
272 
273  // set up key info in PIB
274  ConstBufferPtr pubKey = m_tpm->getPublicKey(keyName);
275  Key key = identity.addKey(pubKey->buf(), pubKey->size(), keyName);
276 
277  NDN_LOG_DEBUG("Requesting self-signing for newly created key " << key.getName());
278  selfSign(key);
279 
280  return key;
281 }
282 
283 void
284 KeyChain::deleteKey(const Identity& identity, const Key& key)
285 {
286  BOOST_ASSERT(static_cast<bool>(identity));
287  BOOST_ASSERT(static_cast<bool>(key));
288 
289  Name keyName = key.getName();
290  if (identity.getName() != key.getIdentity()) {
291  BOOST_THROW_EXCEPTION(std::invalid_argument("Identity `" + identity.getName().toUri() + "` "
292  "does match key `" + keyName.toUri() + "`"));
293  }
294 
295  identity.removeKey(keyName);
296  m_tpm->deleteKey(keyName);
297 }
298 
299 void
300 KeyChain::setDefaultKey(const Identity& identity, const Key& key)
301 {
302  BOOST_ASSERT(static_cast<bool>(identity));
303  BOOST_ASSERT(static_cast<bool>(key));
304 
305  if (identity.getName() != key.getIdentity())
306  BOOST_THROW_EXCEPTION(std::invalid_argument("Identity `" + identity.getName().toUri() + "` "
307  "does match key `" + key.getName().toUri() + "`"));
308 
309  identity.setDefaultKey(key.getName());
310 }
311 
312 void
313 KeyChain::addCertificate(const Key& key, const Certificate& certificate)
314 {
315  BOOST_ASSERT(static_cast<bool>(key));
316 
317  if (key.getName() != certificate.getKeyName() ||
318  !std::equal(certificate.getContent().value_begin(), certificate.getContent().value_end(),
319  key.getPublicKey().begin()))
320  BOOST_THROW_EXCEPTION(std::invalid_argument("Key `" + key.getName().toUri() + "` "
321  "does match certificate `" + certificate.getName().toUri() + "`"));
322 
323  key.addCertificate(certificate);
324 }
325 
326 void
327 KeyChain::deleteCertificate(const Key& key, const Name& certificateName)
328 {
329  BOOST_ASSERT(static_cast<bool>(key));
330 
331  if (!Certificate::isValidName(certificateName)) {
332  BOOST_THROW_EXCEPTION(std::invalid_argument("Wrong certificate name `" + certificateName.toUri() + "`"));
333  }
334 
335  key.removeCertificate(certificateName);
336 }
337 
338 void
339 KeyChain::setDefaultCertificate(const Key& key, const Certificate& cert)
340 {
341  BOOST_ASSERT(static_cast<bool>(key));
342 
343  try {
344  addCertificate(key, cert);
345  }
346  catch (const Pib::Error&) { // force to overwrite the existing certificates
347  key.removeCertificate(cert.getName());
348  addCertificate(key, cert);
349  }
350  key.setDefaultCertificate(cert.getName());
351 }
352 
353 shared_ptr<SafeBag>
354 KeyChain::exportSafeBag(const Certificate& certificate, const char* pw, size_t pwLen)
355 {
356  Name identity = certificate.getIdentity();
357  Name keyName = certificate.getKeyName();
358 
359  ConstBufferPtr encryptedKey;
360  try {
361  encryptedKey = m_tpm->exportPrivateKey(keyName, pw, pwLen);
362  }
363  catch (const tpm::BackEnd::Error&) {
364  BOOST_THROW_EXCEPTION(Error("Private `" + keyName.toUri() + "` key does not exist"));
365  }
366 
367  return make_shared<SafeBag>(certificate, *encryptedKey);
368 }
369 
370 void
371 KeyChain::importSafeBag(const SafeBag& safeBag, const char* pw, size_t pwLen)
372 {
373  Data certData = safeBag.getCertificate();
374  Certificate cert(std::move(certData));
375  Name identity = cert.getIdentity();
376  Name keyName = cert.getKeyName();
377  const Buffer publicKeyBits = cert.getPublicKey();
378 
379  if (m_tpm->hasKey(keyName)) {
380  BOOST_THROW_EXCEPTION(Error("Private key `" + keyName.toUri() + "` already exists"));
381  }
382 
383  try {
384  Identity existingId = m_pib->getIdentity(identity);
385  existingId.getKey(keyName);
386  BOOST_THROW_EXCEPTION(Error("Public key `" + keyName.toUri() + "` already exists"));
387  }
388  catch (const Pib::Error&) {
389  // Either identity or key doesn't exist. OK to import.
390  }
391 
392  try {
393  m_tpm->importPrivateKey(keyName,
394  safeBag.getEncryptedKeyBag().buf(), safeBag.getEncryptedKeyBag().size(),
395  pw, pwLen);
396  }
397  catch (const std::runtime_error&) {
398  BOOST_THROW_EXCEPTION(Error("Fail to import private key `" + keyName.toUri() + "`"));
399  }
400 
401  // check the consistency of private key and certificate
402  const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
403  ConstBufferPtr sigBits;
404  try {
405  sigBits = m_tpm->sign(content, 4, keyName, DigestAlgorithm::SHA256);
406  }
407  catch (const std::runtime_error&) {
408  m_tpm->deleteKey(keyName);
409  BOOST_THROW_EXCEPTION(Error("Invalid private key `" + keyName.toUri() + "`"));
410  }
411  bool isVerified = false;
412  {
413  using namespace transform;
414  PublicKey publicKey;
415  publicKey.loadPkcs8(publicKeyBits.buf(), publicKeyBits.size());
416  bufferSource(content, sizeof(content)) >> verifierFilter(DigestAlgorithm::SHA256, publicKey,
417  sigBits->buf(), sigBits->size())
418  >> boolSink(isVerified);
419  }
420  if (!isVerified) {
421  m_tpm->deleteKey(keyName);
422  BOOST_THROW_EXCEPTION(Error("Certificate `" + cert.getName().toUri() + "` "
423  "and private key `" + keyName.toUri() + "` do not match"));
424  }
425 
426  Identity id = m_pib->addIdentity(identity);
427  Key key = id.addKey(cert.getPublicKey().buf(), cert.getPublicKey().size(), keyName);
428  key.addCertificate(cert);
429 }
430 
431 
432 // public: signing
433 
434 void
435 KeyChain::sign(Data& data, const SigningInfo& params)
436 {
437  Name keyName;
438  SignatureInfo sigInfo;
439  std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
440 
441  data.setSignature(Signature(sigInfo));
442 
443  EncodingBuffer encoder;
444  data.wireEncode(encoder, true);
445 
446  Block sigValue = sign(encoder.buf(), encoder.size(), keyName, params.getDigestAlgorithm());
447 
448  data.wireEncode(encoder, sigValue);
449 }
450 
451 void
452 KeyChain::sign(Interest& interest, const SigningInfo& params)
453 {
454  Name keyName;
455  SignatureInfo sigInfo;
456  std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
457 
458  Name signedName = interest.getName();
459  signedName.append(sigInfo.wireEncode()); // signatureInfo
460 
461  Block sigValue = sign(signedName.wireEncode().value(), signedName.wireEncode().value_size(),
462  keyName, params.getDigestAlgorithm());
463 
464  sigValue.encode();
465  signedName.append(sigValue); // signatureValue
466  interest.setName(signedName);
467 }
468 
469 Block
470 KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const SigningInfo& params)
471 {
472  Name keyName;
473  SignatureInfo sigInfo;
474  std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
475 
476  return sign(buffer, bufferLength, keyName, params.getDigestAlgorithm());
477 }
478 
479 // public: PIB/TPM creation helpers
480 
481 static inline std::tuple<std::string/*type*/, std::string/*location*/>
482 parseLocatorUri(const std::string& uri)
483 {
484  size_t pos = uri.find(':');
485  if (pos != std::string::npos) {
486  return std::make_tuple(uri.substr(0, pos), uri.substr(pos + 1));
487  }
488  else {
489  return std::make_tuple(uri, "");
490  }
491 }
492 
493 std::tuple<std::string/*type*/, std::string/*location*/>
494 KeyChain::parseAndCheckPibLocator(const std::string& pibLocator)
495 {
496  std::string pibScheme, pibLocation;
497  std::tie(pibScheme, pibLocation) = parseLocatorUri(pibLocator);
498 
499  if (pibScheme.empty()) {
500  pibScheme = getDefaultPibScheme();
501  }
502 
503  auto pibFactory = getPibFactories().find(pibScheme);
504  if (pibFactory == getPibFactories().end()) {
505  BOOST_THROW_EXCEPTION(KeyChain::Error("PIB scheme `" + pibScheme + "` is not supported"));
506  }
507 
508  return std::make_tuple(pibScheme, pibLocation);
509 }
510 
511 unique_ptr<Pib>
512 KeyChain::createPib(const std::string& pibLocator)
513 {
514  std::string pibScheme, pibLocation;
515  std::tie(pibScheme, pibLocation) = parseAndCheckPibLocator(pibLocator);
516  auto pibFactory = getPibFactories().find(pibScheme);
517  BOOST_ASSERT(pibFactory != getPibFactories().end());
518  return unique_ptr<Pib>(new Pib(pibScheme, pibLocation, pibFactory->second(pibLocation)));
519 }
520 
521 std::tuple<std::string/*type*/, std::string/*location*/>
522 KeyChain::parseAndCheckTpmLocator(const std::string& tpmLocator)
523 {
524  std::string tpmScheme, tpmLocation;
525  std::tie(tpmScheme, tpmLocation) = parseLocatorUri(tpmLocator);
526 
527  if (tpmScheme.empty()) {
528  tpmScheme = getDefaultTpmScheme();
529  }
530  auto tpmFactory = getTpmFactories().find(tpmScheme);
531  if (tpmFactory == getTpmFactories().end()) {
532  BOOST_THROW_EXCEPTION(KeyChain::Error("TPM scheme `" + tpmScheme + "` is not supported"));
533  }
534 
535  return std::make_tuple(tpmScheme, tpmLocation);
536 }
537 
538 unique_ptr<Tpm>
539 KeyChain::createTpm(const std::string& tpmLocator)
540 {
541  std::string tpmScheme, tpmLocation;
542  std::tie(tpmScheme, tpmLocation) = parseAndCheckTpmLocator(tpmLocator);
543  auto tpmFactory = getTpmFactories().find(tpmScheme);
544  BOOST_ASSERT(tpmFactory != getTpmFactories().end());
545  return unique_ptr<Tpm>(new Tpm(tpmScheme, tpmLocation, tpmFactory->second(tpmLocation)));
546 }
547 
548 // private: signing
549 
550 Certificate
551 KeyChain::selfSign(Key& key)
552 {
553  Certificate certificate;
554 
555  // set name
556  Name certificateName = key.getName();
557  certificateName
558  .append("self")
559  .appendVersion();
560  certificate.setName(certificateName);
561 
562  // set metainfo
563  certificate.setContentType(tlv::ContentType_Key);
564  certificate.setFreshnessPeriod(time::hours(1));
565 
566  // set content
567  certificate.setContent(key.getPublicKey().buf(), key.getPublicKey().size());
568 
569  // set signature-info
570  SignatureInfo signatureInfo;
571  // Note time::system_clock::max() or other NotAfter date results in incorrect encoded value
572  // because of overflow during conversion to boost::posix_time::ptime (bug #3915).
573  signatureInfo.setValidityPeriod(ValidityPeriod(time::system_clock::TimePoint(),
574  time::system_clock::now() + time::days(20 * 365)));
575 
576  sign(certificate, SigningInfo(key).setSignatureInfo(signatureInfo));
577 
578  key.addCertificate(certificate);
579  return certificate;
580 }
581 
582 std::tuple<Name, SignatureInfo>
583 KeyChain::prepareSignatureInfo(const SigningInfo& params)
584 {
585  SignatureInfo sigInfo = params.getSignatureInfo();
586 
587  Name identityName;
588  name::Component keyId;
589  Name certificateName;
590 
591  pib::Identity identity;
592  pib::Key key;
593 
594  switch (params.getSignerType()) {
596  try {
597  identity = m_pib->getDefaultIdentity();
598  }
599  catch (const Pib::Error&) { // no default identity, use sha256 for signing.
600  sigInfo.setSignatureType(tlv::DigestSha256);
601  return std::make_tuple(SigningInfo::getDigestSha256Identity(), sigInfo);
602  }
603  break;
604  }
606  try {
607  identity = m_pib->getIdentity(params.getSignerName());
608  }
609  catch (const Pib::Error&) {
610  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Signing identity `" +
611  params.getSignerName().toUri() + "` does not exist"));
612  }
613  break;
614  }
616  Name identityName = extractIdentityFromKeyName(params.getSignerName());
617 
618  try {
619  identity = m_pib->getIdentity(identityName);
620  key = identity.getKey(params.getSignerName());
621  identity = Identity(); // we will use the PIB key instance, so reset identity;
622  }
623  catch (const Pib::Error&) {
624  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Signing key `" +
625  params.getSignerName().toUri() + "` does not exist"));
626  }
627  break;
628  }
630  Name identityName = extractIdentityFromCertName(params.getSignerName());
631  Name keyName = extractKeyNameFromCertName(params.getSignerName());
632 
633  try {
634  identity = m_pib->getIdentity(identityName);
635  key = identity.getKey(keyName);
636  }
637  catch (const Pib::Error&) {
638  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Signing certificate `" +
639  params.getSignerName().toUri() + "` does not exist"));
640  }
641 
642  break;
643  }
645  sigInfo.setSignatureType(tlv::DigestSha256);
646  return std::make_tuple(SigningInfo::getDigestSha256Identity(), sigInfo);
647  }
649  identity = params.getPibIdentity();
650  if (!identity)
651  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("PIB identity is invalid"));
652  break;
653  }
655  key = params.getPibKey();
656  if (!key)
657  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("PIB key is invalid"));
658  break;
659  }
660  default: {
661  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Unrecognized signer type " +
662  boost::lexical_cast<std::string>(params.getSignerType())));
663  }
664  }
665 
666  if (!identity && !key) {
667  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Cannot determine signing parameters"));
668  }
669 
670  if (identity && !key) {
671  try {
672  key = identity.getDefaultKey();
673  }
674  catch (const Pib::Error&) {
675  BOOST_THROW_EXCEPTION(InvalidSigningInfoError("Signing identity `" + identity.getName().toUri() +
676  "` does not have default certificate"));
677  }
678  }
679 
680  BOOST_ASSERT(key);
681 
682  sigInfo.setSignatureType(getSignatureType(key.getKeyType(), params.getDigestAlgorithm()));
683  sigInfo.setKeyLocator(KeyLocator(key.getName()));
684 
685  NDN_LOG_TRACE("Prepared signature info: " << sigInfo);
686  return std::make_tuple(key.getName(), sigInfo);
687 }
688 
689 Block
690 KeyChain::sign(const uint8_t* buf, size_t size,
691  const Name& keyName, DigestAlgorithm digestAlgorithm) const
692 {
693  if (keyName == SigningInfo::getDigestSha256Identity())
694  return Block(tlv::SignatureValue, crypto::sha256(buf, size));
695 
696  return Block(tlv::SignatureValue, m_tpm->sign(buf, size, keyName, digestAlgorithm));
697 }
698 
700 KeyChain::getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm)
701 {
702  switch (keyType) {
703  case KeyType::RSA:
705  case KeyType::EC:
707  default:
708  BOOST_THROW_EXCEPTION(Error("Unsupported key types"));
709  }
710 }
711 
712 } // namespace v2
713 } // namespace security
714 } // namespace ndn
void deleteKey(const Identity &identity, const Key &key)
Delete a key key of identity.
const Name & getName() const
Definition: interest.hpp:226
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
The certificate following the certificate format naming convention.
Name getKeyName() const
Get key name.
The interface of signing key management.
void addCertificate(const Key &key, const Certificate &certificate)
Add a certificate certificate for key.
#define NDN_CXX_V2_KEYCHAIN_REGISTER_PIB_BACKEND(PibType)
Register Pib backend class in KeyChain.
Key createKey(const Identity &identity, const KeyParams &params=getDefaultKeyParams())
Create a key for identity according to params.
#define NDN_CXX_V2_KEYCHAIN_REGISTER_TPM_BACKEND(TpmType)
Register Tpm backend class in KeyChain.
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.cpp:275
The back-end implementation of file-based TPM.
given PIB identity handle, use its default key and default certificate
Name extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.
static std::tuple< std::string, std::string > parseLocatorUri(const std::string &uri)
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:122
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
Error indicating that the supplied TPM locator does not match the locator stored in PIB...
represents an Interest packet
Definition: interest.hpp:42
#define NDN_LOG_DEBUG(expression)
log at DEBUG level
Definition: logger.hpp:146
use sha256 digest, no signer needs to be specified
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:97
static time_point now() noexcept
Definition: time.cpp:45
Buffer::const_iterator value_begin() const
Definition: block.hpp:330
Buffer::const_iterator value_end() const
Definition: block.hpp:336
Signing parameters passed to KeyChain.
void deleteCertificate(const Key &key, const Name &certificateName)
delete a certificate with name certificateName of key.
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
Identity createIdentity(const Name &identityName, const KeyParams &params=getDefaultKeyParams())
Create an identity identityName.
void importSafeBag(const SafeBag &safeBag, const char *pw, size_t pwLen)
Import a pair of certificate and its corresponding private key encapsulated in a SafeBag.
static const std::string & getScheme()
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:171
Pib backend implementation based on SQLite3 database.
Definition: pib-sqlite3.hpp:39
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
Definition: data.cpp:52
shared_ptr< SafeBag > exportSafeBag(const Certificate &certificate, const char *pw, size_t pwLen)
export a certificate of name certificateName and its corresponding private key.
EncodingImpl< EncoderTag > EncodingBuffer
uint8_t * buf()
Definition: buffer.hpp:87
static const std::string & getScheme()
Interest & setName(const Name &name)
Definition: interest.hpp:232
no signer is specified, use default setting or follow the trust schema
ndn security pib Pib
Definition: pib.cpp:30
static const std::string & getScheme()
void setDefaultIdentity(const Identity &identity)
Set identity as the default identity.
static const SigningInfo & getDefaultSigningInfo()
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
Use the SHA256 hash of the public key as the key id.
Name getIdentity() const
Get identity name.
The back-end implementation of in-memory TPM.
Name abstraction to represent an absolute name.
Definition: name.hpp:46
signer is a certificate, use it directly
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
Buffer getPublicKey() const
Get public key bits (in PKCS#8 format)
signer is a key, use its default certificate
An in-memory implementation of Pib.
Definition: pib-memory.hpp:37
const Buffer & getEncryptedKeyBag() const
Get the private key in PKCS#8 from safe bag.
Definition: safe-bag.hpp:106
time_point TimePoint
Definition: time.hpp:90
void sign(Data &data, const SigningInfo &params=getDefaultSigningInfo())
Sign data according to the supplied signing information.
given PIB key handle, use its default certificate
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Encode to a wire format or estimate wire format.
void deleteIdentity(const Identity &identity)
delete identity.
static bool isValidName(const Name &certName)
Check if the specified name follows the naming convention for the certificate.
static const KeyParams & getDefaultKeyParams()
a secured container for sensitive information(certificate, private key)
Definition: safe-bag.hpp:38
void setDefaultKey(const Identity &identity, const Key &key)
Set key as the default key of identity.
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
void setDefaultCertificate(const Key &key, const Certificate &cert)
Set cert as the default certificate of key.
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:140
const Data & getCertificate() const
Get the certificate data packet from safe bag.
Definition: safe-bag.hpp:97
indicates content is a public key
Base class of key parameters.
Definition: key-params.hpp:36
signer is an identity, use its default key and default certificate
#define NDN_LOG_TRACE(expression)
log at TRACE level
Definition: logger.hpp:141
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
ConstBufferPtr sha256(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
Definition: crypto.hpp:50
represents a Data packet
Definition: data.hpp:37
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:150
DigestAlgorithm getDigestAlgorithm() const
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:148
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:44
unique_ptr< Sink > boolSink(bool &value)
Definition: bool-sink.cpp:51
duration< boost::int_least32_t, boost::ratio< 86400 > > days
Definition: time.hpp:35
A Signature is storage for the signature-related information (info and value) in a Data packet...
Definition: signature.hpp:33
Name extractIdentityFromCertName(const Name &certName)
Extract identity namespace from the certificate name certName.