blob: f8932dede42fe62e7ed5ae0f93c9e677a35a77f1 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "security/transform/private-key.hpp"
#include "security/transform.hpp"
#include "security/key-params.hpp"
#include "encoding/buffer-stream.hpp"
#include <iostream>
#include <boost/mpl/list.hpp>
#include "boost-test.hpp"
namespace ndn {
namespace security {
namespace transform {
namespace tests {
BOOST_AUTO_TEST_SUITE(Security)
BOOST_AUTO_TEST_SUITE(Transform)
BOOST_AUTO_TEST_SUITE(TestPrivateKey)
class RsaKeyTestData
{
public:
RsaKeyTestData()
{
privateKeyPkcs1 =
"MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
"sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n"
"GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n"
"c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n"
"OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n"
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FlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=\n";
publicKeyPkcs8 =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
"WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
"s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
"pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
"ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
"9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
"ywIDAQAB\n";
}
public:
std::string privateKeyPkcs1;
std::string privateKeyPkcs8;
std::string publicKeyPkcs8;
};
class EcKeyTestData
{
public:
EcKeyTestData()
{
privateKeyPkcs1 =
"MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
"AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
"MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
"vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
"axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
"K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
"YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n"
"ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n";
privateKeyP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FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
}
public:
std::string privateKeyPkcs1;
std::string privateKeyPkcs8;
std::string publicKeyPkcs8;
};
typedef boost::mpl::list<RsaKeyTestData,
EcKeyTestData> KeyTestDataSets;
void
checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
{
PrivateKey sKey;
BOOST_REQUIRE_NO_THROW(sKey.loadPkcs8(encoding->buf(), encoding->size(),
password.c_str(), password.size()));
OBufferStream os;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os));
ConstBufferPtr keyPkcs1Str = os.buf();
BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(), keyPkcs1Str->begin(), keyPkcs1Str->end());
}
void
checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
{
OBufferStream os;
bufferSource(*encoding) >> base64Decode() >> streamSink(os);
checkPkcs8Encoding(os.buf(), password, pkcs1);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
{
T dataSet;
const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
OBufferStream os;
bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os);
ConstBufferPtr sKeyPkcs1Buf = os.buf();
const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->buf();
size_t sKeyPkcs1Len = sKeyPkcs1Buf->size();
// load key in base64 encoded pkcs1 format
PrivateKey sKey;
BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
std::stringstream ss2(dataSet.privateKeyPkcs1);
PrivateKey sKey2;
BOOST_REQUIRE_NO_THROW(sKey2.loadPkcs1Base64(ss2));
// load key in pkcs1 format
PrivateKey sKey3;
BOOST_REQUIRE_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
std::stringstream ss4;
ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
PrivateKey sKey4;
BOOST_REQUIRE_NO_THROW(sKey4.loadPkcs1(ss4));
// save key in base64 encoded pkcs1 format
OBufferStream os2;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
ConstBufferPtr keyPkcs1Base64Str = os2.buf();
BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len,
keyPkcs1Base64Str->begin(), keyPkcs1Base64Str->end());
// save key in pkcs1 format
OBufferStream os3;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
ConstBufferPtr keyPkcs1Str = os3.buf();
BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len,
keyPkcs1Str->begin(), keyPkcs1Str->end());
const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.c_str());
size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size();
OBufferStream os4;
bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4);
ConstBufferPtr sKeyPkcs8Buf = os4.buf();
const uint8_t* sKeyPkcs8 = sKeyPkcs8Buf->buf();
size_t sKeyPkcs8Len = sKeyPkcs8Buf->size();
std::string password("password");
std::string wrongpw("wrongpw");
auto pwCallback = [&] (char* buf, size_t size, int rwflag) -> int {
std::copy(password.begin(), password.end(), buf);
return password.size();
};
// load key in base64 encoded pkcs8 format
PrivateKey sKey5;
BOOST_REQUIRE_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
password.c_str(), password.size()));
PrivateKey sKey6;
BOOST_REQUIRE_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
std::stringstream ss7(dataSet.privateKeyPkcs8);
PrivateKey sKey7;
BOOST_REQUIRE_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.c_str(), password.size()));
std::stringstream ss8(dataSet.privateKeyPkcs8);
PrivateKey sKey8;
BOOST_REQUIRE_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
// load key in pkcs8 format
PrivateKey sKey9;
BOOST_REQUIRE_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len,
password.c_str(), password.size()));
PrivateKey sKey10;
BOOST_REQUIRE_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
std::stringstream ss11;
ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
PrivateKey sKey11;
BOOST_REQUIRE_NO_THROW(sKey11.loadPkcs8(ss11, password.c_str(), password.size()));
std::stringstream ss12;
ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
PrivateKey sKey12;
BOOST_REQUIRE_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
// load key using wrong password, Error is expected.
PrivateKey sKey13;
BOOST_REQUIRE_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.c_str(), wrongpw.size()),
PrivateKey::Error);
// save key in base64 encoded pkcs8 format
OBufferStream os14;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.c_str(), password.size()));
ConstBufferPtr encoded14 = os14.buf();
checkPkcs8Base64Encoding(encoded14, password, sKeyPkcs1Buf);
OBufferStream os15;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
ConstBufferPtr encoded15 = os15.buf();
checkPkcs8Base64Encoding(encoded15, password, sKeyPkcs1Buf);
// save key in pkcs8 format
OBufferStream os16;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.c_str(), password.size()));
ConstBufferPtr encoded16 = os16.buf();
checkPkcs8Encoding(encoded16, password, sKeyPkcs1Buf);
OBufferStream os17;
BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
ConstBufferPtr encoded17 = os17.buf();
checkPkcs8Encoding(encoded17, password, sKeyPkcs1Buf);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
{
T dataSet;
const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
PrivateKey sKey;
BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
// derive public key and compare
ConstBufferPtr pKeyBits= sKey.derivePublicKey();
OBufferStream os;
bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os);
BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
os.buf()->begin(), os.buf()->end());
}
BOOST_AUTO_TEST_CASE(RsaDecryption)
{
RsaKeyTestData dataSet;
PrivateKey sKey;
sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
dataSet.privateKeyPkcs1.size());
const uint8_t plainText[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
const std::string cipherTextBase64 =
"i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
"38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
"60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
"5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
"FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
"PmTiSdeDQXZ94U5boDQ4Aw==\n";
OBufferStream os;
bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os);
ConstBufferPtr decryptText = sKey.decrypt(os.buf()->buf(), os.buf()->size());
BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
decryptText->begin(), decryptText->end());
}
BOOST_AUTO_TEST_CASE(RsaEncryption)
{
RsaKeyTestData dataSet;
PublicKey pKey;
pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()),
dataSet.publicKeyPkcs8.size());
PrivateKey sKey;
sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
dataSet.privateKeyPkcs1.size());
const uint8_t plainText[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
ConstBufferPtr cipherText = pKey.encrypt(plainText, sizeof(plainText));
ConstBufferPtr decryptText = sKey.decrypt(cipherText->buf(), cipherText->size());
BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
decryptText->begin(), decryptText->end());
}
typedef boost::mpl::list<RsaKeyParams,
EcKeyParams> TestKeyParams;
BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, TestKeyParams)
{
BOOST_REQUIRE_NO_THROW(generatePrivateKey(T()));
unique_ptr<PrivateKey> sKey = generatePrivateKey(T());
PublicKey pKey;
ConstBufferPtr pKeyBits = sKey->derivePublicKey();
pKey.loadPkcs8(pKeyBits->buf(), pKeyBits->size());
uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
OBufferStream os;
BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
signerFilter(DigestAlgorithm::SHA256, *sKey) >>
streamSink(os));
ConstBufferPtr sig = os.buf();
bool result = false;
BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
verifierFilter(DigestAlgorithm::SHA256, pKey, sig->buf(), sig->size()) >>
boolSink(result));
BOOST_CHECK(result);
unique_ptr<PrivateKey> sKey2 = generatePrivateKey(T());
OBufferStream os1;
sKey->savePkcs1(os1);
ConstBufferPtr key1Pkcs1 = os1.buf();
OBufferStream os2;
sKey2->savePkcs1(os2);
ConstBufferPtr key2Pkcs1 = os2.buf();
BOOST_CHECK(*key1Pkcs1 != *key2Pkcs1);
}
BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
BOOST_AUTO_TEST_SUITE_END() // Transform
BOOST_AUTO_TEST_SUITE_END() // Security
} // namespace tests
} // namespace transform
} // namespace security
} // namespace ndn