blob: 01c95023b86342828cb1667c1a3ee832190c03e6 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yu8dceb1d2014-02-18 12:45:10 -080011 */
12
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080013#include "security/key-chain.hpp"
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080014
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
16
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080017using namespace std;
18
19namespace ndn {
20
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070021BOOST_AUTO_TEST_SUITE(SecurityTestKeyChain)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080022
23BOOST_AUTO_TEST_CASE (ExportIdentity)
24{
25 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
26
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070027 Name identity("/TestKeyChain/ExportIdentity/");
28 identity.appendVersion();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080029 keyChain.createIdentity(identity);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030
Yingdi Yu64c3fb42014-02-26 17:30:04 -080031 shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234");
32
33 Block block = exported->wireEncode();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080034
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 Yu64c3fb42014-02-26 17:30:04 -080046 SecuredBag imported;
47 imported.wireDecode(block);
48 keyChain.importIdentity(imported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080049
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 Yuc55680b2014-02-26 12:31:35 -080065BOOST_AUTO_TEST_CASE (PrepareIdentityCertificate)
66{
67 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070068
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 Name identity("/TestKeyChain/PrepareIdentityCertificate/");
70 identity.appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080071 keyChain.createIdentity(identity);
72
73 vector<CertificateSubjectDescription> subjectDescription;
74 Name lowerIdentity = identity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075 lowerIdentity.append("Lower").appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080076 Name lowerKeyName = keyChain.generateRSAKeyPair(lowerIdentity, true);
77 shared_ptr<IdentityCertificate> idCert
78 = keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 time::system_clock::now(),
80 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -080081 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 Afanasyevaa0e7da2014-03-17 14:37:33 -070086 Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
87 anotherIdentity.appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -080088 Name anotherKeyName = keyChain.generateRSAKeyPair(anotherIdentity, true);
89 shared_ptr<IdentityCertificate> idCert2
90 = keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 time::system_clock::now(),
92 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -080093 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 Afanasyevaa0e7da2014-03-17 14:37:33 -0700101 time::system_clock::now(),
102 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -0800103 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 Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 time::system_clock::now(),
111 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -0800112 subjectDescription);
113 BOOST_CHECK(!static_cast<bool>(idCert4));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700114
Yingdi Yuc55680b2014-02-26 12:31:35 -0800115
116 Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
117 shared_ptr<IdentityCertificate> idCert5
118 = keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 time::system_clock::now(),
120 time::system_clock::now() + time::days(365),
Yingdi Yuc55680b2014-02-26 12:31:35 -0800121 subjectDescription);
122 BOOST_CHECK(!static_cast<bool>(idCert5));
123
124 keyChain.deleteIdentity(identity);
125 keyChain.deleteIdentity(lowerIdentity);
126 keyChain.deleteIdentity(anotherIdentity);
127}
128
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800129BOOST_AUTO_TEST_SUITE_END()
130
131} // namespace ndn