blob: 790ad36eb0b1af89338a66d05f39c6dee97692be [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 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070033class SecPublicInfo : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070034{
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 Yuf56c68f2014-04-24 21:50:13 -070050 ~SecPublicInfo()
51 {
52 }
Yingdi Yu31b4af22014-01-14 14:13:00 -080053
54 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 * @brief Check if the specified identity already exists.
56 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080057 * @param identityName The identity name.
58 * @return true if the identity exists, otherwise false.
59 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080061 doesIdentityExist(const Name& identityName) = 0;
62
63 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080064 * @brief Add a new identity.
65 *
66 * if identity already exist, do not add it again.
67 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080068 * @param identityName The identity name to be added.
69 */
70 virtual void
71 addIdentity(const Name& identityName) = 0;
72
73 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 * @brief Revoke the identity.
75 *
76 * @return true if the identity was revoked, otherwise false.
Yingdi Yu31b4af22014-01-14 14:13:00 -080077 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080079 revokeIdentity() = 0;
80
81 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 * @brief Check if the specified key already exists.
83 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080084 * @param keyName The name of the key.
85 * @return true if the key exists, otherwise false.
86 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080088 doesPublicKeyExist(const Name& keyName) = 0;
89
90 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080091 * @brief Add a public key to the identity storage.
92 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080093 * @param keyName The name of the public key to be added.
94 * @param keyType Type of the public key to be added.
95 * @param publicKeyDer A blob of the public key DER to be added.
96 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080098 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
99
100 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800101 * @brief Get the public key DER blob from the identity storage.
102 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800103 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800104 * @return The DER Blob.
105 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800106 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800107 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800108 getPublicKey(const Name& keyName) = 0;
109
110 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 * @brief Check if the specified certificate already exists.
112 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800113 * @param certificateName The name of the certificate.
114 * @return true if the certificate exists, otherwise false.
115 */
116 virtual bool
117 doesCertificateExist(const Name& certificateName) = 0;
118
119 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800120 * @brief Add a certificate to the identity storage.
121 *
122 * It will add the corresponding public key and identity if they do not exist.
123 *
124 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800125 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800127 addCertificate(const IdentityCertificate& certificate) = 0;
128
129 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800130 * @brief Get a certificate from the identity storage.
131 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800132 * @param certificateName The name of the requested certificate.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 * @return The requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800134 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800135 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 virtual shared_ptr<IdentityCertificate>
137 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800138
139
140 /*****************************************
141 * Default Getter *
142 *****************************************/
143
144 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145 * @brief Get the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800146 *
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700147 * @param return The name of default identity,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800148 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800149 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700150 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800151 getDefaultIdentity() = 0;
152
153 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800154 * @brief Get the default key name for the specified identity.
155 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800156 * @param identityName The identity name.
157 * @return The default key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800158 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800159 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700160 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800161 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
162
163 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800164 * @brief Get the default certificate name for the specified key.
165 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800166 * @param keyName The key name.
167 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800168 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800169 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700170 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800171 getDefaultCertificateNameForKey(const Name& keyName) = 0;
172
Yingdi Yu2e57a582014-02-20 23:34:43 -0800173 /**
174 * @brief Get all the identities in public info.
175 *
176 * @param nameList On return, the identity list.
177 * @param isDefault If specified, only the default identity is returned.
178 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800179 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800181
Yingdi Yu2e57a582014-02-20 23:34:43 -0800182 /**
183 * @brief Get all the key name in public info.
184 *
185 * @param nameList On return, the key name list.
186 * @param isDefault If specified, only the default keys are returned.
187 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800188 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700189 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800190
Yingdi Yu2e57a582014-02-20 23:34:43 -0800191 /**
192 * @brief Get all the key name of a particular identity.
193 *
194 * @param identity The specified identity name.
195 * @param nameList On return, the key name list.
196 * @param isDefault If specified, only the default key is returned.
197 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800198 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800200
201 /**
202 * @brief Get all the certificate name in public info.
203 *
204 * @param nameList On return, the certificate name list.
205 * @param isDefault If specified, only the default certificates are returned.
206 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800207 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
209
Yingdi Yu2e57a582014-02-20 23:34:43 -0800210 /**
211 * @brief Get all the certificate name of a particular key.
212 *
213 * @param identity The specified key name.
214 * @param nameList On return, the certificate name list.
215 * @param isDefault If specified, only the default certificate is returned.
216 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800217 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700218 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800219
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700220 /*****************************************
221 * Delete Methods *
222 *****************************************/
223
224 /**
225 * @brief Delete a certificate.
226 *
227 * @param certificateName The certificate name.
228 */
229 virtual void
230 deleteCertificateInfo(const Name& certificateName) = 0;
231
232 /**
233 * @brief Delete a public key and related certificates.
234 *
235 * @param keyName The key name.
236 */
237 virtual void
238 deletePublicKeyInfo(const Name& keyName) = 0;
239
240 /**
241 * @brief Delete an identity and related public keys and certificates.
242 *
243 * @param identity The identity name.
244 */
245 virtual void
246 deleteIdentityInfo(const Name& identity) = 0;
247
Yingdi Yu31b4af22014-01-14 14:13:00 -0800248protected:
249
250 /*****************************************
251 * Default Setter *
252 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700253
Yingdi Yu31b4af22014-01-14 14:13:00 -0800254 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800255 * @brief Set the default identity.
256 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800257 * @param identityName The default identity name.
258 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700259 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800260 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700261
Yingdi Yu31b4af22014-01-14 14:13:00 -0800262 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800263 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700264 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800265 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800266 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800267 */
268 virtual void
269 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
270
271 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800272 * @brief Set the default certificate name for the corresponding key.
273 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800274 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800275 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800276 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700277 virtual void
278 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800279
280public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700281
Yingdi Yu31b4af22014-01-14 14:13:00 -0800282 /*****************************************
283 * Helper Methods *
284 *****************************************/
285
286 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700287 * @brief Set the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800288 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800289 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800291 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700292 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800293 setDefaultIdentity(const Name& identityName);
294
295 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800296 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700297 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800298 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800299 * @throws SecPublicInfo::Error if either the identity or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800300 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700301 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800302 setDefaultKeyNameForIdentity(const Name& keyName);
303
304 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800305 * @brief Set the default certificate name for the corresponding key.
306 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800307 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800308 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800309 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700310 inline void
311 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800312
313 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800314 * @brief Generate a key name for the identity.
315 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800316 * @param identityName The identity name.
317 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
318 * @return The generated key name.
319 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700320 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800321 getNewKeyName(const Name& identityName, bool useKsk);
322
Yingdi Yu2e57a582014-02-20 23:34:43 -0800323 /**
324 * @brief Get the default certificate name for the specified identity.
325 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800326 * @param identityName The identity name.
327 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800329 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700330 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800331 getDefaultCertificateNameForIdentity(const Name& identityName);
332
333 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800334 * @brief Get the default certificate name of the default identity
335 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800336 * @return The requested certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800337 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800338 */
339 inline Name
340 getDefaultCertificateName();
341
342 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800343 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
344 *
345 * @param certificate The certificate to be added.
346 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800347 */
348 inline void
349 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
350
351 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700352 * @brief Add a certificate into the public key identity storage and set the certificate as the
353 * default one of its corresponding identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800354 *
355 * @param certificate The certificate to be added.
356 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800357 */
358 inline void
359 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
360
Yingdi Yu2e57a582014-02-20 23:34:43 -0800361 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700362 * @brief Add a certificate into the public key identity storage and set the certificate as the
363 * default one of the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800364 *
365 * @param certificate The certificate to be added.
366 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
367 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800368 inline void
369 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
370
Yingdi Yu2e57a582014-02-20 23:34:43 -0800371 /**
372 * @brief get cached default certificate of the default identity.
373 *
374 * @return The certificate which might be a NULL pointer.
375 */
376 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800377 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700378
Yingdi Yu2e57a582014-02-20 23:34:43 -0800379 /**
380 * @brief try to get the default certificate of the default identity from the public info.
381 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800382 inline void
383 refreshDefaultCertificate();
384
385protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800386 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800387};
388
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800389inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800390SecPublicInfo::setDefaultIdentity(const Name& identityName)
391{
392 setDefaultIdentityInternal(identityName);
393 refreshDefaultCertificate();
394}
395
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800396inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800397SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
398{
399 setDefaultKeyNameForIdentityInternal(keyName);
400 refreshDefaultCertificate();
401}
402
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700403inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800404SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
405{
406 setDefaultCertificateNameForKeyInternal(certificateName);
407 refreshDefaultCertificate();
408}
409
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700410inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
412{
413 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
414}
415
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800416inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800417SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
418{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800420
Yingdi Yu31b4af22014-01-14 14:13:00 -0800421 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800422 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800424 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800425
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700426 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700427
Yingdi Yu88663af2014-01-15 15:21:38 -0800428 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800429
430 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800431 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800432
433 return keyName;
434}
435
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800436inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800437SecPublicInfo::getDefaultCertificateName()
438{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700439 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800440 refreshDefaultCertificate();
441
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700442 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800443 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800444
Yingdi Yu2e57a582014-02-20 23:34:43 -0800445 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800446}
447
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800448inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800449SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
450{
451 addCertificate(certificate);
452 setDefaultCertificateNameForKeyInternal(certificate.getName());
453 refreshDefaultCertificate();
454}
455
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800456inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800457SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
458{
459 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800460 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700461 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
462 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800463 setDefaultCertificateNameForKeyInternal(certName);
464 refreshDefaultCertificate();
465}
466
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800467inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800468SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
469{
470 addCertificate(certificate);
471 Name certName = certificate.getName();
472 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
473 setDefaultIdentityInternal(keyName.getPrefix(-1));
474 setDefaultKeyNameForIdentityInternal(keyName);
475 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800476 refreshDefaultCertificate();
477}
478
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800479inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800480SecPublicInfo::defaultCertificate()
481{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800482 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800483}
484
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800485inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800486SecPublicInfo::refreshDefaultCertificate()
487{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800488 try
489 {
490 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
491 m_defaultCertificate = getCertificate(certName);
492 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700493 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800494 {
495 m_defaultCertificate.reset();
496 }
497
Yingdi Yu31b4af22014-01-14 14:13:00 -0800498}
499
Yingdi Yufc40d872014-02-18 12:56:04 -0800500} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800501
Yingdi Yufc40d872014-02-18 12:56:04 -0800502#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP