blob: c8b460cb2a91bc9f8043f1660a1e91e82e96d59e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu2d9c50f2014-01-21 18:25:00 -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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Xingyu Ma <http://www.linkedin.com/pub/xingyu-ma/1a/384/5a8>
22 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
23 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080024 */
25
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080026#include "common.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080027
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080028#include "sec-tpm-file.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029#include "../encoding/buffer-stream.hpp"
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080030
31#include <boost/filesystem.hpp>
32#include <boost/algorithm/string.hpp>
33
Junxiao Shi482ccc52014-03-31 13:05:24 -070034#include "cryptopp.hpp"
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080035
36#include <sys/types.h>
37#include <sys/stat.h>
38
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080039#include <algorithm>
40
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080041using namespace std;
42
Yingdi Yufc40d872014-02-18 12:56:04 -080043namespace ndn {
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080044
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070045class SecTpmFile::Impl
46{
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080047public:
Yingdi Yu4b752752014-02-18 12:24:03 -080048 Impl(const string& dir)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080049 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 if (dir.empty())
Yingdi Yu37e317f2014-03-19 12:16:23 -070051 m_keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndn" / "ndnsec-tpm-file";
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080052 else
53 m_keystorePath = dir;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080055 boost::filesystem::create_directories (m_keystorePath);
56 }
57
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080058 boost::filesystem::path
59 nameTransform(const string& keyName, const string& extension)
60 {
61 using namespace CryptoPP;
62 string digest;
63 SHA256 hash;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 StringSource src(keyName,
65 true,
66 new HashFilter(hash,
67 new Base64Encoder(new CryptoPP::StringSink(digest))));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080068
69 boost::algorithm::trim(digest);
70 std::replace(digest.begin(), digest.end(), '/', '%');
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080072 return m_keystorePath / (digest + extension);
73 }
74
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 string
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080076 maintainMapping(const string& keyName)
77 {
78 string keyFileName = nameTransform(keyName, "").string();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080080 ofstream outfile;
81 string dirFile = (m_keystorePath / "mapping.txt").string();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080083 outfile.open(dirFile.c_str(), std::ios_base::app);
84 outfile << keyName << ' ' << keyFileName << '\n';
85 outfile.close();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080087 return keyFileName;
88 }
89
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080090public:
91 boost::filesystem::path m_keystorePath;
92};
93
Yingdi Yu4b752752014-02-18 12:24:03 -080094
Yingdi Yube4150e2014-02-18 13:02:46 -080095SecTpmFile::SecTpmFile(const string& dir)
Yingdi Yu4b752752014-02-18 12:24:03 -080096 : m_impl(new Impl(dir))
Yingdi Yube4150e2014-02-18 13:02:46 -080097 , m_inTerminal(false)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080098{}
99
100void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700101SecTpmFile::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800102{
103 string keyURI = keyName.toUri();
104
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105 if (doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800106 throw Error("public key exists");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 if (doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800108 throw Error("private key exists");
109
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800110 string keyFileName = m_impl->maintainMapping(keyURI);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800111
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700112 try
113 {
114 switch (keyType)
115 {
116 case KEY_TYPE_RSA:
117 {
118 using namespace CryptoPP;
119 AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800120
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700121 InvertibleRSAFunction privateKey;
122 privateKey.Initialize(rng, keySize);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 string privateKeyFileName = keyFileName + ".pri";
125 Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str()));
126 privateKey.DEREncode(privateKeySink);
127 privateKeySink.MessageEnd();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700129 RSAFunction publicKey(privateKey);
130 string publicKeyFileName = keyFileName + ".pub";
131 Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str()));
132 publicKey.DEREncode(publicKeySink);
133 publicKeySink.MessageEnd();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700135 /*set file permission*/
136 chmod(privateKeyFileName.c_str(), 0000400);
137 chmod(publicKeyFileName.c_str(), 0000444);
138 return;
139 }
140 default:
141 throw Error("Unsupported key type!");
142 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800143 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700144 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700145 {
146 throw Error(e.what());
147 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800148}
149
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800150void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151SecTpmFile::deleteKeyPairInTpm(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800152{
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153 boost::filesystem::path publicKeyPath(m_impl->nameTransform(keyName.toUri(), ".pub"));
154 boost::filesystem::path privateKeyPath(m_impl->nameTransform(keyName.toUri(), ".pri"));
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800155
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156 if (boost::filesystem::exists(publicKeyPath))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157 boost::filesystem::remove(publicKeyPath);
158
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 if (boost::filesystem::exists(privateKeyPath))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800160 boost::filesystem::remove(privateKeyPath);
161}
162
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800163shared_ptr<PublicKey>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700164SecTpmFile::getPublicKeyFromTpm(const Name& keyName)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800165{
166 string keyURI = keyName.toUri();
167
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168 if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu5e96e002014-04-23 18:32:15 -0700169 throw Error("Public Key does not exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800170
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800171 ostringstream os;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700172 try
173 {
174 using namespace CryptoPP;
175 FileSource(m_impl->nameTransform(keyURI, ".pub").string().c_str(),
176 true,
177 new Base64Decoder(new FileSink(os)));
178 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700179 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700180 {
181 throw Error(e.what());
182 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800183
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700184 return make_shared<PublicKey>(reinterpret_cast<const uint8_t*>(os.str().c_str()),
185 os.str().size());
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800186}
187
188ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700189SecTpmFile::exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800190{
191 OBufferStream privateKeyOs;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192 CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true,
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800193 new CryptoPP::Base64Decoder(new CryptoPP::FileSink(privateKeyOs)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700194
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800195 return privateKeyOs.buf();
196}
197
198bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700199SecTpmFile::importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800200{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700201 try
202 {
203 using namespace CryptoPP;
204
205 string keyFileName = m_impl->maintainMapping(keyName.toUri());
206 keyFileName.append(".pri");
207 StringSource(buf, size,
208 true,
209 new Base64Encoder(new FileSink(keyFileName.c_str())));
210 return true;
211 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700212 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700213 {
214 return false;
215 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800216}
217
218bool
219SecTpmFile::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
220{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700221 try
222 {
223 using namespace CryptoPP;
224
225 string keyFileName = m_impl->maintainMapping(keyName.toUri());
226 keyFileName.append(".pub");
227 StringSource(buf, size,
228 true,
229 new Base64Encoder(new FileSink(keyFileName.c_str())));
230 return true;
231 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700232 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700233 {
234 return false;
235 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800236}
237
238Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700239SecTpmFile::signInTpm(const uint8_t* data, size_t dataLength,
240 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800241{
242 string keyURI = keyName.toUri();
243
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700244 if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800245 throw Error("private key doesn't exists");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700246
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700247 try
248 {
249 using namespace CryptoPP;
250 AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800251
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700252 //Read private key
253 ByteQueue bytes;
254 FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(),
255 true, new Base64Decoder);
256 file.TransferTo(bytes);
257 bytes.MessageEnd();
258 RSA::PrivateKey privateKey;
259 privateKey.Load(bytes);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700260
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700261 //Sign message
262 switch (digestAlgorithm)
263 {
264 case DIGEST_ALGORITHM_SHA256:
265 {
266 RSASS<PKCS1v15, SHA256>::Signer signer(privateKey);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700267
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700268 OBufferStream os;
269 StringSource(data, dataLength,
270 true,
271 new SignerFilter(rng, signer, new FileSink(os)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700272
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700273 return Block(Tlv::SignatureValue, os.buf());
274 }
275 default:
276 throw Error("Unsupported digest algorithm!");
277 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800278 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700279 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700280 {
281 throw Error(e.what());
282 }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800283}
284
285
286ConstBufferPtr
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700287SecTpmFile::decryptInTpm(const uint8_t* data, size_t dataLength,
288 const Name& keyName, bool isSymmetric)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800289{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 throw Error("SecTpmFile::decryptInTpm is not supported!");
291 // string keyURI = keyName.toUri();
292 // if (!isSymmetric)
293 // {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700294 // if (!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800295 // throw Error("private key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800296
Yingdi Yu2e57a582014-02-20 23:34:43 -0800297 // try{
298 // using namespace CryptoPP;
299 // AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800300
Yingdi Yu2e57a582014-02-20 23:34:43 -0800301 // //Read private key
302 // ByteQueue bytes;
303 // FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(), true, new Base64Decoder);
304 // file.TransferTo(bytes);
305 // bytes.MessageEnd();
306 // RSA::PrivateKey privateKey;
307 // privateKey.Load(bytes);
308 // RSAES_PKCS1v15_Decryptor decryptor(privateKey);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700309
Yingdi Yu2e57a582014-02-20 23:34:43 -0800310 // OBufferStream os;
311 // StringSource(data, dataLength, true, new PK_DecryptorFilter(rng, decryptor, new FileSink(os)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700312
Yingdi Yu2e57a582014-02-20 23:34:43 -0800313 // return os.buf();
314 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700315 // catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800316 // throw Error(e.what());
317 // }
318 // }
319 // else
320 // {
321 // throw Error("Symmetric encryption is not implemented!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700322 // // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800323 // // throw Error("symmetric key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800324
Yingdi Yu2e57a582014-02-20 23:34:43 -0800325 // // try{
326 // // string keyBits;
327 // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key");
328 // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits)));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700329
Yingdi Yu2e57a582014-02-20 23:34:43 -0800330 // // using CryptoPP::AES;
331 // // AutoSeededRandomPool rnd;
332 // // byte iv[AES::BLOCKSIZE];
333 // // rnd.GenerateBlock(iv, AES::BLOCKSIZE);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800334
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335 // // CFB_Mode<AES>::Decryption decryptor;
336 // // decryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700337
Yingdi Yu2e57a582014-02-20 23:34:43 -0800338 // // OBufferStream os;
339 // // StringSource(data, dataLength, true, new StreamTransformationFilter(decryptor,new FileSink(os)));
340 // // return os.buf();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800341
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700342 // // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800343 // // throw Error(e.what());
344 // // }
345 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800346}
347
348ConstBufferPtr
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700349SecTpmFile::encryptInTpm(const uint8_t* data, size_t dataLength,
350 const Name& keyName, bool isSymmetric)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800351{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352 throw Error("SecTpmFile::encryptInTpm is not supported!");
353 // string keyURI = keyName.toUri();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800354
Yingdi Yu2e57a582014-02-20 23:34:43 -0800355 // if (!isSymmetric)
356 // {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700357 // if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358 // throw Error("public key doesn't exist");
359 // try
360 // {
361 // using namespace CryptoPP;
362 // AutoSeededRandomPool rng;
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800363
Yingdi Yu2e57a582014-02-20 23:34:43 -0800364 // //Read private key
365 // ByteQueue bytes;
366 // FileSource file(m_impl->nameTransform(keyURI, ".pub").string().c_str(), true, new Base64Decoder);
367 // file.TransferTo(bytes);
368 // bytes.MessageEnd();
369 // RSA::PublicKey publicKey;
370 // publicKey.Load(bytes);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800371
Yingdi Yu2e57a582014-02-20 23:34:43 -0800372 // OBufferStream os;
373 // RSAES_PKCS1v15_Encryptor encryptor(publicKey);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800374
Yingdi Yu2e57a582014-02-20 23:34:43 -0800375 // StringSource(data, dataLength, true, new PK_EncryptorFilter(rng, encryptor, new FileSink(os)));
376 // return os.buf();
377 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700378 // catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800379 // throw Error(e.what());
380 // }
381 // }
382 // else
383 // {
384 // throw Error("Symmetric encryption is not implemented!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700385 // // if (!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386 // // throw Error("symmetric key doesn't exist");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800387
Yingdi Yu2e57a582014-02-20 23:34:43 -0800388 // // try{
389 // // string keyBits;
390 // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key");
391 // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits)));
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800392
Yingdi Yu2e57a582014-02-20 23:34:43 -0800393 // // using CryptoPP::AES;
394 // // AutoSeededRandomPool rnd;
395 // // byte iv[AES::BLOCKSIZE];
396 // // rnd.GenerateBlock(iv, AES::BLOCKSIZE);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800397
Yingdi Yu2e57a582014-02-20 23:34:43 -0800398 // // CFB_Mode<AES>::Encryption encryptor;
399 // // encryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800400
Yingdi Yu2e57a582014-02-20 23:34:43 -0800401 // // OBufferStream os;
402 // // StringSource(data, dataLength, true, new StreamTransformationFilter(encryptor, new FileSink(os)));
403 // // return os.buf();
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700404 // // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405 // // throw Error(e.what());
406 // // }
407 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800408}
409
410
411void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700412SecTpmFile::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800413{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800414 throw Error("SecTpmFile::generateSymmetricKeyInTpm is not supported!");
415 // string keyURI = keyName.toUri();
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800416
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700417 // if (doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418 // throw Error("symmetric key exists");
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800419
Yingdi Yu2e57a582014-02-20 23:34:43 -0800420 // string keyFileName = m_impl->maintainMapping(keyURI);
421 // string symKeyFileName = keyFileName + ".key";
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800422
Yingdi Yu2e57a582014-02-20 23:34:43 -0800423 // try{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700424 // switch (keyType){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425 // case KEY_TYPE_AES:
426 // {
427 // using namespace CryptoPP;
428 // AutoSeededRandomPool rng;
Yingdi Yu4b752752014-02-18 12:24:03 -0800429
Yingdi Yu2e57a582014-02-20 23:34:43 -0800430 // SecByteBlock key(0x00, keySize);
431 // rng.GenerateBlock(key, keySize);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700432
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433 // StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str())));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700434
Yingdi Yu2e57a582014-02-20 23:34:43 -0800435 // chmod(symKeyFileName.c_str(), 0000400);
436 // return;
437 // }
438 // default:
439 // throw Error("Unsupported symmetric key type!");
440 // }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700441 // }catch (CryptoPP::Exception& e){
Yingdi Yu2e57a582014-02-20 23:34:43 -0800442 // throw Error(e.what());
443 // }
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800444}
445
446bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700447SecTpmFile::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800448{
449 string keyURI = keyName.toUri();
450 if (keyClass == KEY_CLASS_PUBLIC)
451 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700452 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pub")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800453 return true;
454 else
455 return false;
456 }
457 if (keyClass == KEY_CLASS_PRIVATE)
458 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700459 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pri")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800460 return true;
461 else
462 return false;
463 }
464 if (keyClass == KEY_CLASS_SYMMETRIC)
465 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700466 if (boost::filesystem::exists(m_impl->nameTransform(keyURI, ".key")))
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800467 return true;
468 else
469 return false;
470 }
471 return false;
472}
473
Yingdi Yu4b752752014-02-18 12:24:03 -0800474bool
475SecTpmFile::generateRandomBlock(uint8_t* res, size_t size)
476{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700477 try
478 {
479 CryptoPP::AutoSeededRandomPool rng;
480 rng.GenerateBlock(res, size);
481 return true;
482 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700483 catch (CryptoPP::Exception& e)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700484 {
485 return false;
486 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800487}
488
Yingdi Yufc40d872014-02-18 12:56:04 -0800489} // namespace ndn