blob: 02f8fbc697fcbd98c31075adda355e0f9293ca33 [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
8#ifndef NDN_MEMORY_PRIVATE_KEY_STORAGE_HPP
9#define NDN_MEMORY_PRIVATE_KEY_STORAGE_HPP
10
11#include <map>
12#include "private-key-storage.hpp"
13
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 */
22class MemoryPrivateKeyStorage : public PrivateKeyStorage {
23public:
24 /**
25 * The virtual destructor
26 */
27 virtual
28 ~MemoryPrivateKeyStorage();
29
30 /**
31 * Set the public and private key for the keyName.
32 * @param keyName The key name.
33 * @param publicKeyDer The public key DER byte array.
34 * @param publicKeyDerLength The length of publicKeyDer.
35 * @param privateKeyDer The private key DER byte array.
36 * @param privateKeyDerLength The length of privateKeyDer.
37 */
38 void setKeyPairForKeyName
Jeff Thompson10ad12a2013-09-24 16:19:11 -070039 (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer,
Jeff Thompson6c314bc2013-09-23 18:09:38 -070040 size_t privateKeyDerLength);
41
42 /**
43 * Generate a pair of asymmetric keys.
44 * @param keyName The name of the key pair.
45 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
46 * @param keySize The size of the key pair.
47 */
48 virtual void
49 generateKeyPair(const Name& keyName, KeyType keyType, int keySize);
50
51 /**
52 * Get the public key
53 * @param keyName The name of public key.
54 * @return The public key.
55 */
56 virtual ptr_lib::shared_ptr<PublicKey>
57 getPublicKey(const Name& keyName);
58
59 /**
60 * Fetch the private key for keyName and sign the data, returning a signature Blob.
61 * @param data Pointer to the input byte array.
62 * @param dataLength The length of data.
63 * @param keyName The name of the signing key.
64 * @param digestAlgorithm the digest algorithm.
65 * @return The signature, or a null pointer if signing fails.
66 */
67 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070068 sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070069
70 /**
71 * Decrypt data.
72 * @param keyName The name of the decrypting key.
73 * @param data The byte to be decrypted.
74 * @param dataLength the length of data.
75 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
76 * @return The decrypted data.
77 */
78 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070079 decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070080
81 /**
82 * Encrypt data.
83 * @param keyName The name of the encrypting key.
84 * @param data The byte to be encrypted.
85 * @param dataLength the length of data.
86 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
87 * @return The encrypted data.
88 */
89 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070090 encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070091
92 /**
93 * @brief Generate a symmetric key.
94 * @param keyName The name of the key.
95 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
96 * @param keySize The size of the key.
97 */
98 virtual void
99 generateKey(const Name& keyName, KeyType keyType, int keySize);
100
101 /**
102 * Check if a particular key exists.
103 * @param keyName The name of the key.
104 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
105 * @return True if the key exists, otherwise false.
106 */
107 virtual bool
108 doesKeyExist(const Name& keyName, KeyClass keyClass);
109
110private:
111 /**
112 * RsaPrivateKey is a simple class to hold an RSA private key.
113 */
114 class RsaPrivateKey {
115 public:
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700116 RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700117
118 ~RsaPrivateKey();
119
120 struct rsa_st* getPrivateKey() { return privateKey_; }
121
122 private:
123 struct rsa_st* privateKey_;
124 };
125
126 std::map<std::string, ptr_lib::shared_ptr<PublicKey> > publicKeyStore_; /**< The map key is the keyName.toUri() */
127 std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > privateKeyStore_; /**< The map key is the keyName.toUri() */
128};
129
130}
131
132#endif