blob: e71d083968e23c8969ae1af62e9b1e6c909c46ca [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
86 addKey(const Name& identity, const Name& keyName, const uint8_t* key,
87 size_t keyLen) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080088
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070089 void
90 removeKey(const Name& keyName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080091
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070092 Buffer
93 getKeyBits(const Name& keyName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080094
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070095 std::set<Name>
96 getKeysOfIdentity(const Name& identity) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -080097
Spyridon Mastorakis17be0582017-09-27 20:32:39 -070098 void
99 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800100
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700101 Name
102 getDefaultKeyOfIdentity(const Name& identity) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800103
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700104 // certificate management
105 bool
106 hasCertificate(const Name& certName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800107
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700108 void
109 addCertificate(const v2::Certificate& certificate) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800110
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700111 void
112 removeCertificate(const Name& certName) override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800113
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700114 v2::Certificate
115 getCertificate(const Name& certificateName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800116
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700117 std::set<Name>
118 getCertificatesOfKey(const Name& keyName) const override;
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800119
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700120 void
121 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) override;
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800122
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700123 v2::Certificate
124 getDefaultCertificateOfKey(const Name& keyName) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800125
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700126 static std::string
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800127 getScheme();
128
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800129 static const std::string SCHEME;
130
131private:
132 std::string m_tpmLocator;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800133};
134
135//////////////////////////////////////////////////////////////////////////////////////////
136//////////////////////////////////////////////////////////////////////////////////////////
137
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700138namespace tpm {
139
140class DummyKeyHandle : public KeyHandle
141{
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800142public:
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700143 explicit
144 DummyKeyHandle(shared_ptr<transform::PrivateKey> key);
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800145
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700146private:
147 ConstBufferPtr
148 doSign(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800149
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700150 ConstBufferPtr
151 doDecrypt(const uint8_t* cipherText, size_t cipherTextLen) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800152
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700153 ConstBufferPtr
154 doDerivePublicKey() const final;
155};
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800156
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700157} // namespace tpm
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800158
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700159//////////////////////////////////////////////////////////////////////////////////////////
160//////////////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800161
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700162class DummyTpm : public BackEnd
163{
164public:
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 Afanasyev34e13f32014-12-14 15:13:28 -0800174
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700175public:
176 explicit DummyTpm(const std::string& locator);
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800177
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700178 bool
179 isTerminalMode() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800180
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700181 void
182 setTerminalMode(bool isTerminal) const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800183
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700184 bool
185 isTpmLocked() const override;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800186
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700187 ConstBufferPtr
188 sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800189
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700190 static std::string
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800191 getScheme();
192
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700193private:
194 bool
195 doHasKey(const Name& keyName) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800196
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700197 unique_ptr<tpm::KeyHandle>
198 doGetKeyHandle(const Name& keyName) const final;
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800199
Spyridon Mastorakis17be0582017-09-27 20:32:39 -0700200 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 Afanasyev34e13f32014-12-14 15:13:28 -0800211
Alexander Afanasyevdf26b5a2015-01-15 23:30:56 -0800212public:
213 static const std::string SCHEME;
214};
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800215
216} // namespace security
Alexander Afanasyev34e13f32014-12-14 15:13:28 -0800217} // namespace ndn
218
219#endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP