blob: 245babc9d70db75d6adb16fe9e1973e111895346 [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/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 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
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-public-info-sqlite3.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080023#include "security/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 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080035namespace tests {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080036
Yingdi Yu41546342014-11-30 23:37:53 -080037class PibTmpPathFixture
38{
39public:
40 PibTmpPathFixture()
41 {
42 boost::system::error_code error;
43 tmpPath = boost::filesystem::temp_directory_path(error);
44 BOOST_REQUIRE(boost::system::errc::success == error.value());
45 tmpPath /= boost::lexical_cast<std::string>(random::generateWord32());
46 }
47
48 ~PibTmpPathFixture()
49 {
50 boost::filesystem::remove_all(tmpPath);
51 }
52
53public:
54 boost::filesystem::path tmpPath;
55};
56
Davide Pesaventoeee3e822016-11-26 19:19:34 +010057BOOST_AUTO_TEST_SUITE(Security)
58BOOST_AUTO_TEST_SUITE(TestSecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080059
Yingdi Yu40b53092014-06-17 17:10:02 -070060const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
61hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
62sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
63zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
64yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
65y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
66DwIDAQAB");
67const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
681Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
69
Yingdi Yu41546342014-11-30 23:37:53 -080070BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071{
Yingdi Yu41546342014-11-30 23:37:53 -080072 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080073
Yingdi Yu41546342014-11-30 23:37:53 -080074 BOOST_CHECK(pib.doesTableExist("Identity"));
75 BOOST_CHECK(pib.doesTableExist("Key"));
76 BOOST_CHECK(pib.doesTableExist("Certificate"));
77}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078
Yingdi Yu41546342014-11-30 23:37:53 -080079BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture)
80{
81 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080082
Yingdi Yu41546342014-11-30 23:37:53 -080083 BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error);
84 pib.addIdentity("/test/id1");
85 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086
Yingdi Yu41546342014-11-30 23:37:53 -080087 // Pib does not have tpmInfo set yet, setTpmInfo simply set the tpmInfo.
88 std::string tpmLocator("tpm-file:");
89 tpmLocator.append((tmpPath / "tpm").generic_string());
90 pib.setTpmLocator(tpmLocator);
91 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070092
Yingdi Yu41546342014-11-30 23:37:53 -080093 BOOST_REQUIRE_NO_THROW(pib.getTpmLocator());
94 BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095
Yingdi Yu41546342014-11-30 23:37:53 -080096 // Pib has tpmInfo set, set a different tpmInfo will reset Pib content.
97 std::string tpmLocator3("tpm-osxkeychain:");
98 pib.setTpmLocator(tpmLocator3);
99 BOOST_CHECK(!pib.doesIdentityExist("/test/id1"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800100}
101
Yingdi Yu40b53092014-06-17 17:10:02 -0700102BOOST_AUTO_TEST_CASE(KeyTypeRsa)
103{
104 using namespace CryptoPP;
105
106 OBufferStream os;
107 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
108 true, new Base64Decoder(new FileSink(os)));
109
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700110 shared_ptr<v1::PublicKey> rsaKey;
111 BOOST_REQUIRE_NO_THROW(rsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700112 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700113 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800114 pib.addKey(rsaKeyName, *rsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700115
Yingdi Yu99b2a002015-08-12 12:47:44 -0700116 BOOST_CHECK_EQUAL(KeyType::RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700117
118 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700119}
120
121BOOST_AUTO_TEST_CASE(KeyTypeEcdsa)
122{
123 using namespace CryptoPP;
124
125 OBufferStream os;
126 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
127 true, new Base64Decoder(new FileSink(os)));
128
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700129 shared_ptr<v1::PublicKey> ecdsaKey;
130 BOOST_REQUIRE_NO_THROW(ecdsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700131 Name ecdsaKeyName("/TestSecPublicInfoSqlite3/KeyType/ECDSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700132 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800133 pib.addKey(ecdsaKeyName, *ecdsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700134
Yingdi Yu99b2a002015-08-12 12:47:44 -0700135 BOOST_CHECK_EQUAL(KeyType::EC, pib.getPublicKeyType(ecdsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700136 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/ECDSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700137}
138
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100139BOOST_AUTO_TEST_CASE(KeyTypeNonExistent)
Yingdi Yu40b53092014-06-17 17:10:02 -0700140{
141 Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null");
142 SecPublicInfoSqlite3 pib;
143
Yingdi Yu99b2a002015-08-12 12:47:44 -0700144 BOOST_CHECK_EQUAL(KeyType::NONE, pib.getPublicKeyType(nullKeyName));
Yingdi Yu40b53092014-06-17 17:10:02 -0700145}
146
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100147BOOST_AUTO_TEST_SUITE_END() // TestSecPublicInfoSqlite3
148BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800149
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800150} // namespace tests
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700151} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800152} // namespace ndn