blob: 605d7c853ec8c95a6c711ef01079e8e09e271029 [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.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_MEMORY_HPP
9#define NDN_SECURITY_SEC_TPM_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080012#include "sec-tpm.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013
14struct rsa_st;
15
16namespace ndn {
17
18/**
19 * MemoryPrivateKeyStorage extends PrivateKeyStorage to implement a simple in-memory private key store. You should
20 * initialize by calling setKeyPairForKeyName.
21 */
Yingdi Yu87581582014-01-14 14:28:39 -080022class SecTpmMemory : public SecTpm {
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070024 class Error : public SecTpm::Error
25 {
26 public:
27 explicit
28 Error(const std::string& what)
29 : SecTpm::Error(what)
30 {
31 }
32 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080033
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070034 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080035 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070036
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080037 /******************************
38 * From TrustedPlatformModule *
39 ******************************/
40
Yingdi Yube4150e2014-02-18 13:02:46 -080041 virtual void
42 setTpmPassword(const uint8_t* password, size_t passwordLength)
43 {}
44
45 virtual void
46 resetTpmPassword()
47 {}
48
49 virtual void
50 setInTerminal(bool inTerminal)
51 {
52 m_inTerminal = inTerminal;
53 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054
Yingdi Yube4150e2014-02-18 13:02:46 -080055 virtual bool
56 getInTerminal()
57 {
58 return m_inTerminal;
59 }
60
61 virtual bool
62 locked()
63 {
64 return true;
65 }
66
Yingdi Yu2e57a582014-02-20 23:34:43 -080067 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080068 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080069 {
70 return !locked();
71 }
Yingdi Yube4150e2014-02-18 13:02:46 -080072
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070073 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080074 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070076 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080077 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080078
Yingdi Yu28fd32f2014-01-28 19:03:03 -080079 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080081
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082 virtual Block
83 signInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
84
85 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080086 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070087
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080088 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080089 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070090
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070091 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080092 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070093
Yingdi Yu4b752752014-02-18 12:24:03 -080094 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080096
97 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080098 generateRandomBlock(uint8_t* res, size_t size);
99
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700100 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -0800101 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
102 {}
103
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800104 /******************************
105 * SecTpmMemory specific *
106 ******************************/
107
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700108 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800109 * @brief Set the public and private key for the keyName.
110 *
111 * @param keyName The key name.
112 * @param publicKeyDer The public key DER byte array.
113 * @param publicKeyDerLength The length of publicKeyDer.
114 * @param privateKeyDer The private key DER byte array.
115 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700116 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117 void setKeyPairForKeyName(const Name& keyName,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700118 uint8_t* publicKeyDer, size_t publicKeyDerLength,
119 uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800120
121protected:
122 /******************************
123 * From TrustedPlatformModule *
124 ******************************/
125 virtual ConstBufferPtr
126 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
127
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700128 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800129 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700130
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800131 virtual bool
132 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700133
134
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700135private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800136 class RsaPrivateKey;
137
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
139 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700140
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800141 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
142 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800143
144 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700145};
146
Yingdi Yufc40d872014-02-18 12:56:04 -0800147} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148
Yingdi Yufc40d872014-02-18 12:56:04 -0800149#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP