blob: 2c8cc2b185bf4fb941604a79ced5816f912fe64a [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/**
19 * MemoryPrivateKeyStorage extends PrivateKeyStorage to implement a simple in-memory private key store. You should
20 * initialize by calling setKeyPairForKeyName.
21 */
Yingdi Yu87581582014-01-14 14:28:39 -080022class SecTpmMemory : public SecTpm {
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023public:
Yingdi Yu87581582014-01-14 14:28:39 -080024 struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080027 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070028
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080029 /******************************
30 * From TrustedPlatformModule *
31 ******************************/
32
Yingdi Yube4150e2014-02-18 13:02:46 -080033 virtual void
34 setTpmPassword(const uint8_t* password, size_t passwordLength)
35 {}
36
37 virtual void
38 resetTpmPassword()
39 {}
40
41 virtual void
42 setInTerminal(bool inTerminal)
43 {
44 m_inTerminal = inTerminal;
45 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070046
Yingdi Yube4150e2014-02-18 13:02:46 -080047 virtual bool
48 getInTerminal()
49 {
50 return m_inTerminal;
51 }
52
53 virtual bool
54 locked()
55 {
56 return true;
57 }
58
Yingdi Yu2e57a582014-02-20 23:34:43 -080059 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080060 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080061 {
62 return !locked();
63 }
Yingdi Yube4150e2014-02-18 13:02:46 -080064
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070065 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080066 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070067
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070068 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080069 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080070
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071 virtual void
72 deleteKeyPairInTpm(const Name &keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080073
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070074 virtual Block
75 signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
76
77 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080078 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070079
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080080 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080081 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070082
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080084 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085
Yingdi Yu4b752752014-02-18 12:24:03 -080086 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070087 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080088
89 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080090 generateRandomBlock(uint8_t* res, size_t size);
91
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070092 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -080093 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
94 {}
95
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080096 /******************************
97 * SecTpmMemory specific *
98 ******************************/
99
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700100 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800101 * @brief Set the public and private key for the keyName.
102 *
103 * @param keyName The key name.
104 * @param publicKeyDer The public key DER byte array.
105 * @param publicKeyDerLength The length of publicKeyDer.
106 * @param privateKeyDer The private key DER byte array.
107 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700108 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800109 void setKeyPairForKeyName(const Name& keyName,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700110 uint8_t* publicKeyDer, size_t publicKeyDerLength,
111 uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800112
113protected:
114 /******************************
115 * From TrustedPlatformModule *
116 ******************************/
117 virtual ConstBufferPtr
118 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
119
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700120 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800121 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700122
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123 virtual bool
124 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700125
126
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700127private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800128 class RsaPrivateKey;
129
Yingdi Yu2e57a582014-02-20 23:34:43 -0800130 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
131 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700132
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800133 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
134 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800135
136 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700137};
138
Yingdi Yufc40d872014-02-18 12:56:04 -0800139} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700140
Yingdi Yufc40d872014-02-18 12:56:04 -0800141#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP