Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -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: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 8 | #ifndef NDN_SEC_PUBLIC_INFO_MEMORY_HPP |
| 9 | #define NDN_SEC_PUBLIC_INFO_MEMORY_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 11 | #include "../common.hpp" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 12 | #include "sec-public-info.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | /** |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 17 | * MemoryKeyMetaInfo extends IdentityStorage and implements its methods to store identity, public key and certificate objects in memory. |
| 18 | * The application must get the objects through its own means and add the objects to the MemoryKeyMetaInfo object. |
| 19 | * To use permanent file-based storage, see BasicKeyMetaInfo. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 20 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 21 | class SecPublicInfoMemory : public SecPublicInfo { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 22 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 23 | struct Error : public SecPublicInfo::Error { Error(const std::string &what) : SecPublicInfo::Error(what) {} }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 24 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 25 | /** |
| 26 | * The virtual Destructor. |
| 27 | */ |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 28 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 29 | ~SecPublicInfoMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Check if the specified identity already exists. |
| 33 | * @param identityName The identity name. |
| 34 | * @return true if the identity exists, otherwise false. |
| 35 | */ |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 36 | virtual bool |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 37 | doesIdentityExist(const Name& identityName); |
| 38 | |
| 39 | /** |
| 40 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 41 | * @param identityName The identity name to be added. |
| 42 | */ |
| 43 | virtual void |
| 44 | addIdentity(const Name& identityName); |
| 45 | |
| 46 | /** |
| 47 | * Revoke the identity. |
| 48 | * @return true if the identity was revoked, false if not. |
| 49 | */ |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 50 | virtual bool |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 51 | revokeIdentity(); |
| 52 | |
| 53 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 54 | * Check if the specified key already exists. |
| 55 | * @param keyName The name of the key. |
| 56 | * @return true if the key exists, otherwise false. |
| 57 | */ |
| 58 | virtual bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 59 | doesPublicKeyExist(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 62 | * Add a public key to the identity storage. |
| 63 | * @param keyName The name of the public key to be added. |
| 64 | * @param keyType Type of the public key to be added. |
| 65 | * @param publicKeyDer A blob of the public key DER to be added. |
| 66 | */ |
| 67 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 68 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Get the public key DER blob from the identity storage. |
| 72 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 73 | * @return The DER Blob. If not found, return a Blob with a null pointer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 74 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 75 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 76 | getPublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 80 | * @param keyName name of the key |
| 81 | */ |
| 82 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 83 | activatePublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 87 | * @param keyName name of the key |
| 88 | */ |
| 89 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 90 | deactivatePublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Check if the specified certificate already exists. |
| 94 | * @param certificateName The name of the certificate. |
| 95 | * @return true if the certificate exists, otherwise false. |
| 96 | */ |
| 97 | virtual bool |
| 98 | doesCertificateExist(const Name& certificateName); |
| 99 | |
| 100 | /** |
| 101 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 102 | * @param certificate The certificate to be added. This makes a copy of the certificate. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 103 | */ |
| 104 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 105 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Get a certificate from the identity storage. |
| 109 | * @param certificateName The name of the requested certificate. |
| 110 | * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 111 | * @return The requested certificate. If not found, return a shared_ptr with a null pointer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 112 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 113 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 114 | getCertificate(const Name &certificateName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 115 | |
| 116 | |
| 117 | /***************************************** |
| 118 | * Get/Set Default * |
| 119 | *****************************************/ |
| 120 | |
| 121 | /** |
| 122 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 123 | * @param return The name of default identity, or an empty name if there is no default. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 124 | */ |
| 125 | virtual Name |
| 126 | getDefaultIdentity(); |
| 127 | |
| 128 | /** |
| 129 | * Get the default key name for the specified identity. |
| 130 | * @param identityName The identity name. |
| 131 | * @return The default key name. |
| 132 | */ |
| 133 | virtual Name |
| 134 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 135 | |
| 136 | /** |
| 137 | * Get the default certificate name for the specified key. |
| 138 | * @param keyName The key name. |
| 139 | * @return The default certificate name. |
| 140 | */ |
| 141 | virtual Name |
| 142 | getDefaultCertificateNameForKey(const Name& keyName); |
| 143 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 144 | virtual void |
| 145 | getAllIdentities(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 146 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 147 | virtual void |
| 148 | getAllKeyNames(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 149 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 150 | virtual void |
| 151 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 152 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 153 | virtual void |
| 154 | getAllCertificateNames(std::vector<Name> &nameList, bool isDefault); |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 155 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 156 | virtual void |
| 157 | getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 158 | |
| 159 | protected: |
| 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 | setDefaultIdentityInternal(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 | setDefaultKeyNameForIdentityInternal(const Name& keyName); |
| 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 | setDefaultCertificateNameForKeyInternal(const Name& certificateName); |
| 183 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Delete a certificate. |
| 186 | * @param certificateName The certificate name. |
| 187 | */ |
| 188 | virtual void |
| 189 | deleteCertificateInfo(const Name &certificateName); |
| 190 | |
| 191 | /** |
| 192 | * Delete a public key and related certificates. |
| 193 | * @param keyName The key name. |
| 194 | */ |
| 195 | virtual void |
| 196 | deletePublicKeyInfo(const Name &keyName); |
| 197 | |
| 198 | /** |
| 199 | * Delete an identity and related public keys and certificates. |
| 200 | * @param identity The identity name. |
| 201 | */ |
| 202 | virtual void |
| 203 | deleteIdentityInfo(const Name &identity); |
| 204 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 205 | |
| 206 | private: |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 207 | class KeyRecord { |
| 208 | public: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 209 | KeyRecord(KeyType keyType, const PublicKey &key) |
| 210 | : keyType_(keyType), key_(key) |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 211 | { |
| 212 | } |
| 213 | |
| 214 | const KeyType getKeyType() const { return keyType_; } |
| 215 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 216 | const PublicKey& getKey() { return key_; } |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 217 | |
| 218 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 219 | KeyType keyType_; |
| 220 | PublicKey key_; |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 223 | std::vector<std::string> identityStore_; /**< A list of name URI. */ |
| 224 | std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 225 | Name defaultKeyName_; |
| 226 | Name defaultCert_; |
| 227 | |
| 228 | typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */ |
| 229 | typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */ |
| 230 | |
| 231 | KeyStore keyStore_; |
| 232 | CertificateStore certificateStore_; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | #endif |