Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 9 | #ifndef NDN_SECURITY_SEC_TPM_HPP |
| 10 | #define NDN_SECURITY_SEC_TPM_HPP |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 12 | #include "../common.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 13 | #include "security-common.hpp" |
| 14 | #include "../name.hpp" |
| 15 | #include "../data.hpp" |
| 16 | #include "public-key.hpp" |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 17 | |
| 18 | namespace ndn { |
| 19 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 20 | /** |
| 21 | * @brief SecTpm is the base class of the TPM classes. |
| 22 | * |
| 23 | * It specifies the interfaces of private/secret key related operations. |
| 24 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 25 | class SecTpm { |
Jeff Thompson | a50703f | 2013-09-17 14:24:15 -0700 | [diff] [blame] | 26 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 27 | class Error : public std::runtime_error |
| 28 | { |
| 29 | public: |
| 30 | explicit |
| 31 | Error(const std::string& what) |
| 32 | : std::runtime_error(what) |
| 33 | { |
| 34 | } |
| 35 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 37 | virtual |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 38 | ~SecTpm() {} |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 40 | /** |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 41 | * @brief set password of TPM |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 42 | * |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 43 | * Password is used to unlock TPM when it is locked. |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame^] | 44 | * You should be cautious when using this method, because remembering password is kind of |
| 45 | * dangerous. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 46 | * |
| 47 | * @param password The password. |
| 48 | * @param passwordLength The length of password. |
| 49 | */ |
| 50 | virtual void |
| 51 | setTpmPassword(const uint8_t* password, size_t passwordLength) = 0; |
| 52 | |
| 53 | /** |
| 54 | * @brief reset password of TPM |
| 55 | */ |
| 56 | virtual void |
| 57 | resetTpmPassword() = 0; |
| 58 | |
| 59 | /** |
| 60 | * @brief set inTerminal flag |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | * |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 62 | * If the inTerminal flag is set, and password is not set, TPM may ask for password via terminal. |
| 63 | * inTerminal flag is set by default. |
| 64 | * |
| 65 | * @param inTerminal. |
| 66 | */ |
| 67 | virtual void |
| 68 | setInTerminal(bool inTerminal) = 0; |
| 69 | |
| 70 | /** |
| 71 | * @brief get inTerminal flag |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | * |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 73 | * @return inTerminal flag. |
| 74 | */ |
| 75 | virtual bool |
| 76 | getInTerminal() = 0; |
| 77 | |
| 78 | /** |
| 79 | * @brief check if TPM is locked. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | * |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 81 | * @return true if locked, false otherwise |
| 82 | */ |
| 83 | virtual bool |
| 84 | locked() = 0; |
| 85 | |
| 86 | /** |
| 87 | * @brief Unlock the TPM. |
| 88 | * |
| 89 | * @param password The password. |
| 90 | * @param passwordLength The password size. 0 indicates no password. |
| 91 | * @param usePassword True if we want to use the supplied password to unlock the TPM. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 92 | * @return true if TPM is unlocked, otherwise false. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 93 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 94 | virtual bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 95 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0; |
| 96 | |
| 97 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 98 | * @brief Generate a pair of asymmetric keys. |
| 99 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 100 | * @param keyName The name of the key pair. |
| 101 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 102 | * @param keySize The size of the key pair. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 103 | * @throws SecTpm::Error if fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 104 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 105 | virtual void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 106 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 108 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 109 | * @brief Delete a key pair of asymmetric keys. |
| 110 | * |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 111 | * @param keyName The name of the key pair. |
| 112 | */ |
| 113 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 114 | deleteKeyPairInTpm(const Name& keyName) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 117 | * @brief Get a public key. |
| 118 | * |
| 119 | * @param keyName The public key name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 120 | * @return The public key. |
| 121 | * @throws SecTpm::Error if public key does not exist in TPM. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 122 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 124 | getPublicKeyFromTpm(const Name& keyName) = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 126 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 127 | * @brief Sign data. |
| 128 | * |
| 129 | * @param data Pointer to the byte array to be signed. |
Jeff Thompson | 4c11b9f | 2013-09-13 11:05:28 -0700 | [diff] [blame] | 130 | * @param dataLength The length of data. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 131 | * @param keyName The name of the signing key. |
| 132 | * @param digestAlgorithm the digest algorithm. |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 133 | * @return The signature block. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 134 | * @throws SecTpm::Error if signing fails. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 135 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 136 | virtual Block |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame^] | 137 | signInTpm(const uint8_t* data, size_t dataLength, |
| 138 | const Name& keyName, |
| 139 | DigestAlgorithm digestAlgorithm) = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 140 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 141 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 142 | * @brief Decrypt data. |
| 143 | * |
| 144 | * @param data Pointer to the byte arry to be decrypted. |
| 145 | * @param dataLength The length of data. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 146 | * @param keyName The name of the decrypting key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 147 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | * @return The decrypted data. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 149 | * @throws SecTpm::Error if decryption fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 150 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 151 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 152 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 153 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 154 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 155 | * @brief Encrypt data. |
| 156 | * |
| 157 | * @param data Pointer to the byte arry to be decrypted. |
| 158 | * @param dataLength The length of data. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 159 | * @param keyName The name of the encrypting key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 160 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 161 | * @return The encrypted data. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 162 | * @throws SecTpm::Error if encryption fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 163 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 164 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 165 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 166 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 167 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 168 | * @brief Generate a symmetric key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 169 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | * @param keyName The name of the key. |
| 171 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 172 | * @param keySize The size of the key. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 173 | * @throws SecTpm::Error if key generating fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 174 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 175 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 176 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 179 | * @brief Check if a particular key exists. |
| 180 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 181 | * @param keyName The name of the key. |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame^] | 182 | * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 183 | * @return True if the key exists, otherwise false. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 184 | */ |
| 185 | virtual bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 186 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 187 | |
| 188 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 189 | * @brief Generate a random block. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 190 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 191 | * @param res The pointer to the generated block. |
| 192 | * @param size The random block size. |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 193 | * @return true for success, otherwise false. |
| 194 | */ |
| 195 | virtual bool |
| 196 | generateRandomBlock(uint8_t* res, size_t size) = 0; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 197 | |
| 198 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 199 | * @brief Add the application into the ACL of a particular key. |
| 200 | * |
| 201 | * @param keyName the name of key |
| 202 | * @param keyClass the class of key, e.g. Private Key |
| 203 | * @param appPath the absolute path to the application |
| 204 | * @param acl the new acl of the key |
| 205 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 206 | virtual void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 207 | addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0; |
| 208 | |
| 209 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 210 | * @brief Export a private key in PKCS#8 format. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 211 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 212 | * @param keyName The private key name. |
| 213 | * @param password The password to encrypt the private key. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 214 | * @return The private key info (in PKCS8 format) if exist. |
| 215 | * @throws SecTpm::Error if private key cannot be exported. |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 216 | */ |
| 217 | ConstBufferPtr |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 218 | exportPrivateKeyPkcs8FromTpm(const Name& keyName, const std::string& password); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * @brief Import a private key in PKCS#8 format. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 222 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 223 | * Also recover the public key and installed it in TPM. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 224 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 225 | * @param keyName The private key name. |
| 226 | * @param key The encoded private key info. |
| 227 | * @param password The password to encrypt the private key. |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 228 | * @return False if import fails. |
| 229 | */ |
| 230 | bool |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame^] | 231 | importPrivateKeyPkcs8IntoTpm(const Name& keyName, |
| 232 | const uint8_t* buf, size_t size, |
| 233 | const std::string& password); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 234 | |
| 235 | protected: |
| 236 | /** |
| 237 | * @brief Export a private key in PKCS#1 format. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 238 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 239 | * @param keyName The private key name. |
| 240 | * @return The private key info (in PKCS#1 format) if exist, otherwise a NULL pointer. |
| 241 | */ |
| 242 | virtual ConstBufferPtr |
| 243 | exportPrivateKeyPkcs1FromTpm(const Name& keyName) = 0; |
| 244 | |
| 245 | /** |
| 246 | * @brief Import a private key in PKCS#1 format. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 247 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 248 | * @param keyName The private key name. |
| 249 | * @param key The encoded private key info. |
| 250 | * @return False if import fails. |
| 251 | */ |
| 252 | virtual bool |
| 253 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0; |
| 254 | |
| 255 | /** |
| 256 | * @brief Import a public key in PKCS#1 format. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 257 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 258 | * @param keyName The public key name. |
| 259 | * @param key The encoded public key info. |
| 260 | * @return False if import fails. |
| 261 | */ |
| 262 | virtual bool |
| 263 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0; |
| 264 | |
| 265 | |
| 266 | /** |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 267 | * @brief Get import/export password. |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 268 | * |
| 269 | * @param password On return, the password. |
| 270 | * @param prompt Prompt for password, i.e., "Password for key:" |
| 271 | * @return true if password has been obtained. |
| 272 | */ |
| 273 | inline virtual bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 274 | getImpExpPassWord(std::string& password, const std::string& prompt); |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 277 | inline bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 278 | SecTpm::getImpExpPassWord(std::string& password, const std::string& prompt) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 279 | { |
| 280 | int result = false; |
| 281 | |
| 282 | char* pw0 = NULL; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 283 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 284 | pw0 = getpass(prompt.c_str()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 285 | if (!pw0) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 286 | return false; |
| 287 | std::string password1 = pw0; |
| 288 | memset(pw0, 0, strlen(pw0)); |
| 289 | |
| 290 | pw0 = getpass("Confirm:"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 291 | if (!pw0) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 292 | { |
| 293 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 294 | memset(pw1, 0, password1.size()); |
| 295 | return false; |
| 296 | } |
| 297 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 298 | if (!password1.compare(pw0)) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 299 | { |
| 300 | result = true; |
| 301 | password.swap(password1); |
| 302 | } |
| 303 | |
| 304 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 305 | memset(pw1, 0, password1.size()); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 306 | memset(pw0, 0, strlen(pw0)); |
| 307 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 308 | if (password.empty()) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 309 | return false; |
| 310 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 311 | return result; |
| 312 | } |
| 313 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 314 | } // namespace ndn |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 315 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 316 | #endif //NDN_SECURITY_SEC_TPM_HPP |