Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | 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" |
| 23 | #include "security/transform.hpp" |
| 24 | #include "encoding/buffer-stream.hpp" |
| 25 | |
| 26 | // TODO: remove CryptoPP dependency |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 27 | #include "security/v1/cryptopp.hpp" |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 28 | |
| 29 | #include "boost-test.hpp" |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | namespace transform { |
| 34 | namespace tests { |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Security) |
| 37 | BOOST_AUTO_TEST_SUITE(Transform) |
| 38 | BOOST_AUTO_TEST_SUITE(TestSignerFilter) |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(Rsa) |
| 41 | { |
| 42 | std::string publicKeyPkcs8 = |
| 43 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 44 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 45 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 46 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 47 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 48 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 49 | "ywIDAQAB\n"; |
| 50 | |
| 51 | std::string privateKeyPkcs1 = |
| 52 | "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n" |
| 53 | "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n" |
| 54 | "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n" |
| 55 | "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n" |
| 56 | "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n" |
| 57 | "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n" |
| 58 | "W8LFv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Zq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n" |
| 73 | "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n" |
| 74 | "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n" |
| 75 | "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n" |
| 76 | "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n"; |
| 77 | |
| 78 | uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
| 79 | |
| 80 | OBufferStream os1; |
| 81 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
| 82 | ConstBufferPtr publicKeyBuffer = os1.buf(); |
| 83 | |
| 84 | PrivateKey sKey; |
| 85 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.c_str()), |
| 86 | privateKeyPkcs1.size()); |
| 87 | |
| 88 | OBufferStream os3; |
| 89 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os3); |
| 90 | ConstBufferPtr sig = os3.buf(); |
| 91 | |
| 92 | // TODO: remove CryptoPP dependency |
| 93 | { |
| 94 | using namespace CryptoPP; |
| 95 | |
| 96 | CryptoPP::RSA::PublicKey publicKey; |
| 97 | |
| 98 | ByteQueue keyQueue1; |
| 99 | keyQueue1.LazyPut(publicKeyBuffer->buf(), publicKeyBuffer->size()); |
| 100 | publicKey.Load(keyQueue1); |
| 101 | |
| 102 | // For signature, openssl only support pkcs1v15 padding. |
| 103 | RSASS<PKCS1v15, CryptoPP::SHA256>::Verifier verifier(publicKey); |
| 104 | BOOST_CHECK(verifier.VerifyMessage(data, sizeof(data), sig->buf(), sig->size())); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(Ecdsa) |
| 109 | { |
| 110 | std::string privateKeyPkcs1 = |
| 111 | "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n" |
| 112 | "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n" |
| 113 | "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n" |
| 114 | "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n" |
| 115 | "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n" |
| 116 | "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n" |
| 117 | "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n" |
| 118 | "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n"; |
| 119 | |
| 120 | std::string publicKeyPkcs8 = |
| 121 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 122 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 123 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 124 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 125 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 126 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 127 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
| 128 | |
| 129 | uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
| 130 | |
| 131 | OBufferStream os1; |
| 132 | bufferSource(publicKeyPkcs8) >> base64Decode() >> streamSink(os1); |
| 133 | ConstBufferPtr publicKeyBuffer = os1.buf(); |
| 134 | |
| 135 | PrivateKey sKey; |
| 136 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(privateKeyPkcs1.c_str()), |
| 137 | privateKeyPkcs1.size()); |
| 138 | |
| 139 | OBufferStream os3; |
| 140 | bufferSource(data, sizeof(data)) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os3); |
| 141 | ConstBufferPtr sig = os3.buf(); |
| 142 | |
| 143 | // TODO: remove CryptoPP dependency |
| 144 | { |
| 145 | using namespace CryptoPP; |
| 146 | |
| 147 | ECDSA<ECP, CryptoPP::SHA256>::PublicKey publicKey; |
| 148 | ByteQueue keyQueue1; |
| 149 | keyQueue1.LazyPut(publicKeyBuffer->buf(), publicKeyBuffer->size()); |
| 150 | publicKey.Load(keyQueue1); |
| 151 | |
| 152 | // For signature, openssl only support pkcs1v15 padding. |
| 153 | ECDSA<ECP, CryptoPP::SHA256>::Verifier verifier(publicKey); |
| 154 | |
| 155 | uint8_t buffer[64]; |
| 156 | size_t usedSize = DSAConvertSignatureFormat(buffer, sizeof(buffer), DSA_P1363, |
| 157 | sig->buf(), sig->size(), DSA_DER); |
| 158 | BOOST_CHECK(verifier.VerifyMessage(data, sizeof(data), buffer, usedSize)); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_SUITE_END() // TestSignerFilter |
| 163 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 164 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 165 | |
| 166 | } // namespace tests |
| 167 | } // namespace transform |
| 168 | } // namespace security |
| 169 | } // namespace ndn |