blob: c49fcd7169b4a534de8665b3302cb5db84730d52 [file] [log] [blame]
Yingdi Yu2d9c50f2014-01-21 18:25:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Xingyu Ma <http://www.linkedin.com/pub/xingyu-ma/1a/384/5a8>
13 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
14 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080015 */
16
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080017#include "common.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080018
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080019#include "sec-tpm-file.hpp"
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080020
21#include <boost/filesystem.hpp>
22#include <boost/algorithm/string.hpp>
23
Junxiao Shi482ccc52014-03-31 13:05:24 -070024#include "cryptopp.hpp"
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080025
26#include <sys/types.h>
27#include <sys/stat.h>
28
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080029#include <algorithm>
30
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080031using namespace std;
32
Yingdi Yufc40d872014-02-18 12:56:04 -080033namespace ndn {
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080034
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070035class SecTpmFile::Impl
36{
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080037public:
Yingdi Yu4b752752014-02-18 12:24:03 -080038 Impl(const string& dir)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080039 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 if (dir.empty())
Yingdi Yu37e317f2014-03-19 12:16:23 -070041 m_keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndn" / "ndnsec-tpm-file";
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080042 else
43 m_keystorePath = dir;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080045 boost::filesystem::create_directories (m_keystorePath);
46 }
47
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080048 boost::filesystem::path
49 nameTransform(const string& keyName, const string& extension)
50 {
51 using namespace CryptoPP;
52 string digest;
53 SHA256 hash;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 StringSource src(keyName,
55 true,
56 new HashFilter(hash,
57 new Base64Encoder(new CryptoPP::StringSink(digest))));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080058
59 boost::algorithm::trim(digest);
60 std::replace(digest.begin(), digest.end(), '/', '%');
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080062 return m_keystorePath / (digest + extension);
63 }
64
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065 string
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080066 maintainMapping(const string& keyName)
67 {
68 string keyFileName = nameTransform(keyName, "").string();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080070 ofstream outfile;
71 string dirFile = (m_keystorePath / "mapping.txt").string();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080073 outfile.open(dirFile.c_str(), std::ios_base::app);
74 outfile << keyName << ' ' << keyFileName << '\n';
75 outfile.close();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080077 return keyFileName;
78 }
79
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080080public:
81 boost::filesystem::path m_keystorePath;
82};
83
Yingdi Yu4b752752014-02-18 12:24:03 -080084
Yingdi Yube4150e2014-02-18 13:02:46 -080085SecTpmFile::SecTpmFile(const string& dir)
Yingdi Yu4b752752014-02-18 12:24:03 -080086 : m_impl(new Impl(dir))
Yingdi Yube4150e2014-02-18 13:02:46 -080087 , m_inTerminal(false)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080088{}
89
90void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091SecTpmFile::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080092{
93 string keyURI = keyName.toUri();
94
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 if (doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080096 throw Error("public key exists");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 if (doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080098 throw Error("private key exists");
99
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800100 string keyFileName = m_impl->maintainMapping(keyURI);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800101
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700102 try
103 {
104 switch (keyType)
105 {
106 case KEY_TYPE_RSA:
107 {
108 using namespace CryptoPP;
109 AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800110
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700111 InvertibleRSAFunction privateKey;
112 privateKey.Initialize(rng, keySize);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700114 string privateKeyFileName = keyFileName + ".pri";
115 Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str()));
116 privateKey.DEREncode(privateKeySink);
117 privateKeySink.MessageEnd();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119 RSAFunction publicKey(privateKey);
120 string publicKeyFileName = keyFileName + ".pub";
121 Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str()));
122 publicKey.DEREncode(publicKeySink);
123 publicKeySink.MessageEnd();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 /*set file permission*/
126 chmod(privateKeyFileName.c_str(), 0000400);
127 chmod(publicKeyFileName.c_str(), 0000444);
128 return;
129 }
130 default:
131 throw Error("Unsupported key type!");
132 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800133 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700134 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700135 {
136 throw Error(e.what());
137 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800138}
139
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800140void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141SecTpmFile::deleteKeyPairInTpm(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800142{
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800143 boost::filesystem::path publicKeyPath(m_impl->nameTransform(keyName.toUri(), ".pub"));
144 boost::filesystem::path privateKeyPath(m_impl->nameTransform(keyName.toUri(), ".pri"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800145
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 if (boost::filesystem::exists(publicKeyPath))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800147 boost::filesystem::remove(publicKeyPath);
148
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149 if (boost::filesystem::exists(privateKeyPath))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800150 boost::filesystem::remove(privateKeyPath);
151}
152
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153shared_ptr<PublicKey>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154SecTpmFile::getPublicKeyFromTpm(const Name& keyName)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800155{
156 string keyURI = keyName.toUri();
157
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu5e96e002014-04-23 18:32:15 -0700159 throw Error("Public Key does not exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800160
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800161 ostringstream os;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700162 try
163 {
164 using namespace CryptoPP;
165 FileSource(m_impl->nameTransform(keyURI, ".pub").string().c_str(),
166 true,
167 new Base64Decoder(new FileSink(os)));
168 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700169 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700170 {
171 throw Error(e.what());
172 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800173
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700174 return make_shared<PublicKey>(reinterpret_cast<const uint8_t*>(os.str().c_str()),
175 os.str().size());
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800176}
177
178ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700179SecTpmFile::exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800180{
181 OBufferStream privateKeyOs;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700182 CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true,
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800183 new CryptoPP::Base64Decoder(new CryptoPP::FileSink(privateKeyOs)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700184
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800185 return privateKeyOs.buf();
186}
187
188bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700189SecTpmFile::importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800190{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700191 try
192 {
193 using namespace CryptoPP;
194
195 string keyFileName = m_impl->maintainMapping(keyName.toUri());
196 keyFileName.append(".pri");
197 StringSource(buf, size,
198 true,
199 new Base64Encoder(new FileSink(keyFileName.c_str())));
200 return true;
201 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700202 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700203 {
204 return false;
205 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800206}
207
208bool
209SecTpmFile::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
210{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700211 try
212 {
213 using namespace CryptoPP;
214
215 string keyFileName = m_impl->maintainMapping(keyName.toUri());
216 keyFileName.append(".pub");
217 StringSource(buf, size,
218 true,
219 new Base64Encoder(new FileSink(keyFileName.c_str())));
220 return true;
221 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700222 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700223 {
224 return false;
225 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800226}
227
228Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700229SecTpmFile::signInTpm(const uint8_t* data, size_t dataLength,
230 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800231{
232 string keyURI = keyName.toUri();
233
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700234 if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800235 throw Error("private key doesn't exists");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700236
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700237 try
238 {
239 using namespace CryptoPP;
240 AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800241
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700242 //Read private key
243 ByteQueue bytes;
244 FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(),
245 true, new Base64Decoder);
246 file.TransferTo(bytes);
247 bytes.MessageEnd();
248 RSA::PrivateKey privateKey;
249 privateKey.Load(bytes);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700250
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700251 //Sign message
252 switch (digestAlgorithm)
253 {
254 case DIGEST_ALGORITHM_SHA256:
255 {
256 RSASS<PKCS1v15, SHA256>::Signer signer(privateKey);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700257
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700258 OBufferStream os;
259 StringSource(data, dataLength,
260 true,
261 new SignerFilter(rng, signer, new FileSink(os)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700262
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700263 return Block(Tlv::SignatureValue, os.buf());
264 }
265 default:
266 throw Error("Unsupported digest algorithm!");
267 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800268 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700269 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700270 {
271 throw Error(e.what());
272 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800273}
274
275
276ConstBufferPtr
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700277SecTpmFile::decryptInTpm(const uint8_t* data, size_t dataLength,
278 const Name& keyName, bool isSymmetric)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800279{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800280 throw Error("SecTpmFile::decryptInTpm is not supported!");
281 // string keyURI = keyName.toUri();
282 // if (!isSymmetric)
283 // {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700284 // if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800285 // throw Error("private key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800286
Yingdi Yu2e57a582014-02-20 23:34:43 -0800287 // try{
288 // using namespace CryptoPP;
289 // AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800290
Yingdi Yu2e57a582014-02-20 23:34:43 -0800291 // //Read private key
292 // ByteQueue bytes;
293 // FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(), true, new Base64Decoder);
294 // file.TransferTo(bytes);
295 // bytes.MessageEnd();
296 // RSA::PrivateKey privateKey;
297 // privateKey.Load(bytes);
298 // RSAES_PKCS1v15_Decryptor decryptor(privateKey);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700299
Yingdi Yu2e57a582014-02-20 23:34:43 -0800300 // OBufferStream os;
301 // StringSource(data, dataLength, true, new PK_DecryptorFilter(rng, decryptor, new FileSink(os)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700302
Yingdi Yu2e57a582014-02-20 23:34:43 -0800303 // return os.buf();
304 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700305 // catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800306 // throw Error(e.what());
307 // }
308 // }
309 // else
310 // {
311 // throw Error("Symmetric encryption is not implemented!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700312 // // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800313 // // throw Error("symmetric key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800314
Yingdi Yu2e57a582014-02-20 23:34:43 -0800315 // // try{
316 // // string keyBits;
317 // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key");
318 // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700319
Yingdi Yu2e57a582014-02-20 23:34:43 -0800320 // // using CryptoPP::AES;
321 // // AutoSeededRandomPool rnd;
322 // // byte iv[AES::BLOCKSIZE];
323 // // rnd.GenerateBlock(iv, AES::BLOCKSIZE);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800324
Yingdi Yu2e57a582014-02-20 23:34:43 -0800325 // // CFB_Mode<AES>::Decryption decryptor;
326 // // decryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700327
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 // // OBufferStream os;
329 // // StringSource(data, dataLength, true, new StreamTransformationFilter(decryptor,new FileSink(os)));
330 // // return os.buf();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800331
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700332 // // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 // // throw Error(e.what());
334 // // }
335 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800336}
337
338ConstBufferPtr
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700339SecTpmFile::encryptInTpm(const uint8_t* data, size_t dataLength,
340 const Name& keyName, bool isSymmetric)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800341{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800342 throw Error("SecTpmFile::encryptInTpm is not supported!");
343 // string keyURI = keyName.toUri();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800344
Yingdi Yu2e57a582014-02-20 23:34:43 -0800345 // if (!isSymmetric)
346 // {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700347 // if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800348 // throw Error("public key doesn't exist");
349 // try
350 // {
351 // using namespace CryptoPP;
352 // AutoSeededRandomPool rng;
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800353
Yingdi Yu2e57a582014-02-20 23:34:43 -0800354 // //Read private key
355 // ByteQueue bytes;
356 // FileSource file(m_impl->nameTransform(keyURI, ".pub").string().c_str(), true, new Base64Decoder);
357 // file.TransferTo(bytes);
358 // bytes.MessageEnd();
359 // RSA::PublicKey publicKey;
360 // publicKey.Load(bytes);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800361
Yingdi Yu2e57a582014-02-20 23:34:43 -0800362 // OBufferStream os;
363 // RSAES_PKCS1v15_Encryptor encryptor(publicKey);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800364
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365 // StringSource(data, dataLength, true, new PK_EncryptorFilter(rng, encryptor, new FileSink(os)));
366 // return os.buf();
367 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700368 // catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369 // throw Error(e.what());
370 // }
371 // }
372 // else
373 // {
374 // throw Error("Symmetric encryption is not implemented!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700375 // // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376 // // throw Error("symmetric key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800377
Yingdi Yu2e57a582014-02-20 23:34:43 -0800378 // // try{
379 // // string keyBits;
380 // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key");
381 // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits)));
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800382
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383 // // using CryptoPP::AES;
384 // // AutoSeededRandomPool rnd;
385 // // byte iv[AES::BLOCKSIZE];
386 // // rnd.GenerateBlock(iv, AES::BLOCKSIZE);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800387
Yingdi Yu2e57a582014-02-20 23:34:43 -0800388 // // CFB_Mode<AES>::Encryption encryptor;
389 // // encryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800390
Yingdi Yu2e57a582014-02-20 23:34:43 -0800391 // // OBufferStream os;
392 // // StringSource(data, dataLength, true, new StreamTransformationFilter(encryptor, new FileSink(os)));
393 // // return os.buf();
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700394 // // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800395 // // throw Error(e.what());
396 // // }
397 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800398}
399
400
401void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700402SecTpmFile::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800403{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800404 throw Error("SecTpmFile::generateSymmetricKeyInTpm is not supported!");
405 // string keyURI = keyName.toUri();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800406
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700407 // if (doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800408 // throw Error("symmetric key exists");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800409
Yingdi Yu2e57a582014-02-20 23:34:43 -0800410 // string keyFileName = m_impl->maintainMapping(keyURI);
411 // string symKeyFileName = keyFileName + ".key";
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800412
Yingdi Yu2e57a582014-02-20 23:34:43 -0800413 // try{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700414 // switch (keyType){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800415 // case KEY_TYPE_AES:
416 // {
417 // using namespace CryptoPP;
418 // AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800419
Yingdi Yu2e57a582014-02-20 23:34:43 -0800420 // SecByteBlock key(0x00, keySize);
421 // rng.GenerateBlock(key, keySize);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700422
Yingdi Yu2e57a582014-02-20 23:34:43 -0800423 // StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str())));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700424
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425 // chmod(symKeyFileName.c_str(), 0000400);
426 // return;
427 // }
428 // default:
429 // throw Error("Unsupported symmetric key type!");
430 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700431 // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800432 // throw Error(e.what());
433 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800434}
435
436bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700437SecTpmFile::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800438{
439 string keyURI = keyName.toUri();
440 if (keyClass == KEY_CLASS_PUBLIC)
441 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700442 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pub")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800443 return true;
444 else
445 return false;
446 }
447 if (keyClass == KEY_CLASS_PRIVATE)
448 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700449 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pri")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800450 return true;
451 else
452 return false;
453 }
454 if (keyClass == KEY_CLASS_SYMMETRIC)
455 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700456 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".key")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800457 return true;
458 else
459 return false;
460 }
461 return false;
462}
463
Yingdi Yu4b752752014-02-18 12:24:03 -0800464bool
465SecTpmFile::generateRandomBlock(uint8_t* res, size_t size)
466{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700467 try
468 {
469 CryptoPP::AutoSeededRandomPool rng;
470 rng.GenerateBlock(res, size);
471 return true;
472 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700473 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700474 {
475 return false;
476 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800477}
478
Yingdi Yufc40d872014-02-18 12:56:04 -0800479} // namespace ndn