blob: 95ee50e046d04b832899b14aeb972c48505e7e62 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev34e13f32014-12-14 15:13:28 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev34e13f32014-12-14 15:13:28 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Afanasyev34e13f32014-12-14 15:13:28 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Afanasyev34e13f32014-12-14 15:13:28 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Afanasyev34e13f32014-12-14 15:13:28 -080019
20#ifndef NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP
21#define NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP
22
23#include <ndn-cxx/security/key-chain.hpp>
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080024#include <ndn-cxx/security/security-common.hpp>
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070025#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 Afanasyev34e13f32014-12-14 15:13:28 -080028
29namespace ndn {
30namespace security {
31
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070032using pib::PibImpl;
33using tpm::BackEnd;
34using tpm::KeyHandle;
35
36class DummyPib : public PibImpl
37{
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080038public:
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070039 class Error : public PibImpl::Error
40 {
41 public:
42 explicit
43 Error(const std::string& what)
44 : PibImpl::Error(what)
45 {
46 }
47 };
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -080048
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070049public:
50 explicit DummyPib(const std::string& locator);
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080051
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070052 // TPM management
53 void
54 setTpmLocator(const std::string& tpmLocator) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080055
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070056 std::string
57 getTpmLocator() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080058
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070059 // Identity manangement
60 bool
61 hasIdentity(const Name& identityName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080062
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070063 void
64 addIdentity(const Name& identityName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080065
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070066 void
67 removeIdentity(const Name& identity) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080068
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070069 void
70 clearIdentities() override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080071
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070072 std::set<Name>
73 getIdentities() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080074
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070075 void
76 setDefaultIdentity(const Name& identityName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080077
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070078 Name
79 getDefaultIdentity() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080080
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070081 // Key management
82 bool
83 hasKey(const Name& keyName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080084
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070085 void
Alexander Afanasyevf007a992022-05-05 15:57:08 -040086 addKey(const Name& identity, const Name& keyName, span<const uint8_t> key) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080087
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070088 void
89 removeKey(const Name& keyName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080090
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070091 Buffer
92 getKeyBits(const Name& keyName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080093
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070094 std::set<Name>
95 getKeysOfIdentity(const Name& identity) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080096
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070097 void
98 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080099
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700100 Name
101 getDefaultKeyOfIdentity(const Name& identity) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800102
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700103 // certificate management
104 bool
105 hasCertificate(const Name& certName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800106
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700107 void
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400108 addCertificate(const Certificate& certificate) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800109
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700110 void
111 removeCertificate(const Name& certName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800112
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400113 Certificate
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700114 getCertificate(const Name& certificateName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800115
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700116 std::set<Name>
117 getCertificatesOfKey(const Name& keyName) const override;
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800118
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700119 void
120 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) override;
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800121
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400122 Certificate
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700123 getDefaultCertificateOfKey(const Name& keyName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800124
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700125 static std::string
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800126 getScheme();
127
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800128 static const std::string SCHEME;
129
130private:
131 std::string m_tpmLocator;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800132};
133
134//////////////////////////////////////////////////////////////////////////////////////////
135//////////////////////////////////////////////////////////////////////////////////////////
136
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700137namespace tpm {
138
139class DummyKeyHandle : public KeyHandle
140{
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800141public:
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700142 explicit
143 DummyKeyHandle(shared_ptr<transform::PrivateKey> key);
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800144
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700145private:
146 ConstBufferPtr
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400147 doSign(DigestAlgorithm digestAlgorithm, const InputBuffers& bufs) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800148
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500149 bool
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400150 doVerify(DigestAlgorithm digestAlgorithm, const InputBuffers& bufs, span<const uint8_t> sig) const final;
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500151
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700152 ConstBufferPtr
Alexander Afanasyevf007a992022-05-05 15:57:08 -0400153 doDecrypt(span<const uint8_t> cipherText) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800154
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700155 ConstBufferPtr
156 doDerivePublicKey() const final;
157};
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800158
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700159} // namespace tpm
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800160
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700161//////////////////////////////////////////////////////////////////////////////////////////
162//////////////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800163
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700164class DummyTpm : public BackEnd
165{
166public:
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 Afanasyev34e13f32014-12-14 15:13:28 -0800176
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700177public:
178 explicit DummyTpm(const std::string& locator);
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800179
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700180 bool
181 isTerminalMode() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800182
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700183 void
184 setTerminalMode(bool isTerminal) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800185
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700186 bool
187 isTpmLocked() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800188
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700189 ConstBufferPtr
190 sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800191
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700192 static std::string
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800193 getScheme();
194
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700195private:
196 bool
197 doHasKey(const Name& keyName) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800198
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700199 unique_ptr<tpm::KeyHandle>
200 doGetKeyHandle(const Name& keyName) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800201
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700202 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 Afanasyevf007a992022-05-05 15:57:08 -0400212 doImportKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen) final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800213
Alexander Afanasyev59090db2020-02-21 16:37:43 -0500214 void
215 doImportKey(const Name& keyName, shared_ptr<transform::PrivateKey> key) final;
216
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800217public:
218 static const std::string SCHEME;
219};
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800220
221} // namespace security
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800222} // namespace ndn
223
224#endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP