Added sign which takes publicKeyDer and privateKeyDer
diff --git a/ndn-cpp/key-chain.cpp b/ndn-cpp/key-chain.cpp
index 36e83eb..c2a10ff 100644
--- a/ndn-cpp/key-chain.cpp
+++ b/ndn-cpp/key-chain.cpp
@@ -66,7 +66,7 @@
* @param dataLength The length of data.
* @param digest A pointer to a buffer of size SHA256_DIGEST_LENGTH to receive the data.
*/
-static void digestSha256(unsigned char *data, unsigned int dataLength, unsigned char *digest)
+static void digestSha256(const unsigned char *data, unsigned int dataLength, unsigned char *digest)
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
@@ -80,7 +80,7 @@
* @param dataLength
* @param digest
*/
-static void setSha256(unsigned char *data, unsigned int dataLength, vector<unsigned char> &digest)
+static void setSha256(const unsigned char *data, unsigned int dataLength, vector<unsigned char> &digest)
{
unsigned char digestBuffer[SHA256_DIGEST_LENGTH];
digestSha256(data, dataLength, digestBuffer);
@@ -109,12 +109,16 @@
digestSha256(encoder.output.array + signedFieldsBeginOffset, signedFieldsEndOffset - signedFieldsBeginOffset, digest);
}
-void KeyChain::defaultSign(Data &data)
+void KeyChain::sign
+ (Data &data, const unsigned char *publicKeyDer, unsigned int publicKeyDerLength,
+ const unsigned char *privateKeyDer, unsigned int privateKeyDerLength)
{
// Set the public key.
- setSha256(DEFAULT_PUBLIC_KEY_DER, sizeof(DEFAULT_PUBLIC_KEY_DER), data.getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest());
+ setSha256(publicKeyDer, publicKeyDerLength, data.getSignedInfo().getPublisherPublicKeyDigest().getPublisherPublicKeyDigest());
data.getSignedInfo().getKeyLocator().setType(ndn_KeyLocatorType_KEY);
- data.getSignedInfo().getKeyLocator().setKeyOrCertificate(DEFAULT_PUBLIC_KEY_DER, sizeof(DEFAULT_PUBLIC_KEY_DER));
+ data.getSignedInfo().getKeyLocator().setKeyOrCertificate(publicKeyDer, publicKeyDerLength);
+ // Clear the signature so we don't encode it below.
+ data.getSignature().clear();
// Sign the fields.
unsigned char dataFieldsDigest[SHA256_DIGEST_LENGTH];
@@ -122,8 +126,8 @@
// TODO: use RSA_size to get the proper size of the signature buffer.
unsigned char signature[1000];
unsigned int signatureLength;
- const unsigned char *keyPointer = DEFAULT_PRIVATE_KEY_DER;
- RSA *privateKey = d2i_RSAPrivateKey(NULL, &keyPointer, sizeof(DEFAULT_PRIVATE_KEY_DER));
+ const unsigned char *keyPointer = privateKeyDer;
+ RSA *privateKey = d2i_RSAPrivateKey(NULL, &keyPointer, privateKeyDerLength);
if (!privateKey)
throw std::runtime_error("Error decoding private key in d2i_RSAPrivateKey");
int success = RSA_sign(NID_sha256, dataFieldsDigest, sizeof(dataFieldsDigest), signature, &signatureLength, privateKey);
@@ -135,4 +139,9 @@
data.getSignature().setSignature(signature, signatureLength);
}
+void KeyChain::defaultSign(Data &data)
+{
+ sign(data, DEFAULT_PUBLIC_KEY_DER, sizeof(DEFAULT_PUBLIC_KEY_DER), DEFAULT_PRIVATE_KEY_DER, sizeof(DEFAULT_PRIVATE_KEY_DER));
+}
+
}
diff --git a/ndn-cpp/key-chain.hpp b/ndn-cpp/key-chain.hpp
index 97991e9..8de7a62 100644
--- a/ndn-cpp/key-chain.hpp
+++ b/ndn-cpp/key-chain.hpp
@@ -13,14 +13,25 @@
class KeyChain {
public:
/**
- * In data, set the signed info publisher public key digest and key locator key to the default public key and set the
- * signature using the default private key.
+ * In data, set the signed info publisher public key digest and key locator key to the public key and set the
+ * signature using the private key.
* Note: the caller must make sure the timestamp is correct, for example with
* data.getSignedInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
- * @param data The Data object to sign and set the signature.
+ * @param data The Data object to sign and set the key and signature.
+ * @param publicKeyDer A pointer to a buffer with the DER-encoded public key.
+ * @param publicKeyDerLength The number of bytes in publicKeyDer.
+ * @param privateKeyDer A pointer to a buffer with the DER-encoded private key.
+ * @param privateKeyDerLength The number of bytes in privateKeyDer.
+ */
+ static void sign
+ (Data &data, const unsigned char *publicKeyDer, unsigned int publicKeyDerLength,
+ const unsigned char *privateKeyDer, unsigned int privateKeyDerLength);
+
+ /**
+ * Call sign with the default public and private keys.
+ * @param data
*/
static void defaultSign(Data &data);
-
};
}