blob: 15f17709f4ef1c3297d796886a2666e6ff8b2fd7 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson7b79eb62013-09-12 18:48:29 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson7b79eb62013-09-12 18:48:29 -07004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7b79eb62013-09-12 18:48:29 -07006 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_SEC_TPM_HPP
10#define NDN_SECURITY_SEC_TPM_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080013#include "security-common.hpp"
14#include "../name.hpp"
15#include "../data.hpp"
16#include "public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070017
18namespace ndn {
19
Yingdi Yufc40d872014-02-18 12:56:04 -080020/**
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 Yu31b4af22014-01-14 14:13:00 -080025class SecTpm {
Jeff Thompsona50703f2013-09-17 14:24:15 -070026public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027 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 Afanasyeve64788e2014-01-05 22:38:21 -080036
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080038 ~SecTpm() {}
Jeff Thompson7b79eb62013-09-12 18:48:29 -070039
Jeff Thompson7b79eb62013-09-12 18:48:29 -070040 /**
Yingdi Yube4150e2014-02-18 13:02:46 -080041 * @brief set password of TPM
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042 *
Yingdi Yube4150e2014-02-18 13:02:46 -080043 * Password is used to unlock TPM when it is locked.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070044 * You should be cautious when using this method, because remembering password is kind of
45 * dangerous.
Yingdi Yube4150e2014-02-18 13:02:46 -080046 *
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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 *
Yingdi Yube4150e2014-02-18 13:02:46 -080062 * 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 *
Yingdi Yube4150e2014-02-18 13:02:46 -080073 * @return inTerminal flag.
74 */
75 virtual bool
76 getInTerminal() = 0;
77
78 /**
79 * @brief check if TPM is locked.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 *
Yingdi Yube4150e2014-02-18 13:02:46 -080081 * @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 Yu2e57a582014-02-20 23:34:43 -080092 * @return true if TPM is unlocked, otherwise false.
Yingdi Yube4150e2014-02-18 13:02:46 -080093 */
Yingdi Yu2e57a582014-02-20 23:34:43 -080094 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080095 unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0;
96
97 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080098 * @brief Generate a pair of asymmetric keys.
99 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700100 * @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 Yufc40d872014-02-18 12:56:04 -0800103 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700104 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800106 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800108 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800109 * @brief Delete a key pair of asymmetric keys.
110 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800111 * @param keyName The name of the key pair.
112 */
113 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 deleteKeyPairInTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700115
116 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800117 * @brief Get a public key.
118 *
119 * @param keyName The public key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800120 * @return The public key.
121 * @throws SecTpm::Error if public key does not exist in TPM.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700122 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800124 getPublicKeyFromTpm(const Name& keyName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700126 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800127 * @brief Sign data.
128 *
129 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -0700130 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700131 * @param keyName The name of the signing key.
132 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -0800133 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -0800134 * @throws SecTpm::Error if signing fails.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800136 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700137 signInTpm(const uint8_t* data, size_t dataLength,
138 const Name& keyName,
139 DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700141 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800142 * @brief Decrypt data.
143 *
144 * @param data Pointer to the byte arry to be decrypted.
145 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700146 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800147 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800149 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700150 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800152 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700153
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700154 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800155 * @brief Encrypt data.
156 *
157 * @param data Pointer to the byte arry to be decrypted.
158 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700159 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800160 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700161 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800162 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700163 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800164 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800165 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700166
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700167 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700168 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800169 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170 * @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 Yu2e57a582014-02-20 23:34:43 -0800173 * @throws SecTpm::Error if key generating fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700174 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700175 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800176 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700177
178 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800179 * @brief Check if a particular key exists.
180 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700181 * @param keyName The name of the key.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700182 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700183 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700184 */
185 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700186 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800187
188 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800189 * @brief Generate a random block.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800191 * @param res The pointer to the generated block.
192 * @param size The random block size.
Yingdi Yu4b752752014-02-18 12:24:03 -0800193 * @return true for success, otherwise false.
194 */
195 virtual bool
196 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800197
198 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800199 * @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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700206 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -0800207 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
208
209 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800210 * @brief Export a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700211 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800212 * @param keyName The private key name.
213 * @param password The password to encrypt the private key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800214 * @return The private key info (in PKCS8 format) if exist.
215 * @throws SecTpm::Error if private key cannot be exported.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800216 */
217 ConstBufferPtr
Yingdi Yube4150e2014-02-18 13:02:46 -0800218 exportPrivateKeyPkcs8FromTpm(const Name& keyName, const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800219
220 /**
221 * @brief Import a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800223 * Also recover the public key and installed it in TPM.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800225 * @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 Yu8dceb1d2014-02-18 12:45:10 -0800228 * @return False if import fails.
229 */
230 bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700231 importPrivateKeyPkcs8IntoTpm(const Name& keyName,
232 const uint8_t* buf, size_t size,
233 const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800234
235protected:
236 /**
237 * @brief Export a private key in PKCS#1 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700238 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800239 * @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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700247 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800248 * @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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700257 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800258 * @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 Yube4150e2014-02-18 13:02:46 -0800267 * @brief Get import/export password.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800268 *
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 Yube4150e2014-02-18 13:02:46 -0800274 getImpExpPassWord(std::string& password, const std::string& prompt);
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700275};
276
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800277inline bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800278SecTpm::getImpExpPassWord(std::string& password, const std::string& prompt)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800279{
280 int result = false;
281
282 char* pw0 = NULL;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700283
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800284 pw0 = getpass(prompt.c_str());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700285 if (!pw0)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800286 return false;
287 std::string password1 = pw0;
288 memset(pw0, 0, strlen(pw0));
289
290 pw0 = getpass("Confirm:");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700291 if (!pw0)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800292 {
293 char* pw1 = const_cast<char*>(password1.c_str());
294 memset(pw1, 0, password1.size());
295 return false;
296 }
297
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700298 if (!password1.compare(pw0))
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800299 {
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 Yube4150e2014-02-18 13:02:46 -0800306 memset(pw0, 0, strlen(pw0));
307
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700308 if (password.empty())
Yingdi Yube4150e2014-02-18 13:02:46 -0800309 return false;
310
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800311 return result;
312}
313
Yingdi Yufc40d872014-02-18 12:56:04 -0800314} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700315
Yingdi Yufc40d872014-02-18 12:56:04 -0800316#endif //NDN_SECURITY_SEC_TPM_HPP