blob: eb1613df03a7dd0557cfa2d73baef3018c58bf5d [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Yingdi Yufc40d872014-02-18 12:56:04 -08008#ifndef NDN_SECURITY_SEC_TPM_MEMORY_HPP
9#define NDN_SECURITY_SEC_TPM_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080012#include "sec-tpm.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013
14struct rsa_st;
15
16namespace ndn {
17
18/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070019 * @brief SecTpmMemory implements a simple in-memory TPM.
20 *
21 * You should initialize by calling setKeyPairForKeyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070022 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070023class SecTpmMemory : public SecTpm
24{
Jeff Thompson6c314bc2013-09-23 18:09:38 -070025public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070026 class Error : public SecTpm::Error
27 {
28 public:
29 explicit
30 Error(const std::string& what)
31 : SecTpm::Error(what)
32 {
33 }
34 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080035
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070036 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080037 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070038
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080039 /******************************
40 * From TrustedPlatformModule *
41 ******************************/
42
Yingdi Yube4150e2014-02-18 13:02:46 -080043 virtual void
44 setTpmPassword(const uint8_t* password, size_t passwordLength)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070045 {
46 }
Yingdi Yube4150e2014-02-18 13:02:46 -080047
48 virtual void
49 resetTpmPassword()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070050 {
51 }
Yingdi Yube4150e2014-02-18 13:02:46 -080052
53 virtual void
54 setInTerminal(bool inTerminal)
55 {
56 m_inTerminal = inTerminal;
57 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070058
Yingdi Yube4150e2014-02-18 13:02:46 -080059 virtual bool
60 getInTerminal()
61 {
62 return m_inTerminal;
63 }
64
65 virtual bool
66 locked()
67 {
68 return true;
69 }
70
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080072 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080073 {
74 return !locked();
75 }
Yingdi Yube4150e2014-02-18 13:02:46 -080076
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070077 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080078 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070079
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070080 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080081 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080082
Yingdi Yu28fd32f2014-01-28 19:03:03 -080083 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080085
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 signInTpm(const uint8_t* data, size_t dataLength,
88 const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089
90 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080091 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070092
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080093 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080094 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070095
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080097 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070098
Yingdi Yu4b752752014-02-18 12:24:03 -080099 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700100 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800101
102 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -0800103 generateRandomBlock(uint8_t* res, size_t size);
104
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700105 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -0800106 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 {
108 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800109
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800110 /******************************
111 * SecTpmMemory specific *
112 ******************************/
113
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700114 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800115 * @brief Set the public and private key for the keyName.
116 *
117 * @param keyName The key name.
118 * @param publicKeyDer The public key DER byte array.
119 * @param publicKeyDerLength The length of publicKeyDer.
120 * @param privateKeyDer The private key DER byte array.
121 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700122 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123 void setKeyPairForKeyName(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 const uint8_t* publicKeyDer, size_t publicKeyDerLength,
125 const uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800126
127protected:
128 /******************************
129 * From TrustedPlatformModule *
130 ******************************/
131 virtual ConstBufferPtr
132 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
133
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700134 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800135 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700136
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800137 virtual bool
138 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700139
140
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700141private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800142 class RsaPrivateKey;
143
Yingdi Yu2e57a582014-02-20 23:34:43 -0800144 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
145 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700146
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700147 PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */
148 PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800149
150 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700151};
152
Yingdi Yufc40d872014-02-18 12:56:04 -0800153} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700154
Yingdi Yufc40d872014-02-18 12:56:04 -0800155#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP