Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 94dfcf1 | 2021-09-26 14:18:45 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -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/verifier-filter.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -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 | 94dfcf1 | 2021-09-26 14:18:45 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/impl/openssl.hpp" |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/security/key-params.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #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/private-key.hpp" |
| 31 | #include "ndn-cxx/security/transform/public-key.hpp" |
| 32 | #include "ndn-cxx/security/transform/signer-filter.hpp" |
| 33 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 35 | #include "tests/boost-test.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace security { |
| 39 | namespace transform { |
| 40 | namespace tests { |
| 41 | |
| 42 | BOOST_AUTO_TEST_SUITE(Security) |
| 43 | BOOST_AUTO_TEST_SUITE(Transform) |
| 44 | BOOST_AUTO_TEST_SUITE(TestVerifierFilter) |
| 45 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 46 | const uint8_t DATA[] = {0x01, 0x02, 0x03, 0x04}; |
| 47 | |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 48 | BOOST_AUTO_TEST_CASE(Rsa) |
| 49 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 50 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 51 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 52 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 53 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 54 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 55 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 56 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 57 | "ywIDAQAB\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 58 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 59 | "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDRYzX9aEDHIu0\n" |
| 60 | "SqyIAmBYNla7ORil5hV155lypFnPN9GAFBOw2jNLm3gefBNmHDBdsfuTdA3SRFNX\n" |
| 61 | "zbpehvCzeKKCiGcz3JMr4oqJrI+EgCb/7kcZPpKAUmFiGqjX+6lZ256iOKXpE/j8\n" |
| 62 | "CBq8eGinGUBVuTob1V1WeVBxDlm/Q5waLvdz4SdgP9iBRFgZKeBSL9ieyHvv2nZT\n" |
| 63 | "r3+172BnAgvk+4T0GI7ojf1wv3PsQrVJ9mE5a8N7+oftiEP8EfBxaK3wWNHDDWCX\n" |
| 64 | "BFVMmwD2sfnzKdoCLHS1xH+cExEtRf57etREeDpRtKMlt1v2qYozV9MYcpTMv/mk\n" |
| 65 | "wbq4FTTLAgMBAAECggEANCRyQ4iXghkxROdbwsW/rE52QnAwoLwbpuw9EVvJj4e8\n" |
| 66 | "LZMu3t6lK99L5/gBxhZo49wO7YTj2+3aw2twBKXLyGDCJFEAHd0cf29yxuiJOjxu\n" |
| 67 | "LZEW8yq+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+ZFy4cgKawKBgC0UnWJ2oygfERLeaVGl\n" |
| 82 | "/YnHJC50/dIKbZakaapXOFFgiep5q1jmxR2U8seb+nvtFPsTLFAdOXCfwUk+4z/h\n" |
| 83 | "kfWtB3+8H5jyoC1gkJ7EMyxu8tb4mz5U6+SPB4QLSetwvfWP2YXS/PkTq19G7iGE\n" |
| 84 | "novjJ9azSBJ6OyR5UH/DxBji\n"; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 85 | |
| 86 | OBufferStream os1; |
| 87 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 88 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 89 | |
| 90 | PublicKey pKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 91 | pKey.loadPkcs8(*pubKey); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 92 | |
| 93 | PrivateKey sKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 94 | sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), |
| 95 | privateKeyPkcs1.size()}); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 96 | |
| 97 | OBufferStream os2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 98 | bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 99 | auto sig = os2.buf(); |
| 100 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 101 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 102 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, sig->data(), sig->size()), Error); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 103 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 104 | bool result = false; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 105 | bufferSource(DATA) >> |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 106 | verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 107 | boolSink(result); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 108 | |
| 109 | BOOST_CHECK_EQUAL(result, true); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(Ecdsa) |
| 113 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 114 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 115 | "MIIBeQIBADCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAAB\n" |
| 116 | "AAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA\n" |
| 117 | "///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMV\n" |
| 118 | "AMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg\n" |
| 119 | "9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8A\n" |
| 120 | "AAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBBG0wawIBAQQgRxwcbzK9RV6A\n" |
| 121 | "HYFsDcykI86o3M/a1KlJn0z8PcLMBZOhRANCAARobhYm4MC3RCQQzi3b0oNR3ORC\n" |
| 122 | "Uw8aupbORaGC304afBzo7sBks9KsPKHDKspLtctFeaXkOKxD3dG8HKWXfbLw\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 123 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 124 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 125 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 126 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 127 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 128 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 129 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 130 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 131 | |
| 132 | OBufferStream os1; |
| 133 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 134 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 135 | |
| 136 | PublicKey pKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 137 | pKey.loadPkcs8(*pubKey); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 138 | |
| 139 | PrivateKey sKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 140 | sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), |
| 141 | privateKeyPkcs1.size()}); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 142 | |
| 143 | OBufferStream os2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 144 | bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 145 | auto sig = os2.buf(); |
| 146 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 147 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 148 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, sig->data(), sig->size()), Error); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 149 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 150 | bool result = false; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 151 | bufferSource(DATA) >> |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 152 | verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 153 | boolSink(result); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 154 | |
| 155 | BOOST_CHECK_EQUAL(result, true); |
| 156 | } |
| 157 | |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE(Hmac) |
| 159 | { |
| 160 | auto sKey = generatePrivateKey(HmacKeyParams()); |
| 161 | |
| 162 | OBufferStream os; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 163 | bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, *sKey) >> streamSink(os); |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 164 | auto sig = os.buf(); |
| 165 | |
| 166 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, *sKey, sig->data(), sig->size()), Error); |
| 167 | |
Davide Pesavento | 94dfcf1 | 2021-09-26 14:18:45 -0400 | [diff] [blame] | 168 | #if OPENSSL_VERSION_NUMBER < 0x30000000L // FIXME #5154 |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 169 | bool result = false; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 170 | bufferSource(DATA) >> |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 171 | verifierFilter(DigestAlgorithm::SHA256, *sKey, sig->data(), sig->size()) >> |
| 172 | boolSink(result); |
| 173 | |
| 174 | BOOST_CHECK_EQUAL(result, true); |
Davide Pesavento | 94dfcf1 | 2021-09-26 14:18:45 -0400 | [diff] [blame] | 175 | #endif |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 178 | BOOST_AUTO_TEST_CASE(InvalidKey) |
| 179 | { |
Laqin Fan | 0fe72ea | 2019-05-22 16:42:59 -0500 | [diff] [blame] | 180 | PublicKey pubKey; |
| 181 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, pubKey, nullptr, 0), Error); |
| 182 | PrivateKey privKey; |
| 183 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, privKey, nullptr, 0), Error); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 186 | BOOST_AUTO_TEST_SUITE_END() // TestVerifierFilter |
| 187 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 188 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 189 | |
| 190 | } // namespace tests |
| 191 | } // namespace transform |
| 192 | } // namespace security |
| 193 | } // namespace ndn |