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