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: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 24 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 25 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | /** |
| 27 | * The virtual Destructor. |
| 28 | */ |
| 29 | virtual |
| 30 | ~MemoryIdentityStorage(); |
| 31 | |
| 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 55 | * 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 |
| 60 | doesKeyExist(const Name& keyName); |
| 61 | |
| 62 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 63 | * 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 |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 69 | addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Get the public key DER blob from the identity storage. |
| 73 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 74 | * @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] | 75 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 76 | virtual ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 77 | getKey(const Name& keyName); |
| 78 | |
| 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 |
| 84 | activateKey(const Name& keyName); |
| 85 | |
| 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 |
| 91 | deactivateKey(const Name& keyName); |
| 92 | |
| 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 Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 103 | * @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] | 104 | */ |
| 105 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 106 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 107 | |
| 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 Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 112 | * @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] | 113 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 114 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 115 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 116 | |
| 117 | |
| 118 | /***************************************** |
| 119 | * Get/Set Default * |
| 120 | *****************************************/ |
| 121 | |
| 122 | /** |
| 123 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 124 | * @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] | 125 | */ |
| 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 | |
| 145 | /** |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 146 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 147 | * so that getDefaultIdentity() returns an empty name. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | * @param identityName The default identity name. |
| 149 | */ |
| 150 | virtual void |
| 151 | setDefaultIdentity(const Name& identityName); |
| 152 | |
| 153 | /** |
| 154 | * Set the default key name for the specified identity. |
| 155 | * @param keyName The key name. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 156 | * @param identityNameCheck (optional) The identity name to check the keyName. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 157 | */ |
| 158 | virtual void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 159 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Set the default key name for the specified identity. |
| 163 | * @param keyName The key name. |
| 164 | * @param certificateName The certificate name. |
| 165 | */ |
| 166 | virtual void |
| 167 | setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 168 | |
| 169 | virtual std::vector<Name> |
| 170 | getAllIdentities(bool isDefault); |
| 171 | |
| 172 | virtual std::vector<Name> |
| 173 | getAllKeyNames(bool isDefault); |
| 174 | |
| 175 | virtual std::vector<Name> |
| 176 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 177 | |
| 178 | virtual std::vector<Name> |
| 179 | getAllCertificateNames(bool isDefault); |
| 180 | |
| 181 | virtual std::vector<Name> |
| 182 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 183 | |
| 184 | private: |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 185 | class KeyRecord { |
| 186 | public: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 187 | KeyRecord(KeyType keyType, const PublicKey &key) |
| 188 | : keyType_(keyType), key_(key) |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 189 | { |
| 190 | } |
| 191 | |
| 192 | const KeyType getKeyType() const { return keyType_; } |
| 193 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 194 | const PublicKey& getKey() { return key_; } |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 197 | KeyType keyType_; |
| 198 | PublicKey key_; |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 201 | std::vector<std::string> identityStore_; /**< A list of name URI. */ |
| 202 | std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame^] | 203 | Name defaultKeyName_; |
| 204 | Name defaultCert_; |
| 205 | |
| 206 | typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */ |
| 207 | typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */ |
| 208 | |
| 209 | KeyStore keyStore_; |
| 210 | CertificateStore certificateStore_; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | #endif |