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 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 12 | #include "../common.hpp" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 13 | #include "sec-public-info.hpp" |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 14 | |
Yingdi Yu | 874678f | 2014-01-22 19:30:34 -0800 | [diff] [blame] | 15 | struct sqlite3; |
| 16 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 17 | namespace ndn |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates |
| 22 | * using SQLite. |
| 23 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | class SecPublicInfoSqlite3 : public SecPublicInfo { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 25 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 26 | 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] | 27 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 28 | SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * The virtual Destructor. |
| 32 | */ |
| 33 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 34 | ~SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 35 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 36 | // from SecPublicInfo |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 65 | doesPublicKeyExist(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 74 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 81 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 82 | getPublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 88 | virtual inline void |
| 89 | activatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 95 | virtual inline void |
| 96 | deactivatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 110 | virtual 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 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 126 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 127 | getCertificate(const Name &certificateName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | /***************************************** |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 131 | * Default Getter * |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 157 | virtual std::vector<Name> |
| 158 | getAllIdentities(bool isDefault); |
| 159 | |
| 160 | virtual std::vector<Name> |
| 161 | getAllKeyNames(bool isDefault); |
| 162 | |
| 163 | virtual std::vector<Name> |
| 164 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 165 | |
| 166 | virtual std::vector<Name> |
| 167 | getAllCertificateNames(bool isDefault); |
| 168 | |
| 169 | virtual std::vector<Name> |
| 170 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
| 171 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 172 | protected: |
| 173 | /** |
| 174 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 175 | * so that getDefaultIdentity() returns an empty name. |
| 176 | * @param identityName The default identity name. |
| 177 | */ |
| 178 | virtual void |
| 179 | setDefaultIdentityInternal(const Name& identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 180 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 181 | /** |
| 182 | * Set the default key name for the specified identity. |
| 183 | * @param keyName The key name. |
| 184 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 185 | */ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 186 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 187 | setDefaultKeyNameForIdentityInternal(const Name& keyName); |
| 188 | |
| 189 | /** |
| 190 | * Set the default key name for the specified identity. |
| 191 | * @param keyName The key name. |
| 192 | * @param certificateName The certificate name. |
| 193 | */ |
| 194 | virtual void |
| 195 | setDefaultCertificateNameForKeyInternal(const Name& certificateName); |
| 196 | |
| 197 | private: |
| 198 | void |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 199 | updateKeyStatus(const Name& keyName, bool isActive); |
| 200 | |
| 201 | sqlite3 *database_; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 204 | void |
| 205 | SecPublicInfoSqlite3::activatePublicKey(const Name& keyName) |
| 206 | { |
| 207 | updateKeyStatus(keyName, true); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName) |
| 212 | { |
| 213 | updateKeyStatus(keyName, false); |
| 214 | } |
| 215 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 218 | #endif |