blob: eac75ede1ec9cae88b4169768c23e1e050cfd384 [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
21struct rsa_st;
22
23namespace ndn {
24
25/**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070026 * @brief SecTpmMemory implements a simple in-memory TPM.
27 *
28 * You should initialize by calling setKeyPairForKeyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070030class SecTpmMemory : public SecTpm
31{
Jeff Thompson6c314bc2013-09-23 18:09:38 -070032public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033 class Error : public SecTpm::Error
34 {
35 public:
36 explicit
37 Error(const std::string& what)
38 : SecTpm::Error(what)
39 {
40 }
41 };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080042
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070043 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080044 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070045
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080046 /******************************
47 * From TrustedPlatformModule *
48 ******************************/
49
Yingdi Yube4150e2014-02-18 13:02:46 -080050 virtual void
51 setTpmPassword(const uint8_t* password, size_t passwordLength)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070052 {
53 }
Yingdi Yube4150e2014-02-18 13:02:46 -080054
55 virtual void
56 resetTpmPassword()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070057 {
58 }
Yingdi Yube4150e2014-02-18 13:02:46 -080059
60 virtual void
61 setInTerminal(bool inTerminal)
62 {
63 m_inTerminal = inTerminal;
64 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070065
Yingdi Yube4150e2014-02-18 13:02:46 -080066 virtual bool
67 getInTerminal()
68 {
69 return m_inTerminal;
70 }
71
72 virtual bool
73 locked()
74 {
75 return true;
76 }
77
Yingdi Yu2e57a582014-02-20 23:34:43 -080078 virtual bool
Yingdi Yube4150e2014-02-18 13:02:46 -080079 unlockTpm(const char* password, size_t passwordLength, bool usePassword)
Yingdi Yu2e57a582014-02-20 23:34:43 -080080 {
81 return !locked();
82 }
Yingdi Yube4150e2014-02-18 13:02:46 -080083
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070084 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080085 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070086
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070087 virtual shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080088 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080089
Yingdi Yu28fd32f2014-01-28 19:03:03 -080090 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 deleteKeyPairInTpm(const Name& keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080092
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070093 virtual Block
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 signInTpm(const uint8_t* data, size_t dataLength,
95 const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096
97 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080098 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070099
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800100 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800101 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700103 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800104 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105
Yingdi Yu4b752752014-02-18 12:24:03 -0800106 virtual bool
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700107 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800108
109 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -0800110 generateRandomBlock(uint8_t* res, size_t size);
111
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700112 virtual void
Yingdi Yu2e57a582014-02-20 23:34:43 -0800113 addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700114 {
115 }
Yingdi Yu2e57a582014-02-20 23:34:43 -0800116
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117 /******************************
118 * SecTpmMemory specific *
119 ******************************/
120
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700121 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800122 * @brief Set the public and private key for the keyName.
123 *
124 * @param keyName The key name.
125 * @param publicKeyDer The public key DER byte array.
126 * @param publicKeyDerLength The length of publicKeyDer.
127 * @param privateKeyDer The private key DER byte array.
128 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700129 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800130 void setKeyPairForKeyName(const Name& keyName,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700131 const uint8_t* publicKeyDer, size_t publicKeyDerLength,
132 const uint8_t* privateKeyDer, size_t privateKeyDerLength);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800133
134protected:
135 /******************************
136 * From TrustedPlatformModule *
137 ******************************/
138 virtual ConstBufferPtr
139 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
140
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700141 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800142 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700143
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800144 virtual bool
145 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700146
147
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800149 class RsaPrivateKey;
150
Yingdi Yu2e57a582014-02-20 23:34:43 -0800151 typedef std::map<std::string, shared_ptr<PublicKey> > PublicKeyStore;
152 typedef std::map<std::string, shared_ptr<RsaPrivateKey> > PrivateKeyStore;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700153
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700154 PublicKeyStore m_publicKeyStore; /**< The map key is the keyName.toUri() */
155 PrivateKeyStore m_privateKeyStore; /**< The map key is the keyName.toUri() */
Yingdi Yube4150e2014-02-18 13:02:46 -0800156
157 bool m_inTerminal;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700158};
159
Yingdi Yufc40d872014-02-18 12:56:04 -0800160} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700161
Yingdi Yufc40d872014-02-18 12:56:04 -0800162#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP