blob: 4ce57f25ba5f878deccb3fd8842f12afe85af0dd [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento82d6a4c2017-12-23 19:47:20 -05002/*
Yingdi Yufe4733a2015-10-22 14:24:12 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#ifndef NDN_SECURITY_TPM_TPM_HPP
23#define NDN_SECURITY_TPM_TPM_HPP
24
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070025#include "key-handle.hpp"
Davide Pesavento82d6a4c2017-12-23 19:47:20 -050026#include "../key-params.hpp"
27#include "../../name.hpp"
Davide Pesavento794f6872017-05-15 23:33:38 -040028
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070029#include <unordered_map>
30
31namespace ndn {
32namespace security {
Yingdi Yufe4733a2015-10-22 14:24:12 -070033
34namespace v2 {
35class KeyChain;
36} // namespace v2
37
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070038namespace tpm {
39
40class BackEnd;
41
42/**
43 * @brief represents the front-end of TPM
44 *
45 * The TPM (Trusted Platform Module) stores the private portion of a user's cryptography keys.
46 * The format and location of stored information is indicated by the TpmLocator.
47 * The TPM is designed to work with a PIB (Public Information Base) which stores public keys and
48 * related information such as certificate.
49 *
50 * The TPM also provides functionalities of crypto transformation, such as signing and decryption.
51 *
52 * A TPM consists of a unified front-end interface and a back-end implementation. The front-end
53 * cache the handles of private keys which is provided by the back-end implementation.
54 *
Yingdi Yufe4733a2015-10-22 14:24:12 -070055 * @note Tpm instance is created and managed only by v2::KeyChain. v2::KeyChain::getTpm()
56 * returns a const reference to the managed Tpm instance, through which it is possible to
57 * check existence of private keys, get public keys for the private keys, sign, and decrypt
58 * the supplied buffers using managed private keys.
59 *
60 * @throw BackEnd::Error Failure with the underlying implementation having non-semantic errors
61 * @throw Tpm::Error Failure with semantic error in the underlying implementation
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070062 */
63class Tpm : noncopyable
64{
65public:
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070066 class Error : public std::runtime_error
67 {
68 public:
69 explicit
70 Error(const std::string& what)
71 : std::runtime_error(what)
72 {
73 }
74 };
75
76public:
77 ~Tpm();
78
79 std::string
80 getTpmLocator() const;
81
82 /**
Davide Pesavento92856862017-05-15 21:35:08 -040083 * @brief Check if a private key exists.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070084 *
85 * @param keyName The key name
86 * @return true if the key exists
87 */
88 bool
89 hasKey(const Name& keyName) const;
90
91 /**
Davide Pesavento92856862017-05-15 21:35:08 -040092 * @return The public portion of an asymmetric key with name @p keyName,
93 * or nullptr if the key does not exist,
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070094 *
Davide Pesavento92856862017-05-15 21:35:08 -040095 * The public key is in PKCS#8 format.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070096 */
97 ConstBufferPtr
98 getPublicKey(const Name& keyName) const;
99
100 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400101 * @brief Sign blob using the key with name @p keyName and using the digest @p digestAlgorithm.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700102 *
Davide Pesavento92856862017-05-15 21:35:08 -0400103 * @return The signature, or nullptr if the key does not exist.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700104 */
105 ConstBufferPtr
106 sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
107
108 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400109 * @brief Decrypt blob using the key with name @p keyName.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700110 *
Davide Pesavento92856862017-05-15 21:35:08 -0400111 * @return The decrypted data, or nullptr if the key does not exist.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700112 */
113 ConstBufferPtr
114 decrypt(const uint8_t* buf, size_t size, const Name& keyName) const;
115
Yingdi Yufe4733a2015-10-22 14:24:12 -0700116public: // Management
117 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400118 * @brief Check if the TPM is in terminal mode.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700119 */
120 bool
121 isTerminalMode() const;
122
123 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400124 * @brief Set the terminal mode of the TPM.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700125 *
Davide Pesavento92856862017-05-15 21:35:08 -0400126 * When in terminal mode, the TPM will not ask user permission from GUI.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700127 */
128 void
129 setTerminalMode(bool isTerminal) const;
130
131 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400132 * @return true if the TPM is locked, otherwise false.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700133 */
134 bool
135 isTpmLocked() const;
136
137 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400138 * @brief Unlock the TPM.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700139 *
Davide Pesavento92856862017-05-15 21:35:08 -0400140 * @param password The password to unlock the TPM.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700141 * @param passwordLength The password size.
142 */
143 bool
144 unlockTpm(const char* password, size_t passwordLength) const;
145
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700146NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
147 /*
Davide Pesavento92856862017-05-15 21:35:08 -0400148 * @brief Create a new TPM instance with the specified @p location.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700149 *
150 * @param scheme The scheme for the TPM
151 * @param location The location for the TPM
152 * @param impl The back-end implementation
153 */
154 Tpm(const std::string& scheme, const std::string& location, unique_ptr<BackEnd> impl);
155
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700156 /**
157 * @brief Create key for @p identityName according to @p params.
158 *
159 * The created key is named as: /<identityName>/[keyId]/KEY
160 *
Davide Pesavento92856862017-05-15 21:35:08 -0400161 * @return The key name.
162 * @throw Tpm::Error the key already exists or @p params is invalid.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700163 */
164 Name
165 createKey(const Name& identityName, const KeyParams& params);
166
167 /**
168 * @brief Delete a key pair with name @p keyName.
169 */
170 void
171 deleteKey(const Name& keyName);
172
173 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400174 * @brief Export a private key.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700175 *
Davide Pesavento92856862017-05-15 21:35:08 -0400176 * Export a private key in encrypted PKCS #8 format.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700177 *
Davide Pesavento92856862017-05-15 21:35:08 -0400178 * @param keyName The private key name
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700179 * @param pw The password to encrypt the private key
180 * @param pwLen The length of the password
Davide Pesavento92856862017-05-15 21:35:08 -0400181 * @return The encoded private key wrapper.
Davide Pesavento82d6a4c2017-12-23 19:47:20 -0500182 * @throw BackEnd::Error The key does not exist or it could not be exported.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700183 */
184 ConstBufferPtr
Davide Pesavento794f6872017-05-15 23:33:38 -0400185 exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700186
187 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400188 * @brief Import a private key.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700189 *
190 * @param keyName The private key name
191 * @param pkcs8 The private key wrapper
192 * @param pkcs8Len The length of the private key wrapper
193 * @param pw The password to encrypt the private key
194 * @param pwLen The length of the password
Davide Pesavento82d6a4c2017-12-23 19:47:20 -0500195 * @throw BackEnd::Error The key could not be imported.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700196 */
Davide Pesavento82d6a4c2017-12-23 19:47:20 -0500197 void
Davide Pesavento92856862017-05-15 21:35:08 -0400198 importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len,
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700199 const char* pw, size_t pwLen);
200
201 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400202 * @brief Clear the key cache.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700203 *
Davide Pesavento82d6a4c2017-12-23 19:47:20 -0500204 * An empty cache can force Tpm to do key lookup in the back-end.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700205 */
206 void
207 clearKeyCache()
208 {
209 m_keys.clear();
210 }
211
212private:
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700213 /**
Davide Pesavento92856862017-05-15 21:35:08 -0400214 * @brief Internal KeyHandle lookup.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700215 *
216 * @return A pointer to the handle of key @p keyName if it exists, otherwise nullptr.
217 */
218 const KeyHandle*
219 findKey(const Name& keyName) const;
220
221private:
222 std::string m_scheme;
223 std::string m_location;
224
225 mutable std::unordered_map<Name, unique_ptr<KeyHandle>> m_keys;
226
Davide Pesavento794f6872017-05-15 23:33:38 -0400227 const unique_ptr<BackEnd> m_backEnd;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700228
229 friend class v2::KeyChain;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700230};
231
232} // namespace tpm
233
234using tpm::Tpm;
235
236} // namespace security
237} // namespace ndn
238
239#endif // NDN_SECURITY_TPM_TPM_HPP