blob: 99b4f5713b9bd0f38864ee834fb4a459f479a888 [file] [log] [blame]
Yingdi Yu31b4af22014-01-14 14:13:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_HPP
10#define NDN_SECURITY_SEC_PUBLIC_INFO_HPP
Yingdi Yu31b4af22014-01-14 14:13:00 -080011
Yingdi Yu4f324632014-01-15 18:10:03 -080012#include "../name.hpp"
13#include "security-common.hpp"
14#include "public-key.hpp"
15#include "identity-certificate.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080016
Yingdi Yu88663af2014-01-15 15:21:38 -080017
Yingdi Yu31b4af22014-01-14 14:13:00 -080018namespace ndn {
19
20/**
Yingdi Yu2e57a582014-02-20 23:34:43 -080021 * @brief SecPublicInfo is a base class for the storage of public information.
22 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070023 * It specify interfaces related to public information, such as identity, public keys and
24 * certificates.
Yingdi Yu31b4af22014-01-14 14:13:00 -080025 */
26class SecPublicInfo {
27public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028 class Error : public std::runtime_error
29 {
30 public:
31 explicit
32 Error(const std::string& what)
33 : std::runtime_error(what)
34 {
35 }
36 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080037
38 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080039 * @brief The virtual Destructor.
Yingdi Yu31b4af22014-01-14 14:13:00 -080040 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080042 ~SecPublicInfo() {}
43
44 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080045 * @brief Check if the specified identity already exists.
46 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080047 * @param identityName The identity name.
48 * @return true if the identity exists, otherwise false.
49 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080051 doesIdentityExist(const Name& identityName) = 0;
52
53 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080054 * @brief Add a new identity.
55 *
56 * if identity already exist, do not add it again.
57 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080058 * @param identityName The identity name to be added.
59 */
60 virtual void
61 addIdentity(const Name& identityName) = 0;
62
63 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080064 * @brief Revoke the identity.
65 *
66 * @return true if the identity was revoked, otherwise false.
Yingdi Yu31b4af22014-01-14 14:13:00 -080067 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080069 revokeIdentity() = 0;
70
71 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 * @brief Check if the specified key already exists.
73 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080074 * @param keyName The name of the key.
75 * @return true if the key exists, otherwise false.
76 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080078 doesPublicKeyExist(const Name& keyName) = 0;
79
80 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080081 * @brief Add a public key to the identity storage.
82 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080083 * @param keyName The name of the public key to be added.
84 * @param keyType Type of the public key to be added.
85 * @param publicKeyDer A blob of the public key DER to be added.
86 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080088 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
89
90 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080091 * @brief Get the public key DER blob from the identity storage.
92 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080093 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -080094 * @return The DER Blob.
95 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -080096 */
Yingdi Yu2e57a582014-02-20 23:34:43 -080097 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -080098 getPublicKey(const Name& keyName) = 0;
99
100 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800101 * @brief Check if the specified certificate already exists.
102 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800103 * @param certificateName The name of the certificate.
104 * @return true if the certificate exists, otherwise false.
105 */
106 virtual bool
107 doesCertificateExist(const Name& certificateName) = 0;
108
109 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800110 * @brief Add a certificate to the identity storage.
111 *
112 * It will add the corresponding public key and identity if they do not exist.
113 *
114 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800115 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800117 addCertificate(const IdentityCertificate& certificate) = 0;
118
119 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800120 * @brief Get a certificate from the identity storage.
121 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800122 * @param certificateName The name of the requested certificate.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 * @return The requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800124 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800125 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126 virtual shared_ptr<IdentityCertificate>
127 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800128
129
130 /*****************************************
131 * Default Getter *
132 *****************************************/
133
134 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 * @brief Get the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800136 *
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137 * @param return The name of default identity,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800139 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800141 getDefaultIdentity() = 0;
142
143 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800144 * @brief Get the default key name for the specified identity.
145 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800146 * @param identityName The identity name.
147 * @return The default key name.
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 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
152
153 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800154 * @brief Get the default certificate name for the specified key.
155 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800156 * @param keyName The key name.
157 * @return The default certificate 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 getDefaultCertificateNameForKey(const Name& keyName) = 0;
162
Yingdi Yu2e57a582014-02-20 23:34:43 -0800163 /**
164 * @brief Get all the identities in public info.
165 *
166 * @param nameList On return, the identity list.
167 * @param isDefault If specified, only the default identity is returned.
168 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800169 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700170 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800171
Yingdi Yu2e57a582014-02-20 23:34:43 -0800172 /**
173 * @brief Get all the key name in public info.
174 *
175 * @param nameList On return, the key name list.
176 * @param isDefault If specified, only the default keys are returned.
177 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800178 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700179 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800180
Yingdi Yu2e57a582014-02-20 23:34:43 -0800181 /**
182 * @brief Get all the key name of a particular identity.
183 *
184 * @param identity The specified identity name.
185 * @param nameList On return, the key name list.
186 * @param isDefault If specified, only the default key is returned.
187 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800188 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700189 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800190
191 /**
192 * @brief Get all the certificate name in public info.
193 *
194 * @param nameList On return, the certificate name list.
195 * @param isDefault If specified, only the default certificates are returned.
196 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800197 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700198 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
199
Yingdi Yu2e57a582014-02-20 23:34:43 -0800200 /**
201 * @brief Get all the certificate name of a particular key.
202 *
203 * @param identity The specified key name.
204 * @param nameList On return, the certificate name list.
205 * @param isDefault If specified, only the default certificate is returned.
206 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800207 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800209
210protected:
211
212 /*****************************************
213 * Default Setter *
214 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700215
Yingdi Yu31b4af22014-01-14 14:13:00 -0800216 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800217 * @brief Set the default identity.
218 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800219 * @param identityName The default identity name.
220 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700221 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800222 setDefaultIdentityInternal(const Name& identityName) = 0;
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 key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700226 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800227 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800228 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800229 */
230 virtual void
231 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
232
233 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800234 * @brief Set the default certificate name for the corresponding key.
235 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800236 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800237 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800238 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700239 virtual void
240 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800241
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800242 /*****************************************
243 * Delete Methods *
244 *****************************************/
245
246 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800247 * @brief Delete a certificate.
248 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800249 * @param certificateName The certificate name.
250 */
251 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700252 deleteCertificateInfo(const Name& certificateName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800253
254 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800255 * @brief Delete a public key and related certificates.
256 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800257 * @param keyName The key name.
258 */
259 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700260 deletePublicKeyInfo(const Name& keyName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800261
262 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800263 * @brief Delete an identity and related public keys and certificates.
264 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800265 * @param identity The identity name.
266 */
267 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700268 deleteIdentityInfo(const Name& identity) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800269
Yingdi Yu31b4af22014-01-14 14:13:00 -0800270public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700271
Yingdi Yu31b4af22014-01-14 14:13:00 -0800272 /*****************************************
273 * Helper Methods *
274 *****************************************/
275
276 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700277 * @brief Set the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800278 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800279 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800280 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800281 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700282 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800283 setDefaultIdentity(const Name& identityName);
284
285 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800286 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700287 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800288 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800289 * @throws SecPublicInfo::Error if either the identity or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800290 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700291 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800292 setDefaultKeyNameForIdentity(const Name& keyName);
293
294 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800295 * @brief Set the default certificate name for the corresponding key.
296 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800297 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800298 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800299 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700300 inline void
301 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800302
303 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800304 * @brief Generate a key name for the identity.
305 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800306 * @param identityName The identity name.
307 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
308 * @return The generated key name.
309 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700310 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800311 getNewKeyName(const Name& identityName, bool useKsk);
312
Yingdi Yu2e57a582014-02-20 23:34:43 -0800313 /**
314 * @brief Get the default certificate name for the specified identity.
315 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800316 * @param identityName The identity name.
317 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800318 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800319 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700320 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800321 getDefaultCertificateNameForIdentity(const Name& identityName);
322
323 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800324 * @brief Get the default certificate name of the default identity
325 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800326 * @return The requested certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800327 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800328 */
329 inline Name
330 getDefaultCertificateName();
331
332 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
334 *
335 * @param certificate The certificate to be added.
336 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800337 */
338 inline void
339 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
340
341 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700342 * @brief Add a certificate into the public key identity storage and set the certificate as the
343 * default one of its corresponding identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800344 *
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 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
350
Yingdi Yu2e57a582014-02-20 23:34:43 -0800351 /**
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 the default 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)
357 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800358 inline void
359 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
360
Yingdi Yu2e57a582014-02-20 23:34:43 -0800361 /**
362 * @brief get cached default certificate of the default identity.
363 *
364 * @return The certificate which might be a NULL pointer.
365 */
366 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800367 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700368
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369 /**
370 * @brief try to get the default certificate of the default identity from the public info.
371 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800372 inline void
373 refreshDefaultCertificate();
374
375protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800376 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800377};
378
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800379inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800380SecPublicInfo::setDefaultIdentity(const Name& identityName)
381{
382 setDefaultIdentityInternal(identityName);
383 refreshDefaultCertificate();
384}
385
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800386inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800387SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
388{
389 setDefaultKeyNameForIdentityInternal(keyName);
390 refreshDefaultCertificate();
391}
392
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700393inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800394SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
395{
396 setDefaultCertificateNameForKeyInternal(certificateName);
397 refreshDefaultCertificate();
398}
399
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700400inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800401SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
402{
403 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
404}
405
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800406inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800407SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
408{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800409 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800410
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800412 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800413 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800414 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800415
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700416 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700417
Yingdi Yu88663af2014-01-15 15:21:38 -0800418 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419
420 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800421 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800422
423 return keyName;
424}
425
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800426inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800427SecPublicInfo::getDefaultCertificateName()
428{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700429 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800430 refreshDefaultCertificate();
431
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700432 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800433 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800434
Yingdi Yu2e57a582014-02-20 23:34:43 -0800435 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800436}
437
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800438inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800439SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
440{
441 addCertificate(certificate);
442 setDefaultCertificateNameForKeyInternal(certificate.getName());
443 refreshDefaultCertificate();
444}
445
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800446inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800447SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
448{
449 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800450 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700451 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
452 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800453 setDefaultCertificateNameForKeyInternal(certName);
454 refreshDefaultCertificate();
455}
456
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800457inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800458SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
459{
460 addCertificate(certificate);
461 Name certName = certificate.getName();
462 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
463 setDefaultIdentityInternal(keyName.getPrefix(-1));
464 setDefaultKeyNameForIdentityInternal(keyName);
465 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800466 refreshDefaultCertificate();
467}
468
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800469inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800470SecPublicInfo::defaultCertificate()
471{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800472 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800473}
474
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800475inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800476SecPublicInfo::refreshDefaultCertificate()
477{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800478 try
479 {
480 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
481 m_defaultCertificate = getCertificate(certName);
482 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700483 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800484 {
485 m_defaultCertificate.reset();
486 }
487
Yingdi Yu31b4af22014-01-14 14:13:00 -0800488}
489
Yingdi Yufc40d872014-02-18 12:56:04 -0800490} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800491
Yingdi Yufc40d872014-02-18 12:56:04 -0800492#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP