Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 21 | #define NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 22 | |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 23 | #include "common.hpp" |
| 24 | |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 27 | #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 28 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace nac { |
| 34 | namespace tests { |
| 35 | |
| 36 | /** |
| 37 | * @brief A test suite level fixture to help with identity management |
| 38 | * |
| 39 | * Test cases in the suite can use this fixture to create identities. Identities, |
| 40 | * certificates, and saved certificates are automatically removed during test teardown. |
| 41 | */ |
| 42 | class IdentityManagementFixture |
| 43 | { |
| 44 | public: |
| 45 | IdentityManagementFixture(); |
| 46 | |
| 47 | ~IdentityManagementFixture(); |
| 48 | |
| 49 | /** |
| 50 | * @brief Add identity @p identityName |
| 51 | * @return name of the created self-signed certificate |
| 52 | */ |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 53 | Identity |
| 54 | addIdentity(const Name& identityName, const KeyParams& params = KeyChain::getDefaultKeyParams()); |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @brief Save identity certificate to a file |
| 58 | * @param identity identity |
| 59 | * @param filename file name, should be writable |
| 60 | * @return whether successful |
| 61 | */ |
| 62 | bool |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 63 | saveIdentityCertificate(const Identity& identity, const std::string& filename); |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief Issue a certificate for \p subIdentityName signed by \p issuer |
| 67 | * |
| 68 | * If identity does not exist, it is created. |
| 69 | * A new key is generated as the default key for identity. |
| 70 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 71 | * |
| 72 | * @return the sub identity |
| 73 | */ |
| 74 | security::Identity |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 75 | addSubCertificate(const Name& subIdentityName, const Identity& issuer, |
| 76 | const KeyParams& params = KeyChain::getDefaultKeyParams()); |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * @brief Add a self-signed certificate to @p key with issuer ID @p issuer |
| 80 | */ |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 81 | Certificate |
| 82 | addCertificate(const Key& key, const std::string& issuer); |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 83 | |
| 84 | bool |
| 85 | saveCertToFile(const Data& obj, const std::string& filename); |
| 86 | |
| 87 | protected: |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame^] | 88 | KeyChain m_keyChain; |
Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 89 | std::set<Name> m_identities; |
| 90 | std::set<std::string> m_certFiles; |
| 91 | }; |
| 92 | |
| 93 | } // namespace tests |
| 94 | } // namespace nac |
| 95 | } // namespace ndn |
| 96 | |
| 97 | #endif // NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |