blob: 5fe7b3c74e3d8f5f544a280bbe1d812a351c9756 [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 *
23 * It specify interfaces related to public information, such as identity, public keys and certificates.
Yingdi Yu31b4af22014-01-14 14:13:00 -080024 */
25class SecPublicInfo {
26public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027 class Error : public std::runtime_error
28 {
29 public:
30 explicit
31 Error(const std::string& what)
32 : std::runtime_error(what)
33 {
34 }
35 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080036
37 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080038 * @brief The virtual Destructor.
Yingdi Yu31b4af22014-01-14 14:13:00 -080039 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 virtual
Yingdi Yu31b4af22014-01-14 14:13:00 -080041 ~SecPublicInfo() {}
42
43 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080044 * @brief Check if the specified identity already exists.
45 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080046 * @param identityName The identity name.
47 * @return true if the identity exists, otherwise false.
48 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080050 doesIdentityExist(const Name& identityName) = 0;
51
52 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080053 * @brief Add a new identity.
54 *
55 * if identity already exist, do not add it again.
56 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080057 * @param identityName The identity name to be added.
58 */
59 virtual void
60 addIdentity(const Name& identityName) = 0;
61
62 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080063 * @brief Revoke the identity.
64 *
65 * @return true if the identity was revoked, otherwise false.
Yingdi Yu31b4af22014-01-14 14:13:00 -080066 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080068 revokeIdentity() = 0;
69
70 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 * @brief Check if the specified key already exists.
72 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080073 * @param keyName The name of the key.
74 * @return true if the key exists, otherwise false.
75 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080077 doesPublicKeyExist(const Name& keyName) = 0;
78
79 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080080 * @brief Add a public key to the identity storage.
81 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080082 * @param keyName The name of the public key to be added.
83 * @param keyType Type of the public key to be added.
84 * @param publicKeyDer A blob of the public key DER to be added.
85 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -080087 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
88
89 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080090 * @brief Get the public key DER blob from the identity storage.
91 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080092 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -080093 * @return The DER Blob.
94 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -080095 */
Yingdi Yu2e57a582014-02-20 23:34:43 -080096 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -080097 getPublicKey(const Name& keyName) = 0;
98
99 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800100 * @brief Check if the specified certificate already exists.
101 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800102 * @param certificateName The name of the certificate.
103 * @return true if the certificate exists, otherwise false.
104 */
105 virtual bool
106 doesCertificateExist(const Name& certificateName) = 0;
107
108 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800109 * @brief Add a certificate to the identity storage.
110 *
111 * It will add the corresponding public key and identity if they do not exist.
112 *
113 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800114 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800116 addCertificate(const IdentityCertificate& certificate) = 0;
117
118 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800119 * @brief Get a certificate from the identity storage.
120 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800121 * @param certificateName The name of the requested certificate.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 * @return The requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800123 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800124 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 virtual shared_ptr<IdentityCertificate>
126 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800127
128
129 /*****************************************
130 * Default Getter *
131 *****************************************/
132
133 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134 * @brief Get the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800135 *
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 * @param return The name of default identity,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800137 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800138 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800140 getDefaultIdentity() = 0;
141
142 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800143 * @brief Get the default key name for the specified identity.
144 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800145 * @param identityName The identity name.
146 * @return The default key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800147 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800148 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800150 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
151
152 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800153 * @brief Get the default certificate name for the specified key.
154 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800155 * @param keyName The key name.
156 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800157 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800158 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800160 getDefaultCertificateNameForKey(const Name& keyName) = 0;
161
Yingdi Yu2e57a582014-02-20 23:34:43 -0800162 /**
163 * @brief Get all the identities in public info.
164 *
165 * @param nameList On return, the identity list.
166 * @param isDefault If specified, only the default identity is returned.
167 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800168 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700169 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800170
Yingdi Yu2e57a582014-02-20 23:34:43 -0800171 /**
172 * @brief Get all the key name in public info.
173 *
174 * @param nameList On return, the key name list.
175 * @param isDefault If specified, only the default keys are returned.
176 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800177 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178 getAllKeyNames(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 of a particular identity.
182 *
183 * @param identity The specified identity name.
184 * @param nameList On return, the key name list.
185 * @param isDefault If specified, only the default key is returned.
186 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800187 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700188 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189
190 /**
191 * @brief Get all the certificate name in public info.
192 *
193 * @param nameList On return, the certificate name list.
194 * @param isDefault If specified, only the default certificates are returned.
195 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800196 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
198
Yingdi Yu2e57a582014-02-20 23:34:43 -0800199 /**
200 * @brief Get all the certificate name of a particular key.
201 *
202 * @param identity The specified key name.
203 * @param nameList On return, the certificate name list.
204 * @param isDefault If specified, only the default certificate is returned.
205 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800206 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800208
209protected:
210
211 /*****************************************
212 * Default Setter *
213 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700214
Yingdi Yu31b4af22014-01-14 14:13:00 -0800215 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800216 * @brief Set the default identity.
217 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800218 * @param identityName The default identity name.
219 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700220 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800221 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700222
Yingdi Yu31b4af22014-01-14 14:13:00 -0800223 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800224 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700225 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800226 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800227 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800228 */
229 virtual void
230 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
231
232 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800233 * @brief Set the default certificate name for the corresponding key.
234 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800235 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800236 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800237 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700238 virtual void
239 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800240
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800241 /*****************************************
242 * Delete Methods *
243 *****************************************/
244
245 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800246 * @brief Delete a certificate.
247 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800248 * @param certificateName The certificate name.
249 */
250 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700251 deleteCertificateInfo(const Name& certificateName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800252
253 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800254 * @brief Delete a public key and related certificates.
255 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800256 * @param keyName The key name.
257 */
258 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700259 deletePublicKeyInfo(const Name& keyName) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800260
261 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800262 * @brief Delete an identity and related public keys and certificates.
263 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800264 * @param identity The identity name.
265 */
266 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700267 deleteIdentityInfo(const Name& identity) = 0;
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800268
Yingdi Yu31b4af22014-01-14 14:13:00 -0800269public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700270
Yingdi Yu31b4af22014-01-14 14:13:00 -0800271 /*****************************************
272 * Helper Methods *
273 *****************************************/
274
275 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700276 * @brief Set the default identity.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800277 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800278 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800279 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800280 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700281 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800282 setDefaultIdentity(const Name& identityName);
283
284 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800285 * @brief Set the default key name for the corresponding identity.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700286 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800287 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800288 * @throws SecPublicInfo::Error if either the identity or key 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 setDefaultKeyNameForIdentity(const Name& keyName);
292
293 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800294 * @brief Set the default certificate name for the corresponding key.
295 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800296 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800297 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800298 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700299 inline void
300 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800301
302 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800303 * @brief Generate a key name for the identity.
304 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800305 * @param identityName The identity name.
306 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
307 * @return The generated key name.
308 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700309 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800310 getNewKeyName(const Name& identityName, bool useKsk);
311
Yingdi Yu2e57a582014-02-20 23:34:43 -0800312 /**
313 * @brief Get the default certificate name for the specified identity.
314 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800315 * @param identityName The identity name.
316 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800317 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800318 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700319 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800320 getDefaultCertificateNameForIdentity(const Name& identityName);
321
322 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800323 * @brief Get the default certificate name of the default identity
324 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800325 * @return The requested 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 */
328 inline Name
329 getDefaultCertificateName();
330
331 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800332 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
333 *
334 * @param certificate The certificate to be added.
335 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800336 */
337 inline void
338 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
339
340 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800341 * @brief Add a certificate into the public key identity storage and set the certificate as the default one of its corresponding identity.
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 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
348
Yingdi Yu2e57a582014-02-20 23:34:43 -0800349 /**
350 * @brief Add a certificate into the public key identity storage and set the certificate as the default one of the default identity.
351 *
352 * @param certificate The certificate to be added.
353 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
354 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800355 inline void
356 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
357
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358 /**
359 * @brief get cached default certificate of the default identity.
360 *
361 * @return The certificate which might be a NULL pointer.
362 */
363 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800364 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700365
Yingdi Yu2e57a582014-02-20 23:34:43 -0800366 /**
367 * @brief try to get the default certificate of the default identity from the public info.
368 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800369 inline void
370 refreshDefaultCertificate();
371
372protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800373 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800374};
375
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800376inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800377SecPublicInfo::setDefaultIdentity(const Name& identityName)
378{
379 setDefaultIdentityInternal(identityName);
380 refreshDefaultCertificate();
381}
382
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800383inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800384SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
385{
386 setDefaultKeyNameForIdentityInternal(keyName);
387 refreshDefaultCertificate();
388}
389
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700390inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800391SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
392{
393 setDefaultCertificateNameForKeyInternal(certificateName);
394 refreshDefaultCertificate();
395}
396
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700397inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800398SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
399{
400 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
401}
402
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800403inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800404SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
405{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800406 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800407
Yingdi Yu31b4af22014-01-14 14:13:00 -0800408 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800409 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800410 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800411 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800412
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700413 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700414
Yingdi Yu88663af2014-01-15 15:21:38 -0800415 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800416
417 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800418 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419
420 return keyName;
421}
422
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800423inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800424SecPublicInfo::getDefaultCertificateName()
425{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700426 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800427 refreshDefaultCertificate();
428
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700429 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800430 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800431
Yingdi Yu2e57a582014-02-20 23:34:43 -0800432 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800433}
434
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800435inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800436SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
437{
438 addCertificate(certificate);
439 setDefaultCertificateNameForKeyInternal(certificate.getName());
440 refreshDefaultCertificate();
441}
442
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800443inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800444SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
445{
446 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800447 Name certName = certificate.getName();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700448 setDefaultKeyNameForIdentityInternal(IdentityCertificate::certificateNameToPublicKeyName(certName));
Yingdi Yu88663af2014-01-15 15:21:38 -0800449 setDefaultCertificateNameForKeyInternal(certName);
450 refreshDefaultCertificate();
451}
452
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800453inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800454SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
455{
456 addCertificate(certificate);
457 Name certName = certificate.getName();
458 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
459 setDefaultIdentityInternal(keyName.getPrefix(-1));
460 setDefaultKeyNameForIdentityInternal(keyName);
461 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800462 refreshDefaultCertificate();
463}
464
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800465inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800466SecPublicInfo::defaultCertificate()
467{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800468 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800469}
470
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800471inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800472SecPublicInfo::refreshDefaultCertificate()
473{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800474 try
475 {
476 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
477 m_defaultCertificate = getCertificate(certName);
478 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700479 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800480 {
481 m_defaultCertificate.reset();
482 }
483
Yingdi Yu31b4af22014-01-14 14:13:00 -0800484}
485
Yingdi Yufc40d872014-02-18 12:56:04 -0800486} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800487
Yingdi Yufc40d872014-02-18 12:56:04 -0800488#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP