Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 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 Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 26 | #include "sec-tpm-file.hpp" |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 28 | #include "../encoding/buffer-stream.hpp" |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 29 | |
| 30 | #include <boost/filesystem.hpp> |
| 31 | #include <boost/algorithm/string.hpp> |
| 32 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 33 | #include "cryptopp.hpp" |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 40 | namespace ndn { |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 41 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::ostringstream; |
| 44 | using std::ofstream; |
| 45 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 46 | const std::string SecTpmFile::SCHEME("tpm-file"); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 48 | class SecTpmFile::Impl |
| 49 | { |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 50 | public: |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 51 | explicit |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 52 | Impl(const string& dir) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 53 | { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 54 | boost::filesystem::path actualDir; |
| 55 | if (dir.empty()) { |
| 56 | #ifdef NDN_CXX_HAVE_TESTS |
| 57 | if (getenv("TEST_HOME") != nullptr) { |
| 58 | actualDir = boost::filesystem::path(getenv("TEST_HOME")) / ".ndn"; |
| 59 | } |
| 60 | else |
| 61 | #endif // NDN_CXX_HAVE_TESTS |
| 62 | if (getenv("HOME") != nullptr) { |
| 63 | actualDir = boost::filesystem::path(getenv("HOME")) / ".ndn"; |
| 64 | } |
| 65 | else { |
| 66 | actualDir = boost::filesystem::path(".") / ".ndn"; |
| 67 | } |
| 68 | } |
| 69 | else { |
| 70 | actualDir = boost::filesystem::path(dir); |
| 71 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 73 | m_keystorePath = actualDir / "ndnsec-tpm-file"; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 74 | boost::filesystem::create_directories(m_keystorePath); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 77 | boost::filesystem::path |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 78 | transformName(const string& keyName, const string& extension) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 79 | { |
| 80 | using namespace CryptoPP; |
| 81 | string digest; |
| 82 | SHA256 hash; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 83 | StringSource src(keyName, |
| 84 | true, |
| 85 | new HashFilter(hash, |
| 86 | new Base64Encoder(new CryptoPP::StringSink(digest)))); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 87 | |
| 88 | boost::algorithm::trim(digest); |
| 89 | std::replace(digest.begin(), digest.end(), '/', '%'); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 91 | return m_keystorePath / (digest + extension); |
| 92 | } |
| 93 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 94 | string |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 95 | maintainMapping(const string& keyName) |
| 96 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 97 | string keyFileName = transformName(keyName, "").string(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 99 | ofstream outfile; |
| 100 | string dirFile = (m_keystorePath / "mapping.txt").string(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 102 | outfile.open(dirFile.c_str(), std::ios_base::app); |
| 103 | outfile << keyName << ' ' << keyFileName << '\n'; |
| 104 | outfile.close(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 106 | return keyFileName; |
| 107 | } |
| 108 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 109 | public: |
| 110 | boost::filesystem::path m_keystorePath; |
| 111 | }; |
| 112 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 113 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 114 | SecTpmFile::SecTpmFile(const string& location) |
| 115 | : SecTpm(location) |
| 116 | , m_impl(new Impl(location)) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 117 | , m_inTerminal(false) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 118 | { |
| 119 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 120 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 121 | SecTpmFile::~SecTpmFile() |
| 122 | { |
| 123 | } |
| 124 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 125 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 126 | SecTpmFile::generateKeyPairInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 127 | { |
| 128 | string keyURI = keyName.toUri(); |
| 129 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 130 | if (doesKeyExistInTpm(keyName, KeyClass::PUBLIC)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 131 | BOOST_THROW_EXCEPTION(Error("public key exists")); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 132 | if (doesKeyExistInTpm(keyName, KeyClass::PRIVATE)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 133 | BOOST_THROW_EXCEPTION(Error("private key exists")); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 134 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 135 | string keyFileName = m_impl->maintainMapping(keyURI); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 136 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 137 | try { |
| 138 | switch (params.getKeyType()) { |
| 139 | case KeyType::RSA: { |
| 140 | using namespace CryptoPP; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 141 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 142 | const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(params); |
| 143 | AutoSeededRandomPool rng; |
| 144 | InvertibleRSAFunction privateKey; |
| 145 | privateKey.Initialize(rng, rsaParams.getKeySize()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 147 | string privateKeyFileName = keyFileName + ".pri"; |
| 148 | Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str())); |
| 149 | privateKey.DEREncode(privateKeySink); |
| 150 | privateKeySink.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 151 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 152 | RSAFunction publicKey(privateKey); |
| 153 | string publicKeyFileName = keyFileName + ".pub"; |
| 154 | Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str())); |
| 155 | publicKey.DEREncode(publicKeySink); |
| 156 | publicKeySink.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 157 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 158 | // set file permission |
| 159 | chmod(privateKeyFileName.c_str(), 0000400); |
| 160 | chmod(publicKeyFileName.c_str(), 0000444); |
| 161 | return; |
| 162 | } |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 163 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 164 | case KeyType::EC: { |
| 165 | using namespace CryptoPP; |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 166 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 167 | const EcdsaKeyParams& ecdsaParams = static_cast<const EcdsaKeyParams&>(params); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 168 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 169 | CryptoPP::OID curveName; |
| 170 | switch (ecdsaParams.getKeySize()) { |
| 171 | case 256: |
| 172 | curveName = ASN1::secp256r1(); |
| 173 | break; |
| 174 | case 384: |
| 175 | curveName = ASN1::secp384r1(); |
| 176 | break; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 177 | default: |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 178 | curveName = ASN1::secp256r1(); |
| 179 | break; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 180 | } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 181 | |
| 182 | AutoSeededRandomPool rng; |
| 183 | |
| 184 | ECDSA<ECP, SHA256>::PrivateKey privateKey; |
| 185 | DL_GroupParameters_EC<ECP> cryptoParams(curveName); |
| 186 | cryptoParams.SetEncodeAsOID(true); |
| 187 | privateKey.Initialize(rng, cryptoParams); |
| 188 | |
| 189 | ECDSA<ECP, SHA256>::PublicKey publicKey; |
| 190 | privateKey.MakePublicKey(publicKey); |
| 191 | publicKey.AccessGroupParameters().SetEncodeAsOID(true); |
| 192 | |
| 193 | string privateKeyFileName = keyFileName + ".pri"; |
| 194 | Base64Encoder privateKeySink(new FileSink(privateKeyFileName.c_str())); |
| 195 | privateKey.DEREncode(privateKeySink); |
| 196 | privateKeySink.MessageEnd(); |
| 197 | |
| 198 | string publicKeyFileName = keyFileName + ".pub"; |
| 199 | Base64Encoder publicKeySink(new FileSink(publicKeyFileName.c_str())); |
| 200 | publicKey.Save(publicKeySink); |
| 201 | publicKeySink.MessageEnd(); |
| 202 | |
| 203 | // set file permission |
| 204 | chmod(privateKeyFileName.c_str(), 0000400); |
| 205 | chmod(publicKeyFileName.c_str(), 0000444); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | default: |
| 210 | BOOST_THROW_EXCEPTION(Error("Unsupported key type")); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 211 | } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 212 | } |
| 213 | catch (const KeyParams::Error& e) { |
| 214 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 215 | } |
| 216 | catch (const CryptoPP::Exception& e) { |
| 217 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 218 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 221 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 222 | SecTpmFile::deleteKeyPairInTpm(const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 223 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 224 | boost::filesystem::path publicKeyPath(m_impl->transformName(keyName.toUri(), ".pub")); |
| 225 | boost::filesystem::path privateKeyPath(m_impl->transformName(keyName.toUri(), ".pri")); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 227 | if (boost::filesystem::exists(publicKeyPath)) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 228 | boost::filesystem::remove(publicKeyPath); |
| 229 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 230 | if (boost::filesystem::exists(privateKeyPath)) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 231 | boost::filesystem::remove(privateKeyPath); |
| 232 | } |
| 233 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 234 | shared_ptr<PublicKey> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 235 | SecTpmFile::getPublicKeyFromTpm(const Name& keyName) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 236 | { |
| 237 | string keyURI = keyName.toUri(); |
| 238 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 239 | if (!doesKeyExistInTpm(keyName, KeyClass::PUBLIC)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 240 | BOOST_THROW_EXCEPTION(Error("Public Key does not exist")); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 241 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 242 | ostringstream os; |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 243 | try { |
| 244 | using namespace CryptoPP; |
| 245 | FileSource(m_impl->transformName(keyURI, ".pub").string().c_str(), |
| 246 | true, |
| 247 | new Base64Decoder(new FileSink(os))); |
| 248 | } |
| 249 | catch (const CryptoPP::Exception& e) { |
| 250 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 251 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 252 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 253 | return make_shared<PublicKey>(reinterpret_cast<const uint8_t*>(os.str().c_str()), |
| 254 | os.str().size()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 257 | std::string |
| 258 | SecTpmFile::getScheme() |
| 259 | { |
| 260 | return SCHEME; |
| 261 | } |
| 262 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 263 | ConstBufferPtr |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 264 | SecTpmFile::exportPrivateKeyPkcs8FromTpm(const Name& keyName) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 265 | { |
| 266 | OBufferStream privateKeyOs; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 267 | CryptoPP::FileSource(m_impl->transformName(keyName.toUri(), ".pri").string().c_str(), true, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 268 | new CryptoPP::Base64Decoder(new CryptoPP::FileSink(privateKeyOs))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 269 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 270 | return privateKeyOs.buf(); |
| 271 | } |
| 272 | |
| 273 | bool |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 274 | SecTpmFile::importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 275 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 276 | try { |
| 277 | using namespace CryptoPP; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 278 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 279 | string keyFileName = m_impl->maintainMapping(keyName.toUri()); |
| 280 | keyFileName.append(".pri"); |
| 281 | StringSource(buf, size, |
| 282 | true, |
| 283 | new Base64Encoder(new FileSink(keyFileName.c_str()))); |
| 284 | return true; |
| 285 | } |
| 286 | catch (const CryptoPP::Exception& e) { |
| 287 | return false; |
| 288 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | bool |
| 292 | SecTpmFile::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 293 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 294 | try { |
| 295 | using namespace CryptoPP; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 296 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 297 | string keyFileName = m_impl->maintainMapping(keyName.toUri()); |
| 298 | keyFileName.append(".pub"); |
| 299 | StringSource(buf, size, |
| 300 | true, |
| 301 | new Base64Encoder(new FileSink(keyFileName.c_str()))); |
| 302 | return true; |
| 303 | } |
| 304 | catch (const CryptoPP::Exception& e) { |
| 305 | return false; |
| 306 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | Block |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 310 | SecTpmFile::signInTpm(const uint8_t* data, size_t dataLength, |
| 311 | const Name& keyName, DigestAlgorithm digestAlgorithm) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 312 | { |
| 313 | string keyURI = keyName.toUri(); |
| 314 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 315 | if (!doesKeyExistInTpm(keyName, KeyClass::PRIVATE)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 316 | BOOST_THROW_EXCEPTION(Error("private key doesn't exist")); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 317 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 318 | try { |
| 319 | using namespace CryptoPP; |
| 320 | AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 321 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 322 | // Read public key |
| 323 | shared_ptr<PublicKey> pubkeyPtr; |
| 324 | pubkeyPtr = getPublicKeyFromTpm(keyName); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 325 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 326 | switch (pubkeyPtr->getKeyType()) { |
| 327 | case KeyType::RSA: { |
| 328 | // Read private key |
| 329 | ByteQueue bytes; |
| 330 | FileSource file(m_impl->transformName(keyURI, ".pri").string().c_str(), |
| 331 | true, new Base64Decoder); |
| 332 | file.TransferTo(bytes); |
| 333 | bytes.MessageEnd(); |
| 334 | RSA::PrivateKey privateKey; |
| 335 | privateKey.Load(bytes); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 336 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 337 | // Sign message |
| 338 | switch (digestAlgorithm) { |
| 339 | case DigestAlgorithm::SHA256: { |
| 340 | RSASS<PKCS1v15, SHA256>::Signer signer(privateKey); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 341 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 342 | OBufferStream os; |
| 343 | StringSource(data, dataLength, |
| 344 | true, |
| 345 | new SignerFilter(rng, signer, new FileSink(os))); |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 346 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 347 | return Block(tlv::SignatureValue, os.buf()); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 348 | } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 349 | |
| 350 | default: |
| 351 | BOOST_THROW_EXCEPTION(Error("Unsupported digest algorithm")); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 352 | } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | case KeyType::EC: { |
| 356 | // Read private key |
| 357 | ByteQueue bytes; |
| 358 | FileSource file(m_impl->transformName(keyURI, ".pri").string().c_str(), |
| 359 | true, new Base64Decoder); |
| 360 | file.TransferTo(bytes); |
| 361 | bytes.MessageEnd(); |
| 362 | |
| 363 | // Sign message |
| 364 | switch (digestAlgorithm) { |
| 365 | case DigestAlgorithm::SHA256: { |
| 366 | ECDSA<ECP, SHA256>::PrivateKey privateKey; |
| 367 | privateKey.Load(bytes); |
| 368 | ECDSA<ECP, SHA256>::Signer signer(privateKey); |
| 369 | |
| 370 | OBufferStream os; |
| 371 | StringSource(data, dataLength, |
| 372 | true, |
| 373 | new SignerFilter(rng, signer, new FileSink(os))); |
| 374 | |
| 375 | uint8_t buf[200]; |
| 376 | size_t bufSize = DSAConvertSignatureFormat(buf, sizeof(buf), DSA_DER, |
| 377 | os.buf()->buf(), os.buf()->size(), |
| 378 | DSA_P1363); |
| 379 | |
| 380 | shared_ptr<Buffer> sigBuffer = make_shared<Buffer>(buf, bufSize); |
| 381 | |
| 382 | return Block(tlv::SignatureValue, sigBuffer); |
| 383 | } |
| 384 | |
| 385 | default: |
| 386 | BOOST_THROW_EXCEPTION(Error("Unsupported digest algorithm")); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | default: |
| 391 | BOOST_THROW_EXCEPTION(Error("Unsupported key type")); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 392 | } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 393 | } |
| 394 | catch (const CryptoPP::Exception& e) { |
| 395 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 396 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
| 400 | ConstBufferPtr |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 401 | SecTpmFile::decryptInTpm(const uint8_t* data, size_t dataLength, |
| 402 | const Name& keyName, bool isSymmetric) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 403 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 404 | BOOST_THROW_EXCEPTION(Error("SecTpmFile::decryptInTpm is not supported")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 405 | // string keyURI = keyName.toUri(); |
| 406 | // if (!isSymmetric) |
| 407 | // { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 408 | // if (!doesKeyExistInTpm(keyName, KeyClass::PRIVATE)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 409 | // throw Error("private key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 410 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 411 | // try{ |
| 412 | // using namespace CryptoPP; |
| 413 | // AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 414 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 415 | // //Read private key |
| 416 | // ByteQueue bytes; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 417 | // FileSource file(m_impl->transformName(keyURI, ".pri").string().c_str(), true, new Base64Decoder); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 418 | // file.TransferTo(bytes); |
| 419 | // bytes.MessageEnd(); |
| 420 | // RSA::PrivateKey privateKey; |
| 421 | // privateKey.Load(bytes); |
| 422 | // RSAES_PKCS1v15_Decryptor decryptor(privateKey); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 423 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 424 | // OBufferStream os; |
| 425 | // StringSource(data, dataLength, true, new PK_DecryptorFilter(rng, decryptor, new FileSink(os))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 426 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 427 | // return os.buf(); |
| 428 | // } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 429 | // catch (const CryptoPP::Exception& e){ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 430 | // throw Error(e.what()); |
| 431 | // } |
| 432 | // } |
| 433 | // else |
| 434 | // { |
| 435 | // throw Error("Symmetric encryption is not implemented!"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 436 | // // if (!doesKeyExistInTpm(keyName, KeyClass::SYMMETRIC)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 437 | // // throw Error("symmetric key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 438 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 439 | // // try{ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 440 | // // string keyBits; |
| 441 | // // string symKeyFileName = m_impl->transformName(keyURI, ".key"); |
| 442 | // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 443 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 444 | // // using CryptoPP::AES; |
| 445 | // // AutoSeededRandomPool rnd; |
| 446 | // // byte iv[AES::BLOCKSIZE]; |
| 447 | // // rnd.GenerateBlock(iv, AES::BLOCKSIZE); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 448 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 449 | // // CFB_Mode<AES>::Decryption decryptor; |
| 450 | // // decryptor.SetKeyWithIV(reinterpret_cast<const uint8_t*>(keyBits.c_str()), keyBits.size(), iv); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 451 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 452 | // // OBufferStream os; |
| 453 | // // StringSource(data, dataLength, true, new StreamTransformationFilter(decryptor,new FileSink(os))); |
| 454 | // // return os.buf(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 455 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 456 | // // } |
| 457 | // // catch (const CryptoPP::Exception& e){ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 458 | // // throw Error(e.what()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 459 | // // } |
| 460 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | ConstBufferPtr |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 464 | SecTpmFile::encryptInTpm(const uint8_t* data, size_t dataLength, |
| 465 | const Name& keyName, bool isSymmetric) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 466 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 467 | BOOST_THROW_EXCEPTION(Error("SecTpmFile::encryptInTpm is not supported")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 468 | // string keyURI = keyName.toUri(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 469 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 470 | // if (!isSymmetric) |
| 471 | // { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 472 | // if (!doesKeyExistInTpm(keyName, KeyClass::PUBLIC)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 473 | // throw Error("public key doesn't exist"); |
| 474 | // try |
| 475 | // { |
| 476 | // using namespace CryptoPP; |
| 477 | // AutoSeededRandomPool rng; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 478 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 479 | // //Read private key |
| 480 | // ByteQueue bytes; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 481 | // FileSource file(m_impl->transformName(keyURI, ".pub").string().c_str(), true, new Base64Decoder); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 482 | // file.TransferTo(bytes); |
| 483 | // bytes.MessageEnd(); |
| 484 | // RSA::PublicKey publicKey; |
| 485 | // publicKey.Load(bytes); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 486 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 487 | // OBufferStream os; |
| 488 | // RSAES_PKCS1v15_Encryptor encryptor(publicKey); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 489 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 490 | // StringSource(data, dataLength, true, new PK_EncryptorFilter(rng, encryptor, new FileSink(os))); |
| 491 | // return os.buf(); |
| 492 | // } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 493 | // catch (const CryptoPP::Exception& e){ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 494 | // throw Error(e.what()); |
| 495 | // } |
| 496 | // } |
| 497 | // else |
| 498 | // { |
| 499 | // throw Error("Symmetric encryption is not implemented!"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 500 | // // if (!doesKeyExistInTpm(keyName, KeyClass::SYMMETRIC)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 501 | // // throw Error("symmetric key doesn't exist"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 502 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 503 | // // try{ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 504 | // // string keyBits; |
| 505 | // // string symKeyFileName = m_impl->transformName(keyURI, ".key"); |
| 506 | // // FileSource(symKeyFileName, true, new HexDecoder(new StringSink(keyBits))); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 507 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 508 | // // using CryptoPP::AES; |
| 509 | // // AutoSeededRandomPool rnd; |
| 510 | // // byte iv[AES::BLOCKSIZE]; |
| 511 | // // rnd.GenerateBlock(iv, AES::BLOCKSIZE); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 512 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 513 | // // CFB_Mode<AES>::Encryption encryptor; |
| 514 | // // 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] | 515 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 516 | // // OBufferStream os; |
| 517 | // // StringSource(data, dataLength, true, new StreamTransformationFilter(encryptor, new FileSink(os))); |
| 518 | // // return os.buf(); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 519 | // // } catch (const CryptoPP::Exception& e){ |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 520 | // // throw Error(e.what()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 521 | // // } |
| 522 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 525 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 526 | SecTpmFile::generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 527 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 528 | BOOST_THROW_EXCEPTION(Error("SecTpmFile::generateSymmetricKeyInTpm is not supported")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 529 | // string keyURI = keyName.toUri(); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 530 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 531 | // if (doesKeyExistInTpm(keyName, KeyClass::SYMMETRIC)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 532 | // throw Error("symmetric key exists"); |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 533 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 534 | // string keyFileName = m_impl->maintainMapping(keyURI); |
| 535 | // string symKeyFileName = keyFileName + ".key"; |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 536 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 537 | // try{ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 538 | // switch (keyType){ |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 539 | // case KeyType::AES: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 540 | // { |
| 541 | // using namespace CryptoPP; |
| 542 | // AutoSeededRandomPool rng; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 543 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 544 | // SecByteBlock key(0x00, keySize); |
| 545 | // rng.GenerateBlock(key, keySize); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 546 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 547 | // StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str()))); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 548 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 549 | // chmod(symKeyFileName.c_str(), 0000400); |
| 550 | // return; |
| 551 | // } |
| 552 | // default: |
| 553 | // throw Error("Unsupported symmetric key type!"); |
| 554 | // } |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 555 | // } catch (const CryptoPP::Exception& e){ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 556 | // throw Error(e.what()); |
| 557 | // } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 561 | SecTpmFile::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 562 | { |
| 563 | string keyURI = keyName.toUri(); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 564 | if (keyClass == KeyClass::PUBLIC) { |
| 565 | return boost::filesystem::exists(m_impl->transformName(keyURI, ".pub")); |
| 566 | } |
| 567 | if (keyClass == KeyClass::PRIVATE) { |
| 568 | return boost::filesystem::exists(m_impl->transformName(keyURI, ".pri")); |
| 569 | } |
| 570 | if (keyClass == KeyClass::SYMMETRIC) { |
| 571 | return boost::filesystem::exists(m_impl->transformName(keyURI, ".key")); |
| 572 | } |
Yingdi Yu | 2d9c50f | 2014-01-21 18:25:00 -0800 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 576 | bool |
| 577 | SecTpmFile::generateRandomBlock(uint8_t* res, size_t size) |
| 578 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 579 | try { |
| 580 | CryptoPP::AutoSeededRandomPool rng; |
| 581 | rng.GenerateBlock(res, size); |
| 582 | return true; |
| 583 | } |
| 584 | catch (const CryptoPP::Exception& e) { |
| 585 | return false; |
| 586 | } |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 589 | } // namespace ndn |