blob: 8a76b79e763edec1dbb91e3f6806585721a63688 [file] [log] [blame]
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08001/**
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 Yu8dceb1d2014-02-18 12:45:10 -080010
11using namespace std;
12
13namespace ndn {
14
15BOOST_AUTO_TEST_SUITE(TestKeyChain)
16
17BOOST_AUTO_TEST_CASE (ExportIdentity)
18{
19 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
20
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070021 Name identity("/TestKeyChain/ExportIdentity/");
22 identity.appendVersion();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080023 keyChain.createIdentity(identity);
24
Yingdi Yu64c3fb42014-02-26 17:30:04 -080025 shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234");
26
27 Block block = exported->wireEncode();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080028
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 Yu64c3fb42014-02-26 17:30:04 -080040 SecuredBag imported;
41 imported.wireDecode(block);
42 keyChain.importIdentity(imported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080043
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 Yuc55680b2014-02-26 12:31:35 -080059BOOST_AUTO_TEST_CASE (PrepareIdentityCertificate)
60{
61 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
62
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063 Name identity("/TestKeyChain/PrepareIdentityCertificate/");
64 identity.appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080065 keyChain.createIdentity(identity);
66
67 vector<CertificateSubjectDescription> subjectDescription;
68 Name lowerIdentity = identity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 lowerIdentity.append("Lower").appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080070 Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true);
71 shared_ptr<IdentityCertificate> idCert
72 = keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 time::system_clock::now(),
74 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -080075 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 Afanasyevaa0e7da2014-03-17 14:37:33 -070080 Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
81 anotherIdentity.appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080082 Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true);
83 shared_ptr<IdentityCertificate> idCert2
84 = keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085 time::system_clock::now(),
86 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -080087 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 Afanasyevaa0e7da2014-03-17 14:37:33 -070095 time::system_clock::now(),
96 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -080097 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 Afanasyevaa0e7da2014-03-17 14:37:33 -0700104 time::system_clock::now(),
105 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -0800106 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 Afanasyevaa0e7da2014-03-17 14:37:33 -0700113 time::system_clock::now(),
114 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -0800115 subjectDescription);
116 BOOST_CHECK(!static_cast<bool>(idCert5));
117
118 keyChain.deleteIdentity(identity);
119 keyChain.deleteIdentity(lowerIdentity);
120 keyChain.deleteIdentity(anotherIdentity);
121}
122
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123BOOST_AUTO_TEST_SUITE_END()
124
125} // namespace ndn