blob: 61b1c8aa96e69876b5de39cf88e6ff41c001525a [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 */
Yingdi Yu87581582014-01-14 14:28:39 -080023class SecTpmMemory : public SecTpm {
Jeff Thompson6c314bc2013-09-23 18:09:38 -070024public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070025 class Error : public SecTpm::Error
26 {
27 public:
28 explicit
29 Error(const std::string& what)
30 : SecTpm::Error(what)
31 {
32 }
33 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080034
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070035 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080036 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070037
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080038 /******************************
39 * From TrustedPlatformModule *
40 ******************************/
41
Yingdi Yube4150e2014-02-18 13:02:46 -080042 virtual void
43 setTpmPassword(const uint8_t* password, size_t passwordLength)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070044 {
45 }
Yingdi Yube4150e2014-02-18 13:02:46 -080046
47 virtual void
48 resetTpmPassword()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070049 {
50 }
Yingdi Yube4150e2014-02-18 13:02:46 -080051
52 virtual void
53 setInTerminal(bool inTerminal)
54 {
55 m_inTerminal = inTerminal;
56 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070057
Yingdi Yube4150e2014-02-18 13:02:46 -080058 virtual bool
59 getInTerminal()
60 {
61 return m_inTerminal;
62 }
63
64 virtual bool
65 locked()
66 {
67 return true;
68 }
69
Yingdi Yu2e57a582014-02-20 23:34:43 -080070 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080071 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 {
73 return !locked();
74 }
Yingdi Yube4150e2014-02-18 13:02:46 -080075
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070076 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080077 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070078
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080080 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080081
Yingdi Yu28fd32f2014-01-28 19:03:03 -080082 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080084
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070085 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070086 signInTpm(const uint8_t* data, size_t dataLength,
87 const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070088
89 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080090 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070091
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080092 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080093 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070094
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080096 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097
Yingdi Yu4b752752014-02-18 12:24:03 -080098 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070099 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800100
101 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -0800102 generateRandomBlock(uint8_t* res, size_t size);
103
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700104 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -0800105 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700106 {
107 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800108
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800109 /******************************
110 * SecTpmMemory specific *
111 ******************************/
112
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800114 * @brief Set the public and private key for the keyName.
115 *
116 * @param keyName The key name.
117 * @param publicKeyDer The public key DER byte array.
118 * @param publicKeyDerLength The length of publicKeyDer.
119 * @param privateKeyDer The private key DER byte array.
120 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700121 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800122 void setKeyPairForKeyName(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700123 const uint8_t* publicKeyDer, size_t publicKeyDerLength,
124 const uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800125
126protected:
127 /******************************
128 * From TrustedPlatformModule *
129 ******************************/
130 virtual ConstBufferPtr
131 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
132
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700133 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800134 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800136 virtual bool
137 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700138
139
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700140private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800141 class RsaPrivateKey;
142
Yingdi Yu2e57a582014-02-20 23:34:43 -0800143 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
144 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700145
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700146 PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */
147 PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800148
149 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700150};
151
Yingdi Yufc40d872014-02-18 12:56:04 -0800152} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700153
Yingdi Yufc40d872014-02-18 12:56:04 -0800154#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP