Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 15 | #ifndef NDN_SECURITY_SEC_TPM_MEMORY_HPP |
| 16 | #define NDN_SECURITY_SEC_TPM_MEMORY_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 17 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 18 | #include "../common.hpp" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 19 | #include "sec-tpm.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 20 | |
| 21 | struct rsa_st; |
| 22 | |
| 23 | namespace ndn { |
| 24 | |
| 25 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 26 | * @brief SecTpmMemory implements a simple in-memory TPM. |
| 27 | * |
| 28 | * You should initialize by calling setKeyPairForKeyName. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 29 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 30 | class SecTpmMemory : public SecTpm |
| 31 | { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 32 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | class Error : public SecTpm::Error |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Error(const std::string& what) |
| 38 | : SecTpm::Error(what) |
| 39 | { |
| 40 | } |
| 41 | }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 43 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 44 | ~SecTpmMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 46 | /****************************** |
| 47 | * From TrustedPlatformModule * |
| 48 | ******************************/ |
| 49 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 50 | virtual void |
| 51 | setTpmPassword(const uint8_t* password, size_t passwordLength) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 54 | |
| 55 | virtual void |
| 56 | resetTpmPassword() |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 57 | { |
| 58 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 59 | |
| 60 | virtual void |
| 61 | setInTerminal(bool inTerminal) |
| 62 | { |
| 63 | m_inTerminal = inTerminal; |
| 64 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 65 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 66 | virtual bool |
| 67 | getInTerminal() |
| 68 | { |
| 69 | return m_inTerminal; |
| 70 | } |
| 71 | |
| 72 | virtual bool |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 73 | isLocked() |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 74 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 75 | return false; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 78 | virtual bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 79 | unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 80 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 81 | return !isLocked(); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 82 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 84 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 85 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 87 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 88 | getPublicKeyFromTpm(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 89 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 90 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | deleteKeyPairInTpm(const Name& keyName); |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 93 | virtual Block |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 94 | signInTpm(const uint8_t* data, size_t dataLength, |
| 95 | const Name& keyName, DigestAlgorithm digestAlgorithm); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 96 | |
| 97 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 98 | 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] | 99 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 100 | virtual ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 101 | 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] | 102 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 103 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 104 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 106 | virtual bool |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 107 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 108 | |
| 109 | virtual bool |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 110 | generateRandomBlock(uint8_t* res, size_t size); |
| 111 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 112 | virtual void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 113 | addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 114 | { |
| 115 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 116 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 117 | /****************************** |
| 118 | * SecTpmMemory specific * |
| 119 | ******************************/ |
| 120 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 121 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 122 | * @brief Set the public and private key for the keyName. |
| 123 | * |
| 124 | * @param keyName The key name. |
| 125 | * @param publicKeyDer The public key DER byte array. |
| 126 | * @param publicKeyDerLength The length of publicKeyDer. |
| 127 | * @param privateKeyDer The private key DER byte array. |
| 128 | * @param privateKeyDerLength The length of privateKeyDer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 129 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 130 | void setKeyPairForKeyName(const Name& keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 131 | const uint8_t* publicKeyDer, size_t publicKeyDerLength, |
| 132 | const uint8_t* privateKeyDer, size_t privateKeyDerLength); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 133 | |
| 134 | protected: |
| 135 | /****************************** |
| 136 | * From TrustedPlatformModule * |
| 137 | ******************************/ |
| 138 | virtual ConstBufferPtr |
| 139 | exportPrivateKeyPkcs1FromTpm(const Name& keyName); |
| 140 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 141 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 142 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 143 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 144 | virtual bool |
| 145 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 146 | |
| 147 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 149 | class RsaPrivateKey; |
| 150 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 151 | typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore; |
| 152 | typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 153 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 154 | PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */ |
| 155 | PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */ |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 156 | |
| 157 | bool m_inTerminal; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 160 | } // namespace ndn |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 161 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 162 | #endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP |