blob: 251df2422f44846a4a0404952cb02ae8376ddb7c [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 Yu87581582014-01-14 14:28:39 -08008#ifndef NDN_SEC_TPM_OSX_HPP
9#define NDN_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
Jeff Thompson2747dc02013-10-04 19:11:34 -070014namespace ndn
15{
16
Yingdi Yu87581582014-01-14 14:28:39 -080017class SecTpmOsx : public SecTpm {
Jeff Thompson2747dc02013-10-04 19:11:34 -070018public:
Yingdi Yu2b2b4792014-02-04 16:27:07 -080019 struct Error : public SecTpm::Error { Error(const std::string& what) : SecTpm::Error(what) {} };
Yingdi Yu87581582014-01-14 14:28:39 -080020
Jeff Thompson2747dc02013-10-04 19:11:34 -070021 /**
Yingdi Yu87581582014-01-14 14:28:39 -080022 * constructor of OSXKeyChainTpm
Jeff Thompson2747dc02013-10-04 19:11:34 -070023 * @param keychainName the name of keychain
24 */
Yingdi Yu2b2b4792014-02-04 16:27:07 -080025 SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070026
27 /**
Yingdi Yu87581582014-01-14 14:28:39 -080028 * destructor of OSXKeyChainTpm
Jeff Thompson2747dc02013-10-04 19:11:34 -070029 */
30 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080031 ~SecTpmOsx();
Jeff Thompson2747dc02013-10-04 19:11:34 -070032
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080033
Yingdi Yu87581582014-01-14 14:28:39 -080034 // From TrustedPlatformModule
Jeff Thompson2747dc02013-10-04 19:11:34 -070035 virtual void
Yingdi Yu3c5887c2014-01-21 18:19:49 -080036 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson2747dc02013-10-04 19:11:34 -070037
Yingdi Yu28fd32f2014-01-28 19:03:03 -080038 /**
39 * Delete a key pair of asymmetric keys.
40 * @param keyName The name of the key pair.
41 */
42 virtual void
Yingdi Yu2b2b4792014-02-04 16:27:07 -080043 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080044
Jeff Thompson2747dc02013-10-04 19:11:34 -070045 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080046 getPublicKeyFromTpm(const Name& keyName);
Jeff Thompson2747dc02013-10-04 19:11:34 -070047
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080048 virtual Block
Yingdi Yu2b2b4792014-02-04 16:27:07 -080049 signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080050
Jeff Thompson2747dc02013-10-04 19:11:34 -070051 /**
52 * Decrypt data.
53 * @param keyName The name of the decrypting key.
54 * @param data The byte to be decrypted.
55 * @param dataLength the length of data.
56 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
57 * @return The decrypted data.
58 */
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080059 virtual ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -080060 decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070061
62 /**
63 * Encrypt data.
64 * @param keyName The name of the encrypting key.
65 * @param data The byte to be encrypted.
66 * @param dataLength the length of data.
67 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
68 * @return The encrypted data.
69 */
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080070 virtual ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -080071 encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson2747dc02013-10-04 19:11:34 -070072
73 /**
74 * Generate a symmetric key.
75 * @param keyName The name of the key.
76 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
77 * @param keySize The size of the key.
78 */
79 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080080 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson2747dc02013-10-04 19:11:34 -070081
82 /**
83 * Check if a particular key exists.
84 * @param keyName The name of the key.
85 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
86 * @return True if the key exists, otherwise false.
87 */
88 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080089 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
90
91 virtual bool
92 generateRandomBlock(uint8_t* res, size_t size);
Jeff Thompson2747dc02013-10-04 19:11:34 -070093
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080094
95 ////////////////////////////////////////////////////////////////////////////////////
96 // OSX-specifics
97 ////////////////////////////////////////////////////////////////////////////////////
98
Jeff Thompson2747dc02013-10-04 19:11:34 -070099 /**
100 * configure ACL of a particular key
101 * @param keyName the name of key
102 * @param keyClass the class of key, e.g. Private Key
103 * @param acl the new acl of the key
104 * @param appPath the absolute path to the application
105 * @returns true if setting succeeds
106 */
107 bool
Yingdi Yu2b2b4792014-02-04 16:27:07 -0800108 setACL(const Name& keyName, KeyClass keyClass, int acl, const std::string& appPath);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700109
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800110protected:
111 /******************************
112 * From TrustedPlatformModule *
113 ******************************/
114 virtual ConstBufferPtr
115 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
Jeff Thompson2747dc02013-10-04 19:11:34 -0700116
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117 virtual bool
118 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
119
120 virtual bool
121 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
122
123private:
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800124 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800125 shared_ptr<Impl> m_impl;
Jeff Thompson2747dc02013-10-04 19:11:34 -0700126};
127
Alexander Afanasyev19508852014-01-29 01:01:51 -0800128} // namespace ndn
Jeff Thompson2747dc02013-10-04 19:11:34 -0700129
Alexander Afanasyev19508852014-01-29 01:01:51 -0800130#endif // NDN_SEC_TPM_OSX_HPP