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