blob: f7d9959784a39f1766d9efe11e89ad5e6f9f25b8 [file] [log] [blame]
Jeff Thompson2747dc02013-10-04 19:11:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson2747dc02013-10-04 19:11:34 -070013 */
14
Yingdi Yufc40d872014-02-18 12:56:04 -080015#ifndef NDN_SECURITY_SEC_TPM_OSX_HPP
16#define NDN_SECURITY_SEC_TPM_OSX_HPP
Jeff Thompson2747dc02013-10-04 19:11:34 -070017
Yingdi Yu4f324632014-01-15 18:10:03 -080018#include "../common.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070019
20#ifndef NDN_CXX_HAVE_OSX_SECURITY
21#error "This files should not be compiled ..."
22#endif
23
Yingdi Yu87581582014-01-14 14:28:39 -080024#include "sec-tpm.hpp"
Jeff Thompson2747dc02013-10-04 19:11:34 -070025
Yingdi Yufc40d872014-02-18 12:56:04 -080026namespace ndn {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070028class SecTpmOsx : public SecTpm
29{
Jeff Thompson2747dc02013-10-04 19:11:34 -070030public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 class Error : public SecTpm::Error
32 {
33 public:
34 explicit
35 Error(const std::string& what)
36 : SecTpm::Error(what)
37 {
38 }
39 };
Yingdi Yu87581582014-01-14 14:28:39 -080040
Yingdi Yu2b2b4792014-02-04 16:27:07 -080041 SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070042
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080044 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070045
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080046
Yingdi Yube4150e2014-02-18 13:02:46 -080047 /******************************
48 * From TrustedPlatformModule *
49 ******************************/
Jeff Thompson2747dc02013-10-04 19:11:34 -070050
Yingdi Yu28fd32f2014-01-28 19:03:03 -080051 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080052 setTpmPassword(const uint8_t* password, size_t passwordLength);
53
54 virtual void
55 resetTpmPassword();
56
57 virtual void
58 setInTerminal(bool inTerminal);
59
60 virtual bool
61 getInTerminal();
62
63 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070064 isLocked();
Yingdi Yube4150e2014-02-18 13:02:46 -080065
Yingdi Yu2e57a582014-02-20 23:34:43 -080066 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080067 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
68
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080070 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
71 {
72 generateKeyPairInTpmInternal(keyName, keyType, keySize, false);
73 }
74
75 virtual void
76 deleteKeyPairInTpm(const Name& keyName)
77 {
78 deleteKeyPairInTpmInternal(keyName, false);
79 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080080
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080082 getPublicKeyFromTpm(const Name& keyName);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080084 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070085 signInTpm(const uint8_t* data, size_t dataLength,
86 const Name& keyName, DigestAlgorithm digestAlgorithm)
Yingdi Yube4150e2014-02-18 13:02:46 -080087 {
88 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
89 }
Yingdi Yufc40d872014-02-18 12:56:04 -080090
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080092 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070093
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080094 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080095 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070096
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080098 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson2747dc02013-10-04 19:11:34 -070099
Jeff Thompson2747dc02013-10-04 19:11:34 -0700100 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700101 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu4b752752014-02-18 12:24:03 -0800102
103 virtual bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700105
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700107 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700108
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800109protected:
110 /******************************
111 * From TrustedPlatformModule *
112 ******************************/
113 virtual ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700114 exportPrivateKeyPkcs8FromTpm(const Name& keyName)
Yingdi Yube4150e2014-02-18 13:02:46 -0800115 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700116 return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800117 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700118
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800119 virtual bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700120 importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
Yingdi Yube4150e2014-02-18 13:02:46 -0800121 {
Yingdi Yu5e96e002014-04-23 18:32:15 -0700122 return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
Yingdi Yube4150e2014-02-18 13:02:46 -0800123 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800124
125 virtual bool
126 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800127
128 /******************************
129 * OSX-specifics *
130 ******************************/
131 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700132 generateKeyPairInTpmInternal(const Name& keyName, KeyType keyType, int keySize, bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133
Yingdi Yube4150e2014-02-18 13:02:46 -0800134 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700135 deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800136
137 ConstBufferPtr
Yingdi Yu5e96e002014-04-23 18:32:15 -0700138 exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800139
140 bool
Yingdi Yu5e96e002014-04-23 18:32:15 -0700141 importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700142 const uint8_t* buf, size_t size,
143 bool needRetry);
Yingdi Yube4150e2014-02-18 13:02:46 -0800144
145 Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700146 signInTpmInternal(const uint8_t* data, size_t dataLength,
147 const Name& keyName, DigestAlgorithm digestAlgorithm,
148 bool needRetry);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800150private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800151 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800152 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700153};
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700154
Alexander Afanasyev19508852014-01-29 01:01:51 -0800155} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700156
Yingdi Yufc40d872014-02-18 12:56:04 -0800157#endif // NDN_SECURITY_SEC_TPM_OSX_HPP