blob: a7d1d8067ae4f1c44b8b5aac730b1ae824a209c4 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-tpm-osx.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070023#include "security/cryptopp.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080024
Yingdi Yuf56c68f2014-04-24 21:50:13 -070025#include "util/time.hpp"
26
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070027#include <boost/lexical_cast.hpp>
Yingdi Yu9d9d5992014-06-25 12:25:16 -070028#include <Availability.h>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
31
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080033
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070034BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080035
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070036BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037{
38 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070039
Yingdi Yu5e96e002014-04-23 18:32:15 -070040 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070041 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -070042 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu7036ce22014-06-19 18:53:37 -070043 RsaKeyParams params(2048);
44 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070045
Yingdi Yu28fd32f2014-01-28 19:03:03 -080046 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
47 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070048
Yingdi Yu28fd32f2014-01-28 19:03:03 -080049 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070050
Yingdi Yu28fd32f2014-01-28 19:03:03 -080051 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
52 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
53}
54
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070055BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080056{
57 SecTpmOsx tpm;
58
Yingdi Yu5e96e002014-04-23 18:32:15 -070059 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070060 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -070061 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu7036ce22014-06-19 18:53:37 -070062 RsaKeyParams params(2048);
63 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070064
Yingdi Yube4150e2014-02-18 13:02:46 -080065 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080066 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
67
Yingdi Yu2e57a582014-02-20 23:34:43 -080068 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070069 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
70 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071
Yingdi Yu7036ce22014-06-19 18:53:37 -070072 shared_ptr<PublicKey> publicKey;
73 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 try
75 {
76 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080077
Yingdi Yu7036ce22014-06-19 18:53:37 -070078 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -080079 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -070080 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
81 rsaPublicKey.Load(queue);
Yingdi Yu2e57a582014-02-20 23:34:43 -080082
Yingdi Yu7036ce22014-06-19 18:53:37 -070083 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
84 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
85 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086
Yingdi Yu7036ce22014-06-19 18:53:37 -070087 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -080088 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070089 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080090 {
91 BOOST_CHECK(false);
92 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080093
94 tpm.deleteKeyPairInTpm(keyName);
95}
96
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070097BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -080098{
99 SecTpmOsx tpm;
100
101 size_t scale = 1000;
102 size_t size = 256 * scale;
103 uint8_t* block = new uint8_t[size];
104 tpm.generateRandomBlock(block, size);
105
Yingdi Yu7036ce22014-06-19 18:53:37 -0700106 std::map<uint8_t, int> counter;
107 for (size_t i = 0; i < size; i++)
108 {
109 counter[block[i]] += 1;
110 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800111
112 float dev = 0.0;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700113 for (size_t i = 0; i != 255; i++)
114 {
115 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
116 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800117
118 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
119
120}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800121
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700122BOOST_AUTO_TEST_CASE(ExportImportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123{
124 using namespace CryptoPP;
125
126 SecTpmOsx tpm;
127
Yingdi Yu5e96e002014-04-23 18:32:15 -0700128 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -0700129 boost::lexical_cast<std::string>(
Yingdi Yu5e96e002014-04-23 18:32:15 -0700130 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700131
Yingdi Yu7036ce22014-06-19 18:53:37 -0700132 RsaKeyParams params(2048);
133 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800134
Yingdi Yu7036ce22014-06-19 18:53:37 -0700135 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
136 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800137
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700139 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Yingdi Yu7036ce22014-06-19 18:53:37 -0700140 shared_ptr<PublicKey> publicKey;
141 BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800142
143 tpm.deleteKeyPairInTpm(keyName);
144
Yingdi Yu7036ce22014-06-19 18:53:37 -0700145 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
146 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800147
Yingdi Yu5e96e002014-04-23 18:32:15 -0700148 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
149 exported->buf(), exported->size(),
150 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700151
Yingdi Yu7036ce22014-06-19 18:53:37 -0700152 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
153 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800154
155 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800156 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700157 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
158 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800159
Yingdi Yu2e57a582014-02-20 23:34:43 -0800160 try
161 {
162 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800163
Yingdi Yu7036ce22014-06-19 18:53:37 -0700164 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800165 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700166 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
167 rsaPublicKey.Load(queue);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800168
Yingdi Yu7036ce22014-06-19 18:53:37 -0700169 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
170 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
171 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172
Yingdi Yu7036ce22014-06-19 18:53:37 -0700173 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800174 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700175 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800176 {
177 BOOST_CHECK(false);
178 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700179
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800180 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700181 // This is some problem related to Mac OS Key chain.
182 // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9.
183#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
184#if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9
185 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
186 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
187#endif
188#endif
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800189}
190
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700191BOOST_AUTO_TEST_CASE(NonExistingKey)
192{
193 using namespace CryptoPP;
194
195 SecTpmOsx tpm;
196
197 Name keyName("/TestSecTpmOsx/NonExistingKey");
198
199 BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error);
200
201 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
202 BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256),
203 SecTpmOsx::Error);
204
205 BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DIGEST_ALGORITHM_SHA256),
206 SecTpmOsx::Error);
207}
208
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700209BOOST_AUTO_TEST_CASE(EcdsaSigning)
210{
211 SecTpmOsx tpm;
212
213 Name keyName("/TestSecTpmOsx/EcdsaSigning/ksk-" +
214 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
215 EcdsaKeyParams params;
216 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
217
218 Data data("/TestSecTpmOsx/EcdsaSigning/Data/1");
219 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
220
221 Block sigBlock;
222 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
223 keyName, DIGEST_ALGORITHM_SHA256));
224
225 shared_ptr<PublicKey> pubkeyPtr;
226 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
227
228 try
229 {
230 using namespace CryptoPP;
231
232 ECDSA<ECP, SHA256>::PublicKey publicKey;
233 ByteQueue queue;
234 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
235 publicKey.Load(queue);
236
237 uint8_t buffer[64];
238 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
239 sigBlock.value(), sigBlock.value_size(), DSA_DER);
240
241 ECDSA<ECP, SHA256>::Verifier verifier(publicKey);
242 bool result = verifier.VerifyMessage(content, sizeof(content),
243 buffer, usedSize);
244
245 BOOST_CHECK_EQUAL(result, true);
246 }
247 catch (CryptoPP::Exception& e)
248 {
249 BOOST_CHECK(false);
250 }
251
252 tpm.deleteKeyPairInTpm(keyName);
253}
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700254
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700255
256BOOST_AUTO_TEST_CASE(ExportImportEcdsaKey)
257{
258 using namespace CryptoPP;
259
260 SecTpmOsx tpm;
261
262 Name keyName("/TestSecTpmOsx/ExportImportEcdsaKey/ksk-" +
263 boost::lexical_cast<std::string>(
264 time::toUnixTimestamp(time::system_clock::now()).count()));
265
266 EcdsaKeyParams params;
267 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
268
269 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
270 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
271
272 ConstBufferPtr exported;
273 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
274
275 shared_ptr<PublicKey> publicKey;
276 BOOST_REQUIRE_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
277
278 tpm.deleteKeyPairInTpm(keyName);
279
280 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
281 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
282
283 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
284 exported->buf(), exported->size(),
285 "1234"));
286
287 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
288 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
289
290 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
291 Block sigBlock;
292 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
293 keyName, DIGEST_ALGORITHM_SHA256));
294
295 try
296 {
297 using namespace CryptoPP;
298
299 ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey;
300 ByteQueue queue;
301 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
302 ecdsaPublicKey.Load(queue);
303
304 uint8_t buffer[64];
305 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
306 sigBlock.value(), sigBlock.value_size(),
307 DSA_DER);
308
309 ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey);
310 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
311 buffer, usedSize);
312
313 BOOST_CHECK_EQUAL(isVerified, true);
314 }
315 catch (CryptoPP::Exception& e)
316 {
317 BOOST_CHECK(false);
318 }
319
320 tpm.deleteKeyPairInTpm(keyName);
321 // This is some problem related to Mac OS Key chain.
322 // On OSX 10.8, we cannot delete imported keys, but there is no such problem on OSX 10.9.
323#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
324#if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_9
325 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
326 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
327#endif
328#endif
329}
330
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800331BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800332
333} // namespace ndn