Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -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> |
Jeff Thompson | 22285ec | 2013-10-22 17:43:02 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_BASIC_IDENTITY_STORAGE_H |
| 10 | #define NDN_BASIC_IDENTITY_STORAGE_H |
| 11 | |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 12 | // Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_SQLITE3. |
Jeff Thompson | 6e22904 | 2013-10-10 11:09:49 -0700 | [diff] [blame] | 13 | #include <ndn-cpp/ndn-cpp-config.h> |
Jeff Thompson | 1975def | 2013-10-09 17:06:43 -0700 | [diff] [blame] | 14 | #ifdef NDN_CPP_HAVE_SQLITE3 |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 15 | |
| 16 | #include <sqlite3.h> |
| 17 | #include "../../common.hpp" |
| 18 | #include "identity-storage.hpp" |
| 19 | |
| 20 | namespace ndn |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates |
| 25 | * using SQLite. |
| 26 | */ |
| 27 | class BasicIdentityStorage : public IdentityStorage { |
| 28 | public: |
| 29 | BasicIdentityStorage(); |
| 30 | |
| 31 | /** |
| 32 | * The virtual Destructor. |
| 33 | */ |
| 34 | virtual |
| 35 | ~BasicIdentityStorage(); |
| 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); |
| 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); |
| 51 | |
| 52 | /** |
| 53 | * Revoke the identity. |
| 54 | * @return true if the identity was revoked, false if not. |
| 55 | */ |
| 56 | virtual bool |
| 57 | revokeIdentity(); |
| 58 | |
| 59 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 60 | * Check if the specified key already exists. |
| 61 | * @param keyName The name of the key. |
| 62 | * @return true if the key exists, otherwise false. |
| 63 | */ |
| 64 | virtual bool |
| 65 | doesKeyExist(const Name& keyName); |
| 66 | |
| 67 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 68 | * Add a public key to the identity storage. |
| 69 | * @param keyName The name of the public key to be added. |
| 70 | * @param keyType Type of the public key to be added. |
| 71 | * @param publicKeyDer A blob of the public key DER to be added. |
| 72 | */ |
| 73 | virtual void |
| 74 | addKey(const Name& keyName, KeyType keyType, const Blob& publicKeyDer); |
| 75 | |
| 76 | /** |
| 77 | * Get the public key DER blob from the identity storage. |
| 78 | * @param keyName The name of the requested public key. |
| 79 | * @return The DER Blob. If not found, return a Blob with a null pointer. |
| 80 | */ |
| 81 | virtual Blob |
| 82 | getKey(const Name& keyName); |
| 83 | |
| 84 | /** |
| 85 | * Activate 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 | activateKey(const Name& keyName); |
| 90 | |
| 91 | /** |
| 92 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 93 | * @param keyName name of the key |
| 94 | */ |
| 95 | virtual void |
| 96 | deactivateKey(const Name& keyName); |
| 97 | |
| 98 | /** |
| 99 | * Check if the specified certificate already exists. |
| 100 | * @param certificateName The name of the certificate. |
| 101 | * @return true if the certificate exists, otherwise false. |
| 102 | */ |
| 103 | virtual bool |
| 104 | doesCertificateExist(const Name& certificateName); |
| 105 | |
| 106 | /** |
| 107 | * Add a certificate in to the identity storage without checking if the identity and key exists. |
| 108 | * @param certificate The certificate to be added. |
| 109 | */ |
| 110 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 111 | addAnyCertificate (const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 115 | * @param certificate The certificate to be added. This makes a copy of the certificate. |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 116 | */ |
| 117 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 118 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Get a certificate from the identity storage. |
| 122 | * @param certificateName The name of the requested certificate. |
| 123 | * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded. |
| 124 | * @return The requested certificate. If not found, return a shared_ptr with a null pointer. |
| 125 | */ |
Jeff Thompson | 3bd90bc | 2013-10-19 16:40:14 -0700 | [diff] [blame] | 126 | virtual ptr_lib::shared_ptr<Data> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 127 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 128 | |
| 129 | |
| 130 | /***************************************** |
| 131 | * Get/Set Default * |
| 132 | *****************************************/ |
| 133 | |
| 134 | /** |
| 135 | * Get the default identity. |
| 136 | * @param return The name of default identity, or an empty name if there is no default. |
| 137 | */ |
| 138 | virtual Name |
| 139 | getDefaultIdentity(); |
| 140 | |
| 141 | /** |
| 142 | * Get the default key name for the specified identity. |
| 143 | * @param identityName The identity name. |
| 144 | * @return The default key name. |
| 145 | */ |
| 146 | virtual Name |
| 147 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 148 | |
| 149 | /** |
| 150 | * Get the default certificate name for the specified key. |
| 151 | * @param keyName The key name. |
| 152 | * @return The default certificate name. |
| 153 | */ |
| 154 | virtual Name |
| 155 | getDefaultCertificateNameForKey(const Name& keyName); |
| 156 | |
| 157 | /** |
| 158 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 159 | * so that getDefaultIdentity() returns an empty name. |
| 160 | * @param identityName The default identity name. |
| 161 | */ |
| 162 | virtual void |
| 163 | setDefaultIdentity(const Name& identityName); |
| 164 | |
| 165 | /** |
| 166 | * Set the default key name for the specified identity. |
| 167 | * @param keyName The key name. |
| 168 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 169 | */ |
| 170 | virtual void |
| 171 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()); |
| 172 | |
| 173 | /** |
| 174 | * Set the default key name for the specified identity. |
| 175 | * @param keyName The key name. |
| 176 | * @param certificateName The certificate name. |
| 177 | */ |
| 178 | virtual void |
| 179 | setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName); |
| 180 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 181 | |
| 182 | virtual std::vector<Name> |
| 183 | getAllIdentities(bool isDefault); |
| 184 | |
| 185 | virtual std::vector<Name> |
| 186 | getAllKeyNames(bool isDefault); |
| 187 | |
| 188 | virtual std::vector<Name> |
| 189 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 190 | |
| 191 | virtual std::vector<Name> |
| 192 | getAllCertificateNames(bool isDefault); |
| 193 | |
| 194 | virtual std::vector<Name> |
| 195 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
| 196 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 197 | private: |
| 198 | |
| 199 | virtual void |
| 200 | updateKeyStatus(const Name& keyName, bool isActive); |
| 201 | |
| 202 | sqlite3 *database_; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } |
| 206 | |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 207 | #endif // NDN_CPP_HAVE_SQLITE3 |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 208 | |
| 209 | #endif |