blob: c53607f8c0ad497eb3afd3c4071014ba20e53db1 [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 Thompson18bf6312013-10-04 11:23:55 -070068 * Get the default key for an identity.
69 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
70 * @return The default key name.
71 */
72 Name
73 getDefaultKeyNameForIdentity(const Name& identityName = Name())
74 {
75 return identityStorage_->getDefaultKeyNameForIdentity(identityName);
76 }
77
78 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070079 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
80 * @param identityName The name of the identity.
81 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
82 * @param keySize The size of the key.
83 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070084 */
85 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070086 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070087
88 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070089 * Get the public key with the specified name.
90 * @param keyName The name of the key.
91 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070092 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -070093 ptr_lib::shared_ptr<PublicKey>
94 getPublicKey(const Name& keyName)
95 {
96 return PublicKey::fromDer(identityStorage_->getKey(keyName));
97 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070098
99 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700100 * Add a certificate into the public key identity storage.
101 * @param certificate The certificate to to added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700102 */
103 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700104 addCertificate(const Certificate& certificate)
105 {
106 identityStorage_->addCertificate(certificate);
107 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700108
109 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700110 * Set the certificate as the default for its corresponding key.
111 * @param certificateName The name of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700112 */
113 void
114 setDefaultCertificateForKey(const Name& certificateName);
115
116 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700117 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
118 * @param certificate The certificate to be added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700119 */
120 void
121 addCertificateAsIdentityDefault(const Certificate& certificate);
122
123 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700124 * 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 -0700125 * certificate the certificate to be added
126 */
127 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700128 addCertificateAsDefault(const Certificate& certificate)
129 {
130 identityStorage_->addCertificate(certificate);
131 setDefaultCertificateForKey(certificate.getName());
132 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700133
134 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700135 * Get a certificate with the specified name.
136 * @param certificateName The name of the requested certificate.
137 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700138 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700139 ptr_lib::shared_ptr<Certificate>
140 getCertificate(const Name& certificateName)
141 {
142 return identityStorage_->getCertificate(certificateName, false);
143 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700144
145 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700146 * Get a certificate even if the certificate is not valid anymore.
147 * @param certificateName The name of the requested certificate.
148 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700149 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700150 ptr_lib::shared_ptr<Certificate>
151 getAnyCertificate(const Name& certificateName)
152 {
153 return identityStorage_->getCertificate(certificateName, true);
154 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700155
156 /**
157 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
158 * @param identityName The name of the specified identity.
159 * @return The requested certificate name.
160 */
161 Name
162 getDefaultCertificateNameForIdentity(const Name& identityName)
163 {
164 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
165 }
166
167 /**
168 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
169 * the identity is not specified.
170 * @return The requested certificate name.
171 */
172 Name
173 getDefaultCertificateName()
174 {
175 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
176 }
177
178#if 0
179 /**
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700180 * sign blob based on certificate name
181 * @param blob the blob to be signed
182 * @param certificateName the signing certificate name
183 * @return the generated signature
184 */
185 Ptr<Signature>
186 signByCertificate(const Blob& blob, const Name& certificateName);
187#endif
188
Jeff Thompson41471912013-09-12 16:21:50 -0700189 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700190 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700191 * Note: the caller must make sure the timestamp in data is correct, for example with
192 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
193 * @param data The Data object to sign and update its signature.
194 * @param certificateName The Name identifying the certificate which identifies the signing key.
195 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
196 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700197 void
Jeff Thompson86e1d752013-09-17 17:22:38 -0700198 signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
199
200private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700201 /**
202 * Generate a key pair for the specified identity.
203 * @param identityName The name of the specified identity.
204 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
205 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
206 * @param keySize The size of the key pair.
207 * @return The name of the generated key.
208 */
209 Name
210 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
211
212 /**
213 * Generate a self-signed certificate for a public key.
214 * @param keyName The name of the public key.
215 * @return The generated certificate.
216 */
217 ptr_lib::shared_ptr<Certificate>
218 selfSign(const Name& keyName);
219
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700220 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700221 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700222};
223
224}
225
226#endif