blob: f7c4136203f5a6a7e93594a8853ec9795fedf6b9 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
Yingdi Yu40b53092014-06-17 17:10:02 -070024#include "security/cryptopp.hpp"
25#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 {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080033
Yingdi Yu41546342014-11-30 23:37:53 -080034class PibTmpPathFixture
35{
36public:
37 PibTmpPathFixture()
38 {
39 boost::system::error_code error;
40 tmpPath = boost::filesystem::temp_directory_path(error);
41 BOOST_REQUIRE(boost::system::errc::success == error.value());
42 tmpPath /= boost::lexical_cast<std::string>(random::generateWord32());
43 }
44
45 ~PibTmpPathFixture()
46 {
47 boost::filesystem::remove_all(tmpPath);
48 }
49
50public:
51 boost::filesystem::path tmpPath;
52};
53
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070054BOOST_AUTO_TEST_SUITE(SecurityTestSecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080055
Yingdi Yu40b53092014-06-17 17:10:02 -070056const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
57hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
58sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
59zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
60yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
61y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
62DwIDAQAB");
63const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
641Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
65
Yingdi Yu41546342014-11-30 23:37:53 -080066BOOST_FIXTURE_TEST_CASE(Basic, PibTmpPathFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080067{
Yingdi Yu41546342014-11-30 23:37:53 -080068 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080069
Yingdi Yu41546342014-11-30 23:37:53 -080070 BOOST_CHECK(pib.doesTableExist("Identity"));
71 BOOST_CHECK(pib.doesTableExist("Key"));
72 BOOST_CHECK(pib.doesTableExist("Certificate"));
73}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074
Yingdi Yu41546342014-11-30 23:37:53 -080075BOOST_FIXTURE_TEST_CASE(TpmLocatorTest, PibTmpPathFixture)
76{
77 SecPublicInfoSqlite3 pib(tmpPath.generic_string());
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080078
Yingdi Yu41546342014-11-30 23:37:53 -080079 BOOST_REQUIRE_THROW(pib.getTpmLocator(), SecPublicInfo::Error);
80 pib.addIdentity("/test/id1");
81 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082
Yingdi Yu41546342014-11-30 23:37:53 -080083 // Pib does not have tpmInfo set yet, setTpmInfo simply set the tpmInfo.
84 std::string tpmLocator("tpm-file:");
85 tpmLocator.append((tmpPath / "tpm").generic_string());
86 pib.setTpmLocator(tpmLocator);
87 BOOST_CHECK(pib.doesIdentityExist("/test/id1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070088
Yingdi Yu41546342014-11-30 23:37:53 -080089 BOOST_REQUIRE_NO_THROW(pib.getTpmLocator());
90 BOOST_CHECK_EQUAL(tpmLocator, pib.getTpmLocator());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070091
Yingdi Yu41546342014-11-30 23:37:53 -080092 // Pib has tpmInfo set, set a different tpmInfo will reset Pib content.
93 std::string tpmLocator3("tpm-osxkeychain:");
94 pib.setTpmLocator(tpmLocator3);
95 BOOST_CHECK(!pib.doesIdentityExist("/test/id1"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080096}
97
Yingdi Yu40b53092014-06-17 17:10:02 -070098BOOST_AUTO_TEST_CASE(KeyTypeRsa)
99{
100 using namespace CryptoPP;
101
102 OBufferStream os;
103 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
104 true, new Base64Decoder(new FileSink(os)));
105
106 shared_ptr<PublicKey> rsaKey;
107 BOOST_REQUIRE_NO_THROW(rsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
108 os.buf()->size())));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700109 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700110 SecPublicInfoSqlite3 pib;
Yingdi Yu41546342014-11-30 23:37:53 -0800111 pib.addKey(rsaKeyName, *rsaKey);
Yingdi Yu40b53092014-06-17 17:10:02 -0700112
113 BOOST_CHECK_EQUAL(KEY_TYPE_RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700114
115 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700116}
117
118BOOST_AUTO_TEST_CASE(KeyTypeEcdsa)
119{
120 using namespace CryptoPP;
121
122 OBufferStream os;
123 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
124 true, new Base64Decoder(new FileSink(os)));
125
126 shared_ptr<PublicKey> ecdsaKey;
127 BOOST_REQUIRE_NO_THROW(ecdsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
128 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
133 BOOST_CHECK_EQUAL(KEY_TYPE_ECDSA, 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
142 BOOST_CHECK_EQUAL(KEY_TYPE_NULL, pib.getPublicKeyType(nullKeyName));
143
144}
145
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800146BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800147
148} // namespace ndn