Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 2 | /** |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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. |
| 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. |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 22 | #include "security/sec-tpm-osx.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 23 | #include "security/cryptopp.hpp" |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 24 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 25 | #include "util/time.hpp" |
| 26 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 28 | #include <Availability.h> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 30 | #include "boost-test.hpp" |
| 31 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 32 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 33 | namespace tests { |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 35 | class OsxKeyChainTestFixture |
| 36 | { |
| 37 | public: |
| 38 | OsxKeyChainTestFixture() |
| 39 | { |
| 40 | std::string oldHOME; |
| 41 | if (std::getenv("OLD_HOME")) |
| 42 | oldHOME = std::getenv("OLD_HOME"); |
| 43 | |
| 44 | if (std::getenv("HOME")) |
| 45 | m_HOME = std::getenv("HOME"); |
| 46 | |
| 47 | if (!oldHOME.empty()) |
| 48 | setenv("HOME", oldHOME.c_str(), 1); |
| 49 | else |
| 50 | unsetenv("HOME"); |
| 51 | } |
| 52 | |
| 53 | ~OsxKeyChainTestFixture() |
| 54 | { |
| 55 | if (!m_HOME.empty()) |
| 56 | setenv("HOME", m_HOME.c_str(), 1); |
| 57 | else |
| 58 | unsetenv("HOME"); |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | std::string m_HOME; |
| 63 | }; |
| 64 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 65 | BOOST_FIXTURE_TEST_SUITE(SecuritySecTpmOsx, OsxKeyChainTestFixture) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(Delete) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 68 | { |
| 69 | SecTpmOsx tpm; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 71 | Name keyName("/TestSecTpmOsx/Delete/ksk-" + |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 72 | boost::lexical_cast<std::string>( |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 73 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 74 | RsaKeyParams params(2048); |
| 75 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 76 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 77 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true); |
| 78 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 79 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 80 | tpm.deleteKeyPairInTpm(keyName); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 82 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false); |
| 83 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 86 | BOOST_AUTO_TEST_CASE(SignVerify) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 87 | { |
| 88 | SecTpmOsx tpm; |
| 89 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 90 | Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 91 | boost::lexical_cast<std::string>( |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 92 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 93 | RsaKeyParams params(2048); |
| 94 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 95 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 96 | Data data("/TestSecTpmOsx/SignVaerify/Data/1"); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 97 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 98 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 99 | Block sigBlock; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 100 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 101 | keyName, DigestAlgorithm::SHA256)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 103 | shared_ptr<PublicKey> publicKey; |
| 104 | BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName)); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 105 | try |
| 106 | { |
| 107 | using namespace CryptoPP; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 108 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 109 | RSA::PublicKey rsaPublicKey; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 110 | ByteQueue queue; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 111 | queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size()); |
| 112 | rsaPublicKey.Load(queue); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 113 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 114 | RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey); |
| 115 | bool isVerified = verifier.VerifyMessage(content, sizeof(content), |
| 116 | sigBlock.value(), sigBlock.value_size()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 117 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(isVerified, true); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 119 | } |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 120 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 121 | { |
| 122 | BOOST_CHECK(false); |
| 123 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 124 | |
| 125 | tpm.deleteKeyPairInTpm(keyName); |
| 126 | } |
| 127 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(RandomGenerator) |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 129 | { |
| 130 | SecTpmOsx tpm; |
| 131 | |
| 132 | size_t scale = 1000; |
| 133 | size_t size = 256 * scale; |
| 134 | uint8_t* block = new uint8_t[size]; |
| 135 | tpm.generateRandomBlock(block, size); |
| 136 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 137 | std::map<uint8_t, int> counter; |
| 138 | for (size_t i = 0; i < size; i++) |
| 139 | { |
| 140 | counter[block[i]] += 1; |
| 141 | } |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 142 | |
| 143 | float dev = 0.0; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 144 | for (size_t i = 0; i != 255; i++) |
| 145 | { |
| 146 | dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale); |
| 147 | } |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 148 | |
| 149 | BOOST_CHECK_CLOSE(dev / 256, 0.001, 100); |
| 150 | |
| 151 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 153 | BOOST_AUTO_TEST_CASE(ExportImportKey) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 154 | { |
| 155 | using namespace CryptoPP; |
| 156 | |
| 157 | SecTpmOsx tpm; |
| 158 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 159 | Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 160 | boost::lexical_cast<std::string>( |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 161 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 162 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 163 | RsaKeyParams params(2048); |
| 164 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 165 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 166 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true); |
| 167 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 168 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 169 | ConstBufferPtr exported; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 170 | BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234")); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 171 | shared_ptr<PublicKey> publicKey; |
| 172 | BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 173 | |
| 174 | tpm.deleteKeyPairInTpm(keyName); |
| 175 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false); |
| 177 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 178 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 179 | BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, |
| 180 | exported->buf(), exported->size(), |
| 181 | "1234")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 182 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 183 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true); |
| 184 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 185 | |
| 186 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 187 | Block sigBlock; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 188 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 189 | keyName, DigestAlgorithm::SHA256)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 190 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 191 | try |
| 192 | { |
| 193 | using namespace CryptoPP; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 194 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 195 | RSA::PublicKey rsaPublicKey; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 196 | ByteQueue queue; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 197 | queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size()); |
| 198 | rsaPublicKey.Load(queue); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 199 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 200 | RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey); |
| 201 | bool isVerified = verifier.VerifyMessage(content, sizeof(content), |
| 202 | sigBlock.value(), sigBlock.value_size()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 203 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 204 | BOOST_CHECK_EQUAL(isVerified, true); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 205 | } |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 206 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 207 | { |
| 208 | BOOST_CHECK(false); |
| 209 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 210 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 211 | tpm.deleteKeyPairInTpm(keyName); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 212 | // This is some problem related to Mac OS Key chain. |
| 213 | // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9. |
| 214 | #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED |
| 215 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9 |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 216 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE) == false); |
| 217 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC) == false); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 218 | #endif |
| 219 | #endif |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 222 | BOOST_AUTO_TEST_CASE(NonExistingKey) |
| 223 | { |
| 224 | using namespace CryptoPP; |
| 225 | |
| 226 | SecTpmOsx tpm; |
| 227 | |
| 228 | Name keyName("/TestSecTpmOsx/NonExistingKey"); |
| 229 | |
| 230 | BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error); |
| 231 | |
| 232 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 233 | BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DigestAlgorithm::SHA256), |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 234 | SecTpmOsx::Error); |
| 235 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 236 | BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DigestAlgorithm::SHA256), |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 237 | SecTpmOsx::Error); |
| 238 | } |
| 239 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 240 | BOOST_AUTO_TEST_CASE(EcdsaSigning) |
| 241 | { |
| 242 | SecTpmOsx tpm; |
| 243 | |
| 244 | Name keyName("/TestSecTpmOsx/EcdsaSigning/ksk-" + |
| 245 | boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now()))); |
| 246 | EcdsaKeyParams params; |
| 247 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params)); |
| 248 | |
| 249 | Data data("/TestSecTpmOsx/EcdsaSigning/Data/1"); |
| 250 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 251 | |
| 252 | Block sigBlock; |
| 253 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 254 | keyName, DigestAlgorithm::SHA256)); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 255 | |
| 256 | shared_ptr<PublicKey> pubkeyPtr; |
| 257 | BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName)); |
| 258 | |
| 259 | try |
| 260 | { |
| 261 | using namespace CryptoPP; |
| 262 | |
| 263 | ECDSA<ECP, SHA256>::PublicKey publicKey; |
| 264 | ByteQueue queue; |
| 265 | queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size()); |
| 266 | publicKey.Load(queue); |
| 267 | |
| 268 | uint8_t buffer[64]; |
| 269 | size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363, |
| 270 | sigBlock.value(), sigBlock.value_size(), DSA_DER); |
| 271 | |
| 272 | ECDSA<ECP, SHA256>::Verifier verifier(publicKey); |
| 273 | bool result = verifier.VerifyMessage(content, sizeof(content), |
| 274 | buffer, usedSize); |
| 275 | |
| 276 | BOOST_CHECK_EQUAL(result, true); |
| 277 | } |
| 278 | catch (CryptoPP::Exception& e) |
| 279 | { |
| 280 | BOOST_CHECK(false); |
| 281 | } |
| 282 | |
| 283 | tpm.deleteKeyPairInTpm(keyName); |
| 284 | } |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 285 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 286 | |
| 287 | BOOST_AUTO_TEST_CASE(ExportImportEcdsaKey) |
| 288 | { |
| 289 | using namespace CryptoPP; |
| 290 | |
| 291 | SecTpmOsx tpm; |
| 292 | |
| 293 | Name keyName("/TestSecTpmOsx/ExportImportEcdsaKey/ksk-" + |
| 294 | boost::lexical_cast<std::string>( |
| 295 | time::toUnixTimestamp(time::system_clock::now()).count())); |
| 296 | |
| 297 | EcdsaKeyParams params; |
| 298 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params)); |
| 299 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 300 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true); |
| 301 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 302 | |
| 303 | ConstBufferPtr exported; |
| 304 | BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234")); |
| 305 | |
| 306 | shared_ptr<PublicKey> publicKey; |
| 307 | BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName)); |
| 308 | |
| 309 | tpm.deleteKeyPairInTpm(keyName); |
| 310 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 311 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false); |
| 312 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 313 | |
| 314 | BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, |
| 315 | exported->buf(), exported->size(), |
| 316 | "1234")); |
| 317 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 318 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true); |
| 319 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 320 | |
| 321 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 322 | Block sigBlock; |
| 323 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 324 | keyName, DigestAlgorithm::SHA256)); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 325 | |
| 326 | try |
| 327 | { |
| 328 | using namespace CryptoPP; |
| 329 | |
| 330 | ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey; |
| 331 | ByteQueue queue; |
| 332 | queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size()); |
| 333 | ecdsaPublicKey.Load(queue); |
| 334 | |
| 335 | uint8_t buffer[64]; |
| 336 | size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363, |
| 337 | sigBlock.value(), sigBlock.value_size(), |
| 338 | DSA_DER); |
| 339 | |
| 340 | ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey); |
| 341 | bool isVerified = verifier.VerifyMessage(content, sizeof(content), |
| 342 | buffer, usedSize); |
| 343 | |
| 344 | BOOST_CHECK_EQUAL(isVerified, true); |
| 345 | } |
| 346 | catch (CryptoPP::Exception& e) |
| 347 | { |
| 348 | BOOST_CHECK(false); |
| 349 | } |
| 350 | |
| 351 | tpm.deleteKeyPairInTpm(keyName); |
| 352 | // This is some problem related to Mac OS Key chain. |
| 353 | // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9. |
| 354 | #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED |
| 355 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9 |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 356 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE) == false); |
| 357 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC) == false); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 358 | #endif |
| 359 | #endif |
| 360 | } |
| 361 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 362 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 363 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 364 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 365 | } // namespace ndn |