Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/transform/public-key.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/transform/base64-decode.hpp" |
| 26 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 27 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
| 30 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 31 | #include <boost/mpl/vector.hpp> |
| 32 | |
| 33 | #include <sstream> |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace security { |
| 37 | namespace transform { |
| 38 | namespace tests { |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
| 41 | BOOST_AUTO_TEST_SUITE(Transform) |
| 42 | BOOST_AUTO_TEST_SUITE(TestPublicKey) |
| 43 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 44 | struct RsaKeyTestData |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 45 | { |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 46 | static constexpr KeyType type = KeyType::RSA; |
| 47 | static constexpr size_t size = 2048; |
| 48 | const std::string pkcs8Base64 = |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 49 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 50 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 51 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 52 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 53 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 54 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 55 | "ywIDAQAB\n"; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 56 | }; |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 57 | constexpr KeyType RsaKeyTestData::type; |
| 58 | constexpr size_t RsaKeyTestData::size; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 59 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 60 | struct EcKeyTestData |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 61 | { |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 62 | static constexpr KeyType type = KeyType::EC; |
| 63 | static constexpr size_t size = 256; |
| 64 | const std::string pkcs8Base64 = |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 65 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 66 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 67 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 68 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 69 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 70 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 71 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 72 | }; |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 73 | constexpr KeyType EcKeyTestData::type; |
| 74 | constexpr size_t EcKeyTestData::size; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 76 | using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE_TEMPLATE(LoadAndSave, T, KeyTestDataSets) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 79 | { |
| 80 | T dataSet; |
| 81 | |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 82 | auto pKeyPkcs8Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.pkcs8Base64.data()), |
| 83 | dataSet.pkcs8Base64.size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 84 | OBufferStream os; |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 85 | bufferSource(pKeyPkcs8Base64) >> base64Decode() >> streamSink(os); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 86 | auto pKeyPkcs8 = os.buf(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 87 | |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 88 | // Load |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 89 | PublicKey pKey1; |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 90 | BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64)); |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 91 | BOOST_TEST(pKey1.getKeyType() == T::type); |
| 92 | BOOST_TEST(pKey1.getKeySize() == T::size); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 93 | |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 94 | std::stringstream ss2(dataSet.pkcs8Base64); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 95 | PublicKey pKey2; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 96 | BOOST_CHECK_NO_THROW(pKey2.loadPkcs8Base64(ss2)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 97 | |
| 98 | PublicKey pKey3; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 99 | BOOST_CHECK_NO_THROW(pKey3.loadPkcs8(*pKeyPkcs8)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 100 | |
| 101 | std::stringstream ss4; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 102 | ss4.write(reinterpret_cast<const char*>(pKeyPkcs8->data()), pKeyPkcs8->size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 103 | PublicKey pKey4; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 104 | BOOST_CHECK_NO_THROW(pKey4.loadPkcs8(ss4)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 105 | |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 106 | // Save |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 107 | OBufferStream os5; |
| 108 | BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5)); |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 109 | BOOST_TEST(*os5.buf() == pKeyPkcs8Base64, boost::test_tools::per_element()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 110 | |
| 111 | OBufferStream os6; |
| 112 | BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8(os6)); |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 113 | BOOST_TEST(*os6.buf() == *pKeyPkcs8, boost::test_tools::per_element()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 116 | // NOTE: We cannot test RSA encryption by comparing the computed ciphertext to |
| 117 | // a known-good one, because OAEP padding is randomized and would produce |
| 118 | // different results every time. An encrypt/decrypt round-trip test is |
| 119 | // performed in private-key.t.cpp |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(UnsupportedEcEncryption) |
| 122 | { |
| 123 | EcKeyTestData dataSet; |
| 124 | |
| 125 | PublicKey pKey; |
Davide Pesavento | dd0724b | 2022-04-18 00:30:05 -0400 | [diff] [blame] | 126 | pKey.loadPkcs8Base64({reinterpret_cast<const uint8_t*>(dataSet.pkcs8Base64.data()), |
| 127 | dataSet.pkcs8Base64.size()}); |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 128 | |
| 129 | OBufferStream os; |
| 130 | bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os); |
| 131 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 132 | BOOST_CHECK_THROW(pKey.encrypt(*os.buf()), PublicKey::Error); |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 135 | BOOST_AUTO_TEST_SUITE_END() // TestPublicKey |
| 136 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 137 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 138 | |
| 139 | } // namespace tests |
| 140 | } // namespace transform |
| 141 | } // namespace security |
| 142 | } // namespace ndn |