blob: 031aa2b199ac38b409db3f8cdc20f372053f7dde [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson7b79eb62013-09-12 18:48:29 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson7b79eb62013-09-12 18:48:29 -070022 */
23
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_SEC_TPM_HPP
25#define NDN_SECURITY_SEC_TPM_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070026
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080027#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080028#include "security-common.hpp"
29#include "../name.hpp"
30#include "../data.hpp"
31#include "public-key.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070032#include "key-params.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070033
34namespace ndn {
35
Yingdi Yufc40d872014-02-18 12:56:04 -080036/**
37 * @brief SecTpm is the base class of the TPM classes.
38 *
39 * It specifies the interfaces of private/secret key related operations.
40 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070041class SecTpm : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070042{
Jeff Thompsona50703f2013-09-17 14:24:15 -070043public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : std::runtime_error(what)
50 {
51 }
52 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080053
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 virtual
Yingdi Yuf56c68f2014-04-24 21:50:13 -070055 ~SecTpm()
56 {
57 }
Jeff Thompson7b79eb62013-09-12 18:48:29 -070058
Jeff Thompson7b79eb62013-09-12 18:48:29 -070059 /**
Yingdi Yube4150e2014-02-18 13:02:46 -080060 * @brief set password of TPM
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 *
Yingdi Yube4150e2014-02-18 13:02:46 -080062 * Password is used to unlock TPM when it is locked.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 * You should be cautious when using this method, because remembering password is kind of
64 * dangerous.
Yingdi Yube4150e2014-02-18 13:02:46 -080065 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070066 * @param password The password
67 * @param passwordLength The length of password
Yingdi Yube4150e2014-02-18 13:02:46 -080068 */
69 virtual void
70 setTpmPassword(const uint8_t* password, size_t passwordLength) = 0;
71
72 /**
73 * @brief reset password of TPM
74 */
75 virtual void
76 resetTpmPassword() = 0;
77
78 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070079 * @brief Set inTerminal flag to @param inTerminal
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 *
Yingdi Yube4150e2014-02-18 13:02:46 -080081 * If the inTerminal flag is set, and password is not set, TPM may ask for password via terminal.
82 * inTerminal flag is set by default.
Yingdi Yube4150e2014-02-18 13:02:46 -080083 */
84 virtual void
85 setInTerminal(bool inTerminal) = 0;
86
87 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070088 * @brief Get value of inTerminal flag
Yingdi Yube4150e2014-02-18 13:02:46 -080089 */
90 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070091 getInTerminal() const = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -080092
93 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070094 * @brief Check if TPM is locked
Yingdi Yube4150e2014-02-18 13:02:46 -080095 */
96 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070097 isLocked() = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -080098
99 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700100 * @brief Unlock the TPM
Yingdi Yube4150e2014-02-18 13:02:46 -0800101 *
102 * @param password The password.
103 * @param passwordLength The password size. 0 indicates no password.
104 * @param usePassword True if we want to use the supplied password to unlock the TPM.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800105 * @return true if TPM is unlocked, otherwise false.
Yingdi Yube4150e2014-02-18 13:02:46 -0800106 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800107 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800108 unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0;
109
110 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800111 * @brief Generate a pair of asymmetric keys.
112 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113 * @param keyName The name of the key pair.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700114 * @param params The parameters of key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800115 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700116 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700118 generateKeyPairInTpm(const Name& keyName, const KeyParams& params) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800120 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800121 * @brief Delete a key pair of asymmetric keys.
122 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800123 * @param keyName The name of the key pair.
124 */
125 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126 deleteKeyPairInTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700127
128 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800129 * @brief Get a public key.
130 *
131 * @param keyName The public key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800132 * @return The public key.
133 * @throws SecTpm::Error if public key does not exist in TPM.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700134 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800136 getPublicKeyFromTpm(const Name& keyName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700138 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800139 * @brief Sign data.
140 *
141 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -0700142 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700143 * @param keyName The name of the signing key.
144 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -0800145 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -0800146 * @throws SecTpm::Error if signing fails.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800148 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 signInTpm(const uint8_t* data, size_t dataLength,
150 const Name& keyName,
151 DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700153 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800154 * @brief Decrypt data.
155 *
156 * @param data Pointer to the byte arry to be decrypted.
157 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700158 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800159 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700160 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800161 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700162 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700163 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800164 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700165
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700166 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800167 * @brief Encrypt data.
168 *
169 * @param data Pointer to the byte arry to be decrypted.
170 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700171 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800172 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700173 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800174 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700175 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800176 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800177 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700178
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700179 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700180 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800181 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700182 * @param keyName The name of the key.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700183 * @param params The parameter of the key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800184 * @throws SecTpm::Error if key generating fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700185 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700186 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700187 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700188
189 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800190 * @brief Check if a particular key exists.
191 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700192 * @param keyName The name of the key.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700193 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700194 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700195 */
196 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800198
199 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700200 * @brief Generate a random block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700202 * @param res The pointer to the generated block
203 * @param size The random block size
204 * @return true for success, otherwise false
Yingdi Yu4b752752014-02-18 12:24:03 -0800205 */
206 virtual bool
207 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800208
209 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700210 * @brief Add the application into the ACL of a particular key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800211 *
212 * @param keyName the name of key
213 * @param keyClass the class of key, e.g. Private Key
214 * @param appPath the absolute path to the application
215 * @param acl the new acl of the key
216 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700217 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700218 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800219
220 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700221 * @brief Export a private key in PKCS#5 format
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700223 * @param keyName The private key name
224 * @param password The password to encrypt the private key
225 * @return The private key info (in PKCS8 format) if exist
226 * @throws SecTpm::Error if private key cannot be exported
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800227 */
228 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700229 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800230
231 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700232 * @brief Import a private key in PKCS#5 formatted @param buffer of size @param bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700233 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800234 * Also recover the public key and installed it in TPM.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700235 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700236 * @param keyName The private key name
237 * @param buffer Pointer to the first byte of the buffer containing PKCS#5-encoded
238 * private key info
239 * @param bufferSize Size of the buffer
240 * @param password The password to encrypt the private key
241 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800242 */
243 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700244 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700245 const uint8_t* buffer, size_t bufferSize,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700246 const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800247
248protected:
249 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700250 * @brief Export a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700251 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800252 * @param keyName The private key name.
Yingdi Yu5e96e002014-04-23 18:32:15 -0700253 * @return The private key info (in PKCS#8 format) if exist, otherwise a NULL pointer.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800254 */
255 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700256 exportPrivateKeyPkcs8FromTpm(const Name& keyName) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800257
258 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700259 * @brief Import a private key from PKCS#8 formatted @param buffer of size @param bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700260 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700261 * @param keyName The private key name.
262 * @param buffer Pointer to the first byte of the buffer containing PKCS#8-encoded
263 * private key info
264 * @param bufferSize Size of the buffer
265 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800266 */
267 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700268 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800269
270 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700271 * @brief Import a public key in PKCS#1 formatted @param buffer of size @param bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700272 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700273 * @param keyName The public key name
274 * @param buffer Pointer to the first byte of the buffer containing PKCS#1-encoded
275 * private key info
276 * @param bufferSize Size of the buffer
277 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800278 */
279 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700280 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800281
282 /**
Yingdi Yube4150e2014-02-18 13:02:46 -0800283 * @brief Get import/export password.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800284 *
285 * @param password On return, the password.
286 * @param prompt Prompt for password, i.e., "Password for key:"
287 * @return true if password has been obtained.
288 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700289 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800290 getImpExpPassWord(std::string& password, const std::string& prompt);
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700291};
292
Yingdi Yufc40d872014-02-18 12:56:04 -0800293} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700294
Yingdi Yufc40d872014-02-18 12:56:04 -0800295#endif //NDN_SECURITY_SEC_TPM_HPP