blob: e1c11dfc7c5fae4926362ff4db547fb4ba99fbec [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"
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -080017#include "../certificate/identity-certificate.hpp"
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080018
Jeff Thompson958bf9b2013-10-12 17:20:51 -070019namespace ndn {
20
Jeff Thompsonffa36f92013-09-20 08:42:41 -070021/**
22 * An IdentityManager is the interface of operations related to identity, keys, and certificates.
23 */
Jeff Thompson41471912013-09-12 16:21:50 -070024class IdentityManager {
25public:
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080026 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
27
Alexander Afanasyevbd5ba402014-01-05 22:41:09 -080028 IdentityManager(const ptr_lib::shared_ptr<IdentityStorage> &identityStorage,
29 const ptr_lib::shared_ptr<PrivateKeyStorage> &privateKeyStorage);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080030
31 inline IdentityStorage&
32 info();
33
34 inline const IdentityStorage&
35 info() const;
36
37 inline PrivateKeyStorage&
38 tpm();
39
40 inline const PrivateKeyStorage&
41 tpm() const;
Jeff Thompson86e1d752013-09-17 17:22:38 -070042
Jeff Thompson9296f0c2013-09-23 18:10:27 -070043 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070044 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
45 * @param identityName The name of the identity.
46 * @return The key name of the auto-generated KSK of the identity.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070047 */
48 Name
49 createIdentity(const Name& identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070050
51 /**
52 * Get the default identity.
53 * @return The default identity name.
54 */
55 Name
56 getDefaultIdentity()
57 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080058 return info().getDefaultIdentity();
Jeff Thompson9296f0c2013-09-23 18:10:27 -070059 }
60
Jeff Thompson9296f0c2013-09-23 18:10:27 -070061 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070062 * Generate a pair of RSA keys for the specified identity.
63 * @param identityName The name of the identity.
64 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
65 * @param keySize The size of the key.
66 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070067 */
68 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070069 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070070
71 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070072 * Set a key as the default key of an identity.
73 * @param keyName The name of the key.
74 * @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 -070075 */
76 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070077 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
78 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080079 info().setDefaultKeyNameForIdentity(keyName, identityName);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080080 defaultCertificate_.reset();
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 }
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800221
222 void
223 sign(Data &data);
224
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700225 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700226 * Sign the byte array data based on the certificate name.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700227 * @param buffer The byte array to be signed.
228 * @param bufferLength the length of buffer.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700229 * @param certificateName The signing certificate name.
230 * @return The generated signature.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700231 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800232 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700233 signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
234
235 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700236 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700237 * Note: the caller must make sure the timestamp in data is correct, for example with
238 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
239 * @param data The Data object to sign and update its signature.
240 * @param certificateName The Name identifying the certificate which identifies the signing key.
241 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
242 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700243 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800244 signByCertificate(Data& data, const Name& certificateName);
Jeff Thompson418b05a2013-10-22 17:48:54 -0700245
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800246 void
247 signByCertificate(Data& data, const IdentityCertificate& certificate);
248
Jeff Thompson418b05a2013-10-22 17:48:54 -0700249 /**
250 * Generate a self-signed certificate for a public key.
251 * @param keyName The name of the public key.
252 * @return The generated certificate.
253 */
254 ptr_lib::shared_ptr<IdentityCertificate>
255 selfSign(const Name& keyName);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800256
257 /**
258 * @brief Self-sign the supplied identity certificate
259 */
260 void
261 selfSign (IdentityCertificate& cert);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800262
Jeff Thompson86e1d752013-09-17 17:22:38 -0700263private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700264 /**
265 * Generate a key pair for the specified identity.
266 * @param identityName The name of the specified identity.
267 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
268 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
269 * @param keySize The size of the key pair.
270 * @return The name of the generated key.
271 */
272 Name
273 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
274
Jeff Thompson418b05a2013-10-22 17:48:54 -0700275 static Name
276 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800277
278private:
279 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700280 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800281
282 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
Jeff Thompson41471912013-09-12 16:21:50 -0700283};
284
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800285inline IdentityStorage&
286IdentityManager::info()
287{
288 if (!identityStorage_)
289 throw Error("IdentityStorage is not assigned to IdentityManager");
290
291 return *identityStorage_;
292}
293
294inline const IdentityStorage&
295IdentityManager::info() const
296{
297 if (!identityStorage_)
298 throw Error("IdentityStorage is not assigned to IdentityManager");
299
300 return *identityStorage_;
301}
302
303inline PrivateKeyStorage&
304IdentityManager::tpm()
305{
306 if (!identityStorage_)
307 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
308
309 return *privateKeyStorage_;
310}
311
312inline const PrivateKeyStorage&
313IdentityManager::tpm() const
314{
315 if (!identityStorage_)
316 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
317 return *privateKeyStorage_;
318}
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800319
Jeff Thompson41471912013-09-12 16:21:50 -0700320}
321
322#endif