blob: 21dc2724cf9b32c6e5760c4cfb0038a7833fd944 [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 Yu87581582014-01-14 14:28:39 -08008#ifndef NDN_SEC_TPM_MEMORY_HPP
9#define NDN_SEC_TPM_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
11#include <map>
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 /**
27 * The virtual destructor
28 */
29 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080030 ~SecTpmMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070031
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 Afanasyeve64788e2014-01-05 22:38:21 -080040 void setKeyPairForKeyName(const Name& keyName,
41 uint8_t *publicKeyDer, size_t publicKeyDerLength,
42 uint8_t *privateKeyDer, size_t privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070043
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 Yu87581582014-01-14 14:28:39 -080051 generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070052
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 Yu87581582014-01-14 14:28:39 -080059 getPublicKeyFromTpm(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070060
61 /**
62 * Fetch the private key for keyName and sign the data, returning a signature Blob.
63 * @param data Pointer to the input byte array.
64 * @param dataLength The length of data.
65 * @param keyName The name of the signing key.
66 * @param digestAlgorithm the digest algorithm.
67 * @return The signature, or a null pointer if signing fails.
68 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080069 virtual Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080070 signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080071
Jeff Thompson6c314bc2013-09-23 18:09:38 -070072 /**
73 * Decrypt data.
74 * @param keyName The name of the decrypting key.
75 * @param data The byte to be decrypted.
76 * @param dataLength the length of data.
77 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
78 * @return The decrypted data.
79 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080080 virtual ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -080081 decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070082
83 /**
84 * Encrypt data.
85 * @param keyName The name of the encrypting key.
86 * @param data The byte to be encrypted.
87 * @param dataLength the length of data.
88 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
89 * @return The encrypted data.
90 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080091 virtual ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -080092 encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070093
94 /**
95 * @brief Generate a symmetric key.
96 * @param keyName The name of the key.
97 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
98 * @param keySize The size of the key.
99 */
100 virtual void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800101 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102
103 /**
104 * Check if a particular key exists.
105 * @param keyName The name of the key.
106 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
107 * @return True if the key exists, otherwise false.
108 */
109 virtual bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800110 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700111
112private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800113 class RsaPrivateKey;
114
115 typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore;
116 typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore;
117
118 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
119 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700120};
121
122}
123
124#endif