blob: 509cd09d09c9ffb5f52d09b9555c43993c148e6c [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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700321 inline 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700330 inline 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700339 inline void
340 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700349 inline 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700359 inline 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 */
368 inline Name
369 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 */
377 inline void
378 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 */
387 inline void
388 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 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800397 inline void
398 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>()
Yingdi Yu2e57a582014-02-20 23:34:43 -0800404 */
405 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800406 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700407
Yingdi Yu2e57a582014-02-20 23:34:43 -0800408 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700409 * @brief try to get the default certificate of the default identity from the public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800410 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411 inline void
412 refreshDefaultCertificate();
413
414protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800415 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800416};
417
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800418inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419SecPublicInfo::setDefaultIdentity(const Name& identityName)
420{
421 setDefaultIdentityInternal(identityName);
422 refreshDefaultCertificate();
423}
424
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800425inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800426SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
427{
428 setDefaultKeyNameForIdentityInternal(keyName);
429 refreshDefaultCertificate();
430}
431
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700432inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800433SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
434{
435 setDefaultCertificateNameForKeyInternal(certificateName);
436 refreshDefaultCertificate();
437}
438
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700439inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800440SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
441{
442 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
443}
444
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800445inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800446SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
447{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800448 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800449
Yingdi Yu31b4af22014-01-14 14:13:00 -0800450 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800451 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800452 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800453 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800454
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700455 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700456
Yingdi Yu88663af2014-01-15 15:21:38 -0800457 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800458
459 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800460 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800461
462 return keyName;
463}
464
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800465inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800466SecPublicInfo::getDefaultCertificateName()
467{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700468 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800469 refreshDefaultCertificate();
470
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700471 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800472 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800473
Yingdi Yu2e57a582014-02-20 23:34:43 -0800474 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800475}
476
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800477inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800478SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
479{
480 addCertificate(certificate);
481 setDefaultCertificateNameForKeyInternal(certificate.getName());
482 refreshDefaultCertificate();
483}
484
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800485inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800486SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
487{
488 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800489 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700490 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
491 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800492 setDefaultCertificateNameForKeyInternal(certName);
493 refreshDefaultCertificate();
494}
495
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800496inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800497SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
498{
499 addCertificate(certificate);
500 Name certName = certificate.getName();
501 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
502 setDefaultIdentityInternal(keyName.getPrefix(-1));
503 setDefaultKeyNameForIdentityInternal(keyName);
504 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800505 refreshDefaultCertificate();
506}
507
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800508inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800509SecPublicInfo::defaultCertificate()
510{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800511 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800512}
513
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800514inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800515SecPublicInfo::refreshDefaultCertificate()
516{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800517 try
518 {
519 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
520 m_defaultCertificate = getCertificate(certName);
521 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700522 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800523 {
524 m_defaultCertificate.reset();
525 }
526
Yingdi Yu31b4af22014-01-14 14:13:00 -0800527}
528
Yingdi Yufc40d872014-02-18 12:56:04 -0800529} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800530
Yingdi Yufc40d872014-02-18 12:56:04 -0800531#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP