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