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