blob: 1d497ecc913960909a475ecfc85a4a68539eff2e [file] [log] [blame]
Jeff Thompson41471912013-09-12 16:21:50 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson06e787d2013-09-12 19:00:55 -07003 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07004 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson41471912013-09-12 16:21:50 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_IDENTITY_MANAGER_HPP
9#define NDN_IDENTITY_MANAGER_HPP
10
Jeff Thompsone7e069b2013-09-27 15:48:48 -070011#include "../certificate/certificate.hpp"
Jeff Thompson9296f0c2013-09-23 18:10:27 -070012#include "identity-storage.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070013#include "../certificate/public-key.hpp"
Jeff Thompson86e1d752013-09-17 17:22:38 -070014#include "private-key-storage.hpp"
Jeff Thompson41471912013-09-12 16:21:50 -070015
16namespace ndn {
17
Jeff Thompsonffa36f92013-09-20 08:42:41 -070018/**
19 * An IdentityManager is the interface of operations related to identity, keys, and certificates.
20 */
Jeff Thompson41471912013-09-12 16:21:50 -070021class IdentityManager {
22public:
Jeff Thompson9296f0c2013-09-23 18:10:27 -070023 IdentityManager(const ptr_lib::shared_ptr<IdentityStorage>& identityStorage, const ptr_lib::shared_ptr<PrivateKeyStorage>& privateKeyStorage)
24 : identityStorage_(identityStorage), privateKeyStorage_(privateKeyStorage)
Jeff Thompson86e1d752013-09-17 17:22:38 -070025 {
26 }
27
Jeff Thompson9296f0c2013-09-23 18:10:27 -070028 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070029 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
30 * @param identityName The name of the identity.
31 * @return The key name of the auto-generated KSK of the identity.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070032 */
33 Name
34 createIdentity(const Name& identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070035
36 /**
37 * Get the default identity.
38 * @return The default identity name.
39 */
40 Name
41 getDefaultIdentity()
42 {
43 return identityStorage_->getDefaultIdentity();
44 }
45
Jeff Thompson9296f0c2013-09-23 18:10:27 -070046 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070047 * Generate a pair of RSA keys for the specified identity.
48 * @param identityName The name of the identity.
49 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
50 * @param keySize The size of the key.
51 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070052 */
53 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070054 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070055
56 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070057 * Set a key as the default key of an identity.
58 * @param keyName The name of the key.
59 * @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 -070060 */
61 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070062 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
63 {
64 identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName);
65 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070066
67 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070068 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
69 * @param identityName The name of the identity.
70 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
71 * @param keySize The size of the key.
72 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070073 */
74 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070075 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070076
77 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070078 * Get the public key with the specified name.
79 * @param keyName The name of the key.
80 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070081 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -070082 ptr_lib::shared_ptr<PublicKey>
83 getPublicKey(const Name& keyName)
84 {
85 return PublicKey::fromDer(identityStorage_->getKey(keyName));
86 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070087
88 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070089 * Add a certificate into the public key identity storage.
90 * @param certificate The certificate to to added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070091 */
92 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070093 addCertificate(const Certificate& certificate)
94 {
95 identityStorage_->addCertificate(certificate);
96 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070097
98 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070099 * Set the certificate as the default for its corresponding key.
100 * @param certificateName The name of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700101 */
102 void
103 setDefaultCertificateForKey(const Name& certificateName);
104
105 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700106 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
107 * @param certificate The certificate to be added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700108 */
109 void
110 addCertificateAsIdentityDefault(const Certificate& certificate);
111
112 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700113 * Add a certificate into the public key identity storage and set the certificate as the default of its corresponding key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700114 * certificate the certificate to be added
115 */
116 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700117 addCertificateAsDefault(const Certificate& certificate)
118 {
119 identityStorage_->addCertificate(certificate);
120 setDefaultCertificateForKey(certificate.getName());
121 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700122
123 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700124 * Get a certificate with the specified name.
125 * @param certificateName The name of the requested certificate.
126 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700127 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700128 ptr_lib::shared_ptr<Certificate>
129 getCertificate(const Name& certificateName)
130 {
131 return identityStorage_->getCertificate(certificateName, false);
132 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700133
134 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700135 * Get a certificate even if the certificate is not valid anymore.
136 * @param certificateName The name of the requested certificate.
137 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700138 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700139 ptr_lib::shared_ptr<Certificate>
140 getAnyCertificate(const Name& certificateName)
141 {
142 return identityStorage_->getCertificate(certificateName, true);
143 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700144
145 /**
146 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
147 * @param identityName The name of the specified identity.
148 * @return The requested certificate name.
149 */
150 Name
151 getDefaultCertificateNameForIdentity(const Name& identityName)
152 {
153 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
154 }
155
156 /**
157 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
158 * the identity is not specified.
159 * @return The requested certificate name.
160 */
161 Name
162 getDefaultCertificateName()
163 {
164 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
165 }
166
167#if 0
168 /**
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700169 * sign blob based on certificate name
170 * @param blob the blob to be signed
171 * @param certificateName the signing certificate name
172 * @return the generated signature
173 */
174 Ptr<Signature>
175 signByCertificate(const Blob& blob, const Name& certificateName);
176#endif
177
Jeff Thompson41471912013-09-12 16:21:50 -0700178 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700179 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700180 * Note: the caller must make sure the timestamp in data is correct, for example with
181 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
182 * @param data The Data object to sign and update its signature.
183 * @param certificateName The Name identifying the certificate which identifies the signing key.
184 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
185 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700186 void
Jeff Thompson86e1d752013-09-17 17:22:38 -0700187 signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
188
189private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700190 /**
191 * Generate a key pair for the specified identity.
192 * @param identityName The name of the specified identity.
193 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
194 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
195 * @param keySize The size of the key pair.
196 * @return The name of the generated key.
197 */
198 Name
199 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
200
201 /**
202 * Generate a self-signed certificate for a public key.
203 * @param keyName The name of the public key.
204 * @return The generated certificate.
205 */
206 ptr_lib::shared_ptr<Certificate>
207 selfSign(const Name& keyName);
208
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700209 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700210 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700211};
212
213}
214
215#endif