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