blob: 61f7f68a21336b29c317390b0e1a4781f4503b84 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -04002/*
Yingdi Yufe4733a2015-10-22 14:24:12 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#ifndef NDN_SECURITY_TPM_BACK_END_OSX_HPP
23#define NDN_SECURITY_TPM_BACK_END_OSX_HPP
24
25#include "back-end.hpp"
Junxiao Shi0b1b4672017-07-02 22:02:31 -070026#include "key-ref-osx.hpp"
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070027
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050028#ifndef NDN_CXX_HAVE_OSX_FRAMEWORKS
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070029#error "This file should not be compiled ..."
30#endif
31
32namespace ndn {
33namespace security {
34namespace tpm {
35
36/**
Yingdi Yufe4733a2015-10-22 14:24:12 -070037 * @brief The back-end implementation of TPM based on macOS Keychain Services.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070038 */
39class BackEndOsx : public BackEnd
40{
41public:
42 class Error : public BackEnd::Error
43 {
44 public:
45 explicit
46 Error(const std::string& what)
47 : BackEnd::Error(what)
48 {
49 }
50 };
51
52public:
Yingdi Yufe4733a2015-10-22 14:24:12 -070053 /**
54 * @brief Create TPM backed based on macOS KeyChain service
55 * @param location Not used (required by the TPM-registration interface)
56 */
57 explicit
58 BackEndOsx(const std::string& location = "");
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070059
60 ~BackEndOsx() override;
61
Yingdi Yufe4733a2015-10-22 14:24:12 -070062 static const std::string&
63 getScheme();
64
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070065public: // management
Yingdi Yufe4733a2015-10-22 14:24:12 -070066 bool
67 isTerminalMode() const final;
68
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070069 void
Yingdi Yufe4733a2015-10-22 14:24:12 -070070 setTerminalMode(bool isTerminal) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070071
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070072 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070073 isTpmLocked() const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070074
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070075 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070076 unlockTpm(const char* pw, size_t pwLen) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070077
78public: // crypto transformation
79 /**
80 * @brief Sign @p buf with @p key using @p digestAlgorithm.
81 */
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040082 static ConstBufferPtr
83 sign(const KeyRefOsx& key, DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070084
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040085 static ConstBufferPtr
86 decrypt(const KeyRefOsx& key, const uint8_t* cipherText, size_t cipherSize);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070087
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040088 static ConstBufferPtr
89 derivePublicKey(const KeyRefOsx& key);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070090
91private: // inherited from tpm::BackEnd
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070092 /**
93 * @return True if a key with name @p keyName exists in TPM.
94 */
95 bool
96 doHasKey(const Name& keyName) const final;
97
98 /**
99 * @return The handle of a key with name @p keyName, or nullptr if the key does not exist
100 */
101 unique_ptr<KeyHandle>
102 doGetKeyHandle(const Name& keyName) const final;
103
104 /**
105 * @brief Create key for @p identityName according to @p params.
106 *
107 * The created key is named as: /<identityName>/[keyId]/KEY
108 * The key name is set in the returned KeyHandle.
109 *
110 * @return The handle of the created key.
111 */
112 unique_ptr<KeyHandle>
113 doCreateKey(const Name& identityName, const KeyParams& params) final;
114
115 /**
116 * @brief Delete a key with name @p keyName.
117 *
Yingdi Yufe4733a2015-10-22 14:24:12 -0700118 * @throw Error the deletion failed
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700119 */
120 void
121 doDeleteKey(const Name& keyName) final;
122
123 /**
124 * @return A private key with name @p keyName in encrypted PKCS #8 format using password @p pw
Yingdi Yufe4733a2015-10-22 14:24:12 -0700125 * @throw Error the key cannot be exported, e.g., not enough privilege
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700126 */
127 ConstBufferPtr
128 doExportKey(const Name& keyName, const char* pw, size_t pwLen) final;
129
130 /**
131 * @brief Import a private key in encrypted PKCS #8 format
132 *
133 * @param keyName The name of imported private key
134 * @param buf Pointer to the key in encrypted PKCS #8 format
135 * @param size The size of the key in encrypted PKCS #8 format
136 * @param pw The password to decrypt the private key
137 * @param pwLen The length of the password
Yingdi Yufe4733a2015-10-22 14:24:12 -0700138 * @throw Error import fails
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700139 */
140 void
141 doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) final;
142
143private:
144 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400145 const unique_ptr<Impl> m_impl;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700146};
147
148} // namespace tpm
149} // namespace security
150} // namespace ndn
151
152#endif // NDN_SECURITY_TPM_BACK_END_OSX_HPP