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: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_IDENTITY_STORAGE_HPP |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 10 | #define NDN_IDENTITY_STORAGE_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 11 | |
| 12 | #include "../../name.hpp" |
| 13 | #include "../security-common.hpp" |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 14 | #include "../certificate/public-key.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | class Certificate; |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 19 | class IdentityCertificate; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 20 | class Data; |
| 21 | |
| 22 | /** |
| 23 | * IdentityStorage is a base class for the storage of identity, public keys and certificates. |
| 24 | * Private keys are stored in PrivateKeyStorage. |
| 25 | * This is an abstract base class. A subclass must implement the methods. |
| 26 | */ |
| 27 | class IdentityStorage { |
| 28 | public: |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 29 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 30 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 31 | /** |
| 32 | * The virtual Destructor. |
| 33 | */ |
| 34 | virtual |
| 35 | ~IdentityStorage() {} |
| 36 | |
| 37 | /** |
| 38 | * Check if the specified identity already exists. |
| 39 | * @param identityName The identity name. |
| 40 | * @return true if the identity exists, otherwise false. |
| 41 | */ |
| 42 | virtual bool |
| 43 | doesIdentityExist(const Name& identityName) = 0; |
| 44 | |
| 45 | /** |
| 46 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 47 | * @param identityName The identity name to be added. |
| 48 | */ |
| 49 | virtual void |
| 50 | addIdentity(const Name& identityName) = 0; |
| 51 | |
| 52 | /** |
| 53 | * Revoke the identity. |
| 54 | * @return true if the identity was revoked, false if not. |
| 55 | */ |
| 56 | virtual bool |
| 57 | revokeIdentity() = 0; |
| 58 | |
| 59 | /** |
| 60 | * Generate a name for a new key belonging to the identity. |
| 61 | * @param identityName The identity name. |
| 62 | * @param useKsk If true, generate a KSK name, otherwise a DSK name. |
| 63 | * @return The generated key name. |
| 64 | */ |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 65 | Name |
| 66 | getNewKeyName(const Name& identityName, bool useKsk); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Check if the specified key already exists. |
| 70 | * @param keyName The name of the key. |
| 71 | * @return true if the key exists, otherwise false. |
| 72 | */ |
| 73 | virtual bool |
| 74 | doesKeyExist(const Name& keyName) = 0; |
| 75 | |
| 76 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 77 | * Add a public key to the identity storage. |
| 78 | * @param keyName The name of the public key to be added. |
| 79 | * @param keyType Type of the public key to be added. |
| 80 | * @param publicKeyDer A blob of the public key DER to be added. |
| 81 | */ |
| 82 | virtual void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 83 | addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Get the public key DER blob from the identity storage. |
| 87 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 88 | * @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] | 89 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 90 | virtual ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 91 | getKey(const Name& keyName) = 0; |
| 92 | |
| 93 | /** |
| 94 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 95 | * @param keyName name of the key |
| 96 | */ |
| 97 | virtual void |
| 98 | activateKey(const Name& keyName) = 0; |
| 99 | |
| 100 | /** |
| 101 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 102 | * @param keyName name of the key |
| 103 | */ |
| 104 | virtual void |
| 105 | deactivateKey(const Name& keyName) = 0; |
| 106 | |
| 107 | /** |
| 108 | * Check if the specified certificate already exists. |
| 109 | * @param certificateName The name of the certificate. |
| 110 | * @return true if the certificate exists, otherwise false. |
| 111 | */ |
| 112 | virtual bool |
| 113 | doesCertificateExist(const Name& certificateName) = 0; |
| 114 | |
| 115 | /** |
| 116 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 117 | * @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] | 118 | */ |
| 119 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 120 | addCertificate(const IdentityCertificate& certificate) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Get a certificate from the identity storage. |
| 124 | * @param certificateName The name of the requested certificate. |
| 125 | * @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] | 126 | * @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] | 127 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 128 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 129 | getCertificate(const Name &certificateName, bool allowAny = false) = 0; |
| 130 | |
| 131 | |
| 132 | /***************************************** |
| 133 | * Get/Set Default * |
| 134 | *****************************************/ |
| 135 | |
| 136 | /** |
| 137 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 138 | * @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] | 139 | */ |
| 140 | virtual Name |
| 141 | getDefaultIdentity() = 0; |
| 142 | |
| 143 | /** |
| 144 | * Get the default key name for the specified identity. |
| 145 | * @param identityName The identity name. |
| 146 | * @return The default key name. |
| 147 | */ |
| 148 | virtual Name |
| 149 | getDefaultKeyNameForIdentity(const Name& identityName) = 0; |
| 150 | |
| 151 | /** |
| 152 | * Get the default certificate name for the specified identity. |
| 153 | * @param identityName The identity name. |
| 154 | * @return The default certificate name. |
| 155 | */ |
| 156 | Name |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 157 | getDefaultCertificateNameForIdentity(const Name& identityName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Get the default certificate name for the specified key. |
| 161 | * @param keyName The key name. |
| 162 | * @return The default certificate name. |
| 163 | */ |
| 164 | virtual Name |
| 165 | getDefaultCertificateNameForKey(const Name& keyName) = 0; |
| 166 | |
| 167 | /** |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 168 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 169 | * so that getDefaultIdentity() returns an empty name. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | * @param identityName The default identity name. |
| 171 | */ |
| 172 | virtual void |
| 173 | setDefaultIdentity(const Name& identityName) = 0; |
| 174 | |
| 175 | /** |
| 176 | * Set the default key name for the specified identity. |
| 177 | * @param keyName The key name. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 178 | * @param identityNameCheck (optional) The identity name to check the keyName. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 179 | */ |
| 180 | virtual void |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 181 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Set the default key name for the specified identity. |
| 185 | * @param keyName The key name. |
| 186 | * @param certificateName The certificate name. |
| 187 | */ |
| 188 | virtual void |
| 189 | setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName) = 0; |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 190 | |
| 191 | |
| 192 | virtual std::vector<Name> |
| 193 | getAllIdentities(bool isDefault) = 0; |
| 194 | |
| 195 | virtual std::vector<Name> |
| 196 | getAllKeyNames(bool isDefault) = 0; |
| 197 | |
| 198 | virtual std::vector<Name> |
| 199 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault) = 0; |
| 200 | |
| 201 | virtual std::vector<Name> |
| 202 | getAllCertificateNames(bool isDefault) = 0; |
| 203 | |
| 204 | virtual std::vector<Name> |
| 205 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault) = 0; |
| 206 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | #endif |