blob: 1d992d0894eff94953a7fb7ff84e5fdc63a7b07f [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:
27 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
28
29 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080030 * @brief The virtual Destructor.
Yingdi Yu31b4af22014-01-14 14:13:00 -080031 */
32 virtual
33 ~SecPublicInfo() {}
34
35 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080036 * @brief Check if the specified identity already exists.
37 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080038 * @param identityName The identity name.
39 * @return true if the identity exists, otherwise false.
40 */
41 virtual bool
42 doesIdentityExist(const Name& identityName) = 0;
43
44 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080045 * @brief Add a new identity.
46 *
47 * if identity already exist, do not add it again.
48 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080049 * @param identityName The identity name to be added.
50 */
51 virtual void
52 addIdentity(const Name& identityName) = 0;
53
54 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 * @brief Revoke the identity.
56 *
57 * @return true if the identity was revoked, otherwise false.
Yingdi Yu31b4af22014-01-14 14:13:00 -080058 */
59 virtual bool
60 revokeIdentity() = 0;
61
62 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080063 * @brief Check if the specified key already exists.
64 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080065 * @param keyName The name of the key.
66 * @return true if the key exists, otherwise false.
67 */
68 virtual bool
69 doesPublicKeyExist(const Name& keyName) = 0;
70
71 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 * @brief Add a public key to the identity storage.
73 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080074 * @param keyName The name of the public key to be added.
75 * @param keyType Type of the public key to be added.
76 * @param publicKeyDer A blob of the public key DER to be added.
77 */
78 virtual void
79 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
80
81 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 * @brief Get the public key DER blob from the identity storage.
83 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080084 * @param keyName The name of the requested public key.
Yingdi Yu2e57a582014-02-20 23:34:43 -080085 * @return The DER Blob.
86 * @throws SecPublicInfo::Error if public key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -080087 */
Yingdi Yu2e57a582014-02-20 23:34:43 -080088 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -080089 getPublicKey(const Name& keyName) = 0;
90
91 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080092 * @brief Check if the specified certificate already exists.
93 *
Yingdi Yu31b4af22014-01-14 14:13:00 -080094 * @param certificateName The name of the certificate.
95 * @return true if the certificate exists, otherwise false.
96 */
97 virtual bool
98 doesCertificateExist(const Name& certificateName) = 0;
99
100 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800101 * @brief Add a certificate to the identity storage.
102 *
103 * It will add the corresponding public key and identity if they do not exist.
104 *
105 * @param certificate The certificate to be added.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800106 */
107 virtual void
108 addCertificate(const IdentityCertificate& certificate) = 0;
109
110 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 * @brief Get a certificate from the identity storage.
112 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800113 * @param certificateName The name of the requested certificate.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800114 * @return The requested certificate.
115 * @throws SecPublicInfo::Error if the certificate does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800116 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800117 virtual shared_ptr<IdentityCertificate>
Yingdi Yu88663af2014-01-15 15:21:38 -0800118 getCertificate(const Name &certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800119
120
121 /*****************************************
122 * Default Getter *
123 *****************************************/
124
125 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800126 * @brief Get the default identity.
127 *
128 * @param return The name of default identity,
129 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800130 */
131 virtual Name
132 getDefaultIdentity() = 0;
133
134 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800135 * @brief Get the default key name for the specified identity.
136 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800137 * @param identityName The identity name.
138 * @return The default key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800139 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800140 */
141 virtual Name
142 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
143
144 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800145 * @brief Get the default certificate name for the specified key.
146 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800147 * @param keyName The key name.
148 * @return The default certificate 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 */
151 virtual Name
152 getDefaultCertificateNameForKey(const Name& keyName) = 0;
153
Yingdi Yu2e57a582014-02-20 23:34:43 -0800154 /**
155 * @brief Get all the identities in public info.
156 *
157 * @param nameList On return, the identity list.
158 * @param isDefault If specified, only the default identity is returned.
159 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800160 virtual void
161 getAllIdentities(std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800162
Yingdi Yu2e57a582014-02-20 23:34:43 -0800163 /**
164 * @brief Get all the key name in public info.
165 *
166 * @param nameList On return, the key name list.
167 * @param isDefault If specified, only the default keys are returned.
168 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800169 virtual void
170 getAllKeyNames(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 of a particular identity.
174 *
175 * @param identity The specified identity name.
176 * @param nameList On return, the key name list.
177 * @param isDefault If specified, only the default key is returned.
178 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800179 virtual void
180 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800181
182 /**
183 * @brief Get all the certificate name in public info.
184 *
185 * @param nameList On return, the certificate name list.
186 * @param isDefault If specified, only the default certificates are returned.
187 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800188 virtual void
189 getAllCertificateNames(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 certificate name of a particular key.
193 *
194 * @param identity The specified key name.
195 * @param nameList On return, the certificate name list.
196 * @param isDefault If specified, only the default certificate is returned.
197 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800198 virtual void
199 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800200
201protected:
202
203 /*****************************************
204 * Default Setter *
205 *****************************************/
206
207 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800208 * @brief Set the default identity.
209 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800210 * @param identityName The default identity name.
211 */
212 virtual void
213 setDefaultIdentityInternal(const Name& identityName) = 0;
214
215 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800216 * @brief Set the default key name for the corresponding identity.
217 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800218 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800219 * @throws SecPublicInfo::Error if the key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800220 */
221 virtual void
222 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
223
224 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800225 * @brief Set the default certificate name for the corresponding key.
226 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800227 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800228 * @throws SecPublicInfo::Error if the certificatedoes not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800229 */
230 virtual void
231 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
232
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800233 /*****************************************
234 * Delete Methods *
235 *****************************************/
236
237 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800238 * @brief Delete a certificate.
239 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800240 * @param certificateName The certificate name.
241 */
242 virtual void
243 deleteCertificateInfo(const Name &certificateName) = 0;
244
245 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800246 * @brief Delete a public key and related certificates.
247 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800248 * @param keyName The key name.
249 */
250 virtual void
251 deletePublicKeyInfo(const Name &keyName) = 0;
252
253 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800254 * @brief Delete an identity and related public keys and certificates.
255 *
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800256 * @param identity The identity name.
257 */
258 virtual void
259 deleteIdentityInfo(const Name &identity) = 0;
260
Yingdi Yu31b4af22014-01-14 14:13:00 -0800261public:
262
263 /*****************************************
264 * Helper Methods *
265 *****************************************/
266
267 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800268 * @brief Set the default identity.
269 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800270 * @param identityName The default identity name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800271 * @throws SecPublicInfo::Error if the identity does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800272 */
273 inline void
274 setDefaultIdentity(const Name& identityName);
275
276 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800277 * @brief Set the default key name for the corresponding identity.
278 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800279 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800280 * @throws SecPublicInfo::Error if either the identity or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800281 */
282 inline void
283 setDefaultKeyNameForIdentity(const Name& keyName);
284
285 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800286 * @brief Set the default certificate name for the corresponding key.
287 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800288 * @param certificateName The certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800289 * @throws SecPublicInfo::Error if either the certificate or key does not exist.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800290 */
291 inline void
292 setDefaultCertificateNameForKey(const Name& certificateName);
293
294 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800295 * @brief Generate a key name for the identity.
296 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800297 * @param identityName The identity name.
298 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
299 * @return The generated key name.
300 */
301 inline Name
302 getNewKeyName(const Name& identityName, bool useKsk);
303
Yingdi Yu2e57a582014-02-20 23:34:43 -0800304 /**
305 * @brief Get the default certificate name for the specified identity.
306 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800307 * @param identityName The identity name.
308 * @return The default certificate name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800309 * @throws SecPublicInfo::Error if no certificate is found.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800310 */
311 inline Name
312 getDefaultCertificateNameForIdentity(const Name& identityName);
313
314 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800315 * @brief Get the default certificate name of the default identity
316 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800317 * @return The requested 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 */
320 inline Name
321 getDefaultCertificateName();
322
323 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800324 * @brief Add a certificate and set the certificate as the default one of its corresponding key.
325 *
326 * @param certificate The certificate to be added.
327 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800328 */
329 inline void
330 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
331
332 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800333 * @brief Add a certificate into the public key identity storage and set the certificate as the default one of its corresponding identity.
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 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
340
Yingdi Yu2e57a582014-02-20 23:34:43 -0800341 /**
342 * @brief Add a certificate into the public key identity storage and set the certificate as the default one of the default identity.
343 *
344 * @param certificate The certificate to be added.
345 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
346 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800347 inline void
348 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
349
Yingdi Yu2e57a582014-02-20 23:34:43 -0800350 /**
351 * @brief get cached default certificate of the default identity.
352 *
353 * @return The certificate which might be a NULL pointer.
354 */
355 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800356 defaultCertificate();
357
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358 /**
359 * @brief try to get the default certificate of the default identity from the public info.
360 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800361 inline void
362 refreshDefaultCertificate();
363
364protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800366};
367
368void
369SecPublicInfo::setDefaultIdentity(const Name& identityName)
370{
371 setDefaultIdentityInternal(identityName);
372 refreshDefaultCertificate();
373}
374
375void
376SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
377{
378 setDefaultKeyNameForIdentityInternal(keyName);
379 refreshDefaultCertificate();
380}
381
382void
383SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
384{
385 setDefaultCertificateNameForKeyInternal(certificateName);
386 refreshDefaultCertificate();
387}
388
389Name
390SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
391{
392 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
393}
394
395Name
396SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
397{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800398 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800399
Yingdi Yu31b4af22014-01-14 14:13:00 -0800400 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800401 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800402 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800403 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800404
Yingdi Yu7056ce82014-02-03 12:55:49 -0800405 oss << getNow();
406
Yingdi Yu88663af2014-01-15 15:21:38 -0800407 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800408
409 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800410 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411
412 return keyName;
413}
414
415Name
416SecPublicInfo::getDefaultCertificateName()
417{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800418 if(!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419 refreshDefaultCertificate();
420
Yingdi Yu2e57a582014-02-20 23:34:43 -0800421 if(!static_cast<bool>(m_defaultCertificate))
422 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423
Yingdi Yu2e57a582014-02-20 23:34:43 -0800424 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800425}
426
427void
428SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
429{
430 addCertificate(certificate);
431 setDefaultCertificateNameForKeyInternal(certificate.getName());
432 refreshDefaultCertificate();
433}
434
435void
436SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
437{
438 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800439 Name certName = certificate.getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -0800440 setDefaultKeyNameForIdentityInternal(IdentityCertificate::certificateNameToPublicKeyName(certName));
Yingdi Yu88663af2014-01-15 15:21:38 -0800441 setDefaultCertificateNameForKeyInternal(certName);
442 refreshDefaultCertificate();
443}
444
445void
446SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
447{
448 addCertificate(certificate);
449 Name certName = certificate.getName();
450 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
451 setDefaultIdentityInternal(keyName.getPrefix(-1));
452 setDefaultKeyNameForIdentityInternal(keyName);
453 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800454 refreshDefaultCertificate();
455}
456
Yingdi Yu2e57a582014-02-20 23:34:43 -0800457shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800458SecPublicInfo::defaultCertificate()
459{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800460 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800461}
462
463void
464SecPublicInfo::refreshDefaultCertificate()
465{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800466 try
467 {
468 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
469 m_defaultCertificate = getCertificate(certName);
470 }
471 catch(SecPublicInfo::Error& e)
472 {
473 m_defaultCertificate.reset();
474 }
475
Yingdi Yu31b4af22014-01-14 14:13:00 -0800476}
477
Yingdi Yufc40d872014-02-18 12:56:04 -0800478} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800479
Yingdi Yufc40d872014-02-18 12:56:04 -0800480#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP