blob: 424885099f0dd8c7243276fd81a7dcfd836160c8 [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.
Jeff Thompsonba16b8f2013-12-16 13:11:47 -08004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07006 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_KEY_CHAIN_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070010#define NDN_KEY_CHAIN_HPP
Jeff Thompson47c93cf2013-08-09 00:38:48 -070011
Jeff Thompson7a67cb62013-08-26 11:43:18 -070012#include "../data.hpp"
Jeff Thompson2ce8f492013-09-17 18:01:25 -070013#include "../face.hpp"
14#include "identity/identity-manager.hpp"
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070015#include "encryption/encryption-manager.hpp"
Jeff Thompsonba16b8f2013-12-16 13:11:47 -080016#include "policy/validation-request.hpp"
Jeff Thompson47c93cf2013-08-09 00:38:48 -070017
18namespace ndn {
19
Jeff Thompson29ce3102013-09-27 11:47:48 -070020class PolicyManager;
21
Jeff Thompson2ce8f492013-09-17 18:01:25 -070022/**
Jeff Thompsonba16b8f2013-12-16 13:11:47 -080023 * KeyChain is the main class of the security library.
Jeff Thompsonffa36f92013-09-20 08:42:41 -070024 *
Jeff Thompsonba16b8f2013-12-16 13:11:47 -080025 * The KeyChain class provides a set of interfaces to the security library such as identity management, policy configuration
Jeff Thompsonffa36f92013-09-20 08:42:41 -070026 * and packet signing and verification.
27 */
Jeff Thompson47c93cf2013-08-09 00:38:48 -070028class KeyChain {
29public:
Jeff Thompson29ce3102013-09-27 11:47:48 -070030 KeyChain
31 (const ptr_lib::shared_ptr<IdentityManager>& identityManager, const ptr_lib::shared_ptr<PolicyManager>& policyManager);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070032
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070033 /*****************************************
34 * Identity Management *
35 *****************************************/
36
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070037 /**
38 * Create an identity by creating a pair of Key-Signing-Key (KSK) for this identity and a self-signed certificate of the KSK.
39 * @param identityName The name of the identity.
40 * @return The key name of the auto-generated KSK of the identity.
41 */
42 Name
43 createIdentity(const Name& identityName)
44 {
45 return identityManager_->createIdentity(identityName);
46 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070047
48 /**
49 * Get the default identity.
50 * @return The default identity name.
51 */
52 Name
53 getDefaultIdentity()
54 {
55 return identityManager_->getDefaultIdentity();
56 }
57
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070058 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070059 * Generate a pair of RSA keys for the specified identity.
60 * @param identityName The name of the identity.
61 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
62 * @param keySize The size of the key.
63 * @return The generated key name.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070064 */
65 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070066 generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048)
67 {
68 return identityManager_->generateRSAKeyPair(identityName, isKsk, keySize);
69 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070070
71 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070072 * Set a key as the default key of an identity.
73 * @param keyName The name of the key.
74 * @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 -070075 */
76 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -070077 setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name())
78 {
79 return identityManager_->setDefaultKeyForIdentity(keyName, identityName);
80 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070081
82 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070083 * Generate a pair of RSA keys for the specified identity and set it as default key for the identity.
84 * @param identityName The name of the identity.
85 * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK).
86 * @param keySize The size of the key.
87 * @return The generated key name.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070088 */
89 Name
Jeff Thompsone7e069b2013-09-27 15:48:48 -070090 generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048)
91 {
92 return identityManager_->generateRSAKeyPairAsDefault(identityName, isKsk, keySize);
93 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070094
95 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -070096 * Create a public key signing request.
97 * @param keyName The name of the key.
98 * @returns The signing request data.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -070099 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700100 Blob
101 createSigningRequest(const Name& keyName)
102 {
103 return identityManager_->getPublicKey(keyName)->getKeyDer();
104 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700105
106 /**
Jeff Thompsonb63abf52013-10-04 11:23:34 -0700107 * Install an identity certificate into the public key identity storage.
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700108 * @param certificate The certificate to to added.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700109 */
110 void
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700111 installIdentityCertificate(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700112 {
113 identityManager_->addCertificate(certificate);
114 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700115
116 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700117 * Set the certificate as the default for its corresponding key.
Jeff Thompson418b05a2013-10-22 17:48:54 -0700118 * @param certificateName The certificate.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700119 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700120 void
Jeff Thompson418b05a2013-10-22 17:48:54 -0700121 setDefaultCertificateForKey(const IdentityCertificate& certificate)
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700122 {
Jeff Thompson418b05a2013-10-22 17:48:54 -0700123 identityManager_->setDefaultCertificateForKey(certificate);
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700124 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700125
126 /**
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700127 * Get a certificate with the specified name.
128 * @param certificateName The name of the requested certificate.
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700129 * @return the requested certificate which is valid.
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700130 */
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700131 ptr_lib::shared_ptr<Certificate>
132 getCertificate(const Name& certificateName)
133 {
134 return identityManager_->getCertificate(certificateName);
135 }
136
137 /**
138 * Get a certificate even if the certificate is not valid anymore.
139 * @param certificateName The name of the requested certificate.
140 * @return the requested certificate.
141 */
142 ptr_lib::shared_ptr<Certificate>
143 getAnyCertificate(const Name& certificateName)
144 {
145 return identityManager_->getAnyCertificate(certificateName);
146 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700147
148 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700149 * Get an identity certificate with the specified name.
150 * @param certificateName The name of the requested certificate.
151 * @return the requested certificate which is valid.
152 */
153 ptr_lib::shared_ptr<IdentityCertificate>
154 getIdentityCertificate(const Name& certificateName)
155 {
156 return identityManager_->getCertificate(certificateName);
157 }
158
159 /**
160 * Get an identity certificate even if the certificate is not valid anymore.
161 * @param certificateName The name of the requested certificate.
162 * @return the requested certificate.
163 */
164 ptr_lib::shared_ptr<IdentityCertificate>
165 getAnyIdentityCertificate(const Name& certificateName)
166 {
167 return identityManager_->getAnyCertificate(certificateName);
168 }
169
170 /**
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700171 * Revoke a key
172 * @param keyName the name of the key that will be revoked
173 */
174 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700175 revokeKey(const Name & keyName)
176 {
177 //TODO: Implement
178 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700179
180 /**
181 * Revoke a certificate
182 * @param certificateName the name of the certificate that will be revoked
183 */
184 void
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700185 revokeCertificate(const Name & certificateName)
186 {
187 //TODO: Implement
188 }
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700189
Jeff Thompson418b05a2013-10-22 17:48:54 -0700190 ptr_lib::shared_ptr<IdentityManager>
191 getIdentityManager() { return identityManager_; }
192
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700193 /*****************************************
194 * Policy Management *
195 *****************************************/
196
197 const ptr_lib::shared_ptr<PolicyManager>&
198 getPolicyManager() { return policyManager_; }
199
200 /*****************************************
201 * Sign/Verify *
202 *****************************************/
203
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700204 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700205 * Wire encode the Data object, sign it and set its signature.
Jeff Thompsonade5b1e2013-08-09 12:16:45 -0700206 * Note: the caller must make sure the timestamp is correct, for example with
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700207 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700208 * @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 -0700209 * @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 -0700210 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
Jeff Thompson3c73da42013-08-12 11:19:05 -0700211 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700212 void
Jeff Thompson29ce3102013-09-27 11:47:48 -0700213 sign(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700214
Jeff Thompson29ce3102013-09-27 11:47:48 -0700215 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700216 * Sign the byte array using a certificate name and return a Signature object.
217 * @param buffer The byte array to be signed.
218 * @param bufferLength the length of buffer.
219 * @param certificateName The certificate name used to get the signing key and which will be put into KeyLocator.
220 * @return The Signature.
221 */
222 ptr_lib::shared_ptr<Signature>
223 sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName);
224
225 /**
226 * Sign the byte array using a certificate name and return a Signature object.
227 * @param buffer The byte array to be signed.
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 std::vector<uint8_t>& buffer, const Name& certificateName)
233 {
234 return sign(&buffer[0], buffer.size(), certificateName);
235 }
236
237 /**
Jeff Thompson29ce3102013-09-27 11:47:48 -0700238 * Wire encode the Data object, sign it and set its signature.
239 * Note: the caller must make sure the timestamp is correct, for example with
240 * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0).
241 * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding.
242 * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name.
243 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
244 */
245 void
246 signByIdentity(Data& data, const Name& identityName = Name(), WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompson3c73da42013-08-12 11:19:05 -0700247
248 /**
Jeff Thompsonc01e1782013-10-21 14:08:42 -0700249 * Sign the byte array using an identity name and return a Signature object.
250 * @param buffer The byte array to be signed.
251 * @param bufferLength the length of buffer.
252 * @param identityName The identity name.
253 * @return The Signature.
254 */
255 ptr_lib::shared_ptr<Signature>
256 signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName);
257
258 /**
259 * Sign the byte array using an identity name and return a Signature object.
260 * @param buffer The byte array to be signed.
261 * @param identityName The identity name.
262 * @return The Signature.
263 */
264 ptr_lib::shared_ptr<Signature>
265 signByIdentity(const std::vector<uint8_t>& buffer, const Name& identityName)
266 {
267 return signByIdentity(&buffer[0], buffer.size(), identityName);
268 }
269
270 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700271 * Check the signature on the Data object and call either onVerify or onVerifyFailed.
272 * We use callback functions because verify may fetch information to check the signature.
Jeff Thompson29ce3102013-09-27 11:47:48 -0700273 * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding.
274 * To set the wireEncoding, you can call data.wireDecode.
275 * @param onVerified If the signature is verified, this calls onVerified(data).
276 * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data).
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700277 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700278 void
Jeff Thompson7c5d2312013-09-25 16:07:15 -0700279 verifyData
280 (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount = 0);
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700281
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700282 /*****************************************
283 * Encrypt/Decrypt *
284 *****************************************/
285
286 /**
287 * Generate a symmetric key.
288 * @param keyName The name of the generated key.
289 * @param keyType The type of the key, e.g. KEY_TYPE_AES
290 */
291 void
292 generateSymmetricKey(const Name& keyName, KeyType keyType)
293 {
294 encryptionManager_->createSymmetricKey(keyName, keyType);
295 }
296
297 /**
298 * Encrypt a byte array.
299 * @param keyName The name of the encrypting key.
300 * @param data The byte array that will be encrypted.
301 * @param dataLength The length of data.
302 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
303 * @param encryptMode the encryption mode
304 * @return the encrypted data as an immutable Blob.
305 */
306 Blob
307 encrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
308 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
309 {
310 return encryptionManager_->encrypt(keyName, data, dataLength, useSymmetric, encryptMode);
311 }
312
313 /**
314 * Decrypt a byte array.
315 * @param keyName The name of the decrypting key.
316 * @param data The byte array that will be decrypted.
317 * @param dataLength The length of data.
318 * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used.
319 * @param encryptMode the encryption mode
320 * @return the decrypted data as an immutable Blob.
321 */
322 Blob
323 decrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true,
324 EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT)
325 {
326 return encryptionManager_->decrypt(keyName, data, dataLength, useSymmetric, encryptMode);
327 }
328
Jeff Thompson8efe5ad2013-08-20 17:36:38 -0700329 /**
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700330 * Set the Face which will be used to fetch required certificates.
331 * @param face A pointer to the Face object.
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700332 */
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700333 void
334 setFace(Face* face) { face_ = face; }
335
336private:
Jeff Thompsoncda349e2013-11-05 17:37:39 -0800337 void
338 onCertificateData
339 (const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
340
341 void
342 onCertificateInterestTimeout
343 (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed,
344 const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep);
345
Jeff Thompson40f361a2013-09-25 13:12:48 -0700346 ptr_lib::shared_ptr<IdentityManager> identityManager_;
Jeff Thompson29ce3102013-09-27 11:47:48 -0700347 ptr_lib::shared_ptr<PolicyManager> policyManager_;
Jeff Thompson79a2d5d2013-09-27 14:32:23 -0700348 ptr_lib::shared_ptr<EncryptionManager> encryptionManager_;
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700349 Face* face_;
350 const int maxSteps_;
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700351};
352
353}
354
355#endif