blob: f8f2e09f4dcc4ec1c180e869b1275006059f8e9f [file] [log] [blame]
Yingdi Yu31b4af22014-01-14 14:13:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu31b4af22014-01-14 14:13:00 -080014 */
15
Yingdi Yufc40d872014-02-18 12:56:04 -080016#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_HPP
17#define NDN_SECURITY_SEC_PUBLIC_INFO_HPP
Yingdi Yu31b4af22014-01-14 14:13:00 -080018
Yingdi Yu4f324632014-01-15 18:10:03 -080019#include "../name.hpp"
20#include "security-common.hpp"
21#include "public-key.hpp"
22#include "identity-certificate.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080023
Yingdi Yu88663af2014-01-15 15:21:38 -080024
Yingdi Yu31b4af22014-01-14 14:13:00 -080025namespace ndn {
26
27/**
Yingdi Yu2e57a582014-02-20 23:34:43 -080028 * @brief SecPublicInfo is a base class for the storage of public information.
29 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070030 * It specify interfaces related to public information, such as identity, public keys and
31 * certificates.
Yingdi Yu31b4af22014-01-14 14:13:00 -080032 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033class SecPublicInfo
34{
Yingdi Yu31b4af22014-01-14 14:13:00 -080035public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036 class Error : public std::runtime_error
37 {
38 public:
39 explicit
40 Error(const std::string& what)
41 : std::runtime_error(what)
42 {
43 }
44 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080045
46 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080047 * @brief The virtual Destructor.
Yingdi Yu31b4af22014-01-14 14:13:00 -080048 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080050 ~SecPublicInfo() {}
51
52 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080053 * @brief Check if the specified identity already exists.
54 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080055 * @param identityName The identity name.
56 * @return true if the identity exists, otherwise false.
57 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080059 doesIdentityExist(const Name& identityName) = 0;
60
61 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080062 * @brief Add a new identity.
63 *
64 * if identity already exist, do not add it again.
65 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080066 * @param identityName The identity name to be added.
67 */
68 virtual void
69 addIdentity(const Name& identityName) = 0;
70
71 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 * @brief Revoke the identity.
73 *
74 * @return true if the identity was revoked, otherwise false.
Yingdi Yu31b4af22014-01-14 14:13:00 -080075 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080077 revokeIdentity() = 0;
78
79 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080080 * @brief Check if the specified key already exists.
81 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080082 * @param keyName The name of the key.
83 * @return true if the key exists, otherwise false.
84 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080086 doesPublicKeyExist(const Name& keyName) = 0;
87
88 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080089 * @brief Add a public key to the identity storage.
90 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080091 * @param keyName The name of the public key to be added.
92 * @param keyType Type of the public key to be added.
93 * @param publicKeyDer A blob of the public key DER to be added.
94 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080096 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
97
98 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080099 * @brief Get the public key DER blob from the identity storage.
100 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800101 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800102 * @return The DER Blob.
103 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800104 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800105 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800106 getPublicKey(const Name& keyName) = 0;
107
108 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800109 * @brief Check if the specified certificate already exists.
110 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800111 * @param certificateName The name of the certificate.
112 * @return true if the certificate exists, otherwise false.
113 */
114 virtual bool
115 doesCertificateExist(const Name& certificateName) = 0;
116
117 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800118 * @brief Add a certificate to the identity storage.
119 *
120 * It will add the corresponding public key and identity if they do not exist.
121 *
122 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800123 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800125 addCertificate(const IdentityCertificate& certificate) = 0;
126
127 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800128 * @brief Get a certificate from the identity storage.
129 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800130 * @param certificateName The name of the requested certificate.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131 * @return The requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800132 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800133 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134 virtual shared_ptr<IdentityCertificate>
135 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800136
137
138 /*****************************************
139 * Default Getter *
140 *****************************************/
141
142 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143 * @brief Get the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800144 *
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145 * @param return The name of default identity,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800146 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800147 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800149 getDefaultIdentity() = 0;
150
151 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800152 * @brief Get the default key name for the specified identity.
153 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800154 * @param identityName The identity name.
155 * @return The default key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800156 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800157 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700158 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800159 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
160
161 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800162 * @brief Get the default certificate name for the specified key.
163 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800164 * @param keyName The key name.
165 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800166 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800167 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700168 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800169 getDefaultCertificateNameForKey(const Name& keyName) = 0;
170
Yingdi Yu2e57a582014-02-20 23:34:43 -0800171 /**
172 * @brief Get all the identities in public info.
173 *
174 * @param nameList On return, the identity list.
175 * @param isDefault If specified, only the default identity is returned.
176 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800177 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800179
Yingdi Yu2e57a582014-02-20 23:34:43 -0800180 /**
181 * @brief Get all the key name in public info.
182 *
183 * @param nameList On return, the key name list.
184 * @param isDefault If specified, only the default keys are returned.
185 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800186 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700187 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800188
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189 /**
190 * @brief Get all the key name of a particular identity.
191 *
192 * @param identity The specified identity name.
193 * @param nameList On return, the key name list.
194 * @param isDefault If specified, only the default key is returned.
195 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800196 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800198
199 /**
200 * @brief Get all the certificate name in public info.
201 *
202 * @param nameList On return, the certificate name list.
203 * @param isDefault If specified, only the default certificates are returned.
204 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800205 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700206 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
207
Yingdi Yu2e57a582014-02-20 23:34:43 -0800208 /**
209 * @brief Get all the certificate name of a particular key.
210 *
211 * @param identity The specified key name.
212 * @param nameList On return, the certificate name list.
213 * @param isDefault If specified, only the default certificate is returned.
214 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800215 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800217
218protected:
219
220 /*****************************************
221 * Default Setter *
222 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700223
Yingdi Yu31b4af22014-01-14 14:13:00 -0800224 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800225 * @brief Set the default identity.
226 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800227 * @param identityName The default identity name.
228 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700229 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800230 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700231
Yingdi Yu31b4af22014-01-14 14:13:00 -0800232 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800233 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700234 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800235 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800236 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800237 */
238 virtual void
239 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
240
241 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800242 * @brief Set the default certificate name for the corresponding key.
243 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800244 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800245 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800246 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700247 virtual void
248 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800249
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800250 /*****************************************
251 * Delete Methods *
252 *****************************************/
253
254 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800255 * @brief Delete a certificate.
256 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800257 * @param certificateName The certificate name.
258 */
259 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700260 deleteCertificateInfo(const Name& certificateName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800261
262 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800263 * @brief Delete a public key and related certificates.
264 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800265 * @param keyName The key name.
266 */
267 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700268 deletePublicKeyInfo(const Name& keyName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800269
270 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800271 * @brief Delete an identity and related public keys and certificates.
272 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800273 * @param identity The identity name.
274 */
275 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700276 deleteIdentityInfo(const Name& identity) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800277
Yingdi Yu31b4af22014-01-14 14:13:00 -0800278public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700279
Yingdi Yu31b4af22014-01-14 14:13:00 -0800280 /*****************************************
281 * Helper Methods *
282 *****************************************/
283
284 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700285 * @brief Set the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800286 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800287 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800288 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800289 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700290 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800291 setDefaultIdentity(const Name& identityName);
292
293 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800294 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700295 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800296 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800297 * @throws SecPublicInfo::Error if either the identity or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800298 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700299 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800300 setDefaultKeyNameForIdentity(const Name& keyName);
301
302 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800303 * @brief Set the default certificate name for the corresponding key.
304 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800305 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800306 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800307 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700308 inline void
309 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800310
311 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800312 * @brief Generate a key name for the identity.
313 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800314 * @param identityName The identity name.
315 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
316 * @return The generated key name.
317 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700318 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800319 getNewKeyName(const Name& identityName, bool useKsk);
320
Yingdi Yu2e57a582014-02-20 23:34:43 -0800321 /**
322 * @brief Get the default certificate name for the specified identity.
323 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800324 * @param identityName The identity name.
325 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800326 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800327 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700328 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800329 getDefaultCertificateNameForIdentity(const Name& identityName);
330
331 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332 * @brief Get the default certificate name of the default identity
333 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800334 * @return The requested certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800335 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800336 */
337 inline Name
338 getDefaultCertificateName();
339
340 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800341 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
342 *
343 * @param certificate The certificate to be added.
344 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800345 */
346 inline void
347 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
348
349 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700350 * @brief Add a certificate into the public key identity storage and set the certificate as the
351 * default one of its corresponding identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352 *
353 * @param certificate The certificate to be added.
354 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800355 */
356 inline void
357 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
358
Yingdi Yu2e57a582014-02-20 23:34:43 -0800359 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700360 * @brief Add a certificate into the public key identity storage and set the certificate as the
361 * default one of the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800362 *
363 * @param certificate The certificate to be added.
364 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
365 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800366 inline void
367 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
368
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369 /**
370 * @brief get cached default certificate of the default identity.
371 *
372 * @return The certificate which might be a NULL pointer.
373 */
374 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800375 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700376
Yingdi Yu2e57a582014-02-20 23:34:43 -0800377 /**
378 * @brief try to get the default certificate of the default identity from the public info.
379 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800380 inline void
381 refreshDefaultCertificate();
382
383protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800384 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800385};
386
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800387inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800388SecPublicInfo::setDefaultIdentity(const Name& identityName)
389{
390 setDefaultIdentityInternal(identityName);
391 refreshDefaultCertificate();
392}
393
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800394inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800395SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
396{
397 setDefaultKeyNameForIdentityInternal(keyName);
398 refreshDefaultCertificate();
399}
400
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700401inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800402SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
403{
404 setDefaultCertificateNameForKeyInternal(certificateName);
405 refreshDefaultCertificate();
406}
407
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700408inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800409SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
410{
411 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
412}
413
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800414inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800415SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
416{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800417 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800418
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800420 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800421 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800422 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700424 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700425
Yingdi Yu88663af2014-01-15 15:21:38 -0800426 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800427
428 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800429 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800430
431 return keyName;
432}
433
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800434inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800435SecPublicInfo::getDefaultCertificateName()
436{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700437 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800438 refreshDefaultCertificate();
439
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700440 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800441 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800442
Yingdi Yu2e57a582014-02-20 23:34:43 -0800443 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800444}
445
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800446inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800447SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
448{
449 addCertificate(certificate);
450 setDefaultCertificateNameForKeyInternal(certificate.getName());
451 refreshDefaultCertificate();
452}
453
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800454inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800455SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
456{
457 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800458 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700459 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
460 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800461 setDefaultCertificateNameForKeyInternal(certName);
462 refreshDefaultCertificate();
463}
464
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800465inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800466SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
467{
468 addCertificate(certificate);
469 Name certName = certificate.getName();
470 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
471 setDefaultIdentityInternal(keyName.getPrefix(-1));
472 setDefaultKeyNameForIdentityInternal(keyName);
473 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800474 refreshDefaultCertificate();
475}
476
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800477inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800478SecPublicInfo::defaultCertificate()
479{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800480 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800481}
482
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800483inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800484SecPublicInfo::refreshDefaultCertificate()
485{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800486 try
487 {
488 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
489 m_defaultCertificate = getCertificate(certName);
490 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700491 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800492 {
493 m_defaultCertificate.reset();
494 }
495
Yingdi Yu31b4af22014-01-14 14:13:00 -0800496}
497
Yingdi Yufc40d872014-02-18 12:56:04 -0800498} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800499
Yingdi Yufc40d872014-02-18 12:56:04 -0800500#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP