blob: 01b22ae529baa9e2b7dd6a31bc417b9f762dafd6 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
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
25#include "../../common.hpp"
26#include "../security-common.hpp"
27#include "../../name.hpp"
28#include "../key-params.hpp"
29#include "key-handle.hpp"
30#include <unordered_map>
31
32namespace ndn {
33namespace security {
34namespace tpm {
35
36class BackEnd;
37
38/**
39 * @brief represents the front-end of TPM
40 *
41 * The TPM (Trusted Platform Module) stores the private portion of a user's cryptography keys.
42 * The format and location of stored information is indicated by the TpmLocator.
43 * The TPM is designed to work with a PIB (Public Information Base) which stores public keys and
44 * related information such as certificate.
45 *
46 * The TPM also provides functionalities of crypto transformation, such as signing and decryption.
47 *
48 * A TPM consists of a unified front-end interface and a back-end implementation. The front-end
49 * cache the handles of private keys which is provided by the back-end implementation.
50 *
51 * @throw tpm::BackEnd::Error when underlying implementation has non-semantic error.
52 * @throw Tpm::Error when there is an semantic error.
53 */
54class Tpm : noncopyable
55{
56public:
57 friend class KeyChain;
58
59 class Error : public std::runtime_error
60 {
61 public:
62 explicit
63 Error(const std::string& what)
64 : std::runtime_error(what)
65 {
66 }
67 };
68
69public:
70 ~Tpm();
71
72 std::string
73 getTpmLocator() const;
74
75 /**
76 * @brief Check if a private key exist
77 *
78 * @param keyName The key name
79 * @return true if the key exists
80 */
81 bool
82 hasKey(const Name& keyName) const;
83
84 /**
85 * @return The public portion of an asymmetric key with name @p name
86 * or nullptr if the key does not exist
87 *
88 * The public key is in PKCS#8 format
89 */
90 ConstBufferPtr
91 getPublicKey(const Name& keyName) const;
92
93 /**
94 * @brief Sign blob using key with name @p keyName with digest @p digestAlgorithm.
95 *
96 * @return The signature, or nullptr if the key does not exist
97 */
98 ConstBufferPtr
99 sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
100
101 /**
102 * @brief Decrypt blob using key with name @p keyName.
103 *
104 * @return The signature, or nullptr if the key does not exist
105 */
106 ConstBufferPtr
107 decrypt(const uint8_t* buf, size_t size, const Name& keyName) const;
108
109NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
110 /*
111 * @brief Create a new TPM instance with the specified @p location
112 *
113 * @param scheme The scheme for the TPM
114 * @param location The location for the TPM
115 * @param impl The back-end implementation
116 */
117 Tpm(const std::string& scheme, const std::string& location, unique_ptr<BackEnd> impl);
118
119 BackEnd*
120 getBackEnd()
121 {
122 return m_backEnd.get();
123 }
124
125 /**
126 * @brief Create key for @p identityName according to @p params.
127 *
128 * The created key is named as: /<identityName>/[keyId]/KEY
129 *
130 * @return the key name
131 * @throws Tpm::Error if the key has already existed or the params is invalid
132 */
133 Name
134 createKey(const Name& identityName, const KeyParams& params);
135
136 /**
137 * @brief Delete a key pair with name @p keyName.
138 */
139 void
140 deleteKey(const Name& keyName);
141
142 /**
143 * @brief Export a private key
144 *
145 * This method will export the private key in encrypted PKCS #8 format if the key exists.
146 *
147 * @param keyName The private key name
148 * @param pw The password to encrypt the private key
149 * @param pwLen The length of the password
150 * @return The encoded private key wrapper or an empty block if the key cannot be exported.
151 */
152 ConstBufferPtr
153 exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen);
154
155 /**
156 * @brief Import a private key
157 *
158 * @param keyName The private key name
159 * @param pkcs8 The private key wrapper
160 * @param pkcs8Len The length of the private key wrapper
161 * @param pw The password to encrypt the private key
162 * @param pwLen The length of the password
163 * @return false if importing fails
164 */
165 bool
166 importPrivateKey(const Name& keyName,
167 const uint8_t* pkcs8, size_t pkcs8Len,
168 const char* pw, size_t pwLen);
169
170 /**
171 * @brief Clear the key cache
172 *
173 * An empty cache can force Tpm to do key lookup in back-end.
174 */
175 void
176 clearKeyCache()
177 {
178 m_keys.clear();
179 }
180
181private:
182
183 /**
184 * @brief Internal KeyHandle lookup
185 *
186 * @return A pointer to the handle of key @p keyName if it exists, otherwise nullptr.
187 */
188 const KeyHandle*
189 findKey(const Name& keyName) const;
190
191private:
192 std::string m_scheme;
193 std::string m_location;
194
195 mutable std::unordered_map<Name, unique_ptr<KeyHandle>> m_keys;
196
197 unique_ptr<BackEnd> m_backEnd;
198};
199
200} // namespace tpm
201
202using tpm::Tpm;
203
204} // namespace security
205} // namespace ndn
206
207#endif // NDN_SECURITY_TPM_TPM_HPP