Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 9 | #define NDN_MEMORY_IDENTITY_STORAGE_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 11 | #include <vector> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 12 | #include <map> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | #include "identity-storage.hpp" |
| 14 | |
| 15 | namespace 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 | */ |
| 22 | class MemoryIdentityStorage : public IdentityStorage { |
| 23 | public: |
| 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 53 | * 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 61 | * 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 Thompson | bd04b07 | 2013-09-27 15:14:09 -0700 | [diff] [blame] | 67 | addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Get the public key DER blob from the identity storage. |
| 71 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 72 | * @return The DER Blob. If not found, return a Blob with a null pointer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 73 | */ |
| 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 Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 101 | * @param certificate The certificate to be added. This makes a copy of the certificate. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 102 | */ |
| 103 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 104 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | |
| 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 Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 110 | * @return The requested certificate. If not found, return a shared_ptr with a null pointer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 111 | */ |
Jeff Thompson | 3bd90bc | 2013-10-19 16:40:14 -0700 | [diff] [blame] | 112 | virtual ptr_lib::shared_ptr<Data> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 113 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 114 | |
| 115 | |
| 116 | /***************************************** |
| 117 | * Get/Set Default * |
| 118 | *****************************************/ |
| 119 | |
| 120 | /** |
| 121 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 122 | * @param return The name of default identity, or an empty name if there is no default. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 123 | */ |
| 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 Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 144 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 145 | * so that getDefaultIdentity() returns an empty name. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 146 | * @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 Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 154 | * @param identityNameCheck (optional) The identity name to check the keyName. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 155 | */ |
| 156 | virtual void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 157 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 158 | |
| 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 Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame^] | 166 | |
| 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 Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 181 | |
| 182 | private: |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 183 | 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 Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 199 | std::vector<std::string> identityStore_; /**< A list of name URI. */ |
| 200 | std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */ |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 201 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } |
| 206 | |
| 207 | #endif |