blob: 702ad1fcdc31f15a4270db34f9ddafeab586d2fb [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson41471912013-09-12 16:21:50 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson06e787d2013-09-12 19:00:55 -07004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson41471912013-09-12 16:21:50 -07006 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_IDENTITY_MANAGER_HPP
Jeff Thompsonc69163b2013-10-12 13:49:50 -070010#define NDN_IDENTITY_MANAGER_HPP
Jeff Thompson41471912013-09-12 16:21:50 -070011
Jeff Thompson9296f0c2013-09-23 18:10:27 -070012#include "identity-storage.hpp"
Jeff Thompson86e1d752013-09-17 17:22:38 -070013#include "private-key-storage.hpp"
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080014#include "../certificate/public-key.hpp"
Jeff Thompson41471912013-09-12 16:21:50 -070015
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080016#include "../../data.hpp"
17
Jeff Thompson958bf9b2013-10-12 17:20:51 -070018namespace ndn {
19
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080020class IdentityCertificate;
21
Jeff Thompsonffa36f92013-09-20 08:42:41 -070022/**
23 * An IdentityManager is the interface of operations related to identity, keys, and certificates.
24 */
Jeff Thompson41471912013-09-12 16:21:50 -070025class IdentityManager {
26public:
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080027 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
28
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080029 IdentityManager(const ptr_lib::shared_ptr<IdentityStorage> &identityStorage = DefaultIdentityStorage,
30 const ptr_lib::shared_ptr<PrivateKeyStorage> &privateKeyStorage = DefaultPrivateKeyStorage);
31
32 inline IdentityStorage&
33 info();
34
35 inline const IdentityStorage&
36 info() const;
37
38 inline PrivateKeyStorage&
39 tpm();
40
41 inline const PrivateKeyStorage&
42 tpm() const;
Jeff Thompson86e1d752013-09-17 17:22:38 -070043
Jeff Thompson9296f0c2013-09-23 18:10:27 -070044 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070045 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
46 * @param identityName The name of the identity.
47 * @return The key name of the auto-generated KSK of the identity.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070048 */
49 Name
50 createIdentity(const Name& identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070051
52 /**
53 * Get the default identity.
54 * @return The default identity name.
55 */
56 Name
57 getDefaultIdentity()
58 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080059 return info().getDefaultIdentity();
Jeff Thompson9296f0c2013-09-23 18:10:27 -070060 }
61
Jeff Thompson9296f0c2013-09-23 18:10:27 -070062 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070063 * Generate a pair of RSA keys for the specified identity.
64 * @param identityName The name of the identity.
65 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
66 * @param keySize The size of the key.
67 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070068 */
69 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070070 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070071
72 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070073 * Set a key as the default key of an identity.
74 * @param keyName The name of the key.
75 * @param identityName the name of the identity. If not specified, the identity name is inferred from the keyName.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070076 */
77 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070078 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
79 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080080 info().setDefaultKeyNameForIdentity(keyName, identityName);
Jeff Thompsone7e069b2013-09-27 15:48:48 -070081 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070082
83 /**
Jeff Thompson18bf6312013-10-04 11:23:55 -070084 * Get the default key for an identity.
85 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
86 * @return The default key name.
87 */
88 Name
89 getDefaultKeyNameForIdentity(const Name& identityName = Name())
90 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080091 return info().getDefaultKeyNameForIdentity(identityName);
Jeff Thompson18bf6312013-10-04 11:23:55 -070092 }
93
94 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070095 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
96 * @param identityName The name of the identity.
97 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
98 * @param keySize The size of the key.
99 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700100 */
101 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700102 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700103
104 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700105 * Get the public key with the specified name.
106 * @param keyName The name of the key.
107 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700108 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800109 ptr_lib::shared_ptr<PublicKey>
110 getPublicKey(const Name& keyName)
111 {
112 return info().getKey(keyName);
113 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700114
115 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700116 * Create an identity certificate for a public key managed by this IdentityManager.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700117 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700118 * @param signerCertificateName The name of signing certificate.
119 * @param notBefore The notBefore value in the validity field of the generated certificate.
120 * @param notAfter The notAfter vallue in validity field of the generated certificate.
121 * @return The name of generated identity certificate.
122 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800123 ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson418b05a2013-10-22 17:48:54 -0700124 createIdentityCertificate
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800125 (const Name& certificatePrefix,
126 const Name& signerCertificateName,
127 const MillisecondsSince1970& notBefore,
Jeff Thompson418b05a2013-10-22 17:48:54 -0700128 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700129
130 /**
131 * Create an identity certificate for a public key supplied by the caller.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700132 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700133 * @param publickey The public key to be signed.
134 * @param signerCertificateName The name of signing certificate.
135 * @param notBefore The notBefore value in the validity field of the generated certificate.
136 * @param notAfter The notAfter vallue in validity field of the generated certificate.
137 * @return The generated identity certificate.
138 */
139 ptr_lib::shared_ptr<IdentityCertificate>
140 createIdentityCertificate
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800141 (const Name& certificatePrefix,
142 const PublicKey& publickey,
143 const Name& signerCertificateName,
144 const MillisecondsSince1970& notBefore,
145 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700146
147 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700148 * Add a certificate into the public key identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700149 * @param certificate The certificate to to added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700150 */
151 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700152 addCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700153 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800154 info().addCertificate(certificate);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700155 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700156
157 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700158 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700159 * @param certificateName The certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700160 */
161 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700162 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700163
164 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700165 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700166 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700167 */
168 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700169 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700170
171 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700172 * Add a certificate into the public key identity storage and set the certificate as the default of its corresponding key.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700173 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700174 */
175 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700176 addCertificateAsDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700177
178 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700179 * Get a certificate with the specified name.
180 * @param certificateName The name of the requested certificate.
181 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700182 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800183 ptr_lib::shared_ptr<IdentityCertificate>
184 getCertificate(const Name& certificateName)
185 {
186 return info().getCertificate(certificateName, false);
187 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700188
189 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700190 * Get a certificate even if the certificate is not valid anymore.
191 * @param certificateName The name of the requested certificate.
192 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700193 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800194 ptr_lib::shared_ptr<IdentityCertificate>
195 getAnyCertificate(const Name& certificateName)
196 {
197 return info().getCertificate(certificateName, true);
198 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700199
200 /**
201 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
202 * @param identityName The name of the specified identity.
203 * @return The requested certificate name.
204 */
205 Name
206 getDefaultCertificateNameForIdentity(const Name& identityName)
207 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800208 return info().getDefaultCertificateNameForIdentity(identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700209 }
210
211 /**
212 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
213 * the identity is not specified.
214 * @return The requested certificate name.
215 */
216 Name
217 getDefaultCertificateName()
218 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800219 return info().getDefaultCertificateNameForIdentity(getDefaultIdentity());
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700220 }
221
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700222 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700223 * Sign the byte array data based on the certificate name.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700224 * @param buffer The byte array to be signed.
225 * @param bufferLength the length of buffer.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700226 * @param certificateName The signing certificate name.
227 * @return The generated signature.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700228 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800229 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700230 signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
231
232 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700233 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700234 * Note: the caller must make sure the timestamp in data is correct, for example with
235 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
236 * @param data The Data object to sign and update its signature.
237 * @param certificateName The Name identifying the certificate which identifies the signing key.
238 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
239 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700240 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800241 signByCertificate(Data& data, const Name& certificateName);
Jeff Thompson418b05a2013-10-22 17:48:54 -0700242
243 /**
244 * Generate a self-signed certificate for a public key.
245 * @param keyName The name of the public key.
246 * @return The generated certificate.
247 */
248 ptr_lib::shared_ptr<IdentityCertificate>
249 selfSign(const Name& keyName);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800250
251 /**
252 * @brief Self-sign the supplied identity certificate
253 */
254 void
255 selfSign (IdentityCertificate& cert);
256
257public:
258 static const ptr_lib::shared_ptr<IdentityStorage> DefaultIdentityStorage;
259 static const ptr_lib::shared_ptr<PrivateKeyStorage> DefaultPrivateKeyStorage;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700260
261private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700262 /**
263 * Generate a key pair for the specified identity.
264 * @param identityName The name of the specified identity.
265 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
266 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
267 * @param keySize The size of the key pair.
268 * @return The name of the generated key.
269 */
270 Name
271 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
272
Jeff Thompson418b05a2013-10-22 17:48:54 -0700273 static Name
274 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800275
276private:
277 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700278 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700279};
280
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800281inline IdentityStorage&
282IdentityManager::info()
283{
284 if (!identityStorage_)
285 throw Error("IdentityStorage is not assigned to IdentityManager");
286
287 return *identityStorage_;
288}
289
290inline const IdentityStorage&
291IdentityManager::info() const
292{
293 if (!identityStorage_)
294 throw Error("IdentityStorage is not assigned to IdentityManager");
295
296 return *identityStorage_;
297}
298
299inline PrivateKeyStorage&
300IdentityManager::tpm()
301{
302 if (!identityStorage_)
303 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
304
305 return *privateKeyStorage_;
306}
307
308inline const PrivateKeyStorage&
309IdentityManager::tpm() const
310{
311 if (!identityStorage_)
312 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
313 return *privateKeyStorage_;
314}
315
316
317
Jeff Thompson41471912013-09-12 16:21:50 -0700318}
319
320#endif