34 #ifdef NDN_CXX_HAVE_OSX_FRAMEWORKS 36 #endif // NDN_CXX_HAVE_OSX_FRAMEWORKS 44 #include <boost/lexical_cast.hpp> 59 #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) 61 #endif // defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) 71 std::string KeyChain::s_defaultPibLocator;
72 std::string KeyChain::s_defaultTpmLocator;
74 KeyChain::PibFactories&
75 KeyChain::getPibFactories()
77 static PibFactories pibFactories;
81 KeyChain::TpmFactories&
82 KeyChain::getTpmFactories()
84 static TpmFactories tpmFactories;
89 KeyChain::getDefaultPibScheme()
95 KeyChain::getDefaultTpmScheme()
97 #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) 101 #endif // defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) 105 KeyChain::getDefaultPibLocator()
107 if (!s_defaultPibLocator.empty())
108 return s_defaultPibLocator;
110 if (getenv(
"NDN_CLIENT_PIB") !=
nullptr) {
111 s_defaultPibLocator = getenv(
"NDN_CLIENT_PIB");
115 s_defaultPibLocator = config.getParsedConfiguration().get<std::string>(
"pib", getDefaultPibScheme() +
":");
118 std::string pibScheme, pibLocation;
119 std::tie(pibScheme, pibLocation) = parseAndCheckPibLocator(s_defaultPibLocator);
120 s_defaultPibLocator = pibScheme +
":" + pibLocation;
122 return s_defaultPibLocator;
126 KeyChain::getDefaultTpmLocator()
128 if (!s_defaultTpmLocator.empty())
129 return s_defaultTpmLocator;
131 if (getenv(
"NDN_CLIENT_TPM") !=
nullptr) {
132 s_defaultTpmLocator = getenv(
"NDN_CLIENT_TPM");
136 s_defaultTpmLocator = config.getParsedConfiguration().get<std::string>(
"tpm", getDefaultTpmScheme() +
":");
139 std::string tpmScheme, tpmLocation;
140 std::tie(tpmScheme, tpmLocation) = parseAndCheckTpmLocator(s_defaultTpmLocator);
141 s_defaultTpmLocator = tpmScheme +
":" + tpmLocation;
143 return s_defaultTpmLocator;
150 KeyChain::getDefaultSigningInfo()
157 KeyChain::getDefaultKeyParams()
166 :
KeyChain(getDefaultPibLocator(), getDefaultTpmLocator(), true)
173 std::string pibScheme, pibLocation;
174 std::tie(pibScheme, pibLocation) = parseAndCheckPibLocator(pibLocator);
175 std::string canonicalPibLocator = pibScheme +
":" + pibLocation;
178 m_pib = createPib(canonicalPibLocator);
179 std::string oldTpmLocator;
181 oldTpmLocator = m_pib->getTpmLocator();
183 catch (
const Pib::Error&) {
188 std::string tpmScheme, tpmLocation;
189 std::tie(tpmScheme, tpmLocation) = parseAndCheckTpmLocator(tpmLocator);
190 std::string canonicalTpmLocator = tpmScheme +
":" + tpmLocation;
192 if (canonicalPibLocator == getDefaultPibLocator()) {
194 if (!oldTpmLocator.empty() && oldTpmLocator != getDefaultTpmLocator()) {
196 canonicalTpmLocator = getDefaultTpmLocator();
201 if (!oldTpmLocator.empty() && oldTpmLocator != canonicalTpmLocator) {
206 oldTpmLocator +
" != " + canonicalTpmLocator));
213 m_tpm = createTpm(canonicalTpmLocator);
214 m_pib->setTpmLocator(canonicalTpmLocator);
224 Identity
id = m_pib->addIdentity(identityName);
228 key =
id.getDefaultKey();
230 catch (
const Pib::Error&) {
235 key.getDefaultCertificate();
237 catch (
const Pib::Error&) {
238 NDN_LOG_DEBUG(
"No default cert for " << key.getName() <<
", requesting self-signing");
248 BOOST_ASSERT(static_cast<bool>(identity));
250 Name identityName = identity.getName();
252 for (
const auto& key : identity.getKeys()) {
253 m_tpm->deleteKey(key.getName());
256 m_pib->removeIdentity(identityName);
262 BOOST_ASSERT(static_cast<bool>(identity));
264 m_pib->setDefaultIdentity(identity.getName());
270 BOOST_ASSERT(static_cast<bool>(identity));
273 Name keyName = m_tpm->createKey(identity.getName(), params);
277 Key key = identity.addKey(pubKey->data(), pubKey->size(), keyName);
279 NDN_LOG_DEBUG(
"Requesting self-signing for newly created key " << key.getName());
288 BOOST_ASSERT(static_cast<bool>(identity));
289 BOOST_ASSERT(static_cast<bool>(key));
291 Name keyName = key.getName();
292 if (identity.getName() != key.getIdentity()) {
293 NDN_THROW(std::invalid_argument(
"Identity `" + identity.getName().toUri() +
"` " 294 "does not match key `" + keyName.
toUri() +
"`"));
297 identity.removeKey(keyName);
298 m_tpm->deleteKey(keyName);
304 BOOST_ASSERT(static_cast<bool>(identity));
305 BOOST_ASSERT(static_cast<bool>(key));
307 if (identity.getName() != key.getIdentity())
308 NDN_THROW(std::invalid_argument(
"Identity `" + identity.getName().toUri() +
"` " 309 "does not match key `" + key.getName().toUri() +
"`"));
311 identity.setDefaultKey(key.getName());
317 BOOST_ASSERT(static_cast<bool>(key));
319 if (key.getName() != certificate.
getKeyName() ||
321 key.getPublicKey().begin()))
322 NDN_THROW(std::invalid_argument(
"Key `" + key.getName().toUri() +
"` " 323 "does not match certificate `" + certificate.
getName().
toUri() +
"`"));
325 key.addCertificate(certificate);
331 BOOST_ASSERT(static_cast<bool>(key));
334 NDN_THROW(std::invalid_argument(
"Wrong certificate name `" + certificateName.
toUri() +
"`"));
337 key.removeCertificate(certificateName);
343 BOOST_ASSERT(static_cast<bool>(key));
346 key.setDefaultCertificate(cert.
getName());
357 encryptedKey = m_tpm->exportPrivateKey(keyName, pw, pwLen);
363 return make_shared<SafeBag>(certificate, *encryptedKey);
375 if (m_tpm->hasKey(keyName)) {
380 Identity existingId = m_pib->getIdentity(identity);
381 existingId.getKey(keyName);
384 catch (
const Pib::Error&) {
389 m_tpm->importPrivateKey(keyName,
398 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
403 catch (
const std::runtime_error&) {
404 m_tpm->deleteKey(keyName);
407 bool isVerified =
false;
411 publicKey.loadPkcs8(publicKeyBits.data(), publicKeyBits.size());
413 sigBits->data(), sigBits->size())
417 m_tpm->deleteKey(keyName);
419 "and private key `" + keyName.
toUri() +
"` do not match"));
422 Identity
id = m_pib->addIdentity(identity);
424 key.addCertificate(cert);
434 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
451 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
460 signedName.
append(sigValue);
469 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
476 static inline std::tuple<std::string, std::string>
479 size_t pos = uri.find(
':');
480 if (pos != std::string::npos) {
481 return std::make_tuple(uri.substr(0, pos), uri.substr(pos + 1));
484 return std::make_tuple(uri,
"");
488 std::tuple<std::string, std::string>
489 KeyChain::parseAndCheckPibLocator(
const std::string& pibLocator)
491 std::string pibScheme, pibLocation;
494 if (pibScheme.empty()) {
495 pibScheme = getDefaultPibScheme();
498 auto pibFactory = getPibFactories().find(pibScheme);
499 if (pibFactory == getPibFactories().end()) {
503 return std::make_tuple(pibScheme, pibLocation);
507 KeyChain::createPib(
const std::string& pibLocator)
509 std::string pibScheme, pibLocation;
510 std::tie(pibScheme, pibLocation) = parseAndCheckPibLocator(pibLocator);
511 auto pibFactory = getPibFactories().find(pibScheme);
512 BOOST_ASSERT(pibFactory != getPibFactories().end());
513 return unique_ptr<Pib>(
new Pib(pibScheme, pibLocation, pibFactory->second(pibLocation)));
516 std::tuple<std::string, std::string>
517 KeyChain::parseAndCheckTpmLocator(
const std::string& tpmLocator)
519 std::string tpmScheme, tpmLocation;
522 if (tpmScheme.empty()) {
523 tpmScheme = getDefaultTpmScheme();
525 auto tpmFactory = getTpmFactories().find(tpmScheme);
526 if (tpmFactory == getTpmFactories().end()) {
530 return std::make_tuple(tpmScheme, tpmLocation);
534 KeyChain::createTpm(
const std::string& tpmLocator)
536 std::string tpmScheme, tpmLocation;
537 std::tie(tpmScheme, tpmLocation) = parseAndCheckTpmLocator(tpmLocator);
538 auto tpmFactory = getTpmFactories().find(tpmScheme);
539 BOOST_ASSERT(tpmFactory != getTpmFactories().end());
540 return unique_ptr<Tpm>(
new Tpm(tpmScheme, tpmLocation, tpmFactory->second(tpmLocation)));
546 KeyChain::selfSign(Key& key)
551 Name certificateName = key.getName();
555 certificate.
setName(certificateName);
562 certificate.
setContent(key.getPublicKey().data(), key.getPublicKey().size());
573 key.addCertificate(certificate);
577 std::tuple<Name, SignatureInfo>
578 KeyChain::prepareSignatureInfo(
const SigningInfo& params)
582 Name certificateName;
589 identity = m_pib->getDefaultIdentity();
591 catch (
const Pib::Error&) {
603 catch (
const Pib::Error&) {
615 identity = m_pib->getIdentity(identityName);
617 identity = Identity();
619 catch (
const Pib::Error&) {
630 identity = m_pib->getIdentity(identityName);
631 key = identity.
getKey(keyName);
633 catch (
const Pib::Error&) {
644 NDN_THROW(InvalidSigningInfoError(
"Unrecognized signer type " +
649 if (!identity && !key) {
650 NDN_THROW(InvalidSigningInfoError(
"Cannot determine signing parameters"));
653 if (identity && !key) {
657 catch (
const Pib::Error&) {
659 "` does not have a default certificate"));
669 return std::make_tuple(key.
getName(), sigInfo);
691 NDN_THROW(Error(
"Unsupported key type " + boost::lexical_cast<std::string>(keyType)));
#define NDN_THROW_NESTED(e)
void deleteKey(const Identity &identity, const Key &key)
Delete a key key of identity.
Data & setContentType(uint32_t type)
const Name & getName() const
const Name & getName() const
Get key name.
void setSignatureType(tlv::SignatureTypeValue type)
Set SignatureType.
const Key & getDefaultKey() const
Get the default key for this Identity.
The certificate following the certificate format naming convention.
Name getKeyName() const
Get key name.
Represents a SignatureInfo TLV element.
The interface of signing key management.
void addCertificate(const Key &key, const Certificate &certificate)
Add a certificate certificate for key.
Key createKey(const Identity &identity, const KeyParams ¶ms=getDefaultKeyParams())
Create a key for identity according to params.
Data & setSignature(const Signature &signature)
Set Signature.
Data & setName(const Name &name)
Set name.
const Name & getSignerName() const
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)
RSA key, supports sign/verify and encrypt/decrypt operations.
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
Data & setContent(const Block &block)
Set Content from a block.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Represents a TLV element of NDN packet format.
Error indicating that the supplied TPM locator does not match the locator stored in PIB...
Represents an Interest packet.
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
use sha256 digest, no signer needs to be specified
#define NDN_LOG_INIT(name)
Define a non-member log module.
static time_point now() noexcept
Name & append(const Component &component)
Append a component.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Signing parameters passed to KeyChain.
void deleteCertificate(const Key &key, const Name &certificateName)
delete a certificate with name certificateName of key.
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
#define NDN_CXX_V2_KEYCHAIN_REGISTER_PIB_BACKEND(PibType)
Register Pib backend class in KeyChain.
Identity createIdentity(const Name &identityName, const KeyParams ¶ms=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.
KeyType
The type of a cryptographic key.
static const std::string & getScheme()
void setKeyLocator(const KeyLocator &keyLocator)
Set KeyLocator.
std::string toUri() const
Get URI representation of the name.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
shared_ptr< SafeBag > exportSafeBag(const Certificate &certificate, const char *pw, size_t pwLen)
Export a certificate and its corresponding private key.
const Identity & getPibIdentity() const
A frontend handle of a key instance.
static const std::string & getScheme()
Interest & setName(const Name &name)
no signer is specified, use default setting or follow the trust schema
static const std::string & getScheme()
void setDefaultCertificate(const Key &key, const Certificate &certificate)
Set cert as the default certificate of key.
KeyType getKeyType() const
Get key type.
const SignatureInfo & getSignatureInfo() const
void setDefaultIdentity(const Identity &identity)
Set identity as the default identity.
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
void setValidityPeriod(const security::ValidityPeriod &validityPeriod)
Set ValidityPeriod.
Use the SHA256 hash of the public key as the key id.
Name getIdentity() const
Get identity name.
Represents an absolute name.
signer is a certificate, use it directly
Key getKey(const Name &keyName) const
Get a key with id keyName.
Buffer getPublicKey() const
Get public key bits (in PKCS#8 format)
SignatureTypeValue
SignatureType values.
signer is a key, use its default certificate
const Buffer & getEncryptedKeyBag() const
Get the private key in PKCS#8 from safe bag.
void sign(Data &data, const SigningInfo ¶ms=getDefaultSigningInfo())
Sign data according to the supplied signing information.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
const Name & getName() const
Get name.
Represents a name component.
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.
a secured container for sensitive information(certificate, private key)
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
void setDefaultKey(const Identity &identity, const Key &key)
Set key as the default key of identity.
void encode()
Encode sub-elements into TLV-VALUE.
const Block & getContent() const
Get Content.
const Name & getName() const
Get the name of the identity.
const Data & getCertificate() const
Get the certificate data packet from safe bag.
Base class of key parameters.
signer is an identity, use its default key and default certificate
A frontend handle of an Identity.
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
const Key & getPibKey() const
#define NDN_LOG_TRACE(expression)
Log at TRACE level.
Represents a Data packet.
SimplePublicKeyParams is a template for public keys with only one parameter: size.
DigestAlgorithm getDigestAlgorithm() const
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
General-purpose automatically managed/resized buffer.
EncodingImpl< EncoderTag > EncodingBuffer
#define NDN_CXX_V2_KEYCHAIN_REGISTER_TPM_BACKEND(TpmType)
Register Tpm backend class in KeyChain.
SignerType getSignerType() const
Holds SignatureInfo and SignatureValue in a Data packet.
shared_ptr< const Buffer > ConstBufferPtr
Name extractIdentityFromCertName(const Name &certName)
Extract identity namespace from the certificate name certName.