Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 8 | #ifndef NDN_SECURITY_SEC_TPM_MEMORY_HPP |
| 9 | #define NDN_SECURITY_SEC_TPM_MEMORY_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 11 | #include "../common.hpp" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 12 | #include "sec-tpm.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | |
| 14 | struct rsa_st; |
| 15 | |
| 16 | namespace 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 Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 22 | class SecTpmMemory : public SecTpm { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 23 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 25 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 27 | ~SecTpmMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 29 | /****************************** |
| 30 | * From TrustedPlatformModule * |
| 31 | ******************************/ |
| 32 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 33 | 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 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 59 | virtual bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 60 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 61 | { |
| 62 | return !locked(); |
| 63 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 64 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 65 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 66 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 68 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 69 | getPublicKeyFromTpm(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 70 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 71 | virtual void |
| 72 | deleteKeyPairInTpm(const Name &keyName); |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 74 | virtual Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 75 | signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 77 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 78 | decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 80 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 81 | encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 83 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 84 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 86 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 87 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass); |
| 88 | |
| 89 | virtual bool |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 90 | generateRandomBlock(uint8_t* res, size_t size); |
| 91 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 92 | virtual void |
| 93 | addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 94 | {} |
| 95 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 96 | /****************************** |
| 97 | * SecTpmMemory specific * |
| 98 | ******************************/ |
| 99 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 100 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 101 | * @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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 108 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 109 | void setKeyPairForKeyName(const Name& keyName, |
| 110 | uint8_t *publicKeyDer, size_t publicKeyDerLength, |
| 111 | uint8_t *privateKeyDer, size_t privateKeyDerLength); |
| 112 | |
| 113 | protected: |
| 114 | /****************************** |
| 115 | * From TrustedPlatformModule * |
| 116 | ******************************/ |
| 117 | virtual ConstBufferPtr |
| 118 | exportPrivateKeyPkcs1FromTpm(const Name& keyName); |
| 119 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 120 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 121 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
| 122 | |
| 123 | virtual bool |
| 124 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
| 125 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 128 | class RsaPrivateKey; |
| 129 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 130 | typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore; |
| 131 | typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 132 | |
| 133 | PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */ |
| 134 | PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */ |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 135 | |
| 136 | bool m_inTerminal; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 139 | } // namespace ndn |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 140 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 141 | #endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP |