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