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 | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -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 | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 22 | #include "security/key-chain.hpp" |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 23 | #include "security/validator.hpp" |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 24 | #include "../util/test-home-environment-fixture.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 28 | #include "dummy-keychain.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 30 | namespace ndn { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 31 | namespace security { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 32 | namespace tests { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 33 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_SUITE(SecurityKeyChain, util::TestHomeEnvironmentFixture) |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_CASE(ConstructorNormalConfig) |
| 37 | { |
| 38 | using namespace boost::filesystem; |
| 39 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 40 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 41 | |
| 42 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
| 43 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 44 | KeyChain keyChain; |
| 45 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), |
| 46 | "pib-sqlite3:/tmp/test/ndn-cxx/keychain/sqlite3-file/"); |
| 47 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), |
| 48 | "tpm-file:/tmp/test/ndn-cxx/keychain/sqlite3-file/"); |
| 49 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), |
| 50 | "tpm-file:/tmp/test/ndn-cxx/keychain/sqlite3-file/"); |
| 51 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 52 | path pibPath(absolute(std::getenv("TEST_HOME"))); |
| 53 | pibPath /= ".ndn/ndnsec-public-info.db"; |
| 54 | |
| 55 | boost::filesystem::remove(pibPath); |
| 56 | } |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(ConstructorEmptyConfig) |
| 59 | { |
| 60 | using namespace boost::filesystem; |
| 61 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 62 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-empty-home", 1); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 64 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) |
| 65 | std::string oldHOME; |
| 66 | if (std::getenv("OLD_HOME")) |
| 67 | oldHOME = std::getenv("OLD_HOME"); |
| 68 | |
| 69 | std::string HOME; |
| 70 | if (std::getenv("HOME")) |
| 71 | HOME = std::getenv("HOME"); |
| 72 | |
| 73 | if (!oldHOME.empty()) |
| 74 | setenv("HOME", oldHOME.c_str(), 1); |
| 75 | else |
| 76 | unsetenv("HOME"); |
| 77 | #endif |
| 78 | |
| 79 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 80 | KeyChain keyChain; |
| 81 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), |
| 82 | "pib-sqlite3:/tmp/test/ndn-cxx/keychain/sqlite3-empty/"); |
| 83 | |
| 84 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) |
| 85 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:"); |
| 86 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:"); |
| 87 | #else |
| 88 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), |
| 89 | "tpm-file:"); |
| 90 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), |
| 91 | "tpm-file:"); |
| 92 | #endif |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 93 | |
| 94 | #if defined(NDN_CXX_HAVE_OSX_SECURITY) |
| 95 | if (!HOME.empty()) |
| 96 | setenv("HOME", HOME.c_str(), 1); |
| 97 | else |
| 98 | unsetenv("HOME"); |
| 99 | #endif |
| 100 | |
| 101 | path pibPath(absolute(std::getenv("TEST_HOME"))); |
| 102 | pibPath /= ".ndn/ndnsec-public-info.db"; |
| 103 | |
| 104 | boost::filesystem::remove(pibPath); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(ConstructorEmpty2Config) |
| 108 | { |
| 109 | using namespace boost::filesystem; |
| 110 | |
| 111 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-empty2-home", 1); |
| 112 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 113 | BOOST_REQUIRE_NO_THROW(KeyChain()); |
| 114 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 115 | KeyChain keyChain; |
| 116 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), |
| 117 | "pib-sqlite3:"); |
| 118 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), |
| 119 | "tpm-file:/tmp/test/ndn-cxx/keychain/empty-file/"); |
| 120 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), |
| 121 | "tpm-file:/tmp/test/ndn-cxx/keychain/empty-file/"); |
| 122 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 123 | path pibPath(absolute(std::getenv("TEST_HOME"))); |
| 124 | pibPath /= ".ndn/ndnsec-public-info.db"; |
| 125 | |
| 126 | boost::filesystem::remove(pibPath); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(ConstructorMalConfig) |
| 130 | { |
| 131 | using namespace boost::filesystem; |
| 132 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 133 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-malformed-home", 1); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 134 | |
| 135 | BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(ConstructorMal2Config) |
| 139 | { |
| 140 | using namespace boost::filesystem; |
| 141 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 142 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-malformed2-home", 1); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 143 | |
| 144 | BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected. |
| 145 | } |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(ExportIdentity) |
| 148 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 149 | KeyChain keyChain; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 151 | Name identity("/TestKeyChain/ExportIdentity/"); |
| 152 | identity.appendVersion(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 153 | keyChain.createIdentity(identity); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 155 | shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234"); |
| 156 | |
| 157 | Block block = exported->wireEncode(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 158 | |
| 159 | Name keyName = keyChain.getDefaultKeyNameForIdentity(identity); |
| 160 | Name certName = keyChain.getDefaultCertificateNameForKey(keyName); |
| 161 | |
| 162 | keyChain.deleteIdentity(identity); |
| 163 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false); |
| 165 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName), false); |
| 166 | BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false); |
| 167 | BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false); |
| 168 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName), false); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 169 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 170 | SecuredBag imported; |
| 171 | imported.wireDecode(block); |
| 172 | keyChain.importIdentity(imported, "1234"); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 173 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 174 | BOOST_CHECK(keyChain.doesIdentityExist(identity)); |
| 175 | BOOST_CHECK(keyChain.doesPublicKeyExist(keyName)); |
| 176 | BOOST_CHECK(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)); |
| 177 | BOOST_CHECK(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)); |
| 178 | BOOST_CHECK(keyChain.doesCertificateExist(certName)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 179 | |
| 180 | keyChain.deleteIdentity(identity); |
| 181 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false); |
| 183 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName), false); |
| 184 | BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false); |
| 185 | BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false); |
| 186 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName), false); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 189 | BOOST_AUTO_TEST_CASE(PrepareIdentityCertificate) |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 190 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 191 | KeyChain keyChain; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 193 | Name identity("/TestKeyChain/PrepareIdentityCertificate/"); |
| 194 | identity.appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 195 | keyChain.createIdentity(identity); |
| 196 | |
Junxiao Shi | 8ca4325 | 2015-06-11 21:29:43 -0700 | [diff] [blame] | 197 | std::vector<CertificateSubjectDescription> subjectDescription; |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 198 | Name lowerIdentity = identity; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 199 | lowerIdentity.append("Lower").appendVersion(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 200 | Name lowerKeyName = keyChain.generateRsaKeyPair(lowerIdentity, true); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 201 | shared_ptr<IdentityCertificate> idCert = |
| 202 | keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity, |
| 203 | time::system_clock::now(), |
| 204 | time::system_clock::now() + time::days(365), |
| 205 | subjectDescription); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 206 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(idCert->getName().getPrefix(5), |
| 208 | Name().append(identity).append("KEY").append("Lower")); |
Junxiao Shi | 8ca4325 | 2015-06-11 21:29:43 -0700 | [diff] [blame] | 209 | BOOST_CHECK(idCert->getFreshnessPeriod() >= time::milliseconds::zero()); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 210 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 211 | shared_ptr<IdentityCertificate> idCert11 = |
| 212 | keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity, |
| 213 | time::system_clock::now(), |
| 214 | time::system_clock::now() + time::days(365), |
| 215 | subjectDescription, |
| 216 | lowerIdentity); |
| 217 | BOOST_CHECK(static_cast<bool>(idCert11)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(idCert11->getName().getPrefix(6), |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 219 | Name().append(lowerIdentity).append("KEY")); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 220 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 221 | Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/"); |
| 222 | anotherIdentity.appendVersion(); |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 223 | Name anotherKeyName = keyChain.generateRsaKeyPair(anotherIdentity, true); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 224 | shared_ptr<IdentityCertificate> idCert2 = |
| 225 | keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity, |
| 226 | time::system_clock::now(), |
| 227 | time::system_clock::now() + time::days(365), |
| 228 | subjectDescription); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 229 | BOOST_CHECK(static_cast<bool>(idCert2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(idCert2->getName().getPrefix(5), Name().append(anotherIdentity).append("KEY")); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 231 | |
| 232 | |
| 233 | Name wrongKeyName1; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 234 | shared_ptr<IdentityCertificate> idCert3 = |
| 235 | keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity, |
| 236 | time::system_clock::now(), |
| 237 | time::system_clock::now() + time::days(365), |
| 238 | subjectDescription); |
| 239 | BOOST_CHECK_EQUAL(static_cast<bool>(idCert3), false); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 240 | |
| 241 | |
| 242 | Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate"); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 243 | shared_ptr<IdentityCertificate> idCert4 = |
| 244 | keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity, |
| 245 | time::system_clock::now(), |
| 246 | time::system_clock::now() + time::days(365), |
| 247 | subjectDescription); |
| 248 | BOOST_CHECK_EQUAL(static_cast<bool>(idCert4), false); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 249 | |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 250 | |
| 251 | Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234"); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 252 | shared_ptr<IdentityCertificate> idCert5 = |
| 253 | keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity, |
| 254 | time::system_clock::now(), |
| 255 | time::system_clock::now() + time::days(365), |
| 256 | subjectDescription); |
| 257 | BOOST_CHECK_EQUAL(static_cast<bool>(idCert5), false); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 258 | |
| 259 | keyChain.deleteIdentity(identity); |
| 260 | keyChain.deleteIdentity(lowerIdentity); |
| 261 | keyChain.deleteIdentity(anotherIdentity); |
| 262 | } |
| 263 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 264 | BOOST_AUTO_TEST_CASE(Delete) |
| 265 | { |
| 266 | KeyChain keyChain; |
| 267 | |
| 268 | Name identity("/TestSecPublicInfoSqlite3/Delete"); |
| 269 | identity.appendVersion(); |
| 270 | |
| 271 | Name certName1; |
| 272 | BOOST_REQUIRE_NO_THROW(certName1 = keyChain.createIdentity(identity)); |
| 273 | |
| 274 | Name keyName1 = IdentityCertificate::certificateNameToPublicKeyName(certName1); |
| 275 | Name keyName2; |
| 276 | BOOST_REQUIRE_NO_THROW(keyName2 = keyChain.generateRsaKeyPairAsDefault(identity)); |
| 277 | |
| 278 | shared_ptr<IdentityCertificate> cert2; |
| 279 | BOOST_REQUIRE_NO_THROW(cert2 = keyChain.selfSign(keyName2)); |
| 280 | Name certName2 = cert2->getName(); |
| 281 | BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert2)); |
| 282 | |
| 283 | Name keyName3; |
| 284 | BOOST_REQUIRE_NO_THROW(keyName3 = keyChain.generateRsaKeyPairAsDefault(identity)); |
| 285 | |
| 286 | shared_ptr<IdentityCertificate> cert3; |
| 287 | BOOST_REQUIRE_NO_THROW(cert3 = keyChain.selfSign(keyName3)); |
| 288 | Name certName3 = cert3->getName(); |
| 289 | BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert3)); |
| 290 | shared_ptr<IdentityCertificate> cert4; |
| 291 | BOOST_REQUIRE_NO_THROW(cert4 = keyChain.selfSign(keyName3)); |
| 292 | Name certName4 = cert4->getName(); |
| 293 | BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert4)); |
| 294 | shared_ptr<IdentityCertificate> cert5; |
| 295 | BOOST_REQUIRE_NO_THROW(cert5 = keyChain.selfSign(keyName3)); |
| 296 | Name certName5 = cert5->getName(); |
| 297 | BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert5)); |
| 298 | |
| 299 | BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true); |
| 300 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true); |
| 301 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true); |
| 302 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true); |
| 303 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), true); |
| 304 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), true); |
| 305 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true); |
| 306 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true); |
| 307 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), true); |
| 308 | |
| 309 | BOOST_REQUIRE_NO_THROW(keyChain.deleteCertificate(certName5)); |
| 310 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), false); |
| 311 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true); |
| 312 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true); |
| 313 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true); |
| 314 | |
| 315 | BOOST_REQUIRE_NO_THROW(keyChain.deleteKey(keyName3)); |
| 316 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), false); |
| 317 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), false); |
| 318 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), false); |
| 319 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true); |
| 320 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true); |
| 321 | BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true); |
| 322 | |
| 323 | BOOST_REQUIRE_NO_THROW(keyChain.deleteIdentity(identity)); |
| 324 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), false); |
| 325 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), false); |
| 326 | BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), false); |
| 327 | BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), false); |
| 328 | BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false); |
| 329 | } |
| 330 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 331 | BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib) |
| 332 | { |
| 333 | BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy", "tpm-dummy"))); |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 334 | BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy2", "tpm-dummy2"))); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 335 | BOOST_REQUIRE_NO_THROW((KeyChain("dummy", "dummy"))); |
| 336 | BOOST_REQUIRE_NO_THROW((KeyChain("dummy:", "dummy:"))); |
| 337 | BOOST_REQUIRE_NO_THROW((KeyChain("dummy:/something", "dummy:/something"))); |
| 338 | |
| 339 | KeyChain keyChain("dummy", "dummy"); |
Alexander Afanasyev | 34a3763 | 2015-01-16 17:37:36 -0800 | [diff] [blame] | 340 | BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-dummy:"); |
| 341 | BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-dummy:"); |
| 342 | BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-dummy:"); |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 343 | BOOST_CHECK_EQUAL(keyChain.getDefaultIdentity(), "/dummy/key"); |
| 344 | } |
| 345 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 346 | BOOST_AUTO_TEST_CASE(GeneralSigningInterface) |
| 347 | { |
| 348 | KeyChain keyChain; |
| 349 | Name id("/id"); |
| 350 | Name certName = keyChain.createIdentity(id); |
| 351 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 352 | Name keyName = idCert->getPublicKeyName(); |
| 353 | keyChain.setDefaultIdentity(id); |
| 354 | |
| 355 | Name id2("/id2"); |
| 356 | Name cert2Name = keyChain.createIdentity(id2); |
| 357 | shared_ptr<IdentityCertificate> id2Cert = keyChain.getCertificate(cert2Name); |
| 358 | |
| 359 | // SigningInfo is set to default |
| 360 | Data data1("/data1"); |
| 361 | keyChain.sign(data1); |
| 362 | BOOST_CHECK(Validator::verifySignature(data1, idCert->getPublicKeyInfo())); |
| 363 | BOOST_CHECK_EQUAL(data1.getSignature().getKeyLocator().getName(), certName.getPrefix(-1)); |
| 364 | |
| 365 | Interest interest1("/interest1"); |
| 366 | keyChain.sign(interest1); |
| 367 | BOOST_CHECK(Validator::verifySignature(interest1, idCert->getPublicKeyInfo())); |
| 368 | SignatureInfo sigInfo1(interest1.getName()[-2].blockFromValue()); |
| 369 | BOOST_CHECK_EQUAL(sigInfo1.getKeyLocator().getName(), certName.getPrefix(-1)); |
| 370 | |
| 371 | // SigningInfo is set to Identity |
| 372 | Data data2("/data2"); |
| 373 | keyChain.sign(data2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2)); |
| 374 | BOOST_CHECK(Validator::verifySignature(data2, id2Cert->getPublicKeyInfo())); |
| 375 | BOOST_CHECK_EQUAL(data2.getSignature().getKeyLocator().getName(), cert2Name.getPrefix(-1)); |
| 376 | |
| 377 | Interest interest2("/interest2"); |
| 378 | keyChain.sign(interest2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2)); |
| 379 | BOOST_CHECK(Validator::verifySignature(interest2, id2Cert->getPublicKeyInfo())); |
| 380 | SignatureInfo sigInfo2(interest2.getName()[-2].blockFromValue()); |
| 381 | BOOST_CHECK_EQUAL(sigInfo2.getKeyLocator().getName(), cert2Name.getPrefix(-1)); |
| 382 | |
| 383 | // SigningInfo is set to Key |
| 384 | Data data3("/data3"); |
| 385 | keyChain.sign(data3, SigningInfo(SigningInfo::SIGNER_TYPE_KEY, keyName)); |
| 386 | BOOST_CHECK(Validator::verifySignature(data3, idCert->getPublicKeyInfo())); |
| 387 | BOOST_CHECK_EQUAL(data3.getSignature().getKeyLocator().getName(), certName.getPrefix(-1)); |
| 388 | |
| 389 | Interest interest3("/interest3"); |
| 390 | keyChain.sign(interest3); |
| 391 | BOOST_CHECK(Validator::verifySignature(interest3, idCert->getPublicKeyInfo())); |
| 392 | SignatureInfo sigInfo3(interest1.getName()[-2].blockFromValue()); |
| 393 | BOOST_CHECK_EQUAL(sigInfo3.getKeyLocator().getName(), certName.getPrefix(-1)); |
| 394 | |
| 395 | // SigningInfo is set to Cert |
| 396 | Data data4("/data4"); |
| 397 | keyChain.sign(data4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName)); |
| 398 | BOOST_CHECK(Validator::verifySignature(data4, idCert->getPublicKeyInfo())); |
| 399 | BOOST_CHECK_EQUAL(data4.getSignature().getKeyLocator().getName(), certName.getPrefix(-1)); |
| 400 | |
| 401 | Interest interest4("/interest4"); |
| 402 | keyChain.sign(interest4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName)); |
| 403 | BOOST_CHECK(Validator::verifySignature(interest4, idCert->getPublicKeyInfo())); |
| 404 | SignatureInfo sigInfo4(interest4.getName()[-2].blockFromValue()); |
| 405 | BOOST_CHECK_EQUAL(sigInfo4.getKeyLocator().getName(), certName.getPrefix(-1)); |
| 406 | |
| 407 | |
| 408 | // SigningInfo is set to DigestSha256 |
| 409 | Data data5("/data5"); |
| 410 | keyChain.sign(data5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)); |
| 411 | BOOST_CHECK(Validator::verifySignature(data5, DigestSha256(data5.getSignature()))); |
| 412 | |
| 413 | Interest interest5("/interest4"); |
| 414 | keyChain.sign(interest5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)); |
| 415 | BOOST_CHECK(Validator::verifySignature(interest5, |
| 416 | DigestSha256(Signature(interest5.getName()[-2].blockFromValue(), |
| 417 | interest5.getName()[-1].blockFromValue())))); |
| 418 | } |
| 419 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 420 | BOOST_AUTO_TEST_SUITE_END() |
| 421 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 422 | } // namespace tests |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 423 | } // namespace security |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 424 | } // namespace ndn |