blob: f49518d631316cfa9210bbebe8ca257fa1197286 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson7b79eb62013-09-12 18:48:29 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson7b79eb62013-09-12 18:48:29 -07004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7b79eb62013-09-12 18:48:29 -07006 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_SEC_TPM_HPP
10#define NDN_SECURITY_SEC_TPM_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080013#include "security-common.hpp"
14#include "../name.hpp"
15#include "../data.hpp"
16#include "public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070017
18namespace ndn {
19
Yingdi Yufc40d872014-02-18 12:56:04 -080020/**
21 * @brief SecTpm is the base class of the TPM classes.
22 *
23 * It specifies the interfaces of private/secret key related operations.
24 */
Yingdi Yu31b4af22014-01-14 14:13:00 -080025class SecTpm {
Jeff Thompsona50703f2013-09-17 14:24:15 -070026public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080027 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
28
Jeff Thompson7b79eb62013-09-12 18:48:29 -070029 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070030 * The virtual destructor.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070031 */
32 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080033 ~SecTpm() {}
Jeff Thompson7b79eb62013-09-12 18:48:29 -070034
Jeff Thompson7b79eb62013-09-12 18:48:29 -070035 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080036 * @brief Generate a pair of asymmetric keys.
37 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -070038 * @param keyName The name of the key pair.
39 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
40 * @param keySize The size of the key pair.
Yingdi Yufc40d872014-02-18 12:56:04 -080041 * @throws SecTpm::Error if fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070042 */
43 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080044 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080045
46 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080047 * @brief Delete a key pair of asymmetric keys.
48 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -080049 * @param keyName The name of the key pair.
50 */
51 virtual void
52 deleteKeyPairInTpm(const Name &keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070053
54 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080055 * @brief Get a public key.
56 *
57 * @param keyName The public key name.
58 * @return The public key if exists, otherwise a NULL pointer.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070059 */
Yingdi Yufc40d872014-02-18 12:56:04 -080060 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -080061 getPublicKeyFromTpm(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070062
63 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080064 * @brief Sign data.
65 *
66 * @param data Pointer to the byte array to be signed.
Jeff Thompson4c11b9f2013-09-13 11:05:28 -070067 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070068 * @param keyName The name of the signing key.
69 * @param digestAlgorithm the digest algorithm.
Yingdi Yu3c5887c2014-01-21 18:19:49 -080070 * @return The signature block.
Yingdi Yufc40d872014-02-18 12:56:04 -080071 * @throws SecTpm::Error if signing fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070072 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080073 virtual Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080074 signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm) = 0;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080075
Jeff Thompson7b79eb62013-09-12 18:48:29 -070076 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080077 * @brief Decrypt data.
78 *
79 * @param data Pointer to the byte arry to be decrypted.
80 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070081 * @param keyName The name of the decrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -080082 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070083 * @return The decrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -080084 * @throws SecTpm::Error if decryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070085 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080086 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080087 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070088
Jeff Thompson7b79eb62013-09-12 18:48:29 -070089 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080090 * @brief Encrypt data.
91 *
92 * @param data Pointer to the byte arry to be decrypted.
93 * @param dataLength The length of data.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070094 * @param keyName The name of the encrypting key.
Yingdi Yufc40d872014-02-18 12:56:04 -080095 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070096 * @return The encrypted data.
Yingdi Yufc40d872014-02-18 12:56:04 -080097 * @throws SecTpm::Error if encryption fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070098 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080099 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800100 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700102 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700103 * @brief Generate a symmetric key.
Yingdi Yufc40d872014-02-18 12:56:04 -0800104 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105 * @param keyName The name of the key.
106 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
107 * @param keySize The size of the key.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700108 */
109 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800110 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700111
112 /**
Yingdi Yufc40d872014-02-18 12:56:04 -0800113 * @brief Check if a particular key exists.
114 *
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700115 * @param keyName The name of the key.
116 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
117 * @return True if the key exists, otherwise false.
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700118 */
119 virtual bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800120 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) = 0;
Yingdi Yu4b752752014-02-18 12:24:03 -0800121
122 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123 * @brief Generate a random block.
Yingdi Yu4b752752014-02-18 12:24:03 -0800124 *
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800125 * @param res The pointer to the generated block.
126 * @param size The random block size.
Yingdi Yu4b752752014-02-18 12:24:03 -0800127 * @return true for success, otherwise false.
128 */
129 virtual bool
130 generateRandomBlock(uint8_t* res, size_t size) = 0;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800131
132 /**
133 * @brief Export a private key in PKCS#8 format.
134 *
135 * @param keyName The private key name.
136 * @param password The password to encrypt the private key.
137 * @param inTerminal If password is not supplied, get it via terminal if inTerminal is true, otherwise fail.
138 * @return The private key info (in PKCS8 format) if exist, otherwise a NULL pointer.
139 */
140 ConstBufferPtr
141 exportPrivateKeyPkcs8FromTpm(const Name& keyName, bool inTerminal, const std::string& password);
142
143 /**
144 * @brief Import a private key in PKCS#8 format.
145 *
146 * Also recover the public key and installed it in TPM.
147 *
148 * @param keyName The private key name.
149 * @param key The encoded private key info.
150 * @param password The password to encrypt the private key.
151 * @param inTerminal If password is not supplied, get it via terminal if inTerminal is true, otherwise fail.
152 * @return False if import fails.
153 */
154 bool
155 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size, bool inTerminal, const std::string& password);
156
157protected:
158 /**
159 * @brief Export a private key in PKCS#1 format.
160 *
161 * @param keyName The private key name.
162 * @return The private key info (in PKCS#1 format) if exist, otherwise a NULL pointer.
163 */
164 virtual ConstBufferPtr
165 exportPrivateKeyPkcs1FromTpm(const Name& keyName) = 0;
166
167 /**
168 * @brief Import a private key in PKCS#1 format.
169 *
170 * @param keyName The private key name.
171 * @param key The encoded private key info.
172 * @return False if import fails.
173 */
174 virtual bool
175 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
176
177 /**
178 * @brief Import a public key in PKCS#1 format.
179 *
180 * @param keyName The public key name.
181 * @param key The encoded public key info.
182 * @return False if import fails.
183 */
184 virtual bool
185 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
186
187
188 /**
189 * @brief Get password.
190 *
191 * @param password On return, the password.
192 * @param prompt Prompt for password, i.e., "Password for key:"
193 * @return true if password has been obtained.
194 */
195 inline virtual bool
196 getPassWord(std::string& password, const std::string& prompt);
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700197};
198
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800199bool
200SecTpm::getPassWord(std::string& password, const std::string& prompt)
201{
202 int result = false;
203
204 char* pw0 = NULL;
205
206 pw0 = getpass(prompt.c_str());
207 if(!pw0)
208 return false;
209 std::string password1 = pw0;
210 memset(pw0, 0, strlen(pw0));
211
212 pw0 = getpass("Confirm:");
213 if(!pw0)
214 {
215 char* pw1 = const_cast<char*>(password1.c_str());
216 memset(pw1, 0, password1.size());
217 return false;
218 }
219
220 if(!password1.compare(pw0))
221 {
222 result = true;
223 password.swap(password1);
224 }
225
226 char* pw1 = const_cast<char*>(password1.c_str());
227 memset(pw1, 0, password1.size());
228 memset(pw0, 0, strlen(pw0));
229 return result;
230}
231
Yingdi Yufc40d872014-02-18 12:56:04 -0800232} // namespace ndn
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700233
Yingdi Yufc40d872014-02-18 12:56:04 -0800234#endif //NDN_SECURITY_SEC_TPM_HPP