blob: 743914e5757defe410335eeb6da61378610ea991 [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 *
Yingdi Yu40b53092014-06-17 17:10:02 -0700102 * @deprecated Use addKey instead
103 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700104 * @param keyName The name of the public key to be added
105 * @param keyType Type of the public key to be added
106 * @param publicKey Reference to the PublicKey object
Yingdi Yu31b4af22014-01-14 14:13:00 -0800107 */
Yingdi Yu40b53092014-06-17 17:10:02 -0700108 void
109 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
110 {
111 addKey(keyName, publicKey);
112 }
113
114 /**
115 * @brief Add a public key to the identity storage.
116 *
117 * @param keyName The name of the public key to be added
118 * @param publicKey Reference to the PublicKey object
119 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700120 virtual void
Yingdi Yu40b53092014-06-17 17:10:02 -0700121 addKey(const Name& keyName, const PublicKey& publicKey) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800122
123 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700124 * @brief Get shared pointer to PublicKey object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800125 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700126 * @param keyName The name of the requested public key
127 * @throws SecPublicInfo::Error if public key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800128 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800129 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800130 getPublicKey(const Name& keyName) = 0;
131
132 /**
Yingdi Yu40b53092014-06-17 17:10:02 -0700133 * @brief Get the type of the queried public key
134 *
135 * @note KeyType is also available from PublicKey instance.
136 * This method is more efficient if only KeyType is needed.
137 *
138 * @param keyName The name of the requested public key
139 * @return the type of the key. If the queried key does not exist, KEY_TYPE_NULL will be returned
140 */
141 virtual KeyType
142 getPublicKeyType(const Name& keyName) = 0;
143
144 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700145 * @brief Check if the specified certificate already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -0800146 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700147 * @param certificateName The name of the certificate
Yingdi Yu31b4af22014-01-14 14:13:00 -0800148 */
149 virtual bool
150 doesCertificateExist(const Name& certificateName) = 0;
151
152 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800153 * @brief Add a certificate to the identity storage.
154 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700155 * It will add the corresponding public key and identity if they do not exist
Yingdi Yu2e57a582014-02-20 23:34:43 -0800156 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700157 * @param certificate The certificate to be added
Yingdi Yu31b4af22014-01-14 14:13:00 -0800158 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800160 addCertificate(const IdentityCertificate& certificate) = 0;
161
162 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700163 * @brief Get a shared pointer to identity certificate object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800164 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700165 * @param certificateName The name of the requested certificate
166 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800167 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168 virtual shared_ptr<IdentityCertificate>
169 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800170
171
172 /*****************************************
173 * Default Getter *
174 *****************************************/
175
176 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700177 * @brief Get name of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800178 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800179 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800180 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700181 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800182 getDefaultIdentity() = 0;
183
184 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700185 * @brief Get name of the default key name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800186 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700187 * @param identityName The identity name
188 * @throws SecPublicInfo::Error if there is no default
Yingdi Yu31b4af22014-01-14 14:13:00 -0800189 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800191 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
192
193 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700194 * @brief Get name of the default certificate name for the specified key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800195 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800196 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800197 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800198 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800200 getDefaultCertificateNameForKey(const Name& keyName) = 0;
201
Yingdi Yu2e57a582014-02-20 23:34:43 -0800202 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700203 * @brief Get all the identities from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800204 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700205 * @param [out] nameList On return, the identity list
206 * @param isDefault If specified, only the default identity is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800207 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800208 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700209 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800210
Yingdi Yu2e57a582014-02-20 23:34:43 -0800211 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700212 * @brief Get all the key names from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800213 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700214 * @param [out] nameList On return, the key name list.
215 * @param isDefault If specified, only the default keys are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800216 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800217 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700218 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800219
Yingdi Yu2e57a582014-02-20 23:34:43 -0800220 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700221 * @brief Get all the key names of a particular identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800222 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700223 * @param identity The specified identity name
224 * @param [out] nameList On return, the key name list
225 * @param isDefault If specified, only the default key is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800226 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800227 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700228 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800229
230 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700231 * @brief Get all the certificate name in public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800232 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700233 * @param [out] nameList On return, the certificate name list
234 * @param isDefault If specified, only the default certificates are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800235 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800236 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700237 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
238
Yingdi Yu2e57a582014-02-20 23:34:43 -0800239 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700240 * @brief Get all the certificate name of a particular key name
Yingdi Yu2e57a582014-02-20 23:34:43 -0800241 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700242 * @param keyName The specified key name
243 * @param [out] nameList On return, the certificate name list
244 * @param isDefault If specified, only the default certificate is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800245 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800246 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700247 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800248
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700249 /*****************************************
250 * Delete Methods *
251 *****************************************/
252
253 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700254 * @brief Delete a certificate
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700255 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700256 * @param certificateName The certificate name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700257 */
258 virtual void
259 deleteCertificateInfo(const Name& certificateName) = 0;
260
261 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700262 * @brief Delete a public key and related certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700263 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700264 * @param keyName The key name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700265 */
266 virtual void
267 deletePublicKeyInfo(const Name& keyName) = 0;
268
269 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700270 * @brief Delete an identity and related public keys and certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700271 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700272 * @param identity The identity name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700273 */
274 virtual void
275 deleteIdentityInfo(const Name& identity) = 0;
276
Yingdi Yu31b4af22014-01-14 14:13:00 -0800277protected:
278
279 /*****************************************
280 * Default Setter *
281 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700282
Yingdi Yu31b4af22014-01-14 14:13:00 -0800283 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700284 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800285 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700286 * @param identityName The default identity name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800287 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800289 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700290
Yingdi Yu31b4af22014-01-14 14:13:00 -0800291 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700292 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700293 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700294 * @param keyName The key name
295 * @throws SecPublicInfo::Error if the key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800296 */
297 virtual void
298 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
299
300 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700301 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800302 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700303 * @param certificateName The certificate name
304 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800305 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700306 virtual void
307 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800308
309public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700310
Yingdi Yu31b4af22014-01-14 14:13:00 -0800311 /*****************************************
312 * Helper Methods *
313 *****************************************/
314
315 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700316 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800317 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700318 * @param identityName The default identity name
319 * @throws SecPublicInfo::Error if the identity does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800320 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700321 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800322 setDefaultIdentity(const Name& identityName);
323
324 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700325 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700326 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700327 * @param keyName The key name
328 * @throws SecPublicInfo::Error if either the identity or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800329 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700330 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800331 setDefaultKeyNameForIdentity(const Name& keyName);
332
333 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700334 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700336 * @param certificateName The certificate name
337 * @throws SecPublicInfo::Error if either the certificate or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800338 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700339 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700340 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800341
342 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700343 * @brief Generate a key name for the identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800344 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700345 * @param identityName The identity name
346 * @param useKsk If true, generate a KSK name, otherwise a DSK name
347 * @return The generated key name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800348 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700349 Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800350 getNewKeyName(const Name& identityName, bool useKsk);
351
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700353 * @brief Get the default certificate name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800354 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700355 * @param identityName The identity name
356 * @return The default certificate name
357 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800358 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700359 Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800360 getDefaultCertificateNameForIdentity(const Name& identityName);
361
362 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800363 * @brief Get the default certificate name of the default identity
364 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700365 * @return The requested certificate name
366 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800367 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700368 Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800369 getDefaultCertificateName();
370
371 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700372 * @brief Add a certificate and set the certificate as the default one of its corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800373 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700374 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800375 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800376 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700377 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800378 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
379
380 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700381 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700382 * default one of its corresponding identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800383 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700384 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800385 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800386 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700387 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800388 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
389
Yingdi Yu2e57a582014-02-20 23:34:43 -0800390 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700391 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700392 * default one of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800393 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700394 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800395 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
396 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700397 void
Yingdi Yu88663af2014-01-15 15:21:38 -0800398 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
399
Yingdi Yu2e57a582014-02-20 23:34:43 -0800400 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700401 * @brief Get cached default certificate of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800402 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700403 * @return The certificate which might be empty shared_ptr<IdentityCertificate>()
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700404 * @deprecated Use getDefaultCertificate instead
Yingdi Yu2e57a582014-02-20 23:34:43 -0800405 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700406 DEPRECATED(
407 shared_ptr<IdentityCertificate>
408 defaultCertificate());
409
410 /**
411 * @brief Get cached default certificate of the default identity
412 *
413 * @return The certificate which might be empty shared_ptr<IdentityCertificate>()
414 */
415 shared_ptr<IdentityCertificate>
416 getDefaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700417
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700419 * @brief try to get the default certificate of the default identity from the public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800420 */
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700421 void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800422 refreshDefaultCertificate();
423
424protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800425 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800426};
427
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800428inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800429SecPublicInfo::setDefaultIdentity(const Name& identityName)
430{
431 setDefaultIdentityInternal(identityName);
432 refreshDefaultCertificate();
433}
434
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800435inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800436SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
437{
438 setDefaultKeyNameForIdentityInternal(keyName);
439 refreshDefaultCertificate();
440}
441
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700442inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800443SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
444{
445 setDefaultCertificateNameForKeyInternal(certificateName);
446 refreshDefaultCertificate();
447}
448
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700449inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800450SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
451{
452 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
453}
454
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800455inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800456SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
457{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800458 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800459
Yingdi Yu31b4af22014-01-14 14:13:00 -0800460 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800461 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800462 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800463 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800464
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700465 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700466
Yingdi Yu88663af2014-01-15 15:21:38 -0800467 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800468
469 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800470 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800471
472 return keyName;
473}
474
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800475inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800476SecPublicInfo::getDefaultCertificateName()
477{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700478 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800479 refreshDefaultCertificate();
480
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700481 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800482 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800483
Yingdi Yu2e57a582014-02-20 23:34:43 -0800484 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800485}
486
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800487inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800488SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
489{
490 addCertificate(certificate);
491 setDefaultCertificateNameForKeyInternal(certificate.getName());
492 refreshDefaultCertificate();
493}
494
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800495inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800496SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
497{
498 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800499 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700500 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
501 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800502 setDefaultCertificateNameForKeyInternal(certName);
503 refreshDefaultCertificate();
504}
505
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800506inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800507SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
508{
509 addCertificate(certificate);
510 Name certName = certificate.getName();
511 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
512 setDefaultIdentityInternal(keyName.getPrefix(-1));
513 setDefaultKeyNameForIdentityInternal(keyName);
514 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800515 refreshDefaultCertificate();
516}
517
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800518inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800519SecPublicInfo::defaultCertificate()
520{
Alexander Afanasyevaab79662014-07-07 17:35:34 -0700521 return getDefaultCertificate();
522}
523
524inline shared_ptr<IdentityCertificate>
525SecPublicInfo::getDefaultCertificate()
526{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800527 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800528}
529
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800530inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800531SecPublicInfo::refreshDefaultCertificate()
532{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800533 try
534 {
535 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
536 m_defaultCertificate = getCertificate(certName);
537 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700538 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800539 {
540 m_defaultCertificate.reset();
541 }
542
Yingdi Yu31b4af22014-01-14 14:13:00 -0800543}
544
Yingdi Yufc40d872014-02-18 12:56:04 -0800545} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800546
Yingdi Yufc40d872014-02-18 12:56:04 -0800547#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP