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 | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 12 | #include "identity-storage.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | /** |
| 17 | * MemoryIdentityStorage 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 MemoryIdentityStorage object. |
| 19 | * To use permanent file-based storage, see BasicIdentityStorage. |
| 20 | */ |
| 21 | class MemoryIdentityStorage : public IdentityStorage { |
| 22 | public: |
| 23 | /** |
| 24 | * The virtual Destructor. |
| 25 | */ |
| 26 | virtual |
| 27 | ~MemoryIdentityStorage(); |
| 28 | |
| 29 | /** |
| 30 | * Check if the specified identity already exists. |
| 31 | * @param identityName The identity name. |
| 32 | * @return true if the identity exists, otherwise false. |
| 33 | */ |
| 34 | virtual bool |
| 35 | doesIdentityExist(const Name& identityName); |
| 36 | |
| 37 | /** |
| 38 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 39 | * @param identityName The identity name to be added. |
| 40 | */ |
| 41 | virtual void |
| 42 | addIdentity(const Name& identityName); |
| 43 | |
| 44 | /** |
| 45 | * Revoke the identity. |
| 46 | * @return true if the identity was revoked, false if not. |
| 47 | */ |
| 48 | virtual bool |
| 49 | revokeIdentity(); |
| 50 | |
| 51 | /** |
| 52 | * Generate a name for a new key belonging to the identity. |
| 53 | * @param identityName The identity name. |
| 54 | * @param useKsk If true, generate a KSK name, otherwise a DSK name. |
| 55 | * @return The generated key name. |
| 56 | */ |
| 57 | virtual Name |
| 58 | getNewKeyName(const Name& identityName, bool useKsk); |
| 59 | |
| 60 | /** |
| 61 | * Check if the specified key already exists. |
| 62 | * @param keyName The name of the key. |
| 63 | * @return true if the key exists, otherwise false. |
| 64 | */ |
| 65 | virtual bool |
| 66 | doesKeyExist(const Name& keyName); |
| 67 | |
| 68 | /** |
| 69 | * Extract the key name from the certificate name. |
| 70 | * @param certificateName The certificate name to be processed. |
| 71 | */ |
| 72 | virtual Name |
| 73 | getKeyNameForCertificate(const Name& certificateName); |
| 74 | |
| 75 | /** |
| 76 | * Add a public key to the identity storage. |
| 77 | * @param keyName The name of the public key to be added. |
| 78 | * @param keyType Type of the public key to be added. |
| 79 | * @param publicKeyDer A blob of the public key DER to be added. |
| 80 | */ |
| 81 | virtual void |
Jeff Thompson | bd04b07 | 2013-09-27 15:14:09 -0700 | [diff] [blame] | 82 | addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Get the public key DER blob from the identity storage. |
| 86 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 87 | * @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] | 88 | */ |
| 89 | virtual Blob |
| 90 | getKey(const Name& keyName); |
| 91 | |
| 92 | /** |
| 93 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 94 | * @param keyName name of the key |
| 95 | */ |
| 96 | virtual void |
| 97 | activateKey(const Name& keyName); |
| 98 | |
| 99 | /** |
| 100 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 101 | * @param keyName name of the key |
| 102 | */ |
| 103 | virtual void |
| 104 | deactivateKey(const Name& keyName); |
| 105 | |
| 106 | /** |
| 107 | * Check if the specified certificate already exists. |
| 108 | * @param certificateName The name of the certificate. |
| 109 | * @return true if the certificate exists, otherwise false. |
| 110 | */ |
| 111 | virtual bool |
| 112 | doesCertificateExist(const Name& certificateName); |
| 113 | |
| 114 | /** |
| 115 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 116 | * @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] | 117 | */ |
| 118 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 119 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Get a certificate from the identity storage. |
| 123 | * @param certificateName The name of the requested certificate. |
| 124 | * @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] | 125 | * @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] | 126 | */ |
Jeff Thompson | a6fd638 | 2013-09-24 15:23:37 -0700 | [diff] [blame] | 127 | virtual ptr_lib::shared_ptr<Certificate> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 128 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 129 | |
| 130 | |
| 131 | /***************************************** |
| 132 | * Get/Set Default * |
| 133 | *****************************************/ |
| 134 | |
| 135 | /** |
| 136 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 137 | * @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] | 138 | */ |
| 139 | virtual Name |
| 140 | getDefaultIdentity(); |
| 141 | |
| 142 | /** |
| 143 | * Get the default key name for the specified identity. |
| 144 | * @param identityName The identity name. |
| 145 | * @return The default key name. |
| 146 | */ |
| 147 | virtual Name |
| 148 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 149 | |
| 150 | /** |
| 151 | * Get the default certificate name for the specified key. |
| 152 | * @param keyName The key name. |
| 153 | * @return The default certificate name. |
| 154 | */ |
| 155 | virtual Name |
| 156 | getDefaultCertificateNameForKey(const Name& keyName); |
| 157 | |
| 158 | /** |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 159 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 160 | * so that getDefaultIdentity() returns an empty name. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 161 | * @param identityName The default identity name. |
| 162 | */ |
| 163 | virtual void |
| 164 | setDefaultIdentity(const Name& identityName); |
| 165 | |
| 166 | /** |
| 167 | * Set the default key name for the specified identity. |
| 168 | * @param keyName The key name. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 169 | * @param identityNameCheck (optional) The identity name to check the keyName. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | */ |
| 171 | virtual void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 172 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Set the default key name for the specified identity. |
| 176 | * @param keyName The key name. |
| 177 | * @param certificateName The certificate name. |
| 178 | */ |
| 179 | virtual void |
| 180 | setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName); |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 181 | |
| 182 | private: |
| 183 | std::vector<std::string> identityStore_; /**< A list of name URI. */ |
| 184 | std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | #endif |