blob: 5a1198ab7428c603a84edb259306a4d09b9781c0 [file] [log] [blame]
Jeff Thompson47c93cf2013-08-09 00:38:48 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_KEY_CHAIN_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_KEY_CHAIN_HPP
Jeff Thompson47c93cf2013-08-09 00:38:48 -07009
Jeff Thompson7a67cb62013-08-26 11:43:18 -070010#include "../data.hpp"
Jeff Thompson2ce8f492013-09-17 18:01:25 -070011#include "../face.hpp"
12#include "identity/identity-manager.hpp"
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070013#include "encryption/encryption-manager.hpp"
Jeff Thompson47c93cf2013-08-09 00:38:48 -070014
15namespace ndn {
16
Jeff Thompson29ce3102013-09-27 11:47:48 -070017class PolicyManager;
18
Jeff Thompson2ce8f492013-09-17 18:01:25 -070019/**
20 * An OnVerified function object is used to pass a callback to verifyData to report a successful verification.
21 */
22typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerified;
23
24/**
25 * An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verification.
26 */
Jeff Thompson29ce3102013-09-27 11:47:48 -070027typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerifyFailed;
Jeff Thompson2ce8f492013-09-17 18:01:25 -070028
Jeff Thompsonffa36f92013-09-20 08:42:41 -070029/**
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070030 * Keychain is the main class of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070031 *
32 * The Keychain class provides a set of interfaces to the security library such as identity management, policy configuration
33 * and packet signing and verification.
34 */
Jeff Thompson47c93cf2013-08-09 00:38:48 -070035class KeyChain {
36public:
Jeff Thompson29ce3102013-09-27 11:47:48 -070037 KeyChain
38 (const ptr_lib::shared_ptr<IdentityManager>& identityManager, const ptr_lib::shared_ptr<PolicyManager>& policyManager);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070039
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070040 /*****************************************
41 * Identity Management *
42 *****************************************/
43
44#if 0
45 /**
46 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
47 * @param identityName The name of the identity.
48 * @return The key name of the auto-generated KSK of the identity.
49 */
50 Name
51 createIdentity(const Name& identityName)
52 {
53 return identityManager_->createIdentity(identityName);
54 }
55#endif
56
57 /**
58 * Get the default identity.
59 * @return The default identity name.
60 */
61 Name
62 getDefaultIdentity()
63 {
64 return identityManager_->getDefaultIdentity();
65 }
66
67#if 0
68 /**
69 * Generate a pair of RSA keys for the specified identity
70 * @param identity the name of the identity
71 * @param ksk create a KSK or not, true for KSK, false for DSK
72 * @param keySize the size of the key
73 * @return the generated key name
74 */
75 Name
76 generateRSAKeyPair (const Name& identity, bool ksk = false, int keySize = 2048);
77
78 /**
79 * Set a key as the default key of an identity
80 * @param keyName the name of the key
81 * @param identity the name of the identity, if not specified the identity name can be inferred from the keyName
82 */
83 void
84 setDefaultKeyForIdentity (const Name& keyName, const Name& identity = Name());
85
86 /**
87 * Generate a pair of RSA keys for the specified identity and set it as default key of the identity
88 * @param identity the name of the identity
89 * @param ksk create a KSK or not, true for KSK, false for DSK
90 * @param keySize the size of the key
91 * @return the generated key name
92 */
93 Name
94 generateRSAKeyPairAsDefault (const Name& identity, bool ksk = false, int keySize = 2048);
95
96 /**
97 * Create a public key signing request
98 * @param keyName the name of the key
99 * @returns signing request blob
100 */
101 Ptr<Blob>
102 createSigningRequest(const Name& keyName);
103
104 /**
105 * Install a certificate into identity
106 * @param certificate the certificate in terms of Data packet
107 */
108 void
109 installCertificate(Ptr<Certificate> certificate);
110
111 /**
112 * Set a certificate as the default certificate name of the corresponding key
113 * @param certificateName the name of the certificate
114 */
115 void
116 setDefaultCertificateForKey(const Name& certificateName);
117
118 /**
119 * Get certificate
120 * @param certificateName name of the certificate
121 * @returns certificate that is valid
122 */
123 Ptr<Certificate>
124 getCertificate(const Name& certificateName);
125
126 /**
127 * Get certificate even if it is not valid
128 * @param certificateName name of the certificate
129 * @returns certificate that is valid
130 */
131 Ptr<Certificate>
132 getAnyCertificate(const Name& certName);
133
134 /**
135 * Revoke a key
136 * @param keyName the name of the key that will be revoked
137 */
138 void
139 revokeKey(const Name & keyName);
140
141 /**
142 * Revoke a certificate
143 * @param certificateName the name of the certificate that will be revoked
144 */
145 void
146 revokeCertificate(const Name & certificateName);
147#endif
148
149 /*****************************************
150 * Policy Management *
151 *****************************************/
152
153 const ptr_lib::shared_ptr<PolicyManager>&
154 getPolicyManager() { return policyManager_; }
155
156 /*****************************************
157 * Sign/Verify *
158 *****************************************/
159
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700160 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700161 * Wire encode the Data object, sign it and set its signature.
Jeff Thompsonade5b1e2013-08-09 12:16:45 -0700162 * Note: the caller must make sure the timestamp is correct, for example with
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700163 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700164 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700165 * @param certificateName The certificate name of the key to use for signing. If omitted, infer the signing identity from the data packet name.
Jeff Thompson8d24fe12013-09-18 15:54:51 -0700166 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson3c73da42013-08-12 11:19:05 -0700167 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700168 void
Jeff Thompson29ce3102013-09-27 11:47:48 -0700169 sign(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700170
Jeff Thompson29ce3102013-09-27 11:47:48 -0700171 /**
172 * Wire encode the Data object, sign it and set its signature.
173 * Note: the caller must make sure the timestamp is correct, for example with
174 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
175 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
176 * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name.
177 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
178 */
179 void
180 signByIdentity(Data& data, const Name& identityName = Name(), WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson3c73da42013-08-12 11:19:05 -0700181
182 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700183 * Check the signature on the Data object and call either onVerify or onVerifyFailed.
184 * We use callback functions because verify may fetch information to check the signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700185 * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding.
186 * To set the wireEncoding, you can call data.wireDecode.
187 * @param onVerified If the signature is verified, this calls onVerified(data).
188 * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data).
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700189 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700190 void
Jeff Thompson7c5d2312013-09-25 16:07:15 -0700191 verifyData
192 (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount = 0);
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700193
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700194 /*****************************************
195 * Encrypt/Decrypt *
196 *****************************************/
197
198 /**
199 * Generate a symmetric key.
200 * @param keyName The name of the generated key.
201 * @param keyType The type of the key, e.g. KEY_TYPE_AES
202 */
203 void
204 generateSymmetricKey(const Name& keyName, KeyType keyType)
205 {
206 encryptionManager_->createSymmetricKey(keyName, keyType);
207 }
208
209 /**
210 * Encrypt a byte array.
211 * @param keyName The name of the encrypting key.
212 * @param data The byte array that will be encrypted.
213 * @param dataLength The length of data.
214 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
215 * @param encryptMode the encryption mode
216 * @return the encrypted data as an immutable Blob.
217 */
218 Blob
219 encrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
220 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
221 {
222 return encryptionManager_->encrypt(keyName, data, dataLength, useSymmetric, encryptMode);
223 }
224
225 /**
226 * Decrypt a byte array.
227 * @param keyName The name of the decrypting key.
228 * @param data The byte array that will be decrypted.
229 * @param dataLength The length of data.
230 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
231 * @param encryptMode the encryption mode
232 * @return the decrypted data as an immutable Blob.
233 */
234 Blob
235 decrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
236 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
237 {
238 return encryptionManager_->decrypt(keyName, data, dataLength, useSymmetric, encryptMode);
239 }
240
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700241 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700242 * Set the Face which will be used to fetch required certificates.
243 * @param face A pointer to the Face object.
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700244 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700245 void
246 setFace(Face* face) { face_ = face; }
247
248private:
Jeff Thompson40f361a2013-09-25 13:12:48 -0700249 ptr_lib::shared_ptr<IdentityManager> identityManager_;
Jeff Thompson29ce3102013-09-27 11:47:48 -0700250 ptr_lib::shared_ptr<PolicyManager> policyManager_;
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700251 ptr_lib::shared_ptr<EncryptionManager> encryptionManager_;
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700252 Face* face_;
253 const int maxSteps_;
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700254};
255
256}
257
258#endif