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