blob: 738067a5a578051826834f8937c62d2d4dfd471a [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/*
Junxiao Shifbb69902020-04-24 08:39:18 +00003 * Copyright (c) 2013-2020 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"
Junxiao Shifbb69902020-04-24 08:39:18 +000034#include "ndn-cxx/util/string-helper.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070035
Davide Pesavento7e780642018-11-24 15:51:34 -050036#include "tests/boost-test.hpp"
37
Luca Keidel941fd8c2017-07-24 15:21:22 +020038#include <boost/mpl/vector.hpp>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040039
40#include <sstream>
Yingdi Yu202a2e92015-07-12 16:49:25 -070041
42namespace ndn {
43namespace security {
44namespace transform {
45namespace tests {
46
47BOOST_AUTO_TEST_SUITE(Security)
48BOOST_AUTO_TEST_SUITE(Transform)
49BOOST_AUTO_TEST_SUITE(TestPrivateKey)
50
Davide Pesavento1bac1112019-06-13 21:48:46 -040051BOOST_AUTO_TEST_CASE(Empty)
52{
53 // test invoking member functions on an empty (default-constructed) PrivateKey
54 PrivateKey sKey;
55 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::NONE);
56 BOOST_CHECK_EQUAL(sKey.getKeySize(), 0);
laqinfan56a812d2019-06-03 15:33:58 -050057 BOOST_CHECK_THROW(sKey.getKeyDigest(DigestAlgorithm::SHA256), PrivateKey::Error);
Davide Pesavento1bac1112019-06-13 21:48:46 -040058 BOOST_CHECK_THROW(sKey.derivePublicKey(), PrivateKey::Error);
59 const uint8_t theAnswer = 42;
60 BOOST_CHECK_THROW(sKey.decrypt(&theAnswer, sizeof(theAnswer)), PrivateKey::Error);
61 std::ostringstream os;
62 BOOST_CHECK_THROW(sKey.savePkcs1(os), PrivateKey::Error);
63 std::string passwd("password");
64 BOOST_CHECK_THROW(sKey.savePkcs8(os, passwd.data(), passwd.size()), PrivateKey::Error);
65}
66
laqinfan56a812d2019-06-03 15:33:58 -050067BOOST_AUTO_TEST_CASE(KeyDigest)
68{
69 const Buffer buf(16);
70 PrivateKey sKey;
71 sKey.loadRaw(KeyType::HMAC, buf.data(), buf.size());
72 auto digest = sKey.getKeyDigest(DigestAlgorithm::SHA256);
73
74 const uint8_t expected[] = {
75 0x37, 0x47, 0x08, 0xff, 0xf7, 0x71, 0x9d, 0xd5, 0x97, 0x9e, 0xc8, 0x75, 0xd5, 0x6c, 0xd2, 0x28,
76 0x6f, 0x6d, 0x3c, 0xf7, 0xec, 0x31, 0x7a, 0x3b, 0x25, 0x63, 0x2a, 0xab, 0x28, 0xec, 0x37, 0xbb,
77 };
78 BOOST_CHECK_EQUAL_COLLECTIONS(digest->begin(), digest->end(),
79 expected, expected + sizeof(expected));
80}
81
laqinfan48f97242019-06-03 15:33:58 -050082BOOST_AUTO_TEST_CASE(LoadRaw)
83{
84 const Buffer buf(32);
85 PrivateKey sKey;
86 sKey.loadRaw(KeyType::HMAC, buf.data(), buf.size());
87 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::HMAC);
88 BOOST_CHECK_EQUAL(sKey.getKeySize(), 256);
89
90 PrivateKey sKey2;
91 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::NONE, buf.data(), buf.size()), std::invalid_argument);
92 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::RSA, buf.data(), buf.size()), std::invalid_argument);
93 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::EC, buf.data(), buf.size()), std::invalid_argument);
94 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::AES, buf.data(), buf.size()), std::invalid_argument);
95}
96
Davide Pesaventof45fa212017-09-14 17:23:56 -040097struct RsaKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070098{
Davide Pesavento1bac1112019-06-13 21:48:46 -040099 const size_t keySize = 2048;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400100 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700101 "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
102 "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n"
103 "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n"
104 "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n"
105 "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n"
106 "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n"
107 "W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr\n"
108 "cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV\n"
109 "Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b\n"
110 "wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg\n"
111 "Xb4gfR8zUXaXS1sXcnVm3+24vIs9dApUwykuoyjOqxWqcHRec2QT2FxVGkFEraze\n"
112 "CPa4rMECgYEA5Y8CywomIcTgerFGFCeMHJr8nQGqY2V/owFb3k9maczPnC9p4a9R\n"
113 "c5szLxA9FMYFxurQZMBWSEG2JS1HR2mnjigx8UKjYML/A+rvvjZOMe4M6Sy2ggh4\n"
114 "SkLZKpWTzjTe07ByM/j5v/SjNZhWAG7sw4/LmPGRQkwJv+KZhGojuOkCgYEA2cOF\n"
115 "T6cJRv6kvzTz9S0COZOVm+euJh/BXp7oAsAmbNfOpckPMzqHXy8/wpdKl6AAcB57\n"
116 "OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA\n"
117 "Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e\n"
118 "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n"
119 "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n"
120 "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n"
121 "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n"
122 "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n"
123 "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n"
124 "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n"
125 "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400126 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700127 "MIIFCzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIOKYJXvB6p8kCAggA\n"
128 "MBEGBSsOAwIHBAiQgMK8kQXTyASCBMjeNiKYYw5/yHgs9BfSGrpqvV0LkkgMQNUW\n"
129 "R4ZY8fuNjZynd+PxDuw2pyrv1Yv3jc+tupwUehZEzYOnGd53wQAuLO+Z0TBgRFN7\n"
130 "Lhk+AxlT7hu0xaB3ZpJ/uvWpgEJHsq/aB/GYgyzXcQo2AiqzERVpMCWJVmE1L977\n"
131 "CHwJmLm5mxclVLYp1UK5lkIBFu/M4nPavmNmYNUU1LOrXRo56TlJ2kUp8gQyQI1P\n"
132 "VPxi4chmlsr/OnQ2d1eZN+euFm0CS+yP+LFgI9ZqdyH1w+J43SXdHDzauVcZp7oa\n"
133 "Kw24OrhHfolLAnQIECXEJYeT7tZmhC4O9V6B18PFVyxWnEU4eFNpFE8kYSmm8Um2\n"
134 "buvDKI71q43hm23moYT9uIM1f4M8UkoOliJGrlf4xgEcmDuokEX01PdOq1gc4nvG\n"
135 "0DCwDI9cOsyn8cxhk9UVtFgzuG/seuznxIv1F5H0hzYOyloStXxRisJES0kgByBt\n"
136 "FFTfyoFKRrmCjRIygwVKUSkSDR0DlQS5ZLvQyIswnSQFwxAHqfvoSL4dB9UAIAQ+\n"
137 "ALVF1maaHgptbL6Ifqf0GFCv0hdNCVNDNCdy8R+S6nEYE+YdYSIdT1L88KD5PjU3\n"
138 "YY/CMnxhTncMaT4acPO1UUYuSGRZ/JL6E0ihoqIU+bqUgLSHNzhPySPfN9uqN61Y\n"
139 "HFBtxeEPWKU0f/JPkRBMmZdMI1/OVmA3QHSRBydI+CQN8no2gZRFoVbHTkG8IMpE\n"
140 "1fiDJpwFkpzIv/JPiTSE7DeBH5NJk1bgu7TcuZfa4unyAqss0UuLnXzS06TppkUj\n"
141 "QGft0g8VPW56eli6B4xrSzzuvAdbrxsVfxdmtHPyYxLb3/UG1g4x/H/yULhx7x9P\n"
142 "iI6cw6JUE+8bwJV2ZIlHXXHO+wUp/gCFJ6MHo9wkR1QvnHP2ClJAzBm9OvYnUx2Y\n"
143 "SX0HxEowW8BkhxOF184LEmxeua0yyZUqCdrYmErp7x9EY/LhD1zBwH8OGRa0qzmR\n"
144 "VKxAPKihkb9OgxcUKbvKePx3k2cQ7fbCUspGPm4Kn1zwMgRAZ4fz/o8Lnwc8MSY3\n"
145 "lPWnmLTFu420SRH2g9N0o/r195hiZ5cc+KfF4pwZWKbEbKFk/UfXA9vmOi7BBtDJ\n"
146 "RWshOINhzMU6Ij3KuaEpHni1HoHjw0SQ97ow2x/aB8k2QC28tbsa49lD2KKJku6b\n"
147 "2Or89adwFKqMgS2IXfXMXs/iG5EFLYN6r8e40Dn5f1vJfRLJl03XByIfT2n92pw3\n"
148 "fP7muOIKLUsEKjOrmn94NwMlfeW13oQHEH2KjPOWFS/tyJHDdVU+of4COH5yg59a\n"
149 "TZqFkOTGeliE1O+6sfF9fRuVxFUF3D8Hpr0JIjdc6+3RgIlGsXc8BwiSjDSI2XW+\n"
150 "vo75/2zPU9t8OeXEIJk2CQGyqLwUJ6dyi/yDRrvZAgjrUvbpcxydnBAHrLbLUGXJ\n"
151 "aEHH2tjEtnTqVyTchr1yHoupcFOCkA0dAA66XqwcssQxJiMGrWTpCbgd9mrTXQaZ\n"
152 "U7afFN1jpO78tgBQUUpImXdHLLsqdN5tefqjileZGZ9x3/C6TNAfDwYJdsicNNn5\n"
153 "y+JVsbltfLWlJxb9teb3dtQiFlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400154 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700155 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
156 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
157 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
158 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
159 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
160 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
161 "ywIDAQAB\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700162};
163
Junxiao Shifbb69902020-04-24 08:39:18 +0000164struct EcKeySpecificCurveTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -0700165{
Junxiao Shifbb69902020-04-24 08:39:18 +0000166 // EC keys are generated in named curve format only. However, old keys in specific curve format
167 // are still accepted. See https://redmine.named-data.net/issues/5037
168
Davide Pesavento1bac1112019-06-13 21:48:46 -0400169 const size_t keySize = 256;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400170 const std::string privateKeyPkcs1 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700171 "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
172 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
173 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
174 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
175 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
176 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
177 "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n"
178 "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400179 const std::string privateKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700180 "MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA\n"
181 "MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA\n"
182 "MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO\n"
183 "SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594\n"
184 "ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr\n"
185 "akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd\n"
186 "2OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN\n"
187 "6cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG\n"
188 "HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk\n"
189 "q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=\n";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400190 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -0700191 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
192 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
193 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
194 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
195 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
196 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
197 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700198};
199
Junxiao Shifbb69902020-04-24 08:39:18 +0000200struct EcKeyNamedCurveTestData
201{
202 // openssl ecparam -name secp384r1 -genkey -out pvt1.pem
203 // openssl pkcs8 -topk8 -passout pass:password -in pvt1.pem -out pvt8.pem
204 // openssl ec -in pvt1.pem -pubout -out pub8.pem
205
206 const size_t keySize = 384;
207 const std::string privateKeyPkcs1 =
208 "MIGkAgEBBDCUsb7NymksCkQAjdLMjUilWhOEyeYmGi79sX1RbsmfnoF/8SesKBhO\n"
209 "or+TZ8g8/8igBwYFK4EEACKhZANiAARWGplLOGdQiXRFQcd0VLPeTt0zXEj5zvSv\n"
210 "aHx9MrzBy57wgz10wTAiR561wuLtFAYxmqL9Ikrzx/BaEg0+v2zQ05NCzMNN8v2c\n"
211 "7/FzOhD7fmZrlJsT6Q2aHGExW0Rj3GE=\n";
212 const std::string privateKeyPkcs8 =
213 "MIHgMBsGCSqGSIb3DQEFAzAOBAjniUjwJfWsMQICCAAEgcCakGTKa49csaPpmtzi\n"
214 "5sTJw+AH8ajUqcDbtp2pJP/Ni6M1p9fai9hOKPElf9uJuYh/S80FAU6WQmZBAxL4\n"
215 "bF598ncLPogpGvz21wLuSc1xnbD829zAsMmh0XZvMZpBWX2g0NnZJx7GOraskTSh\n"
216 "7qGu70B+uKzw+JxIzgBEeMcoBUg8mTEht5zghfLGYkQp6BpTPdpU64udpPAKzFNs\n"
217 "5X+BzBnT5Yy49/Lp4uYIji8qwJFF3VqTn8RFKunFYDDejRU=\n";
218 const std::string publicKeyPkcs8 =
219 "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEVhqZSzhnUIl0RUHHdFSz3k7dM1xI+c70\n"
220 "r2h8fTK8wcue8IM9dMEwIkeetcLi7RQGMZqi/SJK88fwWhINPr9s0NOTQszDTfL9\n"
221 "nO/xczoQ+35ma5SbE+kNmhxhMVtEY9xh\n";
222};
223
224using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeySpecificCurveTestData, EcKeyNamedCurveTestData>;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700225
Luca Keidel941fd8c2017-07-24 15:21:22 +0200226static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700227checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
228{
229 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500230 sKey.loadPkcs8(encoding->data(), encoding->size(), password.data(), password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700231 OBufferStream os;
Luca Keidel941fd8c2017-07-24 15:21:22 +0200232 sKey.savePkcs1(os);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200233 BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400234 os.buf()->begin(), os.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700235}
236
Luca Keidel941fd8c2017-07-24 15:21:22 +0200237static void
Yingdi Yu202a2e92015-07-12 16:49:25 -0700238checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
239{
240 OBufferStream os;
241 bufferSource(*encoding) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700242 checkPkcs8Encoding(os.buf(), password, pkcs1);
243}
244
245BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
246{
247 T dataSet;
248
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500249 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700250 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700251 OBufferStream os;
252 bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os);
253 ConstBufferPtr sKeyPkcs1Buf = os.buf();
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400254 const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->data();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700255 size_t sKeyPkcs1Len = sKeyPkcs1Buf->size();
256
Luca Keidel941fd8c2017-07-24 15:21:22 +0200257 // load key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700258 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400259 BOOST_CHECK_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400260 BOOST_CHECK_EQUAL(sKey.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700261
262 std::stringstream ss2(dataSet.privateKeyPkcs1);
263 PrivateKey sKey2;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400264 BOOST_CHECK_NO_THROW(sKey2.loadPkcs1Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700265
266 // load key in pkcs1 format
267 PrivateKey sKey3;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400268 BOOST_CHECK_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400269 BOOST_CHECK_EQUAL(sKey3.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700270
271 std::stringstream ss4;
272 ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
273 PrivateKey sKey4;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400274 BOOST_CHECK_NO_THROW(sKey4.loadPkcs1(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700275
Luca Keidel941fd8c2017-07-24 15:21:22 +0200276 // save key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700277 OBufferStream os2;
278 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700279 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400280 os2.buf()->begin(), os2.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700281
282 // save key in pkcs1 format
283 OBufferStream os3;
284 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700285 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len,
Davide Pesaventof45fa212017-09-14 17:23:56 -0400286 os3.buf()->begin(), os3.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700287
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500288 const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700289 size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700290 OBufferStream os4;
291 bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400292 const uint8_t* sKeyPkcs8 = os4.buf()->data();
Davide Pesaventof45fa212017-09-14 17:23:56 -0400293 size_t sKeyPkcs8Len = os4.buf()->size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700294
295 std::string password("password");
296 std::string wrongpw("wrongpw");
Luca Keidel941fd8c2017-07-24 15:21:22 +0200297 auto pwCallback = [&password] (char* buf, size_t size, int) -> int {
298 BOOST_REQUIRE_LE(password.size(), size);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700299 std::copy(password.begin(), password.end(), buf);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200300 return static_cast<int>(password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700301 };
302
Luca Keidel941fd8c2017-07-24 15:21:22 +0200303 // load key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700304 PrivateKey sKey5;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400305 BOOST_CHECK_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500306 password.data(), password.size()));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400307 BOOST_CHECK_EQUAL(sKey5.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700308
309 PrivateKey sKey6;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400310 BOOST_CHECK_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700311
312 std::stringstream ss7(dataSet.privateKeyPkcs8);
313 PrivateKey sKey7;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500314 BOOST_CHECK_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700315
316 std::stringstream ss8(dataSet.privateKeyPkcs8);
317 PrivateKey sKey8;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400318 BOOST_CHECK_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700319
320 // load key in pkcs8 format
321 PrivateKey sKey9;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500322 BOOST_CHECK_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, password.data(), password.size()));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400323 BOOST_CHECK_EQUAL(sKey9.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700324
325 PrivateKey sKey10;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400326 BOOST_CHECK_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700327
328 std::stringstream ss11;
329 ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
330 PrivateKey sKey11;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500331 BOOST_CHECK_NO_THROW(sKey11.loadPkcs8(ss11, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700332
333 std::stringstream ss12;
334 ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
335 PrivateKey sKey12;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400336 BOOST_CHECK_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700337
Davide Pesaventof45fa212017-09-14 17:23:56 -0400338 // load key using wrong password, Error is expected
Yingdi Yu202a2e92015-07-12 16:49:25 -0700339 PrivateKey sKey13;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500340 BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.data(), wrongpw.size()),
Luca Keidel941fd8c2017-07-24 15:21:22 +0200341 PrivateKey::Error);
Davide Pesavento1bac1112019-06-13 21:48:46 -0400342 BOOST_CHECK_EQUAL(sKey13.getKeyType(), KeyType::NONE);
343 BOOST_CHECK_EQUAL(sKey13.getKeySize(), 0);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700344
Luca Keidel941fd8c2017-07-24 15:21:22 +0200345 // save key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700346 OBufferStream os14;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500347 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.data(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400348 checkPkcs8Base64Encoding(os14.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700349
350 OBufferStream os15;
351 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400352 checkPkcs8Base64Encoding(os15.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700353
354 // save key in pkcs8 format
355 OBufferStream os16;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500356 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.data(), password.size()));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400357 checkPkcs8Encoding(os16.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700358
359 OBufferStream os17;
360 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
Davide Pesaventof45fa212017-09-14 17:23:56 -0400361 checkPkcs8Encoding(os17.buf(), password, sKeyPkcs1Buf);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700362}
363
364BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
365{
366 T dataSet;
367
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500368 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700369 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700370 PrivateKey sKey;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400371 sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700372
373 // derive public key and compare
Davide Pesaventof45fa212017-09-14 17:23:56 -0400374 ConstBufferPtr pKeyBits = sKey.derivePublicKey();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700375 OBufferStream os;
376 bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os);
377 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
378 os.buf()->begin(), os.buf()->end());
379}
380
381BOOST_AUTO_TEST_CASE(RsaDecryption)
382{
383 RsaKeyTestData dataSet;
384
385 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500386 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700387 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400388 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700389
Davide Pesaventof45fa212017-09-14 17:23:56 -0400390 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700391
392 const std::string cipherTextBase64 =
393 "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
394 "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
395 "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
396 "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
397 "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
398 "PmTiSdeDQXZ94U5boDQ4Aw==\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700399 OBufferStream os;
400 bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os);
401
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400402 auto decrypted = sKey.decrypt(os.buf()->data(), os.buf()->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700403 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400404 decrypted->begin(), decrypted->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700405}
406
Davide Pesaventof45fa212017-09-14 17:23:56 -0400407BOOST_AUTO_TEST_CASE(RsaEncryptDecrypt)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700408{
409 RsaKeyTestData dataSet;
410
411 PublicKey pKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500412 pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700413 dataSet.publicKeyPkcs8.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400414 BOOST_CHECK_EQUAL(pKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700415
416 PrivateKey sKey;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500417 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
Yingdi Yu202a2e92015-07-12 16:49:25 -0700418 dataSet.privateKeyPkcs1.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400419 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700420
Davide Pesaventof45fa212017-09-14 17:23:56 -0400421 const uint8_t plainText[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700422
Davide Pesaventof45fa212017-09-14 17:23:56 -0400423 auto cipherText = pKey.encrypt(plainText, sizeof(plainText));
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400424 auto decrypted = sKey.decrypt(cipherText->data(), cipherText->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700425 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400426 decrypted->begin(), decrypted->end());
427}
428
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500429BOOST_AUTO_TEST_CASE(UnsupportedDecryption)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400430{
Davide Pesaventof45fa212017-09-14 17:23:56 -0400431 OBufferStream os;
432 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
433
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500434 auto ecKey = generatePrivateKey(EcKeyParams());
435 BOOST_CHECK_THROW(ecKey->decrypt(os.buf()->data(), os.buf()->size()), PrivateKey::Error);
436
437 auto hmacKey = generatePrivateKey(HmacKeyParams());
438 BOOST_CHECK_THROW(hmacKey->decrypt(os.buf()->data(), os.buf()->size()), PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700439}
440
Junxiao Shifbb69902020-04-24 08:39:18 +0000441class RsaKeyGenParams
Yingdi Yu202a2e92015-07-12 16:49:25 -0700442{
Junxiao Shifbb69902020-04-24 08:39:18 +0000443public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500444 using Params = RsaKeyParams;
445 using hasPublicKey = std::true_type;
446 using canSavePkcs1 = std::true_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000447
448 static void
449 checkPublicKey(const Buffer& bits)
450 {
451 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500452};
453
Junxiao Shifbb69902020-04-24 08:39:18 +0000454class EcKeyGenParams
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500455{
Junxiao Shifbb69902020-04-24 08:39:18 +0000456public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500457 using Params = EcKeyParams;
458 using hasPublicKey = std::true_type;
459 using canSavePkcs1 = std::true_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000460
461 static void
462 checkPublicKey(const Buffer& bits)
463 {
464 // EC key generation should use named curve format. See https://redmine.named-data.net/issues/5037
465 // OBJECT IDENTIFIER 1.2.840.10045.3.1.7 prime256v1 (ANSI X9.62 named elliptic curve)
466 const uint8_t oid[] = {0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07};
467 BOOST_CHECK_MESSAGE(std::search(bits.begin(), bits.end(), oid, oid + sizeof(oid)) != bits.end(),
468 "OID not found in " << toHex(bits));
469 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500470};
471
Junxiao Shifbb69902020-04-24 08:39:18 +0000472class HmacKeyGenParams
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500473{
Junxiao Shifbb69902020-04-24 08:39:18 +0000474public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500475 using Params = HmacKeyParams;
476 using hasPublicKey = std::false_type;
477 using canSavePkcs1 = std::false_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000478
479 static void
480 checkPublicKey(const Buffer& bits)
481 {
482 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500483};
484
485using KeyGenParams = boost::mpl::vector<RsaKeyGenParams,
486 EcKeyGenParams,
487 HmacKeyGenParams>;
488
489BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyGenParams)
490{
Davide Pesavento1bac1112019-06-13 21:48:46 -0400491 typename T::Params params;
492 auto sKey = generatePrivateKey(params);
493 BOOST_CHECK_EQUAL(sKey->getKeyType(), params.getKeyType());
494 BOOST_CHECK_EQUAL(sKey->getKeySize(), params.getKeySize());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700495
Luca Keidel941fd8c2017-07-24 15:21:22 +0200496 const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yub263f152015-07-12 16:50:13 -0700497 OBufferStream os;
498 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
499 signerFilter(DigestAlgorithm::SHA256, *sKey) >>
500 streamSink(os));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500501 auto sig = os.buf();
Yingdi Yub263f152015-07-12 16:50:13 -0700502
Yingdi Yub263f152015-07-12 16:50:13 -0700503 bool result = false;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500504 if (typename T::hasPublicKey()) {
505 auto pKeyBits = sKey->derivePublicKey();
Junxiao Shifbb69902020-04-24 08:39:18 +0000506 BOOST_REQUIRE(pKeyBits != nullptr);
507 T::checkPublicKey(*pKeyBits);
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500508 PublicKey pKey;
509 pKey.loadPkcs8(pKeyBits->data(), pKeyBits->size());
510 BOOST_CHECK_NO_THROW(bufferSource(data, sizeof(data)) >>
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400511 verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
Yingdi Yub263f152015-07-12 16:50:13 -0700512 boolSink(result));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500513 }
514 else {
515#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
516 BOOST_CHECK_THROW(sKey->derivePublicKey(), PrivateKey::Error);
517#endif
518 BOOST_CHECK_NO_THROW(bufferSource(data, sizeof(data)) >>
519 verifierFilter(DigestAlgorithm::SHA256, *sKey, sig->data(), sig->size()) >>
520 boolSink(result));
521 }
Yingdi Yub263f152015-07-12 16:50:13 -0700522 BOOST_CHECK(result);
523
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500524 if (typename T::canSavePkcs1()) {
Davide Pesavento1bac1112019-06-13 21:48:46 -0400525 auto sKey2 = generatePrivateKey(params);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700526
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500527 OBufferStream os1;
528 sKey->savePkcs1(os1);
529 OBufferStream os2;
530 sKey2->savePkcs1(os2);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700531
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500532 BOOST_CHECK(*os1.buf() != *os2.buf());
533 }
534 else {
535#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
536 OBufferStream os1;
537 BOOST_CHECK_THROW(sKey->savePkcs1(os1), PrivateKey::Error);
538#endif
539 }
Yingdi Yu202a2e92015-07-12 16:49:25 -0700540}
541
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500542BOOST_AUTO_TEST_CASE(GenerateKeyUnsupportedType)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400543{
544 BOOST_CHECK_THROW(generatePrivateKey(AesKeyParams()), std::invalid_argument);
545}
546
Yingdi Yu202a2e92015-07-12 16:49:25 -0700547BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
548BOOST_AUTO_TEST_SUITE_END() // Transform
549BOOST_AUTO_TEST_SUITE_END() // Security
550
551} // namespace tests
552} // namespace transform
553} // namespace security
554} // namespace ndn