blob: e18d3bc5d6f41bb2cae06ec684f8068c4fd2cb04 [file] [log] [blame]
Jeff Thompson2747dc02013-10-04 19:11:34 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Yingdi Yufc40d872014-02-18 12:56:04 -08008#ifndef NDN_SECURITY_SEC_TPM_OSX_HPP
9#define NDN_SECURITY_SEC_TPM_OSX_HPP
Jeff Thompson2747dc02013-10-04 19:11:34 -070010
Yingdi Yu4f324632014-01-15 18:10:03 -080011#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080012#include "sec-tpm.hpp"
Jeff Thompson2747dc02013-10-04 19:11:34 -070013
Yingdi Yufc40d872014-02-18 12:56:04 -080014namespace ndn {
Jeff Thompson2747dc02013-10-04 19:11:34 -070015
Yingdi Yu87581582014-01-14 14:28:39 -080016class SecTpmOsx : public SecTpm {
Jeff Thompson2747dc02013-10-04 19:11:34 -070017public:
Yingdi Yu2b2b4792014-02-04 16:27:07 -080018 struct Error : public SecTpm::Error { Error(const std::string& what) : SecTpm::Error(what) {} };
Yingdi Yu87581582014-01-14 14:28:39 -080019
Yingdi Yu2b2b4792014-02-04 16:27:07 -080020 SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070021
Jeff Thompson2747dc02013-10-04 19:11:34 -070022 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080023 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070024
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080025
Yingdi Yube4150e2014-02-18 13:02:46 -080026 /******************************
27 * From TrustedPlatformModule *
28 ******************************/
Jeff Thompson2747dc02013-10-04 19:11:34 -070029
Yingdi Yu28fd32f2014-01-28 19:03:03 -080030 virtual void
Yingdi Yube4150e2014-02-18 13:02:46 -080031 setTpmPassword(const uint8_t* password, size_t passwordLength);
32
33 virtual void
34 resetTpmPassword();
35
36 virtual void
37 setInTerminal(bool inTerminal);
38
39 virtual bool
40 getInTerminal();
41
42 virtual bool
43 locked();
44
45 virtual void
46 unlockTpm(const char* password, size_t passwordLength, bool usePassword);
47
48 virtual void
49 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
50 {
51 generateKeyPairInTpmInternal(keyName, keyType, keySize, false);
52 }
53
54 virtual void
55 deleteKeyPairInTpm(const Name& keyName)
56 {
57 deleteKeyPairInTpmInternal(keyName, false);
58 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080059
Jeff Thompson2747dc02013-10-04 19:11:34 -070060 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080061 getPublicKeyFromTpm(const Name& keyName);
Jeff Thompson2747dc02013-10-04 19:11:34 -070062
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080063 virtual Block
Yingdi Yube4150e2014-02-18 13:02:46 -080064 signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
65 {
66 return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, false);
67 }
Yingdi Yufc40d872014-02-18 12:56:04 -080068
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080069 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080070 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070071
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080072 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080073 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070074
Jeff Thompson2747dc02013-10-04 19:11:34 -070075 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080076 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson2747dc02013-10-04 19:11:34 -070077
Jeff Thompson2747dc02013-10-04 19:11:34 -070078 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080079 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
80
81 virtual bool
82 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -070083
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080084
85 ////////////////////////////////////////////////////////////////////////////////////
86 // OSX-specifics
87 ////////////////////////////////////////////////////////////////////////////////////
88
Jeff Thompson2747dc02013-10-04 19:11:34 -070089 /**
Yingdi Yufc40d872014-02-18 12:56:04 -080090 * @brief Configure ACL of a particular key.
91 *
Jeff Thompson2747dc02013-10-04 19:11:34 -070092 * @param keyName the name of key
93 * @param keyClass the class of key, e.g. Private Key
94 * @param acl the new acl of the key
95 * @param appPath the absolute path to the application
96 * @returns true if setting succeeds
97 */
98 bool
Yingdi Yu2b2b4792014-02-04 16:27:07 -080099 setACL(const Name& keyName, KeyClass keyClass, int acl, const std::string& appPath);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700100
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800101protected:
102 /******************************
103 * From TrustedPlatformModule *
104 ******************************/
105 virtual ConstBufferPtr
Yingdi Yube4150e2014-02-18 13:02:46 -0800106 exportPrivateKeyPkcs1FromTpm(const Name& keyName)
107 {
108 return exportPrivateKeyPkcs1FromTpmInternal(keyName, false);
109 }
Jeff Thompson2747dc02013-10-04 19:11:34 -0700110
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800111 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -0800112 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
113 {
114 return importPrivateKeyPkcs1IntoTpmInternal(keyName, buf, size, false);
115 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800116
117 virtual bool
118 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yube4150e2014-02-18 13:02:46 -0800119
120 /******************************
121 * OSX-specifics *
122 ******************************/
123 void
124 generateKeyPairInTpmInternal(const Name & keyName, KeyType keyType, int keySize, bool retry);
125
126 void
127 deleteKeyPairInTpmInternal(const Name &keyName, bool retry);
128
129 ConstBufferPtr
130 exportPrivateKeyPkcs1FromTpmInternal(const Name& keyName, bool retry);
131
132 bool
133 importPrivateKeyPkcs1IntoTpmInternal(const Name& keyName, const uint8_t* buf, size_t size, bool retry);
134
135 Block
136 signInTpmInternal(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm, bool retry);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800137
138private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800139 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800140 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700141};
142
Alexander Afanasyev19508852014-01-29 01:01:51 -0800143} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700144
Yingdi Yufc40d872014-02-18 12:56:04 -0800145#endif // NDN_SECURITY_SEC_TPM_OSX_HPP