blob: 5027a73d1a5326a3d25a71078ab5115bb7cdbaab [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_IDENTITY_STORAGE_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -07009#define NDN_MEMORY_IDENTITY_STORAGE_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
Jeff Thompson81842272013-09-25 16:12:33 -070011#include <vector>
Jeff Thompson61805e92013-10-23 15:19:39 -070012#include <map>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013#include "identity-storage.hpp"
14
15namespace ndn {
16
17/**
18 * MemoryIdentityStorage extends IdentityStorage and implements its methods to store identity, public key and certificate objects in memory.
19 * The application must get the objects through its own means and add the objects to the MemoryIdentityStorage object.
20 * To use permanent file-based storage, see BasicIdentityStorage.
21 */
22class MemoryIdentityStorage : public IdentityStorage {
23public:
24 /**
25 * The virtual Destructor.
26 */
27 virtual
28 ~MemoryIdentityStorage();
29
30 /**
31 * Check if the specified identity already exists.
32 * @param identityName The identity name.
33 * @return true if the identity exists, otherwise false.
34 */
35 virtual bool
36 doesIdentityExist(const Name& identityName);
37
38 /**
39 * Add a new identity. An exception will be thrown if the identity already exists.
40 * @param identityName The identity name to be added.
41 */
42 virtual void
43 addIdentity(const Name& identityName);
44
45 /**
46 * Revoke the identity.
47 * @return true if the identity was revoked, false if not.
48 */
49 virtual bool
50 revokeIdentity();
51
52 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053 * Check if the specified key already exists.
54 * @param keyName The name of the key.
55 * @return true if the key exists, otherwise false.
56 */
57 virtual bool
58 doesKeyExist(const Name& keyName);
59
60 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070061 * Add a public key to the identity storage.
62 * @param keyName The name of the public key to be added.
63 * @param keyType Type of the public key to be added.
64 * @param publicKeyDer A blob of the public key DER to be added.
65 */
66 virtual void
Jeff Thompsonbd04b072013-09-27 15:14:09 -070067 addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070068
69 /**
70 * Get the public key DER blob from the identity storage.
71 * @param keyName The name of the requested public key.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -070072 * @return The DER Blob. If not found, return a Blob with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070073 */
74 virtual Blob
75 getKey(const Name& keyName);
76
77 /**
78 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
79 * @param keyName name of the key
80 */
81 virtual void
82 activateKey(const Name& keyName);
83
84 /**
85 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
86 * @param keyName name of the key
87 */
88 virtual void
89 deactivateKey(const Name& keyName);
90
91 /**
92 * Check if the specified certificate already exists.
93 * @param certificateName The name of the certificate.
94 * @return true if the certificate exists, otherwise false.
95 */
96 virtual bool
97 doesCertificateExist(const Name& certificateName);
98
99 /**
100 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700101 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102 */
103 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700104 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105
106 /**
107 * Get a certificate from the identity storage.
108 * @param certificateName The name of the requested certificate.
109 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700110 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700111 */
Jeff Thompson3bd90bc2013-10-19 16:40:14 -0700112 virtual ptr_lib::shared_ptr<Data>
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113 getCertificate(const Name &certificateName, bool allowAny = false);
114
115
116 /*****************************************
117 * Get/Set Default *
118 *****************************************/
119
120 /**
121 * Get the default identity.
Jeff Thompson81842272013-09-25 16:12:33 -0700122 * @param return The name of default identity, or an empty name if there is no default.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700123 */
124 virtual Name
125 getDefaultIdentity();
126
127 /**
128 * Get the default key name for the specified identity.
129 * @param identityName The identity name.
130 * @return The default key name.
131 */
132 virtual Name
133 getDefaultKeyNameForIdentity(const Name& identityName);
134
135 /**
136 * Get the default certificate name for the specified key.
137 * @param keyName The key name.
138 * @return The default certificate name.
139 */
140 virtual Name
141 getDefaultCertificateNameForKey(const Name& keyName);
142
143 /**
Jeff Thompson81842272013-09-25 16:12:33 -0700144 * Set the default identity. If the identityName does not exist, then clear the default identity
145 * so that getDefaultIdentity() returns an empty name.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700146 * @param identityName The default identity name.
147 */
148 virtual void
149 setDefaultIdentity(const Name& identityName);
150
151 /**
152 * Set the default key name for the specified identity.
153 * @param keyName The key name.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700154 * @param identityNameCheck (optional) The identity name to check the keyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700155 */
156 virtual void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700157 setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700158
159 /**
160 * Set the default key name for the specified identity.
161 * @param keyName The key name.
162 * @param certificateName The certificate name.
163 */
164 virtual void
165 setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800166
167 virtual std::vector<Name>
168 getAllIdentities(bool isDefault);
169
170 virtual std::vector<Name>
171 getAllKeyNames(bool isDefault);
172
173 virtual std::vector<Name>
174 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
175
176 virtual std::vector<Name>
177 getAllCertificateNames(bool isDefault);
178
179 virtual std::vector<Name>
180 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
Jeff Thompson81842272013-09-25 16:12:33 -0700181
182private:
Jeff Thompson61805e92013-10-23 15:19:39 -0700183 class KeyRecord {
184 public:
185 KeyRecord(KeyType keyType, const Blob &keyDer)
186 : keyType_(keyType), keyDer_(keyDer)
187 {
188 }
189
190 const KeyType getKeyType() const { return keyType_; }
191
192 const Blob& getKeyDer() { return keyDer_; }
193
194 private:
195 KeyType keyType_;
196 Blob keyDer_;
197 };
198
Jeff Thompson81842272013-09-25 16:12:33 -0700199 std::vector<std::string> identityStore_; /**< A list of name URI. */
200 std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
Jeff Thompson61805e92013-10-23 15:19:39 -0700201 std::map<std::string, ptr_lib::shared_ptr<KeyRecord> > keyStore_; /**< The map key is the keyName.toUri() */
202 std::map<std::string, Blob> certificateStore_; /**< The map key is the certificateName.toUri() */
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700203};
204
205}
206
207#endif