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: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 24 | class Error : public SecTpm::Error |
| 25 | { |
| 26 | public: |
| 27 | explicit |
| 28 | Error(const std::string& what) |
| 29 | : SecTpm::Error(what) |
| 30 | { |
| 31 | } |
| 32 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 34 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 35 | ~SecTpmMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 37 | /****************************** |
| 38 | * From TrustedPlatformModule * |
| 39 | ******************************/ |
| 40 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 41 | virtual void |
| 42 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 43 | {} |
| 44 | |
| 45 | virtual void |
| 46 | resetTpmPassword() |
| 47 | {} |
| 48 | |
| 49 | virtual void |
| 50 | setInTerminal(bool inTerminal) |
| 51 | { |
| 52 | m_inTerminal = inTerminal; |
| 53 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 55 | virtual bool |
| 56 | getInTerminal() |
| 57 | { |
| 58 | return m_inTerminal; |
| 59 | } |
| 60 | |
| 61 | virtual bool |
| 62 | locked() |
| 63 | { |
| 64 | return true; |
| 65 | } |
| 66 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 67 | virtual bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 68 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 69 | { |
| 70 | return !locked(); |
| 71 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 73 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 74 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 76 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 77 | getPublicKeyFromTpm(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 78 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 79 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | deleteKeyPairInTpm(const Name& keyName); |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 82 | virtual Block |
| 83 | signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm); |
| 84 | |
| 85 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 86 | 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] | 87 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 88 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 89 | 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] | 90 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 91 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 92 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 94 | virtual bool |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 95 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 96 | |
| 97 | virtual bool |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 98 | generateRandomBlock(uint8_t* res, size_t size); |
| 99 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 100 | virtual void |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 101 | addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
| 102 | {} |
| 103 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 104 | /****************************** |
| 105 | * SecTpmMemory specific * |
| 106 | ******************************/ |
| 107 | |
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 | * @brief Set the public and private key for the keyName. |
| 110 | * |
| 111 | * @param keyName The key name. |
| 112 | * @param publicKeyDer The public key DER byte array. |
| 113 | * @param publicKeyDerLength The length of publicKeyDer. |
| 114 | * @param privateKeyDer The private key DER byte array. |
| 115 | * @param privateKeyDerLength The length of privateKeyDer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 116 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 117 | void setKeyPairForKeyName(const Name& keyName, |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 118 | uint8_t* publicKeyDer, size_t publicKeyDerLength, |
| 119 | uint8_t* privateKeyDer, size_t privateKeyDerLength); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 120 | |
| 121 | protected: |
| 122 | /****************************** |
| 123 | * From TrustedPlatformModule * |
| 124 | ******************************/ |
| 125 | virtual ConstBufferPtr |
| 126 | exportPrivateKeyPkcs1FromTpm(const Name& keyName); |
| 127 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 128 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 129 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 130 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 131 | virtual bool |
| 132 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 133 | |
| 134 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 135 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 136 | class RsaPrivateKey; |
| 137 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 138 | typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore; |
| 139 | typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 141 | PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */ |
| 142 | PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */ |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 143 | |
| 144 | bool m_inTerminal; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 147 | } // namespace ndn |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 149 | #endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP |