blob: 5ed77712c5022d14c753151dfa3f096336cd314a [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);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080081 defaultCertificate_.reset();
Jeff Thompsone7e069b2013-09-27 15:48:48 -070082 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070083
84 /**
Jeff Thompson18bf6312013-10-04 11:23:55 -070085 * Get the default key for an identity.
86 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
87 * @return The default key name.
88 */
89 Name
90 getDefaultKeyNameForIdentity(const Name& identityName = Name())
91 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080092 return info().getDefaultKeyNameForIdentity(identityName);
Jeff Thompson18bf6312013-10-04 11:23:55 -070093 }
94
95 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070096 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
97 * @param identityName The name of the identity.
98 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
99 * @param keySize The size of the key.
100 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700101 */
102 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700103 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700104
105 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700106 * Get the public key with the specified name.
107 * @param keyName The name of the key.
108 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700109 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800110 ptr_lib::shared_ptr<PublicKey>
111 getPublicKey(const Name& keyName)
112 {
113 return info().getKey(keyName);
114 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700115
116 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700117 * Create an identity certificate for a public key managed by this IdentityManager.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700118 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700119 * @param signerCertificateName The name of signing certificate.
120 * @param notBefore The notBefore value in the validity field of the generated certificate.
121 * @param notAfter The notAfter vallue in validity field of the generated certificate.
122 * @return The name of generated identity certificate.
123 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800124 ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompson418b05a2013-10-22 17:48:54 -0700125 createIdentityCertificate
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800126 (const Name& certificatePrefix,
127 const Name& signerCertificateName,
128 const MillisecondsSince1970& notBefore,
Jeff Thompson418b05a2013-10-22 17:48:54 -0700129 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700130
131 /**
132 * Create an identity certificate for a public key supplied by the caller.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700133 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700134 * @param publickey The public key to be signed.
135 * @param signerCertificateName The name of signing certificate.
136 * @param notBefore The notBefore value in the validity field of the generated certificate.
137 * @param notAfter The notAfter vallue in validity field of the generated certificate.
138 * @return The generated identity certificate.
139 */
140 ptr_lib::shared_ptr<IdentityCertificate>
141 createIdentityCertificate
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800142 (const Name& certificatePrefix,
143 const PublicKey& publickey,
144 const Name& signerCertificateName,
145 const MillisecondsSince1970& notBefore,
146 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700147
148 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700149 * Add a certificate into the public key identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700150 * @param certificate The certificate to to added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700151 */
152 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700153 addCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700154 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800155 info().addCertificate(certificate);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700156 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700157
158 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700159 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700160 * @param certificateName The certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700161 */
162 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700163 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700164
165 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700166 * 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 -0700167 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700168 */
169 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700170 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700171
172 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700173 * 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 -0700174 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700175 */
176 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700177 addCertificateAsDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700178
179 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700180 * Get a certificate with the specified name.
181 * @param certificateName The name of the requested certificate.
182 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700183 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800184 ptr_lib::shared_ptr<IdentityCertificate>
185 getCertificate(const Name& certificateName)
186 {
187 return info().getCertificate(certificateName, false);
188 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700189
190 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700191 * Get a certificate even if the certificate is not valid anymore.
192 * @param certificateName The name of the requested certificate.
193 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700194 */
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800195 ptr_lib::shared_ptr<IdentityCertificate>
196 getAnyCertificate(const Name& certificateName)
197 {
198 return info().getCertificate(certificateName, true);
199 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700200
201 /**
202 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
203 * @param identityName The name of the specified identity.
204 * @return The requested certificate name.
205 */
206 Name
207 getDefaultCertificateNameForIdentity(const Name& identityName)
208 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800209 return info().getDefaultCertificateNameForIdentity(identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700210 }
211
212 /**
213 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
214 * the identity is not specified.
215 * @return The requested certificate name.
216 */
217 Name
218 getDefaultCertificateName()
219 {
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800220 return info().getDefaultCertificateNameForIdentity(getDefaultIdentity());
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700221 }
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800222
223 void
224 sign(Data &data);
225
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700226 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700227 * Sign the byte array data based on the certificate name.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700228 * @param buffer The byte array to be signed.
229 * @param bufferLength the length of buffer.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700230 * @param certificateName The signing certificate name.
231 * @return The generated signature.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700232 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800233 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700234 signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
235
236 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700237 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700238 * Note: the caller must make sure the timestamp in data is correct, for example with
239 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
240 * @param data The Data object to sign and update its signature.
241 * @param certificateName The Name identifying the certificate which identifies the signing key.
242 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
243 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700244 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800245 signByCertificate(Data& data, const Name& certificateName);
Jeff Thompson418b05a2013-10-22 17:48:54 -0700246
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800247 void
248 signByCertificate(Data& data, const IdentityCertificate& certificate);
249
Jeff Thompson418b05a2013-10-22 17:48:54 -0700250 /**
251 * Generate a self-signed certificate for a public key.
252 * @param keyName The name of the public key.
253 * @return The generated certificate.
254 */
255 ptr_lib::shared_ptr<IdentityCertificate>
256 selfSign(const Name& keyName);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800257
258 /**
259 * @brief Self-sign the supplied identity certificate
260 */
261 void
262 selfSign (IdentityCertificate& cert);
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800263
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800264public:
265 static const ptr_lib::shared_ptr<IdentityStorage> DefaultIdentityStorage;
266 static const ptr_lib::shared_ptr<PrivateKeyStorage> DefaultPrivateKeyStorage;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800267
Jeff Thompson86e1d752013-09-17 17:22:38 -0700268private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700269 /**
270 * Generate a key pair for the specified identity.
271 * @param identityName The name of the specified identity.
272 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
273 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
274 * @param keySize The size of the key pair.
275 * @return The name of the generated key.
276 */
277 Name
278 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
279
Jeff Thompson418b05a2013-10-22 17:48:54 -0700280 static Name
281 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800282
283private:
284 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700285 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Alexander Afanasyeve64788e2014-01-05 22:38:21 -0800286
287 ptr_lib::shared_ptr<IdentityCertificate> defaultCertificate_;
Jeff Thompson41471912013-09-12 16:21:50 -0700288};
289
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800290inline IdentityStorage&
291IdentityManager::info()
292{
293 if (!identityStorage_)
294 throw Error("IdentityStorage is not assigned to IdentityManager");
295
296 return *identityStorage_;
297}
298
299inline const IdentityStorage&
300IdentityManager::info() const
301{
302 if (!identityStorage_)
303 throw Error("IdentityStorage is not assigned to IdentityManager");
304
305 return *identityStorage_;
306}
307
308inline PrivateKeyStorage&
309IdentityManager::tpm()
310{
311 if (!identityStorage_)
312 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
313
314 return *privateKeyStorage_;
315}
316
317inline const PrivateKeyStorage&
318IdentityManager::tpm() const
319{
320 if (!identityStorage_)
321 throw Error("PrivateKeyStorage is not assigned to IdentityManager");
322 return *privateKeyStorage_;
323}
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800324
Jeff Thompson41471912013-09-12 16:21:50 -0700325}
326
327#endif