Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 22 | #include "security/v1/sec-public-info-sqlite3.hpp" |
| 23 | #include "security/v1/key-chain.hpp" |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 24 | #include "security/v1/cryptopp.hpp" |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 25 | #include "encoding/buffer-stream.hpp" |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 26 | #include "util/time.hpp" |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 28 | #include "boost-test.hpp" |
| 29 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
| 31 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 33 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 34 | namespace security { |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 35 | namespace v1 { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 36 | namespace tests { |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 38 | class PibTmpPathFixture |
| 39 | { |
| 40 | public: |
| 41 | PibTmpPathFixture() |
| 42 | { |
| 43 | boost::system::error_code error; |
| 44 | tmpPath = boost::filesystem::temp_directory_path(error); |
| 45 | BOOST_REQUIRE(boost::system::errc::success == error.value()); |
| 46 | tmpPath /= boost::lexical_cast<std::string>(random::generateWord32()); |
| 47 | } |
| 48 | |
| 49 | ~PibTmpPathFixture() |
| 50 | { |
| 51 | boost::filesystem::remove_all(tmpPath); |
| 52 | } |
| 53 | |
| 54 | public: |
| 55 | boost::filesystem::path tmpPath; |
| 56 | }; |
| 57 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 58 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 59 | BOOST_AUTO_TEST_SUITE(V1) |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 60 | BOOST_AUTO_TEST_SUITE(TestSecPublicInfoSqlite3) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 61 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 62 | const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\ |
| 63 | hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\ |
| 64 | sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\ |
| 65 | zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\ |
| 66 | yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\ |
| 67 | y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\ |
| 68 | DwIDAQAB"); |
| 69 | const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\ |
| 70 | 1Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg=="); |
| 71 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 72 | BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 74 | SecPublicInfoSqlite3 pib(tmpPath.generic_string()); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 75 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 76 | BOOST_CHECK(pib.doesTableExist("Identity")); |
| 77 | BOOST_CHECK(pib.doesTableExist("Key")); |
| 78 | BOOST_CHECK(pib.doesTableExist("Certificate")); |
| 79 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 80 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 81 | BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture) |
| 82 | { |
| 83 | SecPublicInfoSqlite3 pib(tmpPath.generic_string()); |
Alexander Afanasyev | 6835ad8 | 2014-02-12 10:07:20 -0800 | [diff] [blame] | 84 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 85 | BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error); |
| 86 | pib.addIdentity("/test/id1"); |
| 87 | BOOST_CHECK(pib.doesIdentityExist("/test/id1")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 88 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 89 | // Pib does not have tpmInfo set yet, setTpmInfo simply set the tpmInfo. |
| 90 | std::string tpmLocator("tpm-file:"); |
| 91 | tpmLocator.append((tmpPath / "tpm").generic_string()); |
| 92 | pib.setTpmLocator(tpmLocator); |
| 93 | BOOST_CHECK(pib.doesIdentityExist("/test/id1")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 95 | BOOST_REQUIRE_NO_THROW(pib.getTpmLocator()); |
| 96 | BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 98 | // Pib has tpmInfo set, set a different tpmInfo will reset Pib content. |
| 99 | std::string tpmLocator3("tpm-osxkeychain:"); |
| 100 | pib.setTpmLocator(tpmLocator3); |
| 101 | BOOST_CHECK(!pib.doesIdentityExist("/test/id1")); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(KeyTypeRsa) |
| 105 | { |
| 106 | using namespace CryptoPP; |
| 107 | |
| 108 | OBufferStream os; |
| 109 | StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(), |
| 110 | true, new Base64Decoder(new FileSink(os))); |
| 111 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 112 | shared_ptr<v1::PublicKey> rsaKey; |
| 113 | BOOST_REQUIRE_NO_THROW(rsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size())); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 114 | Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123"); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 115 | SecPublicInfoSqlite3 pib; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 116 | pib.addKey(rsaKeyName, *rsaKey); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(KeyType::RSA, pib.getPublicKeyType(rsaKeyName)); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 119 | |
| 120 | pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA")); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(KeyTypeEc) |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 124 | { |
| 125 | using namespace CryptoPP; |
| 126 | |
| 127 | OBufferStream os; |
| 128 | StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(), |
| 129 | true, new Base64Decoder(new FileSink(os))); |
| 130 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 131 | shared_ptr<v1::PublicKey> ecKey; |
| 132 | BOOST_REQUIRE_NO_THROW(ecKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size())); |
| 133 | Name ecKeyName("/TestSecPublicInfoSqlite3/KeyType/EC/ksk-123"); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 134 | SecPublicInfoSqlite3 pib; |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 135 | pib.addKey(ecKeyName, *ecKey); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 136 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(KeyType::EC, pib.getPublicKeyType(ecKeyName)); |
| 138 | pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/EC")); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(KeyTypeNonExistent) |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 142 | { |
| 143 | Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null"); |
| 144 | SecPublicInfoSqlite3 pib; |
| 145 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(KeyType::NONE, pib.getPublicKeyType(nullKeyName)); |
Yingdi Yu | 40b5309 | 2014-06-17 17:10:02 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 149 | BOOST_AUTO_TEST_SUITE_END() // TestSecPublicInfoSqlite3 |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 150 | BOOST_AUTO_TEST_SUITE_END() // V1 |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 151 | BOOST_AUTO_TEST_SUITE_END() // Security |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 152 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 153 | } // namespace tests |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 154 | } // namespace v1 |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 155 | } // namespace security |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 156 | } // namespace ndn |