blob: 08f043480b465a4c5a7aca0ac0221e41e835cafd [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
10#define NDN_IDENTITY_MANAGER_HPP
11
Jeff Thompsone7e069b2013-09-27 15:48:48 -070012#include "../certificate/certificate.hpp"
Jeff Thompson9296f0c2013-09-23 18:10:27 -070013#include "identity-storage.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070014#include "../certificate/public-key.hpp"
Jeff Thompson86e1d752013-09-17 17:22:38 -070015#include "private-key-storage.hpp"
Jeff Thompson41471912013-09-12 16:21:50 -070016
17namespace ndn {
18
Jeff Thompsonffa36f92013-09-20 08:42:41 -070019/**
20 * An IdentityManager is the interface of operations related to identity, keys, and certificates.
21 */
Jeff Thompson41471912013-09-12 16:21:50 -070022class IdentityManager {
23public:
Jeff Thompson9296f0c2013-09-23 18:10:27 -070024 IdentityManager(const ptr_lib::shared_ptr<IdentityStorage>& identityStorage, const ptr_lib::shared_ptr<PrivateKeyStorage>& privateKeyStorage)
25 : identityStorage_(identityStorage), privateKeyStorage_(privateKeyStorage)
Jeff Thompson86e1d752013-09-17 17:22:38 -070026 {
27 }
28
Jeff Thompson9296f0c2013-09-23 18:10:27 -070029 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070030 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
31 * @param identityName The name of the identity.
32 * @return The key name of the auto-generated KSK of the identity.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070033 */
34 Name
35 createIdentity(const Name& identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070036
37 /**
38 * Get the default identity.
39 * @return The default identity name.
40 */
41 Name
42 getDefaultIdentity()
43 {
44 return identityStorage_->getDefaultIdentity();
45 }
46
Jeff Thompson9296f0c2013-09-23 18:10:27 -070047 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070048 * Generate a pair of RSA keys for the specified identity.
49 * @param identityName The name of the identity.
50 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
51 * @param keySize The size of the key.
52 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070053 */
54 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070055 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070056
57 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070058 * Set a key as the default key of an identity.
59 * @param keyName The name of the key.
60 * @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 -070061 */
62 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070063 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
64 {
65 identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName);
66 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070067
68 /**
Jeff Thompson18bf6312013-10-04 11:23:55 -070069 * Get the default key for an identity.
70 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
71 * @return The default key name.
72 */
73 Name
74 getDefaultKeyNameForIdentity(const Name& identityName = Name())
75 {
76 return identityStorage_->getDefaultKeyNameForIdentity(identityName);
77 }
78
79 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070080 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
81 * @param identityName The name of the identity.
82 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
83 * @param keySize The size of the key.
84 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070085 */
86 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070087 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070088
89 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070090 * Get the public key with the specified name.
91 * @param keyName The name of the key.
92 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070093 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -070094 ptr_lib::shared_ptr<PublicKey>
95 getPublicKey(const Name& keyName)
96 {
97 return PublicKey::fromDer(identityStorage_->getKey(keyName));
98 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070099
100 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700101 * Add a certificate into the public key identity storage.
102 * @param certificate The certificate to to added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700103 */
104 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700105 addCertificate(const Certificate& certificate)
106 {
107 identityStorage_->addCertificate(certificate);
108 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700109
110 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700111 * Set the certificate as the default for its corresponding key.
112 * @param certificateName The name of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700113 */
114 void
115 setDefaultCertificateForKey(const Name& certificateName);
116
117 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700118 * Add a certificate into the public key identity storage and set the certificate as the default for its corresponding identity.
119 * @param certificate The certificate to be added.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700120 */
121 void
122 addCertificateAsIdentityDefault(const Certificate& certificate);
123
124 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700125 * 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 -0700126 * certificate the certificate to be added
127 */
128 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700129 addCertificateAsDefault(const Certificate& certificate)
130 {
131 identityStorage_->addCertificate(certificate);
132 setDefaultCertificateForKey(certificate.getName());
133 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700134
135 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700136 * Get a certificate with the specified name.
137 * @param certificateName The name of the requested certificate.
138 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700139 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700140 ptr_lib::shared_ptr<Certificate>
141 getCertificate(const Name& certificateName)
142 {
143 return identityStorage_->getCertificate(certificateName, false);
144 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700145
146 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700147 * Get a certificate even if the certificate is not valid anymore.
148 * @param certificateName The name of the requested certificate.
149 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700150 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700151 ptr_lib::shared_ptr<Certificate>
152 getAnyCertificate(const Name& certificateName)
153 {
154 return identityStorage_->getCertificate(certificateName, true);
155 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700156
157 /**
158 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
159 * @param identityName The name of the specified identity.
160 * @return The requested certificate name.
161 */
162 Name
163 getDefaultCertificateNameForIdentity(const Name& identityName)
164 {
165 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
166 }
167
168 /**
169 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
170 * the identity is not specified.
171 * @return The requested certificate name.
172 */
173 Name
174 getDefaultCertificateName()
175 {
176 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
177 }
178
179#if 0
180 /**
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700181 * sign blob based on certificate name
182 * @param blob the blob to be signed
183 * @param certificateName the signing certificate name
184 * @return the generated signature
185 */
186 Ptr<Signature>
187 signByCertificate(const Blob& blob, const Name& certificateName);
188#endif
189
Jeff Thompson41471912013-09-12 16:21:50 -0700190 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700191 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700192 * Note: the caller must make sure the timestamp in data is correct, for example with
193 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
194 * @param data The Data object to sign and update its signature.
195 * @param certificateName The Name identifying the certificate which identifies the signing key.
196 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
197 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700198 void
Jeff Thompson86e1d752013-09-17 17:22:38 -0700199 signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
200
201private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700202 /**
203 * Generate a key pair for the specified identity.
204 * @param identityName The name of the specified identity.
205 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
206 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
207 * @param keySize The size of the key pair.
208 * @return The name of the generated key.
209 */
210 Name
211 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
212
213 /**
214 * Generate a self-signed certificate for a public key.
215 * @param keyName The name of the public key.
216 * @return The generated certificate.
217 */
218 ptr_lib::shared_ptr<Certificate>
219 selfSign(const Name& keyName);
220
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700221 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700222 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700223};
224
225}
226
227#endif