blob: 29b8f64b5f6ef16afb2a26754681ebb532afe604 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu31b4af22014-01-14 14:13:00 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu31b4af22014-01-14 14:13:00 -080023 */
24
Yingdi Yufc40d872014-02-18 12:56:04 -080025#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_HPP
26#define NDN_SECURITY_SEC_PUBLIC_INFO_HPP
Yingdi Yu31b4af22014-01-14 14:13:00 -080027
Yingdi Yu4f324632014-01-15 18:10:03 -080028#include "../name.hpp"
29#include "security-common.hpp"
30#include "public-key.hpp"
31#include "identity-certificate.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080032
Yingdi Yu88663af2014-01-15 15:21:38 -080033
Yingdi Yu31b4af22014-01-14 14:13:00 -080034namespace ndn {
35
36/**
Yingdi Yu2e57a582014-02-20 23:34:43 -080037 * @brief SecPublicInfo is a base class for the storage of public information.
38 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070039 * It specify interfaces related to public information, such as identity, public keys and
40 * certificates.
Yingdi Yu31b4af22014-01-14 14:13:00 -080041 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070042class SecPublicInfo : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070043{
Yingdi Yu31b4af22014-01-14 14:13:00 -080044public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 class Error : public std::runtime_error
46 {
47 public:
48 explicit
49 Error(const std::string& what)
50 : std::runtime_error(what)
51 {
52 }
53 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080054
55 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070056 * @brief The virtual Destructor
Yingdi Yu31b4af22014-01-14 14:13:00 -080057 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 virtual
Yingdi Yuf56c68f2014-04-24 21:50:13 -070059 ~SecPublicInfo()
60 {
61 }
Yingdi Yu31b4af22014-01-14 14:13:00 -080062
63 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070064 * @brief Check if the specified identity already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -080065 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070066 * @param identityName The identity name
67 * @return true if the identity exists, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080068 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080070 doesIdentityExist(const Name& identityName) = 0;
71
72 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070073 * @brief Add a new identity
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070075 * if identity already exist, do not add it again
Yingdi Yu2e57a582014-02-20 23:34:43 -080076 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070077 * @param identityName The identity name to be added
Yingdi Yu31b4af22014-01-14 14:13:00 -080078 */
79 virtual void
80 addIdentity(const Name& identityName) = 0;
81
82 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070083 * @brief Revoke the identity
Yingdi Yu2e57a582014-02-20 23:34:43 -080084 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070085 * @return true if the identity was revoked, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080086 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080088 revokeIdentity() = 0;
89
90 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070091 * @brief Check if the specified key already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -080092 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070093 * @param keyName The name of the key
94 * @return true if the key exists, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080095 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070096 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080097 doesPublicKeyExist(const Name& keyName) = 0;
98
99 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800100 * @brief Add a public key to the identity storage.
101 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700102 * @param keyName The name of the public key to be added
103 * @param keyType Type of the public key to be added
104 * @param publicKey Reference to the PublicKey object
Yingdi Yu31b4af22014-01-14 14:13:00 -0800105 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 virtual void
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700107 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800108
109 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700110 * @brief Get shared pointer to PublicKey object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700112 * @param keyName The name of the requested public key
113 * @throws SecPublicInfo::Error if public key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800114 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800115 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800116 getPublicKey(const Name& keyName) = 0;
117
118 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700119 * @brief Check if the specified certificate already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -0800120 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700121 * @param certificateName The name of the certificate
Yingdi Yu31b4af22014-01-14 14:13:00 -0800122 */
123 virtual bool
124 doesCertificateExist(const Name& certificateName) = 0;
125
126 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800127 * @brief Add a certificate to the identity storage.
128 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700129 * It will add the corresponding public key and identity if they do not exist
Yingdi Yu2e57a582014-02-20 23:34:43 -0800130 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700131 * @param certificate The certificate to be added
Yingdi Yu31b4af22014-01-14 14:13:00 -0800132 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800134 addCertificate(const IdentityCertificate& certificate) = 0;
135
136 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700137 * @brief Get a shared pointer to identity certificate object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700139 * @param certificateName The name of the requested certificate
140 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800141 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142 virtual shared_ptr<IdentityCertificate>
143 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800144
145
146 /*****************************************
147 * Default Getter *
148 *****************************************/
149
150 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700151 * @brief Get name of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800152 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800153 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800154 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800156 getDefaultIdentity() = 0;
157
158 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700159 * @brief Get name of the default key name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800160 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700161 * @param identityName The identity name
162 * @throws SecPublicInfo::Error if there is no default
Yingdi Yu31b4af22014-01-14 14:13:00 -0800163 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700164 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800165 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
166
167 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700168 * @brief Get name of the default certificate name for the specified key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800169 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800170 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800171 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800172 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700173 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800174 getDefaultCertificateNameForKey(const Name& keyName) = 0;
175
Yingdi Yu2e57a582014-02-20 23:34:43 -0800176 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700177 * @brief Get all the identities from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800178 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700179 * @param [out] nameList On return, the identity list
180 * @param isDefault If specified, only the default identity is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800181 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800182 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700183 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800184
Yingdi Yu2e57a582014-02-20 23:34:43 -0800185 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700186 * @brief Get all the key names from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800187 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700188 * @param [out] nameList On return, the key name list.
189 * @param isDefault If specified, only the default keys are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800190 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800191 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800193
Yingdi Yu2e57a582014-02-20 23:34:43 -0800194 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700195 * @brief Get all the key names of a particular identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800196 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700197 * @param identity The specified identity name
198 * @param [out] nameList On return, the key name list
199 * @param isDefault If specified, only the default key is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800200 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800201 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800203
204 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700205 * @brief Get all the certificate name in public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800206 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700207 * @param [out] nameList On return, the certificate name list
208 * @param isDefault If specified, only the default certificates are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800209 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800210 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700211 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
212
Yingdi Yu2e57a582014-02-20 23:34:43 -0800213 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700214 * @brief Get all the certificate name of a particular key name
Yingdi Yu2e57a582014-02-20 23:34:43 -0800215 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700216 * @param keyName The specified key name
217 * @param [out] nameList On return, the certificate name list
218 * @param isDefault If specified, only the default certificate is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800219 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800220 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700221 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800222
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700223 /*****************************************
224 * Delete Methods *
225 *****************************************/
226
227 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700228 * @brief Delete a certificate
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700229 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700230 * @param certificateName The certificate name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700231 */
232 virtual void
233 deleteCertificateInfo(const Name& certificateName) = 0;
234
235 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700236 * @brief Delete a public key and related certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700237 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700238 * @param keyName The key name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700239 */
240 virtual void
241 deletePublicKeyInfo(const Name& keyName) = 0;
242
243 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700244 * @brief Delete an identity and related public keys and certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700245 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700246 * @param identity The identity name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700247 */
248 virtual void
249 deleteIdentityInfo(const Name& identity) = 0;
250
Yingdi Yu31b4af22014-01-14 14:13:00 -0800251protected:
252
253 /*****************************************
254 * Default Setter *
255 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700256
Yingdi Yu31b4af22014-01-14 14:13:00 -0800257 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700258 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800259 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700260 * @param identityName The default identity name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800261 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700262 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800263 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700264
Yingdi Yu31b4af22014-01-14 14:13:00 -0800265 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700266 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700267 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700268 * @param keyName The key name
269 * @throws SecPublicInfo::Error if the key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800270 */
271 virtual void
272 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
273
274 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700275 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800276 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700277 * @param certificateName The certificate name
278 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800279 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700280 virtual void
281 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800282
283public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700284
Yingdi Yu31b4af22014-01-14 14:13:00 -0800285 /*****************************************
286 * Helper Methods *
287 *****************************************/
288
289 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700290 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800291 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700292 * @param identityName The default identity name
293 * @throws SecPublicInfo::Error if the identity does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800294 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700295 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800296 setDefaultIdentity(const Name& identityName);
297
298 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700299 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700300 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700301 * @param keyName The key name
302 * @throws SecPublicInfo::Error if either the identity or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800303 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700304 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800305 setDefaultKeyNameForIdentity(const Name& keyName);
306
307 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700308 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800309 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700310 * @param certificateName The certificate name
311 * @throws SecPublicInfo::Error if either the certificate or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800312 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700313 inline void
314 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800315
316 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700317 * @brief Generate a key name for the identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800318 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700319 * @param identityName The identity name
320 * @param useKsk If true, generate a KSK name, otherwise a DSK name
321 * @return The generated key name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800322 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700323 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800324 getNewKeyName(const Name& identityName, bool useKsk);
325
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700327 * @brief Get the default certificate name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700329 * @param identityName The identity name
330 * @return The default certificate name
331 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800332 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700333 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800334 getDefaultCertificateNameForIdentity(const Name& identityName);
335
336 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800337 * @brief Get the default certificate name of the default identity
338 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700339 * @return The requested certificate name
340 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800341 */
342 inline Name
343 getDefaultCertificateName();
344
345 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700346 * @brief Add a certificate and set the certificate as the default one of its corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800347 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700348 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800349 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800350 */
351 inline void
352 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
353
354 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700355 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700356 * default one of its corresponding identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800357 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700358 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800359 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800360 */
361 inline void
362 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
363
Yingdi Yu2e57a582014-02-20 23:34:43 -0800364 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700365 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700366 * default one of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800367 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700368 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
370 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800371 inline void
372 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
373
Yingdi Yu2e57a582014-02-20 23:34:43 -0800374 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700375 * @brief Get cached default certificate of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700377 * @return The certificate which might be empty shared_ptr<IdentityCertificate>()
Yingdi Yu2e57a582014-02-20 23:34:43 -0800378 */
379 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800380 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700381
Yingdi Yu2e57a582014-02-20 23:34:43 -0800382 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700383 * @brief try to get the default certificate of the default identity from the public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800384 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800385 inline void
386 refreshDefaultCertificate();
387
388protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800389 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800390};
391
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800392inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800393SecPublicInfo::setDefaultIdentity(const Name& identityName)
394{
395 setDefaultIdentityInternal(identityName);
396 refreshDefaultCertificate();
397}
398
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800399inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800400SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
401{
402 setDefaultKeyNameForIdentityInternal(keyName);
403 refreshDefaultCertificate();
404}
405
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700406inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800407SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
408{
409 setDefaultCertificateNameForKeyInternal(certificateName);
410 refreshDefaultCertificate();
411}
412
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700413inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800414SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
415{
416 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
417}
418
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800419inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800420SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
421{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800422 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423
Yingdi Yu31b4af22014-01-14 14:13:00 -0800424 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800425 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800426 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800427 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800428
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700429 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700430
Yingdi Yu88663af2014-01-15 15:21:38 -0800431 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800432
433 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800434 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800435
436 return keyName;
437}
438
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800439inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800440SecPublicInfo::getDefaultCertificateName()
441{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700442 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800443 refreshDefaultCertificate();
444
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700445 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800446 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800447
Yingdi Yu2e57a582014-02-20 23:34:43 -0800448 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800449}
450
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800451inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800452SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
453{
454 addCertificate(certificate);
455 setDefaultCertificateNameForKeyInternal(certificate.getName());
456 refreshDefaultCertificate();
457}
458
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800459inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800460SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
461{
462 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800463 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700464 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
465 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800466 setDefaultCertificateNameForKeyInternal(certName);
467 refreshDefaultCertificate();
468}
469
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800470inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800471SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
472{
473 addCertificate(certificate);
474 Name certName = certificate.getName();
475 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
476 setDefaultIdentityInternal(keyName.getPrefix(-1));
477 setDefaultKeyNameForIdentityInternal(keyName);
478 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800479 refreshDefaultCertificate();
480}
481
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800482inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800483SecPublicInfo::defaultCertificate()
484{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800485 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800486}
487
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800488inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800489SecPublicInfo::refreshDefaultCertificate()
490{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800491 try
492 {
493 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
494 m_defaultCertificate = getCertificate(certName);
495 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700496 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800497 {
498 m_defaultCertificate.reset();
499 }
500
Yingdi Yu31b4af22014-01-14 14:13:00 -0800501}
502
Yingdi Yufc40d872014-02-18 12:56:04 -0800503} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800504
Yingdi Yufc40d872014-02-18 12:56:04 -0800505#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP