blob: 306e417c1bbfd8cc140f9a95996047238b23700a [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 Yube4150e2014-02-18 13:02:46 -080056 /******************************
57 * From TrustedPlatformModule *
58 ******************************/
Jeff Thompson2747dc02013-10-04 19:11:34 -070059
Yingdi Yu28fd32f2014-01-28 19:03:03 -080060 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080061 setTpmPassword(const uint8_t* password, size_t passwordLength);
62
63 virtual void
64 resetTpmPassword();
65
66 virtual void
67 setInTerminal(bool inTerminal);
68
69 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070070 getInTerminal() const;
Yingdi Yube4150e2014-02-18 13:02:46 -080071
72 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070073 isLocked();
Yingdi Yube4150e2014-02-18 13:02:46 -080074
Yingdi Yu2e57a582014-02-20 23:34:43 -080075 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080076 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
77
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080079 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
80 {
81 generateKeyPairInTpmInternal(keyName, keyType, keySize, false);
82 }
83
84 virtual void
85 deleteKeyPairInTpm(const Name& keyName)
86 {
87 deleteKeyPairInTpmInternal(keyName, false);
88 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080089
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080091 getPublicKeyFromTpm(const Name& keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080093 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 signInTpm(const uint8_t* data, size_t dataLength,
95 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yube4150e2014-02-18 13:02:46 -080096 {
97 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
98 }
Yingdi Yufc40d872014-02-18 12:56:04 -080099
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800101 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700102
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800103 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800104 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700105
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800107 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700108
Jeff Thompson2747dc02013-10-04 19:11:34 -0700109 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu4b752752014-02-18 12:24:03 -0800111
112 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700114
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700116 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700117
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800118protected:
119 /******************************
120 * From TrustedPlatformModule *
121 ******************************/
122 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
137 /******************************
138 * OSX-specifics *
139 ******************************/
140 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700141 generateKeyPairInTpmInternal(const Name& keyName, KeyType keyType, int keySize, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142
Yingdi Yube4150e2014-02-18 13:02:46 -0800143 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700144 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800145
146 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700147 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800148
149 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700150 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700151 const uint8_t* buf, size_t size,
152 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800153
154 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 signInTpmInternal(const uint8_t* data, size_t dataLength,
156 const Name& keyName, DigestAlgorithm digestAlgorithm,
157 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800159private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800160 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800161 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700162};
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700163
Alexander Afanasyev19508852014-01-29 01:01:51 -0800164} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700165
Yingdi Yufc40d872014-02-18 12:56:04 -0800166#endif // NDN_SECURITY_SEC_TPM_OSX_HPP