blob: 5a163c60d5e80a7a19ad4f46062d694af469c65b [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson7b79eb62013-09-12 18:48:29 -070013 */
14
Yingdi Yufc40d872014-02-18 12:56:04 -080015#ifndef NDN_SECURITY_SEC_TPM_HPP
16#define NDN_SECURITY_SEC_TPM_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070017
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080018#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080019#include "security-common.hpp"
20#include "../name.hpp"
21#include "../data.hpp"
22#include "public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070023
24namespace ndn {
25
Yingdi Yufc40d872014-02-18 12:56:04 -080026/**
27 * @brief SecTpm is the base class of the TPM classes.
28 *
29 * It specifies the interfaces of private/secret key related operations.
30 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070031class SecTpm : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070032{
Jeff Thompsona50703f2013-09-17 14:24:15 -070033public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 class Error : public std::runtime_error
35 {
36 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080043
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 virtual
Yingdi Yuf56c68f2014-04-24 21:50:13 -070045 ~SecTpm()
46 {
47 }
Jeff Thompson7b79eb62013-09-12 18:48:29 -070048
Jeff Thompson7b79eb62013-09-12 18:48:29 -070049 /**
Yingdi Yube4150e2014-02-18 13:02:46 -080050 * @brief set password of TPM
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051 *
Yingdi Yube4150e2014-02-18 13:02:46 -080052 * Password is used to unlock TPM when it is locked.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070053 * You should be cautious when using this method, because remembering password is kind of
54 * dangerous.
Yingdi Yube4150e2014-02-18 13:02:46 -080055 *
56 * @param password The password.
57 * @param passwordLength The length of password.
58 */
59 virtual void
60 setTpmPassword(const uint8_t* password, size_t passwordLength) = 0;
61
62 /**
63 * @brief reset password of TPM
64 */
65 virtual void
66 resetTpmPassword() = 0;
67
68 /**
69 * @brief set inTerminal flag
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 *
Yingdi Yube4150e2014-02-18 13:02:46 -080071 * If the inTerminal flag is set, and password is not set, TPM may ask for password via terminal.
72 * inTerminal flag is set by default.
73 *
74 * @param inTerminal.
75 */
76 virtual void
77 setInTerminal(bool inTerminal) = 0;
78
79 /**
80 * @brief get inTerminal flag
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081 *
Yingdi Yube4150e2014-02-18 13:02:46 -080082 * @return inTerminal flag.
83 */
84 virtual bool
85 getInTerminal() = 0;
86
87 /**
88 * @brief check if TPM is locked.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 *
Yingdi Yube4150e2014-02-18 13:02:46 -080090 * @return true if locked, false otherwise
91 */
92 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070093 isLocked() = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -080094
95 /**
96 * @brief Unlock the TPM.
97 *
98 * @param password The password.
99 * @param passwordLength The password size. 0 indicates no password.
100 * @param usePassword True if we want to use the supplied password to unlock the TPM.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800101 * @return true if TPM is unlocked, otherwise false.
Yingdi Yube4150e2014-02-18 13:02:46 -0800102 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800103 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800104 unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0;
105
106 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800107 * @brief Generate a pair of asymmetric keys.
108 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700109 * @param keyName The name of the key pair.
110 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
111 * @param keySize The size of the key pair.
Yingdi Yufc40d872014-02-18 12:56:04 -0800112 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700113 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800115 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800117 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800118 * @brief Delete a key pair of asymmetric keys.
119 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800120 * @param keyName The name of the key pair.
121 */
122 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 deleteKeyPairInTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700124
125 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800126 * @brief Get a public key.
127 *
128 * @param keyName The public key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800129 * @return The public key.
130 * @throws SecTpm::Error if public key does not exist in TPM.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700131 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800133 getPublicKeyFromTpm(const Name& keyName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700135 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800136 * @brief Sign data.
137 *
138 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -0700139 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700140 * @param keyName The name of the signing key.
141 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -0800142 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -0800143 * @throws SecTpm::Error if signing fails.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700144 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800145 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700146 signInTpm(const uint8_t* data, size_t dataLength,
147 const Name& keyName,
148 DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700150 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800151 * @brief Decrypt data.
152 *
153 * @param data Pointer to the byte arry to be decrypted.
154 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700155 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800156 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700157 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800158 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700159 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800161 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700162
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700163 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800164 * @brief Encrypt data.
165 *
166 * @param data Pointer to the byte arry to be decrypted.
167 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700168 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800169 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800171 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700172 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800173 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800174 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700175
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700176 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700177 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800178 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700179 * @param keyName The name of the key.
180 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
181 * @param keySize The size of the key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800182 * @throws SecTpm::Error if key generating fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700183 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700184 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800185 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700186
187 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800188 * @brief Check if a particular key exists.
189 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700190 * @param keyName The name of the key.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700191 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700192 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700193 */
194 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700195 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800196
197 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800198 * @brief Generate a random block.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800200 * @param res The pointer to the generated block.
201 * @param size The random block size.
Yingdi Yu4b752752014-02-18 12:24:03 -0800202 * @return true for success, otherwise false.
203 */
204 virtual bool
205 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800206
207 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800208 * @brief Add the application into the ACL of a particular key.
209 *
210 * @param keyName the name of key
211 * @param keyClass the class of key, e.g. Private Key
212 * @param appPath the absolute path to the application
213 * @param acl the new acl of the key
214 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700215 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700216 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800217
218 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700219 * @brief Export a private key in PKCS#5 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800221 * @param keyName The private key name.
222 * @param password The password to encrypt the private key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800223 * @return The private key info (in PKCS8 format) if exist.
224 * @throws SecTpm::Error if private key cannot be exported.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800225 */
226 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700227 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800228
229 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700230 * @brief Import a private key in PKCS#5 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700231 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800232 * Also recover the public key and installed it in TPM.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700233 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800234 * @param keyName The private key name.
235 * @param key The encoded private key info.
236 * @param password The password to encrypt the private key.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800237 * @return False if import fails.
238 */
239 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700240 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700241 const uint8_t* buf, size_t size,
242 const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800243
244protected:
245 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700246 * @brief Export a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700247 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800248 * @param keyName The private key name.
Yingdi Yu5e96e002014-04-23 18:32:15 -0700249 * @return The private key info (in PKCS#8 format) if exist, otherwise a NULL pointer.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800250 */
251 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700252 exportPrivateKeyPkcs8FromTpm(const Name& keyName) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800253
254 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700255 * @brief Import a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700256 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800257 * @param keyName The private key name.
258 * @param key The encoded private key info.
259 * @return False if import fails.
260 */
261 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700262 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800263
264 /**
265 * @brief Import a public key in PKCS#1 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700266 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800267 * @param keyName The public key name.
268 * @param key The encoded public key info.
269 * @return False if import fails.
270 */
271 virtual bool
272 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
273
274
275 /**
Yingdi Yube4150e2014-02-18 13:02:46 -0800276 * @brief Get import/export password.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800277 *
278 * @param password On return, the password.
279 * @param prompt Prompt for password, i.e., "Password for key:"
280 * @return true if password has been obtained.
281 */
282 inline virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800283 getImpExpPassWord(std::string& password, const std::string& prompt);
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700284};
285
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800286inline bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800287SecTpm::getImpExpPassWord(std::string& password, const std::string& prompt)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800288{
289 int result = false;
290
291 char* pw0 = NULL;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700292
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800293 pw0 = getpass(prompt.c_str());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700294 if (!pw0)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800295 return false;
296 std::string password1 = pw0;
297 memset(pw0, 0, strlen(pw0));
298
299 pw0 = getpass("Confirm:");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700300 if (!pw0)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800301 {
302 char* pw1 = const_cast<char*>(password1.c_str());
303 memset(pw1, 0, password1.size());
304 return false;
305 }
306
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700307 if (!password1.compare(pw0))
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800308 {
309 result = true;
310 password.swap(password1);
311 }
312
313 char* pw1 = const_cast<char*>(password1.c_str());
314 memset(pw1, 0, password1.size());
Yingdi Yube4150e2014-02-18 13:02:46 -0800315 memset(pw0, 0, strlen(pw0));
316
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700317 if (password.empty())
Yingdi Yube4150e2014-02-18 13:02:46 -0800318 return false;
319
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800320 return result;
321}
322
Yingdi Yufc40d872014-02-18 12:56:04 -0800323} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700324
Yingdi Yufc40d872014-02-18 12:56:04 -0800325#endif //NDN_SECURITY_SEC_TPM_HPP