blob: ee32d055380e37fa9fc72c95d745a482552dc27b [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 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070026class SecPublicInfo
27{
Yingdi Yu31b4af22014-01-14 14:13:00 -080028public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029 class Error : public std::runtime_error
30 {
31 public:
32 explicit
33 Error(const std::string& what)
34 : std::runtime_error(what)
35 {
36 }
37 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080038
39 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080040 * @brief The virtual Destructor.
Yingdi Yu31b4af22014-01-14 14:13:00 -080041 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080043 ~SecPublicInfo() {}
44
45 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080046 * @brief Check if the specified identity already exists.
47 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080048 * @param identityName The identity name.
49 * @return true if the identity exists, otherwise false.
50 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080052 doesIdentityExist(const Name& identityName) = 0;
53
54 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 * @brief Add a new identity.
56 *
57 * if identity already exist, do not add it again.
58 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080059 * @param identityName The identity name to be added.
60 */
61 virtual void
62 addIdentity(const Name& identityName) = 0;
63
64 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080065 * @brief Revoke the identity.
66 *
67 * @return true if the identity was revoked, 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 revokeIdentity() = 0;
71
72 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080073 * @brief Check if the specified key already exists.
74 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080075 * @param keyName The name of the key.
76 * @return true if the key exists, otherwise false.
77 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080079 doesPublicKeyExist(const Name& keyName) = 0;
80
81 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 * @brief Add a public key to the identity storage.
83 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080084 * @param keyName The name of the public key to be added.
85 * @param keyType Type of the public key to be added.
86 * @param publicKeyDer A blob of the public key DER to be added.
87 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080089 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
90
91 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080092 * @brief Get the public key DER blob from the identity storage.
93 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080094 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -080095 * @return The DER Blob.
96 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -080097 */
Yingdi Yu2e57a582014-02-20 23:34:43 -080098 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -080099 getPublicKey(const Name& keyName) = 0;
100
101 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800102 * @brief Check if the specified certificate already exists.
103 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800104 * @param certificateName The name of the certificate.
105 * @return true if the certificate exists, otherwise false.
106 */
107 virtual bool
108 doesCertificateExist(const Name& certificateName) = 0;
109
110 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 * @brief Add a certificate to the identity storage.
112 *
113 * It will add the corresponding public key and identity if they do not exist.
114 *
115 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800116 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800118 addCertificate(const IdentityCertificate& certificate) = 0;
119
120 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800121 * @brief Get a certificate from the identity storage.
122 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800123 * @param certificateName The name of the requested certificate.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 * @return The requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800125 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800126 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700127 virtual shared_ptr<IdentityCertificate>
128 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800129
130
131 /*****************************************
132 * Default Getter *
133 *****************************************/
134
135 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 * @brief Get the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800137 *
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138 * @param return The name of default identity,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800139 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800140 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700141 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800142 getDefaultIdentity() = 0;
143
144 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800145 * @brief Get the default key name for the specified identity.
146 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800147 * @param identityName The identity name.
148 * @return The default key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800149 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800150 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700151 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800152 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
153
154 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800155 * @brief Get the default certificate name for the specified key.
156 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800157 * @param keyName The key name.
158 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800159 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800160 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700161 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800162 getDefaultCertificateNameForKey(const Name& keyName) = 0;
163
Yingdi Yu2e57a582014-02-20 23:34:43 -0800164 /**
165 * @brief Get all the identities in public info.
166 *
167 * @param nameList On return, the identity list.
168 * @param isDefault If specified, only the default identity is returned.
169 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800170 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700171 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800172
Yingdi Yu2e57a582014-02-20 23:34:43 -0800173 /**
174 * @brief Get all the key name in public info.
175 *
176 * @param nameList On return, the key name list.
177 * @param isDefault If specified, only the default keys are returned.
178 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800179 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700180 getAllKeyNames(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 of a particular identity.
184 *
185 * @param identity The specified identity name.
186 * @param nameList On return, the key name list.
187 * @param isDefault If specified, only the default key is returned.
188 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800189 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700190 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800191
192 /**
193 * @brief Get all the certificate name in public info.
194 *
195 * @param nameList On return, the certificate name list.
196 * @param isDefault If specified, only the default certificates are returned.
197 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800198 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700199 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
200
Yingdi Yu2e57a582014-02-20 23:34:43 -0800201 /**
202 * @brief Get all the certificate name of a particular key.
203 *
204 * @param identity The specified key name.
205 * @param nameList On return, the certificate name list.
206 * @param isDefault If specified, only the default certificate is returned.
207 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800208 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700209 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800210
211protected:
212
213 /*****************************************
214 * Default Setter *
215 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700216
Yingdi Yu31b4af22014-01-14 14:13:00 -0800217 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800218 * @brief Set the default identity.
219 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800220 * @param identityName The default identity name.
221 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800223 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700224
Yingdi Yu31b4af22014-01-14 14:13:00 -0800225 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800226 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700227 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800228 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800229 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800230 */
231 virtual void
232 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
233
234 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800235 * @brief Set the default certificate name for the corresponding key.
236 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800237 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800238 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800239 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700240 virtual void
241 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800242
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800243 /*****************************************
244 * Delete Methods *
245 *****************************************/
246
247 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800248 * @brief Delete a certificate.
249 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800250 * @param certificateName The certificate name.
251 */
252 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700253 deleteCertificateInfo(const Name& certificateName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800254
255 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800256 * @brief Delete a public key and related certificates.
257 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800258 * @param keyName The key name.
259 */
260 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700261 deletePublicKeyInfo(const Name& keyName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800262
263 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800264 * @brief Delete an identity and related public keys and certificates.
265 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800266 * @param identity The identity name.
267 */
268 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700269 deleteIdentityInfo(const Name& identity) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800270
Yingdi Yu31b4af22014-01-14 14:13:00 -0800271public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700272
Yingdi Yu31b4af22014-01-14 14:13:00 -0800273 /*****************************************
274 * Helper Methods *
275 *****************************************/
276
277 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700278 * @brief Set the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800279 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800280 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800281 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800282 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700283 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800284 setDefaultIdentity(const Name& identityName);
285
286 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800287 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800289 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800290 * @throws SecPublicInfo::Error if either the identity or key 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 setDefaultKeyNameForIdentity(const Name& keyName);
294
295 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800296 * @brief Set the default certificate name for the corresponding key.
297 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800298 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800299 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800300 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700301 inline void
302 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800303
304 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800305 * @brief Generate a key name for the identity.
306 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800307 * @param identityName The identity name.
308 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
309 * @return The generated key name.
310 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700311 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800312 getNewKeyName(const Name& identityName, bool useKsk);
313
Yingdi Yu2e57a582014-02-20 23:34:43 -0800314 /**
315 * @brief Get the default certificate name for the specified identity.
316 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800317 * @param identityName The identity name.
318 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800319 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800320 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700321 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800322 getDefaultCertificateNameForIdentity(const Name& identityName);
323
324 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800325 * @brief Get the default certificate name of the default identity
326 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800327 * @return The requested 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 */
330 inline Name
331 getDefaultCertificateName();
332
333 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800334 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
335 *
336 * @param certificate The certificate to be added.
337 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800338 */
339 inline void
340 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
341
342 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700343 * @brief Add a certificate into the public key identity storage and set the certificate as the
344 * default one of its corresponding identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800345 *
346 * @param certificate The certificate to be added.
347 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800348 */
349 inline void
350 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
351
Yingdi Yu2e57a582014-02-20 23:34:43 -0800352 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700353 * @brief Add a certificate into the public key identity storage and set the certificate as the
354 * default one of the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800355 *
356 * @param certificate The certificate to be added.
357 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
358 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800359 inline void
360 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
361
Yingdi Yu2e57a582014-02-20 23:34:43 -0800362 /**
363 * @brief get cached default certificate of the default identity.
364 *
365 * @return The certificate which might be a NULL pointer.
366 */
367 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800368 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700369
Yingdi Yu2e57a582014-02-20 23:34:43 -0800370 /**
371 * @brief try to get the default certificate of the default identity from the public info.
372 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800373 inline void
374 refreshDefaultCertificate();
375
376protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800377 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800378};
379
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800380inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800381SecPublicInfo::setDefaultIdentity(const Name& identityName)
382{
383 setDefaultIdentityInternal(identityName);
384 refreshDefaultCertificate();
385}
386
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800387inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800388SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
389{
390 setDefaultKeyNameForIdentityInternal(keyName);
391 refreshDefaultCertificate();
392}
393
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700394inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800395SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
396{
397 setDefaultCertificateNameForKeyInternal(certificateName);
398 refreshDefaultCertificate();
399}
400
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700401inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800402SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
403{
404 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
405}
406
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800407inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800408SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
409{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800410 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411
Yingdi Yu31b4af22014-01-14 14:13:00 -0800412 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800413 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800414 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800415 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800416
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700417 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700418
Yingdi Yu88663af2014-01-15 15:21:38 -0800419 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800420
421 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800422 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423
424 return keyName;
425}
426
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800427inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800428SecPublicInfo::getDefaultCertificateName()
429{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700430 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800431 refreshDefaultCertificate();
432
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700433 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800434 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800435
Yingdi Yu2e57a582014-02-20 23:34:43 -0800436 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800437}
438
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800439inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800440SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
441{
442 addCertificate(certificate);
443 setDefaultCertificateNameForKeyInternal(certificate.getName());
444 refreshDefaultCertificate();
445}
446
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800447inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800448SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
449{
450 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800451 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700452 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
453 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800454 setDefaultCertificateNameForKeyInternal(certName);
455 refreshDefaultCertificate();
456}
457
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800458inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800459SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
460{
461 addCertificate(certificate);
462 Name certName = certificate.getName();
463 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
464 setDefaultIdentityInternal(keyName.getPrefix(-1));
465 setDefaultKeyNameForIdentityInternal(keyName);
466 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800467 refreshDefaultCertificate();
468}
469
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800470inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800471SecPublicInfo::defaultCertificate()
472{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800473 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800474}
475
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800476inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800477SecPublicInfo::refreshDefaultCertificate()
478{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800479 try
480 {
481 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
482 m_defaultCertificate = getCertificate(certName);
483 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700484 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800485 {
486 m_defaultCertificate.reset();
487 }
488
Yingdi Yu31b4af22014-01-14 14:13:00 -0800489}
490
Yingdi Yufc40d872014-02-18 12:56:04 -0800491} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800492
Yingdi Yufc40d872014-02-18 12:56:04 -0800493#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP