blob: 5ed93d5b717af3ebdb1ade70812d871e123cc3b9 [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 Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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 Yu41546342014-11-30 23:37:53 -080050 explicit
51 SecTpmOsx(const std::string& location = "");
Jeff Thompson2747dc02013-10-04 19:11:34 -070052
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080054 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070055
Yingdi Yu7036ce22014-06-19 18:53:37 -070056 // Following methods are inherited from SecTpm
Yingdi Yu28fd32f2014-01-28 19:03:03 -080057 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080058 setTpmPassword(const uint8_t* password, size_t passwordLength);
59
60 virtual void
61 resetTpmPassword();
62
63 virtual void
64 setInTerminal(bool inTerminal);
65
66 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070067 getInTerminal() const;
Yingdi Yube4150e2014-02-18 13:02:46 -080068
69 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070070 isLocked();
Yingdi Yube4150e2014-02-18 13:02:46 -080071
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080073 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
74
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -070076 generateKeyPairInTpm(const Name& keyName, const KeyParams& params)
Yingdi Yube4150e2014-02-18 13:02:46 -080077 {
Yingdi Yu7036ce22014-06-19 18:53:37 -070078 generateKeyPairInTpmInternal(keyName, params, false);
Yingdi Yube4150e2014-02-18 13:02:46 -080079 }
80
81 virtual void
82 deleteKeyPairInTpm(const Name& keyName)
83 {
84 deleteKeyPairInTpmInternal(keyName, false);
85 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080086
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080088 getPublicKeyFromTpm(const Name& keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080090 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070091 signInTpm(const uint8_t* data, size_t dataLength,
92 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yube4150e2014-02-18 13:02:46 -080093 {
94 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
95 }
Yingdi Yufc40d872014-02-18 12:56:04 -080096
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080098 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070099
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800100 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800101 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700102
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103 virtual void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700104 generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700105
Jeff Thompson2747dc02013-10-04 19:11:34 -0700106 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu4b752752014-02-18 12:24:03 -0800108
109 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700111
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700113 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700114
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800115protected:
Yingdi Yu7036ce22014-06-19 18:53:37 -0700116 // Following methods are inherited from SecTpm
Yingdi Yu41546342014-11-30 23:37:53 -0800117 virtual std::string
118 getScheme();
119
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800120 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700121 exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yube4150e2014-02-18 13:02:46 -0800122 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700123 return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800124 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700125
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800126 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700127 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yube4150e2014-02-18 13:02:46 -0800128 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700129 return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800130 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800131
132 virtual bool
133 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800134
Yingdi Yu7036ce22014-06-19 18:53:37 -0700135 // Following methods are OSX-specific
Yingdi Yube4150e2014-02-18 13:02:46 -0800136 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700137 generateKeyPairInTpmInternal(const Name& keyName, const KeyParams& params, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138
Yingdi Yube4150e2014-02-18 13:02:46 -0800139 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700140 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800141
142 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700143 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800144
145 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700146 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700147 const uint8_t* buf, size_t size,
148 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800149
150 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700151 signInTpmInternal(const uint8_t* data, size_t dataLength,
152 const Name& keyName, DigestAlgorithm digestAlgorithm,
153 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154
Yingdi Yu41546342014-11-30 23:37:53 -0800155public:
156 static const std::string SCHEME;
157
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800158private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800159 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800160 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700161};
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700162
Alexander Afanasyev19508852014-01-29 01:01:51 -0800163} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700164
Yingdi Yufc40d872014-02-18 12:56:04 -0800165#endif // NDN_SECURITY_SEC_TPM_OSX_HPP