blob: 04104ea596832dfa8d95883a74dd11496300add0 [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 Yufc40d872014-02-18 12:56:04 -08008#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_MEMORY_HPP
9#define NDN_SECURITY_SEC_PUBLIC_INFO_MEMORY_HPP
Jeff Thompson6c314bc2013-09-23 18:09:38 -070010
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "../common.hpp"
Yingdi Yu87581582014-01-14 14:28:39 -080012#include "sec-public-info.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013
14namespace ndn {
15
16/**
Yingdi Yu87581582014-01-14 14:28:39 -080017 * MemoryKeyMetaInfo extends IdentityStorage and implements its methods to store identity, public key and certificate objects in memory.
18 * The application must get the objects through its own means and add the objects to the MemoryKeyMetaInfo object.
19 * To use permanent file-based storage, see BasicKeyMetaInfo.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070020 */
Yingdi Yu87581582014-01-14 14:28:39 -080021class SecPublicInfoMemory : public SecPublicInfo {
Jeff Thompson6c314bc2013-09-23 18:09:38 -070022public:
Yingdi Yu87581582014-01-14 14:28:39 -080023 struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} };
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080024
Jeff Thompson6c314bc2013-09-23 18:09:38 -070025 /**
26 * The virtual Destructor.
27 */
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080028 virtual
Yingdi Yu87581582014-01-14 14:28:39 -080029 ~SecPublicInfoMemory();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070030
31 /**
32 * Check if the specified identity already exists.
33 * @param identityName The identity name.
34 * @return true if the identity exists, otherwise false.
35 */
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080036 virtual bool
Jeff Thompson6c314bc2013-09-23 18:09:38 -070037 doesIdentityExist(const Name& identityName);
38
39 /**
40 * Add a new identity. An exception will be thrown if the identity already exists.
41 * @param identityName The identity name to be added.
42 */
43 virtual void
44 addIdentity(const Name& identityName);
45
46 /**
47 * Revoke the identity.
48 * @return true if the identity was revoked, false if not.
49 */
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080050 virtual bool
Jeff Thompson6c314bc2013-09-23 18:09:38 -070051 revokeIdentity();
52
53 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070054 * Check if the specified key already exists.
55 * @param keyName The name of the key.
56 * @return true if the key exists, otherwise false.
57 */
58 virtual bool
Yingdi Yu87581582014-01-14 14:28:39 -080059 doesPublicKeyExist(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070060
61 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070062 * Add a public key to the identity storage.
63 * @param keyName The name of the public key to be added.
64 * @param keyType Type of the public key to be added.
65 * @param publicKeyDer A blob of the public key DER to be added.
66 */
67 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080068 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070069
70 /**
71 * Get the public key DER blob from the identity storage.
72 * @param keyName The name of the requested public key.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -070073 * @return The DER Blob. If not found, return a Blob with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070074 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080075 virtual ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080076 getPublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070077
78 /**
79 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
80 * @param keyName name of the key
81 */
82 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080083 activatePublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070084
85 /**
86 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
87 * @param keyName name of the key
88 */
89 virtual void
Yingdi Yu87581582014-01-14 14:28:39 -080090 deactivatePublicKey(const Name& keyName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070091
92 /**
93 * Check if the specified certificate already exists.
94 * @param certificateName The name of the certificate.
95 * @return true if the certificate exists, otherwise false.
96 */
97 virtual bool
98 doesCertificateExist(const Name& certificateName);
99
100 /**
101 * Add a certificate to the identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700102 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700103 */
104 virtual void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700105 addCertificate(const IdentityCertificate& certificate);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700106
107 /**
108 * Get a certificate from the identity storage.
109 * @param certificateName The name of the requested certificate.
110 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
Jeff Thompsonabcea7d2013-10-02 15:03:21 -0700111 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700112 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800113 virtual ptr_lib::shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800114 getCertificate(const Name &certificateName);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700115
116
117 /*****************************************
118 * Get/Set Default *
119 *****************************************/
120
121 /**
122 * Get the default identity.
Jeff Thompson81842272013-09-25 16:12:33 -0700123 * @param return The name of default identity, or an empty name if there is no default.
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700124 */
125 virtual Name
126 getDefaultIdentity();
127
128 /**
129 * Get the default key name for the specified identity.
130 * @param identityName The identity name.
131 * @return The default key name.
132 */
133 virtual Name
134 getDefaultKeyNameForIdentity(const Name& identityName);
135
136 /**
137 * Get the default certificate name for the specified key.
138 * @param keyName The key name.
139 * @return The default certificate name.
140 */
141 virtual Name
142 getDefaultCertificateNameForKey(const Name& keyName);
143
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800144 virtual void
145 getAllIdentities(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800146
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800147 virtual void
148 getAllKeyNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800149
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800150 virtual void
151 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800152
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800153 virtual void
154 getAllCertificateNames(std::vector<Name> &nameList, bool isDefault);
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800155
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800156 virtual void
157 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault);
Yingdi Yu87581582014-01-14 14:28:39 -0800158
159protected:
160 /**
161 * Set the default identity. If the identityName does not exist, then clear the default identity
162 * so that getDefaultIdentity() returns an empty name.
163 * @param identityName The default identity name.
164 */
165 virtual void
166 setDefaultIdentityInternal(const Name& identityName);
167
168 /**
169 * Set the default key name for the specified identity.
170 * @param keyName The key name.
171 * @param identityNameCheck (optional) The identity name to check the keyName.
172 */
173 virtual void
174 setDefaultKeyNameForIdentityInternal(const Name& keyName);
175
176 /**
177 * Set the default key name for the specified identity.
178 * @param keyName The key name.
179 * @param certificateName The certificate name.
180 */
181 virtual void
182 setDefaultCertificateNameForKeyInternal(const Name& certificateName);
183
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800184 /**
185 * Delete a certificate.
186 * @param certificateName The certificate name.
187 */
188 virtual void
189 deleteCertificateInfo(const Name &certificateName);
190
191 /**
192 * Delete a public key and related certificates.
193 * @param keyName The key name.
194 */
195 virtual void
196 deletePublicKeyInfo(const Name &keyName);
197
198 /**
199 * Delete an identity and related public keys and certificates.
200 * @param identity The identity name.
201 */
202 virtual void
203 deleteIdentityInfo(const Name &identity);
204
Jeff Thompson81842272013-09-25 16:12:33 -0700205
206private:
Jeff Thompson61805e92013-10-23 15:19:39 -0700207 class KeyRecord {
208 public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800209 KeyRecord(KeyType keyType, const PublicKey &key)
210 : keyType_(keyType), key_(key)
Jeff Thompson61805e92013-10-23 15:19:39 -0700211 {
212 }
213
214 const KeyType getKeyType() const { return keyType_; }
215
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800216 const PublicKey& getKey() { return key_; }
Jeff Thompson61805e92013-10-23 15:19:39 -0700217
218 private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800219 KeyType keyType_;
220 PublicKey key_;
Jeff Thompson61805e92013-10-23 15:19:39 -0700221 };
222
Jeff Thompson81842272013-09-25 16:12:33 -0700223 std::vector<std::string> identityStore_; /**< A list of name URI. */
224 std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800225 Name defaultKeyName_;
226 Name defaultCert_;
227
228 typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */
229 typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */
230
231 KeyStore keyStore_;
232 CertificateStore certificateStore_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700233};
234
Yingdi Yufc40d872014-02-18 12:56:04 -0800235} // namespace ndn
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700236
Yingdi Yufc40d872014-02-18 12:56:04 -0800237#endif //NDN_SECURITY_SEC_PUBLIC_INFO_MEMORY_HPP