blob: d5f49f42e1a5bdb707029819d10cbf509035e16a [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 Thompsonc69163b2013-10-12 13:49:50 -070012#include "../certificate/identity-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
Jeff Thompson958bf9b2013-10-12 17:20:51 -070017namespace 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 Thompsonc69163b2013-10-12 13:49:50 -0700101 * Create an identity certificate for a public key managed by this IdentityManager.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700102 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700103 * @param signerCertificateName The name of signing certificate.
104 * @param notBefore The notBefore value in the validity field of the generated certificate.
105 * @param notAfter The notAfter vallue in validity field of the generated certificate.
106 * @return The name of generated identity certificate.
107 */
108 Name
Jeff Thompson418b05a2013-10-22 17:48:54 -0700109 createIdentityCertificate
110 (const Name& certificatePrefix, const Name& signerCertificateName, const MillisecondsSince1970& notBefore,
111 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700112
113 /**
114 * Create an identity certificate for a public key supplied by the caller.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700115 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700116 * @param publickey The public key to be signed.
117 * @param signerCertificateName The name of signing certificate.
118 * @param notBefore The notBefore value in the validity field of the generated certificate.
119 * @param notAfter The notAfter vallue in validity field of the generated certificate.
120 * @return The generated identity certificate.
121 */
122 ptr_lib::shared_ptr<IdentityCertificate>
123 createIdentityCertificate
Jeff Thompson418b05a2013-10-22 17:48:54 -0700124 (const Name& certificatePrefix, const PublicKey& publickey, const Name& signerCertificateName,
125 const MillisecondsSince1970& notBefore, const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700126
127 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700128 * Add a certificate into the public key identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700129 * @param certificate The certificate to to added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700130 */
131 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700132 addCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700133 {
134 identityStorage_->addCertificate(certificate);
135 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700136
137 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700138 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700139 * @param certificateName The certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700140 */
141 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700142 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700143
144 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700145 * 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 -0700146 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700147 */
148 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700149 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700150
151 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700152 * 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 -0700153 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700154 */
155 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700156 addCertificateAsDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700157
158 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700159 * Get a certificate with the specified name.
160 * @param certificateName The name of the requested certificate.
161 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700162 */
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700163 ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700164 getCertificate(const Name& certificateName)
165 {
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700166 return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, false));
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700167 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700168
169 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700170 * Get a certificate even if the certificate is not valid anymore.
171 * @param certificateName The name of the requested certificate.
172 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700173 */
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700174 ptr_lib::shared_ptr<IdentityCertificate>
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700175 getAnyCertificate(const Name& certificateName)
176 {
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700177 return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, true));
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700178 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700179
180 /**
181 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
182 * @param identityName The name of the specified identity.
183 * @return The requested certificate name.
184 */
185 Name
186 getDefaultCertificateNameForIdentity(const Name& identityName)
187 {
188 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
189 }
190
191 /**
192 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
193 * the identity is not specified.
194 * @return The requested certificate name.
195 */
196 Name
197 getDefaultCertificateName()
198 {
199 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
200 }
201
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700202 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700203 * Sign the byte array data based on the certificate name.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700204 * @param buffer The byte array to be signed.
205 * @param bufferLength the length of buffer.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700206 * @param certificateName The signing certificate name.
207 * @return The generated signature.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700208 */
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700209 ptr_lib::shared_ptr<Signature>
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700210 signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
211
212 /**
213 * Sign the byte array data based on the certificate name.
214 * @param buffer The byte array to be signed.
215 * @param certificateName The signing certificate name.
216 * @return The generated signature.
217 */
218 ptr_lib::shared_ptr<Signature>
219 signByCertificate(const std::vector<uint8_t>& buffer, const Name& certificateName)
220 {
221 return signByCertificate(&buffer[0], buffer.size(), certificateName);
222 }
223
Jeff Thompson41471912013-09-12 16:21:50 -0700224 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700225 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700226 * Note: the caller must make sure the timestamp in data is correct, for example with
227 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
228 * @param data The Data object to sign and update its signature.
229 * @param certificateName The Name identifying the certificate which identifies the signing key.
230 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
231 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700232 void
Jeff Thompson86e1d752013-09-17 17:22:38 -0700233 signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson418b05a2013-10-22 17:48:54 -0700234
235 /**
236 * Generate a self-signed certificate for a public key.
237 * @param keyName The name of the public key.
238 * @return The generated certificate.
239 */
240 ptr_lib::shared_ptr<IdentityCertificate>
241 selfSign(const Name& keyName);
Jeff Thompson86e1d752013-09-17 17:22:38 -0700242
243private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700244 /**
245 * Generate a key pair for the specified identity.
246 * @param identityName The name of the specified identity.
247 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
248 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
249 * @param keySize The size of the key pair.
250 * @return The name of the generated key.
251 */
252 Name
253 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
254
Jeff Thompson418b05a2013-10-22 17:48:54 -0700255 static Name
256 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700257
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700258 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700259 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700260};
261
262}
263
264#endif