blob: 5bddf8827a5b29c9f10f6cf08999816c069eed0a [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
Jeff Thompson6c314bc2013-09-23 18:09:38 -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 }
46
47 virtual bool
48 getInTerminal()
49 {
50 return m_inTerminal;
51 }
52
53 virtual bool
54 locked()
55 {
56 return true;
57 }
58
59 virtual void
60 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
61 {}
62
Jeff Thompson6c314bc2013-09-23 18:09:38 -070063 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080064 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070065
Jeff Thompson6c314bc2013-09-23 18:09:38 -070066 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080067 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080068
Yingdi Yu28fd32f2014-01-28 19:03:03 -080069 virtual void
70 deleteKeyPairInTpm(const Name &keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080071
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080072 virtual Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080073 signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080074
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080075 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080076 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070077
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080078 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080079 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070080
Jeff Thompson6c314bc2013-09-23 18:09:38 -070081 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080082 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070083
Yingdi Yu4b752752014-02-18 12:24:03 -080084 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080085 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
86
87 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080088 generateRandomBlock(uint8_t* res, size_t size);
89
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080090 /******************************
91 * SecTpmMemory specific *
92 ******************************/
93
Jeff Thompson6c314bc2013-09-23 18:09:38 -070094 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080095 * @brief Set the public and private key for the keyName.
96 *
97 * @param keyName The key name.
98 * @param publicKeyDer The public key DER byte array.
99 * @param publicKeyDerLength The length of publicKeyDer.
100 * @param privateKeyDer The private key DER byte array.
101 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800103 void setKeyPairForKeyName(const Name& keyName,
104 uint8_t *publicKeyDer, size_t publicKeyDerLength,
105 uint8_t *privateKeyDer, size_t privateKeyDerLength);
106
107protected:
108 /******************************
109 * From TrustedPlatformModule *
110 ******************************/
111 virtual ConstBufferPtr
112 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
113
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700114 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800115 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
116
117 virtual bool
118 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
119
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700120
121private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800122 class RsaPrivateKey;
123
124 typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore;
125 typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore;
126
127 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
128 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800129
130 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700131};
132
Yingdi Yufc40d872014-02-18 12:56:04 -0800133} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700134
Yingdi Yufc40d872014-02-18 12:56:04 -0800135#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP