blob: 1949d51c015f1fdc3eb77e54a1611afa62e7bf54 [file] [log] [blame]
Yingdi Yu31b4af22014-01-14 14:13:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Yingdi Yu31b4af22014-01-14 14:13:00 -080014 */
15
Yingdi Yufc40d872014-02-18 12:56:04 -080016#ifndef NDN_SECURITY_SEC_PUBLIC_INFO_HPP
17#define NDN_SECURITY_SEC_PUBLIC_INFO_HPP
Yingdi Yu31b4af22014-01-14 14:13:00 -080018
Yingdi Yu4f324632014-01-15 18:10:03 -080019#include "../name.hpp"
20#include "security-common.hpp"
21#include "public-key.hpp"
22#include "identity-certificate.hpp"
Yingdi Yu31b4af22014-01-14 14:13:00 -080023
Yingdi Yu88663af2014-01-15 15:21:38 -080024
Yingdi Yu31b4af22014-01-14 14:13:00 -080025namespace ndn {
26
27/**
Yingdi Yu2e57a582014-02-20 23:34:43 -080028 * @brief SecPublicInfo is a base class for the storage of public information.
29 *
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070030 * It specify interfaces related to public information, such as identity, public keys and
31 * certificates.
Yingdi Yu31b4af22014-01-14 14:13:00 -080032 */
Yingdi Yuf56c68f2014-04-24 21:50:13 -070033class SecPublicInfo : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070034{
Yingdi Yu31b4af22014-01-14 14:13:00 -080035public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036 class Error : public std::runtime_error
37 {
38 public:
39 explicit
40 Error(const std::string& what)
41 : std::runtime_error(what)
42 {
43 }
44 };
Yingdi Yu31b4af22014-01-14 14:13:00 -080045
46 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070047 * @brief The virtual Destructor
Yingdi Yu31b4af22014-01-14 14:13:00 -080048 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 virtual
Yingdi Yuf56c68f2014-04-24 21:50:13 -070050 ~SecPublicInfo()
51 {
52 }
Yingdi Yu31b4af22014-01-14 14:13:00 -080053
54 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070055 * @brief Check if the specified identity already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -080056 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070057 * @param identityName The identity name
58 * @return true if the identity exists, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080059 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080061 doesIdentityExist(const Name& identityName) = 0;
62
63 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070064 * @brief Add a new identity
Yingdi Yu2e57a582014-02-20 23:34:43 -080065 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070066 * if identity already exist, do not add it again
Yingdi Yu2e57a582014-02-20 23:34:43 -080067 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070068 * @param identityName The identity name to be added
Yingdi Yu31b4af22014-01-14 14:13:00 -080069 */
70 virtual void
71 addIdentity(const Name& identityName) = 0;
72
73 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070074 * @brief Revoke the identity
Yingdi Yu2e57a582014-02-20 23:34:43 -080075 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070076 * @return true if the identity was revoked, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080077 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080079 revokeIdentity() = 0;
80
81 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070082 * @brief Check if the specified key already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -080083 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070084 * @param keyName The name of the key
85 * @return true if the key exists, otherwise false
Yingdi Yu31b4af22014-01-14 14:13:00 -080086 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 virtual bool
Yingdi Yu31b4af22014-01-14 14:13:00 -080088 doesPublicKeyExist(const Name& keyName) = 0;
89
90 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -080091 * @brief Add a public key to the identity storage.
92 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070093 * @param keyName The name of the public key to be added
94 * @param keyType Type of the public key to be added
95 * @param publicKey Reference to the PublicKey object
Yingdi Yu31b4af22014-01-14 14:13:00 -080096 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 virtual void
Alexander Afanasyev770827c2014-05-13 17:42:55 -070098 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -080099
100 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700101 * @brief Get shared pointer to PublicKey object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800102 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700103 * @param keyName The name of the requested public key
104 * @throws SecPublicInfo::Error if public key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800105 */
Yingdi Yu2e57a582014-02-20 23:34:43 -0800106 virtual shared_ptr<PublicKey>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800107 getPublicKey(const Name& keyName) = 0;
108
109 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700110 * @brief Check if the specified certificate already exists
Yingdi Yu2e57a582014-02-20 23:34:43 -0800111 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700112 * @param certificateName The name of the certificate
Yingdi Yu31b4af22014-01-14 14:13:00 -0800113 */
114 virtual bool
115 doesCertificateExist(const Name& certificateName) = 0;
116
117 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800118 * @brief Add a certificate to the identity storage.
119 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700120 * It will add the corresponding public key and identity if they do not exist
Yingdi Yu2e57a582014-02-20 23:34:43 -0800121 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700122 * @param certificate The certificate to be added
Yingdi Yu31b4af22014-01-14 14:13:00 -0800123 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800125 addCertificate(const IdentityCertificate& certificate) = 0;
126
127 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700128 * @brief Get a shared pointer to identity certificate object from the identity storage
Yingdi Yu2e57a582014-02-20 23:34:43 -0800129 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700130 * @param certificateName The name of the requested certificate
131 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800132 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 virtual shared_ptr<IdentityCertificate>
134 getCertificate(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800135
136
137 /*****************************************
138 * Default Getter *
139 *****************************************/
140
141 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700142 * @brief Get name of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800143 *
Yingdi Yu2e57a582014-02-20 23:34:43 -0800144 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800145 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800147 getDefaultIdentity() = 0;
148
149 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700150 * @brief Get name of the default key name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800151 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700152 * @param identityName The identity name
153 * @throws SecPublicInfo::Error if there is no default
Yingdi Yu31b4af22014-01-14 14:13:00 -0800154 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700155 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800156 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
157
158 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700159 * @brief Get name of the default certificate name for the specified key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800160 *
Yingdi Yu31b4af22014-01-14 14:13:00 -0800161 * @param keyName The key name.
Yingdi Yu2e57a582014-02-20 23:34:43 -0800162 * @throws SecPublicInfo::Error if there is no default.
Yingdi Yu31b4af22014-01-14 14:13:00 -0800163 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700164 virtual Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800165 getDefaultCertificateNameForKey(const Name& keyName) = 0;
166
Yingdi Yu2e57a582014-02-20 23:34:43 -0800167 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700168 * @brief Get all the identities from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800169 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700170 * @param [out] nameList On return, the identity list
171 * @param isDefault If specified, only the default identity is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800172 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800173 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700174 getAllIdentities(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800175
Yingdi Yu2e57a582014-02-20 23:34:43 -0800176 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700177 * @brief Get all the key names from public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800178 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700179 * @param [out] nameList On return, the key name list.
180 * @param isDefault If specified, only the default keys are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800181 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800182 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700183 getAllKeyNames(std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800184
Yingdi Yu2e57a582014-02-20 23:34:43 -0800185 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700186 * @brief Get all the key names of a particular identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800187 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700188 * @param identity The specified identity name
189 * @param [out] nameList On return, the key name list
190 * @param isDefault If specified, only the default key is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800191 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800192 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800194
195 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700196 * @brief Get all the certificate name in public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800197 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700198 * @param [out] nameList On return, the certificate name list
199 * @param isDefault If specified, only the default certificates are returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800200 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800201 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700202 getAllCertificateNames(std::vector<Name>& nameList, bool isDefault) = 0;
203
Yingdi Yu2e57a582014-02-20 23:34:43 -0800204 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700205 * @brief Get all the certificate name of a particular key name
Yingdi Yu2e57a582014-02-20 23:34:43 -0800206 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700207 * @param keyName The specified key name
208 * @param [out] nameList On return, the certificate name list
209 * @param isDefault If specified, only the default certificate is returned
Yingdi Yu2e57a582014-02-20 23:34:43 -0800210 */
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800211 virtual void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700212 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name>& nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800213
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700214 /*****************************************
215 * Delete Methods *
216 *****************************************/
217
218 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700219 * @brief Delete a certificate
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700220 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700221 * @param certificateName The certificate name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700222 */
223 virtual void
224 deleteCertificateInfo(const Name& certificateName) = 0;
225
226 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700227 * @brief Delete a public key and related certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700228 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700229 * @param keyName The key name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700230 */
231 virtual void
232 deletePublicKeyInfo(const Name& keyName) = 0;
233
234 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700235 * @brief Delete an identity and related public keys and certificates
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700236 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700237 * @param identity The identity name
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700238 */
239 virtual void
240 deleteIdentityInfo(const Name& identity) = 0;
241
Yingdi Yu31b4af22014-01-14 14:13:00 -0800242protected:
243
244 /*****************************************
245 * Default Setter *
246 *****************************************/
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700247
Yingdi Yu31b4af22014-01-14 14:13:00 -0800248 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700249 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800250 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700251 * @param identityName The default identity name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800252 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700253 virtual void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800254 setDefaultIdentityInternal(const Name& identityName) = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700255
Yingdi Yu31b4af22014-01-14 14:13:00 -0800256 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700257 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700258 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700259 * @param keyName The key name
260 * @throws SecPublicInfo::Error if the key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800261 */
262 virtual void
263 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
264
265 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700266 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800267 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700268 * @param certificateName The certificate name
269 * @throws SecPublicInfo::Error if the certificate does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800270 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700271 virtual void
272 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800273
274public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700275
Yingdi Yu31b4af22014-01-14 14:13:00 -0800276 /*****************************************
277 * Helper Methods *
278 *****************************************/
279
280 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700281 * @brief Set the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800282 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700283 * @param identityName The default identity name
284 * @throws SecPublicInfo::Error if the identity does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800285 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700286 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800287 setDefaultIdentity(const Name& identityName);
288
289 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700290 * @brief Set the default key name for the corresponding identity
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700291 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700292 * @param keyName The key name
293 * @throws SecPublicInfo::Error if either the identity or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800294 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700295 inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800296 setDefaultKeyNameForIdentity(const Name& keyName);
297
298 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700299 * @brief Set the default certificate name for the corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800300 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700301 * @param certificateName The certificate name
302 * @throws SecPublicInfo::Error if either the certificate or key does not exist
Yingdi Yu31b4af22014-01-14 14:13:00 -0800303 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700304 inline void
305 setDefaultCertificateNameForKey(const Name& certificateName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800306
307 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700308 * @brief Generate a key name for the identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800309 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700310 * @param identityName The identity name
311 * @param useKsk If true, generate a KSK name, otherwise a DSK name
312 * @return The generated key name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800313 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700314 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800315 getNewKeyName(const Name& identityName, bool useKsk);
316
Yingdi Yu2e57a582014-02-20 23:34:43 -0800317 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700318 * @brief Get the default certificate name for the specified identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800319 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700320 * @param identityName The identity name
321 * @return The default certificate name
322 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800323 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700324 inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800325 getDefaultCertificateNameForIdentity(const Name& identityName);
326
327 /**
Yingdi Yu2e57a582014-02-20 23:34:43 -0800328 * @brief Get the default certificate name of the default identity
329 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700330 * @return The requested certificate name
331 * @throws SecPublicInfo::Error if no certificate is found
Yingdi Yu31b4af22014-01-14 14:13:00 -0800332 */
333 inline Name
334 getDefaultCertificateName();
335
336 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700337 * @brief Add a certificate and set the certificate as the default one of its corresponding key
Yingdi Yu2e57a582014-02-20 23:34:43 -0800338 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700339 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800340 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800341 */
342 inline void
343 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
344
345 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700346 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700347 * default one of its corresponding identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800348 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700349 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800350 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
Yingdi Yu31b4af22014-01-14 14:13:00 -0800351 */
352 inline void
353 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
354
Yingdi Yu2e57a582014-02-20 23:34:43 -0800355 /**
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700356 * @brief Add a certificate into the public key identity storage and set the certificate as the
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700357 * default one of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800358 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700359 * @param certificate The certificate to be added
Yingdi Yu2e57a582014-02-20 23:34:43 -0800360 * @throws SecPublicInfo::Error if the certificate cannot be added (though it is really rare)
361 */
Yingdi Yu88663af2014-01-15 15:21:38 -0800362 inline void
363 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
364
Yingdi Yu2e57a582014-02-20 23:34:43 -0800365 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700366 * @brief Get cached default certificate of the default identity
Yingdi Yu2e57a582014-02-20 23:34:43 -0800367 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700368 * @return The certificate which might be empty shared_ptr<IdentityCertificate>()
Yingdi Yu2e57a582014-02-20 23:34:43 -0800369 */
370 inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800371 defaultCertificate();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700372
Yingdi Yu2e57a582014-02-20 23:34:43 -0800373 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700374 * @brief try to get the default certificate of the default identity from the public info
Yingdi Yu2e57a582014-02-20 23:34:43 -0800375 */
Yingdi Yu31b4af22014-01-14 14:13:00 -0800376 inline void
377 refreshDefaultCertificate();
378
379protected:
Yingdi Yu2e57a582014-02-20 23:34:43 -0800380 shared_ptr<IdentityCertificate> m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800381};
382
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800383inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800384SecPublicInfo::setDefaultIdentity(const Name& identityName)
385{
386 setDefaultIdentityInternal(identityName);
387 refreshDefaultCertificate();
388}
389
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800390inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800391SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
392{
393 setDefaultKeyNameForIdentityInternal(keyName);
394 refreshDefaultCertificate();
395}
396
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700397inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800398SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
399{
400 setDefaultCertificateNameForKeyInternal(certificateName);
401 refreshDefaultCertificate();
402}
403
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700404inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800405SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
406{
407 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
408}
409
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800410inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800411SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
412{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800413 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800414
Yingdi Yu31b4af22014-01-14 14:13:00 -0800415 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800416 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800417 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800418 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800419
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700420 oss << time::toUnixTimestamp(time::system_clock::now()).count();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700421
Yingdi Yu88663af2014-01-15 15:21:38 -0800422 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800423
424 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800425 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800426
427 return keyName;
428}
429
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800430inline Name
Yingdi Yu31b4af22014-01-14 14:13:00 -0800431SecPublicInfo::getDefaultCertificateName()
432{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700433 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu31b4af22014-01-14 14:13:00 -0800434 refreshDefaultCertificate();
435
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700436 if (!static_cast<bool>(m_defaultCertificate))
Yingdi Yu2e57a582014-02-20 23:34:43 -0800437 throw Error("No default certificate is set");
Yingdi Yu31b4af22014-01-14 14:13:00 -0800438
Yingdi Yu2e57a582014-02-20 23:34:43 -0800439 return m_defaultCertificate->getName();
Yingdi Yu31b4af22014-01-14 14:13:00 -0800440}
441
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800442inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800443SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
444{
445 addCertificate(certificate);
446 setDefaultCertificateNameForKeyInternal(certificate.getName());
447 refreshDefaultCertificate();
448}
449
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800450inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800451SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
452{
453 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800454 Name certName = certificate.getName();
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700455 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
456 setDefaultKeyNameForIdentityInternal(keyName);
Yingdi Yu88663af2014-01-15 15:21:38 -0800457 setDefaultCertificateNameForKeyInternal(certName);
458 refreshDefaultCertificate();
459}
460
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800461inline void
Yingdi Yu88663af2014-01-15 15:21:38 -0800462SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
463{
464 addCertificate(certificate);
465 Name certName = certificate.getName();
466 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
467 setDefaultIdentityInternal(keyName.getPrefix(-1));
468 setDefaultKeyNameForIdentityInternal(keyName);
469 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800470 refreshDefaultCertificate();
471}
472
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800473inline shared_ptr<IdentityCertificate>
Yingdi Yu31b4af22014-01-14 14:13:00 -0800474SecPublicInfo::defaultCertificate()
475{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800476 return m_defaultCertificate;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800477}
478
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800479inline void
Yingdi Yu31b4af22014-01-14 14:13:00 -0800480SecPublicInfo::refreshDefaultCertificate()
481{
Yingdi Yu2e57a582014-02-20 23:34:43 -0800482 try
483 {
484 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
485 m_defaultCertificate = getCertificate(certName);
486 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700487 catch (SecPublicInfo::Error& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800488 {
489 m_defaultCertificate.reset();
490 }
491
Yingdi Yu31b4af22014-01-14 14:13:00 -0800492}
493
Yingdi Yufc40d872014-02-18 12:56:04 -0800494} // namespace ndn
Yingdi Yu31b4af22014-01-14 14:13:00 -0800495
Yingdi Yufc40d872014-02-18 12:56:04 -0800496#endif //NDN_SECURITY_SEC_PUBLIC_INFO_HPP