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 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame^] | 150 | bool |
| 151 | doVerify(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t bufLen, |
| 152 | const uint8_t* sig, size_t sigLen) const final; |
| 153 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 154 | ConstBufferPtr |
| 155 | doDecrypt(const uint8_t* cipherText, size_t cipherTextLen) const final; |
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 | ConstBufferPtr |
| 158 | doDerivePublicKey() const final; |
| 159 | }; |
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 | } // namespace tpm |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 162 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 163 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 164 | ////////////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 165 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 166 | class DummyTpm : public BackEnd |
| 167 | { |
| 168 | public: |
| 169 | class Error : public BackEnd::Error |
| 170 | { |
| 171 | public: |
| 172 | explicit |
| 173 | Error(const std::string& what) |
| 174 | : BackEnd::Error(what) |
| 175 | { |
| 176 | } |
| 177 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 178 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 179 | public: |
| 180 | explicit DummyTpm(const std::string& locator); |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 181 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 182 | bool |
| 183 | isTerminalMode() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 184 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 185 | void |
| 186 | setTerminalMode(bool isTerminal) const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 187 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 188 | bool |
| 189 | isTpmLocked() const override; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 190 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 191 | ConstBufferPtr |
| 192 | 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] | 193 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 194 | static std::string |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 195 | getScheme(); |
| 196 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 197 | private: |
| 198 | bool |
| 199 | doHasKey(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 200 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 201 | unique_ptr<tpm::KeyHandle> |
| 202 | doGetKeyHandle(const Name& keyName) const final; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 203 | |
Spyridon Mastorakis | 17be058 | 2017-09-27 20:32:39 -0700 | [diff] [blame] | 204 | unique_ptr<tpm::KeyHandle> |
| 205 | doCreateKey(const Name& identity, const KeyParams& params) final; |
| 206 | |
| 207 | void |
| 208 | doDeleteKey(const Name& keyName) final; |
| 209 | |
| 210 | ConstBufferPtr |
| 211 | doExportKey(const Name& keyName, const char* pw, size_t pwLen) final; |
| 212 | |
| 213 | void |
| 214 | 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] | 215 | |
Alexander Afanasyev | 59090db | 2020-02-21 16:37:43 -0500 | [diff] [blame^] | 216 | void |
| 217 | doImportKey(const Name& keyName, shared_ptr<transform::PrivateKey> key) final; |
| 218 | |
Alexander Afanasyev | df26b5a | 2015-01-15 23:30:56 -0800 | [diff] [blame] | 219 | public: |
| 220 | static const std::string SCHEME; |
| 221 | }; |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 222 | |
| 223 | } // namespace security |
Alexander Afanasyev | 34e13f3 | 2014-12-14 15:13:28 -0800 | [diff] [blame] | 224 | } // namespace ndn |
| 225 | |
| 226 | #endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP |