blob: 5acb0c34daa1dfc15934e8e5a0d4407f96cd7db7 [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 Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024#ifndef NDN_SECURITY_V1_SEC_TPM_HPP
25#define NDN_SECURITY_V1_SEC_TPM_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070026
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080027#include "../../common.hpp"
28#include "../security-common.hpp"
29#include "../../name.hpp"
30#include "../../data.hpp"
31#include "../key-params.hpp"
32#include "public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070033
34namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070035namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080036namespace v1 {
Jeff Thompson7b79eb62013-09-12 18:48:29 -070037
Yingdi Yufc40d872014-02-18 12:56:04 -080038/**
39 * @brief SecTpm is the base class of the TPM classes.
40 *
41 * It specifies the interfaces of private/secret key related operations.
42 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070043class SecTpm : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070044{
Jeff Thompsona50703f2013-09-17 14:24:15 -070045public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 class Error : public std::runtime_error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : std::runtime_error(what)
52 {
53 }
54 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080055
Yingdi Yu41546342014-11-30 23:37:53 -080056 explicit
57 SecTpm(const std::string& location);
58
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059 virtual
Yingdi Yu41546342014-11-30 23:37:53 -080060 ~SecTpm();
61
62 std::string
63 getTpmLocator();
Jeff Thompson7b79eb62013-09-12 18:48:29 -070064
Jeff Thompson7b79eb62013-09-12 18:48:29 -070065 /**
Yingdi Yube4150e2014-02-18 13:02:46 -080066 * @brief set password of TPM
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 *
Yingdi Yube4150e2014-02-18 13:02:46 -080068 * Password is used to unlock TPM when it is locked.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070069 * You should be cautious when using this method, because remembering password is kind of
70 * dangerous.
Yingdi Yube4150e2014-02-18 13:02:46 -080071 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070072 * @param password The password
73 * @param passwordLength The length of password
Yingdi Yube4150e2014-02-18 13:02:46 -080074 */
75 virtual void
76 setTpmPassword(const uint8_t* password, size_t passwordLength) = 0;
77
78 /**
79 * @brief reset password of TPM
80 */
81 virtual void
82 resetTpmPassword() = 0;
83
84 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +020085 * @brief Set inTerminal flag to @p inTerminal
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 *
Yingdi Yube4150e2014-02-18 13:02:46 -080087 * If the inTerminal flag is set, and password is not set, TPM may ask for password via terminal.
88 * inTerminal flag is set by default.
Yingdi Yube4150e2014-02-18 13:02:46 -080089 */
90 virtual void
91 setInTerminal(bool inTerminal) = 0;
92
93 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070094 * @brief Get value of inTerminal flag
Yingdi Yube4150e2014-02-18 13:02:46 -080095 */
96 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070097 getInTerminal() const = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -080098
99 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700100 * @brief Check if TPM is locked
Yingdi Yube4150e2014-02-18 13:02:46 -0800101 */
102 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700103 isLocked() = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -0800104
105 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700106 * @brief Unlock the TPM
Yingdi Yube4150e2014-02-18 13:02:46 -0800107 *
108 * @param password The password.
109 * @param passwordLength The password size. 0 indicates no password.
110 * @param usePassword True if we want to use the supplied password to unlock the TPM.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 * @return true if TPM is unlocked, otherwise false.
Yingdi Yube4150e2014-02-18 13:02:46 -0800112 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800113 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800114 unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0;
115
116 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800117 * @brief Generate a pair of asymmetric keys.
118 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700119 * @param keyName The name of the key pair.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700120 * @param params The parameters of key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800121 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700122 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700124 generateKeyPairInTpm(const Name& keyName, const KeyParams& params) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800126 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800127 * @brief Delete a key pair of asymmetric keys.
128 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800129 * @param keyName The name of the key pair.
130 */
131 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132 deleteKeyPairInTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700133
134 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800135 * @brief Get a public key.
136 *
137 * @param keyName The public key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 * @return The public key.
139 * @throws SecTpm::Error if public key does not exist in TPM.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700140 */
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700141 virtual shared_ptr<v1::PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800142 getPublicKeyFromTpm(const Name& keyName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700144 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800145 * @brief Sign data.
146 *
147 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -0700148 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700149 * @param keyName The name of the signing key.
150 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -0800151 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -0800152 * @throws SecTpm::Error if signing fails.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700153 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800154 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 signInTpm(const uint8_t* data, size_t dataLength,
156 const Name& keyName,
157 DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700159 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800160 * @brief Decrypt data.
161 *
162 * @param data Pointer to the byte arry to be decrypted.
163 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700164 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800165 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700166 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800167 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700168 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700169 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800170 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700171
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700172 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800173 * @brief Encrypt data.
174 *
175 * @param data Pointer to the byte arry to be decrypted.
176 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700177 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800178 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700179 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800180 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700181 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800182 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800183 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700184
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700185 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700186 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800187 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700188 * @param keyName The name of the key.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700189 * @param params The parameter of the key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800190 * @throws SecTpm::Error if key generating fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700191 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700193 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700194
195 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800196 * @brief Check if a particular key exists.
197 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700198 * @param keyName The name of the key.
Yingdi Yu99b2a002015-08-12 12:47:44 -0700199 * @param keyClass The class of the key, e.g. KeyClass::PUBLIC, KeyClass::PRIVATE.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700200 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700201 */
202 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700203 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800204
205 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700206 * @brief Generate a random block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700208 * @param res The pointer to the generated block
209 * @param size The random block size
210 * @return true for success, otherwise false
Yingdi Yu4b752752014-02-18 12:24:03 -0800211 */
212 virtual bool
213 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800214
215 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700216 * @brief Add the application into the ACL of a particular key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800217 *
218 * @param keyName the name of key
219 * @param keyClass the class of key, e.g. Private Key
220 * @param appPath the absolute path to the application
221 * @param acl the new acl of the key
222 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700224 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800225
226 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700227 * @brief Export a private key in PKCS#5 format
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700228 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700229 * @param keyName The private key name
230 * @param password The password to encrypt the private key
231 * @return The private key info (in PKCS8 format) if exist
232 * @throws SecTpm::Error if private key cannot be exported
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800233 */
234 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700235 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800236
237 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200238 * @brief Import a private key in PKCS#5 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700239 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800240 * Also recover the public key and installed it in TPM.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700241 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700242 * @param keyName The private key name
243 * @param buffer Pointer to the first byte of the buffer containing PKCS#5-encoded
244 * private key info
245 * @param bufferSize Size of the buffer
246 * @param password The password to encrypt the private key
247 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800248 */
249 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700250 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700251 const uint8_t* buffer, size_t bufferSize,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700252 const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800253
254protected:
Yingdi Yu41546342014-11-30 23:37:53 -0800255 virtual std::string
256 getScheme() = 0;
257
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800258 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700259 * @brief Export a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700260 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800261 * @param keyName The private key name.
Yingdi Yu5e96e002014-04-23 18:32:15 -0700262 * @return The private key info (in PKCS#8 format) if exist, otherwise a NULL pointer.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800263 */
264 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700265 exportPrivateKeyPkcs8FromTpm(const Name& keyName) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800266
267 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200268 * @brief Import a private key from PKCS#8 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700269 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700270 * @param keyName The private key name.
271 * @param buffer Pointer to the first byte of the buffer containing PKCS#8-encoded
272 * private key info
273 * @param bufferSize Size of the buffer
274 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800275 */
276 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700277 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800278
279 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200280 * @brief Import a public key in PKCS#1 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700281 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700282 * @param keyName The public key name
283 * @param buffer Pointer to the first byte of the buffer containing PKCS#1-encoded
284 * private key info
285 * @param bufferSize Size of the buffer
286 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800287 */
288 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700289 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800290
291 /**
Yingdi Yube4150e2014-02-18 13:02:46 -0800292 * @brief Get import/export password.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800293 *
294 * @param password On return, the password.
295 * @param prompt Prompt for password, i.e., "Password for key:"
296 * @return true if password has been obtained.
297 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700298 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800299 getImpExpPassWord(std::string& password, const std::string& prompt);
Yingdi Yu41546342014-11-30 23:37:53 -0800300
301protected:
302 std::string m_location;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700303};
304
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800305} // namespace v1
306
307#ifdef NDN_CXX_KEEP_SECURITY_V1_ALIASES
308using v1::SecTpm;
309#endif // NDN_CXX_KEEP_SECURITY_V1_ALIASES
310
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700311} // namespace security
312
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800313#ifdef NDN_CXX_KEEP_SECURITY_V1_ALIASES
314using security::v1::SecTpm;
315#endif // NDN_CXX_KEEP_SECURITY_V1_ALIASES
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700316
Yingdi Yufc40d872014-02-18 12:56:04 -0800317} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700318
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800319#endif // NDN_SECURITY_V1_SEC_TPM_HPP