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 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 9 | #ifndef NDN_SEC_PUBLIC_INFO_SQLITE3_HPP |
| 10 | #define NDN_SEC_PUBLIC_INFO_SQLITE3_HPP |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 18 | #include "sec-public-info.hpp" |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 27 | class SecPublicInfoSqlite3 : public SecPublicInfo { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 28 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 29 | struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} }; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 31 | SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * The virtual Destructor. |
| 35 | */ |
| 36 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 37 | ~SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 39 | // from SecPublicInfo |
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 |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 68 | doesPublicKeyExist(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 77 | addPublicKey(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> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 85 | getPublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 91 | virtual inline void |
| 92 | activatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 98 | virtual inline void |
| 99 | deactivatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 113 | virtual 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> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 130 | getCertificate(const Name &certificateName, bool allowAny); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 131 | |
| 132 | |
| 133 | /***************************************** |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 134 | * Default Getter * |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 160 | virtual std::vector<Name> |
| 161 | getAllIdentities(bool isDefault); |
| 162 | |
| 163 | virtual std::vector<Name> |
| 164 | getAllKeyNames(bool isDefault); |
| 165 | |
| 166 | virtual std::vector<Name> |
| 167 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 168 | |
| 169 | virtual std::vector<Name> |
| 170 | getAllCertificateNames(bool isDefault); |
| 171 | |
| 172 | virtual std::vector<Name> |
| 173 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
| 174 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 175 | protected: |
| 176 | /** |
| 177 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 178 | * so that getDefaultIdentity() returns an empty name. |
| 179 | * @param identityName The default identity name. |
| 180 | */ |
| 181 | virtual void |
| 182 | setDefaultIdentityInternal(const Name& identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 183 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Set the default key name for the specified identity. |
| 186 | * @param keyName The key name. |
| 187 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 188 | */ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 189 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 190 | setDefaultKeyNameForIdentityInternal(const Name& keyName); |
| 191 | |
| 192 | /** |
| 193 | * Set the default key name for the specified identity. |
| 194 | * @param keyName The key name. |
| 195 | * @param certificateName The certificate name. |
| 196 | */ |
| 197 | virtual void |
| 198 | setDefaultCertificateNameForKeyInternal(const Name& certificateName); |
| 199 | |
| 200 | private: |
| 201 | void |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 202 | updateKeyStatus(const Name& keyName, bool isActive); |
| 203 | |
| 204 | sqlite3 *database_; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 207 | void |
| 208 | SecPublicInfoSqlite3::activatePublicKey(const Name& keyName) |
| 209 | { |
| 210 | updateKeyStatus(keyName, true); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName) |
| 215 | { |
| 216 | updateKeyStatus(keyName, false); |
| 217 | } |
| 218 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 221 | #endif // NDN_CPP_HAVE_SQLITE3 |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 222 | |
| 223 | #endif |