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 | a84f464 | 2017-08-23 16:14:51 -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/private-key.hpp" |
| 23 | #include "security/transform.hpp" |
| 24 | #include "security/key-params.hpp" |
| 25 | #include "encoding/buffer-stream.hpp" |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 28 | #include <boost/mpl/vector.hpp> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 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(TestPrivateKey) |
| 40 | |
| 41 | class RsaKeyTestData |
| 42 | { |
| 43 | public: |
| 44 | RsaKeyTestData() |
| 45 | { |
| 46 | privateKeyPkcs1 = |
| 47 | "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n" |
| 48 | "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n" |
| 49 | "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n" |
| 50 | "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n" |
| 51 | "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n" |
| 52 | "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n" |
| 53 | "W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr\n" |
| 54 | "cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV\n" |
| 55 | "Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b\n" |
| 56 | "wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg\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cs8 = |
| 103 | "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n" |
| 104 | "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n" |
| 105 | "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n" |
| 106 | "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n" |
| 107 | "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n" |
| 108 | "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n" |
| 109 | "ywIDAQAB\n"; |
| 110 | } |
| 111 | |
| 112 | public: |
| 113 | std::string privateKeyPkcs1; |
| 114 | std::string privateKeyPkcs8; |
| 115 | std::string publicKeyPkcs8; |
| 116 | }; |
| 117 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 118 | class EcKeyTestData |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 119 | { |
| 120 | public: |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 121 | EcKeyTestData() |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 122 | { |
| 123 | privateKeyPkcs1 = |
| 124 | "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n" |
| 125 | "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n" |
| 126 | "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n" |
| 127 | "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n" |
| 128 | "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n" |
| 129 | "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n" |
| 130 | "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n" |
| 131 | "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n"; |
| 132 | |
| 133 | privateKeyPkcs8 = |
| 134 | "MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA\n" |
| 135 | "MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA\n" |
| 136 | "MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO\n" |
| 137 | "SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594\n" |
| 138 | "ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr\n" |
| 139 | "akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd\n" |
| 140 | "2OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN\n" |
| 141 | "6cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG\n" |
| 142 | "HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk\n" |
| 143 | "q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=\n"; |
| 144 | |
| 145 | publicKeyPkcs8 = |
| 146 | "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n" |
| 147 | "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n" |
| 148 | "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n" |
| 149 | "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n" |
| 150 | "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n" |
| 151 | "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n" |
| 152 | "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n"; |
| 153 | } |
| 154 | |
| 155 | public: |
| 156 | std::string privateKeyPkcs1; |
| 157 | std::string privateKeyPkcs8; |
| 158 | std::string publicKeyPkcs8; |
| 159 | }; |
| 160 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 161 | using KeyTestDataSets = boost::mpl::vector<RsaKeyTestData, EcKeyTestData>; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 162 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 163 | static void |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 164 | checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1) |
| 165 | { |
| 166 | PrivateKey sKey; |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 167 | sKey.loadPkcs8(encoding->buf(), encoding->size(), password.c_str(), password.size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 168 | OBufferStream os; |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 169 | sKey.savePkcs1(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 170 | ConstBufferPtr keyPkcs1Str = os.buf(); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 171 | BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(), |
| 172 | keyPkcs1Str->begin(), keyPkcs1Str->end()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 175 | static void |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 176 | checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1) |
| 177 | { |
| 178 | OBufferStream os; |
| 179 | bufferSource(*encoding) >> base64Decode() >> streamSink(os); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 180 | checkPkcs8Encoding(os.buf(), password, pkcs1); |
| 181 | } |
| 182 | |
| 183 | BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets) |
| 184 | { |
| 185 | T dataSet; |
| 186 | |
| 187 | const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()); |
| 188 | size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size(); |
| 189 | |
| 190 | OBufferStream os; |
| 191 | bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os); |
| 192 | ConstBufferPtr sKeyPkcs1Buf = os.buf(); |
| 193 | const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->buf(); |
| 194 | size_t sKeyPkcs1Len = sKeyPkcs1Buf->size(); |
| 195 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 196 | // load key in base64-encoded pkcs1 format |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 197 | PrivateKey sKey; |
| 198 | BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len)); |
| 199 | |
| 200 | std::stringstream ss2(dataSet.privateKeyPkcs1); |
| 201 | PrivateKey sKey2; |
| 202 | BOOST_REQUIRE_NO_THROW(sKey2.loadPkcs1Base64(ss2)); |
| 203 | |
| 204 | // load key in pkcs1 format |
| 205 | PrivateKey sKey3; |
| 206 | BOOST_REQUIRE_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len)); |
| 207 | |
| 208 | std::stringstream ss4; |
| 209 | ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len); |
| 210 | PrivateKey sKey4; |
| 211 | BOOST_REQUIRE_NO_THROW(sKey4.loadPkcs1(ss4)); |
| 212 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 213 | // save key in base64-encoded pkcs1 format |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 214 | OBufferStream os2; |
| 215 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2)); |
| 216 | ConstBufferPtr keyPkcs1Base64Str = os2.buf(); |
| 217 | BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len, |
| 218 | keyPkcs1Base64Str->begin(), keyPkcs1Base64Str->end()); |
| 219 | |
| 220 | // save key in pkcs1 format |
| 221 | OBufferStream os3; |
| 222 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3)); |
| 223 | ConstBufferPtr keyPkcs1Str = os3.buf(); |
| 224 | BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len, |
| 225 | keyPkcs1Str->begin(), keyPkcs1Str->end()); |
| 226 | |
| 227 | |
| 228 | |
| 229 | const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.c_str()); |
| 230 | size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size(); |
| 231 | |
| 232 | OBufferStream os4; |
| 233 | bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4); |
| 234 | ConstBufferPtr sKeyPkcs8Buf = os4.buf(); |
| 235 | const uint8_t* sKeyPkcs8 = sKeyPkcs8Buf->buf(); |
| 236 | size_t sKeyPkcs8Len = sKeyPkcs8Buf->size(); |
| 237 | |
| 238 | std::string password("password"); |
| 239 | std::string wrongpw("wrongpw"); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 240 | auto pwCallback = [&password] (char* buf, size_t size, int) -> int { |
| 241 | BOOST_REQUIRE_LE(password.size(), size); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 242 | std::copy(password.begin(), password.end(), buf); |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 243 | return static_cast<int>(password.size()); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 246 | // load key in base64-encoded pkcs8 format |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 247 | PrivateKey sKey5; |
| 248 | BOOST_REQUIRE_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, |
| 249 | password.c_str(), password.size())); |
| 250 | |
| 251 | PrivateKey sKey6; |
| 252 | BOOST_REQUIRE_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback)); |
| 253 | |
| 254 | std::stringstream ss7(dataSet.privateKeyPkcs8); |
| 255 | PrivateKey sKey7; |
| 256 | BOOST_REQUIRE_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.c_str(), password.size())); |
| 257 | |
| 258 | std::stringstream ss8(dataSet.privateKeyPkcs8); |
| 259 | PrivateKey sKey8; |
| 260 | BOOST_REQUIRE_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback)); |
| 261 | |
| 262 | // load key in pkcs8 format |
| 263 | PrivateKey sKey9; |
| 264 | BOOST_REQUIRE_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, |
| 265 | password.c_str(), password.size())); |
| 266 | |
| 267 | PrivateKey sKey10; |
| 268 | BOOST_REQUIRE_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback)); |
| 269 | |
| 270 | std::stringstream ss11; |
| 271 | ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len); |
| 272 | PrivateKey sKey11; |
| 273 | BOOST_REQUIRE_NO_THROW(sKey11.loadPkcs8(ss11, password.c_str(), password.size())); |
| 274 | |
| 275 | std::stringstream ss12; |
| 276 | ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len); |
| 277 | PrivateKey sKey12; |
| 278 | BOOST_REQUIRE_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback)); |
| 279 | |
| 280 | // load key using wrong password, Error is expected. |
| 281 | PrivateKey sKey13; |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 282 | BOOST_CHECK_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, |
| 283 | wrongpw.c_str(), wrongpw.size()), |
| 284 | PrivateKey::Error); |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 285 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 286 | // save key in base64-encoded pkcs8 format |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 287 | OBufferStream os14; |
| 288 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.c_str(), password.size())); |
| 289 | ConstBufferPtr encoded14 = os14.buf(); |
| 290 | checkPkcs8Base64Encoding(encoded14, password, sKeyPkcs1Buf); |
| 291 | |
| 292 | OBufferStream os15; |
| 293 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback)); |
| 294 | ConstBufferPtr encoded15 = os15.buf(); |
| 295 | checkPkcs8Base64Encoding(encoded15, password, sKeyPkcs1Buf); |
| 296 | |
| 297 | // save key in pkcs8 format |
| 298 | OBufferStream os16; |
| 299 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.c_str(), password.size())); |
| 300 | ConstBufferPtr encoded16 = os16.buf(); |
| 301 | checkPkcs8Encoding(encoded16, password, sKeyPkcs1Buf); |
| 302 | |
| 303 | OBufferStream os17; |
| 304 | BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback)); |
| 305 | ConstBufferPtr encoded17 = os17.buf(); |
| 306 | checkPkcs8Encoding(encoded17, password, sKeyPkcs1Buf); |
| 307 | } |
| 308 | |
| 309 | BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets) |
| 310 | { |
| 311 | T dataSet; |
| 312 | |
| 313 | const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()); |
| 314 | size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size(); |
| 315 | |
| 316 | PrivateKey sKey; |
| 317 | BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len)); |
| 318 | |
| 319 | // derive public key and compare |
| 320 | ConstBufferPtr pKeyBits= sKey.derivePublicKey(); |
| 321 | OBufferStream os; |
| 322 | bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os); |
| 323 | BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(), |
| 324 | os.buf()->begin(), os.buf()->end()); |
| 325 | } |
| 326 | |
| 327 | BOOST_AUTO_TEST_CASE(RsaDecryption) |
| 328 | { |
| 329 | RsaKeyTestData dataSet; |
| 330 | |
| 331 | PrivateKey sKey; |
| 332 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()), |
| 333 | dataSet.privateKeyPkcs1.size()); |
| 334 | |
| 335 | const uint8_t plainText[] = { |
| 336 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 337 | }; |
| 338 | |
| 339 | const std::string cipherTextBase64 = |
| 340 | "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n" |
| 341 | "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n" |
| 342 | "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n" |
| 343 | "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n" |
| 344 | "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n" |
| 345 | "PmTiSdeDQXZ94U5boDQ4Aw==\n"; |
| 346 | |
| 347 | OBufferStream os; |
| 348 | bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os); |
| 349 | |
| 350 | ConstBufferPtr decryptText = sKey.decrypt(os.buf()->buf(), os.buf()->size()); |
| 351 | |
| 352 | BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText), |
| 353 | decryptText->begin(), decryptText->end()); |
| 354 | } |
| 355 | |
| 356 | BOOST_AUTO_TEST_CASE(RsaEncryption) |
| 357 | { |
| 358 | RsaKeyTestData dataSet; |
| 359 | |
| 360 | PublicKey pKey; |
| 361 | pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()), |
| 362 | dataSet.publicKeyPkcs8.size()); |
| 363 | |
| 364 | PrivateKey sKey; |
| 365 | sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()), |
| 366 | dataSet.privateKeyPkcs1.size()); |
| 367 | |
| 368 | const uint8_t plainText[] = { |
| 369 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 370 | }; |
| 371 | |
| 372 | ConstBufferPtr cipherText = pKey.encrypt(plainText, sizeof(plainText)); |
| 373 | ConstBufferPtr decryptText = sKey.decrypt(cipherText->buf(), cipherText->size()); |
| 374 | |
| 375 | BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText), |
| 376 | decryptText->begin(), decryptText->end()); |
| 377 | } |
| 378 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 379 | using KeyParams = boost::mpl::vector<RsaKeyParams, EcKeyParams>; |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 380 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 381 | BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, KeyParams) |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 382 | { |
| 383 | BOOST_REQUIRE_NO_THROW(generatePrivateKey(T())); |
| 384 | |
| 385 | unique_ptr<PrivateKey> sKey = generatePrivateKey(T()); |
| 386 | PublicKey pKey; |
| 387 | ConstBufferPtr pKeyBits = sKey->derivePublicKey(); |
| 388 | pKey.loadPkcs8(pKeyBits->buf(), pKeyBits->size()); |
| 389 | |
Luca Keidel | 941fd8c | 2017-07-24 15:21:22 +0200 | [diff] [blame^] | 390 | const uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 391 | OBufferStream os; |
| 392 | BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >> |
| 393 | signerFilter(DigestAlgorithm::SHA256, *sKey) >> |
| 394 | streamSink(os)); |
| 395 | |
| 396 | ConstBufferPtr sig = os.buf(); |
| 397 | bool result = false; |
| 398 | BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >> |
| 399 | verifierFilter(DigestAlgorithm::SHA256, pKey, sig->buf(), sig->size()) >> |
| 400 | boolSink(result)); |
Yingdi Yu | b263f15 | 2015-07-12 16:50:13 -0700 | [diff] [blame] | 401 | BOOST_CHECK(result); |
| 402 | |
Yingdi Yu | 202a2e9 | 2015-07-12 16:49:25 -0700 | [diff] [blame] | 403 | unique_ptr<PrivateKey> sKey2 = generatePrivateKey(T()); |
| 404 | |
| 405 | OBufferStream os1; |
| 406 | sKey->savePkcs1(os1); |
| 407 | ConstBufferPtr key1Pkcs1 = os1.buf(); |
| 408 | |
| 409 | OBufferStream os2; |
| 410 | sKey2->savePkcs1(os2); |
| 411 | ConstBufferPtr key2Pkcs1 = os2.buf(); |
| 412 | |
| 413 | BOOST_CHECK(*key1Pkcs1 != *key2Pkcs1); |
| 414 | } |
| 415 | |
| 416 | BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey |
| 417 | BOOST_AUTO_TEST_SUITE_END() // Transform |
| 418 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 419 | |
| 420 | } // namespace tests |
| 421 | } // namespace transform |
| 422 | } // namespace security |
| 423 | } // namespace ndn |