Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_KEY_CHAIN_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_KEY_CHAIN_HPP |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 7a67cb6 | 2013-08-26 11:43:18 -0700 | [diff] [blame] | 11 | #include "../data.hpp" |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 12 | #include "../face.hpp" |
| 13 | #include "identity/identity-manager.hpp" |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 14 | #include "encryption/encryption-manager.hpp" |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 18 | class PolicyManager; |
| 19 | |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 20 | /** |
| 21 | * An OnVerified function object is used to pass a callback to verifyData to report a successful verification. |
| 22 | */ |
| 23 | typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerified; |
| 24 | |
| 25 | /** |
| 26 | * An OnVerifyFailed function object is used to pass a callback to verifyData to report a failed verification. |
| 27 | */ |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 28 | typedef func_lib::function<void(const ptr_lib::shared_ptr<Data>& data)> OnVerifyFailed; |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 29 | |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 30 | /** |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 31 | * Keychain is the main class of the security library. |
Jeff Thompson | ffa36f9 | 2013-09-20 08:42:41 -0700 | [diff] [blame] | 32 | * |
| 33 | * The Keychain class provides a set of interfaces to the security library such as identity management, policy configuration |
| 34 | * and packet signing and verification. |
| 35 | */ |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 36 | class KeyChain { |
| 37 | public: |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 38 | KeyChain |
| 39 | (const ptr_lib::shared_ptr<IdentityManager>& identityManager, const ptr_lib::shared_ptr<PolicyManager>& policyManager); |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 41 | /***************************************** |
| 42 | * Identity Management * |
| 43 | *****************************************/ |
| 44 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 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 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Get the default identity. |
| 58 | * @return The default identity name. |
| 59 | */ |
| 60 | Name |
| 61 | getDefaultIdentity() |
| 62 | { |
| 63 | return identityManager_->getDefaultIdentity(); |
| 64 | } |
| 65 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 66 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 67 | * Generate a pair of RSA keys for the specified identity. |
| 68 | * @param identityName The name of the identity. |
| 69 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 70 | * @param keySize The size of the key. |
| 71 | * @return The generated key name. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 72 | */ |
| 73 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 74 | generateRSAKeyPair(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 75 | { |
| 76 | return identityManager_->generateRSAKeyPair(identityName, isKsk, keySize); |
| 77 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 80 | * Set a key as the default key of an identity. |
| 81 | * @param keyName The name of the key. |
| 82 | * @param identityName the name of the identity. If not specified, the identity name is inferred from the keyName. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 83 | */ |
| 84 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 85 | setDefaultKeyForIdentity(const Name& keyName, const Name& identityName = Name()) |
| 86 | { |
| 87 | return identityManager_->setDefaultKeyForIdentity(keyName, identityName); |
| 88 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 91 | * Generate a pair of RSA keys for the specified identity and set it as default key for the identity. |
| 92 | * @param identityName The name of the identity. |
| 93 | * @param isKsk true for generating a Key-Signing-Key (KSK), false for a Data-Signing-Key (KSK). |
| 94 | * @param keySize The size of the key. |
| 95 | * @return The generated key name. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 96 | */ |
| 97 | Name |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 98 | generateRSAKeyPairAsDefault(const Name& identityName, bool isKsk = false, int keySize = 2048) |
| 99 | { |
| 100 | return identityManager_->generateRSAKeyPairAsDefault(identityName, isKsk, keySize); |
| 101 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 104 | * Create a public key signing request. |
| 105 | * @param keyName The name of the key. |
| 106 | * @returns The signing request data. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 107 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 108 | Blob |
| 109 | createSigningRequest(const Name& keyName) |
| 110 | { |
| 111 | return identityManager_->getPublicKey(keyName)->getKeyDer(); |
| 112 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Jeff Thompson | b63abf5 | 2013-10-04 11:23:34 -0700 | [diff] [blame] | 115 | * Install an identity certificate into the public key identity storage. |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 116 | * @param certificate The certificate to to added. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 117 | */ |
| 118 | void |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 119 | installIdentityCertificate(const IdentityCertificate& certificate) |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 120 | { |
| 121 | identityManager_->addCertificate(certificate); |
| 122 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 125 | * Set the certificate as the default for its corresponding key. |
| 126 | * @param certificateName The name of the certificate. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 127 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 128 | void |
| 129 | setDefaultCertificateForKey(const Name& certificateName) |
| 130 | { |
| 131 | identityManager_->setDefaultCertificateForKey(certificateName); |
| 132 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 135 | * Get a certificate with the specified name. |
| 136 | * @param certificateName The name of the requested certificate. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 137 | * @return the requested certificate which is valid. |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 138 | */ |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 139 | ptr_lib::shared_ptr<Certificate> |
| 140 | getCertificate(const Name& certificateName) |
| 141 | { |
| 142 | return identityManager_->getCertificate(certificateName); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get a certificate even if the certificate is not valid anymore. |
| 147 | * @param certificateName The name of the requested certificate. |
| 148 | * @return the requested certificate. |
| 149 | */ |
| 150 | ptr_lib::shared_ptr<Certificate> |
| 151 | getAnyCertificate(const Name& certificateName) |
| 152 | { |
| 153 | return identityManager_->getAnyCertificate(certificateName); |
| 154 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 157 | * Get an identity certificate with the specified name. |
| 158 | * @param certificateName The name of the requested certificate. |
| 159 | * @return the requested certificate which is valid. |
| 160 | */ |
| 161 | ptr_lib::shared_ptr<IdentityCertificate> |
| 162 | getIdentityCertificate(const Name& certificateName) |
| 163 | { |
| 164 | return identityManager_->getCertificate(certificateName); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get an identity certificate even if the certificate is not valid anymore. |
| 169 | * @param certificateName The name of the requested certificate. |
| 170 | * @return the requested certificate. |
| 171 | */ |
| 172 | ptr_lib::shared_ptr<IdentityCertificate> |
| 173 | getAnyIdentityCertificate(const Name& certificateName) |
| 174 | { |
| 175 | return identityManager_->getAnyCertificate(certificateName); |
| 176 | } |
| 177 | |
| 178 | /** |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 179 | * Revoke a key |
| 180 | * @param keyName the name of the key that will be revoked |
| 181 | */ |
| 182 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 183 | revokeKey(const Name & keyName) |
| 184 | { |
| 185 | //TODO: Implement |
| 186 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Revoke a certificate |
| 190 | * @param certificateName the name of the certificate that will be revoked |
| 191 | */ |
| 192 | void |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 193 | revokeCertificate(const Name & certificateName) |
| 194 | { |
| 195 | //TODO: Implement |
| 196 | } |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 197 | |
| 198 | /***************************************** |
| 199 | * Policy Management * |
| 200 | *****************************************/ |
| 201 | |
| 202 | const ptr_lib::shared_ptr<PolicyManager>& |
| 203 | getPolicyManager() { return policyManager_; } |
| 204 | |
| 205 | /***************************************** |
| 206 | * Sign/Verify * |
| 207 | *****************************************/ |
| 208 | |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 209 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 210 | * Wire encode the Data object, sign it and set its signature. |
Jeff Thompson | ade5b1e | 2013-08-09 12:16:45 -0700 | [diff] [blame] | 211 | * Note: the caller must make sure the timestamp is correct, for example with |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 212 | * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0). |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 213 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
Jeff Thompson | 9296f0c | 2013-09-23 18:10:27 -0700 | [diff] [blame] | 214 | * @param certificateName The certificate name of the key to use for signing. If omitted, infer the signing identity from the data packet name. |
Jeff Thompson | 8d24fe1 | 2013-09-18 15:54:51 -0700 | [diff] [blame] | 215 | * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat(). |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 216 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 217 | void |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 218 | sign(Data& data, const Name& certificateName, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 219 | |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Wire encode the Data object, sign it and set its signature. |
| 222 | * Note: the caller must make sure the timestamp is correct, for example with |
| 223 | * data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0). |
| 224 | * @param data The Data object to be signed. This updates its signature and key locator field and wireEncoding. |
| 225 | * @param identityName The identity name for the key to use for signing. If omitted, infer the signing identity from the data packet name. |
| 226 | * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat(). |
| 227 | */ |
| 228 | void |
| 229 | signByIdentity(Data& data, const Name& identityName = Name(), WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 3c73da4 | 2013-08-12 11:19:05 -0700 | [diff] [blame] | 230 | |
| 231 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 232 | * Check the signature on the Data object and call either onVerify or onVerifyFailed. |
| 233 | * We use callback functions because verify may fetch information to check the signature. |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 234 | * @param data The Data object with the signature to check. It is an error if data does not have a wireEncoding. |
| 235 | * To set the wireEncoding, you can call data.wireDecode. |
| 236 | * @param onVerified If the signature is verified, this calls onVerified(data). |
| 237 | * @param onVerifyFailed If the signature check fails, this calls onVerifyFailed(data). |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 238 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 239 | void |
Jeff Thompson | 7c5d231 | 2013-09-25 16:07:15 -0700 | [diff] [blame] | 240 | verifyData |
| 241 | (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount = 0); |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 242 | |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 243 | /***************************************** |
| 244 | * Encrypt/Decrypt * |
| 245 | *****************************************/ |
| 246 | |
| 247 | /** |
| 248 | * Generate a symmetric key. |
| 249 | * @param keyName The name of the generated key. |
| 250 | * @param keyType The type of the key, e.g. KEY_TYPE_AES |
| 251 | */ |
| 252 | void |
| 253 | generateSymmetricKey(const Name& keyName, KeyType keyType) |
| 254 | { |
| 255 | encryptionManager_->createSymmetricKey(keyName, keyType); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Encrypt a byte array. |
| 260 | * @param keyName The name of the encrypting key. |
| 261 | * @param data The byte array that will be encrypted. |
| 262 | * @param dataLength The length of data. |
| 263 | * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used. |
| 264 | * @param encryptMode the encryption mode |
| 265 | * @return the encrypted data as an immutable Blob. |
| 266 | */ |
| 267 | Blob |
| 268 | encrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true, |
| 269 | EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT) |
| 270 | { |
| 271 | return encryptionManager_->encrypt(keyName, data, dataLength, useSymmetric, encryptMode); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Decrypt a byte array. |
| 276 | * @param keyName The name of the decrypting key. |
| 277 | * @param data The byte array that will be decrypted. |
| 278 | * @param dataLength The length of data. |
| 279 | * @param useSymmetric If true then symmetric encryption is used, otherwise asymmetric encryption is used. |
| 280 | * @param encryptMode the encryption mode |
| 281 | * @return the decrypted data as an immutable Blob. |
| 282 | */ |
| 283 | Blob |
| 284 | decrypt(const Name &keyName, const uint8_t* data, size_t dataLength, bool useSymmetric = true, |
| 285 | EncryptMode encryptMode = ENCRYPT_MODE_DEFAULT) |
| 286 | { |
| 287 | return encryptionManager_->decrypt(keyName, data, dataLength, useSymmetric, encryptMode); |
| 288 | } |
| 289 | |
Jeff Thompson | 8efe5ad | 2013-08-20 17:36:38 -0700 | [diff] [blame] | 290 | /** |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 291 | * Set the Face which will be used to fetch required certificates. |
| 292 | * @param face A pointer to the Face object. |
Jeff Thompson | 1e90d8c | 2013-08-12 16:09:25 -0700 | [diff] [blame] | 293 | */ |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 294 | void |
| 295 | setFace(Face* face) { face_ = face; } |
| 296 | |
| 297 | private: |
Jeff Thompson | 40f361a | 2013-09-25 13:12:48 -0700 | [diff] [blame] | 298 | ptr_lib::shared_ptr<IdentityManager> identityManager_; |
Jeff Thompson | 29ce310 | 2013-09-27 11:47:48 -0700 | [diff] [blame] | 299 | ptr_lib::shared_ptr<PolicyManager> policyManager_; |
Jeff Thompson | 79a2d5d | 2013-09-27 14:32:23 -0700 | [diff] [blame] | 300 | ptr_lib::shared_ptr<EncryptionManager> encryptionManager_; |
Jeff Thompson | 2ce8f49 | 2013-09-17 18:01:25 -0700 | [diff] [blame] | 301 | Face* face_; |
| 302 | const int maxSteps_; |
Jeff Thompson | 47c93cf | 2013-08-09 00:38:48 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | #endif |