blob: ae234b3005a28d247f2a092524eac6dc09776553 [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 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/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"
25#include "ndn-cxx/security/transform.hpp"
Davide Pesavento1c31a712017-09-15 00:52:03 -040026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
28
Davide Pesavento1c31a712017-09-15 00:52:03 -040029#include <boost/mpl/vector.hpp>
30
31#include <sstream>
Yingdi Yu202a2e92015-07-12 16:49:25 -070032
33namespace ndn {
34namespace security {
35namespace transform {
36namespace tests {
37
38BOOST_AUTO_TEST_SUITE(Security)
39BOOST_AUTO_TEST_SUITE(Transform)
40BOOST_AUTO_TEST_SUITE(TestPublicKey)
41
Davide Pesavento1c31a712017-09-15 00:52:03 -040042struct RsaKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070043{
Davide Pesavento1c31a712017-09-15 00:52:03 -040044 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070045 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
46 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
47 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
48 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
49 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
50 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
51 "ywIDAQAB\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -070052};
53
Davide Pesavento1c31a712017-09-15 00:52:03 -040054struct EcKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -070055{
Davide Pesavento1c31a712017-09-15 00:52:03 -040056 const std::string publicKeyPkcs8 =
Yingdi Yu202a2e92015-07-12 16:49:25 -070057 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
58 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
59 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
60 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
61 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
62 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
63 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
Yingdi Yu202a2e92015-07-12 16:49:25 -070064};
65
Davide Pesavento1c31a712017-09-15 00:52:03 -040066using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>;
Yingdi Yu202a2e92015-07-12 16:49:25 -070067
Davide Pesavento1c31a712017-09-15 00:52:03 -040068BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
Yingdi Yu202a2e92015-07-12 16:49:25 -070069{
70 T dataSet;
71
72 const uint8_t* pKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str());
73 size_t pKeyPkcs8Base64Len = dataSet.publicKeyPkcs8.size();
Yingdi Yu202a2e92015-07-12 16:49:25 -070074 OBufferStream os;
75 bufferSource(pKeyPkcs8Base64, pKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os);
76 ConstBufferPtr pKeyPkcs8Buf = os.buf();
Davide Pesavento5d0b0102017-10-07 13:43:16 -040077 const uint8_t* pKeyPkcs8 = pKeyPkcs8Buf->data();
Yingdi Yu202a2e92015-07-12 16:49:25 -070078 size_t pKeyPkcs8Len = pKeyPkcs8Buf->size();
79
80 PublicKey pKey1;
Davide Pesavento1c31a712017-09-15 00:52:03 -040081 BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64, pKeyPkcs8Base64Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -070082
83 std::stringstream ss2(dataSet.publicKeyPkcs8);
84 PublicKey pKey2;
Davide Pesavento1c31a712017-09-15 00:52:03 -040085 BOOST_CHECK_NO_THROW(pKey2.loadPkcs8Base64(ss2));
Yingdi Yu202a2e92015-07-12 16:49:25 -070086
87 PublicKey pKey3;
Davide Pesavento1c31a712017-09-15 00:52:03 -040088 BOOST_CHECK_NO_THROW(pKey3.loadPkcs8(pKeyPkcs8, pKeyPkcs8Len));
Yingdi Yu202a2e92015-07-12 16:49:25 -070089
90 std::stringstream ss4;
91 ss4.write(reinterpret_cast<const char*>(pKeyPkcs8), pKeyPkcs8Len);
92 PublicKey pKey4;
Davide Pesavento1c31a712017-09-15 00:52:03 -040093 BOOST_CHECK_NO_THROW(pKey4.loadPkcs8(ss4));
Yingdi Yu202a2e92015-07-12 16:49:25 -070094
95 OBufferStream os5;
96 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5));
97 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8Base64, pKeyPkcs8Base64 + pKeyPkcs8Base64Len,
98 os5.buf()->begin(), os5.buf()->end());
99
100 OBufferStream os6;
101 BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8(os6));
102 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8, pKeyPkcs8 + pKeyPkcs8Len,
103 os6.buf()->begin(), os6.buf()->end());
104}
105
Davide Pesavento1c31a712017-09-15 00:52:03 -0400106// NOTE: We cannot test RSA encryption by comparing the computed ciphertext to
107// a known-good one, because OAEP padding is randomized and would produce
108// different results every time. An encrypt/decrypt round-trip test is
109// performed in private-key.t.cpp
110
111BOOST_AUTO_TEST_CASE(UnsupportedEcEncryption)
112{
113 EcKeyTestData dataSet;
114
115 PublicKey pKey;
116 pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()),
117 dataSet.publicKeyPkcs8.size());
Davide Pesavento06f1bdf2017-09-16 18:59:15 -0400118 BOOST_CHECK_EQUAL(pKey.getKeyType(), KeyType::EC);
Davide Pesavento1c31a712017-09-15 00:52:03 -0400119
120 OBufferStream os;
121 bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os);
122
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400123 BOOST_CHECK_THROW(pKey.encrypt(os.buf()->data(), os.buf()->size()), PublicKey::Error);
Davide Pesavento1c31a712017-09-15 00:52:03 -0400124}
125
Yingdi Yu202a2e92015-07-12 16:49:25 -0700126BOOST_AUTO_TEST_SUITE_END() // TestPublicKey
127BOOST_AUTO_TEST_SUITE_END() // Transform
128BOOST_AUTO_TEST_SUITE_END() // Security
129
130} // namespace tests
131} // namespace transform
132} // namespace security
133} // namespace ndn