blob: f267583c30f33cdf29b9f5b89fe38a638f10b111 [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
Jeff Thompsone589c3f2013-10-12 17:30:50 -07009#define NDN_MEMORY_PRIVATE_KEY_STORAGE_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
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:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080024 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
25
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026 /**
27 * The virtual destructor
28 */
29 virtual
30 ~MemoryPrivateKeyStorage();
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 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
51 generateKeyPair(const Name& keyName, KeyType keyType, int keySize);
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>
59 getPublicKey(const Name& keyName);
60
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
70 sign(const uint8_t *data, size_t dataLength, const Signature &signature, const Name& keyName, DigestAlgorithm digestAlgorithm);
71
72 virtual Block
73 sign(const Data &data, const Signature &signature, const Name& keyName, DigestAlgorithm digestAlgorithm);
74
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075 /**
76 * Decrypt data.
77 * @param keyName The name of the decrypting key.
78 * @param data The byte to be decrypted.
79 * @param dataLength the length of data.
80 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
81 * @return The decrypted data.
82 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080083 virtual ConstBufferPtr
Jeff Thompson97223af2013-09-24 17:01:27 -070084 decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085
86 /**
87 * Encrypt data.
88 * @param keyName The name of the encrypting key.
89 * @param data The byte to be encrypted.
90 * @param dataLength the length of data.
91 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
92 * @return The encrypted data.
93 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080094 virtual ConstBufferPtr
Jeff Thompson97223af2013-09-24 17:01:27 -070095 encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070096
97 /**
98 * @brief Generate a symmetric key.
99 * @param keyName The name of the key.
100 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
101 * @param keySize The size of the key.
102 */
103 virtual void
104 generateKey(const Name& keyName, KeyType keyType, int keySize);
105
106 /**
107 * Check if a particular key exists.
108 * @param keyName The name of the key.
109 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
110 * @return True if the key exists, otherwise false.
111 */
112 virtual bool
113 doesKeyExist(const Name& keyName, KeyClass keyClass);
114
115private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800116 class RsaPrivateKey;
117
118 typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore;
119 typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore;
120
121 PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */
122 PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700123};
124
125}
126
127#endif