blob: 673f5513c32b4bae340e348c0b2eeb5b0f98ea0f [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa84f4642017-08-23 16:14:51 -04002/*
Laqin Fan0fe72ea2019-05-22 16:42:59 -05003 * Copyright (c) 2013-2019 Regents of the University of California.
Yingdi Yu202a2e92015-07-12 16:49:25 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/transform/private-key.hpp"
Davide Pesaventof45fa212017-09-14 17:23:56 -040023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/encoding/buffer-stream.hpp"
Laqin Fan0fe72ea2019-05-22 16:42:59 -050025#include "ndn-cxx/security/impl/openssl.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "ndn-cxx/security/key-params.hpp"
Laqin Fan0fe72ea2019-05-22 16:42:59 -050027#include "ndn-cxx/security/transform/base64-decode.hpp"
28#include "ndn-cxx/security/transform/bool-sink.hpp"
29#include "ndn-cxx/security/transform/buffer-source.hpp"
30#include "ndn-cxx/security/transform/public-key.hpp"
31#include "ndn-cxx/security/transform/signer-filter.hpp"
32#include "ndn-cxx/security/transform/stream-sink.hpp"
33#include "ndn-cxx/security/transform/verifier-filter.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070034
Davide Pesavento7e780642018-11-24 15:51:34 -050035#include "tests/boost-test.hpp"
36
Luca Keidel941fd8c2017-07-24 15:21:22 +020037#include <boost/mpl/vector.hpp>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040038
39#include <sstream>
Yingdi Yu202a2e92015-07-12 16:49:25 -070040
41namespace ndn {
42namespace security {
43namespace transform {
44namespace tests {
45
46BOOST_AUTO_TEST_SUITE(Security)
47BOOST_AUTO_TEST_SUITE(Transform)
48BOOST_AUTO_TEST_SUITE(TestPrivateKey)
49
laqinfan48f97242019-06-03 15:33:58 -050050BOOST_AUTO_TEST_CASE(LoadRaw)
51{
52 const Buffer buf(32);
53 PrivateKey sKey;
54 sKey.loadRaw(KeyType::HMAC, buf.data(), buf.size());
55 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::HMAC);
56 BOOST_CHECK_EQUAL(sKey.getKeySize(), 256);
57
58 PrivateKey sKey2;
59 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::NONE, buf.data(), buf.size()), std::invalid_argument);
60 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::RSA, buf.data(), buf.size()), std::invalid_argument);
61 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::EC, buf.data(), buf.size()), std::invalid_argument);
62 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::AES, buf.data(), buf.size()), std::invalid_argument);
63}
64
Davide Pesaventof45fa212017-09-14 17:23:56 -040065struct RsaKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070066{
Davide Pesaventof45fa212017-09-14 17:23:56 -040067 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070068 "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
69 "sNozS5t4HnwTZhwwXbH7k3QN0kR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YEAsM3Q7OjILcIg0t6uht9e\n"
85 "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n"
86 "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n"
87 "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n"
88 "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n"
89 "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n"
90 "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n"
91 "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n"
92 "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -040093 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070094 "MIIFCzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIOKYJXvB6p8kCAggA\n"
95 "MBEGBSsOAwIHBAiQgMK8kQXTyASCBMjeNiKYYw5/yHgs9BfSGrpqvV0LkkgMQNUW\n"
96 "R4ZY8fuNjZynd+PxDuw2pyrv1Yv3jc+tupwUehZEzYOnGd53wQAuLO+Z0TBgRFN7\n"
97 "Lhk+AxlT7hu0xaB3ZpJ/uvWpgEJHsq/aB/GYgyzXcQo2AiqzERVpMCWJVmE1L977\n"
98 "CHwJmLm5mxclVLYp1UK5lkIBFu/M4nPavmNmYNUU1LOrXRo56TlJ2kUp8gQyQI1P\n"
99 "VPxi4chmlsr/OnQ2d1eZN+euFm0CS+yP+LFgI9Zqd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fRLJl03XByIfT2n92pw3\n"
115 "fP7muOIKLUsEKjOrmn94NwMlfeW13oQHEH2KjPOWFS/tyJHDdVU+of4COH5yg59a\n"
116 "TZqFkOTGeliE1O+6sfF9fRuVxFUF3D8Hpr0JIjdc6+3RgIlGsXc8BwiSjDSI2XW+\n"
117 "vo75/2zPU9t8OeXEIJk2CQGyqLwUJ6dyi/yDRrvZAgjrUvbpcxydnBAHrLbLUGXJ\n"
118 "aEHH2tjEtnTqVyTchr1yHoupcFOCkA0dAA66XqwcssQxJiMGrWTpCbgd9mrTXQaZ\n"
119 "U7afFN1jpO78tgBQUUpImXdHLLsqdN5tefqjileZGZ9x3/C6TNAfDwYJdsicNNn5\n"
120 "y+JVsbltfLWlJxb9teb3dtQiFlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400121 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700122 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
123 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
124 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
125 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
126 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
127 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
128 "ywIDAQAB\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700129};
130
Davide Pesaventof45fa212017-09-14 17:23:56 -0400131struct EcKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -0700132{
Davide Pesaventof45fa212017-09-14 17:23:56 -0400133 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700134 "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
135 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
136 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
137 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
138 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
139 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
140 "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n"
141 "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400142 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700143 "MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA\n"
144 "MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA\n"
145 "MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO\n"
146 "SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594\n"
147 "ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr\n"
148 "akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd\n"
149 "2OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN\n"
150 "6cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG\n"
151 "HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk\n"
152 "q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400153 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700154 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
155 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
156 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
157 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
158 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
159 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
160 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700161};
162
Luca Keidel941fd8c2017-07-24 15:21:22 +0200163using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700164
Luca Keidel941fd8c2017-07-24 15:21:22 +0200165static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700166checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
167{
168 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500169 sKey.loadPkcs8(encoding->data(), encoding->size(), password.data(), password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700170 OBufferStream os;
Luca Keidel941fd8c2017-07-24 15:21:22 +0200171 sKey.savePkcs1(os);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200172 BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400173 os.buf()->begin(), os.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700174}
175
Luca Keidel941fd8c2017-07-24 15:21:22 +0200176static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700177checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
178{
179 OBufferStream os;
180 bufferSource(*encoding) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700181 checkPkcs8Encoding(os.buf(), password, pkcs1);
182}
183
184BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
185{
186 T dataSet;
187
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500188 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700189 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700190 OBufferStream os;
191 bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os);
192 ConstBufferPtr sKeyPkcs1Buf = os.buf();
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400193 const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->data();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700194 size_t sKeyPkcs1Len = sKeyPkcs1Buf->size();
195
Luca Keidel941fd8c2017-07-24 15:21:22 +0200196 // load key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700197 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400198 BOOST_CHECK_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700199
200 std::stringstream ss2(dataSet.privateKeyPkcs1);
201 PrivateKey sKey2;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400202 BOOST_CHECK_NO_THROW(sKey2.loadPkcs1Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700203
204 // load key in pkcs1 format
205 PrivateKey sKey3;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400206 BOOST_CHECK_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700207
208 std::stringstream ss4;
209 ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
210 PrivateKey sKey4;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400211 BOOST_CHECK_NO_THROW(sKey4.loadPkcs1(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700212
Luca Keidel941fd8c2017-07-24 15:21:22 +0200213 // save key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700214 OBufferStream os2;
215 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700216 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400217 os2.buf()->begin(), os2.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700218
219 // save key in pkcs1 format
220 OBufferStream os3;
221 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700222 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400223 os3.buf()->begin(), os3.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700224
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500225 const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700226 size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700227 OBufferStream os4;
228 bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400229 const uint8_t* sKeyPkcs8 = os4.buf()->data();
Davide Pesaventof45fa212017-09-14 17:23:56 -0400230 size_t sKeyPkcs8Len = os4.buf()->size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700231
232 std::string password("password");
233 std::string wrongpw("wrongpw");
Luca Keidel941fd8c2017-07-24 15:21:22 +0200234 auto pwCallback = [&password] (char* buf, size_t size, int) -> int {
235 BOOST_REQUIRE_LE(password.size(), size);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700236 std::copy(password.begin(), password.end(), buf);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200237 return static_cast<int>(password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700238 };
239
Luca Keidel941fd8c2017-07-24 15:21:22 +0200240 // load key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700241 PrivateKey sKey5;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400242 BOOST_CHECK_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500243 password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700244
245 PrivateKey sKey6;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400246 BOOST_CHECK_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700247
248 std::stringstream ss7(dataSet.privateKeyPkcs8);
249 PrivateKey sKey7;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500250 BOOST_CHECK_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700251
252 std::stringstream ss8(dataSet.privateKeyPkcs8);
253 PrivateKey sKey8;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400254 BOOST_CHECK_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700255
256 // load key in pkcs8 format
257 PrivateKey sKey9;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500258 BOOST_CHECK_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700259
260 PrivateKey sKey10;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400261 BOOST_CHECK_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700262
263 std::stringstream ss11;
264 ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
265 PrivateKey sKey11;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500266 BOOST_CHECK_NO_THROW(sKey11.loadPkcs8(ss11, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700267
268 std::stringstream ss12;
269 ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
270 PrivateKey sKey12;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400271 BOOST_CHECK_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700272
Davide Pesaventof45fa212017-09-14 17:23:56 -0400273 // load key using wrong password, Error is expected
Yingdi Yu202a2e92015-07-12 16:49:25 -0700274 PrivateKey sKey13;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500275 BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.data(), wrongpw.size()),
Luca Keidel941fd8c2017-07-24 15:21:22 +0200276 PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700277
Luca Keidel941fd8c2017-07-24 15:21:22 +0200278 // save key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700279 OBufferStream os14;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500280 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.data(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400281 checkPkcs8Base64Encoding(os14.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700282
283 OBufferStream os15;
284 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400285 checkPkcs8Base64Encoding(os15.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700286
287 // save key in pkcs8 format
288 OBufferStream os16;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500289 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.data(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400290 checkPkcs8Encoding(os16.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700291
292 OBufferStream os17;
293 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400294 checkPkcs8Encoding(os17.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700295}
296
297BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
298{
299 T dataSet;
300
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500301 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700302 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700303 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400304 sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700305
306 // derive public key and compare
Davide Pesaventof45fa212017-09-14 17:23:56 -0400307 ConstBufferPtr pKeyBits = sKey.derivePublicKey();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700308 OBufferStream os;
309 bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os);
310 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
311 os.buf()->begin(), os.buf()->end());
312}
313
314BOOST_AUTO_TEST_CASE(RsaDecryption)
315{
316 RsaKeyTestData dataSet;
317
318 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500319 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700320 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400321 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700322
Davide Pesaventof45fa212017-09-14 17:23:56 -0400323 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700324
325 const std::string cipherTextBase64 =
326 "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
327 "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
328 "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
329 "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
330 "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
331 "PmTiSdeDQXZ94U5boDQ4Aw==\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700332 OBufferStream os;
333 bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os);
334
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400335 auto decrypted = sKey.decrypt(os.buf()->data(), os.buf()->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700336 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400337 decrypted->begin(), decrypted->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700338}
339
Davide Pesaventof45fa212017-09-14 17:23:56 -0400340BOOST_AUTO_TEST_CASE(RsaEncryptDecrypt)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700341{
342 RsaKeyTestData dataSet;
343
344 PublicKey pKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500345 pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700346 dataSet.publicKeyPkcs8.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400347 BOOST_CHECK_EQUAL(pKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700348
349 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500350 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700351 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400352 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700353
Davide Pesaventof45fa212017-09-14 17:23:56 -0400354 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700355
Davide Pesaventof45fa212017-09-14 17:23:56 -0400356 auto cipherText = pKey.encrypt(plainText, sizeof(plainText));
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400357 auto decrypted = sKey.decrypt(cipherText->data(), cipherText->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700358 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400359 decrypted->begin(), decrypted->end());
360}
361
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500362BOOST_AUTO_TEST_CASE(UnsupportedDecryption)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400363{
Davide Pesaventof45fa212017-09-14 17:23:56 -0400364 OBufferStream os;
365 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
366
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500367 auto ecKey = generatePrivateKey(EcKeyParams());
368 BOOST_CHECK_THROW(ecKey->decrypt(os.buf()->data(), os.buf()->size()), PrivateKey::Error);
369
370 auto hmacKey = generatePrivateKey(HmacKeyParams());
371 BOOST_CHECK_THROW(hmacKey->decrypt(os.buf()->data(), os.buf()->size()), PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700372}
373
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500374struct RsaKeyGenParams
Yingdi Yu202a2e92015-07-12 16:49:25 -0700375{
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500376 using Params = RsaKeyParams;
377 using hasPublicKey = std::true_type;
378 using canSavePkcs1 = std::true_type;
379};
380
381struct EcKeyGenParams
382{
383 using Params = EcKeyParams;
384 using hasPublicKey = std::true_type;
385 using canSavePkcs1 = std::true_type;
386};
387
388struct HmacKeyGenParams
389{
390 using Params = HmacKeyParams;
391 using hasPublicKey = std::false_type;
392 using canSavePkcs1 = std::false_type;
393};
394
395using KeyGenParams = boost::mpl::vector<RsaKeyGenParams,
396 EcKeyGenParams,
397 HmacKeyGenParams>;
398
399BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyGenParams)
400{
401 unique_ptr<PrivateKey> sKey = generatePrivateKey(typename T::Params());
402 BOOST_CHECK_NE(sKey->getKeyType(), KeyType::NONE);
403 BOOST_CHECK_GT(sKey->getKeySize(), 0);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700404
Luca Keidel941fd8c2017-07-24 15:21:22 +0200405 const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yub263f152015-07-12 16:50:13 -0700406 OBufferStream os;
407 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
408 signerFilter(DigestAlgorithm::SHA256, *sKey) >>
409 streamSink(os));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500410 auto sig = os.buf();
Yingdi Yub263f152015-07-12 16:50:13 -0700411
Yingdi Yub263f152015-07-12 16:50:13 -0700412 bool result = false;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500413 if (typename T::hasPublicKey()) {
414 auto pKeyBits = sKey->derivePublicKey();
415 PublicKey pKey;
416 pKey.loadPkcs8(pKeyBits->data(), pKeyBits->size());
417 BOOST_CHECK_NO_THROW(bufferSource(data, sizeof(data)) >>
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400418 verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
Yingdi Yub263f152015-07-12 16:50:13 -0700419 boolSink(result));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500420 }
421 else {
422#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
423 BOOST_CHECK_THROW(sKey->derivePublicKey(), PrivateKey::Error);
424#endif
425 BOOST_CHECK_NO_THROW(bufferSource(data, sizeof(data)) >>
426 verifierFilter(DigestAlgorithm::SHA256, *sKey, sig->data(), sig->size()) >>
427 boolSink(result));
428 }
Yingdi Yub263f152015-07-12 16:50:13 -0700429 BOOST_CHECK(result);
430
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500431 if (typename T::canSavePkcs1()) {
432 unique_ptr<PrivateKey> sKey2 = generatePrivateKey(typename T::Params());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700433
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500434 OBufferStream os1;
435 sKey->savePkcs1(os1);
436 OBufferStream os2;
437 sKey2->savePkcs1(os2);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700438
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500439 BOOST_CHECK(*os1.buf() != *os2.buf());
440 }
441 else {
442#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
443 OBufferStream os1;
444 BOOST_CHECK_THROW(sKey->savePkcs1(os1), PrivateKey::Error);
445#endif
446 }
Yingdi Yu202a2e92015-07-12 16:49:25 -0700447}
448
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500449BOOST_AUTO_TEST_CASE(GenerateKeyUnsupportedType)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400450{
451 BOOST_CHECK_THROW(generatePrivateKey(AesKeyParams()), std::invalid_argument);
452}
453
Yingdi Yu202a2e92015-07-12 16:49:25 -0700454BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
455BOOST_AUTO_TEST_SUITE_END() // Transform
456BOOST_AUTO_TEST_SUITE_END() // Security
457
458} // namespace tests
459} // namespace transform
460} // namespace security
461} // namespace ndn