blob: 2dd9b6e232ad9e84015dfc83c7b19153c0a23d1d [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 Thompsonf39a5362013-10-10 16:22:44 -0700129 addCertificateAsDefault(const Certificate& certificate);
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700130
131 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700132 * Get a certificate with the specified name.
133 * @param certificateName The name of the requested certificate.
134 * @return the requested certificate which is valid.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700135 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700136 ptr_lib::shared_ptr<Certificate>
137 getCertificate(const Name& certificateName)
138 {
139 return identityStorage_->getCertificate(certificateName, false);
140 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700141
142 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700143 * Get a certificate even if the certificate is not valid anymore.
144 * @param certificateName The name of the requested certificate.
145 * @return the requested certificate.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700146 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700147 ptr_lib::shared_ptr<Certificate>
148 getAnyCertificate(const Name& certificateName)
149 {
150 return identityStorage_->getCertificate(certificateName, true);
151 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700152
153 /**
154 * Get the default certificate name for the specified identity, which will be used when signing is performed based on identity.
155 * @param identityName The name of the specified identity.
156 * @return The requested certificate name.
157 */
158 Name
159 getDefaultCertificateNameForIdentity(const Name& identityName)
160 {
161 return identityStorage_->getDefaultCertificateNameForIdentity(identityName);
162 }
163
164 /**
165 * Get the default certificate name of the default identity, which will be used when signing is based on identity and
166 * the identity is not specified.
167 * @return The requested certificate name.
168 */
169 Name
170 getDefaultCertificateName()
171 {
172 return identityStorage_->getDefaultCertificateNameForIdentity(getDefaultIdentity());
173 }
174
175#if 0
176 /**
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700177 * sign blob based on certificate name
178 * @param blob the blob to be signed
179 * @param certificateName the signing certificate name
180 * @return the generated signature
181 */
182 Ptr<Signature>
183 signByCertificate(const Blob& blob, const Name& certificateName);
184#endif
185
Jeff Thompson41471912013-09-12 16:21:50 -0700186 /**
Jeff Thompson86e1d752013-09-17 17:22:38 -0700187 * Sign data packet based on the certificate name.
Jeff Thompson41471912013-09-12 16:21:50 -0700188 * Note: the caller must make sure the timestamp in data is correct, for example with
189 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
190 * @param data The Data object to sign and update its signature.
191 * @param certificateName The Name identifying the certificate which identifies the signing key.
192 * @param wireFormat The WireFormat for calling encodeData, or WireFormat::getDefaultWireFormat() if omitted.
193 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700194 void
Jeff Thompson86e1d752013-09-17 17:22:38 -0700195 signByCertificate(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
196
197private:
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700198 /**
199 * Generate a key pair for the specified identity.
200 * @param identityName The name of the specified identity.
201 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
202 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
203 * @param keySize The size of the key pair.
204 * @return The name of the generated key.
205 */
206 Name
207 generateKeyPair(const Name& identityName, bool isKsk = false, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048);
208
209 /**
210 * Generate a self-signed certificate for a public key.
211 * @param keyName The name of the public key.
212 * @return The generated certificate.
213 */
214 ptr_lib::shared_ptr<Certificate>
215 selfSign(const Name& keyName);
216
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700217 ptr_lib::shared_ptr<IdentityStorage> identityStorage_;
Jeff Thompson86e1d752013-09-17 17:22:38 -0700218 ptr_lib::shared_ptr<PrivateKeyStorage> privateKeyStorage_;
Jeff Thompson41471912013-09-12 16:21:50 -0700219};
220
221}
222
223#endif