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: |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 29 | struct Error : public IdentityStorage::Error { Error(const std::string &what) : IdentityStorage::Error(what) {} }; |
| 30 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 31 | BasicIdentityStorage(); |
| 32 | |
| 33 | /** |
| 34 | * The virtual Destructor. |
| 35 | */ |
| 36 | virtual |
| 37 | ~BasicIdentityStorage(); |
| 38 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 39 | // from IdentityStorage |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Check if the specified identity already exists. |
| 42 | * @param identityName The identity name. |
| 43 | * @return true if the identity exists, otherwise false. |
| 44 | */ |
| 45 | virtual bool |
| 46 | doesIdentityExist(const Name& identityName); |
| 47 | |
| 48 | /** |
| 49 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 50 | * @param identityName The identity name to be added. |
| 51 | */ |
| 52 | virtual void |
| 53 | addIdentity(const Name& identityName); |
| 54 | |
| 55 | /** |
| 56 | * Revoke the identity. |
| 57 | * @return true if the identity was revoked, false if not. |
| 58 | */ |
| 59 | virtual bool |
| 60 | revokeIdentity(); |
| 61 | |
| 62 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 63 | * Check if the specified key already exists. |
| 64 | * @param keyName The name of the key. |
| 65 | * @return true if the key exists, otherwise false. |
| 66 | */ |
| 67 | virtual bool |
| 68 | doesKeyExist(const Name& keyName); |
| 69 | |
| 70 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 71 | * Add a public key to the identity storage. |
| 72 | * @param keyName The name of the public key to be added. |
| 73 | * @param keyType Type of the public key to be added. |
| 74 | * @param publicKeyDer A blob of the public key DER to be added. |
| 75 | */ |
| 76 | virtual void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 77 | addKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Get the public key DER blob from the identity storage. |
| 81 | * @param keyName The name of the requested public key. |
| 82 | * @return The DER Blob. If not found, return a Blob with a null pointer. |
| 83 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 84 | virtual ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 85 | getKey(const Name& keyName); |
| 86 | |
| 87 | /** |
| 88 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 89 | * @param keyName name of the key |
| 90 | */ |
| 91 | virtual void |
| 92 | activateKey(const Name& keyName); |
| 93 | |
| 94 | /** |
| 95 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 96 | * @param keyName name of the key |
| 97 | */ |
| 98 | virtual void |
| 99 | deactivateKey(const Name& keyName); |
| 100 | |
| 101 | /** |
| 102 | * Check if the specified certificate already exists. |
| 103 | * @param certificateName The name of the certificate. |
| 104 | * @return true if the certificate exists, otherwise false. |
| 105 | */ |
| 106 | virtual bool |
| 107 | doesCertificateExist(const Name& certificateName); |
| 108 | |
| 109 | /** |
| 110 | * Add a certificate in to the identity storage without checking if the identity and key exists. |
| 111 | * @param certificate The certificate to be added. |
| 112 | */ |
| 113 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 114 | addAnyCertificate (const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 118 | * @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] | 119 | */ |
| 120 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 121 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Get a certificate from the identity storage. |
| 125 | * @param certificateName The name of the requested certificate. |
| 126 | * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded. |
| 127 | * @return The requested certificate. If not found, return a shared_ptr with a null pointer. |
| 128 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame^] | 129 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 130 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 131 | |
| 132 | |
| 133 | /***************************************** |
| 134 | * Get/Set Default * |
| 135 | *****************************************/ |
| 136 | |
| 137 | /** |
| 138 | * Get the default identity. |
| 139 | * @param return The name of default identity, or an empty name if there is no default. |
| 140 | */ |
| 141 | virtual Name |
| 142 | getDefaultIdentity(); |
| 143 | |
| 144 | /** |
| 145 | * Get the default key name for the specified identity. |
| 146 | * @param identityName The identity name. |
| 147 | * @return The default key name. |
| 148 | */ |
| 149 | virtual Name |
| 150 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 151 | |
| 152 | /** |
| 153 | * Get the default certificate name for the specified key. |
| 154 | * @param keyName The key name. |
| 155 | * @return The default certificate name. |
| 156 | */ |
| 157 | virtual Name |
| 158 | getDefaultCertificateNameForKey(const Name& keyName); |
| 159 | |
| 160 | /** |
| 161 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 162 | * so that getDefaultIdentity() returns an empty name. |
| 163 | * @param identityName The default identity name. |
| 164 | */ |
| 165 | virtual void |
| 166 | setDefaultIdentity(const Name& identityName); |
| 167 | |
| 168 | /** |
| 169 | * Set the default key name for the specified identity. |
| 170 | * @param keyName The key name. |
| 171 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 172 | */ |
| 173 | virtual void |
| 174 | setDefaultKeyNameForIdentity(const Name& keyName, const Name& identityNameCheck = Name()); |
| 175 | |
| 176 | /** |
| 177 | * Set the default key name for the specified identity. |
| 178 | * @param keyName The key name. |
| 179 | * @param certificateName The certificate name. |
| 180 | */ |
| 181 | virtual void |
| 182 | setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName); |
| 183 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 184 | |
| 185 | virtual std::vector<Name> |
| 186 | getAllIdentities(bool isDefault); |
| 187 | |
| 188 | virtual std::vector<Name> |
| 189 | getAllKeyNames(bool isDefault); |
| 190 | |
| 191 | virtual std::vector<Name> |
| 192 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 193 | |
| 194 | virtual std::vector<Name> |
| 195 | getAllCertificateNames(bool isDefault); |
| 196 | |
| 197 | virtual std::vector<Name> |
| 198 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
| 199 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 200 | private: |
| 201 | |
| 202 | virtual void |
| 203 | updateKeyStatus(const Name& keyName, bool isActive); |
| 204 | |
| 205 | sqlite3 *database_; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | } |
| 209 | |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 210 | #endif // NDN_CPP_HAVE_SQLITE3 |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 211 | |
| 212 | #endif |