security: Added IdentityCertificate::certificateNameToPublicKeyName and use it in signByCertificate so that it doesn't t need to retrieve the certificate just to get the key name.
diff --git a/include/ndn-cpp/security/certificate/identity-certificate.hpp b/include/ndn-cpp/security/certificate/identity-certificate.hpp
index d111acc..7ce95f9 100644
--- a/include/ndn-cpp/security/certificate/identity-certificate.hpp
+++ b/include/ndn-cpp/security/certificate/identity-certificate.hpp
@@ -49,6 +49,14 @@
static bool
isIdentityCertificate(const Certificate& certificate);
+ /**
+ * Get the public key name from the full certificate name.
+ * @param certificateName The full certificate name.
+ * @return The related public key name.
+ */
+ static Name
+ certificateNameToPublicKeyName(const Name& certificateName);
+
private:
static bool
isCorrectName(const Name& name);
diff --git a/src/security/certificate/identity-certificate.cpp b/src/security/certificate/identity-certificate.cpp
index 2e2ea79..95f6c1b 100644
--- a/src/security/certificate/identity-certificate.cpp
+++ b/src/security/certificate/identity-certificate.cpp
@@ -67,8 +67,18 @@
void
IdentityCertificate::setPublicKeyName()
{
- const Name& certificateName = getName();
+ publicKeyName_ = certificateNameToPublicKeyName(getName());
+}
+bool
+IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
+{
+ return isCorrectName(certificate.getName());
+}
+
+Name
+IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
+{
int i = certificateName.size() - 1;
string idString("ID-CERT");
for (; i >= 0; i--) {
@@ -83,13 +93,7 @@
break;
}
- publicKeyName_ = tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
-}
-
-bool
-IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
-{
- return isCorrectName(certificate.getName());
+ return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
}
}
diff --git a/src/security/identity/identity-manager.cpp b/src/security/identity/identity-manager.cpp
index 6ee3082..90bc043 100644
--- a/src/security/identity/identity-manager.cpp
+++ b/src/security/identity/identity-manager.cpp
@@ -187,9 +187,8 @@
ptr_lib::shared_ptr<Signature>
IdentityManager::signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName)
-{
- shared_ptr<IdentityCertificate> certificate = getCertificate(certificateName);
- Name keyName = certificate->getPublicKeyName();
+{
+ Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName.toUri());
Blob sigBits = privateKeyStorage_->sign(buffer, bufferLength, keyName.toUri());
@@ -211,8 +210,7 @@
void
IdentityManager::signByCertificate(Data &data, const Name &certificateName, WireFormat& wireFormat)
{
- shared_ptr<IdentityCertificate> certificate = getCertificate(certificateName);
- Name keyName = certificate->getPublicKeyName();
+ Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certificateName);
shared_ptr<PublicKey> publicKey = privateKeyStorage_->getPublicKey(keyName);
// For temporary usage, we support RSA + SHA256 only, but will support more.