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 | 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 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" |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | namespace ndn { |
| 33 | |
| 34 | ConstBufferPtr |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 35 | SecTpm::exportPrivateKeyPkcs5FromTpm(const Name& keyName, const string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 36 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 37 | using namespace CryptoPP; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 39 | uint8_t salt[8] = {0}; |
| 40 | uint8_t iv[8] = {0}; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 42 | // derive key |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | if (!generateRandomBlock(salt, 8) || !generateRandomBlock(iv, 8)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 44 | throw Error("Cannot generate salt or iv"); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 46 | uint32_t iterationCount = 2048; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 48 | PKCS5_PBKDF2_HMAC<SHA1> keyGenerator; |
| 49 | size_t derivedLen = 24; //For DES-EDE3-CBC-PAD |
| 50 | byte derived[24] = {0}; |
| 51 | byte purpose = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 53 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 54 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | keyGenerator.DeriveKey(derived, derivedLen, purpose, |
| 56 | reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 57 | salt, 8, iterationCount); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 58 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 60 | { |
| 61 | throw Error("Cannot derived the encryption key"); |
| 62 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 63 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 64 | //encrypt |
| 65 | CBC_Mode< DES_EDE3 >::Encryption e; |
| 66 | e.SetKeyWithIV(derived, derivedLen, iv); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 68 | ConstBufferPtr pkcs8PrivateKey = exportPrivateKeyPkcs8FromTpm(keyName); |
| 69 | if (!static_cast<bool>(pkcs8PrivateKey)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 70 | throw Error("Cannot export the private key, #1"); |
| 71 | |
| 72 | OBufferStream encryptedOs; |
| 73 | try |
| 74 | { |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 75 | StringSource stringSource(pkcs8PrivateKey->buf(), pkcs8PrivateKey->size(), true, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 76 | new StreamTransformationFilter(e, new FileSink(encryptedOs))); |
| 77 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 79 | { |
| 80 | throw Error("Cannot export the private key, #2"); |
| 81 | } |
| 82 | |
| 83 | //encode |
| 84 | OID pbes2Id("1.2.840.113549.1.5.13"); |
| 85 | OID pbkdf2Id("1.2.840.113549.1.5.12"); |
| 86 | OID pbes2encsId("1.2.840.113549.3.7"); |
| 87 | |
| 88 | OBufferStream pkcs8Os; |
| 89 | try |
| 90 | { |
| 91 | FileSink sink(pkcs8Os); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 92 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 93 | // EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 94 | // encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 95 | // encryptedData OCTET STRING } |
| 96 | DERSequenceEncoder encryptedPrivateKeyInfo(sink); |
| 97 | { |
| 98 | // EncryptionAlgorithmIdentifier ::= SEQUENCE { |
| 99 | // algorithm OBJECT IDENTIFIER {{PBES2-id}}, |
| 100 | // parameters SEQUENCE {{PBES2-params}} } |
| 101 | DERSequenceEncoder encryptionAlgorithm(encryptedPrivateKeyInfo); |
| 102 | { |
| 103 | pbes2Id.encode(encryptionAlgorithm); |
| 104 | // PBES2-params ::= SEQUENCE { |
| 105 | // keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
| 106 | // encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } |
| 107 | DERSequenceEncoder pbes2Params(encryptionAlgorithm); |
| 108 | { |
| 109 | // AlgorithmIdentifier ::= SEQUENCE { |
| 110 | // algorithm OBJECT IDENTIFIER {{PBKDF2-id}}, |
| 111 | // parameters SEQUENCE {{PBKDF2-params}} } |
| 112 | DERSequenceEncoder pbes2KDFs(pbes2Params); |
| 113 | { |
| 114 | pbkdf2Id.encode(pbes2KDFs); |
| 115 | // AlgorithmIdentifier ::= SEQUENCE { |
| 116 | // salt OCTET STRING, |
| 117 | // iterationCount INTEGER (1..MAX), |
| 118 | // keyLength INTEGER (1..MAX) OPTIONAL, |
| 119 | // prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1 } |
| 120 | DERSequenceEncoder pbkdf2Params(pbes2KDFs); |
| 121 | { |
| 122 | DEREncodeOctetString(pbkdf2Params, salt, 8); |
| 123 | DEREncodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER); |
| 124 | } |
| 125 | pbkdf2Params.MessageEnd(); |
| 126 | } |
| 127 | pbes2KDFs.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 128 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 129 | // AlgorithmIdentifier ::= SEQUENCE { |
| 130 | // algorithm OBJECT IDENTIFIER {{DES-EDE3-CBC-PAD}}, |
| 131 | // parameters OCTET STRING} {{iv}} } |
| 132 | DERSequenceEncoder pbes2Encs(pbes2Params); |
| 133 | { |
| 134 | pbes2encsId.encode(pbes2Encs); |
| 135 | DEREncodeOctetString(pbes2Encs, iv, 8); |
| 136 | } |
| 137 | pbes2Encs.MessageEnd(); |
| 138 | } |
| 139 | pbes2Params.MessageEnd(); |
| 140 | } |
| 141 | encryptionAlgorithm.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 142 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 143 | DEREncodeOctetString(encryptedPrivateKeyInfo, |
| 144 | encryptedOs.buf()->buf(), encryptedOs.buf()->size()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 145 | } |
| 146 | encryptedPrivateKeyInfo.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 148 | return pkcs8Os.buf(); |
| 149 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 151 | { |
| 152 | throw Error("Cannot export the private key, #3"); |
| 153 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 157 | SecTpm::importPrivateKeyPkcs5IntoTpm(const Name& keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 158 | const uint8_t* buf, size_t size, |
| 159 | const string& passwordStr) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 160 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 161 | using namespace CryptoPP; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 162 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 163 | OID pbes2Id; |
| 164 | OID pbkdf2Id; |
| 165 | SecByteBlock saltBlock; |
| 166 | uint32_t iterationCount; |
| 167 | OID pbes2encsId; |
| 168 | SecByteBlock ivBlock; |
| 169 | SecByteBlock encryptedDataBlock; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 170 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 171 | try |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 172 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 173 | // decode some decoding processes are not necessary for now, |
| 174 | // because we assume only one encryption scheme. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 175 | StringSource source(buf, size, true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 176 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 177 | // EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 178 | // encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 179 | // encryptedData OCTET STRING } |
| 180 | BERSequenceDecoder encryptedPrivateKeyInfo(source); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 181 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 182 | // EncryptionAlgorithmIdentifier ::= SEQUENCE { |
| 183 | // algorithm OBJECT IDENTIFIER {{PBES2-id}}, |
| 184 | // parameters SEQUENCE {{PBES2-params}} } |
| 185 | BERSequenceDecoder encryptionAlgorithm(encryptedPrivateKeyInfo); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 186 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 187 | pbes2Id.decode(encryptionAlgorithm); |
| 188 | // PBES2-params ::= SEQUENCE { |
| 189 | // keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, |
| 190 | // encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } |
| 191 | BERSequenceDecoder pbes2Params(encryptionAlgorithm); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 192 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 193 | // AlgorithmIdentifier ::= SEQUENCE { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 194 | // algorithm OBJECT IDENTIFIER {{PBKDF2-id}}, |
| 195 | // parameters SEQUENCE {{PBKDF2-params}} } |
| 196 | BERSequenceDecoder pbes2KDFs(pbes2Params); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 197 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 198 | pbkdf2Id.decode(pbes2KDFs); |
| 199 | // AlgorithmIdentifier ::= SEQUENCE { |
| 200 | // salt OCTET STRING, |
| 201 | // iterationCount INTEGER (1..MAX), |
| 202 | // keyLength INTEGER (1..MAX) OPTIONAL, |
| 203 | // prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1 } |
| 204 | BERSequenceDecoder pbkdf2Params(pbes2KDFs); |
| 205 | { |
| 206 | BERDecodeOctetString(pbkdf2Params, saltBlock); |
| 207 | BERDecodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER); |
| 208 | } |
| 209 | pbkdf2Params.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 210 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 211 | pbes2KDFs.MessageEnd(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 212 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 213 | // AlgorithmIdentifier ::= SEQUENCE { |
| 214 | // algorithm OBJECT IDENTIFIER {{DES-EDE3-CBC-PAD}}, |
| 215 | // parameters OCTET STRING} {{iv}} } |
| 216 | BERSequenceDecoder pbes2Encs(pbes2Params); |
| 217 | { |
| 218 | pbes2encsId.decode(pbes2Encs); |
| 219 | BERDecodeOctetString(pbes2Encs, ivBlock); |
| 220 | } |
| 221 | pbes2Encs.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 222 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 223 | pbes2Params.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 224 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 225 | encryptionAlgorithm.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 226 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 227 | BERDecodeOctetString(encryptedPrivateKeyInfo, encryptedDataBlock); |
| 228 | } |
| 229 | encryptedPrivateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 230 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 231 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 232 | { |
| 233 | return false; |
| 234 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 236 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 237 | PKCS5_PBKDF2_HMAC<SHA1> keyGenerator; |
| 238 | size_t derivedLen = 24; //For DES-EDE3-CBC-PAD |
| 239 | byte derived[24] = {0}; |
| 240 | byte purpose = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 241 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 242 | try |
| 243 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 244 | keyGenerator.DeriveKey(derived, derivedLen, |
| 245 | purpose, |
| 246 | reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(), |
| 247 | saltBlock.BytePtr(), saltBlock.size(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 248 | iterationCount); |
| 249 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 250 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 251 | { |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 252 | return false; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 253 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 255 | //decrypt |
| 256 | CBC_Mode< DES_EDE3 >::Decryption d; |
| 257 | d.SetKeyWithIV(derived, derivedLen, ivBlock.BytePtr()); |
| 258 | |
| 259 | OBufferStream privateKeyOs; |
| 260 | try |
| 261 | { |
| 262 | StringSource encryptedSource(encryptedDataBlock.BytePtr(), encryptedDataBlock.size(), true, |
| 263 | new StreamTransformationFilter(d, new FileSink(privateKeyOs))); |
| 264 | } |
| 265 | catch (CryptoPP::Exception& e) |
| 266 | { |
| 267 | return false; |
| 268 | } |
| 269 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 270 | if (!importPrivateKeyPkcs8IntoTpm(keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 271 | privateKeyOs.buf()->buf(), privateKeyOs.buf()->size())) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 272 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 273 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 274 | //derive public key |
| 275 | OBufferStream publicKeyOs; |
| 276 | |
| 277 | try |
| 278 | { |
| 279 | RSA::PrivateKey privateKey; |
| 280 | privateKey.Load(StringStore(privateKeyOs.buf()->buf(), privateKeyOs.buf()->size()).Ref()); |
| 281 | RSAFunction publicKey(privateKey); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 282 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 283 | FileSink publicKeySink(publicKeyOs); |
| 284 | publicKey.DEREncode(publicKeySink); |
| 285 | publicKeySink.MessageEnd(); |
| 286 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 287 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 288 | { |
| 289 | return false; |
| 290 | } |
| 291 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 292 | if (!importPublicKeyPkcs1IntoTpm(keyName, publicKeyOs.buf()->buf(), publicKeyOs.buf()->size())) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 293 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 294 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 295 | return true; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 299 | } // namespace ndn |