9 #ifndef NDN_SECURITY_KEY_CHAIN_HPP
10 #define NDN_SECURITY_KEY_CHAIN_HPP
17 #include "../interest.hpp"
18 #include "../util/random.hpp"
19 #include "../util/crypto.hpp"
28 #ifdef NDN_CPP_HAVE_OSX_SECURITY
41 template<
class Info,
class Tpm>
57 Info::addIdentity(identityName);
62 keyName = Info::getDefaultKeyNameForIdentity(identityName);
72 certName = Info::getDefaultCertificateNameForKey(keyName);
76 shared_ptr<IdentityCertificate> selfCert =
selfSign(keyName);
77 Info::addCertificateAsIdentityDefault(*selfCert);
78 certName = selfCert->getName();
95 return generateKeyPair(identityName, isKsk,
KEY_TYPE_RSA, keySize);
112 Info::setDefaultKeyNameForIdentity(keyName);
127 shared_ptr<IdentityCertificate>
129 const Name& signingIdentity,
132 const std::vector<CertificateSubjectDescription>& subjectDescription)
135 if (keyName.
size() < 1)
136 return shared_ptr<IdentityCertificate>();
139 if (keyIdPrefix !=
"ksk-" && keyIdPrefix !=
"dsk-")
140 return shared_ptr<IdentityCertificate>();
142 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
147 certName.
append(signingIdentity)
162 certificate->setName(certName);
163 certificate->setNotBefore(notBefore);
164 certificate->setNotAfter(notAfter);
166 shared_ptr<PublicKey> publicKey;
169 publicKey = Info::getPublicKey(keyName);
173 return shared_ptr<IdentityCertificate>();
175 certificate->setPublicKeyInfo(*publicKey);
177 if (subjectDescription.empty())
180 certificate->addSubjectDescription(subDescryptName);
184 std::vector<CertificateSubjectDescription>::const_iterator sdIt =
185 subjectDescription.begin();
186 std::vector<CertificateSubjectDescription>::const_iterator sdEnd =
187 subjectDescription.end();
188 for(; sdIt != sdEnd; sdIt++)
189 certificate->addSubjectDescription(*sdIt);
192 certificate->encode();
210 if (!static_cast<bool>(Info::defaultCertificate()))
212 Info::refreshDefaultCertificate();
214 if (!static_cast<bool>(Info::defaultCertificate()))
216 Name defaultIdentity;
219 defaultIdentity = Info::getDefaultIdentity();
224 defaultIdentity.
append(
"tmp-identity")
225 .
append(reinterpret_cast<uint8_t*>(&random), 4);
228 Info::setDefaultIdentity(defaultIdentity);
229 Info::refreshDefaultCertificate();
233 sign(packet, *Info::defaultCertificate());
247 if (!Info::doesCertificateExist(certificateName))
248 throw InfoError(
"Requested certificate [" + certificateName.
toUri() +
"] doesn't exist");
255 signPacketWrapper(packet, signature,
270 sign(
const uint8_t* buffer,
size_t bufferLength,
const Name& certificateName)
272 if (!Info::doesCertificateExist(certificateName))
273 throw InfoError(
"Requested certificate [" + certificateName.
toUri() +
"] doesn't exist");
299 Name signingCertificateName;
302 signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName);
313 sign(packet, signingCertificateName);
327 Name signingCertificateName;
330 signingCertificateName = Info::getDefaultCertificateNameForIdentity(identityName);
341 return sign(buffer, bufferLength, signingCertificateName);
368 shared_ptr<IdentityCertificate>
371 shared_ptr<PublicKey> pubKey;
374 pubKey = Info::getPublicKey(keyName);
378 return shared_ptr<IdentityCertificate>();
381 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
386 certificate->setName(certificateName);
387 certificate->setNotBefore(time::system_clock::now());
388 certificate->setNotAfter(time::system_clock::now() +
time::days(7300));
389 certificate->setPublicKeyInfo(*pubKey);
391 certificate->encode();
408 throw TpmError(
"private key does not exist!");
430 if (Info::getDefaultCertificateName() == certificateName)
438 Info::deleteCertificateInfo(certificateName);
454 if (Info::getDefaultKeyNameForIdentity(Info::getDefaultIdentity()) == keyName)
462 Info::deletePublicKeyInfo(keyName);
463 Tpm::deleteKeyPairInTpm(keyName);
479 if (Info::getDefaultIdentity() == identity)
487 std::vector<Name> nameList;
488 Info::getAllKeyNamesOfIdentity(identity, nameList,
true);
489 Info::getAllKeyNamesOfIdentity(identity, nameList,
false);
491 Info::deleteIdentityInfo(identity);
493 std::vector<Name>::const_iterator it = nameList.begin();
494 for(; it != nameList.end(); it++)
495 Tpm::deleteKeyPairInTpm(*it);
506 shared_ptr<SecuredBag>
509 if (!Info::doesIdentityExist(identity))
510 throw InfoError(
"Identity does not exist!");
512 Name keyName = Info::getDefaultKeyNameForIdentity(identity);
517 pkcs5 = Tpm::exportPrivateKeyPkcs5FromTpm(keyName, passwordStr);
521 throw InfoError(
"Fail to export PKCS5 of private key");
524 shared_ptr<IdentityCertificate> cert;
527 cert = Info::getCertificate(Info::getDefaultCertificateNameForKey(keyName));
532 Info::addCertificateAsIdentityDefault(*cert);
535 shared_ptr<SecuredBag> secureBag = make_shared<SecuredBag>(boost::cref(*cert),
555 Info::addIdentity(identity);
558 Tpm::importPrivateKeyPkcs5IntoTpm(keyName,
559 securedBag.
getKey()->buf(),
560 securedBag.
getKey()->size(),
563 shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.
toUri());
566 Info::setDefaultKeyNameForIdentity(keyName);
569 Info::addCertificateAsIdentityDefault(securedBag.
getCertificate());
601 generateKeyPair(
const Name& identityName,
bool isKsk =
false,
604 Name keyName = Info::getNewKeyName(identityName, isKsk);
606 Tpm::generateKeyPairInTpm(keyName.toUri(), keyType, keySize);
608 shared_ptr<PublicKey> pubKey = Tpm::getPublicKeyFromTpm(keyName.toUri());
609 Info::addPublicKey(keyName, keyType, *pubKey);
627 data.setSignature(signature);
628 data.setSignatureValue(Tpm::signInTpm(data.wireEncode().value(),
629 data.wireEncode().value_size() -
630 data.getSignature().getValue().size(),
631 keyName, digestAlgorithm));
647 Name signedName = interest.getName();
648 signedName.append(signature.getInfo());
650 Block sigValue = Tpm::signInTpm(signedName.wireEncode().value(),
651 signedName.wireEncode().value_size(),
655 signedName.append(sigValue);
656 interest.setName(signedName);
665 #if defined(NDN_CPP_HAVE_OSX_SECURITY) and defined(NDN_CPP_WITH_OSX_KEYCHAIN)
669 typedef KeyChainImpl<SecPublicInfoSqlite3, SecTpmOsx>
KeyChain;
681 #endif //NDN_CPP_HAVE_OSX_SECURITY
683 #endif //NDN_SECURITY_KEY_CHAIN_HPP
Representing of SHA256-with-RSA signature in a data packet.
Name getSubName(size_t iStartComponent, size_t nComponents) const
Get a new name, constructed as a subset of components.
void toEscapedString(std::ostream &result) const
Write the value to result, escaping characters according to the NDN URI Scheme.
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Name generateRSAKeyPair(const Name &identityName, bool isKsk=false, int keySize=2048)
Generate a pair of RSA keys for the specified identity.
A CertificateSubjectDescription represents the SubjectDescription entry in a Certificate.
Representing of SHA256 signature in a data packet.
ConstBufferPtr getKey() const
void selfSign(IdentityCertificate &cert)
Self-sign the supplied identity certificate.
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 default key for the identity...
void signByIdentity(T &packet, const Name &identityName)
Sign packet using the default certificate of a particular identity.
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
void importIdentity(const SecuredBag &securedBag, const std::string &passwordStr)
import an identity.
Class representing wire element of the NDN packet.
const Block & getValue() const
void sign(T &packet, const Name &certificateName)
Sign packet with a particular certificate.
void deleteCertificate(const Name &certificateName)
delete a certificate.
size_t wireEncode(EncodingImpl< T > &block, bool unsignedPortion=false) const
Fast encoding or block size estimation.
const Name & getName() const
std::string toUri() const
Encode this name as a URI.
ptr_lib::shared_ptr< const Buffer > ConstBufferPtr
uint32_t generateWord32()
void sign(T &packet)
Sign packet with default identity.
shared_ptr< IdentityCertificate > selfSign(const Name &keyName)
Generate a self-signed certificate for a public key.
void signWithSha256(Data &data)
Set Sha256 weak signature.
void deleteKey(const Name &keyName)
delete a key.
void setValue(const Block &value)
size_t size() const
Get the number of components.
A Name holds an array of Name::Component and represents an NDN name.
const Name & getPublicKeyName() const
shared_ptr< IdentityCertificate > prepareUnsignedIdentityCertificate(const Name &keyName, const Name &signingIdentity, const time::system_clock::TimePoint ¬Before, const time::system_clock::TimePoint ¬After, const std::vector< CertificateSubjectDescription > &subjectDescription)
prepare an unsigned identity certificate
const Signature & getSignature() const
Signature signByIdentity(const uint8_t *buffer, size_t bufferLength, const Name &identityName)
Sign the byte array using the default certificate of a particular identity.
KeyChainImpl< SecPublicInfoSqlite3, SecTpmFile > KeyChain
Name createIdentity(const Name &identityName)
Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed ce...
void setKeyLocator(const KeyLocator &keyLocator)
shared_ptr< SecuredBag > exportIdentity(const Name &identity, const std::string &passwordStr)
export an identity.
KeyChain is one of the main classes of the security library.
void setSignatureValue(const Block &value)
const IdentityCertificate & getCertificate() const
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Signature sign(const uint8_t *buffer, size_t bufferLength, const Name &certificateName)
Sign the byte array using a particular certificate.
Name & appendVersion(uint64_t number)
An alias for appendNumber(uint64_t)
ConstBufferPtr sha256(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
void setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
const Component & get(ssize_t i) const
Get the component at the given index.
void deleteIdentity(const Name &identity)
delete an identity.
duration< boost::int_least32_t, boost::ratio< 86400 > > days
A Signature is storage for the signature-related information (info and value) in a Data packet...