Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 8 | #ifndef NDN_SEC_TPM_MEMORY_HPP |
| 9 | #define NDN_SEC_TPM_MEMORY_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 10 | |
| 11 | #include <map> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 12 | #include "sec-tpm.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | |
| 14 | struct rsa_st; |
| 15 | |
| 16 | namespace 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 Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 22 | class SecTpmMemory : public SecTpm { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 23 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 25 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | /** |
| 27 | * The virtual destructor |
| 28 | */ |
| 29 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 30 | ~SecTpmMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Set the public and private key for the keyName. |
| 34 | * @param keyName The key name. |
| 35 | * @param publicKeyDer The public key DER byte array. |
| 36 | * @param publicKeyDerLength The length of publicKeyDer. |
| 37 | * @param privateKeyDer The private key DER byte array. |
| 38 | * @param privateKeyDerLength The length of privateKeyDer. |
| 39 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 40 | void setKeyPairForKeyName(const Name& keyName, |
| 41 | uint8_t *publicKeyDer, size_t publicKeyDerLength, |
| 42 | uint8_t *privateKeyDer, size_t privateKeyDerLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Generate a pair of asymmetric keys. |
| 46 | * @param keyName The name of the key pair. |
| 47 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 48 | * @param keySize The size of the key pair. |
| 49 | */ |
| 50 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 51 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Get the public key |
| 55 | * @param keyName The name of public key. |
| 56 | * @return The public key. |
| 57 | */ |
| 58 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 59 | getPublicKeyFromTpm(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Delete a key pair of asymmetric keys. |
| 63 | * @param keyName The name of the key pair. |
| 64 | */ |
| 65 | virtual void |
| 66 | deleteKeyPairInTpm(const Name &keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Fetch the private key for keyName and sign the data, returning a signature Blob. |
| 70 | * @param data Pointer to the input byte array. |
| 71 | * @param dataLength The length of data. |
| 72 | * @param keyName The name of the signing key. |
| 73 | * @param digestAlgorithm the digest algorithm. |
| 74 | * @return The signature, or a null pointer if signing fails. |
| 75 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 76 | virtual Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 77 | signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 78 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Decrypt data. |
| 81 | * @param keyName The name of the decrypting key. |
| 82 | * @param data The byte to be decrypted. |
| 83 | * @param dataLength the length of data. |
| 84 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 85 | * @return The decrypted data. |
| 86 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 87 | virtual ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 88 | decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Encrypt data. |
| 92 | * @param keyName The name of the encrypting key. |
| 93 | * @param data The byte to be encrypted. |
| 94 | * @param dataLength the length of data. |
| 95 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 96 | * @return The encrypted data. |
| 97 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 98 | virtual ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 99 | encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * @brief Generate a symmetric key. |
| 103 | * @param keyName The name of the key. |
| 104 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 105 | * @param keySize The size of the key. |
| 106 | */ |
| 107 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 108 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Check if a particular key exists. |
| 112 | * @param keyName The name of the key. |
| 113 | * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC. |
| 114 | * @return True if the key exists, otherwise false. |
| 115 | */ |
| 116 | virtual bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 117 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 118 | |
| 119 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 120 | class RsaPrivateKey; |
| 121 | |
| 122 | typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore; |
| 123 | typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore; |
| 124 | |
| 125 | PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */ |
| 126 | PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | #endif |