blob: 6cc46958ae51a57912978b11b51b130a5cf99e98 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47c93cf2013-08-09 00:38:48 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_KEY_CHAIN_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_KEY_CHAIN_HPP
Jeff Thompson47c93cf2013-08-09 00:38:48 -070010
Jeff Thompson7a67cb62013-08-26 11:43:18 -070011#include "../data.hpp"
Jeff Thompson2ce8f492013-09-17 18:01:25 -070012#include "../face.hpp"
13#include "identity/identity-manager.hpp"
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070014#include "encryption/encryption-manager.hpp"
Jeff Thompson47c93cf2013-08-09 00:38:48 -070015
16namespace ndn {
17
Jeff Thompson29ce3102013-09-27 11:47:48 -070018class PolicyManager;
Jeff Thompsoncda349e2013-11-05 17:37:39 -080019class ValidationRequest;
Jeff Thompson29ce3102013-09-27 11:47:48 -070020
Jeff Thompson2ce8f492013-09-17 18:01:25 -070021/**
22 * An OnVerified function object is used to pass a callback to verifyData to report a successful verification.
23 */
24typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerified;
25
26/**
27 * An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verification.
28 */
Jeff Thompson29ce3102013-09-27 11:47:48 -070029typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerifyFailed;
Jeff Thompson2ce8f492013-09-17 18:01:25 -070030
Jeff Thompsonffa36f92013-09-20 08:42:41 -070031/**
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070032 * Keychain is the main class of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070033 *
34 * The Keychain class provides a set of interfaces to the security library such as identity management, policy configuration
35 * and packet signing and verification.
36 */
Jeff Thompson47c93cf2013-08-09 00:38:48 -070037class KeyChain {
38public:
Jeff Thompson29ce3102013-09-27 11:47:48 -070039 KeyChain
40 (const ptr_lib::shared_ptr<IdentityManager>& identityManager, const ptr_lib::shared_ptr<PolicyManager>& policyManager);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070041
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070042 /*****************************************
43 * Identity Management *
44 *****************************************/
45
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070046 /**
47 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
48 * @param identityName The name of the identity.
49 * @return The key name of the auto-generated KSK of the identity.
50 */
51 Name
52 createIdentity(const Name& identityName)
53 {
54 return identityManager_->createIdentity(identityName);
55 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070056
57 /**
58 * Get the default identity.
59 * @return The default identity name.
60 */
61 Name
62 getDefaultIdentity()
63 {
64 return identityManager_->getDefaultIdentity();
65 }
66
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070067 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070068 * Generate a pair of RSA keys for the specified identity.
69 * @param identityName The name of the identity.
70 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
71 * @param keySize The size of the key.
72 * @return The generated key name.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070073 */
74 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070075 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048)
76 {
77 return identityManager_->generateRSAKeyPair(identityName, isKsk, keySize);
78 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070079
80 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070081 * Set a key as the default key of an identity.
82 * @param keyName The name of the key.
83 * @param identityName the name of the identity. If not specified, the identity name is inferred from the keyName.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070084 */
85 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070086 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
87 {
88 return identityManager_->setDefaultKeyForIdentity(keyName, identityName);
89 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070090
91 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070092 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
93 * @param identityName The name of the identity.
94 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
95 * @param keySize The size of the key.
96 * @return The generated key name.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070097 */
98 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070099 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048)
100 {
101 return identityManager_->generateRSAKeyPairAsDefault(identityName, isKsk, keySize);
102 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700103
104 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700105 * Create a public key signing request.
106 * @param keyName The name of the key.
107 * @returns The signing request data.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700108 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700109 Blob
110 createSigningRequest(const Name& keyName)
111 {
112 return identityManager_->getPublicKey(keyName)->getKeyDer();
113 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700114
115 /**
Jeff Thompsonb63abf52013-10-04 11:23:34 -0700116 * Install an identity certificate into the public key identity storage.
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700117 * @param certificate The certificate to to added.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700118 */
119 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700120 installIdentityCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700121 {
122 identityManager_->addCertificate(certificate);
123 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700124
125 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700126 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700127 * @param certificateName The certificate.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700128 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700129 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700130 setDefaultCertificateForKey(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700131 {
Jeff Thompson418b05a2013-10-22 17:48:54 -0700132 identityManager_->setDefaultCertificateForKey(certificate);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700133 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -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.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700138 * @return the requested certificate which is valid.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700139 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700140 ptr_lib::shared_ptr<Certificate>
141 getCertificate(const Name& certificateName)
142 {
143 return identityManager_->getCertificate(certificateName);
144 }
145
146 /**
147 * 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.
150 */
151 ptr_lib::shared_ptr<Certificate>
152 getAnyCertificate(const Name& certificateName)
153 {
154 return identityManager_->getAnyCertificate(certificateName);
155 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700156
157 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700158 * Get an identity certificate with the specified name.
159 * @param certificateName The name of the requested certificate.
160 * @return the requested certificate which is valid.
161 */
162 ptr_lib::shared_ptr<IdentityCertificate>
163 getIdentityCertificate(const Name& certificateName)
164 {
165 return identityManager_->getCertificate(certificateName);
166 }
167
168 /**
169 * Get an identity certificate even if the certificate is not valid anymore.
170 * @param certificateName The name of the requested certificate.
171 * @return the requested certificate.
172 */
173 ptr_lib::shared_ptr<IdentityCertificate>
174 getAnyIdentityCertificate(const Name& certificateName)
175 {
176 return identityManager_->getAnyCertificate(certificateName);
177 }
178
179 /**
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700180 * Revoke a key
181 * @param keyName the name of the key that will be revoked
182 */
183 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700184 revokeKey(const Name & keyName)
185 {
186 //TODO: Implement
187 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700188
189 /**
190 * Revoke a certificate
191 * @param certificateName the name of the certificate that will be revoked
192 */
193 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700194 revokeCertificate(const Name & certificateName)
195 {
196 //TODO: Implement
197 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700198
Jeff Thompson418b05a2013-10-22 17:48:54 -0700199 ptr_lib::shared_ptr<IdentityManager>
200 getIdentityManager() { return identityManager_; }
201
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700202 /*****************************************
203 * Policy Management *
204 *****************************************/
205
206 const ptr_lib::shared_ptr<PolicyManager>&
207 getPolicyManager() { return policyManager_; }
208
209 /*****************************************
210 * Sign/Verify *
211 *****************************************/
212
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700213 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700214 * Wire encode the Data object, sign it and set its signature.
Jeff Thompsonade5b1e2013-08-09 12:16:45 -0700215 * Note: the caller must make sure the timestamp is correct, for example with
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700216 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700217 * @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 -0700218 * @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 -0700219 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson3c73da42013-08-12 11:19:05 -0700220 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700221 void
Jeff Thompson29ce3102013-09-27 11:47:48 -0700222 sign(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700223
Jeff Thompson29ce3102013-09-27 11:47:48 -0700224 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700225 * Sign the byte array using a certificate name and return a Signature object.
226 * @param buffer The byte array to be signed.
227 * @param bufferLength the length of buffer.
228 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
229 * @return The Signature.
230 */
231 ptr_lib::shared_ptr<Signature>
232 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
233
234 /**
235 * Sign the byte array using a certificate name and return a Signature object.
236 * @param buffer The byte array to be signed.
237 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
238 * @return The Signature.
239 */
240 ptr_lib::shared_ptr<Signature>
241 sign(const std::vector<uint8_t>& buffer, const Name& certificateName)
242 {
243 return sign(&buffer[0], buffer.size(), certificateName);
244 }
245
246 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700247 * Wire encode the Data object, sign it and set its signature.
248 * Note: the caller must make sure the timestamp is correct, for example with
249 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
250 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
251 * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name.
252 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
253 */
254 void
255 signByIdentity(Data& data, const Name& identityName = Name(), WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson3c73da42013-08-12 11:19:05 -0700256
257 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700258 * Sign the byte array using an identity name and return a Signature object.
259 * @param buffer The byte array to be signed.
260 * @param bufferLength the length of buffer.
261 * @param identityName The identity name.
262 * @return The Signature.
263 */
264 ptr_lib::shared_ptr<Signature>
265 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName);
266
267 /**
268 * Sign the byte array using an identity name and return a Signature object.
269 * @param buffer The byte array to be signed.
270 * @param identityName The identity name.
271 * @return The Signature.
272 */
273 ptr_lib::shared_ptr<Signature>
274 signByIdentity(const std::vector<uint8_t>& buffer, const Name& identityName)
275 {
276 return signByIdentity(&buffer[0], buffer.size(), identityName);
277 }
278
279 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700280 * Check the signature on the Data object and call either onVerify or onVerifyFailed.
281 * We use callback functions because verify may fetch information to check the signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700282 * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding.
283 * To set the wireEncoding, you can call data.wireDecode.
284 * @param onVerified If the signature is verified, this calls onVerified(data).
285 * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data).
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700286 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700287 void
Jeff Thompson7c5d2312013-09-25 16:07:15 -0700288 verifyData
289 (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount = 0);
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700290
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700291 /*****************************************
292 * Encrypt/Decrypt *
293 *****************************************/
294
295 /**
296 * Generate a symmetric key.
297 * @param keyName The name of the generated key.
298 * @param keyType The type of the key, e.g. KEY_TYPE_AES
299 */
300 void
301 generateSymmetricKey(const Name& keyName, KeyType keyType)
302 {
303 encryptionManager_->createSymmetricKey(keyName, keyType);
304 }
305
306 /**
307 * Encrypt a byte array.
308 * @param keyName The name of the encrypting key.
309 * @param data The byte array that will be encrypted.
310 * @param dataLength The length of data.
311 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
312 * @param encryptMode the encryption mode
313 * @return the encrypted data as an immutable Blob.
314 */
315 Blob
316 encrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
317 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
318 {
319 return encryptionManager_->encrypt(keyName, data, dataLength, useSymmetric, encryptMode);
320 }
321
322 /**
323 * Decrypt a byte array.
324 * @param keyName The name of the decrypting key.
325 * @param data The byte array that will be decrypted.
326 * @param dataLength The length of data.
327 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
328 * @param encryptMode the encryption mode
329 * @return the decrypted data as an immutable Blob.
330 */
331 Blob
332 decrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
333 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
334 {
335 return encryptionManager_->decrypt(keyName, data, dataLength, useSymmetric, encryptMode);
336 }
337
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700338 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700339 * Set the Face which will be used to fetch required certificates.
340 * @param face A pointer to the Face object.
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700341 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700342 void
343 setFace(Face* face) { face_ = face; }
344
345private:
Jeff Thompsoncda349e2013-11-05 17:37:39 -0800346 void
347 onCertificateData
348 (const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
349
350 void
351 onCertificateInterestTimeout
352 (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed,
353 const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
354
Jeff Thompson40f361a2013-09-25 13:12:48 -0700355 ptr_lib::shared_ptr<IdentityManager> identityManager_;
Jeff Thompson29ce3102013-09-27 11:47:48 -0700356 ptr_lib::shared_ptr<PolicyManager> policyManager_;
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700357 ptr_lib::shared_ptr<EncryptionManager> encryptionManager_;
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700358 Face* face_;
359 const int maxSteps_;
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700360};
361
362}
363
364#endif