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 |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 86 | addKey(const Name& identity, const Name& keyName, span<const uint8_t> key) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 87 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 88 | void |
| 89 | removeKey(const Name& keyName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 90 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 91 | Buffer |
| 92 | getKeyBits(const Name& keyName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 93 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 94 | std::set<Name> |
| 95 | getKeysOfIdentity(const Name& identity) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 96 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 97 | void |
| 98 | setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 99 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 100 | Name |
| 101 | getDefaultKeyOfIdentity(const Name& identity) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 102 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 103 | // certificate management |
| 104 | bool |
| 105 | hasCertificate(const Name& certName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 106 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 107 | void |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 108 | addCertificate(const Certificate& certificate) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 109 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 110 | void |
| 111 | removeCertificate(const Name& certName) override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 113 | Certificate |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 114 | getCertificate(const Name& certificateName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 115 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 116 | std::set<Name> |
| 117 | getCertificatesOfKey(const Name& keyName) const override; |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 118 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 119 | void |
| 120 | setDefaultCertificateOfKey(const Name& keyName, const Name& certName) override; |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 122 | Certificate |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 123 | getDefaultCertificateOfKey(const Name& keyName) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 124 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 125 | static std::string |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 126 | getScheme(); |
| 127 | |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 128 | static const std::string SCHEME; |
| 129 | |
| 130 | private: |
| 131 | std::string m_tpmLocator; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 135 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 137 | namespace tpm { |
| 138 | |
| 139 | class DummyKeyHandle : public KeyHandle |
| 140 | { |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 141 | public: |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 142 | explicit |
| 143 | DummyKeyHandle(shared_ptr<transform::PrivateKey> key); |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 144 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 145 | private: |
| 146 | ConstBufferPtr |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 147 | doSign(DigestAlgorithm digestAlgorithm, const InputBuffers& bufs) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 149 | bool |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 150 | doVerify(DigestAlgorithm digestAlgorithm, const InputBuffers& bufs, span<const uint8_t> sig) const final; |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 151 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 152 | ConstBufferPtr |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 153 | doDecrypt(span<const uint8_t> cipherText) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 154 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 155 | ConstBufferPtr |
| 156 | doDerivePublicKey() const final; |
| 157 | }; |
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 | } // namespace tpm |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 160 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 161 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 162 | ////////////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 163 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 164 | class DummyTpm : public BackEnd |
| 165 | { |
| 166 | public: |
| 167 | class Error : public BackEnd::Error |
| 168 | { |
| 169 | public: |
| 170 | explicit |
| 171 | Error(const std::string& what) |
| 172 | : BackEnd::Error(what) |
| 173 | { |
| 174 | } |
| 175 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 176 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 177 | public: |
| 178 | explicit DummyTpm(const std::string& locator); |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 179 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 180 | bool |
| 181 | isTerminalMode() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 182 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 183 | void |
| 184 | setTerminalMode(bool isTerminal) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 185 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 186 | bool |
| 187 | isTpmLocked() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 188 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 189 | ConstBufferPtr |
| 190 | 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] | 191 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 192 | static std::string |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 193 | getScheme(); |
| 194 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 195 | private: |
| 196 | bool |
| 197 | doHasKey(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 198 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 199 | unique_ptr<tpm::KeyHandle> |
| 200 | doGetKeyHandle(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 201 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 202 | unique_ptr<tpm::KeyHandle> |
| 203 | doCreateKey(const Name& identity, const KeyParams& params) final; |
| 204 | |
| 205 | void |
| 206 | doDeleteKey(const Name& keyName) final; |
| 207 | |
| 208 | ConstBufferPtr |
| 209 | doExportKey(const Name& keyName, const char* pw, size_t pwLen) final; |
| 210 | |
| 211 | void |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame] | 212 | doImportKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen) final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame] | 214 | void |
| 215 | doImportKey(const Name& keyName, shared_ptr<transform::PrivateKey> key) final; |
| 216 | |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 217 | public: |
| 218 | static const std::string SCHEME; |
| 219 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 220 | |
| 221 | } // namespace security |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 222 | } // namespace ndn |
| 223 | |
| 224 | #endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP |