blob: 7c36328a06087f88c2aa91c40fd8bfd9bd2f7bb3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -08002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu28fd32f2014-01-28 19:03:03 -080020 */
21
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080022#include "security/v1/sec-tpm-osx.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070023#include "security/v1/cryptopp.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070024#include "util/time.hpp"
25
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026#include "boost-test.hpp"
27
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include <boost/lexical_cast.hpp>
Yingdi Yu9d9d5992014-06-25 12:25:16 -070029#include <Availability.h>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080031namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070032namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080033namespace v1 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080034namespace tests {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080035
Yingdi Yu41546342014-11-30 23:37:53 -080036class OsxKeyChainTestFixture
37{
38public:
39 OsxKeyChainTestFixture()
40 {
41 std::string oldHOME;
42 if (std::getenv("OLD_HOME"))
43 oldHOME = std::getenv("OLD_HOME");
44
45 if (std::getenv("HOME"))
46 m_HOME = std::getenv("HOME");
47
48 if (!oldHOME.empty())
49 setenv("HOME", oldHOME.c_str(), 1);
50 else
51 unsetenv("HOME");
52 }
53
54 ~OsxKeyChainTestFixture()
55 {
56 if (!m_HOME.empty())
57 setenv("HOME", m_HOME.c_str(), 1);
58 else
59 unsetenv("HOME");
60 }
61
62protected:
63 std::string m_HOME;
64};
65
Davide Pesaventoeee3e822016-11-26 19:19:34 +010066BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080067BOOST_AUTO_TEST_SUITE(V1)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010068BOOST_FIXTURE_TEST_SUITE(TestSecTpmOsx, OsxKeyChainTestFixture)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080069
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070070BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071{
72 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070073
Yingdi Yu5e96e002014-04-23 18:32:15 -070074 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070075 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -070076 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu7036ce22014-06-19 18:53:37 -070077 RsaKeyParams params(2048);
78 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079
Yingdi Yu99b2a002015-08-12 12:47:44 -070080 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
81 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082
Yingdi Yu28fd32f2014-01-28 19:03:03 -080083 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070084
Yingdi Yu99b2a002015-08-12 12:47:44 -070085 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
86 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080087}
88
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070089BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080090{
91 SecTpmOsx tpm;
92
Yingdi Yu5e96e002014-04-23 18:32:15 -070093 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070094 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -070095 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu7036ce22014-06-19 18:53:37 -070096 RsaKeyParams params(2048);
97 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070098
Yingdi Yube4150e2014-02-18 13:02:46 -080099 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800100 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
101
Yingdi Yu2e57a582014-02-20 23:34:43 -0800102 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700103 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700104 keyName, DigestAlgorithm::SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800105
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700106 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700107 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800108 try
109 {
110 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800111
Yingdi Yu7036ce22014-06-19 18:53:37 -0700112 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800113 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700114 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
115 rsaPublicKey.Load(queue);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800116
Yingdi Yu7036ce22014-06-19 18:53:37 -0700117 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
118 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
119 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700120
Yingdi Yu7036ce22014-06-19 18:53:37 -0700121 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800122 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700123 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800124 {
125 BOOST_CHECK(false);
126 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800127
128 tpm.deleteKeyPairInTpm(keyName);
129}
130
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700131BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -0800132{
133 SecTpmOsx tpm;
134
135 size_t scale = 1000;
136 size_t size = 256 * scale;
137 uint8_t* block = new uint8_t[size];
138 tpm.generateRandomBlock(block, size);
139
Yingdi Yu7036ce22014-06-19 18:53:37 -0700140 std::map<uint8_t, int> counter;
141 for (size_t i = 0; i < size; i++)
142 {
143 counter[block[i]] += 1;
144 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800145
146 float dev = 0.0;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700147 for (size_t i = 0; i != 255; i++)
148 {
149 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
150 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800151
152 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
153
154}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800155
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700156BOOST_AUTO_TEST_CASE(ExportImportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800157{
158 using namespace CryptoPP;
159
160 SecTpmOsx tpm;
161
Yingdi Yu5e96e002014-04-23 18:32:15 -0700162 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -0700163 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -0700164 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700165
Yingdi Yu7036ce22014-06-19 18:53:37 -0700166 RsaKeyParams params(2048);
167 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800168
Yingdi Yu99b2a002015-08-12 12:47:44 -0700169 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
170 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800171
Yingdi Yu2e57a582014-02-20 23:34:43 -0800172 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700173 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700174 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700175 BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800176
177 tpm.deleteKeyPairInTpm(keyName);
178
Yingdi Yu99b2a002015-08-12 12:47:44 -0700179 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
180 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800181
Yingdi Yu5e96e002014-04-23 18:32:15 -0700182 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
183 exported->buf(), exported->size(),
184 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700185
Yingdi Yu99b2a002015-08-12 12:47:44 -0700186 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
187 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800188
189 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800190 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700191 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700192 keyName, DigestAlgorithm::SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800193
Yingdi Yu2e57a582014-02-20 23:34:43 -0800194 try
195 {
196 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800197
Yingdi Yu7036ce22014-06-19 18:53:37 -0700198 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800199 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700200 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
201 rsaPublicKey.Load(queue);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800202
Yingdi Yu7036ce22014-06-19 18:53:37 -0700203 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
204 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
205 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700206
Yingdi Yu7036ce22014-06-19 18:53:37 -0700207 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800208 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700209 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800210 {
211 BOOST_CHECK(false);
212 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700213
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800214 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700215 // This is some problem related to Mac OS Key chain.
216 // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9.
217#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
218#if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9
Yingdi Yu99b2a002015-08-12 12:47:44 -0700219 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE) == false);
220 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC) == false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700221#endif
222#endif
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800223}
224
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700225BOOST_AUTO_TEST_CASE(NonExistingKey)
226{
227 using namespace CryptoPP;
228
229 SecTpmOsx tpm;
230
231 Name keyName("/TestSecTpmOsx/NonExistingKey");
232
233 BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error);
234
235 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu99b2a002015-08-12 12:47:44 -0700236 BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DigestAlgorithm::SHA256),
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700237 SecTpmOsx::Error);
238
Yingdi Yu99b2a002015-08-12 12:47:44 -0700239 BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DigestAlgorithm::SHA256),
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700240 SecTpmOsx::Error);
241}
242
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700243BOOST_AUTO_TEST_CASE(EcdsaSigning)
244{
245 SecTpmOsx tpm;
246
247 Name keyName("/TestSecTpmOsx/EcdsaSigning/ksk-" +
248 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700249 EcKeyParams params;
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700250 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
251
252 Data data("/TestSecTpmOsx/EcdsaSigning/Data/1");
253 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
254
255 Block sigBlock;
256 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700257 keyName, DigestAlgorithm::SHA256));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700258
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700259 shared_ptr<v1::PublicKey> pubkeyPtr;
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700260 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
261
262 try
263 {
264 using namespace CryptoPP;
265
266 ECDSA<ECP, SHA256>::PublicKey publicKey;
267 ByteQueue queue;
268 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
269 publicKey.Load(queue);
270
271 uint8_t buffer[64];
272 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
273 sigBlock.value(), sigBlock.value_size(), DSA_DER);
274
275 ECDSA<ECP, SHA256>::Verifier verifier(publicKey);
276 bool result = verifier.VerifyMessage(content, sizeof(content),
277 buffer, usedSize);
278
279 BOOST_CHECK_EQUAL(result, true);
280 }
281 catch (CryptoPP::Exception& e)
282 {
283 BOOST_CHECK(false);
284 }
285
286 tpm.deleteKeyPairInTpm(keyName);
287}
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700288
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700289
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700290BOOST_AUTO_TEST_CASE(ExportImportEcKey)
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700291{
292 using namespace CryptoPP;
293
294 SecTpmOsx tpm;
295
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700296 Name keyName("/TestSecTpmOsx/ExportImportEcKey/ksk-" +
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700297 boost::lexical_cast<std::string>(
298 time::toUnixTimestamp(time::system_clock::now()).count()));
299
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700300 EcKeyParams params;
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700301 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
302
Yingdi Yu99b2a002015-08-12 12:47:44 -0700303 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
304 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700305
306 ConstBufferPtr exported;
307 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
308
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700309 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700310 BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
311
312 tpm.deleteKeyPairInTpm(keyName);
313
Yingdi Yu99b2a002015-08-12 12:47:44 -0700314 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
315 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700316
317 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
318 exported->buf(), exported->size(),
319 "1234"));
320
Yingdi Yu99b2a002015-08-12 12:47:44 -0700321 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
322 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700323
324 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
325 Block sigBlock;
326 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700327 keyName, DigestAlgorithm::SHA256));
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700328
329 try
330 {
331 using namespace CryptoPP;
332
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700333 ECDSA<ECP, SHA256>::PublicKey ecPublicKey;
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700334 ByteQueue queue;
335 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700336 ecPublicKey.Load(queue);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700337
338 uint8_t buffer[64];
339 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
340 sigBlock.value(), sigBlock.value_size(),
341 DSA_DER);
342
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700343 ECDSA<ECP, SHA256>::Verifier verifier(ecPublicKey);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700344 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
345 buffer, usedSize);
346
347 BOOST_CHECK_EQUAL(isVerified, true);
348 }
349 catch (CryptoPP::Exception& e)
350 {
351 BOOST_CHECK(false);
352 }
353
354 tpm.deleteKeyPairInTpm(keyName);
355 // This is some problem related to Mac OS Key chain.
356 // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9.
357#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
358#if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9
Yingdi Yu99b2a002015-08-12 12:47:44 -0700359 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE) == false);
360 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC) == false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700361#endif
362#endif
363}
364
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100365BOOST_AUTO_TEST_SUITE_END() // TestSecTpmOsx
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800366BOOST_AUTO_TEST_SUITE_END() // V1
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100367BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800368
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800369} // namespace tests
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800370} // namespace v1
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700371} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800372} // namespace ndn