blob: 734d4d7017a24f3d9af9a4497f7b798bdd4f49e9 [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>
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
Alexander Afanasyev0c632112013-12-30 15:59:31 -0800145 virtual std::vector<Name>
146 getAllIdentities(bool isDefault);
147
148 virtual std::vector<Name>
149 getAllKeyNames(bool isDefault);
150
151 virtual std::vector<Name>
152 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault);
153
154 virtual std::vector<Name>
155 getAllCertificateNames(bool isDefault);
156
157 virtual std::vector<Name>
158 getAllCertificateNamesOfKey(const Name& keyName, 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
Jeff Thompson81842272013-09-25 16:12:33 -0700185
186private:
Jeff Thompson61805e92013-10-23 15:19:39 -0700187 class KeyRecord {
188 public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800189 KeyRecord(KeyType keyType, const PublicKey &key)
190 : keyType_(keyType), key_(key)
Jeff Thompson61805e92013-10-23 15:19:39 -0700191 {
192 }
193
194 const KeyType getKeyType() const { return keyType_; }
195
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800196 const PublicKey& getKey() { return key_; }
Jeff Thompson61805e92013-10-23 15:19:39 -0700197
198 private:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800199 KeyType keyType_;
200 PublicKey key_;
Jeff Thompson61805e92013-10-23 15:19:39 -0700201 };
202
Jeff Thompson81842272013-09-25 16:12:33 -0700203 std::vector<std::string> identityStore_; /**< A list of name URI. */
204 std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800205 Name defaultKeyName_;
206 Name defaultCert_;
207
208 typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */
209 typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */
210
211 KeyStore keyStore_;
212 CertificateStore certificateStore_;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700213};
214
215}
216
217#endif