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 | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 27 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 28 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 29 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 30 | * The virtual destructor. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 31 | */ |
| 32 | virtual |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 33 | ~SecTpm() {} |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 35 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 36 | * @brief Generate a pair of asymmetric keys. |
| 37 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 38 | * @param keyName The name of the key pair. |
| 39 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 40 | * @param keySize The size of the key pair. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 41 | * @throws SecTpm::Error if fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 42 | */ |
| 43 | virtual void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 44 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 47 | * @brief Delete a key pair of asymmetric keys. |
| 48 | * |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 49 | * @param keyName The name of the key pair. |
| 50 | */ |
| 51 | virtual void |
| 52 | deleteKeyPairInTpm(const Name &keyName) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 55 | * @brief Get a public key. |
| 56 | * |
| 57 | * @param keyName The public key name. |
| 58 | * @return The public key if exists, otherwise a NULL pointer. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 59 | */ |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 60 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 61 | getPublicKeyFromTpm(const Name& keyName) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 64 | * @brief Sign data. |
| 65 | * |
| 66 | * @param data Pointer to the byte array to be signed. |
Jeff Thompson | 4c11b9f | 2013-09-13 11:05:28 -0700 | [diff] [blame] | 67 | * @param dataLength The length of data. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 68 | * @param keyName The name of the signing key. |
| 69 | * @param digestAlgorithm the digest algorithm. |
Yingdi Yu | 3c5887c | 2014-01-21 18:19:49 -0800 | [diff] [blame] | 70 | * @return The signature block. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 71 | * @throws SecTpm::Error if signing fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 72 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 73 | virtual Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 74 | signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm) = 0; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 75 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 76 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 77 | * @brief Decrypt data. |
| 78 | * |
| 79 | * @param data Pointer to the byte arry to be decrypted. |
| 80 | * @param dataLength The length of data. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 81 | * @param keyName The name of the decrypting key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 82 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 83 | * @return The decrypted data. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 84 | * @throws SecTpm::Error if decryption fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 86 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 87 | 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] | 88 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 89 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 90 | * @brief Encrypt data. |
| 91 | * |
| 92 | * @param data Pointer to the byte arry to be decrypted. |
| 93 | * @param dataLength The length of data. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 94 | * @param keyName The name of the encrypting key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 95 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 96 | * @return The encrypted data. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 97 | * @throws SecTpm::Error if encryption fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 98 | */ |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 99 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 100 | 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] | 101 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 102 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 103 | * @brief Generate a symmetric key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 104 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | * @param keyName The name of the key. |
| 106 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 107 | * @param keySize The size of the key. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 108 | */ |
| 109 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 110 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 113 | * @brief Check if a particular key exists. |
| 114 | * |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 115 | * @param keyName The name of the key. |
| 116 | * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC. |
| 117 | * @return True if the key exists, otherwise false. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 118 | */ |
| 119 | virtual bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 120 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 121 | |
| 122 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 123 | * @brief Generate a random block. |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 124 | * |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 125 | * @param res The pointer to the generated block. |
| 126 | * @param size The random block size. |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 127 | * @return true for success, otherwise false. |
| 128 | */ |
| 129 | virtual bool |
| 130 | generateRandomBlock(uint8_t* res, size_t size) = 0; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Export a private key in PKCS#8 format. |
| 134 | * |
| 135 | * @param keyName The private key name. |
| 136 | * @param password The password to encrypt the private key. |
| 137 | * @param inTerminal If password is not supplied, get it via terminal if inTerminal is true, otherwise fail. |
| 138 | * @return The private key info (in PKCS8 format) if exist, otherwise a NULL pointer. |
| 139 | */ |
| 140 | ConstBufferPtr |
| 141 | exportPrivateKeyPkcs8FromTpm(const Name& keyName, bool inTerminal, const std::string& password); |
| 142 | |
| 143 | /** |
| 144 | * @brief Import a private key in PKCS#8 format. |
| 145 | * |
| 146 | * Also recover the public key and installed it in TPM. |
| 147 | * |
| 148 | * @param keyName The private key name. |
| 149 | * @param key The encoded private key info. |
| 150 | * @param password The password to encrypt the private key. |
| 151 | * @param inTerminal If password is not supplied, get it via terminal if inTerminal is true, otherwise fail. |
| 152 | * @return False if import fails. |
| 153 | */ |
| 154 | bool |
| 155 | importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size, bool inTerminal, const std::string& password); |
| 156 | |
| 157 | protected: |
| 158 | /** |
| 159 | * @brief Export a private key in PKCS#1 format. |
| 160 | * |
| 161 | * @param keyName The private key name. |
| 162 | * @return The private key info (in PKCS#1 format) if exist, otherwise a NULL pointer. |
| 163 | */ |
| 164 | virtual ConstBufferPtr |
| 165 | exportPrivateKeyPkcs1FromTpm(const Name& keyName) = 0; |
| 166 | |
| 167 | /** |
| 168 | * @brief Import a private key in PKCS#1 format. |
| 169 | * |
| 170 | * @param keyName The private key name. |
| 171 | * @param key The encoded private key info. |
| 172 | * @return False if import fails. |
| 173 | */ |
| 174 | virtual bool |
| 175 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0; |
| 176 | |
| 177 | /** |
| 178 | * @brief Import a public key in PKCS#1 format. |
| 179 | * |
| 180 | * @param keyName The public key name. |
| 181 | * @param key The encoded public key info. |
| 182 | * @return False if import fails. |
| 183 | */ |
| 184 | virtual bool |
| 185 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0; |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * @brief Get password. |
| 190 | * |
| 191 | * @param password On return, the password. |
| 192 | * @param prompt Prompt for password, i.e., "Password for key:" |
| 193 | * @return true if password has been obtained. |
| 194 | */ |
| 195 | inline virtual bool |
| 196 | getPassWord(std::string& password, const std::string& prompt); |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 199 | bool |
| 200 | SecTpm::getPassWord(std::string& password, const std::string& prompt) |
| 201 | { |
| 202 | int result = false; |
| 203 | |
| 204 | char* pw0 = NULL; |
| 205 | |
| 206 | pw0 = getpass(prompt.c_str()); |
| 207 | if(!pw0) |
| 208 | return false; |
| 209 | std::string password1 = pw0; |
| 210 | memset(pw0, 0, strlen(pw0)); |
| 211 | |
| 212 | pw0 = getpass("Confirm:"); |
| 213 | if(!pw0) |
| 214 | { |
| 215 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 216 | memset(pw1, 0, password1.size()); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | if(!password1.compare(pw0)) |
| 221 | { |
| 222 | result = true; |
| 223 | password.swap(password1); |
| 224 | } |
| 225 | |
| 226 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 227 | memset(pw1, 0, password1.size()); |
| 228 | memset(pw0, 0, strlen(pw0)); |
| 229 | return result; |
| 230 | } |
| 231 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 232 | } // namespace ndn |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 233 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 234 | #endif //NDN_SECURITY_SEC_TPM_HPP |