Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [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> |
| 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 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_HPP |
| 10 | #define NDN_SECURITY_SEC_PUBLIC_INFO_HPP |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 11 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 12 | #include "../name.hpp" |
| 13 | #include "security-common.hpp" |
| 14 | #include "public-key.hpp" |
| 15 | #include "identity-certificate.hpp" |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 16 | |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 17 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 21 | * @brief SecPublicInfo is a base class for the storage of public information. |
| 22 | * |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 23 | * It specify interfaces related to public information, such as identity, public keys and |
| 24 | * certificates. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 25 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 26 | class SecPublicInfo |
| 27 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 28 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 29 | class Error : public std::runtime_error |
| 30 | { |
| 31 | public: |
| 32 | explicit |
| 33 | Error(const std::string& what) |
| 34 | : std::runtime_error(what) |
| 35 | { |
| 36 | } |
| 37 | }; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 40 | * @brief The virtual Destructor. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 42 | virtual |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 43 | ~SecPublicInfo() {} |
| 44 | |
| 45 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 46 | * @brief Check if the specified identity already exists. |
| 47 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 48 | * @param identityName The identity name. |
| 49 | * @return true if the identity exists, otherwise false. |
| 50 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 51 | virtual bool |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 52 | doesIdentityExist(const Name& identityName) = 0; |
| 53 | |
| 54 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 55 | * @brief Add a new identity. |
| 56 | * |
| 57 | * if identity already exist, do not add it again. |
| 58 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 59 | * @param identityName The identity name to be added. |
| 60 | */ |
| 61 | virtual void |
| 62 | addIdentity(const Name& identityName) = 0; |
| 63 | |
| 64 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 65 | * @brief Revoke the identity. |
| 66 | * |
| 67 | * @return true if the identity was revoked, otherwise false. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 69 | virtual bool |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 70 | revokeIdentity() = 0; |
| 71 | |
| 72 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 73 | * @brief Check if the specified key already exists. |
| 74 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 75 | * @param keyName The name of the key. |
| 76 | * @return true if the key exists, otherwise false. |
| 77 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 78 | virtual bool |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 79 | doesPublicKeyExist(const Name& keyName) = 0; |
| 80 | |
| 81 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 82 | * @brief Add a public key to the identity storage. |
| 83 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 84 | * @param keyName The name of the public key to be added. |
| 85 | * @param keyType Type of the public key to be added. |
| 86 | * @param publicKeyDer A blob of the public key DER to be added. |
| 87 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | virtual void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 89 | addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0; |
| 90 | |
| 91 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 92 | * @brief Get the public key DER blob from the identity storage. |
| 93 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 94 | * @param keyName The name of the requested public key. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 95 | * @return The DER Blob. |
| 96 | * @throws SecPublicInfo::Error if public key does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 97 | */ |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 98 | virtual shared_ptr<PublicKey> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 99 | getPublicKey(const Name& keyName) = 0; |
| 100 | |
| 101 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 102 | * @brief Check if the specified certificate already exists. |
| 103 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 104 | * @param certificateName The name of the certificate. |
| 105 | * @return true if the certificate exists, otherwise false. |
| 106 | */ |
| 107 | virtual bool |
| 108 | doesCertificateExist(const Name& certificateName) = 0; |
| 109 | |
| 110 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 111 | * @brief Add a certificate to the identity storage. |
| 112 | * |
| 113 | * It will add the corresponding public key and identity if they do not exist. |
| 114 | * |
| 115 | * @param certificate The certificate to be added. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 116 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 117 | virtual void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 118 | addCertificate(const IdentityCertificate& certificate) = 0; |
| 119 | |
| 120 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 121 | * @brief Get a certificate from the identity storage. |
| 122 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 123 | * @param certificateName The name of the requested certificate. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | * @return The requested certificate. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 125 | * @throws SecPublicInfo::Error if the certificate does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 126 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 127 | virtual shared_ptr<IdentityCertificate> |
| 128 | getCertificate(const Name& certificateName) = 0; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 129 | |
| 130 | |
| 131 | /***************************************** |
| 132 | * Default Getter * |
| 133 | *****************************************/ |
| 134 | |
| 135 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | * @brief Get the default identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 137 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 138 | * @param return The name of default identity, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 139 | * @throws SecPublicInfo::Error if there is no default. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 140 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 141 | virtual Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 142 | getDefaultIdentity() = 0; |
| 143 | |
| 144 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 145 | * @brief Get the default key name for the specified identity. |
| 146 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 147 | * @param identityName The identity name. |
| 148 | * @return The default key name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 149 | * @throws SecPublicInfo::Error if there is no default. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 150 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 151 | virtual Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 152 | getDefaultKeyNameForIdentity(const Name& identityName) = 0; |
| 153 | |
| 154 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 155 | * @brief Get the default certificate name for the specified key. |
| 156 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 157 | * @param keyName The key name. |
| 158 | * @return The default certificate name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 159 | * @throws SecPublicInfo::Error if there is no default. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 160 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 161 | virtual Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 162 | getDefaultCertificateNameForKey(const Name& keyName) = 0; |
| 163 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 164 | /** |
| 165 | * @brief Get all the identities in public info. |
| 166 | * |
| 167 | * @param nameList On return, the identity list. |
| 168 | * @param isDefault If specified, only the default identity is returned. |
| 169 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 170 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 171 | getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 172 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 173 | /** |
| 174 | * @brief Get all the key name in public info. |
| 175 | * |
| 176 | * @param nameList On return, the key name list. |
| 177 | * @param isDefault If specified, only the default keys are returned. |
| 178 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 179 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 180 | getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 181 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 182 | /** |
| 183 | * @brief Get all the key name of a particular identity. |
| 184 | * |
| 185 | * @param identity The specified identity name. |
| 186 | * @param nameList On return, the key name list. |
| 187 | * @param isDefault If specified, only the default key is returned. |
| 188 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 189 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 190 | getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * @brief Get all the certificate name in public info. |
| 194 | * |
| 195 | * @param nameList On return, the certificate name list. |
| 196 | * @param isDefault If specified, only the default certificates are returned. |
| 197 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 198 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 199 | getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0; |
| 200 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 201 | /** |
| 202 | * @brief Get all the certificate name of a particular key. |
| 203 | * |
| 204 | * @param identity The specified key name. |
| 205 | * @param nameList On return, the certificate name list. |
| 206 | * @param isDefault If specified, only the default certificate is returned. |
| 207 | */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 208 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 209 | getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 210 | |
| 211 | protected: |
| 212 | |
| 213 | /***************************************** |
| 214 | * Default Setter * |
| 215 | *****************************************/ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 216 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 217 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 218 | * @brief Set the default identity. |
| 219 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 220 | * @param identityName The default identity name. |
| 221 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 222 | virtual void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 223 | setDefaultIdentityInternal(const Name& identityName) = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 224 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 225 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 226 | * @brief Set the default key name for the corresponding identity. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 227 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 228 | * @param keyName The key name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 229 | * @throws SecPublicInfo::Error if the key does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 230 | */ |
| 231 | virtual void |
| 232 | setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0; |
| 233 | |
| 234 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 235 | * @brief Set the default certificate name for the corresponding key. |
| 236 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 237 | * @param certificateName The certificate name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 238 | * @throws SecPublicInfo::Error if the certificatedoes not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 239 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 240 | virtual void |
| 241 | setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 242 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 243 | /***************************************** |
| 244 | * Delete Methods * |
| 245 | *****************************************/ |
| 246 | |
| 247 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 248 | * @brief Delete a certificate. |
| 249 | * |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 250 | * @param certificateName The certificate name. |
| 251 | */ |
| 252 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 253 | deleteCertificateInfo(const Name& certificateName) = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 254 | |
| 255 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 256 | * @brief Delete a public key and related certificates. |
| 257 | * |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 258 | * @param keyName The key name. |
| 259 | */ |
| 260 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 261 | deletePublicKeyInfo(const Name& keyName) = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 262 | |
| 263 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 264 | * @brief Delete an identity and related public keys and certificates. |
| 265 | * |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 266 | * @param identity The identity name. |
| 267 | */ |
| 268 | virtual void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 269 | deleteIdentityInfo(const Name& identity) = 0; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 270 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 271 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 272 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 273 | /***************************************** |
| 274 | * Helper Methods * |
| 275 | *****************************************/ |
| 276 | |
| 277 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 278 | * @brief Set the default identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 279 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 280 | * @param identityName The default identity name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 281 | * @throws SecPublicInfo::Error if the identity does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 282 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 283 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 284 | setDefaultIdentity(const Name& identityName); |
| 285 | |
| 286 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 287 | * @brief Set the default key name for the corresponding identity. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 288 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 289 | * @param keyName The key name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 290 | * @throws SecPublicInfo::Error if either the identity or key does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 291 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 292 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 293 | setDefaultKeyNameForIdentity(const Name& keyName); |
| 294 | |
| 295 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 296 | * @brief Set the default certificate name for the corresponding key. |
| 297 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 298 | * @param certificateName The certificate name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 299 | * @throws SecPublicInfo::Error if either the certificate or key does not exist. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 300 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 301 | inline void |
| 302 | setDefaultCertificateNameForKey(const Name& certificateName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 303 | |
| 304 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 305 | * @brief Generate a key name for the identity. |
| 306 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 307 | * @param identityName The identity name. |
| 308 | * @param useKsk If true, generate a KSK name, otherwise a DSK name. |
| 309 | * @return The generated key name. |
| 310 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 311 | inline Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 312 | getNewKeyName(const Name& identityName, bool useKsk); |
| 313 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 314 | /** |
| 315 | * @brief Get the default certificate name for the specified identity. |
| 316 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 317 | * @param identityName The identity name. |
| 318 | * @return The default certificate name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 319 | * @throws SecPublicInfo::Error if no certificate is found. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 320 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 321 | inline Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 322 | getDefaultCertificateNameForIdentity(const Name& identityName); |
| 323 | |
| 324 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 325 | * @brief Get the default certificate name of the default identity |
| 326 | * |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 327 | * @return The requested certificate name. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 328 | * @throws SecPublicInfo::Error if no certificate is found. |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 329 | */ |
| 330 | inline Name |
| 331 | getDefaultCertificateName(); |
| 332 | |
| 333 | /** |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 334 | * @brief Add a certificate and set the certificate as the default one of its corresponding key. |
| 335 | * |
| 336 | * @param certificate The certificate to be added. |
| 337 | * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 338 | */ |
| 339 | inline void |
| 340 | addCertificateAsKeyDefault(const IdentityCertificate& certificate); |
| 341 | |
| 342 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 343 | * @brief Add a certificate into the public key identity storage and set the certificate as the |
| 344 | * default one of its corresponding identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 345 | * |
| 346 | * @param certificate The certificate to be added. |
| 347 | * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 348 | */ |
| 349 | inline void |
| 350 | addCertificateAsIdentityDefault(const IdentityCertificate& certificate); |
| 351 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 352 | /** |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 353 | * @brief Add a certificate into the public key identity storage and set the certificate as the |
| 354 | * default one of the default identity. |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 355 | * |
| 356 | * @param certificate The certificate to be added. |
| 357 | * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare) |
| 358 | */ |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 359 | inline void |
| 360 | addCertificateAsSystemDefault(const IdentityCertificate& certificate); |
| 361 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 362 | /** |
| 363 | * @brief get cached default certificate of the default identity. |
| 364 | * |
| 365 | * @return The certificate which might be a NULL pointer. |
| 366 | */ |
| 367 | inline shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 368 | defaultCertificate(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 369 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 370 | /** |
| 371 | * @brief try to get the default certificate of the default identity from the public info. |
| 372 | */ |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 373 | inline void |
| 374 | refreshDefaultCertificate(); |
| 375 | |
| 376 | protected: |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 377 | shared_ptr<IdentityCertificate> m_defaultCertificate; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 378 | }; |
| 379 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 380 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 381 | SecPublicInfo::setDefaultIdentity(const Name& identityName) |
| 382 | { |
| 383 | setDefaultIdentityInternal(identityName); |
| 384 | refreshDefaultCertificate(); |
| 385 | } |
| 386 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 387 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 388 | SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName) |
| 389 | { |
| 390 | setDefaultKeyNameForIdentityInternal(keyName); |
| 391 | refreshDefaultCertificate(); |
| 392 | } |
| 393 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 394 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 395 | SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName) |
| 396 | { |
| 397 | setDefaultCertificateNameForKeyInternal(certificateName); |
| 398 | refreshDefaultCertificate(); |
| 399 | } |
| 400 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 401 | inline Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 402 | SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName) |
| 403 | { |
| 404 | return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName)); |
| 405 | } |
| 406 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 407 | inline Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 408 | SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk) |
| 409 | { |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 410 | std::ostringstream oss; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 411 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 412 | if (useKsk) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 413 | oss << "ksk-"; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 414 | else |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 415 | oss << "dsk-"; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 416 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 417 | oss << time::toUnixTimestamp(time::system_clock::now()).count(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 418 | |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 419 | Name keyName = Name(identityName).append(oss.str()); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 420 | |
| 421 | if (doesPublicKeyExist(keyName)) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 422 | throw Error("Key name already exists: " + keyName.toUri()); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 423 | |
| 424 | return keyName; |
| 425 | } |
| 426 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 427 | inline Name |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 428 | SecPublicInfo::getDefaultCertificateName() |
| 429 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 430 | if (!static_cast<bool>(m_defaultCertificate)) |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 431 | refreshDefaultCertificate(); |
| 432 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 433 | if (!static_cast<bool>(m_defaultCertificate)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 434 | throw Error("No default certificate is set"); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 435 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 436 | return m_defaultCertificate->getName(); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 439 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 440 | SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
| 441 | { |
| 442 | addCertificate(certificate); |
| 443 | setDefaultCertificateNameForKeyInternal(certificate.getName()); |
| 444 | refreshDefaultCertificate(); |
| 445 | } |
| 446 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 447 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 448 | SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
| 449 | { |
| 450 | addCertificate(certificate); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 451 | Name certName = certificate.getName(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 452 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName); |
| 453 | setDefaultKeyNameForIdentityInternal(keyName); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 454 | setDefaultCertificateNameForKeyInternal(certName); |
| 455 | refreshDefaultCertificate(); |
| 456 | } |
| 457 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 458 | inline void |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 459 | SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
| 460 | { |
| 461 | addCertificate(certificate); |
| 462 | Name certName = certificate.getName(); |
| 463 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName); |
| 464 | setDefaultIdentityInternal(keyName.getPrefix(-1)); |
| 465 | setDefaultKeyNameForIdentityInternal(keyName); |
| 466 | setDefaultCertificateNameForKeyInternal(certName); |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 467 | refreshDefaultCertificate(); |
| 468 | } |
| 469 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 470 | inline shared_ptr<IdentityCertificate> |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 471 | SecPublicInfo::defaultCertificate() |
| 472 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 473 | return m_defaultCertificate; |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 476 | inline void |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 477 | SecPublicInfo::refreshDefaultCertificate() |
| 478 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 479 | try |
| 480 | { |
| 481 | Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity()); |
| 482 | m_defaultCertificate = getCertificate(certName); |
| 483 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 484 | catch (SecPublicInfo::Error& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 485 | { |
| 486 | m_defaultCertificate.reset(); |
| 487 | } |
| 488 | |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 491 | } // namespace ndn |
Yingdi Yu | 31b4af2 | 2014-01-14 14:13:00 -0800 | [diff] [blame] | 492 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 493 | #endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP |