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