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