blob: 7eebc3e5a8899e016889f7553406235cfe15abbe [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
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08007#include "security/key-chain.hpp"
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08008
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -07009#include "boost-test.hpp"
10
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080011using namespace std;
12
13namespace ndn {
14
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070015BOOST_AUTO_TEST_SUITE(SecurityTestKeyChain)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080016
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);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024
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;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062
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));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700108
Yingdi Yuc55680b2014-02-26 12:31:35 -0800109
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