blob: 01860e2679300bdf50759c280fcf32419f8d7d48 [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:
Yingdi Yu87581582014-01-14 14:28:39 -080024 struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080025
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080027 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070028
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080029 /******************************
30 * From TrustedPlatformModule *
31 ******************************/
32
Jeff Thompson6c314bc2013-09-23 18:09:38 -070033 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080034 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070035
Jeff Thompson6c314bc2013-09-23 18:09:38 -070036 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080037 getPublicKeyFromTpm(const Name& keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080038
Yingdi Yu28fd32f2014-01-28 19:03:03 -080039 virtual void
40 deleteKeyPairInTpm(const Name &keyName);
Yingdi Yufc40d872014-02-18 12:56:04 -080041
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080042 virtual Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080043 signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080044
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080045 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080046 decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070047
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080048 virtual ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -080049 encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070050
Jeff Thompson6c314bc2013-09-23 18:09:38 -070051 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -080052 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053
Yingdi Yu4b752752014-02-18 12:24:03 -080054 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080055 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
56
57 virtual bool
Yingdi Yu4b752752014-02-18 12:24:03 -080058 generateRandomBlock(uint8_t* res, size_t size);
59
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080060 /******************************
61 * SecTpmMemory specific *
62 ******************************/
63
Jeff Thompson6c314bc2013-09-23 18:09:38 -070064 /**
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080065 * @brief Set the public and private key for the keyName.
66 *
67 * @param keyName The key name.
68 * @param publicKeyDer The public key DER byte array.
69 * @param publicKeyDerLength The length of publicKeyDer.
70 * @param privateKeyDer The private key DER byte array.
71 * @param privateKeyDerLength The length of privateKeyDer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070072 */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080073 void setKeyPairForKeyName(const Name& keyName,
74 uint8_t *publicKeyDer, size_t publicKeyDerLength,
75 uint8_t *privateKeyDer, size_t privateKeyDerLength);
76
77protected:
78 /******************************
79 * From TrustedPlatformModule *
80 ******************************/
81 virtual ConstBufferPtr
82 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
83
Jeff Thompson6c314bc2013-09-23 18:09:38 -070084 virtual bool
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080085 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
86
87 virtual bool
88 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
89
Jeff Thompson6c314bc2013-09-23 18:09:38 -070090
91private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080092 class RsaPrivateKey;
93
94 typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore;
95 typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore;
96
97 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
98 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070099};
100
Yingdi Yufc40d872014-02-18 12:56:04 -0800101} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102
Yingdi Yufc40d872014-02-18 12:56:04 -0800103#endif //NDN_SECURITY_SEC_TPM_MEMORY_HPP