blob: 57f80509abf461bef115ff31a02f498167c2d4ba [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:
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 ~MemoryIdentityStorage();
31
32 /**
33 * Check if the specified identity already exists.
34 * @param identityName The identity name.
35 * @return true if the identity exists, otherwise false.
36 */
37 virtual bool
38 doesIdentityExist(const Name& identityName);
39
40 /**
41 * Add a new identity. An exception will be thrown if the identity already exists.
42 * @param identityName The identity name to be added.
43 */
44 virtual void
45 addIdentity(const Name& identityName);
46
47 /**
48 * Revoke the identity.
49 * @return true if the identity was revoked, false if not.
50 */
51 virtual bool
52 revokeIdentity();
53
54 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070055 * Check if the specified key already exists.
56 * @param keyName The name of the key.
57 * @return true if the key exists, otherwise false.
58 */
59 virtual bool
60 doesKeyExist(const Name& keyName);
61
62 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070063 * Add a public key to the identity storage.
64 * @param keyName The name of the public key to be added.
65 * @param keyType Type of the public key to be added.
66 * @param publicKeyDer A blob of the public key DER to be added.
67 */
68 virtual void
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080069 addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070070
71 /**
72 * Get the public key DER blob from the identity storage.
73 * @param keyName The name of the requested public key.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -070074 * @return The DER Blob. If not found, return a Blob with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080076 virtual ptr_lib::shared_ptr<PublicKey>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070077 getKey(const Name& keyName);
78
79 /**
80 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
81 * @param keyName name of the key
82 */
83 virtual void
84 activateKey(const Name& keyName);
85
86 /**
87 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
88 * @param keyName name of the key
89 */
90 virtual void
91 deactivateKey(const Name& keyName);
92
93 /**
94 * Check if the specified certificate already exists.
95 * @param certificateName The name of the certificate.
96 * @return true if the certificate exists, otherwise false.
97 */
98 virtual bool
99 doesCertificateExist(const Name& certificateName);
100
101 /**
102 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700103 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700104 */
105 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700106 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700107
108 /**
109 * Get a certificate from the identity storage.
110 * @param certificateName The name of the requested certificate.
111 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700112 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800114 virtual ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700115 getCertificate(const Name &certificateName, bool allowAny = false);
116
117
118 /*****************************************
119 * Get/Set Default *
120 *****************************************/
121
122 /**
123 * Get the default identity.
Jeff Thompson81842272013-09-25 16:12:33 -0700124 * @param return The name of default identity, or an empty name if there is no default.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700125 */
126 virtual Name
127 getDefaultIdentity();
128
129 /**
130 * Get the default key name for the specified identity.
131 * @param identityName The identity name.
132 * @return The default key name.
133 */
134 virtual Name
135 getDefaultKeyNameForIdentity(const Name& identityName);
136
137 /**
138 * Get the default certificate name for the specified key.
139 * @param keyName The key name.
140 * @return The default certificate name.
141 */
142 virtual Name
143 getDefaultCertificateNameForKey(const Name& keyName);
144
145 /**
Jeff Thompson81842272013-09-25 16:12:33 -0700146 * Set the default identity. If the identityName does not exist, then clear the default identity
147 * so that getDefaultIdentity() returns an empty name.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148 * @param identityName The default identity name.
149 */
150 virtual void
151 setDefaultIdentity(const Name& identityName);
152
153 /**
154 * Set the default key name for the specified identity.
155 * @param keyName The key name.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700156 * @param identityNameCheck (optional) The identity name to check the keyName.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700157 */
158 virtual void
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700159 setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700160
161 /**
162 * Set the default key name for the specified identity.
163 * @param keyName The key name.
164 * @param certificateName The certificate name.
165 */
166 virtual void
167 setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800168
169 virtual std::vector<Name>
170 getAllIdentities(bool isDefault);
171
172 virtual std::vector<Name>
173 getAllKeyNames(bool isDefault);
174
175 virtual std::vector<Name>
176 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
177
178 virtual std::vector<Name>
179 getAllCertificateNames(bool isDefault);
180
181 virtual std::vector<Name>
182 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault);
Jeff Thompson81842272013-09-25 16:12:33 -0700183
184private:
Jeff Thompson61805e92013-10-23 15:19:39 -0700185 class KeyRecord {
186 public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800187 KeyRecord(KeyType keyType, const PublicKey &key)
188 : keyType_(keyType), key_(key)
Jeff Thompson61805e92013-10-23 15:19:39 -0700189 {
190 }
191
192 const KeyType getKeyType() const { return keyType_; }
193
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800194 const PublicKey& getKey() { return key_; }
Jeff Thompson61805e92013-10-23 15:19:39 -0700195
196 private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800197 KeyType keyType_;
198 PublicKey key_;
Jeff Thompson61805e92013-10-23 15:19:39 -0700199 };
200
Jeff Thompson81842272013-09-25 16:12:33 -0700201 std::vector<std::string> identityStore_; /**< A list of name URI. */
202 std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800203 Name defaultKeyName_;
204 Name defaultCert_;
205
206 typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */
207 typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */
208
209 KeyStore keyStore_;
210 CertificateStore certificateStore_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700211};
212
213}
214
215#endif