blob: e2d668bd95babab9e440e8008247ccce757c4d9a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -08002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu28fd32f2014-01-28 19:03:03 -080020 */
21
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080022#include "security/v1/sec-public-info-sqlite3.hpp"
23#include "security/v1/key-chain.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070024#include "security/v1/cryptopp.hpp"
Yingdi Yu40b53092014-06-17 17:10:02 -070025#include "encoding/buffer-stream.hpp"
Yingdi Yu9a335352014-01-31 11:57:46 -080026#include "util/time.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080027
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include "boost-test.hpp"
29
Yingdi Yu41546342014-11-30 23:37:53 -080030#include <boost/filesystem.hpp>
31#include <boost/lexical_cast.hpp>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070032
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080033namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070034namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080035namespace v1 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080036namespace tests {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037
Yingdi Yu41546342014-11-30 23:37:53 -080038class PibTmpPathFixture
39{
40public:
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
54public:
55 boost::filesystem::path tmpPath;
56};
57
Davide Pesaventoeee3e822016-11-26 19:19:34 +010058BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080059BOOST_AUTO_TEST_SUITE(V1)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060BOOST_AUTO_TEST_SUITE(TestSecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080061
Yingdi Yu40b53092014-06-17 17:10:02 -070062const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
63hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
64sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
65zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
66yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
67y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
68DwIDAQAB");
69const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
701Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
71
Yingdi Yu41546342014-11-30 23:37:53 -080072BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080073{
Yingdi Yu41546342014-11-30 23:37:53 -080074 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080075
Yingdi Yu41546342014-11-30 23:37:53 -080076 BOOST_CHECK(pib.doesTableExist("Identity"));
77 BOOST_CHECK(pib.doesTableExist("Key"));
78 BOOST_CHECK(pib.doesTableExist("Certificate"));
79}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080
Yingdi Yu41546342014-11-30 23:37:53 -080081BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture)
82{
83 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080084
Yingdi Yu41546342014-11-30 23:37:53 -080085 BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error);
86 pib.addIdentity("/test/id1");
87 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070088
Yingdi Yu41546342014-11-30 23:37:53 -080089 // 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 Afanasyevb1db7c62014-04-03 14:57:25 -070094
Yingdi Yu41546342014-11-30 23:37:53 -080095 BOOST_REQUIRE_NO_THROW(pib.getTpmLocator());
96 BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070097
Yingdi Yu41546342014-11-30 23:37:53 -080098 // 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 Yu28fd32f2014-01-28 19:03:03 -0800102}
103
Yingdi Yu40b53092014-06-17 17:10:02 -0700104BOOST_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 Afanasyev2fa59392016-07-29 17:24:23 -0700112 shared_ptr<v1::PublicKey> rsaKey;
113 BOOST_REQUIRE_NO_THROW(rsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700114 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700115 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800116 pib.addKey(rsaKeyName, *rsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700117
Yingdi Yu99b2a002015-08-12 12:47:44 -0700118 BOOST_CHECK_EQUAL(KeyType::RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700119
120 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700121}
122
123BOOST_AUTO_TEST_CASE(KeyTypeEcdsa)
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
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700131 shared_ptr<v1::PublicKey> ecdsaKey;
132 BOOST_REQUIRE_NO_THROW(ecdsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700133 Name ecdsaKeyName("/TestSecPublicInfoSqlite3/KeyType/ECDSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700134 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800135 pib.addKey(ecdsaKeyName, *ecdsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700136
Yingdi Yu99b2a002015-08-12 12:47:44 -0700137 BOOST_CHECK_EQUAL(KeyType::EC, pib.getPublicKeyType(ecdsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700138 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/ECDSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700139}
140
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100141BOOST_AUTO_TEST_CASE(KeyTypeNonExistent)
Yingdi Yu40b53092014-06-17 17:10:02 -0700142{
143 Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null");
144 SecPublicInfoSqlite3 pib;
145
Yingdi Yu99b2a002015-08-12 12:47:44 -0700146 BOOST_CHECK_EQUAL(KeyType::NONE, pib.getPublicKeyType(nullKeyName));
Yingdi Yu40b53092014-06-17 17:10:02 -0700147}
148
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100149BOOST_AUTO_TEST_SUITE_END() // TestSecPublicInfoSqlite3
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800150BOOST_AUTO_TEST_SUITE_END() // V1
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100151BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800152
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800153} // namespace tests
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800154} // namespace v1
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700155} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800156} // namespace ndn