blob: 4273f6b34efe0d7e11e7e06825adf0cd7d4489cc [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"
Jeff Thompson41471912013-09-12 16:21:50 -070014
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080015#include "../../data.hpp"
16
Jeff Thompson958bf9b2013-10-12 17:20:51 -070017namespace ndn {
18
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080019class IdentityCertificate;
20
Jeff Thompsonffa36f92013-09-20 08:42:41 -070021/**
22 * An IdentityManager is the interface of operations related to identity, keys, and certificates.
23 */
Jeff Thompson41471912013-09-12 16:21:50 -070024class IdentityManager {
25public:
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080026 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
27
Jeff Thompson9296f0c2013-09-23 18:10:27 -070028 IdentityManager(const ptr_lib::shared_ptr<IdentityStorage>& identityStorage, const ptr_lib::shared_ptr<PrivateKeyStorage>& privateKeyStorage)
29 : identityStorage_(identityStorage), privateKeyStorage_(privateKeyStorage)
Jeff Thompson86e1d752013-09-17 17:22:38 -070030 {
31 }
32
Jeff Thompson9296f0c2013-09-23 18:10:27 -070033 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070034 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
35 * @param identityName The name of the identity.
36 * @return The key name of the auto-generated KSK of the identity.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070037 */
38 Name
39 createIdentity(const Name& identityName);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070040
41 /**
42 * Get the default identity.
43 * @return The default identity name.
44 */
45 Name
46 getDefaultIdentity()
47 {
48 return identityStorage_->getDefaultIdentity();
49 }
50
Jeff Thompson9296f0c2013-09-23 18:10:27 -070051 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070052 * Generate a pair of RSA keys for the specified identity.
53 * @param identityName The name of the identity.
54 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
55 * @param keySize The size of the key.
56 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070057 */
58 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070059 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070060
61 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070062 * Set a key as the default key of an identity.
63 * @param keyName The name of the key.
64 * @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 -070065 */
66 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070067 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
68 {
69 identityStorage_->setDefaultKeyNameForIdentity(keyName, identityName);
70 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070071
72 /**
Jeff Thompson18bf6312013-10-04 11:23:55 -070073 * Get the default key for an identity.
74 * @param identityName the name of the identity. If omitted, the identity name is inferred from the keyName.
75 * @return The default key name.
76 */
77 Name
78 getDefaultKeyNameForIdentity(const Name& identityName = Name())
79 {
80 return identityStorage_->getDefaultKeyNameForIdentity(identityName);
81 }
82
83 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070084 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
85 * @param identityName The name of the identity.
86 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
87 * @param keySize The size of the key.
88 * @return The generated key name.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070089 */
90 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070091 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048);
Jeff Thompson9296f0c2013-09-23 18:10:27 -070092
93 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070094 * Get the public key with the specified name.
95 * @param keyName The name of the key.
96 * @return The public key.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070097 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080098 // ptr_lib::shared_ptr<PublicKey>
99 // getPublicKey(const Name& keyName)
100 // {
101 // return PublicKey::fromDer(identityStorage_->getKey(keyName));
102 // }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700103
104 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700105 * Create an identity certificate for a public key managed by this IdentityManager.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700106 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700107 * @param signerCertificateName The name of signing certificate.
108 * @param notBefore The notBefore value in the validity field of the generated certificate.
109 * @param notAfter The notAfter vallue in validity field of the generated certificate.
110 * @return The name of generated identity certificate.
111 */
112 Name
Jeff Thompson418b05a2013-10-22 17:48:54 -0700113 createIdentityCertificate
114 (const Name& certificatePrefix, const Name& signerCertificateName, const MillisecondsSince1970& notBefore,
115 const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700116
117 /**
118 * Create an identity certificate for a public key supplied by the caller.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700119 * @param certificatePrefix The name of public key to be signed.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700120 * @param publickey The public key to be signed.
121 * @param signerCertificateName The name of signing certificate.
122 * @param notBefore The notBefore value in the validity field of the generated certificate.
123 * @param notAfter The notAfter vallue in validity field of the generated certificate.
124 * @return The generated identity certificate.
125 */
126 ptr_lib::shared_ptr<IdentityCertificate>
127 createIdentityCertificate
Jeff Thompson418b05a2013-10-22 17:48:54 -0700128 (const Name& certificatePrefix, const PublicKey& publickey, const Name& signerCertificateName,
129 const MillisecondsSince1970& notBefore, const MillisecondsSince1970& notAfter);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700130
131 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700132 * Add a certificate into the public key identity storage.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700133 * @param certificate The certificate to to added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700134 */
135 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700136 addCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700137 {
138 identityStorage_->addCertificate(certificate);
139 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700140
141 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700142 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700143 * @param certificateName The certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700144 */
145 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700146 setDefaultCertificateForKey(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700147
148 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700149 * 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 -0700150 * @param certificate The certificate to be 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 addCertificateAsIdentityDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700154
155 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700156 * 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 -0700157 * @param certificate The certificate to be added. This makes a copy of the certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700158 */
159 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700160 addCertificateAsDefault(const IdentityCertificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700161
162 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700163 * Get a certificate with the specified name.
164 * @param certificateName The name of the requested certificate.
165 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700166 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800167 // ptr_lib::shared_ptr<IdentityCertificate>
168 // getCertificate(const Name& certificateName)
169 // {
170 // return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, false));
171 // }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700172
173 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700174 * Get a certificate even if the certificate is not valid anymore.
175 * @param certificateName The name of the requested certificate.
176 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700177 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800178 // ptr_lib::shared_ptr<IdentityCertificate>
179 // getAnyCertificate(const Name& certificateName)
180 // {
181 // return ptr_lib::make_shared<IdentityCertificate>(*identityStorage_->getCertificate(certificateName, true));
182 // }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700183
184 /**
185 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
186 * @param identityName The name of the specified identity.
187 * @return The requested certificate name.
188 */
189 Name
190 getDefaultCertificateNameForIdentity(const Name& identityName)
191 {
192 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
193 }
194
195 /**
196 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
197 * the identity is not specified.
198 * @return The requested certificate name.
199 */
200 Name
201 getDefaultCertificateName()
202 {
203 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
204 }
205
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700206 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700207 * Sign the byte array data based on the certificate name.
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700208 * @param buffer The byte array to be signed.
209 * @param bufferLength the length of buffer.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700210 * @param certificateName The signing certificate name.
211 * @return The generated signature.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700212 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800213 Signature
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700214 signByCertificate(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
215
216 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700217 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700218 * Note: the caller must make sure the timestamp in data is correct, for example with
219 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
220 * @param data The Data object to sign and update its signature.
221 * @param certificateName The Name identifying the certificate which identifies the signing key.
222 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
223 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224 void
Alexander Afanasyev64a3d812014-01-05 23:35:05 -0800225 signByCertificate(Data& data, const Name& certificateName);
Jeff Thompson418b05a2013-10-22 17:48:54 -0700226
227 /**
228 * Generate a self-signed certificate for a public key.
229 * @param keyName The name of the public key.
230 * @return The generated certificate.
231 */
232 ptr_lib::shared_ptr<IdentityCertificate>
233 selfSign(const Name& keyName);
Jeff Thompson86e1d752013-09-17 17:22:38 -0700234
235private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700236 /**
237 * Generate a key pair for the specified identity.
238 * @param identityName The name of the specified identity.
239 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
240 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
241 * @param keySize The size of the key pair.
242 * @return The name of the generated key.
243 */
244 Name
245 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
246
Jeff Thompson418b05a2013-10-22 17:48:54 -0700247 static Name
248 getKeyNameFromCertificatePrefix(const Name& certificatePrefix);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700249
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700250 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700251 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700252};
253
254}
255
256#endif