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 | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 9 | #ifndef NDN_SECURITY_SEC_PUBLIC_INFO_SQLITE3_HPP |
| 10 | #define NDN_SECURITY_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 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 17 | namespace ndn { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * BasicIdentityStorage extends IdentityStorage to implement a basic storage of identity, public keys and certificates |
| 21 | * using SQLite. |
| 22 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 23 | class SecPublicInfoSqlite3 : public SecPublicInfo { |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 24 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 25 | 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] | 26 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 27 | SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * The virtual Destructor. |
| 31 | */ |
| 32 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 33 | ~SecPublicInfoSqlite3(); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 34 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 35 | // from SecPublicInfo |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Check if the specified identity already exists. |
| 38 | * @param identityName The identity name. |
| 39 | * @return true if the identity exists, otherwise false. |
| 40 | */ |
| 41 | virtual bool |
| 42 | doesIdentityExist(const Name& identityName); |
| 43 | |
| 44 | /** |
| 45 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 46 | * @param identityName The identity name to be added. |
| 47 | */ |
| 48 | virtual void |
| 49 | addIdentity(const Name& identityName); |
| 50 | |
| 51 | /** |
| 52 | * Revoke the identity. |
| 53 | * @return true if the identity was revoked, false if not. |
| 54 | */ |
| 55 | virtual bool |
| 56 | revokeIdentity(); |
| 57 | |
| 58 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 59 | * Check if the specified key already exists. |
| 60 | * @param keyName The name of the key. |
| 61 | * @return true if the key exists, otherwise false. |
| 62 | */ |
| 63 | virtual bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 64 | doesPublicKeyExist(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 67 | * Add a public key to the identity storage. |
| 68 | * @param keyName The name of the public key to be added. |
| 69 | * @param keyType Type of the public key to be added. |
| 70 | * @param publicKeyDer A blob of the public key DER to be added. |
| 71 | */ |
| 72 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 73 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Get the public key DER blob from the identity storage. |
| 77 | * @param keyName The name of the requested public key. |
| 78 | * @return The DER Blob. If not found, return a Blob with a null pointer. |
| 79 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 80 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 81 | getPublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 85 | * @param keyName name of the key |
| 86 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 87 | virtual inline void |
| 88 | activatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 92 | * @param keyName name of the key |
| 93 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 94 | virtual inline void |
| 95 | deactivatePublicKey(const Name& keyName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Check if the specified certificate already exists. |
| 99 | * @param certificateName The name of the certificate. |
| 100 | * @return true if the certificate exists, otherwise false. |
| 101 | */ |
| 102 | virtual bool |
| 103 | doesCertificateExist(const Name& certificateName); |
| 104 | |
| 105 | /** |
| 106 | * Add a certificate in to the identity storage without checking if the identity and key exists. |
| 107 | * @param certificate The certificate to be added. |
| 108 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 109 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 110 | addAnyCertificate (const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 114 | * @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] | 115 | */ |
| 116 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 117 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Get a certificate from the identity storage. |
| 121 | * @param certificateName The name of the requested certificate. |
| 122 | * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded. |
| 123 | * @return The requested certificate. If not found, return a shared_ptr with a null pointer. |
| 124 | */ |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 125 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 126 | getCertificate(const Name &certificateName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 127 | |
| 128 | |
| 129 | /***************************************** |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 130 | * Default Getter * |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 131 | *****************************************/ |
| 132 | |
| 133 | /** |
| 134 | * Get the default identity. |
| 135 | * @param return The name of default identity, or an empty name if there is no default. |
| 136 | */ |
| 137 | virtual Name |
| 138 | getDefaultIdentity(); |
| 139 | |
| 140 | /** |
| 141 | * Get the default key name for the specified identity. |
| 142 | * @param identityName The identity name. |
| 143 | * @return The default key name. |
| 144 | */ |
| 145 | virtual Name |
| 146 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 147 | |
| 148 | /** |
| 149 | * Get the default certificate name for the specified key. |
| 150 | * @param keyName The key name. |
| 151 | * @return The default certificate name. |
| 152 | */ |
| 153 | virtual Name |
| 154 | getDefaultCertificateNameForKey(const Name& keyName); |
| 155 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 156 | virtual void |
| 157 | getAllIdentities(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 158 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 159 | virtual void |
| 160 | getAllKeyNames(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 161 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 162 | virtual void |
| 163 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 164 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 165 | virtual void |
| 166 | getAllCertificateNames(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 167 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 168 | virtual void |
| 169 | getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 170 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 171 | protected: |
| 172 | /** |
| 173 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 174 | * so that getDefaultIdentity() returns an empty name. |
| 175 | * @param identityName The default identity name. |
| 176 | */ |
| 177 | virtual void |
| 178 | setDefaultIdentityInternal(const Name& identityName); |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 179 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 180 | /** |
| 181 | * Set the default key name for the specified identity. |
| 182 | * @param keyName The key name. |
| 183 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 184 | */ |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 185 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 186 | setDefaultKeyNameForIdentityInternal(const Name& keyName); |
| 187 | |
| 188 | /** |
| 189 | * Set the default key name for the specified identity. |
| 190 | * @param keyName The key name. |
| 191 | * @param certificateName The certificate name. |
| 192 | */ |
| 193 | virtual void |
| 194 | setDefaultCertificateNameForKeyInternal(const Name& certificateName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Delete a certificate. |
| 198 | * @param certificateName The certificate name. |
| 199 | */ |
| 200 | virtual void |
| 201 | deleteCertificateInfo(const Name &certificateName); |
| 202 | |
| 203 | /** |
| 204 | * Delete a public key and related certificates. |
| 205 | * @param keyName The key name. |
| 206 | */ |
| 207 | virtual void |
| 208 | deletePublicKeyInfo(const Name &keyName); |
| 209 | |
| 210 | /** |
| 211 | * Delete an identity and related public keys and certificates. |
| 212 | * @param identity The identity name. |
| 213 | */ |
| 214 | virtual void |
| 215 | deleteIdentityInfo(const Name &identity); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 216 | |
| 217 | private: |
| 218 | void |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 219 | updateKeyStatus(const Name& keyName, bool isActive); |
| 220 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 221 | sqlite3 * m_database; |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 224 | void |
| 225 | SecPublicInfoSqlite3::activatePublicKey(const Name& keyName) |
| 226 | { |
| 227 | updateKeyStatus(keyName, true); |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | SecPublicInfoSqlite3::deactivatePublicKey(const Name& keyName) |
| 232 | { |
| 233 | updateKeyStatus(keyName, false); |
| 234 | } |
| 235 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 236 | } // namespace ndn |
Jeff Thompson | 7ca11f2 | 2013-10-04 19:01:30 -0700 | [diff] [blame] | 237 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 238 | #endif //NDN_SECURITY_SEC_PUBLIC_INFO_SQLITE3_HPP |