Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 13 | #include "security/key-chain.hpp" |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 14 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 15 | #include "boost-test.hpp" |
| 16 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 17 | using namespace std; |
| 18 | |
| 19 | namespace ndn { |
| 20 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 21 | BOOST_AUTO_TEST_SUITE(SecurityTestKeyChain) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 22 | |
| 23 | BOOST_AUTO_TEST_CASE (ExportIdentity) |
| 24 | { |
| 25 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
| 26 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 27 | Name identity("/TestKeyChain/ExportIdentity/"); |
| 28 | identity.appendVersion(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 29 | keyChain.createIdentity(identity); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 31 | shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234"); |
| 32 | |
| 33 | Block block = exported->wireEncode(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 34 | |
| 35 | Name keyName = keyChain.getDefaultKeyNameForIdentity(identity); |
| 36 | Name certName = keyChain.getDefaultCertificateNameForKey(keyName); |
| 37 | |
| 38 | keyChain.deleteIdentity(identity); |
| 39 | |
| 40 | BOOST_REQUIRE(keyChain.doesIdentityExist(identity) == false); |
| 41 | BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName) == false); |
| 42 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false); |
| 43 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false); |
| 44 | BOOST_REQUIRE(keyChain.doesCertificateExist(certName) == false); |
| 45 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 46 | SecuredBag imported; |
| 47 | imported.wireDecode(block); |
| 48 | keyChain.importIdentity(imported, "1234"); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 49 | |
| 50 | BOOST_REQUIRE(keyChain.doesIdentityExist(identity)); |
| 51 | BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName)); |
| 52 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)); |
| 53 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)); |
| 54 | BOOST_REQUIRE(keyChain.doesCertificateExist(certName)); |
| 55 | |
| 56 | keyChain.deleteIdentity(identity); |
| 57 | |
| 58 | BOOST_REQUIRE(keyChain.doesIdentityExist(identity) == false); |
| 59 | BOOST_REQUIRE(keyChain.doesPublicKeyExist(keyName) == false); |
| 60 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false); |
| 61 | BOOST_REQUIRE(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false); |
| 62 | BOOST_REQUIRE(keyChain.doesCertificateExist(certName) == false); |
| 63 | } |
| 64 | |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE (PrepareIdentityCertificate) |
| 66 | { |
| 67 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 69 | Name identity("/TestKeyChain/PrepareIdentityCertificate/"); |
| 70 | identity.appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 71 | keyChain.createIdentity(identity); |
| 72 | |
| 73 | vector<CertificateSubjectDescription> subjectDescription; |
| 74 | Name lowerIdentity = identity; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 75 | lowerIdentity.append("Lower").appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 76 | Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true); |
| 77 | shared_ptr<IdentityCertificate> idCert |
| 78 | = keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 79 | time::system_clock::now(), |
| 80 | time::system_clock::now() + time::days(365), |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 81 | subjectDescription); |
| 82 | BOOST_CHECK(static_cast<bool>(idCert)); |
| 83 | BOOST_CHECK(idCert->getName().getPrefix(5) == Name().append(identity).append("KEY").append("Lower")); |
| 84 | |
| 85 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 86 | Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/"); |
| 87 | anotherIdentity.appendVersion(); |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 88 | Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true); |
| 89 | shared_ptr<IdentityCertificate> idCert2 |
| 90 | = keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | time::system_clock::now(), |
| 92 | time::system_clock::now() + time::days(365), |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 93 | subjectDescription); |
| 94 | BOOST_CHECK(static_cast<bool>(idCert2)); |
| 95 | BOOST_CHECK(idCert2->getName().getPrefix(5) == Name().append(anotherIdentity).append("KEY")); |
| 96 | |
| 97 | |
| 98 | Name wrongKeyName1; |
| 99 | shared_ptr<IdentityCertificate> idCert3 |
| 100 | = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 101 | time::system_clock::now(), |
| 102 | time::system_clock::now() + time::days(365), |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 103 | subjectDescription); |
| 104 | BOOST_CHECK(!static_cast<bool>(idCert3)); |
| 105 | |
| 106 | |
| 107 | Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate"); |
| 108 | shared_ptr<IdentityCertificate> idCert4 |
| 109 | = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 110 | time::system_clock::now(), |
| 111 | time::system_clock::now() + time::days(365), |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 112 | subjectDescription); |
| 113 | BOOST_CHECK(!static_cast<bool>(idCert4)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 114 | |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 115 | |
| 116 | Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234"); |
| 117 | shared_ptr<IdentityCertificate> idCert5 |
| 118 | = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity, |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 119 | time::system_clock::now(), |
| 120 | time::system_clock::now() + time::days(365), |
Yingdi Yu | c55680b | 2014-02-26 12:31:35 -0800 | [diff] [blame] | 121 | subjectDescription); |
| 122 | BOOST_CHECK(!static_cast<bool>(idCert5)); |
| 123 | |
| 124 | keyChain.deleteIdentity(identity); |
| 125 | keyChain.deleteIdentity(lowerIdentity); |
| 126 | keyChain.deleteIdentity(anotherIdentity); |
| 127 | } |
| 128 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 129 | BOOST_AUTO_TEST_SUITE_END() |
| 130 | |
| 131 | } // namespace ndn |