blob: 31970c4f0d10c875ebe46185861e63f319b2f846 [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento1c31a712017-09-15 00:52:03 -04002/*
Davide Pesavento05ec0be2023-03-14 21:18:06 -04003 * Copyright (c) 2013-2023 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/public-key.hpp"
Yingdi Yu202a2e92015-07-12 16:49:25 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/encoding/buffer-stream.hpp"
Davide Pesavento765abc92021-12-27 00:44:04 -050025#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 Pesavento1c31a712017-09-15 00:52:03 -040028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30
Davide Pesavento1c31a712017-09-15 00:52:03 -040031#include <boost/mpl/vector.hpp>
32
33#include <sstream>
Yingdi Yu202a2e92015-07-12 16:49:25 -070034
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040035namespace ndn::tests {
36
37using namespace ndn::security::transform;
Yingdi Yu202a2e92015-07-12 16:49:25 -070038
39BOOST_AUTO_TEST_SUITE(Security)
40BOOST_AUTO_TEST_SUITE(Transform)
41BOOST_AUTO_TEST_SUITE(TestPublicKey)
42
Davide Pesavento1c31a712017-09-15 00:52:03 -040043struct RsaKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070044{
Davide Pesaventodd0724b2022-04-18 00:30:05 -040045 static constexpr KeyType type = KeyType::RSA;
46 static constexpr size_t size = 2048;
47 const std::string pkcs8Base64 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070048 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
49 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
50 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
51 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
52 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
53 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
54 "ywIDAQAB\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -070055};
56
Davide Pesavento1c31a712017-09-15 00:52:03 -040057struct EcKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070058{
Davide Pesaventodd0724b2022-04-18 00:30:05 -040059 static constexpr KeyType type = KeyType::EC;
60 static constexpr size_t size = 256;
61 const std::string pkcs8Base64 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070062 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
63 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
64 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
65 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
66 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
67 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
68 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -070069};
70
Davide Pesavento1c31a712017-09-15 00:52:03 -040071using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>;
Yingdi Yu202a2e92015-07-12 16:49:25 -070072
Davide Pesaventodd0724b2022-04-18 00:30:05 -040073BOOST_AUTO_TEST_CASE_TEMPLATE(LoadAndSave, T, KeyTestDataSets)
Yingdi Yu202a2e92015-07-12 16:49:25 -070074{
75 T dataSet;
76
Davide Pesaventodd0724b2022-04-18 00:30:05 -040077 auto pKeyPkcs8Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.pkcs8Base64.data()),
78 dataSet.pkcs8Base64.size());
Yingdi Yu202a2e92015-07-12 16:49:25 -070079 OBufferStream os;
Davide Pesavento35c63792022-01-17 02:06:03 -050080 bufferSource(pKeyPkcs8Base64) >> base64Decode() >> streamSink(os);
Davide Pesavento765abc92021-12-27 00:44:04 -050081 auto pKeyPkcs8 = os.buf();
Yingdi Yu202a2e92015-07-12 16:49:25 -070082
Davide Pesaventodd0724b2022-04-18 00:30:05 -040083 // Load
Yingdi Yu202a2e92015-07-12 16:49:25 -070084 PublicKey pKey1;
Davide Pesavento35c63792022-01-17 02:06:03 -050085 BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64));
Davide Pesaventodd0724b2022-04-18 00:30:05 -040086 BOOST_TEST(pKey1.getKeyType() == T::type);
87 BOOST_TEST(pKey1.getKeySize() == T::size);
Yingdi Yu202a2e92015-07-12 16:49:25 -070088
Davide Pesaventodd0724b2022-04-18 00:30:05 -040089 std::stringstream ss2(dataSet.pkcs8Base64);
Yingdi Yu202a2e92015-07-12 16:49:25 -070090 PublicKey pKey2;
Davide Pesavento1c31a712017-09-15 00:52:03 -040091 BOOST_CHECK_NO_THROW(pKey2.loadPkcs8Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -070092
93 PublicKey pKey3;
Davide Pesavento765abc92021-12-27 00:44:04 -050094 BOOST_CHECK_NO_THROW(pKey3.loadPkcs8(*pKeyPkcs8));
Yingdi Yu202a2e92015-07-12 16:49:25 -070095
96 std::stringstream ss4;
Davide Pesavento765abc92021-12-27 00:44:04 -050097 ss4.write(reinterpret_cast<const char*>(pKeyPkcs8->data()), pKeyPkcs8->size());
Yingdi Yu202a2e92015-07-12 16:49:25 -070098 PublicKey pKey4;
Davide Pesavento1c31a712017-09-15 00:52:03 -040099 BOOST_CHECK_NO_THROW(pKey4.loadPkcs8(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -0700100
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400101 // Save
Yingdi Yu202a2e92015-07-12 16:49:25 -0700102 OBufferStream os5;
103 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5));
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400104 BOOST_TEST(*os5.buf() == pKeyPkcs8Base64, boost::test_tools::per_element());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700105
106 OBufferStream os6;
107 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8(os6));
Davide Pesaventodd0724b2022-04-18 00:30:05 -0400108 BOOST_TEST(*os6.buf() == *pKeyPkcs8, boost::test_tools::per_element());
Yingdi Yu202a2e92015-07-12 16:49:25 -0700109}
110
Davide Pesavento07db0732022-05-06 15:20:26 -0400111BOOST_AUTO_TEST_CASE(LoadError)
112{
113 EcKeyTestData dataSet;
114 auto pkcs8Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.pkcs8Base64.data()),
115 dataSet.pkcs8Base64.size());
116 OBufferStream os;
117 bufferSource(pkcs8Base64) >> base64Decode() >> streamSink(os);
118 auto pkcs8 = os.buf();
119
120 PublicKey pKey;
121 // empty
122 BOOST_CHECK_THROW(pKey.loadPkcs8(span<uint8_t>{}), PublicKey::Error);
123 BOOST_CHECK_THROW(pKey.loadPkcs8Base64(span<uint8_t>{}), PublicKey::Error);
124 // truncated
125 BOOST_CHECK_THROW(pKey.loadPkcs8(make_span(*pkcs8).first(10)), PublicKey::Error);
126 BOOST_CHECK_THROW(pKey.loadPkcs8Base64(pkcs8Base64.first(10)), PublicKey::Error);
127}
128
Davide Pesavento1c31a712017-09-15 00:52:03 -0400129// NOTE: We cannot test RSA encryption by comparing the computed ciphertext to
130// a known-good one, because OAEP padding is randomized and would produce
131// different results every time. An encrypt/decrypt round-trip test is
132// performed in private-key.t.cpp
133
Davide Pesavento07db0732022-05-06 15:20:26 -0400134BOOST_AUTO_TEST_CASE(UnsupportedEncryption)
Davide Pesavento1c31a712017-09-15 00:52:03 -0400135{
Davide Pesavento1c31a712017-09-15 00:52:03 -0400136 OBufferStream os;
137 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
Davide Pesavento07db0732022-05-06 15:20:26 -0400138 auto plain = os.buf();
Davide Pesavento1c31a712017-09-15 00:52:03 -0400139
Davide Pesavento07db0732022-05-06 15:20:26 -0400140 PublicKey pKey;
141 BOOST_CHECK_THROW(pKey.encrypt(*plain), PublicKey::Error);
142
143 EcKeyTestData dataSet;
144 pKey.loadPkcs8Base64({reinterpret_cast<const uint8_t*>(dataSet.pkcs8Base64.data()),
145 dataSet.pkcs8Base64.size()});
146 BOOST_CHECK_THROW(pKey.encrypt(*plain), PublicKey::Error);
Davide Pesavento1c31a712017-09-15 00:52:03 -0400147}
148
Yingdi Yu202a2e92015-07-12 16:49:25 -0700149BOOST_AUTO_TEST_SUITE_END() // TestPublicKey
150BOOST_AUTO_TEST_SUITE_END() // Transform
151BOOST_AUTO_TEST_SUITE_END() // Security
152
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400153} // namespace ndn::tests