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 | 8dceb1d | 2014-02-18 12:45:10 -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 Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "sec-tpm.hpp" |
| 25 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "../encoding/oid.hpp" |
| 27 | #include "../encoding/buffer-stream.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 28 | #include "cryptopp.hpp" |
Alexander Afanasyev | a2ada22 | 2015-01-22 18:34:16 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 35 | SecTpm::SecTpm(const string& location) |
| 36 | : m_location(location) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | SecTpm::~SecTpm() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | std::string |
| 45 | SecTpm::getTpmLocator() |
| 46 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 47 | return this->getScheme() + ":" + m_location; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 50 | ConstBufferPtr |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 51 | SecTpm::exportPrivateKeyPkcs5FromTpm(const Name& keyName, const string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 52 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 53 | using namespace CryptoPP; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 55 | uint8_t salt[8] = {0}; |
| 56 | uint8_t iv[8] = {0}; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 58 | // derive key |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | if (!generateRandomBlock(salt, 8) || !generateRandomBlock(iv, 8)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 60 | BOOST_THROW_EXCEPTION(Error("Cannot generate salt or iv")); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 61 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 62 | uint32_t iterationCount = 2048; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 64 | PKCS5_PBKDF2_HMAC<SHA1> keyGenerator; |
| 65 | size_t derivedLen = 24; //For DES-EDE3-CBC-PAD |
| 66 | byte derived[24] = {0}; |
| 67 | byte purpose = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 69 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | keyGenerator.DeriveKey(derived, derivedLen, purpose, |
| 72 | reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 73 | salt, 8, iterationCount); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 74 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 75 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 76 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 77 | BOOST_THROW_EXCEPTION(Error("Cannot derived the encryption key")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 78 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 79 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 80 | //encrypt |
| 81 | CBC_Mode< DES_EDE3 >::Encryption e; |
| 82 | e.SetKeyWithIV(derived, derivedLen, iv); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 84 | ConstBufferPtr pkcs8PrivateKey = exportPrivateKeyPkcs8FromTpm(keyName); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 86 | if (!static_cast<bool>(pkcs8PrivateKey)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 87 | BOOST_THROW_EXCEPTION(Error("Cannot export the private key, #1")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 88 | |
| 89 | OBufferStream encryptedOs; |
| 90 | try |
| 91 | { |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 92 | StringSource stringSource(pkcs8PrivateKey->buf(), pkcs8PrivateKey->size(), true, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 93 | new StreamTransformationFilter(e, new FileSink(encryptedOs))); |
| 94 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 96 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("Cannot export the private key, #2")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | //encode |
| 101 | OID pbes2Id("1.2.840.113549.1.5.13"); |
| 102 | OID pbkdf2Id("1.2.840.113549.1.5.12"); |
| 103 | OID pbes2encsId("1.2.840.113549.3.7"); |
| 104 | |
| 105 | OBufferStream pkcs8Os; |
| 106 | try |
| 107 | { |
| 108 | FileSink sink(pkcs8Os); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 110 | // EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 111 | // encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 112 | // encryptedData OCTET STRING } |
| 113 | DERSequenceEncoder encryptedPrivateKeyInfo(sink); |
| 114 | { |
| 115 | // EncryptionAlgorithmIdentifier ::= SEQUENCE { |
| 116 | // algorithm OBJECT IDENTIFIER {{PBES2-id}}, |
| 117 | // parameters SEQUENCE {{PBES2-params}} } |
| 118 | DERSequenceEncoder encryptionAlgorithm(encryptedPrivateKeyInfo); |
| 119 | { |
| 120 | pbes2Id.encode(encryptionAlgorithm); |
| 121 | // PBES2-params ::= SEQUENCE { |
| 122 | // keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
| 123 | // encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } |
| 124 | DERSequenceEncoder pbes2Params(encryptionAlgorithm); |
| 125 | { |
| 126 | // AlgorithmIdentifier ::= SEQUENCE { |
| 127 | // algorithm OBJECT IDENTIFIER {{PBKDF2-id}}, |
| 128 | // parameters SEQUENCE {{PBKDF2-params}} } |
| 129 | DERSequenceEncoder pbes2KDFs(pbes2Params); |
| 130 | { |
| 131 | pbkdf2Id.encode(pbes2KDFs); |
| 132 | // AlgorithmIdentifier ::= SEQUENCE { |
| 133 | // salt OCTET STRING, |
| 134 | // iterationCount INTEGER (1..MAX), |
| 135 | // keyLength INTEGER (1..MAX) OPTIONAL, |
| 136 | // prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1 } |
| 137 | DERSequenceEncoder pbkdf2Params(pbes2KDFs); |
| 138 | { |
| 139 | DEREncodeOctetString(pbkdf2Params, salt, 8); |
| 140 | DEREncodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER); |
| 141 | } |
| 142 | pbkdf2Params.MessageEnd(); |
| 143 | } |
| 144 | pbes2KDFs.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 145 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 146 | // AlgorithmIdentifier ::= SEQUENCE { |
| 147 | // algorithm OBJECT IDENTIFIER {{DES-EDE3-CBC-PAD}}, |
| 148 | // parameters OCTET STRING} {{iv}} } |
| 149 | DERSequenceEncoder pbes2Encs(pbes2Params); |
| 150 | { |
| 151 | pbes2encsId.encode(pbes2Encs); |
| 152 | DEREncodeOctetString(pbes2Encs, iv, 8); |
| 153 | } |
| 154 | pbes2Encs.MessageEnd(); |
| 155 | } |
| 156 | pbes2Params.MessageEnd(); |
| 157 | } |
| 158 | encryptionAlgorithm.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 159 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 160 | DEREncodeOctetString(encryptedPrivateKeyInfo, |
| 161 | encryptedOs.buf()->buf(), encryptedOs.buf()->size()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 162 | } |
| 163 | encryptedPrivateKeyInfo.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 164 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 165 | return pkcs8Os.buf(); |
| 166 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 167 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 168 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(Error("Cannot export the private key, #3")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 170 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 174 | SecTpm::importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 175 | const uint8_t* buf, size_t size, |
| 176 | const string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 177 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 178 | using namespace CryptoPP; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 179 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 180 | OID pbes2Id; |
| 181 | OID pbkdf2Id; |
| 182 | SecByteBlock saltBlock; |
| 183 | uint32_t iterationCount; |
| 184 | OID pbes2encsId; |
| 185 | SecByteBlock ivBlock; |
| 186 | SecByteBlock encryptedDataBlock; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 187 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 188 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 189 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 190 | // decode some decoding processes are not necessary for now, |
| 191 | // because we assume only one encryption scheme. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 192 | StringSource source(buf, size, true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 193 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 194 | // EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 195 | // encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 196 | // encryptedData OCTET STRING } |
| 197 | BERSequenceDecoder encryptedPrivateKeyInfo(source); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 198 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 199 | // EncryptionAlgorithmIdentifier ::= SEQUENCE { |
| 200 | // algorithm OBJECT IDENTIFIER {{PBES2-id}}, |
| 201 | // parameters SEQUENCE {{PBES2-params}} } |
| 202 | BERSequenceDecoder encryptionAlgorithm(encryptedPrivateKeyInfo); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 203 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 204 | pbes2Id.decode(encryptionAlgorithm); |
| 205 | // PBES2-params ::= SEQUENCE { |
| 206 | // keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
| 207 | // encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } |
| 208 | BERSequenceDecoder pbes2Params(encryptionAlgorithm); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 209 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 210 | // AlgorithmIdentifier ::= SEQUENCE { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 211 | // algorithm OBJECT IDENTIFIER {{PBKDF2-id}}, |
| 212 | // parameters SEQUENCE {{PBKDF2-params}} } |
| 213 | BERSequenceDecoder pbes2KDFs(pbes2Params); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 214 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 215 | pbkdf2Id.decode(pbes2KDFs); |
| 216 | // AlgorithmIdentifier ::= SEQUENCE { |
| 217 | // salt OCTET STRING, |
| 218 | // iterationCount INTEGER (1..MAX), |
| 219 | // keyLength INTEGER (1..MAX) OPTIONAL, |
| 220 | // prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1 } |
| 221 | BERSequenceDecoder pbkdf2Params(pbes2KDFs); |
| 222 | { |
| 223 | BERDecodeOctetString(pbkdf2Params, saltBlock); |
| 224 | BERDecodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER); |
| 225 | } |
| 226 | pbkdf2Params.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 227 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 228 | pbes2KDFs.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 229 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 230 | // AlgorithmIdentifier ::= SEQUENCE { |
| 231 | // algorithm OBJECT IDENTIFIER {{DES-EDE3-CBC-PAD}}, |
| 232 | // parameters OCTET STRING} {{iv}} } |
| 233 | BERSequenceDecoder pbes2Encs(pbes2Params); |
| 234 | { |
| 235 | pbes2encsId.decode(pbes2Encs); |
| 236 | BERDecodeOctetString(pbes2Encs, ivBlock); |
| 237 | } |
| 238 | pbes2Encs.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 239 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 240 | pbes2Params.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 241 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 242 | encryptionAlgorithm.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 243 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 244 | BERDecodeOctetString(encryptedPrivateKeyInfo, encryptedDataBlock); |
| 245 | } |
| 246 | encryptedPrivateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 247 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 248 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 249 | { |
| 250 | return false; |
| 251 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 252 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 253 | PKCS5_PBKDF2_HMAC<SHA1> keyGenerator; |
| 254 | size_t derivedLen = 24; //For DES-EDE3-CBC-PAD |
| 255 | byte derived[24] = {0}; |
| 256 | byte purpose = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 257 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 258 | try |
| 259 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 260 | keyGenerator.DeriveKey(derived, derivedLen, |
| 261 | purpose, |
| 262 | reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(), |
| 263 | saltBlock.BytePtr(), saltBlock.size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 264 | iterationCount); |
| 265 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 266 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 267 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 268 | return false; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 269 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 270 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 271 | //decrypt |
| 272 | CBC_Mode< DES_EDE3 >::Decryption d; |
| 273 | d.SetKeyWithIV(derived, derivedLen, ivBlock.BytePtr()); |
| 274 | |
| 275 | OBufferStream privateKeyOs; |
| 276 | try |
| 277 | { |
| 278 | StringSource encryptedSource(encryptedDataBlock.BytePtr(), encryptedDataBlock.size(), true, |
| 279 | new StreamTransformationFilter(d, new FileSink(privateKeyOs))); |
| 280 | } |
| 281 | catch (CryptoPP::Exception& e) |
| 282 | { |
| 283 | return false; |
| 284 | } |
| 285 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 286 | if (!importPrivateKeyPkcs8IntoTpm(keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 287 | privateKeyOs.buf()->buf(), privateKeyOs.buf()->size())) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 288 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 289 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 290 | //determine key type |
| 291 | StringSource privateKeySource(privateKeyOs.buf()->buf(), privateKeyOs.buf()->size(), true); |
| 292 | |
| 293 | KeyType publicKeyType = KEY_TYPE_NULL; |
| 294 | SecByteBlock rawKeyBits; |
| 295 | // PrivateKeyInfo ::= SEQUENCE { |
| 296 | // INTEGER, |
| 297 | // SEQUENCE, |
| 298 | // OCTECT STRING} |
| 299 | BERSequenceDecoder privateKeyInfo(privateKeySource); |
| 300 | { |
| 301 | uint32_t versionNum; |
| 302 | BERDecodeUnsigned<uint32_t>(privateKeyInfo, versionNum, INTEGER); |
| 303 | BERSequenceDecoder sequenceDecoder(privateKeyInfo); |
| 304 | { |
| 305 | OID keyTypeOID; |
| 306 | keyTypeOID.decode(sequenceDecoder); |
| 307 | if (keyTypeOID == oid::RSA) |
| 308 | publicKeyType = KEY_TYPE_RSA; |
| 309 | else if (keyTypeOID == oid::ECDSA) |
| 310 | publicKeyType = KEY_TYPE_ECDSA; |
| 311 | else |
| 312 | return false; // Unsupported key type; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 317 | //derive public key |
| 318 | OBufferStream publicKeyOs; |
| 319 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 320 | try { |
| 321 | switch (publicKeyType) { |
| 322 | case KEY_TYPE_RSA: |
| 323 | { |
| 324 | RSA::PrivateKey privateKey; |
| 325 | privateKey.Load(StringStore(privateKeyOs.buf()->buf(), privateKeyOs.buf()->size()).Ref()); |
| 326 | RSAFunction publicKey(privateKey); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 327 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 328 | FileSink publicKeySink(publicKeyOs); |
| 329 | publicKey.DEREncode(publicKeySink); |
| 330 | publicKeySink.MessageEnd(); |
| 331 | break; |
| 332 | } |
| 333 | case KEY_TYPE_ECDSA: |
| 334 | { |
| 335 | ECDSA<ECP, SHA256>::PrivateKey privateKey; |
| 336 | privateKey.Load(StringStore(privateKeyOs.buf()->buf(), privateKeyOs.buf()->size()).Ref()); |
| 337 | |
| 338 | ECDSA<ECP, SHA256>::PublicKey publicKey; |
| 339 | privateKey.MakePublicKey(publicKey); |
| 340 | publicKey.AccessGroupParameters().SetEncodeAsOID(true); |
| 341 | |
| 342 | FileSink publicKeySink(publicKeyOs); |
| 343 | publicKey.DEREncode(publicKeySink); |
| 344 | publicKeySink.MessageEnd(); |
| 345 | break; |
| 346 | } |
| 347 | default: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 348 | return false; |
| 349 | } |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 350 | } |
| 351 | catch (CryptoPP::Exception& e) { |
| 352 | return false; |
| 353 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 354 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 355 | if (!importPublicKeyPkcs1IntoTpm(keyName, publicKeyOs.buf()->buf(), publicKeyOs.buf()->size())) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 356 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 357 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 358 | return true; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 361 | bool |
| 362 | SecTpm::getImpExpPassWord(std::string& password, const std::string& prompt) |
| 363 | { |
| 364 | bool isInitialized = false; |
| 365 | |
Alexander Afanasyev | cf3a667 | 2015-02-01 20:33:22 -0800 | [diff] [blame] | 366 | #ifdef NDN_CXX_HAVE_GETPASS |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 367 | char* pw0 = 0; |
| 368 | |
| 369 | pw0 = getpass(prompt.c_str()); |
| 370 | if (0 == pw0) |
| 371 | return false; |
| 372 | std::string password1 = pw0; |
| 373 | memset(pw0, 0, strlen(pw0)); |
| 374 | |
| 375 | pw0 = getpass("Confirm:"); |
| 376 | if (0 == pw0) |
| 377 | { |
| 378 | std::fill(password1.begin(), password1.end(), 0); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (0 == password1.compare(pw0)) |
| 383 | { |
| 384 | isInitialized = true; |
| 385 | password.swap(password1); |
| 386 | } |
| 387 | |
| 388 | std::fill(password1.begin(), password1.end(), 0); |
| 389 | memset(pw0, 0, strlen(pw0)); |
| 390 | |
| 391 | if (password.empty()) |
| 392 | return false; |
| 393 | |
Alexander Afanasyev | a2ada22 | 2015-01-22 18:34:16 -0800 | [diff] [blame] | 394 | #endif // NDN_CXX_HAVE_GETPASS |
| 395 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 396 | return isInitialized; |
| 397 | } |
| 398 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 399 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 400 | } // namespace ndn |