blob: eb747e9c8736683007b188582211694f4d15a602 [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_PUBLIC_INFO_MEMORY_HPP
9#define NDN_SEC_PUBLIC_INFO_MEMORY_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>
Yingdi Yu87581582014-01-14 14:28:39 -080013#include "sec-public-info.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070014
15namespace ndn {
16
17/**
Yingdi Yu87581582014-01-14 14:28:39 -080018 * MemoryKeyMetaInfo 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 MemoryKeyMetaInfo object.
20 * To use permanent file-based storage, see BasicKeyMetaInfo.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070021 */
Yingdi Yu87581582014-01-14 14:28:39 -080022class SecPublicInfoMemory : public SecPublicInfo {
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023public:
Yingdi Yu87581582014-01-14 14:28:39 -080024 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::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 ~SecPublicInfoMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070031
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
Yingdi Yu87581582014-01-14 14:28:39 -080060 doesPublicKeyExist(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070061
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
Yingdi Yu87581582014-01-14 14:28:39 -080069 addPublicKey(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>
Yingdi Yu87581582014-01-14 14:28:39 -080077 getPublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070078
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
Yingdi Yu87581582014-01-14 14:28:39 -080084 activatePublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085
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
Yingdi Yu87581582014-01-14 14:28:39 -080091 deactivatePublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070092
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>
Yingdi Yu88663af2014-01-15 15:21:38 -0800115 getCertificate(const Name &certificateName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700116
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
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800145 virtual void
146 getAllIdentities(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800147
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800148 virtual void
149 getAllKeyNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800150
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800151 virtual void
152 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800153
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800154 virtual void
155 getAllCertificateNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800156
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157 virtual void
158 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault);
Yingdi Yu87581582014-01-14 14:28:39 -0800159
160protected:
161 /**
162 * Set the default identity. If the identityName does not exist, then clear the default identity
163 * so that getDefaultIdentity() returns an empty name.
164 * @param identityName The default identity name.
165 */
166 virtual void
167 setDefaultIdentityInternal(const Name& identityName);
168
169 /**
170 * Set the default key name for the specified identity.
171 * @param keyName The key name.
172 * @param identityNameCheck (optional) The identity name to check the keyName.
173 */
174 virtual void
175 setDefaultKeyNameForIdentityInternal(const Name& keyName);
176
177 /**
178 * Set the default key name for the specified identity.
179 * @param keyName The key name.
180 * @param certificateName The certificate name.
181 */
182 virtual void
183 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
184
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800185 /**
186 * Delete a certificate.
187 * @param certificateName The certificate name.
188 */
189 virtual void
190 deleteCertificateInfo(const Name &certificateName);
191
192 /**
193 * Delete a public key and related certificates.
194 * @param keyName The key name.
195 */
196 virtual void
197 deletePublicKeyInfo(const Name &keyName);
198
199 /**
200 * Delete an identity and related public keys and certificates.
201 * @param identity The identity name.
202 */
203 virtual void
204 deleteIdentityInfo(const Name &identity);
205
Jeff Thompson81842272013-09-25 16:12:33 -0700206
207private:
Jeff Thompson61805e92013-10-23 15:19:39 -0700208 class KeyRecord {
209 public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800210 KeyRecord(KeyType keyType, const PublicKey &key)
211 : keyType_(keyType), key_(key)
Jeff Thompson61805e92013-10-23 15:19:39 -0700212 {
213 }
214
215 const KeyType getKeyType() const { return keyType_; }
216
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800217 const PublicKey& getKey() { return key_; }
Jeff Thompson61805e92013-10-23 15:19:39 -0700218
219 private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800220 KeyType keyType_;
221 PublicKey key_;
Jeff Thompson61805e92013-10-23 15:19:39 -0700222 };
223
Jeff Thompson81842272013-09-25 16:12:33 -0700224 std::vector<std::string> identityStore_; /**< A list of name URI. */
225 std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800226 Name defaultKeyName_;
227 Name defaultCert_;
228
229 typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */
230 typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */
231
232 KeyStore keyStore_;
233 CertificateStore certificateStore_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700234};
235
236}
237
238#endif