23 #ifndef NDN_KEY_CHAIN_HPP
24 #define NDN_KEY_CHAIN_HPP
28 #include "../data.hpp"
29 #include "../interest.hpp"
30 #include "../face.hpp"
31 #include "identity/identity-manager.hpp"
32 #include "policy/validation-request.hpp"
33 #include "pib/pib.hpp"
34 #include "pib/pib.hpp"
35 #include "tpm/tpm.hpp"
36 #include "signing-info.hpp"
37 #include "key-params.hpp"
38 #include "safe-bag.hpp"
59 class Error :
public std::runtime_error
62 Error(
const std::string& what)
63 : std::runtime_error(what)
94 typedef func_lib::function<ptr_lib::shared_ptr<PibImpl>
95 (
const std::string& location)> MakePibImpl;
97 typedef func_lib::function<ptr_lib::shared_ptr<TpmBackEnd>
98 (
const std::string& location)> MakeTpmBackEnd;
114 (
const std::string& pibLocator,
const std::string& tpmLocator,
115 bool allowReset =
false);
127 (
const ptr_lib::shared_ptr<PibImpl>& pibImpl,
128 const ptr_lib::shared_ptr<TpmBackEnd>& tpmBackEnd,
129 const ptr_lib::shared_ptr<PolicyManager>& policyManager =
130 ptr_lib::shared_ptr<PolicyManager>());
140 (
const ptr_lib::shared_ptr<IdentityManager>& identityManager,
141 const ptr_lib::shared_ptr<PolicyManager>& policyManager);
149 KeyChain(
const ptr_lib::shared_ptr<IdentityManager>& identityManager);
166 throw Error(
"getPib is not supported for security v1");
175 throw Error(
"getTpm is not supported for security v1");
205 ptr_lib::shared_ptr<PibIdentity>
207 (
const Name& identityName,
const KeyParams& params = getDefaultKeyParams());
235 ptr_lib::shared_ptr<PibKey>
328 identityManager_->signByCertificate
329 (data, prepareDefaultCertificateName(), wireFormat);
333 sign(data, getDefaultSigningInfo(), wireFormat);
371 identityManager_->signInterestByCertificate
372 (interest, prepareDefaultCertificateName(), wireFormat);
376 sign(interest, getDefaultSigningInfo(), wireFormat);
391 sign(
const uint8_t* buffer,
size_t bufferLength,
392 const SigningInfo& params = getDefaultSigningInfo());
405 ptr_lib::shared_ptr<CertificateV2>
407 (ptr_lib::shared_ptr<PibKey>& key,
433 (
const SafeBag& safeBag,
const uint8_t* password = 0,
434 size_t passwordLength = 0);
449 getPibFactories()[scheme] = makePibImpl;
462 (
const std::string& scheme,
const MakeTpmBackEnd& makeTpmBackEnd)
464 getTpmFactories()[scheme] = makeTpmBackEnd;
487 if (!isSecurityV1_) {
489 (identityName, params);
490 return identity->getDefaultKey()->getDefaultCertificate()->getName();
493 return identityManager_->createIdentityAndCertificate(identityName, params);
526 if (!isSecurityV1_) {
534 identityManager_->deleteIdentity(identityName);
547 return pib_->getDefaultIdentity()->getName();
549 return identityManager_->getDefaultIdentity();
564 return pib_->getDefaultIdentity()->getDefaultKey()->getDefaultCertificate()
567 return identityManager_->getDefaultCertificateName();
584 (
"generateRSAKeyPair is not supported for security v2. Use createIdentityV2.");
586 return identityManager_->generateRSAKeyPair(identityName, isKsk, keySize);
603 (
"generateEcdsaKeyPair is not supported for security v2. Use createIdentityV2.");
605 return identityManager_->generateEcdsaKeyPair(identityName, isKsk, keySize);
620 (
"setDefaultKeyForIdentity is not supported for security v2. Use getPib() methods.");
622 return identityManager_->setDefaultKeyForIdentity(keyName, identityNameCheck);
640 (
"generateRSAKeyPairAsDefault is not supported for security v2. Use createIdentityV2.");
642 return identityManager_->generateRSAKeyPairAsDefault(identityName, isKsk, keySize);
660 (
"generateEcdsaKeyPairAsDefault is not supported for security v2. Use createIdentityV2.");
662 return identityManager_->generateEcdsaKeyPairAsDefault(identityName, isKsk, keySize);
675 ->getKey(keyName)->getPublicKey();
677 return identityManager_->getPublicKey(keyName)->getKeyDer();
689 (
"installIdentityCertificate is not supported for security v2. Use getPib() methods.");
691 identityManager_->addCertificate(certificate);
703 (
"setDefaultCertificateForKey is not supported for security v2. Use getPib() methods.");
705 identityManager_->setDefaultCertificateForKey(certificate);
713 ptr_lib::shared_ptr<IdentityCertificate>
718 (
"getCertificate is not supported for security v2. Use getPib() methods.");
720 return identityManager_->getCertificate(certificateName);
726 ptr_lib::shared_ptr<IdentityCertificate>
731 (
"getIdentityCertificate is not supported for security v2. Use getPib() methods.");
733 return identityManager_->getCertificate(certificateName);
760 const ptr_lib::shared_ptr<IdentityManager>&
764 throw Error(
"getIdentityManager is not supported for security v2");
766 return identityManager_;
777 const ptr_lib::shared_ptr<PolicyManager>&
794 if (!isSecurityV1_) {
797 sign(data, signingInfo, wireFormat);
801 identityManager_->signByCertificate(data, certificateName, wireFormat);
818 if (!isSecurityV1_) {
821 sign(interest, signingInfo, wireFormat);
825 identityManager_->signInterestByCertificate
826 (interest, certificateName, wireFormat);
836 ptr_lib::shared_ptr<Signature>
837 sign(
const uint8_t* buffer,
size_t bufferLength,
const Name& certificateName)
841 (
"sign(buffer, certificateName) is not supported for security v2. Use sign with SigningInfo.");
843 return identityManager_->signByCertificate
844 (buffer, bufferLength, certificateName);
853 ptr_lib::shared_ptr<Signature>
854 sign(
const std::vector<uint8_t>& buffer,
const Name& certificateName)
856 return sign(&buffer[0], buffer.size(), certificateName);
875 ptr_lib::shared_ptr<Signature>
884 ptr_lib::shared_ptr<Signature>
902 if (!isSecurityV1_) {
905 sign(data, signingInfo, wireFormat);
909 identityManager_->signWithSha256(data, wireFormat);
925 if (!isSecurityV1_) {
928 sign(interest, signingInfo, wireFormat);
932 identityManager_->signInterestWithSha256(interest, wireFormat);
952 (
const ptr_lib::shared_ptr<Data>& data,
const OnVerified& onVerified,
973 (
const ptr_lib::shared_ptr<Data>& data,
const OnVerified& onVerified,
993 (
const ptr_lib::shared_ptr<Interest>& interest,
1016 (
const ptr_lib::shared_ptr<Interest>& interest,
1073 (
const Data& data,
const Blob& key,
1092 getDefaultKeyParams();
1113 (
const std::string& pibLocator,
const std::string& tpmLocator,
1122 static std::map<std::string, MakePibImpl>&
1131 static std::map<std::string, MakeTpmBackEnd>&
1139 (
const std::string& uri, std::string& scheme, std::string& location);
1145 parseAndCheckPibLocator
1146 (
const std::string& pibLocator, std::string& pibScheme,
1147 std::string& pibLocation);
1153 parseAndCheckTpmLocator
1154 (
const std::string& tpmLocator, std::string& tpmScheme,
1155 std::string& tpmLocation);
1158 getDefaultPibScheme();
1161 getDefaultTpmScheme();
1168 static ptr_lib::shared_ptr<Pib>
1169 createPib(
const std::string& pibLocator);
1176 static ptr_lib::shared_ptr<Tpm>
1177 createTpm(
const std::string& tpmLocator);
1194 ptr_lib::shared_ptr<Signature>
1207 sign(
const uint8_t* buffer,
size_t bufferLength,
const Name& keyName,
1208 DigestAlgorithm digestAlgorithm)
const;
1211 getDefaultSigningInfo();
1217 (
const ptr_lib::shared_ptr<const Interest> &interest,
const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
1220 onCertificateInterestTimeout
1221 (
const ptr_lib::shared_ptr<const Interest> &interest,
int retry,
1223 const ptr_lib::shared_ptr<Data> &data,
1224 ptr_lib::shared_ptr<ValidationRequest> nextStep);
1231 onCertificateInterestTimeoutForVerifyInterest
1232 (
const ptr_lib::shared_ptr<const Interest> &interest,
int retry,
1234 const ptr_lib::shared_ptr<Interest>& originalInterest,
1235 ptr_lib::shared_ptr<ValidationRequest> nextStep);
1243 prepareDefaultCertificateName();
1253 ptr_lib::shared_ptr<IdentityManager> identityManager_;
1254 ptr_lib::shared_ptr<PolicyManager> policyManager_;
1257 ptr_lib::shared_ptr<Pib> pib_;
1258 ptr_lib::shared_ptr<Tpm> tpm_;
1260 static std::string* defaultPibLocator_;
1261 static std::string* defaultTpmLocator_;
1262 static std::map<std::string, MakePibImpl>* pibFactories_;
1263 static std::map<std::string, MakeTpmBackEnd>* tpmFactories_;
void revokeKey(const Name &keyName)
Revoke a key.
Definition: key-chain.hpp:741
CommandInterestSigner is a helper class to create command interests.
Definition: command-interest-signer.hpp:38
const ptr_lib::shared_ptr< PolicyManager > & getPolicyManager()
Get the policy manager given to or created by the constructor.
Definition: key-chain.hpp:778
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest)> OnVerifyInterestFailed
An OnVerifyInterestFailed function object is used to pass a callback to verifyInterest to report a fa...
Definition: validation-request.hpp:68
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest)> OnVerifiedInterest
An OnVerifiedInterest function object is used to pass a callback to verifyInterest to report a succes...
Definition: validation-request.hpp:53
Name getDefaultIdentity()
Get the default identity.
Definition: key-chain.hpp:544
ptr_lib::shared_ptr< CertificateV2 > selfSign(ptr_lib::shared_ptr< PibKey > &key, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Generate a self-signed certificate for the public key and add it to the PIB.
Definition: key-chain.cpp:392
static bool verifyDataWithHmacWithSha256(const Data &data, const Blob &key, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Compute a new HmacWithSha256 for the data packet and verify it against the signature value...
A SigningInfo holds the signing parameters passed to the KeyChain.
Definition: signing-info.hpp:39
void installIdentityCertificate(const IdentityCertificate &certificate)
Install an identity certificate into the public key identity storage.
Definition: key-chain.hpp:685
void signWithSha256(Data &data, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, digest it and set its SignatureInfo to a DigestSha256.
Definition: key-chain.hpp:900
static void registerPibBackend(const std::string &scheme, const MakePibImpl &makePibImpl)
Add to the PIB factories map where scheme is the key and makePibImpl is the value.
Definition: key-chain.hpp:447
Definition: key-params.hpp:80
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
A KeyChain::LocatorMismatchError extends KeyChain::Error to indicate that the supplied TPM locator do...
Definition: key-chain.hpp:85
Definition: identity-certificate.hpp:30
bool getIsSecurityV1() const
Get the flag set by the constructor if this is a security v1 or v2 KeyChain.
Definition: key-chain.hpp:186
void verifyInterest(const ptr_lib::shared_ptr< Interest > &interest, const OnVerifiedInterest &onVerified, const OnInterestValidationFailed &onValidationFailed, int stepCount=0, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Check the signature on the signed interest and call either onVerify or onValidationFailed.
Definition: key-chain.cpp:588
static void registerTpmBackend(const std::string &scheme, const MakeTpmBackEnd &makeTpmBackEnd)
Add to the TPM factories map where scheme is the key and makeTpmBackEnd is the value.
Definition: key-chain.hpp:462
ptr_lib::shared_ptr< Signature > sign(const std::vector< uint8_t > &buffer, const Name &certificateName)
Sign the byte array using a certificate name and return a Signature object.
Definition: key-chain.hpp:854
The TPM (Trusted Platform Module) stores the private portion of a user's cryptography keys...
Definition: tpm.hpp:54
static void signWithHmacWithSha256(Data &data, const Blob &key, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, compute an HmacWithSha256 and update the signature value.
void sign(Interest &interest, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Sign the Interest with the default key of the default identity.
Definition: key-chain.hpp:367
void signByIdentity(Data &data, const Name &identityName=Name(), WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, sign it and set its signature.
Definition: key-chain.cpp:482
Name generateEcdsaKeyPairAsDefault(const Name &identityName, bool isKsk=false, int keySize=256)
Generate a pair of ECDSA keys for the specified identity and set it as the default key for the identi...
Definition: key-chain.hpp:656
void addCertificate(PibKey &key, const CertificateV2 &certificate)
Add a certificate for the key.
Definition: key-chain.cpp:305
ptr_lib::shared_ptr< PibIdentity > createIdentityV2(const Name &identityName, const KeyParams ¶ms=getDefaultKeyParams())
Create a security V2 identity for identityName.
Definition: key-chain.cpp:222
CertificateV2 represents a certificate following the certificate format naming convention.
Definition: certificate-v2.hpp:81
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data)> OnVerified
An OnVerified function object is used to pass a callback to verifyData to report a successful verific...
Definition: validation-request.hpp:33
static const RsaKeyParams DEPRECATED_IN_NDN_CPP DEFAULT_KEY_PARAMS
Definition: key-chain.hpp:1097
SigningInfo & setSigningCertificateName(const Name &certificateName)
Set this to type SIGNER_TYPE_CERT and a certificate with name certificateName.
Definition: signing-info.hpp:151
Name generateRSAKeyPairAsDefault(const Name &identityName, bool isKsk=false, int keySize=2048)
Generate a pair of RSA keys for the specified identity and set it as the default key for the identity...
Definition: key-chain.hpp:636
Name createIdentityAndCertificate(const Name &identityName, const KeyParams ¶ms=getDefaultKeyParams())
Create a security v1 identity by creating a pair of Key-Signing-Key (KSK) for this identity and a sel...
Definition: key-chain.hpp:485
void setFace(Face *face)
Set the Face which will be used to fetch required certificates.
Definition: key-chain.hpp:1026
ptr_lib::shared_ptr< Signature > signByIdentity(const std::vector< uint8_t > &buffer, const Name &identityName)
Sign the byte array using an identity name and return a Signature object.
Definition: key-chain.hpp:885
ptr_lib::shared_ptr< IdentityCertificate > getCertificate(const Name &certificateName)
Get a certificate with the specified name.
Definition: key-chain.hpp:714
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
Definition: identity-certificate.cpp:101
ptr_lib::shared_ptr< IdentityCertificate > DEPRECATED_IN_NDN_CPP getIdentityCertificate(const Name &certificateName)
Definition: key-chain.hpp:727
void setDefaultKey(PibIdentity &identity, PibKey &key)
Set the key as the default key of identity.
Definition: key-chain.cpp:295
void sign(Data &data, const Name &certificateName, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, sign it and set its signature.
Definition: key-chain.hpp:791
Name DEPRECATED_IN_NDN_CPP createIdentity(const Name &identityName, const KeyParams ¶ms=getDefaultKeyParams())
Create a security v1 identity by creating a pair of Key-Signing-Key (KSK) for this identity and a sel...
Definition: key-chain.hpp:511
The PibKey class provides access to a key at the second level in the PIB's Identity-Key-Certificate h...
Definition: pib-key.hpp:43
KeyChain is the main class of the security library.
Definition: key-chain.hpp:53
ptr_lib::shared_ptr< PibKey > createKey(PibIdentity &identity, const KeyParams ¶ms=getDefaultKeyParams())
Create a key for the identity according to params.
Definition: key-chain.cpp:266
void sign(Data &data, const SigningInfo ¶ms, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, sign it according to the supplied signing parameters, and set its signature.
Definition: key-chain.cpp:335
PibIdentity is at the top level in PIB's Identity-Key-Certificate hierarchy.
Definition: pib-identity.hpp:48
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
void deleteIdentity(const Name &identityName)
Delete the identity from the public and private key storage.
Definition: key-chain.hpp:524
A KeyChain::Error extends runtime_error and represents an error in KeyChain processing.
Definition: key-chain.hpp:59
ptr_lib::shared_ptr< Signature > sign(const uint8_t *buffer, size_t bufferLength, const Name &certificateName)
Sign the byte array using a certificate name and return a Signature object.
Definition: key-chain.hpp:837
Blob createSigningRequest(const Name &keyName)
Create a public key signing request.
Definition: key-chain.hpp:671
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:43
A KeyChain::InvalidSigningInfoError extends KeyChain::Error to indicate that the supplied SigningInfo...
Definition: key-chain.hpp:72
void sign(Data &data, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Wire encode the Data object, sign it with the default key of the default identity, and set its signature.
Definition: key-chain.hpp:325
void setDefaultKeyForIdentity(const Name &keyName, const Name &identityNameCheck=Name())
Set a key as the default key of an identity.
Definition: key-chain.hpp:616
KeyChain()
Create a KeyChain with the default PIB and TPM, which are platform-dependent and can be overridden sy...
Definition: key-chain.cpp:141
A SafeBag represents a container for sensitive related information such as a certificate and private ...
Definition: safe-bag.hpp:35
void setDefaultCertificateForKey(const IdentityCertificate &certificate)
Set the certificate as the default for its corresponding key.
Definition: key-chain.hpp:699
func_lib::function< void(const ptr_lib::shared_ptr< Interest > &interest, const std::string &reason)> OnInterestValidationFailed
An OnInterestValidationFailed function object is used to pass a callback to verifyInterest to report ...
Definition: validation-request.hpp:61
void deleteKey(PibIdentity &identity, PibKey &key)
Delete the given key of the given identity.
Definition: key-chain.cpp:283
void deleteCertificate(PibKey &key, const Name &certificateName)
Delete the certificate with the given name from the given key.
Definition: key-chain.cpp:316
static Name extractIdentityFromKeyName(const Name &keyName)
Extract the identity namespace from keyName.
Definition: pib-key.cpp:90
static bool verifyInterestWithHmacWithSha256(const Interest &interest, const Blob &key, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Compute a new HmacWithSha256 for all but the final name component and verify it against the signature...
KeyParams is a base class for key parameters.
Definition: key-params.hpp:36
Name generateEcdsaKeyPair(const Name &identityName, bool isKsk=false, int keySize=256)
Generate a pair of ECDSA keys for the specified identity.
Definition: key-chain.hpp:599
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data)> OnVerifyFailed
An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verific...
Definition: validation-request.hpp:46
void setDefaultIdentity(PibIdentity &identity)
Set the identity as the default identity.
Definition: key-chain.cpp:260
void setDefaultCertificate(PibKey &key, const CertificateV2 &certificate)
Set the certificate as the default certificate of the key.
Definition: key-chain.cpp:326
A ConfigFile locates, opens, and parses a library configuration file, and holds the values for the ap...
Definition: config-file.hpp:36
SigningInfo & setSha256Signing()
Set this to type SIGNER_TYPE_SHA256, and set the digest algorithm to DIGEST_ALGORITHM_SHA256.
Definition: signing-info.hpp:164
void deleteIdentity(PibIdentity &identity)
Delete the identity.
Definition: key-chain.cpp:246
Name generateRSAKeyPair(const Name &identityName, bool isKsk=false, int keySize=2048)
Generate a pair of RSA keys for the specified identity.
Definition: key-chain.hpp:580
Name getDefaultCertificateName()
Get the default certificate name of the default identity.
Definition: key-chain.hpp:561
const ptr_lib::shared_ptr< IdentityManager > & getIdentityManager()
Get the identity manager given to or created by the constructor.
Definition: key-chain.hpp:761
A Pib::Error extends runtime_error and represents a semantic error in PIB processing.
Definition: pib.hpp:60
void importSafeBag(const SafeBag &safeBag, const uint8_t *password=0, size_t passwordLength=0)
Import a certificate and its corresponding private key encapsulated in a SafeBag. ...
Definition: key-chain.cpp:424
void verifyData(const ptr_lib::shared_ptr< Data > &data, const OnVerified &onVerified, const OnDataValidationFailed &onValidationFailed, int stepCount=0)
Check the signature on the Data object and call either onVerify or onValidationFailed.
Definition: key-chain.cpp:529
In general, a PIB (Public Information Base) stores the public portion of a user's cryptography keys...
Definition: pib.hpp:54
void revokeCertificate(const Name &certificateName)
Revoke a certificate.
Definition: key-chain.hpp:751
func_lib::function< void(const ptr_lib::shared_ptr< Data > &data, const std::string &reason)> OnDataValidationFailed
An OnDataValidationFailed function object is used to pass a callback to verifyData to report a failed...
Definition: validation-request.hpp:41