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 | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "security/transform/signer-filter.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 24 | #include "encoding/buffer-stream.hpp" |
| 25 | #include "security/transform/base64-decode.hpp" |
| 26 | #include "security/transform/buffer-source.hpp" |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 27 | #include "security/transform/private-key.hpp" |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 28 | #include "security/transform/stream-sink.hpp" |
| 29 | #include "security/verification-helpers.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 30 | |
| 31 | #include "boost-test.hpp" |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
| 35 | namespace transform { |
| 36 | namespace tests { |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
| 39 | BOOST_AUTO_TEST_SUITE(Transform) |
| 40 | BOOST_AUTO_TEST_SUITE(TestSignerFilter) |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(Rsa) |
| 43 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 44 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 45 | "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"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 52 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 53 | "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpe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OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA\n" |
| 69 | "Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e\n" |
| 70 | "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n" |
| 71 | "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n" |
| 72 | "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n" |
| 73 | "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n" |
| 74 | "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n" |
| 75 | "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n" |
| 76 | "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n" |
| 77 | "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 78 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 79 | |
| 80 | OBufferStream os1; |
| 81 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 82 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 83 | |
| 84 | PrivateKey sKey; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 85 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), privateKeyPkcs1.size()); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 86 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 87 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::NONE, sKey), Error); |
| 88 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 89 | OBufferStream os2; |
| 90 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
| 91 | auto sig = os2.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 92 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 93 | BOOST_CHECK(verifySignature(data, sizeof(data), sig->data(), sig->size(), pubKey->data(), pubKey->size())); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(Ecdsa) |
| 97 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 98 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 99 | "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n" |
| 100 | "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n" |
| 101 | "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n" |
| 102 | "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n" |
| 103 | "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n" |
| 104 | "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n" |
| 105 | "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n" |
| 106 | "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 107 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 108 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 109 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 110 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 111 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 112 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 113 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 114 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 115 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 116 | |
| 117 | OBufferStream os1; |
| 118 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 119 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 120 | |
| 121 | PrivateKey sKey; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 122 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), privateKeyPkcs1.size()); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 123 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 124 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::NONE, sKey), Error); |
| 125 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 126 | OBufferStream os2; |
| 127 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
| 128 | auto sig = os2.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 130 | BOOST_CHECK(verifySignature(data, sizeof(data), sig->data(), sig->size(), pubKey->data(), pubKey->size())); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 133 | BOOST_AUTO_TEST_CASE(InvalidKey) |
| 134 | { |
| 135 | PrivateKey sKey; |
| 136 | BOOST_CHECK_THROW(SignerFilter(DigestAlgorithm::SHA256, sKey), Error); |
| 137 | } |
| 138 | |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 139 | BOOST_AUTO_TEST_SUITE_END() // TestSignerFilter |
| 140 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 141 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 142 | |
| 143 | } // namespace tests |
| 144 | } // namespace transform |
| 145 | } // namespace security |
| 146 | } // namespace ndn |