blob: 3da278e76a24a8dc6ef4b3043b8df6c345aa77e0 [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/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 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"
Yingdi Yu7036ce22014-06-19 18:53:37 -070031#include "key-params.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070032#include "v1/public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070033
34namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070035namespace security {
Jeff Thompson7b79eb62013-09-12 18:48:29 -070036
Yingdi Yufc40d872014-02-18 12:56:04 -080037/**
38 * @brief SecTpm is the base class of the TPM classes.
39 *
40 * It specifies the interfaces of private/secret key related operations.
41 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070042class SecTpm : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070043{
Jeff Thompsona50703f2013-09-17 14:24:15 -070044public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 class Error : public std::runtime_error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : std::runtime_error(what)
51 {
52 }
53 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080054
Yingdi Yu41546342014-11-30 23:37:53 -080055 explicit
56 SecTpm(const std::string& location);
57
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 virtual
Yingdi Yu41546342014-11-30 23:37:53 -080059 ~SecTpm();
60
61 std::string
62 getTpmLocator();
Jeff Thompson7b79eb62013-09-12 18:48:29 -070063
Jeff Thompson7b79eb62013-09-12 18:48:29 -070064 /**
Yingdi Yube4150e2014-02-18 13:02:46 -080065 * @brief set password of TPM
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 *
Yingdi Yube4150e2014-02-18 13:02:46 -080067 * Password is used to unlock TPM when it is locked.
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070068 * You should be cautious when using this method, because remembering password is kind of
69 * dangerous.
Yingdi Yube4150e2014-02-18 13:02:46 -080070 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070071 * @param password The password
72 * @param passwordLength The length of password
Yingdi Yube4150e2014-02-18 13:02:46 -080073 */
74 virtual void
75 setTpmPassword(const uint8_t* password, size_t passwordLength) = 0;
76
77 /**
78 * @brief reset password of TPM
79 */
80 virtual void
81 resetTpmPassword() = 0;
82
83 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +020084 * @brief Set inTerminal flag to @p inTerminal
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085 *
Yingdi Yube4150e2014-02-18 13:02:46 -080086 * If the inTerminal flag is set, and password is not set, TPM may ask for password via terminal.
87 * inTerminal flag is set by default.
Yingdi Yube4150e2014-02-18 13:02:46 -080088 */
89 virtual void
90 setInTerminal(bool inTerminal) = 0;
91
92 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070093 * @brief Get value of inTerminal flag
Yingdi Yube4150e2014-02-18 13:02:46 -080094 */
95 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070096 getInTerminal() const = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -080097
98 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070099 * @brief Check if TPM is locked
Yingdi Yube4150e2014-02-18 13:02:46 -0800100 */
101 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700102 isLocked() = 0;
Yingdi Yube4150e2014-02-18 13:02:46 -0800103
104 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700105 * @brief Unlock the TPM
Yingdi Yube4150e2014-02-18 13:02:46 -0800106 *
107 * @param password The password.
108 * @param passwordLength The password size. 0 indicates no password.
109 * @param usePassword True if we want to use the supplied password to unlock the TPM.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800110 * @return true if TPM is unlocked, otherwise false.
Yingdi Yube4150e2014-02-18 13:02:46 -0800111 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800112 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800113 unlockTpm(const char* password, size_t passwordLength, bool usePassword) = 0;
114
115 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800116 * @brief Generate a pair of asymmetric keys.
117 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700118 * @param keyName The name of the key pair.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700119 * @param params The parameters of key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800120 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700121 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700123 generateKeyPairInTpm(const Name& keyName, const KeyParams& params) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800125 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800126 * @brief Delete a key pair of asymmetric keys.
127 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800128 * @param keyName The name of the key pair.
129 */
130 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131 deleteKeyPairInTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700132
133 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800134 * @brief Get a public key.
135 *
136 * @param keyName The public key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800137 * @return The public key.
138 * @throws SecTpm::Error if public key does not exist in TPM.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700139 */
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700140 virtual shared_ptr<v1::PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800141 getPublicKeyFromTpm(const Name& keyName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700143 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800144 * @brief Sign data.
145 *
146 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -0700147 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700148 * @param keyName The name of the signing key.
149 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -0800150 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -0800151 * @throws SecTpm::Error if signing fails.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800153 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700154 signInTpm(const uint8_t* data, size_t dataLength,
155 const Name& keyName,
156 DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700158 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800159 * @brief Decrypt data.
160 *
161 * @param data Pointer to the byte arry to be decrypted.
162 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700163 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800164 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700165 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800166 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700167 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800169 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700171 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800172 * @brief Encrypt data.
173 *
174 * @param data Pointer to the byte arry to be decrypted.
175 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700176 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800177 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700178 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -0800179 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700180 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800181 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800182 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700183
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700184 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700185 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800186 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700187 * @param keyName The name of the key.
Yingdi Yu7036ce22014-06-19 18:53:37 -0700188 * @param params The parameter of the key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189 * @throws SecTpm::Error if key generating fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700190 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700192 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700193
194 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800195 * @brief Check if a particular key exists.
196 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700197 * @param keyName The name of the key.
Yingdi Yu99b2a002015-08-12 12:47:44 -0700198 * @param keyClass The class of the key, e.g. KeyClass::PUBLIC, KeyClass::PRIVATE.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700199 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700200 */
201 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800203
204 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700205 * @brief Generate a random block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700206 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700207 * @param res The pointer to the generated block
208 * @param size The random block size
209 * @return true for success, otherwise false
Yingdi Yu4b752752014-02-18 12:24:03 -0800210 */
211 virtual bool
212 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800213
214 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700215 * @brief Add the application into the ACL of a particular key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800216 *
217 * @param keyName the name of key
218 * @param keyClass the class of key, e.g. Private Key
219 * @param appPath the absolute path to the application
220 * @param acl the new acl of the key
221 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700223 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800224
225 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700226 * @brief Export a private key in PKCS#5 format
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700227 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700228 * @param keyName The private key name
229 * @param password The password to encrypt the private key
230 * @return The private key info (in PKCS8 format) if exist
231 * @throws SecTpm::Error if private key cannot be exported
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800232 */
233 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700234 exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800235
236 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200237 * @brief Import a private key in PKCS#5 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700238 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800239 * Also recover the public key and installed it in TPM.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700240 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700241 * @param keyName The private key name
242 * @param buffer Pointer to the first byte of the buffer containing PKCS#5-encoded
243 * private key info
244 * @param bufferSize Size of the buffer
245 * @param password The password to encrypt the private key
246 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800247 */
248 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700249 importPrivateKeyPkcs5IntoTpm(const Name& keyName,
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700250 const uint8_t* buffer, size_t bufferSize,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700251 const std::string& password);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800252
253protected:
Yingdi Yu41546342014-11-30 23:37:53 -0800254 virtual std::string
255 getScheme() = 0;
256
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800257 /**
Yingdi Yu5e96e002014-04-23 18:32:15 -0700258 * @brief Export a private key in PKCS#8 format.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700259 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800260 * @param keyName The private key name.
Yingdi Yu5e96e002014-04-23 18:32:15 -0700261 * @return The private key info (in PKCS#8 format) if exist, otherwise a NULL pointer.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800262 */
263 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700264 exportPrivateKeyPkcs8FromTpm(const Name& keyName) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800265
266 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200267 * @brief Import a private key from PKCS#8 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700268 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700269 * @param keyName The private key name.
270 * @param buffer Pointer to the first byte of the buffer containing PKCS#8-encoded
271 * private key info
272 * @param bufferSize Size of the buffer
273 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800274 */
275 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700276 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800277
278 /**
Davide Pesavento18cf81b2015-09-12 23:36:43 +0200279 * @brief Import a public key in PKCS#1 formatted buffer of size @p bufferSize
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700280 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700281 * @param keyName The public key name
282 * @param buffer Pointer to the first byte of the buffer containing PKCS#1-encoded
283 * private key info
284 * @param bufferSize Size of the buffer
285 * @return false if import fails
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800286 */
287 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700288 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buffer, size_t bufferSize) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800289
290 /**
Yingdi Yube4150e2014-02-18 13:02:46 -0800291 * @brief Get import/export password.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800292 *
293 * @param password On return, the password.
294 * @param prompt Prompt for password, i.e., "Password for key:"
295 * @return true if password has been obtained.
296 */
Yingdi Yu7036ce22014-06-19 18:53:37 -0700297 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800298 getImpExpPassWord(std::string& password, const std::string& prompt);
Yingdi Yu41546342014-11-30 23:37:53 -0800299
300protected:
301 std::string m_location;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700302};
303
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700304} // namespace security
305
306using security::SecTpm;
307
Yingdi Yufc40d872014-02-18 12:56:04 -0800308} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700309
Yingdi Yu41546342014-11-30 23:37:53 -0800310#endif // NDN_SECURITY_SEC_TPM_HPP