blob: 0cef079229ce2404602e4f72f23e99710eac0d56 [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/*
Davide Pesavento74daf742018-11-23 18:14:13 -05003 * Copyright (c) 2013-2018 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"
25#include "ndn-cxx/security/key-params.hpp"
26#include "ndn-cxx/security/transform.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070027
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
29
Luca Keidel941fd8c2017-07-24 15:21:22 +020030#include <boost/mpl/vector.hpp>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040031
32#include <sstream>
Yingdi Yu202a2e92015-07-12 16:49:25 -070033
34namespace ndn {
35namespace security {
36namespace transform {
37namespace tests {
38
39BOOST_AUTO_TEST_SUITE(Security)
40BOOST_AUTO_TEST_SUITE(Transform)
41BOOST_AUTO_TEST_SUITE(TestPrivateKey)
42
Davide Pesaventof45fa212017-09-14 17:23:56 -040043struct RsaKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070044{
Davide Pesaventof45fa212017-09-14 17:23:56 -040045 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070046 "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
47 "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n"
48 "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n"
49 "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n"
50 "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n"
51 "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n"
52 "W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr\n"
53 "cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV\n"
54 "Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b\n"
55 "wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg\n"
56 "Xb4gfR8zUXaXS1sXcnVm3+24vIs9dApUwykuoyjOqxWqcHRec2QT2FxVGkFEraze\n"
57 "CPa4rMECgYEA5Y8CywomIcTgerFGFCeMHJr8nQGqY2V/owFb3k9maczPnC9p4a9R\n"
58 "c5szLxA9FMYFxurQZMBWSEG2JS1HR2mnjigx8UKjYML/A+rvvjZOMe4M6Sy2ggh4\n"
59 "SkLZKpWTzjTe07ByM/j5v/SjNZhWAG7sw4/LmPGRQkwJv+KZhGojuOkCgYEA2cOF\n"
60 "T6cJRv6kvzTz9S0COZOVm+euJh/BXp7oAsAmbNfOpckPMzqHXy8/wpdKl6AAcB57\n"
61 "OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA\n"
62 "Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e\n"
63 "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n"
64 "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n"
65 "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n"
66 "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n"
67 "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n"
68 "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n"
69 "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n"
70 "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -040071 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070072 "MIIFCzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIOKYJXvB6p8kCAggA\n"
73 "MBEGBSsOAwIHBAiQgMK8kQXTyASCBMjeNiKYYw5/yHgs9BfSGrpqvV0LkkgMQNUW\n"
74 "R4ZY8fuNjZynd+PxDuw2pyrv1Yv3jc+tupwUehZEzYOnGd53wQAuLO+Z0TBgRFN7\n"
75 "Lhk+AxlT7hu0xaB3ZpJ/uvWpgEJHsq/aB/GYgyzXcQo2AiqzERVpMCWJVmE1L977\n"
76 "CHwJmLm5mxclVLYp1UK5lkIBFu/M4nPavmNmYNUU1LOrXRo56TlJ2kUp8gQyQI1P\n"
77 "VPxi4chmlsr/OnQ2d1eZN+euFm0CS+yP+LFgI9ZqdyH1w+J43SXdHDzauVcZp7oa\n"
78 "Kw24OrhHfolLAnQIECXEJYeT7tZmhC4O9V6B18PFVyxWnEU4eFNpFE8kYSmm8Um2\n"
79 "buvDKI71q43hm23moYT9uIM1f4M8UkoOliJGrlf4xgEcmDuokEX01PdOq1gc4nvG\n"
80 "0DCwDI9cOsyn8cxhk9UVtFgzuG/seuznxIv1F5H0hzYOyloStXxRisJES0kgByBt\n"
81 "FFTfyoFKRrmCjRIygwVKUSkSDR0DlQS5ZLvQyIswnSQFwxAHqfvoSL4dB9UAIAQ+\n"
82 "ALVF1maaHgptbL6Ifqf0GFCv0hdNCVNDNCdy8R+S6nEYE+YdYSIdT1L88KD5PjU3\n"
83 "YY/CMnxhTncMaT4acPO1UUYuSGRZ/JL6E0ihoqIU+bqUgLSHNzhPySPfN9uqN61Y\n"
84 "HFBtxeEPWKU0f/JPkRBMmZdMI1/OVmA3QHSRBydI+CQN8no2gZRFoVbHTkG8IMpE\n"
85 "1fiDJpwFkpzIv/JPiTSE7DeBH5NJk1bgu7TcuZfa4unyAqss0UuLnXzS06TppkUj\n"
86 "QGft0g8VPW56eli6B4xrSzzuvAdbrxsVfxdmtHPyYxLb3/UG1g4x/H/yULhx7x9P\n"
87 "iI6cw6JUE+8bwJV2ZIlHXXHO+wUp/gCFJ6MHo9wkR1QvnHP2ClJAzBm9OvYnUx2Y\n"
88 "SX0HxEowW8BkhxOF184LEmxeua0yyZUqCdrYmErp7x9EY/LhD1zBwH8OGRa0qzmR\n"
89 "VKxAPKihkb9OgxcUKbvKePx3k2cQ7fbCUspGPm4Kn1zwMgRAZ4fz/o8Lnwc8MSY3\n"
90 "lPWnmLTFu420SRH2g9N0o/r195hiZ5cc+KfF4pwZWKbEbKFk/UfXA9vmOi7BBtDJ\n"
91 "RWshOINhzMU6Ij3KuaEpHni1HoHjw0SQ97ow2x/aB8k2QC28tbsa49lD2KKJku6b\n"
92 "2Or89adwFKqMgS2IXfXMXs/iG5EFLYN6r8e40Dn5f1vJfRLJl03XByIfT2n92pw3\n"
93 "fP7muOIKLUsEKjOrmn94NwMlfeW13oQHEH2KjPOWFS/tyJHDdVU+of4COH5yg59a\n"
94 "TZqFkOTGeliE1O+6sfF9fRuVxFUF3D8Hpr0JIjdc6+3RgIlGsXc8BwiSjDSI2XW+\n"
95 "vo75/2zPU9t8OeXEIJk2CQGyqLwUJ6dyi/yDRrvZAgjrUvbpcxydnBAHrLbLUGXJ\n"
96 "aEHH2tjEtnTqVyTchr1yHoupcFOCkA0dAA66XqwcssQxJiMGrWTpCbgd9mrTXQaZ\n"
97 "U7afFN1jpO78tgBQUUpImXdHLLsqdN5tefqjileZGZ9x3/C6TNAfDwYJdsicNNn5\n"
98 "y+JVsbltfLWlJxb9teb3dtQiFlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -040099 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700100 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
101 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
102 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
103 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
104 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
105 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
106 "ywIDAQAB\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700107};
108
Davide Pesaventof45fa212017-09-14 17:23:56 -0400109struct EcKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -0700110{
Davide Pesaventof45fa212017-09-14 17:23:56 -0400111 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700112 "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
113 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
114 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
115 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
116 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
117 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
118 "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n"
119 "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400120 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700121 "MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA\n"
122 "MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA\n"
123 "MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO\n"
124 "SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594\n"
125 "ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr\n"
126 "akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd\n"
127 "2OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN\n"
128 "6cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG\n"
129 "HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk\n"
130 "q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400131 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700132 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
133 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
134 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
135 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
136 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
137 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
138 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700139};
140
Luca Keidel941fd8c2017-07-24 15:21:22 +0200141using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700142
Luca Keidel941fd8c2017-07-24 15:21:22 +0200143static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700144checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
145{
146 PrivateKey sKey;
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400147 sKey.loadPkcs8(encoding->data(), encoding->size(), password.c_str(), password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700148 OBufferStream os;
Luca Keidel941fd8c2017-07-24 15:21:22 +0200149 sKey.savePkcs1(os);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200150 BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400151 os.buf()->begin(), os.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700152}
153
Luca Keidel941fd8c2017-07-24 15:21:22 +0200154static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700155checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
156{
157 OBufferStream os;
158 bufferSource(*encoding) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700159 checkPkcs8Encoding(os.buf(), password, pkcs1);
160}
161
162BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
163{
164 T dataSet;
165
166 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
167 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700168 OBufferStream os;
169 bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os);
170 ConstBufferPtr sKeyPkcs1Buf = os.buf();
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400171 const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->data();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700172 size_t sKeyPkcs1Len = sKeyPkcs1Buf->size();
173
Luca Keidel941fd8c2017-07-24 15:21:22 +0200174 // load key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700175 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400176 BOOST_CHECK_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700177
178 std::stringstream ss2(dataSet.privateKeyPkcs1);
179 PrivateKey sKey2;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400180 BOOST_CHECK_NO_THROW(sKey2.loadPkcs1Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700181
182 // load key in pkcs1 format
183 PrivateKey sKey3;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400184 BOOST_CHECK_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700185
186 std::stringstream ss4;
187 ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
188 PrivateKey sKey4;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400189 BOOST_CHECK_NO_THROW(sKey4.loadPkcs1(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700190
Luca Keidel941fd8c2017-07-24 15:21:22 +0200191 // save key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700192 OBufferStream os2;
193 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700194 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400195 os2.buf()->begin(), os2.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700196
197 // save key in pkcs1 format
198 OBufferStream os3;
199 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700200 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400201 os3.buf()->begin(), os3.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700202
203 const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.c_str());
204 size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700205 OBufferStream os4;
206 bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400207 const uint8_t* sKeyPkcs8 = os4.buf()->data();
Davide Pesaventof45fa212017-09-14 17:23:56 -0400208 size_t sKeyPkcs8Len = os4.buf()->size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700209
210 std::string password("password");
211 std::string wrongpw("wrongpw");
Luca Keidel941fd8c2017-07-24 15:21:22 +0200212 auto pwCallback = [&password] (char* buf, size_t size, int) -> int {
213 BOOST_REQUIRE_LE(password.size(), size);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700214 std::copy(password.begin(), password.end(), buf);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200215 return static_cast<int>(password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700216 };
217
Luca Keidel941fd8c2017-07-24 15:21:22 +0200218 // load key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700219 PrivateKey sKey5;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400220 BOOST_CHECK_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
221 password.c_str(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700222
223 PrivateKey sKey6;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400224 BOOST_CHECK_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700225
226 std::stringstream ss7(dataSet.privateKeyPkcs8);
227 PrivateKey sKey7;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400228 BOOST_CHECK_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.c_str(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700229
230 std::stringstream ss8(dataSet.privateKeyPkcs8);
231 PrivateKey sKey8;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400232 BOOST_CHECK_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700233
234 // load key in pkcs8 format
235 PrivateKey sKey9;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400236 BOOST_CHECK_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, password.c_str(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700237
238 PrivateKey sKey10;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400239 BOOST_CHECK_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700240
241 std::stringstream ss11;
242 ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
243 PrivateKey sKey11;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400244 BOOST_CHECK_NO_THROW(sKey11.loadPkcs8(ss11, password.c_str(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700245
246 std::stringstream ss12;
247 ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
248 PrivateKey sKey12;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400249 BOOST_CHECK_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700250
Davide Pesaventof45fa212017-09-14 17:23:56 -0400251 // load key using wrong password, Error is expected
Yingdi Yu202a2e92015-07-12 16:49:25 -0700252 PrivateKey sKey13;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400253 BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.c_str(), wrongpw.size()),
Luca Keidel941fd8c2017-07-24 15:21:22 +0200254 PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700255
Luca Keidel941fd8c2017-07-24 15:21:22 +0200256 // save key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700257 OBufferStream os14;
258 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.c_str(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400259 checkPkcs8Base64Encoding(os14.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700260
261 OBufferStream os15;
262 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400263 checkPkcs8Base64Encoding(os15.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700264
265 // save key in pkcs8 format
266 OBufferStream os16;
267 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.c_str(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400268 checkPkcs8Encoding(os16.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700269
270 OBufferStream os17;
271 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400272 checkPkcs8Encoding(os17.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700273}
274
275BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
276{
277 T dataSet;
278
279 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
280 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700281 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400282 sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700283
284 // derive public key and compare
Davide Pesaventof45fa212017-09-14 17:23:56 -0400285 ConstBufferPtr pKeyBits = sKey.derivePublicKey();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700286 OBufferStream os;
287 bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os);
288 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
289 os.buf()->begin(), os.buf()->end());
290}
291
292BOOST_AUTO_TEST_CASE(RsaDecryption)
293{
294 RsaKeyTestData dataSet;
295
296 PrivateKey sKey;
297 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
298 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400299 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700300
Davide Pesaventof45fa212017-09-14 17:23:56 -0400301 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700302
303 const std::string cipherTextBase64 =
304 "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
305 "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
306 "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
307 "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
308 "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
309 "PmTiSdeDQXZ94U5boDQ4Aw==\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700310 OBufferStream os;
311 bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os);
312
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400313 auto decrypted = sKey.decrypt(os.buf()->data(), os.buf()->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700314 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400315 decrypted->begin(), decrypted->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700316}
317
Davide Pesaventof45fa212017-09-14 17:23:56 -0400318BOOST_AUTO_TEST_CASE(RsaEncryptDecrypt)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700319{
320 RsaKeyTestData dataSet;
321
322 PublicKey pKey;
323 pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()),
324 dataSet.publicKeyPkcs8.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400325 BOOST_CHECK_EQUAL(pKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700326
327 PrivateKey sKey;
328 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
329 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400330 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700331
Davide Pesaventof45fa212017-09-14 17:23:56 -0400332 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700333
Davide Pesaventof45fa212017-09-14 17:23:56 -0400334 auto cipherText = pKey.encrypt(plainText, sizeof(plainText));
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400335 auto decrypted = sKey.decrypt(cipherText->data(), cipherText->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());
338}
339
340BOOST_AUTO_TEST_CASE(UnsupportedEcDecryption)
341{
342 EcKeyTestData dataSet;
343
344 PrivateKey sKey;
345 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
346 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400347 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::EC);
Davide Pesaventof45fa212017-09-14 17:23:56 -0400348
349 OBufferStream os;
350 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
351
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400352 BOOST_CHECK_THROW(sKey.decrypt(os.buf()->data(), os.buf()->size()), PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700353}
354
Luca Keidel941fd8c2017-07-24 15:21:22 +0200355using KeyParams = boost::mpl::vector<RsaKeyParams, EcKeyParams>;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700356
Luca Keidel941fd8c2017-07-24 15:21:22 +0200357BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyParams)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700358{
Yingdi Yu202a2e92015-07-12 16:49:25 -0700359 unique_ptr<PrivateKey> sKey = generatePrivateKey(T());
360 PublicKey pKey;
361 ConstBufferPtr pKeyBits = sKey->derivePublicKey();
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400362 pKey.loadPkcs8(pKeyBits->data(), pKeyBits->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700363
Luca Keidel941fd8c2017-07-24 15:21:22 +0200364 const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yub263f152015-07-12 16:50:13 -0700365 OBufferStream os;
366 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
367 signerFilter(DigestAlgorithm::SHA256, *sKey) >>
368 streamSink(os));
369
370 ConstBufferPtr sig = os.buf();
371 bool result = false;
372 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400373 verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
Yingdi Yub263f152015-07-12 16:50:13 -0700374 boolSink(result));
Yingdi Yub263f152015-07-12 16:50:13 -0700375 BOOST_CHECK(result);
376
Yingdi Yu202a2e92015-07-12 16:49:25 -0700377 unique_ptr<PrivateKey> sKey2 = generatePrivateKey(T());
378
379 OBufferStream os1;
380 sKey->savePkcs1(os1);
381 ConstBufferPtr key1Pkcs1 = os1.buf();
382
383 OBufferStream os2;
384 sKey2->savePkcs1(os2);
385 ConstBufferPtr key2Pkcs1 = os2.buf();
386
387 BOOST_CHECK(*key1Pkcs1 != *key2Pkcs1);
388}
389
Davide Pesaventof45fa212017-09-14 17:23:56 -0400390BOOST_AUTO_TEST_CASE(UnsupportedKeyType)
391{
392 BOOST_CHECK_THROW(generatePrivateKey(AesKeyParams()), std::invalid_argument);
393}
394
Yingdi Yu202a2e92015-07-12 16:49:25 -0700395BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
396BOOST_AUTO_TEST_SUITE_END() // Transform
397BOOST_AUTO_TEST_SUITE_END() // Security
398
399} // namespace tests
400} // namespace transform
401} // namespace security
402} // namespace ndn