blob: a6e1b30acdad381b988bb31773fd96d5ddeca2b3 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001diff --git a/ndn-cxx/security/v2/key-chain.cpp b/ndn-cxx/security/v2/key-chain.cpp
2index 8043635..b9af6f0 100644
3--- a/ndn-cxx/security/v2/key-chain.cpp
4+++ b/ndn-cxx/security/v2/key-chain.cpp
5@@ -25,6 +25,7 @@
6 #include "ndn-cxx/util/config-file.hpp"
7 #include "ndn-cxx/util/logger.hpp"
8 #include "ndn-cxx/util/sha256.hpp"
9+#include "ndn-cxx/util/dummy-keychain.hpp"
10
11 #include "ndn-cxx/security/pib/pib-memory.hpp"
12 #include "ndn-cxx/security/pib/pib-sqlite3.hpp"
13@@ -163,7 +164,7 @@ KeyChain::getDefaultKeyParams()
14 //
15
16 KeyChain::KeyChain()
17- : KeyChain(getDefaultPibLocator(), getDefaultTpmLocator(), true)
18+ : KeyChain("pib-dummy", "tpm-dummy", true)
19 {
20 }
21
22diff --git a/ndn-cxx/util/dummy-keychain.cpp b/ndn-cxx/util/dummy-keychain.cpp
23new file mode 100644
24index 0000000..aa24465
25--- /dev/null
26+++ b/ndn-cxx/util/dummy-keychain.cpp
27@@ -0,0 +1,346 @@
28+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
29+/**
30+ * Copyright (c) 2011-2015 Regents of the University of California.
31+ *
32+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
33+ * contributors.
34+ *
35+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
36+ * of the GNU General Public License as published by the Free Software Foundation,
37+ * either version 3 of the License, or (at your option) any later version.
38+ *
39+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
40+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
41+ * PURPOSE. See the GNU General Public License for more details.
42+ *
43+ * You should have received a copy of the GNU General Public License along with
44+ * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
45+ **/
46+
47+#include "dummy-keychain.hpp"
48+
49+#include <ndn-cxx/util/io.hpp>
50+#include <ndn-cxx/security/transform/public-key.hpp>
51+#include <boost/iostreams/device/array.hpp>
52+#include <boost/iostreams/stream.hpp>
53+
54+namespace ndn {
55+namespace security {
56+
57+static const uint8_t DUMMY_CERT[] =
58+ "Bv0CqQclCAVkdW1teQgDS0VZCAgtnChyuKo7YAgCTkEICf0AAAFe3zzGfBQJGAEC"
59+ "GQQANu6AFf0BJjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOJSa1j6"
60+ "JAzobrmtlUdJi38EWQZOigLykf9psImvIu7pa29Q3apBXENcV1E9687FmfY85Ec3"
61+ "/onMtN7WG/wiuKiu/9eOr5WslD3VGDgxhesx80ygP0GNsN8FzsMl0lRKduXx3wG4"
62+ "MCT8CX7uA4n4JbHY+0QaKUEEMRCiXcRAtF+yKfr+GaVeSemg+i/LR+6CSgpOyH0K"
63+ "ogH9nlNhBn1Hxyc8X+B/nEu6P6NNEKkSnhT1jAbWtT1eL3BPGK/HNY19w9k2Ln6/"
64+ "OYlhaHfB4m0oR/ePcUguQBwTgYS+40YQYUPivBsSQv3X2/7+gGLhIzA4YppwOooK"
65+ "RGhbmL8zF2evmJ8CAwEAARZHGwEBHBgHFggFZHVtbXkIA0tFWQgILZwocriqO2D9"
66+ "AP0m/QD+DzIwMTcxMDAyVDIyMzczNv0A/w8yMDE4MTAwMlQyMjM3MzUX/QEAMBUV"
67+ "O51BEQwxp646i7IuHkuc1C/LISnOP2+wBFF2Ea1ht9MKjKkyJdmB1/GAOiR3njKd"
68+ "UgRjBlownL11EwYDxkPY39RR05TmhF6PkpP81Ro/Vzv7rtSi/dxFIZXCiyuKPRUx"
69+ "E0pZelPWVs3zMfqQ+8rWG89Kqs1vM0dglLBzlX9Lbim71TyLmaJaMmmBKv8+eQ22"
70+ "CN71sRZOovl1kKcTHpOm61nD3C1n9GRflFtaMAXE/XU4zM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86, 0x09, 0x5c, 0x97, 0x6b, 0x8f, 0xae, 0xe0,
85+ 0x60, 0xe7, 0x23, 0x98, 0x6a, 0xee, 0xc1, 0xb0, 0x14, 0xbe, 0x46, 0x2c, 0xfb, 0xa7, 0x27, 0x73,
86+ 0xe4, 0xf3, 0x26, 0x33, 0xba, 0x99, 0xd4, 0x01, 0x38, 0xa8, 0xf2, 0x9e, 0x87, 0xe0, 0x71, 0x0b,
87+ 0x25, 0x44, 0x07, 0x35, 0x88, 0xab, 0x67, 0x27, 0x56, 0x0e, 0xb5, 0xb5, 0xe8, 0x27, 0xb4, 0x49,
88+ 0xdc, 0xb8, 0x48, 0x31, 0xff, 0x99, 0x48, 0xab, 0x11, 0xb4, 0xa0, 0xdf, 0x8a, 0x6d, 0xff, 0x43,
89+ 0x69, 0x32, 0xa7, 0xbc, 0x63, 0x9d, 0x0f, 0xe0, 0x95, 0x34, 0x36, 0x25, 0x4b, 0x3e, 0x36, 0xbd,
90+ 0x81, 0x91, 0x0b, 0x91, 0x9f, 0x3a, 0x04, 0xa2, 0x44, 0x28, 0x19, 0xa1, 0x38, 0x21, 0x4f, 0x25,
91+ 0x59, 0x8a, 0x48, 0xc2};
92+
93+const std::string DummyPib::SCHEME = "pib-dummy";
94+const std::string DummyTpm::SCHEME = "tpm-dummy";
95+
96+NDN_CXX_V2_KEYCHAIN_REGISTER_PIB_BACKEND(DummyPib);
97+NDN_CXX_V2_KEYCHAIN_REGISTER_TPM_BACKEND(DummyTpm);
98+
99+DummyPib::DummyPib(const std::string& locator)
100+{
101+}
102+
103+void
104+DummyPib::setTpmLocator(const std::string& tpmLocator)
105+{
106+ m_tpmLocator = tpmLocator;
107+}
108+
109+std::string
110+DummyPib::getTpmLocator() const
111+{
112+ return m_tpmLocator;
113+}
114+
115+bool
116+DummyPib::hasIdentity(const Name& identityName) const
117+{
118+ return true;
119+}
120+
121+void
122+DummyPib::addIdentity(const Name& identityName)
123+{
124+}
125+
126+void
127+DummyPib::removeIdentity(const Name& identity)
128+{
129+}
130+
131+void
132+DummyPib::clearIdentities()
133+{
134+}
135+
136+std::set<Name>
137+DummyPib::getIdentities() const
138+{
139+ std::set<Name> identities;
140+ identities.insert("/dummy");
141+ return identities;
142+}
143+
144+void
145+DummyPib::setDefaultIdentity(const Name& identityName)
146+{
147+}
148+
149+Name
150+DummyPib::getDefaultIdentity() const
151+{
152+ return "/dummy";
153+}
154+
155+bool
156+DummyPib::hasKey(const Name& keyName) const
157+{
158+ return true;
159+}
160+
161+void
162+DummyPib::addKey(const Name& identity, const Name& keyName,
163+ const uint8_t* key, size_t keyLen)
164+{
165+}
166+
167+void
168+DummyPib::removeKey(const Name& keyName)
169+{
170+}
171+
172+Buffer
173+DummyPib::getKeyBits(const Name& keyName) const
174+{
175+ typedef boost::iostreams::stream<boost::iostreams::array_source> arrayStream;
176+ arrayStream
177+ is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
178+ auto cert = io::load<v2::Certificate>(is, io::BASE64);
179+ return cert->getPublicKey();
180+}
181+
182+std::set<Name>
183+DummyPib::getKeysOfIdentity(const Name& identity) const
184+{
185+ std::set<Name> keys;
186+ keys.insert("/dummy/KEY/-%9C%28r%B8%AA%3B%60");
187+ return keys;
188+}
189+
190+void
191+DummyPib::setDefaultKeyOfIdentity(const Name& identity, const Name& keyName)
192+{
193+}
194+
195+Name
196+DummyPib::getDefaultKeyOfIdentity(const Name& identity) const
197+{
198+ return "/dummy/KEY/-%9C%28r%B8%AA%3B%60";
199+}
200+
201+bool
202+DummyPib::hasCertificate(const Name& certName) const
203+{
204+ return true;
205+}
206+
207+void
208+DummyPib::addCertificate(const v2::Certificate& certificate)
209+{
210+}
211+
212+void
213+DummyPib::removeCertificate(const Name& certName)
214+{
215+}
216+
217+v2::Certificate
218+DummyPib::getCertificate(const Name& certificateName) const
219+{
220+ static shared_ptr<v2::Certificate> cert = nullptr;
221+ if (cert == nullptr) {
222+ typedef boost::iostreams::stream<boost::iostreams::array_source> arrayStream;
223+ arrayStream
224+ is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
225+ cert = io::load<v2::Certificate>(is, io::BASE64);
226+ }
227+
228+ return *cert;
229+}
230+
231+std::set<Name>
232+DummyPib::getCertificatesOfKey(const Name& keyName) const
233+{
234+ std::set<Name> certs;
235+ certs.insert("/dummy/KEY/-%9C%28r%B8%AA%3B%60/self/%FD%00%00%01%5E%DF%3Bv%01");
236+ return certs;
237+}
238+
239+void
240+DummyPib::setDefaultCertificateOfKey(const Name& keyName, const Name& certName)
241+{
242+}
243+
244+v2::Certificate
245+DummyPib::getDefaultCertificateOfKey(const Name& keyName) const
246+{
247+ static shared_ptr<v2::Certificate> cert = nullptr;
248+ if (cert == nullptr) {
249+ typedef boost::iostreams::stream<boost::iostreams::array_source> arrayStream;
250+ arrayStream
251+ is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
252+ cert = io::load<v2::Certificate>(is, io::BASE64);
253+ }
254+
255+ return *cert;
256+}
257+
258+std::string
259+DummyPib::getScheme()
260+{
261+ return DummyPib::SCHEME;
262+}
263+
264+//////////////////////////////////////////////////////////////////////////////////////////
265+//////////////////////////////////////////////////////////////////////////////////////////
266+//////////////////////////////////////////////////////////////////////////////////////////
267+//////////////////////////////////////////////////////////////////////////////////////////
268+
269+namespace tpm {
270+
271+DummyKeyHandle::DummyKeyHandle(shared_ptr<transform::PrivateKey> key)
272+{
273+}
274+
275+ConstBufferPtr
276+DummyKeyHandle::doSign(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size) const
277+{
278+ return make_shared<Buffer>(DUMMY_SIGNATURE, sizeof(DUMMY_SIGNATURE));
279+}
280+
281+bool
282+DummyKeyHandle::doVerify(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t bufLen,
283+ const uint8_t* sig, size_t sigLen) const
284+{
285+ return true;
286+}
287+
288+ConstBufferPtr
289+DummyKeyHandle::doDecrypt(const uint8_t* cipherText, size_t cipherTextLen) const
290+{
291+ throw Error("Not supported");
292+}
293+
294+ConstBufferPtr
295+DummyKeyHandle::doDerivePublicKey() const
296+{
297+ throw Error("Not supported");
298+}
299+
300+} // namespace tpm
301+
302+//////////////////////////////////////////////////////////////////////////////////////////
303+//////////////////////////////////////////////////////////////////////////////////////////
304+//////////////////////////////////////////////////////////////////////////////////////////
305+//////////////////////////////////////////////////////////////////////////////////////////
306+
307+DummyTpm::DummyTpm(const std::string& locator)
308+{
309+}
310+
311+bool
312+DummyTpm::isTerminalMode() const
313+{
314+ return false;
315+}
316+
317+void
318+DummyTpm::setTerminalMode(bool isTerminal) const
319+{
320+}
321+
322+bool
323+DummyTpm::isTpmLocked() const
324+{
325+ return false;
326+}
327+
328+bool
329+DummyTpm::doHasKey(const Name& keyName) const
330+{
331+ return false;
332+}
333+
334+unique_ptr<tpm::KeyHandle>
335+DummyTpm::doGetKeyHandle(const Name& keyName) const
336+{
337+ unique_ptr<tpm::KeyHandle> m_dummyKeyHandle = make_unique<tpm::DummyKeyHandle>(nullptr);
338+ return m_dummyKeyHandle;
339+}
340+
341+unique_ptr<tpm::KeyHandle>
342+DummyTpm::doCreateKey(const Name& identity, const KeyParams& params)
343+{
344+ unique_ptr<tpm::KeyHandle> m_dummyKeyHandle = make_unique<tpm::DummyKeyHandle>(nullptr);
345+ return m_dummyKeyHandle;
346+}
347+
348+void
349+DummyTpm::doDeleteKey(const Name& keyName)
350+{
351+ throw Error("Not supported");
352+}
353+
354+ConstBufferPtr
355+DummyTpm::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
356+{
357+ throw Error("Not supported");
358+}
359+
360+void
361+DummyTpm::doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen)
362+{
363+ throw Error("Not supported");
364+}
365+
366+std::string
367+DummyTpm::getScheme()
368+{
369+ return DummyTpm::SCHEME;
370+}
371+
372+} // namespace security
373+} // namespace ndn
374diff --git a/ndn-cxx/util/dummy-keychain.hpp b/ndn-cxx/util/dummy-keychain.hpp
375new file mode 100644
376index 0000000..d1432a6
377--- /dev/null
378+++ b/ndn-cxx/util/dummy-keychain.hpp
379@@ -0,0 +1,223 @@
380+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
381+/**
382+ * Copyright (c) 2011-2015 Regents of the University of California.
383+ *
384+ * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
385+ * contributors.
386+ *
387+ * ndnSIM is free software: you can redistribute it and/or modify it under the terms
388+ * of the GNU General Public License as published by the Free Software Foundation,
389+ * either version 3 of the License, or (at your option) any later version.
390+ *
391+ * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
392+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
393+ * PURPOSE. See the GNU General Public License for more details.
394+ *
395+ * You should have received a copy of the GNU General Public License along with
396+ * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
397+ **/
398+
399+#ifndef NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP
400+#define NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP
401+
402+#include <ndn-cxx/security/key-chain.hpp>
403+#include <ndn-cxx/security/security-common.hpp>
404+#include <ndn-cxx/security/pib/pib-impl.hpp>
405+#include <ndn-cxx/security/tpm/back-end.hpp>
406+#include <ndn-cxx/security/transform/private-key.hpp>
407+
408+namespace ndn {
409+namespace security {
410+
411+using pib::PibImpl;
412+using tpm::BackEnd;
413+using tpm::KeyHandle;
414+
415+class DummyPib : public PibImpl
416+{
417+public:
418+ class Error : public PibImpl::Error
419+ {
420+ public:
421+ explicit
422+ Error(const std::string& what)
423+ : PibImpl::Error(what)
424+ {
425+ }
426+ };
427+
428+public:
429+ explicit DummyPib(const std::string& locator);
430+
431+ // TPM management
432+ void
433+ setTpmLocator(const std::string& tpmLocator) override;
434+
435+ std::string
436+ getTpmLocator() const override;
437+
438+ // Identity manangement
439+ bool
440+ hasIdentity(const Name& identityName) const override;
441+
442+ void
443+ addIdentity(const Name& identityName) override;
444+
445+ void
446+ removeIdentity(const Name& identity) override;
447+
448+ void
449+ clearIdentities() override;
450+
451+ std::set<Name>
452+ getIdentities() const override;
453+
454+ void
455+ setDefaultIdentity(const Name& identityName) override;
456+
457+ Name
458+ getDefaultIdentity() const override;
459+
460+ // Key management
461+ bool
462+ hasKey(const Name& keyName) const override;
463+
464+ void
465+ addKey(const Name& identity, const Name& keyName, const uint8_t* key,
466+ size_t keyLen) override;
467+
468+ void
469+ removeKey(const Name& keyName) override;
470+
471+ Buffer
472+ getKeyBits(const Name& keyName) const override;
473+
474+ std::set<Name>
475+ getKeysOfIdentity(const Name& identity) const override;
476+
477+ void
478+ setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) override;
479+
480+ Name
481+ getDefaultKeyOfIdentity(const Name& identity) const override;
482+
483+ // certificate management
484+ bool
485+ hasCertificate(const Name& certName) const override;
486+
487+ void
488+ addCertificate(const v2::Certificate& certificate) override;
489+
490+ void
491+ removeCertificate(const Name& certName) override;
492+
493+ v2::Certificate
494+ getCertificate(const Name& certificateName) const override;
495+
496+ std::set<Name>
497+ getCertificatesOfKey(const Name& keyName) const override;
498+
499+ void
500+ setDefaultCertificateOfKey(const Name& keyName, const Name& certName) override;
501+
502+ v2::Certificate
503+ getDefaultCertificateOfKey(const Name& keyName) const override;
504+
505+ static std::string
506+ getScheme();
507+
508+ static const std::string SCHEME;
509+
510+private:
511+ std::string m_tpmLocator;
512+};
513+
514+//////////////////////////////////////////////////////////////////////////////////////////
515+//////////////////////////////////////////////////////////////////////////////////////////
516+
517+namespace tpm {
518+
519+class DummyKeyHandle : public KeyHandle
520+{
521+public:
522+ explicit
523+ DummyKeyHandle(shared_ptr<transform::PrivateKey> key);
524+
525+private:
526+ ConstBufferPtr
527+ doSign(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size) const final;
528+
529+ bool
530+ doVerify(DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t bufLen,
531+ const uint8_t* sig, size_t sigLen) const final;
532+
533+ ConstBufferPtr
534+ doDecrypt(const uint8_t* cipherText, size_t cipherTextLen) const final;
535+
536+ ConstBufferPtr
537+ doDerivePublicKey() const final;
538+};
539+
540+} // namespace tpm
541+
542+//////////////////////////////////////////////////////////////////////////////////////////
543+//////////////////////////////////////////////////////////////////////////////////////////
544+
545+class DummyTpm : public BackEnd
546+{
547+public:
548+ class Error : public BackEnd::Error
549+ {
550+ public:
551+ explicit
552+ Error(const std::string& what)
553+ : BackEnd::Error(what)
554+ {
555+ }
556+ };
557+
558+public:
559+ explicit DummyTpm(const std::string& locator);
560+
561+ bool
562+ isTerminalMode() const override;
563+
564+ void
565+ setTerminalMode(bool isTerminal) const override;
566+
567+ bool
568+ isTpmLocked() const override;
569+
570+ ConstBufferPtr
571+ sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
572+
573+ static std::string
574+ getScheme();
575+
576+private:
577+ bool
578+ doHasKey(const Name& keyName) const final;
579+
580+ unique_ptr<tpm::KeyHandle>
581+ doGetKeyHandle(const Name& keyName) const final;
582+
583+ unique_ptr<tpm::KeyHandle>
584+ doCreateKey(const Name& identity, const KeyParams& params) final;
585+
586+ void
587+ doDeleteKey(const Name& keyName) final;
588+
589+ ConstBufferPtr
590+ doExportKey(const Name& keyName, const char* pw, size_t pwLen) final;
591+
592+ void
593+ doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) final;
594+
595+public:
596+ static const std::string SCHEME;
597+};
598+
599+} // namespace security
600+} // namespace ndn
601+
602+#endif // NDNSIM_UTILS_DUMMY_KEYCHAIN_HPP