Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Xingyu Ma <maxy12@cs.ucla.edu> |
| 5 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 7 | * See COPYING for copyright and distribution information. |
| 8 | */ |
| 9 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 10 | #include "common.hpp" |
Yingdi Yu | 0402092 | 2014-01-22 12:46:53 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 12 | #include "sec-tpm-file.hpp" |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 13 | |
| 14 | #include <boost/filesystem.hpp> |
| 15 | #include <boost/algorithm/string.hpp> |
| 16 | |
| 17 | #include <cryptopp/rsa.h> |
| 18 | #include <cryptopp/files.h> |
| 19 | #include <cryptopp/base64.h> |
| 20 | #include <cryptopp/hex.h> |
| 21 | #include <cryptopp/osrng.h> |
| 22 | #include <cryptopp/sha.h> |
| 23 | #include <cryptopp/pssr.h> |
| 24 | #include <cryptopp/modes.h> |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 25 | #include <cryptopp/pwdbased.h> |
| 26 | #include <cryptopp/sha.h> |
| 27 | #include <cryptopp/des.h> |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 34 | using namespace std; |
| 35 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 36 | namespace ndn { |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 37 | |
| 38 | class SecTpmFile::Impl { |
| 39 | public: |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 40 | Impl(const string& dir) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 41 | { |
| 42 | if(dir.empty()) |
Yingdi Yu | 37e317f | 2014-03-19 12:16:23 -0700 | [diff] [blame] | 43 | m_keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndn" / "ndnsec-tpm-file"; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 44 | else |
| 45 | m_keystorePath = dir; |
| 46 | |
| 47 | boost::filesystem::create_directories (m_keystorePath); |
| 48 | } |
| 49 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 50 | boost::filesystem::path |
| 51 | nameTransform(const string& keyName, const string& extension) |
| 52 | { |
| 53 | using namespace CryptoPP; |
| 54 | string digest; |
| 55 | SHA256 hash; |
| 56 | StringSource src(keyName, true, new HashFilter(hash, new Base64Encoder (new CryptoPP::StringSink(digest)))); |
| 57 | |
| 58 | boost::algorithm::trim(digest); |
| 59 | std::replace(digest.begin(), digest.end(), '/', '%'); |
| 60 | |
| 61 | return m_keystorePath / (digest + extension); |
| 62 | } |
| 63 | |
| 64 | string |
| 65 | maintainMapping(const string& keyName) |
| 66 | { |
| 67 | string keyFileName = nameTransform(keyName, "").string(); |
| 68 | |
| 69 | ofstream outfile; |
| 70 | string dirFile = (m_keystorePath / "mapping.txt").string(); |
| 71 | |
| 72 | outfile.open(dirFile.c_str(), std::ios_base::app); |
| 73 | outfile << keyName << ' ' << keyFileName << '\n'; |
| 74 | outfile.close(); |
| 75 | |
| 76 | return keyFileName; |
| 77 | } |
| 78 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 79 | public: |
| 80 | boost::filesystem::path m_keystorePath; |
| 81 | }; |
| 82 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 83 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 84 | SecTpmFile::SecTpmFile(const string& dir) |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 85 | : m_impl(new Impl(dir)) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 86 | , m_inTerminal(false) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 87 | {} |
| 88 | |
| 89 | void |
| 90 | SecTpmFile::generateKeyPairInTpm(const Name & keyName, KeyType keyType, int keySize) |
| 91 | { |
| 92 | string keyURI = keyName.toUri(); |
| 93 | |
| 94 | if(doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)) |
| 95 | throw Error("public key exists"); |
| 96 | if(doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
| 97 | throw Error("private key exists"); |
| 98 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 99 | string keyFileName = m_impl->maintainMapping(keyURI); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 100 | |
| 101 | try{ |
| 102 | switch(keyType){ |
| 103 | case KEY_TYPE_RSA: |
| 104 | { |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 105 | using namespace CryptoPP; |
| 106 | AutoSeededRandomPool rng; |
| 107 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 108 | InvertibleRSAFunction privateKey; |
| 109 | privateKey.Initialize(rng, keySize); |
| 110 | |
| 111 | string privateKeyFileName = keyFileName + ".pri"; |
| 112 | Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str())); |
| 113 | privateKey.DEREncode(privateKeySink); |
| 114 | privateKeySink.MessageEnd(); |
| 115 | |
| 116 | RSAFunction publicKey(privateKey); |
| 117 | string publicKeyFileName = keyFileName + ".pub"; |
| 118 | Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str())); |
| 119 | publicKey.DEREncode(publicKeySink); |
| 120 | publicKeySink.MessageEnd(); |
| 121 | |
| 122 | /*set file permission*/ |
| 123 | chmod(privateKeyFileName.c_str(), 0000400); |
| 124 | chmod(publicKeyFileName.c_str(), 0000444); |
| 125 | return; |
| 126 | } |
| 127 | default: |
| 128 | throw Error("Unsupported key type!"); |
| 129 | } |
| 130 | }catch(const CryptoPP::Exception& e){ |
| 131 | throw Error(e.what()); |
| 132 | } |
| 133 | } |
| 134 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 135 | void |
| 136 | SecTpmFile::deleteKeyPairInTpm(const Name &keyName) |
| 137 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 138 | boost::filesystem::path publicKeyPath(m_impl->nameTransform(keyName.toUri(), ".pub")); |
| 139 | boost::filesystem::path privateKeyPath(m_impl->nameTransform(keyName.toUri(), ".pri")); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 140 | |
| 141 | if(boost::filesystem::exists(publicKeyPath)) |
| 142 | boost::filesystem::remove(publicKeyPath); |
| 143 | |
| 144 | if(boost::filesystem::exists(privateKeyPath)) |
| 145 | boost::filesystem::remove(privateKeyPath); |
| 146 | } |
| 147 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 148 | shared_ptr<PublicKey> |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 149 | SecTpmFile::getPublicKeyFromTpm(const Name & keyName) |
| 150 | { |
| 151 | string keyURI = keyName.toUri(); |
| 152 | |
| 153 | if(!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 154 | throw Error("Public Key already exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 155 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 156 | ostringstream os; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 157 | try{ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 158 | using namespace CryptoPP; |
| 159 | FileSource(m_impl->nameTransform(keyURI, ".pub").string().c_str(), true, new Base64Decoder(new FileSink(os))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 160 | }catch(const CryptoPP::Exception& e){ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 161 | throw Error(e.what()); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 164 | return make_shared<PublicKey>(reinterpret_cast<const uint8_t*>(os.str().c_str()), os.str().size()); |
| 165 | } |
| 166 | |
| 167 | ConstBufferPtr |
| 168 | SecTpmFile::exportPrivateKeyPkcs1FromTpm(const Name& keyName) |
| 169 | { |
| 170 | OBufferStream privateKeyOs; |
| 171 | CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true, |
| 172 | new CryptoPP::Base64Decoder(new CryptoPP::FileSink(privateKeyOs))); |
| 173 | |
| 174 | return privateKeyOs.buf(); |
| 175 | } |
| 176 | |
| 177 | bool |
| 178 | SecTpmFile::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 179 | { |
| 180 | try{ |
| 181 | string keyFileName = m_impl->maintainMapping(keyName.toUri()); |
| 182 | keyFileName.append(".pri"); |
| 183 | CryptoPP::StringSource(buf, size, true, |
| 184 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(keyFileName.c_str()))); |
| 185 | return true; |
| 186 | }catch(...){ |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bool |
| 192 | SecTpmFile::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 193 | { |
| 194 | try{ |
| 195 | string keyFileName = m_impl->maintainMapping(keyName.toUri()); |
| 196 | keyFileName.append(".pub"); |
| 197 | CryptoPP::StringSource(buf, size, true, |
| 198 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(keyFileName.c_str()))); |
| 199 | return true; |
| 200 | }catch(...){ |
| 201 | return false; |
| 202 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | Block |
| 206 | SecTpmFile::signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm) |
| 207 | { |
| 208 | string keyURI = keyName.toUri(); |
| 209 | |
| 210 | if(!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
| 211 | throw Error("private key doesn't exists"); |
| 212 | |
| 213 | try{ |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 214 | using namespace CryptoPP; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 215 | AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 216 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 217 | //Read private key |
| 218 | ByteQueue bytes; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 219 | FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(), true, new Base64Decoder); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 220 | file.TransferTo(bytes); |
| 221 | bytes.MessageEnd(); |
| 222 | RSA::PrivateKey privateKey; |
| 223 | privateKey.Load(bytes); |
| 224 | |
| 225 | //Sign message |
| 226 | switch(digestAlgorithm){ |
| 227 | case DIGEST_ALGORITHM_SHA256: |
| 228 | { |
| 229 | RSASS<PKCS1v15, SHA256>::Signer signer(privateKey); |
| 230 | |
| 231 | OBufferStream os; |
| 232 | StringSource(data, dataLength, true, new SignerFilter(rng, signer, new FileSink(os))); |
| 233 | |
| 234 | return Block(Tlv::SignatureValue, os.buf()); |
| 235 | } |
| 236 | default: |
| 237 | throw Error("Unsupported digest algorithm!"); |
| 238 | } |
| 239 | }catch(const CryptoPP::Exception& e){ |
| 240 | throw Error(e.what()); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 246 | SecTpmFile::decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 247 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 248 | throw Error("SecTpmFile::decryptInTpm is not supported!"); |
| 249 | // string keyURI = keyName.toUri(); |
| 250 | // if (!isSymmetric) |
| 251 | // { |
| 252 | // if(!doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) |
| 253 | // throw Error("private key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 254 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 255 | // try{ |
| 256 | // using namespace CryptoPP; |
| 257 | // AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 258 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 259 | // //Read private key |
| 260 | // ByteQueue bytes; |
| 261 | // FileSource file(m_impl->nameTransform(keyURI, ".pri").string().c_str(), true, new Base64Decoder); |
| 262 | // file.TransferTo(bytes); |
| 263 | // bytes.MessageEnd(); |
| 264 | // RSA::PrivateKey privateKey; |
| 265 | // privateKey.Load(bytes); |
| 266 | // RSAES_PKCS1v15_Decryptor decryptor(privateKey); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 267 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 268 | // OBufferStream os; |
| 269 | // StringSource(data, dataLength, true, new PK_DecryptorFilter(rng, decryptor, new FileSink(os))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 270 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 271 | // return os.buf(); |
| 272 | // } |
| 273 | // catch(const CryptoPP::Exception& e){ |
| 274 | // throw Error(e.what()); |
| 275 | // } |
| 276 | // } |
| 277 | // else |
| 278 | // { |
| 279 | // throw Error("Symmetric encryption is not implemented!"); |
| 280 | // // if(!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
| 281 | // // throw Error("symmetric key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 282 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 283 | // // try{ |
| 284 | // // string keyBits; |
| 285 | // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key"); |
| 286 | // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 287 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 288 | // // using CryptoPP::AES; |
| 289 | // // AutoSeededRandomPool rnd; |
| 290 | // // byte iv[AES::BLOCKSIZE]; |
| 291 | // // rnd.GenerateBlock(iv, AES::BLOCKSIZE); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 292 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 293 | // // CFB_Mode<AES>::Decryption decryptor; |
| 294 | // // decryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 295 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 296 | // // OBufferStream os; |
| 297 | // // StringSource(data, dataLength, true, new StreamTransformationFilter(decryptor,new FileSink(os))); |
| 298 | // // return os.buf(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 299 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 300 | // // }catch(const CryptoPP::Exception& e){ |
| 301 | // // throw Error(e.what()); |
| 302 | // // } |
| 303 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 307 | SecTpmFile::encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 308 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 309 | throw Error("SecTpmFile::encryptInTpm is not supported!"); |
| 310 | // string keyURI = keyName.toUri(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 311 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 312 | // if (!isSymmetric) |
| 313 | // { |
| 314 | // if(!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)) |
| 315 | // throw Error("public key doesn't exist"); |
| 316 | // try |
| 317 | // { |
| 318 | // using namespace CryptoPP; |
| 319 | // AutoSeededRandomPool rng; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 320 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 321 | // //Read private key |
| 322 | // ByteQueue bytes; |
| 323 | // FileSource file(m_impl->nameTransform(keyURI, ".pub").string().c_str(), true, new Base64Decoder); |
| 324 | // file.TransferTo(bytes); |
| 325 | // bytes.MessageEnd(); |
| 326 | // RSA::PublicKey publicKey; |
| 327 | // publicKey.Load(bytes); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 328 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 329 | // OBufferStream os; |
| 330 | // RSAES_PKCS1v15_Encryptor encryptor(publicKey); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 331 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 332 | // StringSource(data, dataLength, true, new PK_EncryptorFilter(rng, encryptor, new FileSink(os))); |
| 333 | // return os.buf(); |
| 334 | // } |
| 335 | // catch(const CryptoPP::Exception& e){ |
| 336 | // throw Error(e.what()); |
| 337 | // } |
| 338 | // } |
| 339 | // else |
| 340 | // { |
| 341 | // throw Error("Symmetric encryption is not implemented!"); |
| 342 | // // if(!doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
| 343 | // // throw Error("symmetric key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 344 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 345 | // // try{ |
| 346 | // // string keyBits; |
| 347 | // // string symKeyFileName = m_impl->nameTransform(keyURI, ".key"); |
| 348 | // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 349 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 350 | // // using CryptoPP::AES; |
| 351 | // // AutoSeededRandomPool rnd; |
| 352 | // // byte iv[AES::BLOCKSIZE]; |
| 353 | // // rnd.GenerateBlock(iv, AES::BLOCKSIZE); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 354 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 355 | // // CFB_Mode<AES>::Encryption encryptor; |
| 356 | // // encryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 357 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 358 | // // OBufferStream os; |
| 359 | // // StringSource(data, dataLength, true, new StreamTransformationFilter(encryptor, new FileSink(os))); |
| 360 | // // return os.buf(); |
| 361 | // // }catch(const CryptoPP::Exception& e){ |
| 362 | // // throw Error(e.what()); |
| 363 | // // } |
| 364 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | void |
| 369 | SecTpmFile::generateSymmetricKeyInTpm(const Name & keyName, KeyType keyType, int keySize) |
| 370 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 371 | throw Error("SecTpmFile::generateSymmetricKeyInTpm is not supported!"); |
| 372 | // string keyURI = keyName.toUri(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 373 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 374 | // if(doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
| 375 | // throw Error("symmetric key exists"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 376 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 377 | // string keyFileName = m_impl->maintainMapping(keyURI); |
| 378 | // string symKeyFileName = keyFileName + ".key"; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 379 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 380 | // try{ |
| 381 | // switch(keyType){ |
| 382 | // case KEY_TYPE_AES: |
| 383 | // { |
| 384 | // using namespace CryptoPP; |
| 385 | // AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 386 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 387 | // SecByteBlock key(0x00, keySize); |
| 388 | // rng.GenerateBlock(key, keySize); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 389 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 390 | // StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str()))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 391 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 392 | // chmod(symKeyFileName.c_str(), 0000400); |
| 393 | // return; |
| 394 | // } |
| 395 | // default: |
| 396 | // throw Error("Unsupported symmetric key type!"); |
| 397 | // } |
| 398 | // }catch(const CryptoPP::Exception& e){ |
| 399 | // throw Error(e.what()); |
| 400 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | bool |
| 404 | SecTpmFile::doesKeyExistInTpm(const Name & keyName, KeyClass keyClass) |
| 405 | { |
| 406 | string keyURI = keyName.toUri(); |
| 407 | if (keyClass == KEY_CLASS_PUBLIC) |
| 408 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 409 | if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pub"))) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 410 | return true; |
| 411 | else |
| 412 | return false; |
| 413 | } |
| 414 | if (keyClass == KEY_CLASS_PRIVATE) |
| 415 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 416 | if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".pri"))) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 417 | return true; |
| 418 | else |
| 419 | return false; |
| 420 | } |
| 421 | if (keyClass == KEY_CLASS_SYMMETRIC) |
| 422 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 423 | if(boost::filesystem::exists(m_impl->nameTransform(keyURI, ".key"))) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 424 | return true; |
| 425 | else |
| 426 | return false; |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 431 | bool |
| 432 | SecTpmFile::generateRandomBlock(uint8_t* res, size_t size) |
| 433 | { |
| 434 | try{ |
| 435 | CryptoPP::AutoSeededRandomPool rng; |
| 436 | rng.GenerateBlock(res, size); |
| 437 | return true; |
| 438 | }catch(const CryptoPP::Exception& e){ |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 443 | } // namespace ndn |