blob: 3ad2c133621a36a09a24b891e9f1cdff572f1a25 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson6c314bc2013-09-23 18:09:38 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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 Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070022 */
23
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_SEC_TPM_MEMORY_HPP
25#define NDN_SECURITY_SEC_TPM_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080027#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080028#include "sec-tpm.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include <map>
31
Jeff Thompson6c314bc2013-09-23 18:09:38 -070032struct rsa_st;
33
34namespace ndn {
35
36/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070037 * @brief SecTpmMemory implements a simple in-memory TPM.
38 *
39 * You should initialize by calling setKeyPairForKeyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070040 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070041class SecTpmMemory : public SecTpm
42{
Jeff Thompson6c314bc2013-09-23 18:09:38 -070043public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 class Error : public SecTpm::Error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : SecTpm::Error(what)
50 {
51 }
52 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080053
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080055 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080057 /******************************
58 * From TrustedPlatformModule *
59 ******************************/
60
Yingdi Yube4150e2014-02-18 13:02:46 -080061 virtual void
62 setTpmPassword(const uint8_t* password, size_t passwordLength)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 {
64 }
Yingdi Yube4150e2014-02-18 13:02:46 -080065
66 virtual void
67 resetTpmPassword()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070068 {
69 }
Yingdi Yube4150e2014-02-18 13:02:46 -080070
71 virtual void
72 setInTerminal(bool inTerminal)
73 {
74 m_inTerminal = inTerminal;
75 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070076
Yingdi Yube4150e2014-02-18 13:02:46 -080077 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070078 getInTerminal() const
Yingdi Yube4150e2014-02-18 13:02:46 -080079 {
80 return m_inTerminal;
81 }
82
83 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070084 isLocked()
Yingdi Yube4150e2014-02-18 13:02:46 -080085 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070086 return false;
Yingdi Yube4150e2014-02-18 13:02:46 -080087 }
88
Yingdi Yu2e57a582014-02-20 23:34:43 -080089 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080090 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080091 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070092 return !isLocked();
Yingdi Yu2e57a582014-02-20 23:34:43 -080093 }
Yingdi Yube4150e2014-02-18 13:02:46 -080094
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080096 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070098 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080099 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800100
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800101 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -0800103
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700104 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700105 signInTpm(const uint8_t* data, size_t dataLength,
106 const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700107
108 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800109 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700110
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800111 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800112 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700114 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800115 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700116
Yingdi Yu4b752752014-02-18 12:24:03 -0800117 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700118 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800119
120 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -0800121 generateRandomBlock(uint8_t* res, size_t size);
122
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700123 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700124 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 {
126 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800127
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800128 /******************************
129 * SecTpmMemory specific *
130 ******************************/
131
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700132 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800133 * @brief Set the public and private key for the keyName.
134 *
135 * @param keyName The key name.
136 * @param publicKeyDer The public key DER byte array.
137 * @param publicKeyDerLength The length of publicKeyDer.
138 * @param privateKeyDer The private key DER byte array.
139 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700140 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800141 void setKeyPairForKeyName(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700142 const uint8_t* publicKeyDer, size_t publicKeyDerLength,
143 const uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800144
145protected:
146 /******************************
147 * From TrustedPlatformModule *
148 ******************************/
149 virtual ConstBufferPtr
150 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
151
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700152 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700154
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800155 virtual bool
156 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700157
158
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700159private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800160 class RsaPrivateKey;
161
Yingdi Yu2e57a582014-02-20 23:34:43 -0800162 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
163 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700164
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700165 PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */
166 PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800167
168 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700169};
170
Yingdi Yufc40d872014-02-18 12:56:04 -0800171} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700172
Yingdi Yufc40d872014-02-18 12:56:04 -0800173#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP