blob: 37afd5d3608f9f19b15cbeb2b61dde6fef124e32 [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
9#ifndef NDN_SEC_PUBLIC_INFO_HPP
10#define NDN_SEC_PUBLIC_INFO_HPP
11
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/**
21 * SecPublicInfo is a base class for the storage of identity, public keys and certificates.
22 * Private keys are stored in SecTpm.
23 * This is an abstract base class. A subclass must implement the methods.
24 */
25class SecPublicInfo {
26public:
27 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
28
29 /**
30 * The virtual Destructor.
31 */
32 virtual
33 ~SecPublicInfo() {}
34
35 /**
36 * Check if the specified identity already exists.
37 * @param identityName The identity name.
38 * @return true if the identity exists, otherwise false.
39 */
40 virtual bool
41 doesIdentityExist(const Name& identityName) = 0;
42
43 /**
44 * Add a new identity. An exception will be thrown if the identity already exists.
45 * @param identityName The identity name to be added.
46 */
47 virtual void
48 addIdentity(const Name& identityName) = 0;
49
50 /**
51 * Revoke the identity.
52 * @return true if the identity was revoked, false if not.
53 */
54 virtual bool
55 revokeIdentity() = 0;
56
57 /**
58 * Check if the specified key already exists.
59 * @param keyName The name of the key.
60 * @return true if the key exists, otherwise false.
61 */
62 virtual bool
63 doesPublicKeyExist(const Name& keyName) = 0;
64
65 /**
66 * Add a public key to the identity storage.
67 * @param keyName The name of the public key to be added.
68 * @param keyType Type of the public key to be added.
69 * @param publicKeyDer A blob of the public key DER to be added.
70 */
71 virtual void
72 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
73
74 /**
75 * Get the public key DER blob from the identity storage.
76 * @param keyName The name of the requested public key.
77 * @return The DER Blob. If not found, return a Blob with a null pointer.
78 */
79 virtual ptr_lib::shared_ptr<PublicKey>
80 getPublicKey(const Name& keyName) = 0;
81
82 /**
83 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
84 * @param keyName name of the key
85 */
86 virtual void
87 activatePublicKey(const Name& keyName) = 0;
88
89 /**
90 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
91 * @param keyName name of the key
92 */
93 virtual void
94 deactivatePublicKey(const Name& keyName) = 0;
95
96 /**
97 * Check if the specified certificate already exists.
98 * @param certificateName The name of the certificate.
99 * @return true if the certificate exists, otherwise false.
100 */
101 virtual bool
102 doesCertificateExist(const Name& certificateName) = 0;
103
104 /**
105 * Add a certificate to the identity storage.
106 * @param certificate The certificate to be added. This makes a copy of the certificate.
107 */
108 virtual void
109 addCertificate(const IdentityCertificate& certificate) = 0;
110
111 /**
112 * Get a certificate from the identity storage.
113 * @param certificateName The name of the requested certificate.
114 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
115 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
116 */
117 virtual ptr_lib::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 /**
126 * Get the default identity.
127 * @param return The name of default identity, or an empty name if there is no default.
128 */
129 virtual Name
130 getDefaultIdentity() = 0;
131
132 /**
133 * Get the default key name for the specified identity.
134 * @param identityName The identity name.
135 * @return The default key name.
136 */
137 virtual Name
138 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
139
140 /**
141 * Get the default certificate name for the specified key.
142 * @param keyName The key name.
143 * @return The default certificate name.
144 */
145 virtual Name
146 getDefaultCertificateNameForKey(const Name& keyName) = 0;
147
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800148 virtual void
149 getAllIdentities(std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800150
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800151 virtual void
152 getAllKeyNames(std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800153
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800154 virtual void
155 getAllKeyNamesOfIdentity(const Name& identity, std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800156
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157 virtual void
158 getAllCertificateNames(std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800159
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800160 virtual void
161 getAllCertificateNamesOfKey(const Name& keyName, std::vector<Name> &nameList, bool isDefault) = 0;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800162
163protected:
164
165 /*****************************************
166 * Default Setter *
167 *****************************************/
168
169 /**
170 * Set the default identity. If the identityName does not exist, then clear the default identity
171 * so that getDefaultIdentity() returns an empty name.
172 * @param identityName The default identity name.
173 */
174 virtual void
175 setDefaultIdentityInternal(const Name& identityName) = 0;
176
177 /**
178 * Set the default key name for the corresponding identity.
179 * @param keyName The key name.
180 */
181 virtual void
182 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
183
184 /**
185 * Set the default certificate name for the corresponding key.
186 * @param certificateName The certificate name.
187 */
188 virtual void
189 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
190
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800191 /*****************************************
192 * Delete Methods *
193 *****************************************/
194
195 /**
196 * Delete a certificate.
197 * @param certificateName The certificate name.
198 */
199 virtual void
200 deleteCertificateInfo(const Name &certificateName) = 0;
201
202 /**
203 * Delete a public key and related certificates.
204 * @param keyName The key name.
205 */
206 virtual void
207 deletePublicKeyInfo(const Name &keyName) = 0;
208
209 /**
210 * Delete an identity and related public keys and certificates.
211 * @param identity The identity name.
212 */
213 virtual void
214 deleteIdentityInfo(const Name &identity) = 0;
215
Yingdi Yu31b4af22014-01-14 14:13:00 -0800216public:
217
218 /*****************************************
219 * Helper Methods *
220 *****************************************/
221
222 /**
223 * Set the default identity. If the identityName does not exist, then clear the default identity
224 * so that getDefaultIdentity() returns an empty name.
225 * @param identityName The default identity name.
226 */
227 inline void
228 setDefaultIdentity(const Name& identityName);
229
230 /**
231 * Set the default key name for the corresponding identity.
232 * @param keyName The key name.
233 */
234 inline void
235 setDefaultKeyNameForIdentity(const Name& keyName);
236
237 /**
238 * Set the default certificate name for the corresponding key.
239 * @param certificateName The certificate name.
240 */
241 inline void
242 setDefaultCertificateNameForKey(const Name& certificateName);
243
244 /**
245 * Generate a name for a new key belonging to the identity.
246 * @param identityName The identity name.
247 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
248 * @return The generated key name.
249 */
250 inline Name
251 getNewKeyName(const Name& identityName, bool useKsk);
252
253 /**
254 * Get the default certificate name for the specified identity.
255 * @param identityName The identity name.
256 * @return The default certificate name.
257 */
258 inline Name
259 getDefaultCertificateNameForIdentity(const Name& identityName);
260
261 /**
262 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
263 * the identity is not specified.
264 * @return The requested certificate name.
265 */
266 inline Name
267 getDefaultCertificateName();
268
269 /**
270 * Add a certificate and set the certificate as the default of its corresponding key.
271 * @param certificate The certificate to be added. This makes a copy of the certificate.
272 */
273 inline void
274 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
275
276 /**
277 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
278 * @param certificate The certificate to be added. This makes a copy of the certificate.
279 */
280 inline void
281 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
282
Yingdi Yu88663af2014-01-15 15:21:38 -0800283 inline void
284 addCertificateAsSystemDefault(const IdentityCertificate& certificate);
285
Yingdi Yu31b4af22014-01-14 14:13:00 -0800286 inline ptr_lib::shared_ptr<IdentityCertificate>
287 defaultCertificate();
288
289 inline void
290 refreshDefaultCertificate();
291
292protected:
293 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
294
295};
296
297void
298SecPublicInfo::setDefaultIdentity(const Name& identityName)
299{
300 setDefaultIdentityInternal(identityName);
301 refreshDefaultCertificate();
302}
303
304void
305SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
306{
307 setDefaultKeyNameForIdentityInternal(keyName);
308 refreshDefaultCertificate();
309}
310
311void
312SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
313{
314 setDefaultCertificateNameForKeyInternal(certificateName);
315 refreshDefaultCertificate();
316}
317
318Name
319SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
320{
321 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
322}
323
324Name
325SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
326{
Yingdi Yu31b4af22014-01-14 14:13:00 -0800327 std::ostringstream oss;
Yingdi Yu31b4af22014-01-14 14:13:00 -0800328
Yingdi Yu31b4af22014-01-14 14:13:00 -0800329 if (useKsk)
Yingdi Yu88663af2014-01-15 15:21:38 -0800330 oss << "ksk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800331 else
Yingdi Yu88663af2014-01-15 15:21:38 -0800332 oss << "dsk-";
Yingdi Yu31b4af22014-01-14 14:13:00 -0800333
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800334 oss << static_cast<int>(getNow());
Yingdi Yu88663af2014-01-15 15:21:38 -0800335
336 Name keyName = Name(identityName).append(oss.str());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800337
338 if (doesPublicKeyExist(keyName))
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800339 throw Error("Key name already exists: " + keyName.toUri());
Yingdi Yu31b4af22014-01-14 14:13:00 -0800340
341 return keyName;
342}
343
344Name
345SecPublicInfo::getDefaultCertificateName()
346{
347 if(!static_cast<bool>(defaultCertificate_))
348 refreshDefaultCertificate();
349
350 if(!static_cast<bool>(defaultCertificate_))
351 return Name();
352
353 return defaultCertificate_->getName();
354}
355
356void
357SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
358{
359 addCertificate(certificate);
360 setDefaultCertificateNameForKeyInternal(certificate.getName());
361 refreshDefaultCertificate();
362}
363
364void
365SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
366{
367 addCertificate(certificate);
Yingdi Yu88663af2014-01-15 15:21:38 -0800368 Name certName = certificate.getName();
369 setDefaultKeyNameForIdentityInternal(IdentityCertificate::certificateNameToPublicKeyName(certName));
370 setDefaultCertificateNameForKeyInternal(certName);
371 refreshDefaultCertificate();
372}
373
374void
375SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
376{
377 addCertificate(certificate);
378 Name certName = certificate.getName();
379 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
380 setDefaultIdentityInternal(keyName.getPrefix(-1));
381 setDefaultKeyNameForIdentityInternal(keyName);
382 setDefaultCertificateNameForKeyInternal(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800383 refreshDefaultCertificate();
384}
385
386ptr_lib::shared_ptr<IdentityCertificate>
387SecPublicInfo::defaultCertificate()
388{
389 return defaultCertificate_;
390}
391
392void
393SecPublicInfo::refreshDefaultCertificate()
394{
Yingdi Yu88663af2014-01-15 15:21:38 -0800395 Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity());
396 if(certName.empty())
397 defaultCertificate_.reset();
398 else
399 defaultCertificate_ = getCertificate(certName);
Yingdi Yu31b4af22014-01-14 14:13:00 -0800400}
401
Yingdi Yu31b4af22014-01-14 14:13:00 -0800402}
403
404#endif