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 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 11 | #include <vector> |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 12 | #include <map> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 13 | #include "sec-public-info.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | /** |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 18 | * MemoryKeyMetaInfo extends IdentityStorage and implements its methods to store identity, public key and certificate objects in memory. |
| 19 | * The application must get the objects through its own means and add the objects to the MemoryKeyMetaInfo object. |
| 20 | * To use permanent file-based storage, see BasicKeyMetaInfo. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 21 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 22 | class SecPublicInfoMemory : public SecPublicInfo { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 23 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 24 | 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] | 25 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | /** |
| 27 | * The virtual Destructor. |
| 28 | */ |
| 29 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 30 | ~SecPublicInfoMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Check if the specified identity already exists. |
| 34 | * @param identityName The identity name. |
| 35 | * @return true if the identity exists, otherwise false. |
| 36 | */ |
| 37 | virtual bool |
| 38 | doesIdentityExist(const Name& identityName); |
| 39 | |
| 40 | /** |
| 41 | * Add a new identity. An exception will be thrown if the identity already exists. |
| 42 | * @param identityName The identity name to be added. |
| 43 | */ |
| 44 | virtual void |
| 45 | addIdentity(const Name& identityName); |
| 46 | |
| 47 | /** |
| 48 | * Revoke the identity. |
| 49 | * @return true if the identity was revoked, false if not. |
| 50 | */ |
| 51 | virtual bool |
| 52 | revokeIdentity(); |
| 53 | |
| 54 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 55 | * Check if the specified key already exists. |
| 56 | * @param keyName The name of the key. |
| 57 | * @return true if the key exists, otherwise false. |
| 58 | */ |
| 59 | virtual bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 60 | doesPublicKeyExist(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 63 | * Add a public key to the identity storage. |
| 64 | * @param keyName The name of the public key to be added. |
| 65 | * @param keyType Type of the public key to be added. |
| 66 | * @param publicKeyDer A blob of the public key DER to be added. |
| 67 | */ |
| 68 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 69 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Get the public key DER blob from the identity storage. |
| 73 | * @param keyName The name of the requested public key. |
Jeff Thompson | abcea7d | 2013-10-02 15:03:21 -0700 | [diff] [blame] | 74 | * @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] | 75 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 76 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 77 | getPublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 81 | * @param keyName name of the key |
| 82 | */ |
| 83 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 84 | activatePublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing. |
| 88 | * @param keyName name of the key |
| 89 | */ |
| 90 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 91 | deactivatePublicKey(const Name& keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Check if the specified certificate already exists. |
| 95 | * @param certificateName The name of the certificate. |
| 96 | * @return true if the certificate exists, otherwise false. |
| 97 | */ |
| 98 | virtual bool |
| 99 | doesCertificateExist(const Name& certificateName); |
| 100 | |
| 101 | /** |
| 102 | * Add a certificate to the identity storage. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 103 | * @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] | 104 | */ |
| 105 | virtual void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 106 | addCertificate(const IdentityCertificate& certificate); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Get a certificate from the identity storage. |
| 110 | * @param certificateName The name of the requested certificate. |
| 111 | * @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] | 112 | * @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] | 113 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 114 | virtual ptr_lib::shared_ptr<IdentityCertificate> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 115 | getCertificate(const Name &certificateName, bool allowAny = false); |
| 116 | |
| 117 | |
| 118 | /***************************************** |
| 119 | * Get/Set Default * |
| 120 | *****************************************/ |
| 121 | |
| 122 | /** |
| 123 | * Get the default identity. |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 124 | * @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] | 125 | */ |
| 126 | virtual Name |
| 127 | getDefaultIdentity(); |
| 128 | |
| 129 | /** |
| 130 | * Get the default key name for the specified identity. |
| 131 | * @param identityName The identity name. |
| 132 | * @return The default key name. |
| 133 | */ |
| 134 | virtual Name |
| 135 | getDefaultKeyNameForIdentity(const Name& identityName); |
| 136 | |
| 137 | /** |
| 138 | * Get the default certificate name for the specified key. |
| 139 | * @param keyName The key name. |
| 140 | * @return The default certificate name. |
| 141 | */ |
| 142 | virtual Name |
| 143 | getDefaultCertificateNameForKey(const Name& keyName); |
| 144 | |
Alexander Afanasyev | 0c63211 | 2013-12-30 15:59:31 -0800 | [diff] [blame] | 145 | virtual std::vector<Name> |
| 146 | getAllIdentities(bool isDefault); |
| 147 | |
| 148 | virtual std::vector<Name> |
| 149 | getAllKeyNames(bool isDefault); |
| 150 | |
| 151 | virtual std::vector<Name> |
| 152 | getAllKeyNamesOfIdentity(const Name& identity, bool isDefault); |
| 153 | |
| 154 | virtual std::vector<Name> |
| 155 | getAllCertificateNames(bool isDefault); |
| 156 | |
| 157 | virtual std::vector<Name> |
| 158 | getAllCertificateNamesOfKey(const Name& keyName, bool isDefault); |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame^] | 159 | |
| 160 | protected: |
| 161 | /** |
| 162 | * Set the default identity. If the identityName does not exist, then clear the default identity |
| 163 | * so that getDefaultIdentity() returns an empty name. |
| 164 | * @param identityName The default identity name. |
| 165 | */ |
| 166 | virtual void |
| 167 | setDefaultIdentityInternal(const Name& identityName); |
| 168 | |
| 169 | /** |
| 170 | * Set the default key name for the specified identity. |
| 171 | * @param keyName The key name. |
| 172 | * @param identityNameCheck (optional) The identity name to check the keyName. |
| 173 | */ |
| 174 | virtual void |
| 175 | setDefaultKeyNameForIdentityInternal(const Name& keyName); |
| 176 | |
| 177 | /** |
| 178 | * Set the default key name for the specified identity. |
| 179 | * @param keyName The key name. |
| 180 | * @param certificateName The certificate name. |
| 181 | */ |
| 182 | virtual void |
| 183 | setDefaultCertificateNameForKeyInternal(const Name& certificateName); |
| 184 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 185 | |
| 186 | private: |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 187 | class KeyRecord { |
| 188 | public: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 189 | KeyRecord(KeyType keyType, const PublicKey &key) |
| 190 | : keyType_(keyType), key_(key) |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 191 | { |
| 192 | } |
| 193 | |
| 194 | const KeyType getKeyType() const { return keyType_; } |
| 195 | |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 196 | const PublicKey& getKey() { return key_; } |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 197 | |
| 198 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 199 | KeyType keyType_; |
| 200 | PublicKey key_; |
Jeff Thompson | 61805e9 | 2013-10-23 15:19:39 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Jeff Thompson | 8184227 | 2013-09-25 16:12:33 -0700 | [diff] [blame] | 203 | std::vector<std::string> identityStore_; /**< A list of name URI. */ |
| 204 | std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 205 | Name defaultKeyName_; |
| 206 | Name defaultCert_; |
| 207 | |
| 208 | typedef std::map< std::string, ptr_lib::shared_ptr<KeyRecord> > KeyStore; /**< The map key is the keyName.toUri() */ |
| 209 | typedef std::map< std::string, ptr_lib::shared_ptr<IdentityCertificate> > CertificateStore; /**< The map key is the certificateName.toUri() */ |
| 210 | |
| 211 | KeyStore keyStore_; |
| 212 | CertificateStore certificateStore_; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } |
| 216 | |
| 217 | #endif |