Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cafa402 | 2017-09-15 23:20:20 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 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/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/tpm/back-end.hpp" |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
| 25 | #include "ndn-cxx/security/pib/key.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/security/transform/bool-sink.hpp" |
| 27 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 28 | #include "ndn-cxx/security/transform/private-key.hpp" |
| 29 | #include "ndn-cxx/security/transform/public-key.hpp" |
| 30 | #include "ndn-cxx/security/transform/verifier-filter.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 32 | #include "tests/unit/security/tpm/back-end-wrapper-file.hpp" |
| 33 | #include "tests/unit/security/tpm/back-end-wrapper-mem.hpp" |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 34 | #ifdef NDN_CXX_HAVE_OSX_FRAMEWORKS |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 35 | #include "tests/unit/security/tpm/back-end-wrapper-osx.hpp" |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 36 | #endif // NDN_CXX_HAVE_OSX_FRAMEWORKS |
Davide Pesavento | cafa402 | 2017-09-15 23:20:20 -0400 | [diff] [blame] | 37 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 38 | #include "tests/boost-test.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 40 | #include <set> |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 41 | #include <boost/mp11/list.hpp> |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 43 | namespace ndn::tests { |
| 44 | |
| 45 | using namespace ndn::security; |
| 46 | using ndn::security::tpm::BackEnd; |
| 47 | using ndn::security::tpm::KeyHandle; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 48 | |
| 49 | BOOST_AUTO_TEST_SUITE(Security) |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 50 | BOOST_AUTO_TEST_SUITE(TestTpmBackEnd) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 51 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 52 | using TestBackEnds = boost::mp11::mp_list< |
Davide Pesavento | dc3575f | 2022-07-30 21:10:34 -0400 | [diff] [blame] | 53 | #if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 54 | BackEndWrapperOsx, |
Davide Pesavento | dc3575f | 2022-07-30 21:10:34 -0400 | [diff] [blame] | 55 | #endif |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 56 | BackEndWrapperMem, |
Davide Pesavento | cafa402 | 2017-09-15 23:20:20 -0400 | [diff] [blame] | 57 | BackEndWrapperFile>; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 58 | |
| 59 | BOOST_AUTO_TEST_CASE_TEMPLATE(KeyManagement, T, TestBackEnds) |
| 60 | { |
| 61 | T wrapper; |
| 62 | BackEnd& tpm = wrapper.getTpm(); |
| 63 | |
| 64 | Name identity("/Test/KeyName"); |
| 65 | name::Component keyId("1"); |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 66 | Name keyName = constructKeyName(identity, keyId); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 67 | |
| 68 | // key should not exist |
| 69 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
| 70 | BOOST_CHECK(tpm.getKeyHandle(keyName) == nullptr); |
| 71 | |
| 72 | // create key, should exist |
| 73 | BOOST_CHECK(tpm.createKey(identity, RsaKeyParams(keyId)) != nullptr); |
| 74 | BOOST_CHECK(tpm.hasKey(keyName)); |
| 75 | BOOST_CHECK(tpm.getKeyHandle(keyName) != nullptr); |
| 76 | |
| 77 | // create a key with the same name, should throw error |
| 78 | BOOST_CHECK_THROW(tpm.createKey(identity, RsaKeyParams(keyId)), Tpm::Error); |
| 79 | |
| 80 | // delete key, should not exist |
| 81 | tpm.deleteKey(keyName); |
| 82 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
| 83 | BOOST_CHECK(tpm.getKeyHandle(keyName) == nullptr); |
| 84 | } |
| 85 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 86 | BOOST_AUTO_TEST_CASE(CreateHmacKey) |
| 87 | { |
| 88 | Name identity("/Test/Identity/HMAC"); |
| 89 | |
| 90 | BackEndWrapperMem mem; |
| 91 | BackEnd& memTpm = mem.getTpm(); |
| 92 | auto key = memTpm.createKey(identity, HmacKeyParams()); |
| 93 | BOOST_REQUIRE(key != nullptr); |
| 94 | BOOST_CHECK(!key->getKeyName().empty()); |
| 95 | BOOST_CHECK(memTpm.hasKey(key->getKeyName())); |
| 96 | |
| 97 | BackEndWrapperFile file; |
| 98 | BackEnd& fileTpm = file.getTpm(); |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 99 | BOOST_CHECK_THROW(fileTpm.createKey(identity, HmacKeyParams()), std::invalid_argument); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 100 | |
| 101 | #ifdef NDN_CXX_HAVE_OSX_FRAMEWORKS |
| 102 | BackEndWrapperOsx osx; |
| 103 | BackEnd& osxTpm = osx.getTpm(); |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 104 | BOOST_CHECK_THROW(osxTpm.createKey(identity, HmacKeyParams()), std::invalid_argument); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 105 | #endif // NDN_CXX_HAVE_OSX_FRAMEWORKS |
| 106 | } |
| 107 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE_TEMPLATE(RsaSigning, T, TestBackEnds) |
| 109 | { |
| 110 | T wrapper; |
| 111 | BackEnd& tpm = wrapper.getTpm(); |
| 112 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 113 | // create an RSA key |
laqinfan | cf0baa2 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 114 | Name identity("/Test/RSA/KeyName"); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 115 | unique_ptr<KeyHandle> key = tpm.createKey(identity, RsaKeyParams()); |
| 116 | Name keyName = key->getKeyName(); |
| 117 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 118 | transform::PublicKey pubKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 119 | auto pubKeyBits = key->derivePublicKey(); |
| 120 | pubKey.loadPkcs8(*pubKeyBits); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 121 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 122 | // Sign a single buffer |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 123 | const uint8_t content1[] = {0x01, 0x02, 0x03, 0x04}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 124 | auto sigValueSingle = key->sign(DigestAlgorithm::SHA256, {content1}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 125 | BOOST_REQUIRE(sigValueSingle != nullptr); |
| 126 | |
| 127 | bool resultSingle; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 128 | { |
| 129 | using namespace transform; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 130 | bufferSource(content1) |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 131 | >> verifierFilter(DigestAlgorithm::SHA256, pubKey, *sigValueSingle) |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 132 | >> boolSink(resultSingle); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 133 | } |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(resultSingle, true); |
| 135 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 136 | // Sign multiple buffers |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 137 | const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 138 | auto sigValueVector = key->sign(DigestAlgorithm::SHA256, {content1, content2}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 139 | BOOST_REQUIRE(sigValueVector != nullptr); |
| 140 | |
| 141 | bool resultVector; |
| 142 | { |
| 143 | using namespace transform; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 144 | bufferSource(InputBuffers{content1, content2}) |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 145 | >> verifierFilter(DigestAlgorithm::SHA256, pubKey, *sigValueVector) |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 146 | >> boolSink(resultVector); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 147 | } |
| 148 | BOOST_CHECK_EQUAL(resultVector, true); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 149 | |
| 150 | tpm.deleteKey(keyName); |
| 151 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
| 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE_TEMPLATE(RsaDecryption, T, TestBackEnds) |
| 155 | { |
| 156 | T wrapper; |
| 157 | BackEnd& tpm = wrapper.getTpm(); |
| 158 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 159 | // create an RSA key |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 160 | Name identity("/Test/KeyName"); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 161 | unique_ptr<KeyHandle> key = tpm.createKey(identity, RsaKeyParams()); |
| 162 | Name keyName = key->getKeyName(); |
| 163 | |
| 164 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 165 | |
| 166 | transform::PublicKey pubKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 167 | auto pubKeyBits = key->derivePublicKey(); |
| 168 | pubKey.loadPkcs8(*pubKeyBits); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 169 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 170 | ConstBufferPtr cipherText = pubKey.encrypt(content); |
| 171 | ConstBufferPtr plainText = key->decrypt(*cipherText); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 172 | |
| 173 | BOOST_CHECK_EQUAL_COLLECTIONS(content, content + sizeof(content), |
| 174 | plainText->begin(), plainText->end()); |
| 175 | |
| 176 | tpm.deleteKey(keyName); |
| 177 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
| 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_CASE_TEMPLATE(EcdsaSigning, T, TestBackEnds) |
| 181 | { |
| 182 | T wrapper; |
| 183 | BackEnd& tpm = wrapper.getTpm(); |
| 184 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 185 | // create an EC key |
laqinfan | cf0baa2 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 186 | Name identity("/Test/EC/KeyName"); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 187 | unique_ptr<KeyHandle> key = tpm.createKey(identity, EcKeyParams()); |
| 188 | Name ecKeyName = key->getKeyName(); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 189 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 190 | transform::PublicKey pubKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 191 | auto pubKeyBits = key->derivePublicKey(); |
| 192 | pubKey.loadPkcs8(*pubKeyBits); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 193 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 194 | // Sign a single buffer |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 195 | const uint8_t content1[] = {0x01, 0x02, 0x03, 0x04}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 196 | auto sigValueSingle = key->sign(DigestAlgorithm::SHA256, {content1}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 197 | BOOST_REQUIRE(sigValueSingle != nullptr); |
| 198 | |
| 199 | bool resultSingle; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 200 | { |
| 201 | using namespace transform; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 202 | bufferSource(content1) |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 203 | >> verifierFilter(DigestAlgorithm::SHA256, pubKey, *sigValueSingle) |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 204 | >> boolSink(resultSingle); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 205 | } |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 206 | BOOST_CHECK_EQUAL(resultSingle, true); |
| 207 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 208 | // Sign multiple buffers |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 209 | const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 210 | auto sigValueVector = key->sign(DigestAlgorithm::SHA256, {content1, content2}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 211 | BOOST_REQUIRE(sigValueVector != nullptr); |
| 212 | |
| 213 | bool resultVector; |
| 214 | { |
| 215 | using namespace transform; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 216 | bufferSource(InputBuffers{content1, content2}) |
Davide Pesavento | 35c6379 | 2022-01-17 02:06:03 -0500 | [diff] [blame] | 217 | >> verifierFilter(DigestAlgorithm::SHA256, pubKey, *sigValueVector) |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 218 | >> boolSink(resultVector); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 219 | } |
| 220 | BOOST_CHECK_EQUAL(resultVector, true); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 221 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 222 | tpm.deleteKey(ecKeyName); |
| 223 | BOOST_CHECK_EQUAL(tpm.hasKey(ecKeyName), false); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 224 | } |
| 225 | |
laqinfan | cf0baa2 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 226 | BOOST_AUTO_TEST_CASE(HmacSigningAndVerifying) |
| 227 | { |
| 228 | BackEndWrapperMem wrapper; |
| 229 | BackEnd& tpm = wrapper.getTpm(); |
| 230 | |
| 231 | // create an HMAC key |
| 232 | Name identity("/Test/HMAC/KeyName"); |
| 233 | unique_ptr<KeyHandle> key = tpm.createKey(identity, HmacKeyParams()); |
| 234 | Name hmacKeyName = key->getKeyName(); |
| 235 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 236 | // Sign and verify a single buffer |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 237 | const uint8_t content1[] = {0x01, 0x02, 0x03, 0x04}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 238 | auto sigValueSingle = key->sign(DigestAlgorithm::SHA256, {content1}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 239 | BOOST_REQUIRE(sigValueSingle != nullptr); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 240 | bool resultSingle = key->verify(DigestAlgorithm::SHA256, {content1}, *sigValueSingle); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(resultSingle, true); |
laqinfan | cf0baa2 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 242 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 243 | // Sign and verify multiple buffers |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 244 | const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08}; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 245 | auto sigValueVector = key->sign(DigestAlgorithm::SHA256, {content1, content2}); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 246 | BOOST_REQUIRE(sigValueVector != nullptr); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 247 | bool resultVector = key->verify(DigestAlgorithm::SHA256, {content1, content2}, *sigValueVector); |
Eric Newberry | 6d024ba | 2020-06-14 16:10:34 -0700 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL(resultVector, true); |
laqinfan | cf0baa2 | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 249 | |
| 250 | tpm.deleteKey(hmacKeyName); |
| 251 | BOOST_CHECK_EQUAL(tpm.hasKey(hmacKeyName), false); |
| 252 | } |
| 253 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 254 | BOOST_AUTO_TEST_CASE_TEMPLATE(ImportExport, T, TestBackEnds) |
| 255 | { |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 256 | const std::string privKeyPkcs1 = |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 257 | "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n" |
| 258 | "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n" |
| 259 | "GT6SgFJhYhqo1/upWdueojil6RP4/AgavHhopxlAVbk6G9VdVnlQcQ5Zv0OcGi73\n" |
| 260 | "c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9gZwIL5PuE9BiO6I39cL9z7EK1SfZh\n" |
| 261 | "OWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA9rH58ynaAix0tcR/nBMRLUX+e3rU\n" |
| 262 | "RHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0ywIDAQABAoIBADQkckOIl4IZMUTn\n" |
| 263 | "W8LFv6xOdkJwMKC8G6bsPRFbyY+HvC2TLt7epSvfS+f4AcYWaOPcDu2E49vt2sNr\n" |
| 264 | "cASly8hgwiRRAB3dHH9vcsboiTo8bi2RFvMqvjv9w3tK2yMxVDtmZamzrrnaV3YV\n" |
| 265 | "Q+5nyKo2F/PMDjQ4eUAKDOzjhBuKHsZBTFnA1MFNI+UKj5X4Yp64DFmKlxTX/U2b\n" |
| 266 | "wzVywo5hzx2Uhw51jmoLls4YUvMJXD0wW5ZtYRuPogXvXb/of9ef/20/wU11WFKg\n" |
| 267 | "Xb4gfR8zUXaXS1sXcnVm3+24vIs9dApUwykuoyjOqxWqcHRec2QT2FxVGkFEraze\n" |
| 268 | "CPa4rMECgYEA5Y8CywomIcTgerFGFCeMHJr8nQGqY2V/owFb3k9maczPnC9p4a9R\n" |
| 269 | "c5szLxA9FMYFxurQZMBWSEG2JS1HR2mnjigx8UKjYML/A+rvvjZOMe4M6Sy2ggh4\n" |
| 270 | "SkLZKpWTzjTe07ByM/j5v/SjNZhWAG7sw4/LmPGRQkwJv+KZhGojuOkCgYEA2cOF\n" |
| 271 | "T6cJRv6kvzTz9S0COZOVm+euJh/BXp7oAsAmbNfOpckPMzqHXy8/wpdKl6AAcB57\n" |
| 272 | "OuztlNfV1D7qvbz7JuRlYwQ0cEfBgbZPcz1p18HHDXhwn57ZPb8G33Yh9Omg0HNA\n" |
| 273 | "Imb4LsVuSqxA6NwSj7cpRekgTedrhLFPJ+Ydb5MCgYEAsM3Q7OjILcIg0t6uht9e\n" |
| 274 | "vrlwTsz1mtCV2co2I6crzdj9HeI2vqf1KAElDt6G7PUHhglcr/yjd8uEqmWRPKNX\n" |
| 275 | "ddnnfVZB10jYeP/93pac6z/Zmc3iU4yKeUe7U10ZFf0KkiiYDQd59CpLef/2XScS\n" |
| 276 | "HB0oRofnxRQjfjLc4muNT+ECgYEAlcDk06MOOTly+F8lCc1bA1dgAmgwFd2usDBd\n" |
| 277 | "Y07a3e0HGnGLN3Kfl7C5i0tZq64HvxLnMd2vgLVxQlXGPpdQrC1TH+XLXg+qnlZO\n" |
| 278 | "ivSH7i0/gx75bHvj75eH1XK65V8pDVDEoSPottllAIs21CxLw3N1ObOZWJm2EfmR\n" |
| 279 | "cuHICmsCgYAtFJ1idqMoHxES3mlRpf2JxyQudP3SCm2WpGmqVzhRYInqeatY5sUd\n" |
| 280 | "lPLHm/p77RT7EyxQHTlwn8FJPuM/4ZH1rQd/vB+Y8qAtYJCexDMsbvLW+Js+VOvk\n" |
| 281 | "jweEC0nrcL31j9mF0vz5E6tfRu4hhJ6L4yfWs0gSejskeVB/w8QY4g==\n"; |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 282 | const std::string password("password"); |
| 283 | const std::string wrongPassword("wrong"); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 284 | |
| 285 | T wrapper; |
| 286 | BackEnd& tpm = wrapper.getTpm(); |
| 287 | |
| 288 | Name keyName("/Test/KeyName/KEY/1"); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 289 | tpm.deleteKey(keyName); |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 290 | BOOST_REQUIRE_EQUAL(tpm.hasKey(keyName), false); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 291 | |
| 292 | transform::PrivateKey sKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 293 | sKey.loadPkcs1Base64({reinterpret_cast<const uint8_t*>(privKeyPkcs1.data()), privKeyPkcs1.size()}); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 294 | OBufferStream os; |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 295 | sKey.savePkcs8(os, password.data(), password.size()); |
| 296 | auto pkcs8 = os.buf(); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 297 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 298 | // import with wrong password |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 299 | BOOST_CHECK_THROW(tpm.importKey(keyName, *pkcs8, wrongPassword.data(), wrongPassword.size()), |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 300 | Tpm::Error); |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 301 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
| 302 | |
| 303 | // import with correct password |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 304 | tpm.importKey(keyName, *pkcs8, password.data(), password.size()); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 305 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), true); |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 306 | |
| 307 | // import already present key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 308 | BOOST_CHECK_THROW(tpm.importKey(keyName, *pkcs8, password.data(), password.size()), |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 309 | Tpm::Error); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 310 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 311 | // test derivePublicKey with the imported key |
| 312 | auto keyHdl = tpm.getKeyHandle(keyName); |
| 313 | auto pubKey = keyHdl->derivePublicKey(); |
| 314 | BOOST_CHECK(pubKey != nullptr); |
| 315 | |
| 316 | // export |
| 317 | auto exportedKey = tpm.exportKey(keyName, password.data(), password.size()); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 318 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), true); |
| 319 | |
| 320 | transform::PrivateKey sKey2; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 321 | sKey2.loadPkcs8(*exportedKey, password.data(), password.size()); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 322 | OBufferStream os2; |
| 323 | sKey.savePkcs1Base64(os2); |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 324 | auto pkcs1 = os2.buf(); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 325 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 326 | // verify that the exported key is identical to the key that was imported |
| 327 | BOOST_CHECK_EQUAL_COLLECTIONS(privKeyPkcs1.begin(), privKeyPkcs1.end(), |
| 328 | pkcs1->begin(), pkcs1->end()); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 329 | |
Davide Pesavento | 13fffa3 | 2018-09-30 16:21:33 -0400 | [diff] [blame] | 330 | // export nonexistent key |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 331 | tpm.deleteKey(keyName); |
| 332 | BOOST_CHECK_EQUAL(tpm.hasKey(keyName), false); |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 333 | BOOST_CHECK_THROW(tpm.exportKey(keyName, password.data(), password.size()), Tpm::Error); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | BOOST_AUTO_TEST_CASE(RandomKeyId) |
| 337 | { |
| 338 | BackEndWrapperMem wrapper; |
| 339 | BackEnd& tpm = wrapper.getTpm(); |
| 340 | Name identity("/Test/KeyName"); |
| 341 | |
| 342 | std::set<Name> keyNames; |
| 343 | for (int i = 0; i < 100; i++) { |
| 344 | auto key = tpm.createKey(identity, RsaKeyParams()); |
| 345 | Name keyName = key->getKeyName(); |
Davide Pesavento | 334516a | 2024-02-09 18:02:36 -0500 | [diff] [blame] | 346 | BOOST_TEST_INFO_SCOPE("KeyName = " << keyName); |
| 347 | BOOST_TEST(keyNames.insert(keyName).second); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 351 | BOOST_AUTO_TEST_SUITE_END() // TestTpmBackEnd |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 352 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 353 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 354 | } // namespace ndn::tests |