blob: 1713f0666a819e35d643e13ec2f8ec475d580685 [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 Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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 Afanasyev2fa59392016-07-29 17:24:23 -070036namespace security {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070038class SecTpmOsx : public SecTpm
39{
Jeff Thompson2747dc02013-10-04 19:11:34 -070040public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 class Error : public SecTpm::Error
42 {
43 public:
44 explicit
45 Error(const std::string& what)
46 : SecTpm::Error(what)
47 {
48 }
49 };
Yingdi Yu87581582014-01-14 14:28:39 -080050
Yingdi Yu41546342014-11-30 23:37:53 -080051 explicit
52 SecTpmOsx(const std::string& location = "");
Jeff Thompson2747dc02013-10-04 19:11:34 -070053
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080055 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070056
Yingdi Yu7036ce22014-06-19 18:53:37 -070057 // Following methods are inherited from SecTpm
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 Afanasyev2fa59392016-07-29 17:24:23 -070088 virtual shared_ptr<v1::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 Yu41546342014-11-30 23:37:53 -0800118 virtual std::string
119 getScheme();
120
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800121 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700122 exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yube4150e2014-02-18 13:02:46 -0800123 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700124 return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800125 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700126
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800127 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700128 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yube4150e2014-02-18 13:02:46 -0800129 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700130 return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800131 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800132
133 virtual bool
134 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800135
Yingdi Yu7036ce22014-06-19 18:53:37 -0700136 // Following methods are OSX-specific
Yingdi Yube4150e2014-02-18 13:02:46 -0800137 void
Yingdi Yu7036ce22014-06-19 18:53:37 -0700138 generateKeyPairInTpmInternal(const Name& keyName, const KeyParams& params, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139
Yingdi Yube4150e2014-02-18 13:02:46 -0800140 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700141 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800142
143 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700144 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800145
146 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700147 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700148 const uint8_t* buf, size_t size,
149 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800150
151 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700152 signInTpmInternal(const uint8_t* data, size_t dataLength,
153 const Name& keyName, DigestAlgorithm digestAlgorithm,
154 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155
Yingdi Yu41546342014-11-30 23:37:53 -0800156public:
157 static const std::string SCHEME;
158
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 Afanasyev2fa59392016-07-29 17:24:23 -0700164} // namespace security
165
166using security::SecTpmOsx;
167
Alexander Afanasyev19508852014-01-29 01:01:51 -0800168} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700169
Yingdi Yufc40d872014-02-18 12:56:04 -0800170#endif // NDN_SECURITY_SEC_TPM_OSX_HPP