blob: 3a46310849ee4f8aecb635b1450c2105b339ac06 [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
12#include "../../name.hpp"
13#include "../security-common.hpp"
14#include "../certificate/public-key.hpp"
15#include "../certificate/identity-certificate.hpp"
16
17namespace ndn {
18
19/**
20 * SecPublicInfo is a base class for the storage of identity, public keys and certificates.
21 * Private keys are stored in SecTpm.
22 * This is an abstract base class. A subclass must implement the methods.
23 */
24class SecPublicInfo {
25public:
26 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
27
28 /**
29 * The virtual Destructor.
30 */
31 virtual
32 ~SecPublicInfo() {}
33
34 /**
35 * Check if the specified identity already exists.
36 * @param identityName The identity name.
37 * @return true if the identity exists, otherwise false.
38 */
39 virtual bool
40 doesIdentityExist(const Name& identityName) = 0;
41
42 /**
43 * Add a new identity. An exception will be thrown if the identity already exists.
44 * @param identityName The identity name to be added.
45 */
46 virtual void
47 addIdentity(const Name& identityName) = 0;
48
49 /**
50 * Revoke the identity.
51 * @return true if the identity was revoked, false if not.
52 */
53 virtual bool
54 revokeIdentity() = 0;
55
56 /**
57 * Check if the specified key already exists.
58 * @param keyName The name of the key.
59 * @return true if the key exists, otherwise false.
60 */
61 virtual bool
62 doesPublicKeyExist(const Name& keyName) = 0;
63
64 /**
65 * Add a public key to the identity storage.
66 * @param keyName The name of the public key to be added.
67 * @param keyType Type of the public key to be added.
68 * @param publicKeyDer A blob of the public key DER to be added.
69 */
70 virtual void
71 addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKeyDer) = 0;
72
73 /**
74 * Get the public key DER blob from the identity storage.
75 * @param keyName The name of the requested public key.
76 * @return The DER Blob. If not found, return a Blob with a null pointer.
77 */
78 virtual ptr_lib::shared_ptr<PublicKey>
79 getPublicKey(const Name& keyName) = 0;
80
81 /**
82 * Activate a key. If a key is marked as inactive, its private part will not be used in packet signing.
83 * @param keyName name of the key
84 */
85 virtual void
86 activatePublicKey(const Name& keyName) = 0;
87
88 /**
89 * Deactivate a key. If a key is marked as inactive, its private part will not be used in packet signing.
90 * @param keyName name of the key
91 */
92 virtual void
93 deactivatePublicKey(const Name& keyName) = 0;
94
95 /**
96 * Check if the specified certificate already exists.
97 * @param certificateName The name of the certificate.
98 * @return true if the certificate exists, otherwise false.
99 */
100 virtual bool
101 doesCertificateExist(const Name& certificateName) = 0;
102
103 /**
104 * Add a certificate to the identity storage.
105 * @param certificate The certificate to be added. This makes a copy of the certificate.
106 */
107 virtual void
108 addCertificate(const IdentityCertificate& certificate) = 0;
109
110 /**
111 * Get a certificate from the identity storage.
112 * @param certificateName The name of the requested certificate.
113 * @param allowAny If false, only a valid certificate will be returned, otherwise validity is disregarded.
114 * @return The requested certificate. If not found, return a shared_ptr with a null pointer.
115 */
116 virtual ptr_lib::shared_ptr<IdentityCertificate>
117 getCertificate(const Name &certificateName, bool allowAny = false) = 0;
118
119
120 /*****************************************
121 * Default Getter *
122 *****************************************/
123
124 /**
125 * Get the default identity.
126 * @param return The name of default identity, or an empty name if there is no default.
127 */
128 virtual Name
129 getDefaultIdentity() = 0;
130
131 /**
132 * Get the default key name for the specified identity.
133 * @param identityName The identity name.
134 * @return The default key name.
135 */
136 virtual Name
137 getDefaultKeyNameForIdentity(const Name& identityName) = 0;
138
139 /**
140 * Get the default certificate name for the specified key.
141 * @param keyName The key name.
142 * @return The default certificate name.
143 */
144 virtual Name
145 getDefaultCertificateNameForKey(const Name& keyName) = 0;
146
147 virtual std::vector<Name>
148 getAllIdentities(bool isDefault) = 0;
149
150 virtual std::vector<Name>
151 getAllKeyNames(bool isDefault) = 0;
152
153 virtual std::vector<Name>
154 getAllKeyNamesOfIdentity(const Name& identity, bool isDefault) = 0;
155
156 virtual std::vector<Name>
157 getAllCertificateNames(bool isDefault) = 0;
158
159 virtual std::vector<Name>
160 getAllCertificateNamesOfKey(const Name& keyName, bool isDefault) = 0;
161
162protected:
163
164 /*****************************************
165 * Default Setter *
166 *****************************************/
167
168 /**
169 * Set the default identity. If the identityName does not exist, then clear the default identity
170 * so that getDefaultIdentity() returns an empty name.
171 * @param identityName The default identity name.
172 */
173 virtual void
174 setDefaultIdentityInternal(const Name& identityName) = 0;
175
176 /**
177 * Set the default key name for the corresponding identity.
178 * @param keyName The key name.
179 */
180 virtual void
181 setDefaultKeyNameForIdentityInternal(const Name& keyName) = 0;
182
183 /**
184 * Set the default certificate name for the corresponding key.
185 * @param certificateName The certificate name.
186 */
187 virtual void
188 setDefaultCertificateNameForKeyInternal(const Name& certificateName) = 0;
189
190public:
191
192 /*****************************************
193 * Helper Methods *
194 *****************************************/
195
196 /**
197 * Set the default identity. If the identityName does not exist, then clear the default identity
198 * so that getDefaultIdentity() returns an empty name.
199 * @param identityName The default identity name.
200 */
201 inline void
202 setDefaultIdentity(const Name& identityName);
203
204 /**
205 * Set the default key name for the corresponding identity.
206 * @param keyName The key name.
207 */
208 inline void
209 setDefaultKeyNameForIdentity(const Name& keyName);
210
211 /**
212 * Set the default certificate name for the corresponding key.
213 * @param certificateName The certificate name.
214 */
215 inline void
216 setDefaultCertificateNameForKey(const Name& certificateName);
217
218 /**
219 * Generate a name for a new key belonging to the identity.
220 * @param identityName The identity name.
221 * @param useKsk If true, generate a KSK name, otherwise a DSK name.
222 * @return The generated key name.
223 */
224 inline Name
225 getNewKeyName(const Name& identityName, bool useKsk);
226
227 /**
228 * Get the default certificate name for the specified identity.
229 * @param identityName The identity name.
230 * @return The default certificate name.
231 */
232 inline Name
233 getDefaultCertificateNameForIdentity(const Name& identityName);
234
235 /**
236 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
237 * the identity is not specified.
238 * @return The requested certificate name.
239 */
240 inline Name
241 getDefaultCertificateName();
242
243 /**
244 * Add a certificate and set the certificate as the default of its corresponding key.
245 * @param certificate The certificate to be added. This makes a copy of the certificate.
246 */
247 inline void
248 addCertificateAsKeyDefault(const IdentityCertificate& certificate);
249
250 /**
251 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
252 * @param certificate The certificate to be added. This makes a copy of the certificate.
253 */
254 inline void
255 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
256
257 inline ptr_lib::shared_ptr<IdentityCertificate>
258 defaultCertificate();
259
260 inline void
261 refreshDefaultCertificate();
262
263protected:
264 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
265
266};
267
268void
269SecPublicInfo::setDefaultIdentity(const Name& identityName)
270{
271 setDefaultIdentityInternal(identityName);
272 refreshDefaultCertificate();
273}
274
275void
276SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
277{
278 setDefaultKeyNameForIdentityInternal(keyName);
279 refreshDefaultCertificate();
280}
281
282void
283SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
284{
285 setDefaultCertificateNameForKeyInternal(certificateName);
286 refreshDefaultCertificate();
287}
288
289Name
290SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
291{
292 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
293}
294
295Name
296SecPublicInfo::getNewKeyName (const Name& identityName, bool useKsk)
297{
298 MillisecondsSince1970 ti = getNow();
299 // Get the number of seconds.
300 std::ostringstream oss;
301 oss << floor(ti / 1000.0);
302
303 std::string keyIdStr;
304
305 if (useKsk)
306 keyIdStr = ("KSK-" + oss.str());
307 else
308 keyIdStr = ("DSK-" + oss.str());
309
310 Name keyName = Name(identityName).append(keyIdStr);
311
312 if (doesPublicKeyExist(keyName))
313 throw Error("Key name already exists");
314
315 return keyName;
316}
317
318Name
319SecPublicInfo::getDefaultCertificateName()
320{
321 if(!static_cast<bool>(defaultCertificate_))
322 refreshDefaultCertificate();
323
324 if(!static_cast<bool>(defaultCertificate_))
325 return Name();
326
327 return defaultCertificate_->getName();
328}
329
330void
331SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
332{
333 addCertificate(certificate);
334 setDefaultCertificateNameForKeyInternal(certificate.getName());
335 refreshDefaultCertificate();
336}
337
338void
339SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
340{
341 addCertificate(certificate);
342 setDefaultKeyNameForIdentityInternal(certificate.getPublicKeyName());
343 setDefaultCertificateNameForKeyInternal(certificate.getName());
344 refreshDefaultCertificate();
345}
346
347ptr_lib::shared_ptr<IdentityCertificate>
348SecPublicInfo::defaultCertificate()
349{
350 return defaultCertificate_;
351}
352
353void
354SecPublicInfo::refreshDefaultCertificate()
355{
356 defaultCertificate_ = getCertificate(getDefaultCertificateNameForIdentity(getDefaultIdentity()));
357}
358
359
360}
361
362#endif