blob: 76415141c9e9e2f874285adc7b2c45a465bd494b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson2747dc02013-10-04 19:11:34 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson2747dc02013-10-04 19:11:34 -070022 */
23
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024#ifndef NDN_SECURITY_V1_SEC_TPM_OSX_HPP
25#define NDN_SECURITY_V1_SEC_TPM_OSX_HPP
Jeff Thompson2747dc02013-10-04 19:11:34 -070026
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080027#include "../../common.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070028
29#ifndef NDN_CXX_HAVE_OSX_SECURITY
30#error "This files should not be compiled ..."
31#endif
32
Yingdi Yu87581582014-01-14 14:28:39 -080033#include "sec-tpm.hpp"
Jeff Thompson2747dc02013-10-04 19:11:34 -070034
Yingdi Yufc40d872014-02-18 12:56:04 -080035namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070036namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080037namespace v1 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070039class SecTpmOsx : public SecTpm
40{
Jeff Thompson2747dc02013-10-04 19:11:34 -070041public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042 class Error : public SecTpm::Error
43 {
44 public:
45 explicit
46 Error(const std::string& what)
47 : SecTpm::Error(what)
48 {
49 }
50 };
Yingdi Yu87581582014-01-14 14:28:39 -080051
Yingdi Yu41546342014-11-30 23:37:53 -080052 explicit
53 SecTpmOsx(const std::string& location = "");
Jeff Thompson2747dc02013-10-04 19:11:34 -070054
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070055 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080056 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070057
Yingdi Yu7036ce22014-06-19 18:53:37 -070058 // Following methods are inherited from SecTpm
Yingdi Yu28fd32f2014-01-28 19:03:03 -080059 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080060 setTpmPassword(const uint8_t* password, size_t passwordLength);
61
62 virtual void
63 resetTpmPassword();
64
65 virtual void
66 setInTerminal(bool inTerminal);
67
68 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070069 getInTerminal() const;
Yingdi Yube4150e2014-02-18 13:02:46 -080070
71 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070072 isLocked();
Yingdi Yube4150e2014-02-18 13:02:46 -080073
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080075 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
76
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -070078 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yube4150e2014-02-18 13:02:46 -080079 {
Yingdi Yu7036ce22014-06-19 18:53:37 -070080 generateKeyPairInTpmInternal(keyName, params, false);
Yingdi Yube4150e2014-02-18 13:02:46 -080081 }
82
83 virtual void
84 deleteKeyPairInTpm(const Name& keyName)
85 {
86 deleteKeyPairInTpmInternal(keyName, false);
87 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080088
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070089 virtual shared_ptr<v1::PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080090 getPublicKeyFromTpm(const Name& keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080092 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070093 signInTpm(const uint8_t* data, size_t dataLength,
94 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yube4150e2014-02-18 13:02:46 -080095 {
96 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
97 }
Yingdi Yufc40d872014-02-18 12:56:04 -080098
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800100 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700101
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800102 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800103 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700104
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700105 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700106 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700107
Jeff Thompson2747dc02013-10-04 19:11:34 -0700108 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu4b752752014-02-18 12:24:03 -0800110
111 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700113
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700115 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700116
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117protected:
Yingdi Yu7036ce22014-06-19 18:53:37 -0700118 // Following methods are inherited from SecTpm
Yingdi Yu41546342014-11-30 23:37:53 -0800119 virtual std::string
120 getScheme();
121
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800122 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700123 exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yube4150e2014-02-18 13:02:46 -0800124 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700125 return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800126 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700127
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800128 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700129 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yube4150e2014-02-18 13:02:46 -0800130 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700131 return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800132 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800133
134 virtual bool
135 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800136
Yingdi Yu7036ce22014-06-19 18:53:37 -0700137 // Following methods are OSX-specific
Yingdi Yube4150e2014-02-18 13:02:46 -0800138 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700139 generateKeyPairInTpmInternal(const Name& keyName, const KeyParams& params, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140
Yingdi Yube4150e2014-02-18 13:02:46 -0800141 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700142 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800143
144 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700145 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800146
147 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700148 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 const uint8_t* buf, size_t size,
150 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800151
152 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700153 signInTpmInternal(const uint8_t* data, size_t dataLength,
154 const Name& keyName, DigestAlgorithm digestAlgorithm,
155 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156
Yingdi Yu41546342014-11-30 23:37:53 -0800157public:
158 static const std::string SCHEME;
159
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800160private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800161 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800162 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700163};
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700164
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800165} // namespace v1
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700166} // namespace security
Alexander Afanasyev19508852014-01-29 01:01:51 -0800167} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700168
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800169#endif // NDN_SECURITY_V1_SEC_TPM_OSX_HPP