Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 19 | |
| 20 | #ifndef NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP |
| 21 | #define NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP |
| 22 | |
| 23 | #include <ndn-cxx/security/key-chain.hpp> |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 24 | #include <ndn-cxx/security/security-common.hpp> |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/pib/pib-impl.hpp> |
| 26 | #include <ndn-cxx/security/tpm/back-end.hpp> |
| 27 | #include <ndn-cxx/security/transform/private-key.hpp> |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 32 | using pib::PibImpl; |
| 33 | using tpm::BackEnd; |
| 34 | using tpm::KeyHandle; |
| 35 | |
| 36 | class DummyPib : public PibImpl |
| 37 | { |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 38 | public: |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 39 | class Error : public PibImpl::Error |
| 40 | { |
| 41 | public: |
| 42 | explicit |
| 43 | Error(const std::string& what) |
| 44 | : PibImpl::Error(what) |
| 45 | { |
| 46 | } |
| 47 | }; |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 48 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 49 | public: |
| 50 | explicit DummyPib(const std::string& locator); |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 51 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 52 | // TPM management |
| 53 | void |
| 54 | setTpmLocator(const std::string& tpmLocator) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 55 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 56 | std::string |
| 57 | getTpmLocator() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 58 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 59 | // Identity manangement |
| 60 | bool |
| 61 | hasIdentity(const Name& identityName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 62 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 63 | void |
| 64 | addIdentity(const Name& identityName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 65 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 66 | void |
| 67 | removeIdentity(const Name& identity) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 68 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 69 | void |
| 70 | clearIdentities() override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 71 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 72 | std::set<Name> |
| 73 | getIdentities() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 74 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 75 | void |
| 76 | setDefaultIdentity(const Name& identityName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 77 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 78 | Name |
| 79 | getDefaultIdentity() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 80 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 81 | // Key management |
| 82 | bool |
| 83 | hasKey(const Name& keyName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 84 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 85 | void |
| 86 | addKey(const Name& identity, const Name& keyName, const uint8_t* key, |
| 87 | size_t keyLen) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 88 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 89 | void |
| 90 | removeKey(const Name& keyName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 91 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 92 | Buffer |
| 93 | getKeyBits(const Name& keyName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 94 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 95 | std::set<Name> |
| 96 | getKeysOfIdentity(const Name& identity) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 97 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 98 | void |
| 99 | setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 100 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 101 | Name |
| 102 | getDefaultKeyOfIdentity(const Name& identity) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 103 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 104 | // certificate management |
| 105 | bool |
| 106 | hasCertificate(const Name& certName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 107 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 108 | void |
| 109 | addCertificate(const v2::Certificate& certificate) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 110 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 111 | void |
| 112 | removeCertificate(const Name& certName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 113 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 114 | v2::Certificate |
| 115 | getCertificate(const Name& certificateName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 116 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 117 | std::set<Name> |
| 118 | getCertificatesOfKey(const Name& keyName) const override; |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 119 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 120 | void |
| 121 | setDefaultCertificateOfKey(const Name& keyName, const Name& certName) override; |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 122 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 123 | v2::Certificate |
| 124 | getDefaultCertificateOfKey(const Name& keyName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 125 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 126 | static std::string |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 127 | getScheme(); |
| 128 | |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 129 | static const std::string SCHEME; |
| 130 | |
| 131 | private: |
| 132 | std::string m_tpmLocator; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 136 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 137 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 138 | namespace tpm { |
| 139 | |
| 140 | class DummyKeyHandle : public KeyHandle |
| 141 | { |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 142 | public: |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 143 | explicit |
| 144 | DummyKeyHandle(shared_ptr<transform::PrivateKey> key); |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 145 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 146 | private: |
| 147 | ConstBufferPtr |
| 148 | doSign(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 149 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 150 | ConstBufferPtr |
| 151 | doDecrypt(const uint8_t* cipherText, size_t cipherTextLen) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 152 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 153 | ConstBufferPtr |
| 154 | doDerivePublicKey() const final; |
| 155 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 156 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 157 | } // namespace tpm |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 158 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 159 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 160 | ////////////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 161 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 162 | class DummyTpm : public BackEnd |
| 163 | { |
| 164 | public: |
| 165 | class Error : public BackEnd::Error |
| 166 | { |
| 167 | public: |
| 168 | explicit |
| 169 | Error(const std::string& what) |
| 170 | : BackEnd::Error(what) |
| 171 | { |
| 172 | } |
| 173 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 174 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 175 | public: |
| 176 | explicit DummyTpm(const std::string& locator); |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 177 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 178 | bool |
| 179 | isTerminalMode() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 180 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 181 | void |
| 182 | setTerminalMode(bool isTerminal) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 183 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 184 | bool |
| 185 | isTpmLocked() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 186 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 187 | ConstBufferPtr |
| 188 | sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 189 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 190 | static std::string |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 191 | getScheme(); |
| 192 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 193 | private: |
| 194 | bool |
| 195 | doHasKey(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 196 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 197 | unique_ptr<tpm::KeyHandle> |
| 198 | doGetKeyHandle(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 199 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 200 | unique_ptr<tpm::KeyHandle> |
| 201 | doCreateKey(const Name& identity, const KeyParams& params) final; |
| 202 | |
| 203 | void |
| 204 | doDeleteKey(const Name& keyName) final; |
| 205 | |
| 206 | ConstBufferPtr |
| 207 | doExportKey(const Name& keyName, const char* pw, size_t pwLen) final; |
| 208 | |
| 209 | void |
| 210 | doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 212 | public: |
| 213 | static const std::string SCHEME; |
| 214 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 215 | |
| 216 | } // namespace security |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 217 | } // namespace ndn |
| 218 | |
| 219 | #endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP |