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 | /* |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
| 22 | #include "security/transform/public-key.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 24 | #include "encoding/buffer-stream.hpp" |
| 25 | #include "security/transform.hpp" |
| 26 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 28 | #include <boost/mpl/vector.hpp> |
| 29 | |
| 30 | #include <sstream> |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace security { |
| 34 | namespace transform { |
| 35 | namespace tests { |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
| 38 | BOOST_AUTO_TEST_SUITE(Transform) |
| 39 | BOOST_AUTO_TEST_SUITE(TestPublicKey) |
| 40 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 41 | struct RsaKeyTestData |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 42 | { |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 43 | const std::string publicKeyPkcs8 = |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 44 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 45 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 46 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 47 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 48 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 49 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 50 | "ywIDAQAB\n"; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 53 | struct EcKeyTestData |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 54 | { |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 55 | const std::string publicKeyPkcs8 = |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 56 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 57 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 58 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 59 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 60 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 61 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 62 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 65 | using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 66 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 68 | { |
| 69 | T dataSet; |
| 70 | |
| 71 | const uint8_t* pKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()); |
| 72 | size_t pKeyPkcs8Base64Len = dataSet.publicKeyPkcs8.size(); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 73 | OBufferStream os; |
| 74 | bufferSource(pKeyPkcs8Base64, pKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os); |
| 75 | ConstBufferPtr pKeyPkcs8Buf = os.buf(); |
| 76 | const uint8_t* pKeyPkcs8 = pKeyPkcs8Buf->buf(); |
| 77 | size_t pKeyPkcs8Len = pKeyPkcs8Buf->size(); |
| 78 | |
| 79 | PublicKey pKey1; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 80 | BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64, pKeyPkcs8Base64Len)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 81 | |
| 82 | std::stringstream ss2(dataSet.publicKeyPkcs8); |
| 83 | PublicKey pKey2; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 84 | BOOST_CHECK_NO_THROW(pKey2.loadPkcs8Base64(ss2)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 85 | |
| 86 | PublicKey pKey3; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 87 | BOOST_CHECK_NO_THROW(pKey3.loadPkcs8(pKeyPkcs8, pKeyPkcs8Len)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 88 | |
| 89 | std::stringstream ss4; |
| 90 | ss4.write(reinterpret_cast<const char*>(pKeyPkcs8), pKeyPkcs8Len); |
| 91 | PublicKey pKey4; |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 92 | BOOST_CHECK_NO_THROW(pKey4.loadPkcs8(ss4)); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 93 | |
| 94 | OBufferStream os5; |
| 95 | BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5)); |
| 96 | BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8Base64, pKeyPkcs8Base64 + pKeyPkcs8Base64Len, |
| 97 | os5.buf()->begin(), os5.buf()->end()); |
| 98 | |
| 99 | OBufferStream os6; |
| 100 | BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8(os6)); |
| 101 | BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8, pKeyPkcs8 + pKeyPkcs8Len, |
| 102 | os6.buf()->begin(), os6.buf()->end()); |
| 103 | } |
| 104 | |
Davide Pesavento | 1c31a71 | 2017-09-15 00:52:03 -0400 | [diff] [blame] | 105 | // NOTE: We cannot test RSA encryption by comparing the computed ciphertext to |
| 106 | // a known-good one, because OAEP padding is randomized and would produce |
| 107 | // different results every time. An encrypt/decrypt round-trip test is |
| 108 | // performed in private-key.t.cpp |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(UnsupportedEcEncryption) |
| 111 | { |
| 112 | EcKeyTestData dataSet; |
| 113 | |
| 114 | PublicKey pKey; |
| 115 | pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()), |
| 116 | dataSet.publicKeyPkcs8.size()); |
| 117 | |
| 118 | OBufferStream os; |
| 119 | bufferSource("Y2lhbyFob2xhIWhlbGxvIQ==") >> base64Decode() >> streamSink(os); |
| 120 | |
| 121 | BOOST_CHECK_THROW(pKey.encrypt(os.buf()->buf(), os.buf()->size()), PublicKey::Error); |
| 122 | } |
| 123 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 124 | BOOST_AUTO_TEST_SUITE_END() // TestPublicKey |
| 125 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 126 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 127 | |
| 128 | } // namespace tests |
| 129 | } // namespace transform |
| 130 | } // namespace security |
| 131 | } // namespace ndn |