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/verifier-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/bool-sink.hpp" |
| 27 | #include "security/transform/buffer-source.hpp" |
Davide Pesavento | 06f1bdf | 2017-09-16 18:59:15 -0400 | [diff] [blame] | 28 | #include "security/transform/private-key.hpp" |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 29 | #include "security/transform/public-key.hpp" |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 30 | #include "security/transform/signer-filter.hpp" |
| 31 | #include "security/transform/stream-sink.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 32 | |
| 33 | #include "boost-test.hpp" |
| 34 | |
| 35 | namespace ndn { |
| 36 | namespace security { |
| 37 | namespace transform { |
| 38 | namespace tests { |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
| 41 | BOOST_AUTO_TEST_SUITE(Transform) |
| 42 | BOOST_AUTO_TEST_SUITE(TestVerifierFilter) |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(Rsa) |
| 45 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 46 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 47 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 48 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 49 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 50 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 51 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 52 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 53 | "ywIDAQAB\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 54 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 55 | "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDRYzX9aEDHIu0\n" |
| 56 | "SqyIAmBYNla7ORil5hV155lypFnPN9GAFBOw2jNLm3gefBNmHDBdsfuTdA3SRFNX\n" |
| 57 | "zbpehvCzeKKCiGcz3JMr4oqJrI+EgCb/7kcZPpKAUmFiGqjX+6lZ256iOKXpE/j8\n" |
| 58 | "CBq8eGinGUBVuTob1V1WeVBxDlm/Q5waLvdz4SdgP9iBRFgZKeBSL9ieyHvv2nZT\n" |
| 59 | "r3+172BnAgvk+4T0GI7ojf1wv3PsQrVJ9mE5a8N7+oftiEP8EfBxaK3wWNHDDWCX\n" |
| 60 | "BFVMmwD2sfnzKdoCLHS1xH+cExEtRf57etREeDpRtKMlt1v2qYozV9MYcpTMv/mk\n" |
| 61 | "wbq4FTTLAgMBAAECggEANCRyQ4iXghkxROdbwsW/rE52QnAwoLwbpuw9EVvJj4e8\n" |
| 62 | "LZMu3t6lK99L5/gBxhZo49wO7YTj2+3aw2twBKXLyGDCJFEAHd0cf29yxuiJOjxu\n" |
| 63 | "LZEW8yq+O/3De0rbIzFUO2ZlqbOuudpXdhVD7mfIqjYX88wONDh5QAoM7OOEG4oe\n" |
| 64 | "xkFMWcDUwU0j5QqPlfhinrgMWYqXFNf9TZvDNXLCjmHPHZSHDnWOaguWzhhS8wlc\n" |
| 65 | "PTBblm1hG4+iBe9dv+h/15//bT/BTXVYUqBdviB9HzNRdpdLWxdydWbf7bi8iz10\n" |
| 66 | "ClTDKS6jKM6rFapwdF5zZBPYXFUaQUStrN4I9riswQKBgQDljwLLCiYhxOB6sUYU\n" |
| 67 | "J4wcmvydAapjZX+jAVveT2ZpzM+cL2nhr1FzmzMvED0UxgXG6tBkwFZIQbYlLUdH\n" |
| 68 | "aaeOKDHxQqNgwv8D6u++Nk4x7gzpLLaCCHhKQtkqlZPONN7TsHIz+Pm/9KM1mFYA\n" |
| 69 | "buzDj8uY8ZFCTAm/4pmEaiO46QKBgQDZw4VPpwlG/qS/NPP1LQI5k5Wb564mH8Fe\n" |
| 70 | "nugCwCZs186lyQ8zOodfLz/Cl0qXoABwHns67O2U19XUPuq9vPsm5GVjBDRwR8GB\n" |
| 71 | "tk9zPWnXwccNeHCfntk9vwbfdiH06aDQc0AiZvguxW5KrEDo3BKPtylF6SBN52uE\n" |
| 72 | "sU8n5h1vkwKBgQCwzdDs6MgtwiDS3q6G316+uXBOzPWa0JXZyjYjpyvN2P0d4ja+\n" |
| 73 | "p/UoASUO3obs9QeGCVyv/KN3y4SqZZE8o1d12ed9VkHXSNh4//3elpzrP9mZzeJT\n" |
| 74 | "jIp5R7tTXRkV/QqSKJgNB3n0Kkt5//ZdJxIcHShGh+fFFCN+Mtzia41P4QKBgQCV\n" |
| 75 | "wOTTow45OXL4XyUJzVsDV2ACaDAV3a6wMF1jTtrd7QcacYs3cp+XsLmLS1mrrge/\n" |
| 76 | "Eucx3a+AtXFCVcY+l1CsLVMf5cteD6qeVk6K9IfuLT+DHvlse+Pvl4fVcrrlXykN\n" |
| 77 | "UMShI+i22WUAizbULEvDc3U5s5lYmbYR+ZFy4cgKawKBgC0UnWJ2oygfERLeaVGl\n" |
| 78 | "/YnHJC50/dIKbZakaapXOFFgiep5q1jmxR2U8seb+nvtFPsTLFAdOXCfwUk+4z/h\n" |
| 79 | "kfWtB3+8H5jyoC1gkJ7EMyxu8tb4mz5U6+SPB4QLSetwvfWP2YXS/PkTq19G7iGE\n" |
| 80 | "novjJ9azSBJ6OyR5UH/DxBji\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 81 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 82 | |
| 83 | OBufferStream os1; |
| 84 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 85 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 86 | |
| 87 | PublicKey pKey; |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 88 | pKey.loadPkcs8(pubKey->data(), pubKey->size()); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 89 | |
| 90 | PrivateKey sKey; |
| 91 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), privateKeyPkcs1.size()); |
| 92 | |
| 93 | OBufferStream os2; |
| 94 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
| 95 | auto sig = os2.buf(); |
| 96 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 97 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 98 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 99 | bool result = false; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 100 | bufferSource(data, sizeof(data)) >> |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 101 | verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 102 | boolSink(result); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 103 | |
| 104 | BOOST_CHECK_EQUAL(result, true); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(Ecdsa) |
| 108 | { |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 109 | const std::string privateKeyPkcs1 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 110 | "MIIBeQIBADCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAAB\n" |
| 111 | "AAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA\n" |
| 112 | "///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMV\n" |
| 113 | "AMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg\n" |
| 114 | "9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8A\n" |
| 115 | "AAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBBG0wawIBAQQgRxwcbzK9RV6A\n" |
| 116 | "HYFsDcykI86o3M/a1KlJn0z8PcLMBZOhRANCAARobhYm4MC3RCQQzi3b0oNR3ORC\n" |
| 117 | "Uw8aupbORaGC304afBzo7sBks9KsPKHDKspLtctFeaXkOKxD3dG8HKWXfbLw\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 118 | const std::string publicKeyPkcs8 = |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 119 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 120 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 121 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 122 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 123 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 124 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 125 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 126 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 127 | |
| 128 | OBufferStream os1; |
| 129 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 130 | auto pubKey = os1.buf(); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 131 | |
| 132 | PublicKey pKey; |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 133 | pKey.loadPkcs8(pubKey->data(), pubKey->size()); |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 134 | |
| 135 | PrivateKey sKey; |
| 136 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.data()), privateKeyPkcs1.size()); |
| 137 | |
| 138 | OBufferStream os2; |
| 139 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2); |
| 140 | auto sig = os2.buf(); |
| 141 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 142 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error); |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 143 | |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 144 | bool result = false; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 145 | bufferSource(data, sizeof(data)) >> |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 146 | verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >> |
Davide Pesavento | 6158f47 | 2017-08-11 18:55:09 -0400 | [diff] [blame] | 147 | boolSink(result); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 148 | |
| 149 | BOOST_CHECK_EQUAL(result, true); |
| 150 | } |
| 151 | |
Davide Pesavento | 8a14b9b | 2017-09-17 01:26:06 -0400 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(InvalidKey) |
| 153 | { |
| 154 | PublicKey pKey; |
| 155 | BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, pKey, nullptr, 0), Error); |
| 156 | } |
| 157 | |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 158 | BOOST_AUTO_TEST_SUITE_END() // TestVerifierFilter |
| 159 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 160 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 161 | |
| 162 | } // namespace tests |
| 163 | } // namespace transform |
| 164 | } // namespace security |
| 165 | } // namespace ndn |