blob: 8bbfac521958749bc4e505b4e0deb641034804e8 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi68b53852018-07-25 13:56:38 -06002/*
3 * Copyright (c) 2013-2018 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_BACK_END_HPP
23#define NDN_SECURITY_TPM_BACK_END_HPP
24
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070025#include "../key-params.hpp"
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040026#include "../../encoding/buffer.hpp"
27#include "../../name.hpp"
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070028
29namespace ndn {
30namespace security {
31namespace tpm {
32
33class KeyHandle;
34
35/**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040036 * @brief Abstract interface for a TPM backend implementation.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070037 *
38 * This class provides KeyHandle to the front-end and other TPM management operations.
39 */
40class BackEnd : noncopyable
41{
42public:
43 class Error : public std::runtime_error
44 {
45 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060046 using std::runtime_error::runtime_error;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070047 };
48
49public:
50 virtual
51 ~BackEnd();
52
53public: // key management
54 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040055 * @brief Check if the key with name @p keyName exists in the TPM.
56 *
57 * @return True if the key exists, false otherwise.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070058 */
59 bool
60 hasKey(const Name& keyName) const;
61
62 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040063 * @brief Get the handle of the key with name @p keyName.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070064 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040065 * Calling this function multiple times with the same @p keyName will return different KeyHandle
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070066 * objects that all refer to the same key.
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040067 *
68 * @return The handle of the key, or nullptr if the key does not exist.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070069 */
70 unique_ptr<KeyHandle>
71 getKeyHandle(const Name& keyName) const;
72
73 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040074 * @brief Create a key for @p identityName according to @p params.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070075 *
76 * @return The handle of the created key.
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040077 * @throw Tpm::Error @p params are invalid.
78 * @throw Error The key could not be created.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070079 */
80 unique_ptr<KeyHandle>
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040081 createKey(const Name& identityName, const KeyParams& params);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070082
83 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040084 * @brief Delete the key with name @p keyName.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070085 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040086 * @warning Continuing to use existing KeyHandle objects for a deleted key
87 * results in undefined behavior.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070088 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040089 * @throw Error The key could not be deleted.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070090 */
91 void
92 deleteKey(const Name& keyName);
93
94 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -040095 * @brief Get the private key with name @p keyName in encrypted PKCS #8 format.
96 *
97 * @param keyName The name of the key.
98 * @param pw The password to encrypt the private key.
99 * @param pwLen The length of the password.
100 *
101 * @return The encoded private key.
102 * @throw Error The key does not exist or cannot be exported.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700103 */
104 ConstBufferPtr
105 exportKey(const Name& keyName, const char* pw, size_t pwLen);
106
107 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400108 * @brief Import a private key in encrypted PKCS #8 format.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700109 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400110 * @param keyName The name of the key to use in the TPM.
111 * @param pkcs8 Pointer to the key in encrypted PKCS #8 format.
112 * @param pkcs8Len The size of the key in encrypted PKCS #8 format.
113 * @param pw The password to decrypt the private key.
114 * @param pwLen The length of the password.
115 *
116 * @throw Error The key could not be imported.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700117 */
118 void
119 importKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen);
120
Yingdi Yufe4733a2015-10-22 14:24:12 -0700121 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400122 * @brief Check if the TPM is in terminal mode.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700123 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400124 * The default implementation always returns true.
125 *
126 * @return True if in terminal mode, false otherwise.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700127 */
128 virtual bool
129 isTerminalMode() const;
130
131 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400132 * @brief Set the terminal mode of the TPM.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700133 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400134 * In terminal mode, the TPM will not ask for a password from the GUI.
135 * The default implementation does nothing.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700136 */
137 virtual void
138 setTerminalMode(bool isTerminal) const;
139
140 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400141 * @brief Check if the TPM is locked.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700142 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400143 * The default implementation always returns false.
144 *
145 * @return True if locked, false otherwise.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700146 */
147 virtual bool
148 isTpmLocked() const;
149
150 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400151 * @brief Unlock the TPM.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700152 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400153 * The default implementation does nothing and returns `!isTpmLocked()`.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700154 *
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400155 * @param pw The password to unlock the TPM.
156 * @param pwLen The length of the password.
157 *
158 * @return True if the TPM was unlocked.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700159 */
160 virtual bool
161 unlockTpm(const char* pw, size_t pwLen) const;
162
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400163protected: // static helper methods
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700164 /**
Davide Pesavento5ee8ec02018-09-01 19:06:12 -0400165 * @brief Set the key name in @p keyHandle according to @p identity and @p params.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700166 */
167 static void
168 setKeyName(KeyHandle& keyHandle, const Name& identity, const KeyParams& params);
169
170private: // pure virtual methods
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700171 virtual bool
172 doHasKey(const Name& keyName) const = 0;
173
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700174 virtual unique_ptr<KeyHandle>
175 doGetKeyHandle(const Name& keyName) const = 0;
176
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700177 virtual unique_ptr<KeyHandle>
178 doCreateKey(const Name& identity, const KeyParams& params) = 0;
179
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700180 virtual void
181 doDeleteKey(const Name& keyName) = 0;
182
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700183 virtual ConstBufferPtr
184 doExportKey(const Name& keyName, const char* pw, size_t pwLen) = 0;
185
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700186 virtual void
187 doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) = 0;
188};
189
190} // namespace tpm
191} // namespace security
192} // namespace ndn
193
194#endif // NDN_SECURITY_TPM_BACK_END_HPP