24 #ifndef NDN_SECURITY_KEY_CHAIN_HPP 25 #define NDN_SECURITY_KEY_CHAIN_HPP 36 #include "../interest.hpp" 37 #include "../util/crypto.hpp" 38 #include "../util/random.hpp" 39 #include <initializer_list> 50 class Error :
public std::runtime_error
55 :
std::runtime_error(what)
74 typedef function<unique_ptr<SecPublicInfo> (
const std::string&)>
PibCreateFunc;
82 template<
class PibType>
84 registerPib(std::initializer_list<std::string> aliases);
91 template<
class TpmType>
93 registerTpm(std::initializer_list<std::string> aliases);
104 static unique_ptr<SecPublicInfo>
105 createPib(
const std::string& pibLocator);
116 static unique_ptr<SecTpm>
117 createTpm(
const std::string& tpmLocator);
139 KeyChain(
const std::string& pibLocator,
140 const std::string& tpmLocator,
141 bool allowReset =
false);
222 shared_ptr<v1::IdentityCertificate>
224 const Name& signingIdentity,
227 const std::vector<security::v1::CertificateSubjectDescription>& subjectDescription,
246 shared_ptr<v1::IdentityCertificate>
249 const Name& signingIdentity,
252 const std::vector<security::v1::CertificateSubjectDescription>& subjectDescription,
304 sign(
const uint8_t* buffer,
size_t bufferLength,
const SigningInfo& params);
316 sign(T& packet,
const Name& certificateName);
329 sign(
const uint8_t* buffer,
size_t bufferLength,
const Name& certificateName);
377 shared_ptr<v1::IdentityCertificate>
424 shared_ptr<SecuredBag>
466 return m_pib->doesIdentityExist(identityName);
472 return m_pib->addIdentity(identityName);
478 return m_pib->doesPublicKeyExist(keyName);
484 return m_pib->addKey(keyName, publicKeyDer);
490 return m_pib->addKey(keyName, publicKeyDer);
493 shared_ptr<v1::PublicKey>
496 return m_pib->getPublicKey(keyName);
502 return m_pib->doesCertificateExist(certificateName);
508 return m_pib->addCertificate(certificate);
511 shared_ptr<v1::IdentityCertificate>
514 return m_pib->getCertificate(certificateName);
520 return m_pib->getDefaultIdentity();
526 return m_pib->getDefaultKeyNameForIdentity(identityName);
542 return m_pib->getDefaultCertificateNameForKey(keyName);
548 return m_pib->getAllIdentities(nameList, isDefault);
554 return m_pib->getAllKeyNames(nameList, isDefault);
560 return m_pib->getAllKeyNamesOfIdentity(identity, nameList, isDefault);
566 return m_pib->getAllCertificateNames(nameList, isDefault);
571 std::vector<Name>& nameList,
572 bool isDefault)
const 574 return m_pib->getAllCertificateNamesOfKey(keyName, nameList, isDefault);
580 return m_pib->deleteCertificateInfo(certificateName);
586 return m_pib->deletePublicKeyInfo(keyName);
592 return m_pib->deleteIdentityInfo(identity);
598 return m_pib->setDefaultIdentity(identityName);
604 return m_pib->setDefaultKeyNameForIdentity(keyName);
610 return m_pib->setDefaultCertificateNameForKey(certificateName);
616 return m_pib->getNewKeyName(identityName, useKsk);
622 return m_pib->getDefaultCertificateNameForIdentity(identityName);
628 return m_pib->getDefaultCertificateName();
634 return m_pib->addCertificateAsKeyDefault(certificate);
640 return m_pib->addCertificateAsIdentityDefault(certificate);
646 return m_pib->addCertificateAsSystemDefault(certificate);
649 shared_ptr<v1::IdentityCertificate>
652 if (!static_cast<bool>(m_pib->getDefaultCertificate()))
653 const_cast<KeyChain*
>(
this)->setDefaultCertificateInternal();
661 return m_pib->refreshDefaultCertificate();
671 return m_tpm->setTpmPassword(password, passwordLength);
677 return m_tpm->resetTpmPassword();
683 return m_tpm->setInTerminal(inTerminal);
689 return m_tpm->getInTerminal();
695 return m_tpm->isLocked();
699 unlockTpm(
const char* password,
size_t passwordLength,
bool usePassword)
701 return m_tpm->unlockTpm(password, passwordLength, usePassword);
707 return m_tpm->generateKeyPairInTpm(keyName, params);
713 return m_tpm->deleteKeyPairInTpm(keyName);
716 shared_ptr<v1::PublicKey>
719 return m_tpm->getPublicKeyFromTpm(keyName);
727 return m_tpm->signInTpm(data, dataLength, keyName, digestAlgorithm);
731 decryptInTpm(
const uint8_t* data,
size_t dataLength,
const Name& keyName,
bool isSymmetric)
733 return m_tpm->decryptInTpm(data, dataLength, keyName, isSymmetric);
737 encryptInTpm(
const uint8_t* data,
size_t dataLength,
const Name& keyName,
bool isSymmetric)
739 return m_tpm->encryptInTpm(data, dataLength, keyName, isSymmetric);
745 return m_tpm->generateSymmetricKeyInTpm(keyName, params);
751 return m_tpm->doesKeyExistInTpm(keyName, keyClass);
757 return m_tpm->generateRandomBlock(res, size);
763 return m_tpm->addAppToAcl(keyName, keyClass, appPath, acl);
769 return m_tpm->exportPrivateKeyPkcs5FromTpm(keyName, password);
774 const uint8_t* buf,
size_t size,
775 const std::string& password)
777 return m_tpm->importPrivateKeyPkcs5IntoTpm(keyName, buf, size, password);
782 initialize(
const std::string& pibLocatorUri,
783 const std::string& tpmLocatorUri,
793 std::tuple<Name, SignatureInfo>
811 setDefaultCertificateInternal();
822 generateKeyPair(
const Name& identityName,
bool isKsk =
false,
856 pureSign(
const uint8_t* buf,
size_t size,
const Name& keyName,
DigestAlgorithm digestAlgorithm)
const;
859 registerPibImpl(
const std::string& canonicalName,
860 std::initializer_list<std::string> aliases,
PibCreateFunc createFunc);
863 registerTpmImpl(
const std::string& canonicalName,
864 std::initializer_list<std::string> aliases,
TpmCreateFunc createFunc);
886 std::unique_ptr<SecPublicInfo> m_pib;
887 std::unique_ptr<SecTpm> m_tpm;
888 time::milliseconds m_lastTimestamp;
893 KeyChain::signImpl(T& packet,
const SigningInfo& params)
897 std::tie(keyName, sigInfo) = prepareSignatureInfo(params);
899 signPacketWrapper(packet,
Signature(sigInfo),
917 template<
class PibType>
921 registerPibImpl(*aliases.begin(), aliases, [] (
const std::string& locator) {
922 return make_unique<PibType>(locator);
926 template<
class TpmType>
930 registerTpmImpl(*aliases.begin(), aliases, [] (
const std::string& locator) {
931 return make_unique<TpmType>(locator);
941 #define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...) \ 942 static class NdnCxxAuto ## PibType ## PibRegistrationClass \ 945 NdnCxxAuto ## PibType ## PibRegistrationClass() \ 947 ::ndn::KeyChain::registerPib<PibType>({__VA_ARGS__}); \ 949 } ndnCxxAuto ## PibType ## PibRegistrationVariable 957 #define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...) \ 958 static class NdnCxxAuto ## TpmType ## TpmRegistrationClass \ 961 NdnCxxAuto ## TpmType ## TpmRegistrationClass() \ 963 ::ndn::KeyChain::registerTpm<TpmType>({__VA_ARGS__}); \ 965 } ndnCxxAuto ## TpmType ## TpmRegistrationVariable 969 using security::KeyChain;
973 #endif // NDN_SECURITY_KEY_CHAIN_HPP static const RsaKeyParams DEFAULT_KEY_PARAMS
const SecPublicInfo & getPib() const
void addCertificateAsIdentityDefault(const v1::IdentityCertificate &certificate)
Name getNewKeyName(const Name &identityName, bool useKsk)
bool unlockTpm(const char *password, size_t passwordLength, bool usePassword)
const SecTpm & getTpm() const
MismatchError(const std::string &what)
Copyright (c) 2011-2015 Regents of the University of California.
function< unique_ptr< SecPublicInfo >const std::string &)> PibCreateFunc
bool doesCertificateExist(const Name &certificateName) const
Name getDefaultCertificateName() const
shared_ptr< v1::IdentityCertificate > getCertificate(const Name &certificateName) const
bool doesIdentityExist(const Name &identityName) const
void getAllIdentities(std::vector< Name > &nameList, bool isDefault) const
void addIdentity(const Name &identityName)
void importIdentity(const SecuredBag &securedBag, const std::string &passwordStr)
import an identity.
void setDefaultIdentity(const Name &identityName)
shared_ptr< v1::IdentityCertificate > getDefaultCertificate() const
static unique_ptr< SecPublicInfo > createPib(const std::string &pibLocator)
Create a PIB according to pibLocator.
Name getDefaultCertificateNameForIdentity(const Name &identityName) const
DigestAlgorithm getDigestAlgorithm() const
The packet signing interface.
void signByIdentity(T &packet, const Name &identityName)
Sign packet using the default certificate of a particular identity.
void generateSymmetricKeyInTpm(const Name &keyName, const KeyParams ¶ms)
SecTpm is the base class of the TPM classes.
bool doesKeyExistInTpm(const Name &keyName, KeyClass keyClass) const
ConstBufferPtr encryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
Error thrown when the supplied TPM locator to KeyChain constructor does not match the locator stored ...
shared_ptr< v1::IdentityCertificate > selfSign(const Name &keyName)
Generate a self-signed certificate for a public key.
Class representing a wire element of NDN-TLV packet format.
represents an Interest packet
void deleteKeyPairInTpm(const Name &keyName)
void sign(Data &data, const SigningInfo ¶ms=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
void setTpmPassword(const uint8_t *password, size_t passwordLength)
Signing parameters passed to KeyChain.
shared_ptr< SecuredBag > exportIdentity(const Name &identity, const std::string &passwordStr)
export an identity.
void getAllCertificateNames(std::vector< Name > &nameList, bool isDefault) const
static void registerPib(std::initializer_list< std::string > aliases)
Register a new PIB.
shared_ptr< v1::PublicKey > getPublicKeyFromTpm(const Name &keyName) const
Name generateEcdsaKeyPairAsDefault(const Name &identityName, bool isKsk=false, uint32_t keySize=256)
Generate a pair of ECDSA keys for the specified identity and set it as default key for the identity...
bool doesPublicKeyExist(const Name &keyName) const
static const SigningInfo DEFAULT_SIGNING_INFO
void generateKeyPairInTpm(const Name &keyName, const KeyParams ¶ms)
Name getDefaultCertificateNameForKey(const Name &keyName) const
Name createIdentity(const Name &identityName, const KeyParams ¶ms=DEFAULT_KEY_PARAMS)
Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed ce...
void addPublicKey(const Name &keyName, KeyType keyType, const v1::PublicKey &publicKeyDer)
function< unique_ptr< SecTpm >const std::string &)> TpmCreateFunc
static std::string getDefaultTpmLocator()
Get default TPM locator.
void addAppToAcl(const Name &keyName, KeyClass keyClass, const std::string &appPath, AclType acl)
void addKey(const Name &keyName, const v1::PublicKey &publicKeyDer)
bool generateRandomBlock(uint8_t *res, size_t size) const
void setInTerminal(bool inTerminal)
void getAllCertificateNamesOfKey(const Name &keyName, std::vector< Name > &nameList, bool isDefault) const
std::map< std::string, Block > SignParams
void deletePublicKeyInfo(const Name &keyName)
Block signInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, DigestAlgorithm digestAlgorithm)
Name abstraction to represent an absolute name.
signer is a certificate, use it directly
shared_ptr< v1::PublicKey > getPublicKey(const Name &keyName) const
Name getDefaultKeyNameForIdentity(const Name &identityName) const
void deleteIdentity(const Name &identity)
delete an identity.
void deleteIdentityInfo(const Name &identity)
Name generateRsaKeyPair(const Name &identityName, bool isKsk=false, uint32_t keySize=2048)
Generate a pair of RSA keys for the specified identity.
void addCertificate(const v1::IdentityCertificate &certificate)
static tlv::SignatureTypeValue getSignatureType(KeyType keyType, DigestAlgorithm digestAlgorithm)
KeyChain()
Constructor to create KeyChain with default PIB and TPM.
static unique_ptr< SecTpm > createTpm(const std::string &tpmLocator)
Create a TPM according to tpmLocator.
static std::string getDefaultPibLocator()
Get default PIB locator.
SecPublicInfo is a base class for the storage of public information.
void addCertificateAsKeyDefault(const v1::IdentityCertificate &certificate)
Base class of key parameters.
signer is an identity, use its default key and default certificate
void getAllKeyNamesOfIdentity(const Name &identity, std::vector< Name > &nameList, bool isDefault) const
Name generateEcdsaKeyPair(const Name &identityName, bool isKsk=false, uint32_t keySize=256)
Generate a pair of ECDSA keys for the specified identity.
void getAllKeyNames(std::vector< Name > &nameList, bool isDefault) const
ConstBufferPtr decryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
void setDefaultKeyNameForIdentity(const Name &keyName)
void addCertificateAsSystemDefault(const v1::IdentityCertificate &certificate)
static const Name DEFAULT_PREFIX
Name getDefaultIdentity() const
void refreshDefaultCertificate()
bool getInTerminal() const
void signWithSha256(Data &data)
Set Sha256 weak signature for data.
shared_ptr< const Buffer > ConstBufferPtr
void setDefaultCertificateNameForKey(const Name &certificateName)
const KeyParams & getDefaultKeyParamsForIdentity(const Name &identityName) const
Get default key parameters for the specified identity.
SimplePublicKeyParams is a template for public keys with only one parameter: size.
shared_ptr< v1::IdentityCertificate > prepareUnsignedIdentityCertificate(const Name &keyName, const Name &signingIdentity, const time::system_clock::TimePoint ¬Before, const time::system_clock::TimePoint ¬After, const std::vector< security::v1::CertificateSubjectDescription > &subjectDescription, const Name &certPrefix=DEFAULT_PREFIX)
prepare an unsigned identity certificate
Name generateRsaKeyPairAsDefault(const Name &identityName, bool isKsk=false, uint32_t keySize=2048)
Generate a pair of RSA keys for the specified identity and set it as default key for the identity...
static const Name DIGEST_SHA256_IDENTITY
A localhost identity which indicates that signature is generated using SHA-256.
void deleteCertificateInfo(const Name &certificateName)
void deleteKey(const Name &keyName)
delete a key.
static void registerTpm(std::initializer_list< std::string > aliases)
Register a new TPM.
ConstBufferPtr exportPrivateKeyPkcs5FromTpm(const Name &keyName, const std::string &password)
void deleteCertificate(const Name &certificateName)
delete a certificate.
bool importPrivateKeyPkcs5IntoTpm(const Name &keyName, const uint8_t *buf, size_t size, const std::string &password)
Error(const std::string &what)
A Signature is storage for the signature-related information (info and value) in a Data packet...