blob: fca8ddafd4ca68ca31a97031d366d430c5b03611 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_SEC_TPM_OSX_HPP
25#define NDN_SECURITY_SEC_TPM_OSX_HPP
Jeff Thompson2747dc02013-10-04 19:11:34 -070026
Yingdi Yu4f324632014-01-15 18:10:03 -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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070036
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070037class SecTpmOsx : public SecTpm
38{
Jeff Thompson2747dc02013-10-04 19:11:34 -070039public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 class Error : public SecTpm::Error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : SecTpm::Error(what)
46 {
47 }
48 };
Yingdi Yu87581582014-01-14 14:28:39 -080049
Yingdi Yu2b2b4792014-02-04 16:27:07 -080050 SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070051
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080053 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070054
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080055
Yingdi Yu7036ce22014-06-19 18:53:37 -070056 // Following methods are inherited from SecTpm
Jeff Thompson2747dc02013-10-04 19:11:34 -070057
Yingdi Yu28fd32f2014-01-28 19:03:03 -080058 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080059 setTpmPassword(const uint8_t* password, size_t passwordLength);
60
61 virtual void
62 resetTpmPassword();
63
64 virtual void
65 setInTerminal(bool inTerminal);
66
67 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070068 getInTerminal() const;
Yingdi Yube4150e2014-02-18 13:02:46 -080069
70 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070071 isLocked();
Yingdi Yube4150e2014-02-18 13:02:46 -080072
Yingdi Yu2e57a582014-02-20 23:34:43 -080073 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080074 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
75
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -070077 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yube4150e2014-02-18 13:02:46 -080078 {
Yingdi Yu7036ce22014-06-19 18:53:37 -070079 generateKeyPairInTpmInternal(keyName, params, false);
Yingdi Yube4150e2014-02-18 13:02:46 -080080 }
81
82 virtual void
83 deleteKeyPairInTpm(const Name& keyName)
84 {
85 deleteKeyPairInTpmInternal(keyName, false);
86 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080087
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080089 getPublicKeyFromTpm(const Name& keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080091 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 signInTpm(const uint8_t* data, size_t dataLength,
93 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yube4150e2014-02-18 13:02:46 -080094 {
95 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
96 }
Yingdi Yufc40d872014-02-18 12:56:04 -080097
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080099 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700100
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800101 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800102 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700103
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700105 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700106
Jeff Thompson2747dc02013-10-04 19:11:34 -0700107 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu4b752752014-02-18 12:24:03 -0800109
110 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700112
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700114 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700115
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800116protected:
Yingdi Yu7036ce22014-06-19 18:53:37 -0700117 // Following methods are inherited from SecTpm
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800118 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700119 exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yube4150e2014-02-18 13:02:46 -0800120 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700121 return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800122 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700123
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800124 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700125 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yube4150e2014-02-18 13:02:46 -0800126 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700127 return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800128 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800129
130 virtual bool
131 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800132
Yingdi Yu7036ce22014-06-19 18:53:37 -0700133 // Following methods are OSX-specific
Yingdi Yube4150e2014-02-18 13:02:46 -0800134 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700135 generateKeyPairInTpmInternal(const Name& keyName, const KeyParams& params, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136
Yingdi Yube4150e2014-02-18 13:02:46 -0800137 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700138 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800139
140 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700141 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800142
143 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700144 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700145 const uint8_t* buf, size_t size,
146 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800147
148 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 signInTpmInternal(const uint8_t* data, size_t dataLength,
150 const Name& keyName, DigestAlgorithm digestAlgorithm,
151 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800154 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800155 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700156};
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700157
Alexander Afanasyev19508852014-01-29 01:01:51 -0800158} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700159
Yingdi Yufc40d872014-02-18 12:56:04 -0800160#endif // NDN_SECURITY_SEC_TPM_OSX_HPP