blob: 4be078bb367636bd7c9c3831015c2ffd6ac7f05d [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 Pesavento35c63792022-01-17 02:06:03 -05003 * Copyright (c) 2013-2022 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);
Davide Pesavento765abc92021-12-27 00:44:04 -050059 BOOST_CHECK_THROW(sKey.decrypt({}), PrivateKey::Error);
Davide Pesavento1bac1112019-06-13 21:48:46 -040060 std::ostringstream os;
61 BOOST_CHECK_THROW(sKey.savePkcs1(os), PrivateKey::Error);
62 std::string passwd("password");
63 BOOST_CHECK_THROW(sKey.savePkcs8(os, passwd.data(), passwd.size()), PrivateKey::Error);
64}
65
Davide Pesavento94dfcf12021-09-26 14:18:45 -040066#if OPENSSL_VERSION_NUMBER < 0x30000000L // FIXME #5154
laqinfan56a812d2019-06-03 15:33:58 -050067BOOST_AUTO_TEST_CASE(KeyDigest)
68{
69 const Buffer buf(16);
70 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -050071 sKey.loadRaw(KeyType::HMAC, buf);
laqinfan56a812d2019-06-03 15:33:58 -050072 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}
Davide Pesavento94dfcf12021-09-26 14:18:45 -040081#endif
laqinfan56a812d2019-06-03 15:33:58 -050082
laqinfan48f97242019-06-03 15:33:58 -050083BOOST_AUTO_TEST_CASE(LoadRaw)
84{
85 const Buffer buf(32);
86 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -050087 sKey.loadRaw(KeyType::HMAC, buf);
Davide Pesavento94dfcf12021-09-26 14:18:45 -040088#if OPENSSL_VERSION_NUMBER < 0x30000000L // FIXME #5154
laqinfan48f97242019-06-03 15:33:58 -050089 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::HMAC);
90 BOOST_CHECK_EQUAL(sKey.getKeySize(), 256);
Davide Pesavento94dfcf12021-09-26 14:18:45 -040091#endif
laqinfan48f97242019-06-03 15:33:58 -050092
93 PrivateKey sKey2;
Davide Pesavento765abc92021-12-27 00:44:04 -050094 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::NONE, buf), std::invalid_argument);
95 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::RSA, buf), std::invalid_argument);
96 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::EC, buf), std::invalid_argument);
97 BOOST_CHECK_THROW(sKey2.loadRaw(KeyType::AES, buf), std::invalid_argument);
laqinfan48f97242019-06-03 15:33:58 -050098}
99
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400100struct RsaKey_DesEncrypted
Yingdi Yu202a2e92015-07-12 16:49:25 -0700101{
Davide Pesavento1bac1112019-06-13 21:48:46 -0400102 const size_t keySize = 2048;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400103 const std::string privateKeyPkcs1 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400104R"PEM(MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT
105sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H
106GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73
107c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh
108OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU
109RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn
110W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr
111cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV
112Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b
113wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg
114Xb4gfR8zUXaXS1sXcnVm3+24vIs9dApUwykuoyjOqxWqcHRec2QT2FxVGkFEraze
115CPa4rMECgYEA5Y8CywomIcTgerFGFCeMHJr8nQGqY2V/owFb3k9maczPnC9p4a9R
116c5szLxA9FMYFxurQZMBWSEG2JS1HR2mnjigx8UKjYML/A+rvvjZOMe4M6Sy2ggh4
117SkLZKpWTzjTe07ByM/j5v/SjNZhWAG7sw4/LmPGRQkwJv+KZhGojuOkCgYEA2cOF
118T6cJRv6kvzTz9S0COZOVm+euJh/BXp7oAsAmbNfOpckPMzqHXy8/wpdKl6AAcB57
119OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA
120Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e
121vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX
122ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS
123HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd
124Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO
125ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR
126cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd
127lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk
128jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==
129)PEM";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400130 const std::string privateKeyPkcs8 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400131R"PEM(MIIFCzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIOKYJXvB6p8kCAggA
132MBEGBSsOAwIHBAiQgMK8kQXTyASCBMjeNiKYYw5/yHgs9BfSGrpqvV0LkkgMQNUW
133R4ZY8fuN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9N0o/r195hiZ5cc+KfF4pwZWKbEbKFk/UfXA9vmOi7BBtDJ
150RWshOINhzMU6Ij3KuaEpHni1HoHjw0SQ97ow2x/aB8k2QC28tbsa49lD2KKJku6b
1512Or89adwFKqMgS2IXfXMXs/iG5EFLYN6r8e40Dn5f1vJfRLJl03XByIfT2n92pw3
152fP7muOIKLUsEKjOrmn94NwMlfeW13oQHEH2KjPOWFS/tyJHDdVU+of4COH5yg59a
153TZqFkOTGeliE1O+6sfF9fRuVxFUF3D8Hpr0JIjdc6+3RgIlGsXc8BwiSjDSI2XW+
154vo75/2zPU9t8OeXEIJk2CQGyqLwUJ6dyi/yDRrvZAgjrUvbpcxydnBAHrLbLUGXJ
155aEHH2tjEtnTqVyTchr1yHoupcFOCkA0dAA66XqwcssQxJiMGrWTpCbgd9mrTXQaZ
156U7afFN1jpO78tgBQUUpImXdHLLsqdN5tefqjileZGZ9x3/C6TNAfDwYJdsicNNn5
157y+JVsbltfLWlJxb9teb3dtQiFlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=
158)PEM";
159 const std::string publicKey =
160R"PEM(MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg
161WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw
162s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho
163pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g
164ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA
1659rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0
166ywIDAQAB
167)PEM";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700168};
169
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400170struct RsaKey_Aes256Encrypted
Yingdi Yu202a2e92015-07-12 16:49:25 -0700171{
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400172 // Generated with OpenSSL 3.0.0
173 // openssl genpkey -quiet -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -outform PEM -out key.pem
174 // openssl pkcs8 -in key.pem -nocrypt -traditional -outform PEM -out pvt1.pem
175 // openssl pkcs8 -in key.pem -topk8 -v2 aes256 -passout pass:password -outform PEM -out pvt8.pem
176 // openssl pkey -in key.pem -pubout -outform PEM -out pub.pem
177
178 const size_t keySize = 2048;
179 const std::string privateKeyPkcs1 =
180R"PEM(MIIEowIBAAKCAQEAzEKtiRrzvhwGDMpsUlKMd7Veo9+lvLpttdX/ETo2exU36HqV
1816zSM7LnpfqId7zug+l+VGwPd+oJOzROSCAoUcP3C3m9CkfqT97H9g2zdfwXqJnMQ
182tnpAGngXOTF6RSBDVqo+jfeRw3c+MCXDZ0cHd49uv+CnWks6Sj+gowppdsQfvQRr
1832incnwqht3JuaiTytxNrk6DjpoNTHyZJ53SY6DibGSk6aDFWMJcfvTZ6EuUbcepP
1847HRwABn4Ctj1FTx3hDPe4sIrBFrbfP7yDQ9NxYh1baxXcCjWfCS43qUjrtcQFVkK
185vE3uucmw1rf0C0Wy7dDvF2n1v+WLAEPSgJnumwIDAQABAoIBAEdNgWGKkIqNIsmF
186QhHssg85t2tSL3t1wsWGic8cOJd3vTgAzuO3yPf8IBeuBPAVqyirhBPVokAIC/UH
187v2LiDeexlbxrL1xhEhUVw48Eyj9Es8uvQCbK/ySeRlEXRfzqecc/j62kPfRzZDiP
188fipHv8ILRlhh1lmtSBBSLMOtZ0pnJUmrNIOaFlJa56t0G3a+4C1u2swOtoCOWtyR
189o7NAHiCcZNbnFQQSLyfDj3Sqnsaxke41A+gtrSeeUHkqnP0rxdmqChdyU36Yszi2
190nB+yUXJT7Iw5GD+/vVUoby1/FVCRbh8zNfXvozC3llUSmsn3CBzVLoI+uwD5/zSz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1XByu3jlQPblcEeJ33iRcQDqh96GgMnJstsGuUjBREVjbNZ30YZYeVg
226+L/h9otyO92Nq0knADHeixy9JZ2ATRMoTAT4XydSN9lHOh+K5pC3kf9pke2NCJyr
227r/w9VQnQiIkxZ+THFshHHJxZZLlLwYWWr894rKftyZB+nDidNZfXr6K8VWoCAwh4
228XwV6109KeHx86Kim9KQPn5RYVFdoxt2dO5O9vcXWtcNciIEi4MPyUM+oHwWrKTLX
229EQsVu5gaz8vFt3lg0+Ctf8vAIA1/FxXEZ/BTAiV96lvRcUbXU2AdL+rNU2rtpqxH
230h9L3r6IEN9WVIkNH5Q4CqSS9z+BxIPj76dA1MXK1nb8OJ9Y+BMPhktMz9M+KcVpD
2311rXebv+/9Tw0lKLcNL50AglJOP+IRL0hHh62xtXs+FP5KocBs1kVAJORrS6wiy22
232DGwcVmgTvWsPHzIf3IEa7DEjhClnU6alxKxCEmzPtm29upYXL9o3y7CMwidvCYpD
233zFU2pIKA85YpjUYL25MiCyJPODc3VoW9l+rBqPu6bVWZXsfgsD4lcdFaJbPqcVby
234PEe2dEUjhwpYgjbjOO7geMe+VeShipZJ63yFsNaPWzcz
235)PEM";
236 const std::string publicKey =
237R"PEM(MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzEKtiRrzvhwGDMpsUlKM
238d7Veo9+lvLpttdX/ETo2exU36HqV6zSM7LnpfqId7zug+l+VGwPd+oJOzROSCAoU
239cP3C3m9CkfqT97H9g2zdfwXqJnMQtnpAGngXOTF6RSBDVqo+jfeRw3c+MCXDZ0cH
240d49uv+CnWks6Sj+gowppdsQfvQRr2incnwqht3JuaiTytxNrk6DjpoNTHyZJ53SY
2416DibGSk6aDFWMJcfvTZ6EuUbcepP7HRwABn4Ctj1FTx3hDPe4sIrBFrbfP7yDQ9N
242xYh1baxXcCjWfCS43qUjrtcQFVkKvE3uucmw1rf0C0Wy7dDvF2n1v+WLAEPSgJnu
243mwIDAQAB
244)PEM";
245};
246
247struct EcKeySpecifiedCurve_DesEncrypted
248{
249 // EC keys are generated in "namedCurve" format only. However, old keys in "specifiedCurve"
250 // format are still accepted. See https://redmine.named-data.net/issues/5037
Junxiao Shifbb69902020-04-24 08:39:18 +0000251
Davide Pesavento1bac1112019-06-13 21:48:46 -0400252 const size_t keySize = 256;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400253 const std::string privateKeyPkcs1 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400254R"PEM(MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC
255AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////
256MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr
257vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE
258axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W
259K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8
260YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A
261ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==
262)PEM";
Davide Pesaventof45fa212017-09-14 17:23:56 -0400263 const std::string privateKeyPkcs8 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400264R"PEM(MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA
265MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA
266MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO
267SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594
268ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr
269akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd
2702OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN
2716cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG
272HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk
273q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=
274)PEM";
275 const std::string publicKey =
276R"PEM(MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
277AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
278///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
279NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
280RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
281//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc
2825EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=
283)PEM";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700284};
285
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400286struct EcKeyNamedCurve_DesEncrypted
Junxiao Shifbb69902020-04-24 08:39:18 +0000287{
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400288 // Generated with older (1.0.x?) OpenSSL
Junxiao Shifbb69902020-04-24 08:39:18 +0000289 // openssl ecparam -name secp384r1 -genkey -out pvt1.pem
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400290 // openssl pkcs8 -in pvt1.pem -topk8 -passout pass:password -outform PEM -out pvt8.pem
291 // openssl ec -in pvt1.pem -pubout -outform PEM -out pub.pem
Junxiao Shifbb69902020-04-24 08:39:18 +0000292
293 const size_t keySize = 384;
294 const std::string privateKeyPkcs1 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400295R"PEM(MIGkAgEBBDCUsb7NymksCkQAjdLMjUilWhOEyeYmGi79sX1RbsmfnoF/8SesKBhO
296or+TZ8g8/8igBwYFK4EEACKhZANiAARWGplLOGdQiXRFQcd0VLPeTt0zXEj5zvSv
297aHx9MrzBy57wgz10wTAiR561wuLtFAYxmqL9Ikrzx/BaEg0+v2zQ05NCzMNN8v2c
2987/FzOhD7fmZrlJsT6Q2aHGExW0Rj3GE=
299)PEM";
Junxiao Shifbb69902020-04-24 08:39:18 +0000300 const std::string privateKeyPkcs8 =
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400301R"PEM(MIHgMBsGCSqGSIb3DQEFAzAOBAjniUjwJfWsMQICCAAEgcCakGTKa49csaPpmtzi
3025sTJw+AH8ajUqcDbtp2pJP/Ni6M1p9fai9hOKPElf9uJuYh/S80FAU6WQmZBAxL4
303bF598ncLPogpGvz21wLuSc1xnbD829zAsMmh0XZvMZpBWX2g0NnZJx7GOraskTSh
3047qGu70B+uKzw+JxIzgBEeMcoBUg8mTEht5zghfLGYkQp6BpTPdpU64udpPAKzFNs
3055X+BzBnT5Yy49/Lp4uYIji8qwJFF3VqTn8RFKunFYDDejRU=
306)PEM";
307 const std::string publicKey =
308R"PEM(MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEVhqZSzhnUIl0RUHHdFSz3k7dM1xI+c70
309r2h8fTK8wcue8IM9dMEwIkeetcLi7RQGMZqi/SJK88fwWhINPr9s0NOTQszDTfL9
310nO/xczoQ+35ma5SbE+kNmhxhMVtEY9xh
311)PEM";
Junxiao Shifbb69902020-04-24 08:39:18 +0000312};
313
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400314struct EcKeyNamedCurve_Aes256Encrypted
315{
316 // Generated with OpenSSL 3.0.0
317 // openssl genpkey -quiet -algorithm EC -pkeyopt ec_paramgen_curve:P-384
318 // -pkeyopt ec_param_enc:named_curve -outform PEM -out key.pem
319 // openssl pkcs8 -in key.pem -nocrypt -traditional -outform PEM -out pvt1.pem
320 // openssl pkcs8 -in key.pem -topk8 -v2 aes256 -passout pass:password -outform PEM -out pvt8.pem
321 // openssl pkey -in key.pem -pubout -outform PEM -out pub.pem
322
323 const size_t keySize = 384;
324 const std::string privateKeyPkcs1 =
325R"PEM(MIGkAgEBBDAa5I6hfwcDZ8Hzn6/ZU3jV/Fp6jZP+tgUHSdMcfRS3iLOY296KdcXd
326xISVHU3g+XygBwYFK4EEACKhZANiAAQh3xGTxgMFXwoRNBCrnzawZnoQUojZ1Qe5
3273VrR+9ECawVf2sPklX+Lw1pE1TG5q+YepM8imRclRbXLBSGPF6f+qJCiFWR71XS2
328CX2kW/AZOGluxYrVMe22ns5TY2o50G8=
329)PEM";
330 const std::string privateKeyPkcs8 =
331R"PEM(MIIBHDBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIy4A7Il3JRNQCAggA
332MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBBPYOn/2qSDBlKhZ6+ckre8BIHA
333jXwlDh+OX+1VayCWohXUooxZPfgKmIVkxhNrQzn0DV/FW9WbiXC72vss75/o5O2H
334o4EuIOxnbxIM5HhsrdDY5g8Abkk5gKYt+eVJQn1EPAR+PU9T8OUKyQr9rO0Pvsu8
3359h5V9+WFHNnNBTN3pqIV5cU25oVX7tqeZKs+5I4QIaq4hzPj+k15dLFaaq5XejZH
336KjMKNdKbfz4TEs7u1W9YyrD3Z8/RgzLwVRjJqqOKeoJF4b1lOqOd5rWb4Oa06alr
337)PEM";
338 const std::string publicKey =
339R"PEM(MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEId8Rk8YDBV8KETQQq582sGZ6EFKI2dUH
340ud1a0fvRAmsFX9rD5JV/i8NaRNUxuavmHqTPIpkXJUW1ywUhjxen/qiQohVke9V0
341tgl9pFvwGThpbsWK1THttp7OU2NqOdBv
342)PEM";
343};
344
345struct EcKeyNamedCurve_Des3Encrypted
346{
347 // Generated with OpenSSL 3.0.0
348 // openssl genpkey -quiet -algorithm EC -pkeyopt ec_paramgen_curve:P-256
349 // -pkeyopt ec_param_enc:named_curve -outform PEM -out key.pem
350 // openssl pkcs8 -in key.pem -nocrypt -traditional -outform PEM -out pvt1.pem
351 // openssl pkcs8 -in key.pem -topk8 -v2 des3 -passout pass:password -outform PEM -out pvt8.pem
352 // openssl pkey -in key.pem -pubout -outform PEM -out pub.pem
353
354 const size_t keySize = 256;
355 const std::string privateKeyPkcs1 =
356R"PEM(MHcCAQEEIEbb/pnOVzkIZP4jrpZQCxl2OTJVQD7rHBJxxv8Aa215oAoGCCqGSM49
357AwEHoUQDQgAEvSFhYwmFe17QAj0xhumERNJFf5MTkAegdE+db4ojchAbcDLRcnzY
358+TUx5pTBqdBg+STMK7h36ZUn+KcMeizMRA==
359)PEM";
360 const std::string privateKeyPkcs8 =
361R"PEM(MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAj2rY09BeCZGgICCAAw
362DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI6CIHMAz7K7AEgZBzyr8QcH1OWEqc
363ByPy4Vlze9D0izD7/NIxUeauHILqXMjU6Z057oYZ14Nm+DpbyJWu16gjZ2N+M2GH
3644bH8gS6JUGhqtSbQ1Oo5lmjPXphefZp0tarv19Hp8S1VKmcVoj65kxfn5x0GKepg
365fhXlMB2uQHvNoujU9PyeTZhwjAPQ6vHgFonbPPaL/f256NqMmnA=
366)PEM";
367 const std::string publicKey =
368R"PEM(MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEvSFhYwmFe17QAj0xhumERNJFf5MT
369kAegdE+db4ojchAbcDLRcnzY+TUx5pTBqdBg+STMK7h36ZUn+KcMeizMRA==
370)PEM";
371};
372
373using KeyTestDataSets = boost::mpl::vector<
374#if OPENSSL_VERSION_NUMBER < 0x30000000L
375 // DES-encrypted keys
376 // .privateKeyPkcs8 uses either the PBES1 or PBES2 encryption scheme with DES-CBC-Pad (see RFC 8018)
377 // This is no longer supported out-of-the-box by OpenSSL 3.0 and later
378 RsaKey_DesEncrypted,
379 EcKeySpecifiedCurve_DesEncrypted,
380 EcKeyNamedCurve_DesEncrypted,
381#endif
382 // AES-encrypted keys
383 // .privateKeyPkcs8 uses the PBES2 encryption scheme with AES-CBC-Pad (see RFC 8018)
384 // This works with all supported versions of OpenSSL
385 RsaKey_Aes256Encrypted,
386 EcKeyNamedCurve_Aes256Encrypted,
387 // 3DES-encrypted keys
388 // .privateKeyPkcs8 uses the PBES2 encryption scheme with DES-EDE3-CBC-Pad (see RFC 8018)
389 // This works with all supported versions of OpenSSL
390 EcKeyNamedCurve_Des3Encrypted
391>;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700392
Luca Keidel941fd8c2017-07-24 15:21:22 +0200393static void
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400394checkPkcs8Encoding(const ConstBufferPtr& encoding,
395 const std::string& password,
396 const ConstBufferPtr& pkcs1)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700397{
398 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500399 sKey.loadPkcs8(*encoding, password.data(), password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700400 OBufferStream os;
Luca Keidel941fd8c2017-07-24 15:21:22 +0200401 sKey.savePkcs1(os);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200402 BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400403 os.buf()->begin(), os.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700404}
405
Luca Keidel941fd8c2017-07-24 15:21:22 +0200406static void
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400407checkPkcs8Base64Encoding(const ConstBufferPtr& encoding,
408 const std::string& password,
409 const ConstBufferPtr& pkcs1)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700410{
411 OBufferStream os;
412 bufferSource(*encoding) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700413 checkPkcs8Encoding(os.buf(), password, pkcs1);
414}
415
416BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
417{
418 T dataSet;
419
Davide Pesavento765abc92021-12-27 00:44:04 -0500420 auto sKeyPkcs1Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
421 dataSet.privateKeyPkcs1.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700422 OBufferStream os;
Davide Pesavento765abc92021-12-27 00:44:04 -0500423 bufferSource(sKeyPkcs1Base64) >> base64Decode() >> streamSink(os);
424 auto sKeyPkcs1 = os.buf();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700425
Luca Keidel941fd8c2017-07-24 15:21:22 +0200426 // load key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700427 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500428 BOOST_CHECK_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400429 BOOST_CHECK_EQUAL(sKey.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700430
431 std::stringstream ss2(dataSet.privateKeyPkcs1);
432 PrivateKey sKey2;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400433 BOOST_CHECK_NO_THROW(sKey2.loadPkcs1Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700434
435 // load key in pkcs1 format
436 PrivateKey sKey3;
Davide Pesavento765abc92021-12-27 00:44:04 -0500437 BOOST_CHECK_NO_THROW(sKey3.loadPkcs1(*sKeyPkcs1));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400438 BOOST_CHECK_EQUAL(sKey3.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700439
440 std::stringstream ss4;
Davide Pesavento765abc92021-12-27 00:44:04 -0500441 ss4.write(reinterpret_cast<const char*>(sKeyPkcs1->data()), sKeyPkcs1->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700442 PrivateKey sKey4;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400443 BOOST_CHECK_NO_THROW(sKey4.loadPkcs1(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700444
Luca Keidel941fd8c2017-07-24 15:21:22 +0200445 // save key in base64-encoded pkcs1 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700446 OBufferStream os2;
447 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
Davide Pesavento765abc92021-12-27 00:44:04 -0500448 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64.begin(), sKeyPkcs1Base64.end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400449 os2.buf()->begin(), os2.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700450
451 // save key in pkcs1 format
452 OBufferStream os3;
453 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
Davide Pesavento765abc92021-12-27 00:44:04 -0500454 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1->begin(), sKeyPkcs1->end(),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400455 os3.buf()->begin(), os3.buf()->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700456
Davide Pesavento765abc92021-12-27 00:44:04 -0500457 auto sKeyPkcs8Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.data()),
458 dataSet.privateKeyPkcs8.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700459 OBufferStream os4;
Davide Pesavento765abc92021-12-27 00:44:04 -0500460 bufferSource(sKeyPkcs8Base64) >> base64Decode() >> streamSink(os4);
461 auto sKeyPkcs8 = os4.buf();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700462
463 std::string password("password");
464 std::string wrongpw("wrongpw");
Luca Keidel941fd8c2017-07-24 15:21:22 +0200465 auto pwCallback = [&password] (char* buf, size_t size, int) -> int {
466 BOOST_REQUIRE_LE(password.size(), size);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700467 std::copy(password.begin(), password.end(), buf);
Luca Keidel941fd8c2017-07-24 15:21:22 +0200468 return static_cast<int>(password.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700469 };
470
Luca Keidel941fd8c2017-07-24 15:21:22 +0200471 // load key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700472 PrivateKey sKey5;
Davide Pesavento765abc92021-12-27 00:44:04 -0500473 BOOST_CHECK_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, password.data(), password.size()));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400474 BOOST_CHECK_EQUAL(sKey5.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700475
476 PrivateKey sKey6;
Davide Pesavento765abc92021-12-27 00:44:04 -0500477 BOOST_CHECK_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700478
479 std::stringstream ss7(dataSet.privateKeyPkcs8);
480 PrivateKey sKey7;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500481 BOOST_CHECK_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700482
483 std::stringstream ss8(dataSet.privateKeyPkcs8);
484 PrivateKey sKey8;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400485 BOOST_CHECK_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700486
487 // load key in pkcs8 format
488 PrivateKey sKey9;
Davide Pesavento765abc92021-12-27 00:44:04 -0500489 BOOST_CHECK_NO_THROW(sKey9.loadPkcs8(*sKeyPkcs8, password.data(), password.size()));
Davide Pesavento1bac1112019-06-13 21:48:46 -0400490 BOOST_CHECK_EQUAL(sKey9.getKeySize(), dataSet.keySize);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700491
492 PrivateKey sKey10;
Davide Pesavento765abc92021-12-27 00:44:04 -0500493 BOOST_CHECK_NO_THROW(sKey10.loadPkcs8(*sKeyPkcs8, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700494
495 std::stringstream ss11;
Davide Pesavento765abc92021-12-27 00:44:04 -0500496 ss11.write(reinterpret_cast<const char*>(sKeyPkcs8->data()), sKeyPkcs8->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700497 PrivateKey sKey11;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500498 BOOST_CHECK_NO_THROW(sKey11.loadPkcs8(ss11, password.data(), password.size()));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700499
500 std::stringstream ss12;
Davide Pesavento765abc92021-12-27 00:44:04 -0500501 ss12.write(reinterpret_cast<const char*>(sKeyPkcs8->data()), sKeyPkcs8->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700502 PrivateKey sKey12;
Davide Pesaventof45fa212017-09-14 17:23:56 -0400503 BOOST_CHECK_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700504
Davide Pesaventof45fa212017-09-14 17:23:56 -0400505 // load key using wrong password, Error is expected
Yingdi Yu202a2e92015-07-12 16:49:25 -0700506 PrivateKey sKey13;
Davide Pesavento765abc92021-12-27 00:44:04 -0500507 BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, wrongpw.data(), wrongpw.size()),
Luca Keidel941fd8c2017-07-24 15:21:22 +0200508 PrivateKey::Error);
Davide Pesavento1bac1112019-06-13 21:48:46 -0400509 BOOST_CHECK_EQUAL(sKey13.getKeyType(), KeyType::NONE);
510 BOOST_CHECK_EQUAL(sKey13.getKeySize(), 0);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700511
Luca Keidel941fd8c2017-07-24 15:21:22 +0200512 // save key in base64-encoded pkcs8 format
Yingdi Yu202a2e92015-07-12 16:49:25 -0700513 OBufferStream os14;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500514 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.data(), password.size()));
Davide Pesavento765abc92021-12-27 00:44:04 -0500515 checkPkcs8Base64Encoding(os14.buf(), password, sKeyPkcs1);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700516
517 OBufferStream os15;
518 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
Davide Pesavento765abc92021-12-27 00:44:04 -0500519 checkPkcs8Base64Encoding(os15.buf(), password, sKeyPkcs1);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700520
521 // save key in pkcs8 format
522 OBufferStream os16;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500523 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.data(), password.size()));
Davide Pesavento765abc92021-12-27 00:44:04 -0500524 checkPkcs8Encoding(os16.buf(), password, sKeyPkcs1);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700525
526 OBufferStream os17;
527 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
Davide Pesavento765abc92021-12-27 00:44:04 -0500528 checkPkcs8Encoding(os17.buf(), password, sKeyPkcs1);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700529}
530
531BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
532{
533 T dataSet;
534
Davide Pesavento765abc92021-12-27 00:44:04 -0500535 auto sKeyPkcs1Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
536 dataSet.privateKeyPkcs1.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700537 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500538 sKey.loadPkcs1Base64(sKeyPkcs1Base64);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700539
540 // derive public key and compare
Davide Pesaventof45fa212017-09-14 17:23:56 -0400541 ConstBufferPtr pKeyBits = sKey.derivePublicKey();
Yingdi Yu202a2e92015-07-12 16:49:25 -0700542 OBufferStream os;
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400543 bufferSource(dataSet.publicKey) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700544 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
545 os.buf()->begin(), os.buf()->end());
546}
547
548BOOST_AUTO_TEST_CASE(RsaDecryption)
549{
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400550 RsaKey_Aes256Encrypted dataSet;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700551
552 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500553 sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
554 dataSet.privateKeyPkcs1.size()});
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400555 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700556
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400557 const uint8_t plaintext[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700558
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400559 // Ciphertext generated with OpenSSL 3.0.0
560 // printf ... | openssl pkeyutl -encrypt -inkey pub.pem -pubin -pkeyopt rsa_padding_mode:oaep | base64
561 const std::string ciphertext =
562R"BASE64(BgVqf6yKhMytCeL6+/oEoyx9rahh2+a1iU0D17RgfX6Q/3eNEcC2xY5QomjI7k/no8yiTZbXT9R4
563REE6ic0DjFSbFwJjC/P0/oTSQyUNn4xNO+aK0MGnyeN34A8c3bZfLuo2DU496SQF0B9I5SS3gEA9
564rqdIAdbWoOCKi1EX5SXtAOPTsj5PHHH+UM52bxOrUPvGSl9tRaz5S6hVf2haSoio4nnG3/bA6pdb
565hHjyHHA/oi1PlFRIe0EO2/riAdqdIW3eOqZGP5t5QAMmiP178A9YQdbSavTgBSuE20T01U82Ln+L
566pNk+PE/mmM20iNBo8C9cAJVUju6T4DCNZk7H5Q==
567)BASE64";
Yingdi Yu202a2e92015-07-12 16:49:25 -0700568 OBufferStream os;
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400569 bufferSource(ciphertext) >> base64Decode() >> streamSink(os);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700570
Davide Pesavento765abc92021-12-27 00:44:04 -0500571 auto decrypted = sKey.decrypt(*os.buf());
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400572 BOOST_CHECK_EQUAL_COLLECTIONS(plaintext, plaintext + sizeof(plaintext),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400573 decrypted->begin(), decrypted->end());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700574}
575
Davide Pesaventof45fa212017-09-14 17:23:56 -0400576BOOST_AUTO_TEST_CASE(RsaEncryptDecrypt)
Yingdi Yu202a2e92015-07-12 16:49:25 -0700577{
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400578 RsaKey_Aes256Encrypted dataSet;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700579
580 PublicKey pKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500581 pKey.loadPkcs8Base64({reinterpret_cast<const uint8_t*>(dataSet.publicKey.data()),
582 dataSet.publicKey.size()});
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400583 BOOST_CHECK_EQUAL(pKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700584
585 PrivateKey sKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500586 sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.data()),
587 dataSet.privateKeyPkcs1.size()});
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400588 BOOST_CHECK_EQUAL(sKey.getKeyType(), KeyType::RSA);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700589
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400590 const uint8_t plaintext[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
Yingdi Yu202a2e92015-07-12 16:49:25 -0700591
Davide Pesavento765abc92021-12-27 00:44:04 -0500592 auto ciphertext = pKey.encrypt(plaintext);
593 auto decrypted = sKey.decrypt(*ciphertext);
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400594 BOOST_CHECK_EQUAL_COLLECTIONS(plaintext, plaintext + sizeof(plaintext),
Davide Pesaventof45fa212017-09-14 17:23:56 -0400595 decrypted->begin(), decrypted->end());
596}
597
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500598BOOST_AUTO_TEST_CASE(UnsupportedDecryption)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400599{
Davide Pesaventof45fa212017-09-14 17:23:56 -0400600 OBufferStream os;
601 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
602
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500603 auto ecKey = generatePrivateKey(EcKeyParams());
Davide Pesavento765abc92021-12-27 00:44:04 -0500604 BOOST_CHECK_THROW(ecKey->decrypt(*os.buf()), PrivateKey::Error);
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500605
606 auto hmacKey = generatePrivateKey(HmacKeyParams());
Davide Pesavento765abc92021-12-27 00:44:04 -0500607 BOOST_CHECK_THROW(hmacKey->decrypt(*os.buf()), PrivateKey::Error);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700608}
609
Junxiao Shifbb69902020-04-24 08:39:18 +0000610class RsaKeyGenParams
Yingdi Yu202a2e92015-07-12 16:49:25 -0700611{
Junxiao Shifbb69902020-04-24 08:39:18 +0000612public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500613 using Params = RsaKeyParams;
614 using hasPublicKey = std::true_type;
615 using canSavePkcs1 = std::true_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000616
617 static void
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400618 checkPublicKey(const Buffer&)
Junxiao Shifbb69902020-04-24 08:39:18 +0000619 {
620 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500621};
622
Junxiao Shifbb69902020-04-24 08:39:18 +0000623class EcKeyGenParams
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500624{
Junxiao Shifbb69902020-04-24 08:39:18 +0000625public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500626 using Params = EcKeyParams;
627 using hasPublicKey = std::true_type;
628 using canSavePkcs1 = std::true_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000629
630 static void
631 checkPublicKey(const Buffer& bits)
632 {
633 // EC key generation should use named curve format. See https://redmine.named-data.net/issues/5037
634 // OBJECT IDENTIFIER 1.2.840.10045.3.1.7 prime256v1 (ANSI X9.62 named elliptic curve)
635 const uint8_t oid[] = {0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07};
636 BOOST_CHECK_MESSAGE(std::search(bits.begin(), bits.end(), oid, oid + sizeof(oid)) != bits.end(),
637 "OID not found in " << toHex(bits));
638 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500639};
640
Junxiao Shifbb69902020-04-24 08:39:18 +0000641class HmacKeyGenParams
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500642{
Junxiao Shifbb69902020-04-24 08:39:18 +0000643public:
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500644 using Params = HmacKeyParams;
645 using hasPublicKey = std::false_type;
646 using canSavePkcs1 = std::false_type;
Junxiao Shifbb69902020-04-24 08:39:18 +0000647
648 static void
Davide Pesaventoea9e43e2021-09-20 00:45:03 -0400649 checkPublicKey(const Buffer&)
Junxiao Shifbb69902020-04-24 08:39:18 +0000650 {
651 }
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500652};
653
Davide Pesavento94dfcf12021-09-26 14:18:45 -0400654using KeyGenParams = boost::mpl::vector<
655#if OPENSSL_VERSION_NUMBER < 0x30000000L // FIXME #5154
656 HmacKeyGenParams,
657#endif
658 RsaKeyGenParams,
659 EcKeyGenParams
660>;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500661
662BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyGenParams)
663{
Davide Pesavento1bac1112019-06-13 21:48:46 -0400664 typename T::Params params;
665 auto sKey = generatePrivateKey(params);
666 BOOST_CHECK_EQUAL(sKey->getKeyType(), params.getKeyType());
667 BOOST_CHECK_EQUAL(sKey->getKeySize(), params.getKeySize());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700668
Luca Keidel941fd8c2017-07-24 15:21:22 +0200669 const uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yub263f152015-07-12 16:50:13 -0700670 OBufferStream os;
Davide Pesavento765abc92021-12-27 00:44:04 -0500671 BOOST_REQUIRE_NO_THROW(bufferSource(data) >>
Yingdi Yub263f152015-07-12 16:50:13 -0700672 signerFilter(DigestAlgorithm::SHA256, *sKey) >>
673 streamSink(os));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500674 auto sig = os.buf();
Yingdi Yub263f152015-07-12 16:50:13 -0700675
Yingdi Yub263f152015-07-12 16:50:13 -0700676 bool result = false;
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500677 if (typename T::hasPublicKey()) {
678 auto pKeyBits = sKey->derivePublicKey();
Junxiao Shifbb69902020-04-24 08:39:18 +0000679 BOOST_REQUIRE(pKeyBits != nullptr);
680 T::checkPublicKey(*pKeyBits);
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500681 PublicKey pKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500682 pKey.loadPkcs8(*pKeyBits);
683 BOOST_CHECK_NO_THROW(bufferSource(data) >>
Davide Pesavento35c63792022-01-17 02:06:03 -0500684 verifierFilter(DigestAlgorithm::SHA256, pKey, *sig) >>
Yingdi Yub263f152015-07-12 16:50:13 -0700685 boolSink(result));
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500686 }
687 else {
688#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
689 BOOST_CHECK_THROW(sKey->derivePublicKey(), PrivateKey::Error);
690#endif
Davide Pesavento765abc92021-12-27 00:44:04 -0500691 BOOST_CHECK_NO_THROW(bufferSource(data) >>
Davide Pesavento35c63792022-01-17 02:06:03 -0500692 verifierFilter(DigestAlgorithm::SHA256, *sKey, *sig) >>
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500693 boolSink(result));
694 }
Yingdi Yub263f152015-07-12 16:50:13 -0700695 BOOST_CHECK(result);
696
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500697 if (typename T::canSavePkcs1()) {
Davide Pesavento1bac1112019-06-13 21:48:46 -0400698 auto sKey2 = generatePrivateKey(params);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700699
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500700 OBufferStream os1;
701 sKey->savePkcs1(os1);
702 OBufferStream os2;
703 sKey2->savePkcs1(os2);
Yingdi Yu202a2e92015-07-12 16:49:25 -0700704
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500705 BOOST_CHECK(*os1.buf() != *os2.buf());
706 }
707 else {
708#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
709 OBufferStream os1;
710 BOOST_CHECK_THROW(sKey->savePkcs1(os1), PrivateKey::Error);
711#endif
712 }
Yingdi Yu202a2e92015-07-12 16:49:25 -0700713}
714
Laqin Fan0fe72ea2019-05-22 16:42:59 -0500715BOOST_AUTO_TEST_CASE(GenerateKeyUnsupportedType)
Davide Pesaventof45fa212017-09-14 17:23:56 -0400716{
717 BOOST_CHECK_THROW(generatePrivateKey(AesKeyParams()), std::invalid_argument);
718}
719
Yingdi Yu202a2e92015-07-12 16:49:25 -0700720BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
721BOOST_AUTO_TEST_SUITE_END() // Transform
722BOOST_AUTO_TEST_SUITE_END() // Security
723
724} // namespace tests
725} // namespace transform
726} // namespace security
727} // namespace ndn