blob: 45e3bd37c5912fa9851b5db28c64144b56e73e5d [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -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 Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013 */
14
Yingdi Yufc40d872014-02-18 12:56:04 -080015#ifndef NDN_SECURITY_SEC_TPM_MEMORY_HPP
16#define NDN_SECURITY_SEC_TPM_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070017
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080018#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080019#include "sec-tpm.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070020
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070021#include <map>
22
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023struct rsa_st;
24
25namespace ndn {
26
27/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070028 * @brief SecTpmMemory implements a simple in-memory TPM.
29 *
30 * You should initialize by calling setKeyPairForKeyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070031 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070032class SecTpmMemory : public SecTpm
33{
Jeff Thompson6c314bc2013-09-23 18:09:38 -070034public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035 class Error : public SecTpm::Error
36 {
37 public:
38 explicit
39 Error(const std::string& what)
40 : SecTpm::Error(what)
41 {
42 }
43 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080044
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070045 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080046 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070047
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080048 /******************************
49 * From TrustedPlatformModule *
50 ******************************/
51
Yingdi Yube4150e2014-02-18 13:02:46 -080052 virtual void
53 setTpmPassword(const uint8_t* password, size_t passwordLength)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 {
55 }
Yingdi Yube4150e2014-02-18 13:02:46 -080056
57 virtual void
58 resetTpmPassword()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070059 {
60 }
Yingdi Yube4150e2014-02-18 13:02:46 -080061
62 virtual void
63 setInTerminal(bool inTerminal)
64 {
65 m_inTerminal = inTerminal;
66 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070067
Yingdi Yube4150e2014-02-18 13:02:46 -080068 virtual bool
Alexander Afanasyev770827c2014-05-13 17:42:55 -070069 getInTerminal() const
Yingdi Yube4150e2014-02-18 13:02:46 -080070 {
71 return m_inTerminal;
72 }
73
74 virtual bool
Yingdi Yuf56c68f2014-04-24 21:50:13 -070075 isLocked()
Yingdi Yube4150e2014-02-18 13:02:46 -080076 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070077 return false;
Yingdi Yube4150e2014-02-18 13:02:46 -080078 }
79
Yingdi Yu2e57a582014-02-20 23:34:43 -080080 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080081 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070083 return !isLocked();
Yingdi Yu2e57a582014-02-20 23:34:43 -080084 }
Yingdi Yube4150e2014-02-18 13:02:46 -080085
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080087 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070088
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080090 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080091
Yingdi Yu28fd32f2014-01-28 19:03:03 -080092 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080094
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070096 signInTpm(const uint8_t* data, size_t dataLength,
97 const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070098
99 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800100 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800102 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800103 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700104
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700105 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800106 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700107
Yingdi Yu4b752752014-02-18 12:24:03 -0800108 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700109 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800110
111 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -0800112 generateRandomBlock(uint8_t* res, size_t size);
113
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700114 virtual void
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700115 addAppToAcl(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700116 {
117 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800118
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800119 /******************************
120 * SecTpmMemory specific *
121 ******************************/
122
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700123 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800124 * @brief Set the public and private key for the keyName.
125 *
126 * @param keyName The key name.
127 * @param publicKeyDer The public key DER byte array.
128 * @param publicKeyDerLength The length of publicKeyDer.
129 * @param privateKeyDer The private key DER byte array.
130 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700131 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800132 void setKeyPairForKeyName(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700133 const uint8_t* publicKeyDer, size_t publicKeyDerLength,
134 const uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800135
136protected:
137 /******************************
138 * From TrustedPlatformModule *
139 ******************************/
140 virtual ConstBufferPtr
141 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
142
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700143 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800144 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700145
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800146 virtual bool
147 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700148
149
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700150private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800151 class RsaPrivateKey;
152
Yingdi Yu2e57a582014-02-20 23:34:43 -0800153 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
154 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700155
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700156 PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */
157 PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800158
159 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700160};
161
Yingdi Yufc40d872014-02-18 12:56:04 -0800162} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700163
Yingdi Yufc40d872014-02-18 12:56:04 -0800164#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP