blob: aa2a748fd36da9a33bbecefe835dbfa9822a213d [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
Yingdi Yu41546342014-11-30 23:37:53 -080028#include <boost/filesystem.hpp>
29#include <boost/lexical_cast.hpp>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
31
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070033namespace security {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080034namespace tests {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080035
Yingdi Yu41546342014-11-30 23:37:53 -080036class PibTmpPathFixture
37{
38public:
39 PibTmpPathFixture()
40 {
41 boost::system::error_code error;
42 tmpPath = boost::filesystem::temp_directory_path(error);
43 BOOST_REQUIRE(boost::system::errc::success == error.value());
44 tmpPath /= boost::lexical_cast<std::string>(random::generateWord32());
45 }
46
47 ~PibTmpPathFixture()
48 {
49 boost::filesystem::remove_all(tmpPath);
50 }
51
52public:
53 boost::filesystem::path tmpPath;
54};
55
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080056BOOST_AUTO_TEST_SUITE(SecuritySecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080057
Yingdi Yu40b53092014-06-17 17:10:02 -070058const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
59hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
60sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
61zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
62yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
63y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
64DwIDAQAB");
65const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
661Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
67
Yingdi Yu41546342014-11-30 23:37:53 -080068BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080069{
Yingdi Yu41546342014-11-30 23:37:53 -080070 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071
Yingdi Yu41546342014-11-30 23:37:53 -080072 BOOST_CHECK(pib.doesTableExist("Identity"));
73 BOOST_CHECK(pib.doesTableExist("Key"));
74 BOOST_CHECK(pib.doesTableExist("Certificate"));
75}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
Yingdi Yu41546342014-11-30 23:37:53 -080077BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture)
78{
79 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080080
Yingdi Yu41546342014-11-30 23:37:53 -080081 BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error);
82 pib.addIdentity("/test/id1");
83 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070084
Yingdi Yu41546342014-11-30 23:37:53 -080085 // Pib does not have tpmInfo set yet, setTpmInfo simply set the tpmInfo.
86 std::string tpmLocator("tpm-file:");
87 tpmLocator.append((tmpPath / "tpm").generic_string());
88 pib.setTpmLocator(tpmLocator);
89 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070090
Yingdi Yu41546342014-11-30 23:37:53 -080091 BOOST_REQUIRE_NO_THROW(pib.getTpmLocator());
92 BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070093
Yingdi Yu41546342014-11-30 23:37:53 -080094 // Pib has tpmInfo set, set a different tpmInfo will reset Pib content.
95 std::string tpmLocator3("tpm-osxkeychain:");
96 pib.setTpmLocator(tpmLocator3);
97 BOOST_CHECK(!pib.doesIdentityExist("/test/id1"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080098}
99
Yingdi Yu40b53092014-06-17 17:10:02 -0700100BOOST_AUTO_TEST_CASE(KeyTypeRsa)
101{
102 using namespace CryptoPP;
103
104 OBufferStream os;
105 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
106 true, new Base64Decoder(new FileSink(os)));
107
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700108 shared_ptr<v1::PublicKey> rsaKey;
109 BOOST_REQUIRE_NO_THROW(rsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700110 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700111 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800112 pib.addKey(rsaKeyName, *rsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700113
Yingdi Yu99b2a002015-08-12 12:47:44 -0700114 BOOST_CHECK_EQUAL(KeyType::RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700115
116 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700117}
118
119BOOST_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
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700127 shared_ptr<v1::PublicKey> ecdsaKey;
128 BOOST_REQUIRE_NO_THROW(ecdsaKey = make_shared<v1::PublicKey>(os.buf()->buf(), os.buf()->size()));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700129 Name ecdsaKeyName("/TestSecPublicInfoSqlite3/KeyType/ECDSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700130 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800131 pib.addKey(ecdsaKeyName, *ecdsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700132
Yingdi Yu99b2a002015-08-12 12:47:44 -0700133 BOOST_CHECK_EQUAL(KeyType::EC, pib.getPublicKeyType(ecdsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700134 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/ECDSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700135}
136
137BOOST_AUTO_TEST_CASE(KeyTypeNonExist)
138{
139 Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null");
140 SecPublicInfoSqlite3 pib;
141
Yingdi Yu99b2a002015-08-12 12:47:44 -0700142 BOOST_CHECK_EQUAL(KeyType::NONE, pib.getPublicKeyType(nullKeyName));
Yingdi Yu40b53092014-06-17 17:10:02 -0700143
144}
145
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800146BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800147
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800148} // namespace tests
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700149} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800150} // namespace ndn